Created
May 29, 2013 11:01
-
-
Save LoicMahieu/5669496 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
// OUTPUT: | |
// Executing: DROP TABLE IF EXISTS `Persons`; | |
// Executing: CREATE TABLE IF NOT EXISTS `Persons` (`name` VARCHAR(255), `id` INTEGER NOT NULL auto_increment , `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB; | |
// Executing: DROP TABLE IF EXISTS `Pets`; | |
// Executing: CREATE TABLE IF NOT EXISTS `Pets` (`name` VARCHAR(255), `id` INTEGER NOT NULL auto_increment , `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, `PersonId` INTEGER, PRIMARY KEY (`id`)) ENGINE=InnoDB; | |
// Executing: INSERT INTO `Persons` (`name`,`id`,`createdAt`,`updatedAt`) VALUES ('Luke',NULL,'2013-05-29 11:00:37','2013-05-29 11:00:40'); | |
// Executing: INSERT INTO `Pets` (`name`,`id`,`createdAt`,`updatedAt`,`PersonId`) VALUES ('Bob',NULL,'2013-05-29 11:00:37','2013-05-29 11:00:40',NULL); | |
// Executing: SELECT * FROM `Pets` WHERE `Pets`.`PersonId`=1; | |
// Executing: UPDATE `Pets` SET `name`='Bob',`id`=1,`createdAt`='2013-05-29 11:00:37',`updatedAt`='2013-05-29 11:00:40',`PersonId`=1 WHERE `id`=1 | |
// Executing: SELECT * FROM `Pets` WHERE `Pets`.`PersonId`=1; | |
// my pets: Bob | |
var Sequelize = require(__dirname + "/../../index") | |
, config = require(__dirname + "/../../spec/config/config") | |
, sequelize = new Sequelize(config.database, config.username, config.password, {logging: true}) | |
, Person = sequelize.define('Person', { | |
name: Sequelize.STRING | |
}) | |
, Pet = sequelize.define('Pet', { name: Sequelize.STRING }) | |
Person.hasMany(Pet) | |
var chainer = new Sequelize.Utils.QueryChainer | |
, person = Person.build({ name: 'Luke' }) | |
, pet = Pet.build({ name: 'Bob' }) | |
sequelize.sync({force:true}).on('success', function() { | |
chainer | |
.add(person.save()) | |
.add(pet.save()) | |
chainer.run().on('success', function() { | |
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) | |
}) | |
}) |
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
// OUTPUT: | |
// Executing: DROP TABLE IF EXISTS `Persons`; | |
// Executing: CREATE TABLE IF NOT EXISTS `Persons` (`name` VARCHAR(255), `user_id` INTEGER auto_increment , `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (`user_id`)) ENGINE=InnoDB; | |
// Executing: DROP TABLE IF EXISTS `Pets`; | |
// Executing: CREATE TABLE IF NOT EXISTS `Pets` (`name` VARCHAR(255), `id` INTEGER NOT NULL auto_increment , `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, `PersonId` INTEGER, PRIMARY KEY (`id`)) ENGINE=InnoDB; | |
// Executing: INSERT INTO `Persons` (`name`,`user_id`,`createdAt`,`updatedAt`) VALUES ('Luke',NULL,'2013-05-29 10:57:58','2013-05-29 10:57:58'); | |
// Executing: INSERT INTO `Pets` (`name`,`id`,`createdAt`,`updatedAt`,`PersonId`) VALUES ('Bob',NULL,'2013-05-29 10:57:58','2013-05-29 10:57:58',NULL); | |
// Executing: SELECT * FROM `Pets` WHERE `Pets`.`PersonId` IS NULL; | |
// Executing: SELECT * FROM `Pets` WHERE `Pets`.`PersonId` IS NULL; | |
// my pets: Bob | |
var Sequelize = require(__dirname + "/../../index") | |
, config = require(__dirname + "/../../spec/config/config") | |
, sequelize = new Sequelize(config.database, config.username, config.password, {logging: true}) | |
, Person = sequelize.define('Person', { | |
name: Sequelize.STRING, | |
user_id: { | |
type: Sequelize.INTEGER, | |
primaryKey: true, | |
autoIncrement: true | |
} | |
}) | |
, Pet = sequelize.define('Pet', { name: Sequelize.STRING }) | |
Person.hasMany(Pet) | |
var chainer = new Sequelize.Utils.QueryChainer | |
, person = Person.build({ name: 'Luke' }) | |
, pet = Pet.build({ name: 'Bob' }) | |
sequelize.sync({force:true}).on('success', function() { | |
chainer | |
.add(person.save()) | |
.add(pet.save()) | |
chainer.run().on('success', function() { | |
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) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment