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

Commit 7aebe2c6 by Sascha Depold

fixed example

1 parent 8347de69
Showing with 46 additions and 27 deletions
...@@ -8,34 +8,52 @@ ...@@ -8,34 +8,52 @@
The rest of the example is about setting and getting the associated data. The rest of the example is about setting and getting the associated data.
*/ */
var Sequelize = require(__dirname + "/../../lib/sequelize/Sequelize").Sequelize, var Sequelize = require(__dirname + "/../../index")
sequelize = new Sequelize("sequelize_test", "root", null, {disableLogging: true}), , config = require(__dirname + "/../../test/config")
Person = sequelize.define('person', { , sequelize = new Sequelize(config.database, config.username, config.password, {logging: false})
name: Sequelize.STRING , Person = sequelize.define('Person', { name: Sequelize.STRING })
}), , Pet = sequelize.define('Pet', { name: Sequelize.STRING })
Pet = sequelize.define('pet', {
name: Sequelize.STRING
})
Person.hasMany('brothers') Person.hasMany(Person, {as: 'Brothers'})
Person.hasMany('sisters') Person.hasMany(Person, {as: 'Sisters'})
Person.hasOneAndBelongsTo('father', Person) Person.hasOne(Person, {as: 'Father', foreignKey: 'FatherId'})
Person.hasOneAndBelongsTo('mother', Person) Person.hasOne(Person, {as: 'Mother', foreignKey: 'MotherId'})
Person.hasManyAndBelongsTo('pets', Pet, 'owner') Person.hasMany(Pet)
Sequelize.chainQueries([{drop: sequelize}, {sync: sequelize}], function() { var chainer = new Sequelize.Utils.QueryChainer
var person = new Person({ name: 'Luke' }), , person = Person.build({ name: 'Luke' })
mother = new Person({ name: 'Jane' }), , mother = Person.build({ name: 'Jane' })
father = new Person({ name: 'John' }), , father = Person.build({ name: 'John' })
brother = new Person({ name: 'Brother' }), , brother = Person.build({ name: 'Brother' })
sister = new Person({ name: 'Sister' }), , sister = Person.build({ name: 'Sister' })
pet = new Pet({ name: 'Bob' }) , pet = Pet.build({ name: 'Bob' })
Sequelize.chainQueries({save: [person, mother, father, brother, sister, pet]}, function() { sequelize.sync({force:true}).on('success', function() {
person.setMother(mother, function(mom) { Sequelize.Helper.log('my mom: ' + mom.name) }) chainer
person.setFather(father, function(dad) { Sequelize.Helper.log('my dad: ' + dad.name) }) .add(person.save())
person.setBrothers([brother], function(bros) { Sequelize.Helper.log("ma bro: " + bros[0].name)}) .add(mother.save())
person.setSisters([sister], function(sis) { Sequelize.Helper.log("ma sis: " + sis[0].name)}) .add(father.save())
person.setPets([pet], function(pets) { Sequelize.Helper.log('my pet: ' + pets[0].name )}) .add(brother.save())
.add(sister.save())
.add(pet.save())
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!