Last active
December 29, 2015 05:09
-
-
Save jack-wong-build/7619585 to your computer and use it in GitHub Desktop.
JugglingDB with Postgres
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
var q = require('q'); | |
var Schema = require('jugglingdb').Schema; | |
var schema = new Schema('postgres', { | |
database: 'woonketwong', | |
username: 'woonketwong' | |
}); | |
// The first argument to schema.define is the table | |
// and schema name. Do not use any capital letter | |
// in the table name because the database create table | |
// in low case letters. | |
var Computer = schema.define("computer", { | |
name: {type: String}, | |
email: {type: String}, | |
}); | |
// The schema.autoupdate function is asynchronous | |
// If you start saving to a table before it is created, | |
// postgres throws error. | |
// Therefore, I created this updateSchema function with | |
// q and promise to make sure accesses to table | |
// doesn't happen until it is created | |
var updateSchema = function(){ | |
var deferred = q.defer(); | |
console.log("updating schema"); | |
schema.autoupdate(function(msg){ | |
console.log("*** db schema update is done", msg) | |
deferred.resolve('deferred resolved!!'); | |
}); | |
return deferred.promise; | |
} | |
// call updateSchema, and write to the table | |
updateSchema().then(function(){ | |
var computer = new Computer({ | |
name: "William", | |
email: "[email protected]" | |
}); | |
computer.save(function (err) { | |
if (err) { | |
console.log("ERROR in writing to table!!!"); | |
console.log("ERROR:",err); | |
} | |
console.log("**end**"); | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment