Skip to content

Instantly share code, notes, and snippets.

@mrotaru
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrotaru/0155e3b3cbb49ba4774f to your computer and use it in GitHub Desktop.
Save mrotaru/0155e3b3cbb49ba4774f to your computer and use it in GitHub Desktop.
var Sequelize = require('sequelize');
var sequelize = new Sequelize('db','db_user','password');
var chainer = new Sequelize.Utils.QueryChainer
// define models
var User = sequelize.define('User', {name: Sequelize.STRING})
// build instances
var joe = User.build({name: 'Joe'});
var jane = User.build({name: 'Jane'});
// associations
// User.hasMany(User, {as: 'Children'}); // will NOT create a junction table
User.hasMany(User, {as: 'Children', through: 'Children'}); // will create a `Children` junction table
sequelize.sync().success(function(){
chainer
.add(joe.save())
.add(jane.save())
.add(joe.setChildren([jane]))
.runSerially()
.success(function(){ console.log('all good');})
.error(function(err){ console.log(err);});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment