Created
May 26, 2012 19:16
-
-
Save bjpirt/2795013 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env node | |
var Backbone = require('backbone-postgresql'); | |
// Configure our connection to PostgreSQL | |
Backbone.pg_connector.config = {db: 'pg://postgres:m0nday27@localhost/bb_pg_demo'} | |
// Define some models | |
var Category = Backbone.Model.extend({ | |
urlRoot: 'category' | |
}); | |
var Product = Backbone.Model.extend({ | |
urlRoot: 'product' | |
}); | |
// Make a new category | |
var cat = new Category({name: 'cat1', description: 'This is the first category'}); | |
cat.save(null, { | |
success: function(){ | |
console.log("cat was saved!"); | |
console.log(cat.attributes); | |
// We succeeded, so let's relate a product to this category | |
var prod = new Product({category_id: cat.id, name: 'Widget One', price: 123.45, description: "A really super widget"}); | |
prod.save(null, { | |
success: function(){ | |
console.log("product was saved!"); | |
console.log(prod.attributes); | |
// Now let's take a look what was actually stored in PostgreSQL | |
Backbone.pg_connector.connect(function(err, client){ | |
client.query('SELECT * FROM product WHERE id = $1', [prod.id], function(err, res){ | |
console.log(res); | |
process.exit(); | |
}); | |
}); | |
}, | |
error: function(model, err){ console.log("Error: " + err.message) } | |
}); | |
}, | |
error: function(model, err){ console.log("Error: " + err.message) } | |
}); |
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
cat was saved! | |
{ name: 'cat1', | |
description: 'This is the first category', | |
id: 1 } | |
product was saved! | |
{ category_id: 1, | |
name: 'Widget One', | |
price: '123.45', | |
description: 'A really super widget', | |
id: 1 } | |
{ rows: | |
[ { id: 1, | |
category_id: 1, | |
name: 'Widget One', | |
attributes: '"price"=>"123.45", "description"=>"A really super widget"' } ] } | |
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
{ | |
"name" : "backbone-postgresql-demo", | |
"version" : "0.0.1", | |
"description" : "An demo of the backbone-postgresql module", | |
"dependencies" : { | |
"backbone-postgresql":"0.0.1" | |
} | |
} |
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
-- This first command needs to be done by an admin | |
CREATE EXTENSION hstore; | |
CREATE TABLE category ( | |
id SERIAL PRIMARY KEY, | |
name VARCHAR(128), | |
description TEXT | |
); | |
CREATE TABLE product ( | |
id SERIAL, | |
category_id INTEGER REFERENCES category(id), | |
name VARCHAR(128), | |
attributes hstore | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment