不要怂,就是干,撸起袖子干!

Commit 7aebe2c6 by Sascha Depold

fixed example

1 parent 8347de69
Showing with 45 additions and 26 deletions
......@@ -8,34 +8,52 @@
The rest of the example is about setting and getting the associated data.
*/
var Sequelize = require(__dirname + "/../../lib/sequelize/Sequelize").Sequelize,
sequelize = new Sequelize("sequelize_test", "root", null, {disableLogging: true}),
Person = sequelize.define('person', {
name: Sequelize.STRING
}),
Pet = sequelize.define('pet', {
name: Sequelize.STRING
})
var Sequelize = require(__dirname + "/../../index")
, config = require(__dirname + "/../../test/config")
, sequelize = new Sequelize(config.database, config.username, config.password, {logging: false})
, Person = sequelize.define('Person', { name: Sequelize.STRING })
, Pet = sequelize.define('Pet', { name: Sequelize.STRING })
Person.hasMany(Person, {as: 'Brothers'})
Person.hasMany(Person, {as: 'Sisters'})
Person.hasOne(Person, {as: 'Father', foreignKey: 'FatherId'})
Person.hasOne(Person, {as: 'Mother', foreignKey: 'MotherId'})
Person.hasMany(Pet)
Person.hasMany('brothers')
Person.hasMany('sisters')
Person.hasOneAndBelongsTo('father', Person)
Person.hasOneAndBelongsTo('mother', Person)
Person.hasManyAndBelongsTo('pets', Pet, 'owner')
var chainer = new Sequelize.Utils.QueryChainer
, person = Person.build({ name: 'Luke' })
, mother = Person.build({ name: 'Jane' })
, father = Person.build({ name: 'John' })
, brother = Person.build({ name: 'Brother' })
, sister = Person.build({ name: 'Sister' })
, pet = Pet.build({ name: 'Bob' })
Sequelize.chainQueries([{drop: sequelize}, {sync: sequelize}], function() {
var person = new Person({ name: 'Luke' }),
mother = new Person({ name: 'Jane' }),
father = new Person({ name: 'John' }),
brother = new Person({ name: 'Brother' }),
sister = new Person({ name: 'Sister' }),
pet = new Pet({ name: 'Bob' })
sequelize.sync({force:true}).on('success', function() {
chainer
.add(person.save())
.add(mother.save())
.add(father.save())
.add(brother.save())
.add(sister.save())
.add(pet.save())
Sequelize.chainQueries({save: [person, mother, father, brother, sister, pet]}, function() {
person.setMother(mother, function(mom) { Sequelize.Helper.log('my mom: ' + mom.name) })
person.setFather(father, function(dad) { Sequelize.Helper.log('my dad: ' + dad.name) })
person.setBrothers([brother], function(bros) { Sequelize.Helper.log("ma bro: " + bros[0].name)})
person.setSisters([sister], function(sis) { Sequelize.Helper.log("ma sis: " + sis[0].name)})
person.setPets([pet], function(pets) { Sequelize.Helper.log('my pet: ' + pets[0].name )})
chainer.run().on('success', function() {
person.setMother(mother).on('success', function() { person.getMother().on('success', function(mom) {
console.log('my mom: ', mom.name)
})})
person.setFather(father).on('success', function() { person.getFather().on('success', function(dad) {
console.log('my dad: ', dad.name)
})})
person.setBrothers([brother]).on('success', function() { person.getBrothers().on('success', function(brothers) {
console.log("my brothers: " + brothers.map(function(b) { return b.name }))
})})
person.setSisters([sister]).on('success', function() { person.getSisters().on('success', function(sisters) {
console.log("my sisters: " + sisters.map(function(s) { return s.name }))
})})
person.setPets([pet]).on('success', function() { person.getPets().on('success', function(pets) {
console.log("my pets: " + pets.map(function(p) { return p.name }))
})})
}).on('failure', function(err) {
console.log(err)
})
})
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!