Created
December 12, 2011 18:55
-
-
Save sdepold/1468569 to your computer and use it in GitHub Desktop.
Example for connector models in sequelize
This file contains hidden or 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 Sequelize = require("sequelize") | |
, sequelize = new Sequelize('sequelize_test', 'root', null, {logging: false}) | |
var User = sequelize.define('User', { username: Sequelize.STRING }) | |
, Product = sequelize.define('Product', { title: Sequelize.STRING }) | |
, Purchase = sequelize.define('Purchase', { comment: Sequelize.TEXT }) | |
User.hasMany(Purchase) | |
Purchase.belongsTo(Product) | |
Purchase.belongsTo(User) | |
// don't use force if you are in production! | |
sequelize.sync({ force: true }).on('success', function() { | |
var create = function(callback) { | |
User.create({username: 'sdepold'}).on('success', function(sdepold) { | |
Product.create({title: 'football'}).on('success', function(football) { | |
Purchase.create({comment: 'can i have the ball in green?'}).on('success', function(purchase) { | |
purchase.setUser(sdepold).on('success', function() { | |
purchase.setProduct(football).on('success', function(){ | |
callback && callback(purchase) | |
}) | |
}) | |
}) | |
}) | |
}) | |
} | |
var read = function(purchase) { | |
console.log('The purchase:') | |
console.log(purchase) | |
console.log() | |
purchase.getProduct().on('success', function(product) { | |
console.log('The product:') | |
console.log(product) | |
console.log() | |
purchase.getUser().on('success', function(user) { | |
console.log('The user:') | |
console.log(user) | |
console.log() | |
}) | |
}) | |
} | |
create(read) | |
}).on('failure', function(err) { | |
console.log(err) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment