Created
March 11, 2015 20:39
-
-
Save mbroadst/d3bf05286d2a85bb28e5 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
var Sequelize = require('sequelize'), | |
DataTypes = Sequelize, | |
sequelize = new Sequelize('groups_test', 'root', 'password'); | |
var Device = sequelize.define('Device', { | |
name: DataTypes.STRING | |
}, { | |
underscored: true, | |
timestamps: false | |
}); | |
var Group = sequelize.define('Group', { | |
name: DataTypes.STRING | |
}, { | |
underscored: true, | |
timestamps: false | |
}); | |
Group.belongsToMany(Device); | |
Device.belongsToMany(Group); | |
sequelize.sync({force: true}) | |
.then(function() { | |
return Promise.all([ | |
Device.create({ name: 'device1' }), | |
Device.create({ name: 'device2' }), | |
Group.create({ name: 'group1' }), | |
Group.create({ name: 'group2' }), | |
Group.create({ name: 'group3' }) | |
]); | |
}) | |
.spread(function(device1, device2, group1, group2, group3) { | |
return Promise.all([ | |
device1.setGroups([group1, group2, group3]), | |
device2.setGroups([group3]) | |
]); | |
}) | |
.then(function() { | |
return Device.find({ | |
include: [ | |
{ model: Group, where: { name: 'group2' } } | |
] | |
}); | |
}) | |
.then(function(devices) { | |
console.log(devices); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment