Last active
August 29, 2015 14:16
-
-
Save mbroadst/429a5c00a43f6e4c80b0 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 | |
}); | |
Device.hasMany(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({ | |
where: { | |
group: { | |
name: 'somename' | |
} | |
} | |
}, { | |
include: [ Group ] | |
}); | |
}) | |
.then(function(devices) { | |
console.log(devices); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment