Skip to content

Instantly share code, notes, and snippets.

@woonketwong
Last active December 29, 2015 05:09
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woonketwong/7619585 to your computer and use it in GitHub Desktop.
Save woonketwong/7619585 to your computer and use it in GitHub Desktop.
JugglingDB with Postgres
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: "William@hotmail.com"
});
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