Last active
December 16, 2015 21:21
-
-
Save MalucoMarinero/5499229 to your computer and use it in GitHub Desktop.
Early API for a client database abstraction I'm writing, taking cues from the way Django's ORM works.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Start a new database with name "testDB" and version 20 | |
window.db = new JohoDB "testDB", 20 | |
# Define the database schema, early days so the relations don't do anything yet, but I'm getting there. | |
db.addSchema 'Household', { | |
id: {type: "int", primaryKey: true, autoIncrement: true} | |
address: {type: "string", required: true} | |
suburb: {type: "string", required: true} | |
postCode: {type: "string", required: true} | |
} | |
db.addSchema 'People', { | |
id: {type: "int", primaryKey: true, autoIncrement: true} | |
firstName: {type: "string", required: true} | |
lastName: {type: "string", required: true} | |
dateOfBirth: {type: "datetime", required: true} | |
home: {type: "fk", relation: "Household", relatedName: "occupants"} | |
bio: {type: "string"} | |
pets: {type: "m2m", relation: "Pets", relatedName: "owners"} | |
} | |
db.addSchema 'Pets', { | |
id: {type: "int", primaryKey: true, autoIncrement: true} | |
name: {type: "string", required: true} | |
age: {type: "int"} | |
} | |
# Create the database as an IndexedDB, (webSQL is in the plans too), clobber option means it will just destroy whatever is there, for testing | |
db.initDB "indexedDb", clobber: true | |
# Creating an array of functions to make the execution of promises sequential. | |
# Each save action, when done without the primary key, will automatically find the nnext available key. Of course this only works because | |
# the actions are being done sequentially via promises, otherwise it would be a mess. I believe | |
# in Async often the better solution will be to use a hashed string as an ID, with enough seeding to avoid collisions. | |
saves = [ | |
-> | |
db.models.Household.save { | |
address: "8/17 Churchill Ave" | |
suburb: "Sandy Bay" | |
postCode: "7005" | |
} | |
-> | |
db.models.Household.save { | |
address: "9 Monterey Sq" | |
suburb: "Kingston" | |
postCode: "7532" | |
} | |
-> | |
db.models.Household.save { | |
address: "11 Malamar Place" | |
suburb: "Glenorchy" | |
postCode: "7023" | |
} | |
-> | |
db.models.People.save { | |
firstName: "James" | |
lastName: "Rakich" | |
dateOfBirth: new Date(1925, 10, 8) | |
} | |
-> | |
db.models.People.save { | |
firstName: "Joe" | |
lastName: "Bloggs" | |
dateOfBirth: new Date(1942, 2, 1) | |
bio: "Top bloke here." | |
} | |
-> | |
db.models.Household.query().all().evaluate() | |
(result) -> | |
console.log 'Household.query().all()', result | |
-> | |
db.models.Household.query().filter({postCode__gte: "7005"}).evaluate() | |
(result) -> | |
console.log 'Household.query().filter({postCode__gte: "7005"})', result | |
-> | |
db.models.Household.query().filter({postCode__gt: "7005"}).evaluate() | |
(result) -> | |
console.log 'Household.query().filter({postCode__gt: "7005"})', result | |
-> | |
db.models.Household.query().filter({postCode__gt: "7005"}) | |
.exclude({suburb: "Kingston"}).evaluate() | |
(result) -> | |
console.log 'Household.query().filter({postCode__gt: "7005"}).exclude({suburb: "Kingston"})', result | |
] | |
# Iterating over the array here. | |
result = Q.resolve(true) | |
for save in saves | |
result = result.then save |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment