Skip to content
Browse files

fixed example

  • Loading branch information...
1 parent 8347de6 commit 7aebe2c6ed0efc410bf5ecabcb6efbe31b087665 @sdepold committed May 9, 2011
Showing with 45 additions and 27 deletions.
  1. +45 −27 examples/Associations/app.js
View
72 examples/Associations/app.js
@@ -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('brothers')
-Person.hasMany('sisters')
-Person.hasOneAndBelongsTo('father', Person)
-Person.hasOneAndBelongsTo('mother', Person)
-Person.hasManyAndBelongsTo('pets', Pet, 'owner')
+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)
-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.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 )})
+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.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())
+
+ 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)
})
})

0 comments on commit 7aebe2c

Please sign in to comment.
Something went wrong with that request. Please try again.