Last active
August 29, 2015 14:17
-
-
Save couto/c33121c50adfa98ec4dc to your computer and use it in GitHub Desktop.
Save the credits from a movie.
This file contains hidden or 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 Promise = require('bluebird'), | |
Sequelize = require('sequelize'), | |
sequelize = new Sequelize('database', 'username', 'password', { | |
host: 'localhost', | |
dialect: 'sqlite' | |
}); | |
/* | |
* Quick explanation: | |
* A movie has a lot of credits (jon doe is the director, jane doe is the photographer, peter doe is the hair dresser...) | |
* So a credit is always the pair of role + person. | |
* A person can do multiple roles, example: | |
* Jon Doe might be director AND manager on a movie, but hair dresser in another movie | |
*/ | |
var Person = sequelize.define('Person', { | |
id: { | |
primaryKey: true, | |
allowNull: false, | |
unique: true, | |
type: Sequelize.UUID, | |
defaultValue: Sequelize.UUIDV4 | |
}, | |
name: { | |
allowNull: false, | |
type: Sequelize.STRING | |
} | |
}); | |
var Role = sequelize.define('Role', { | |
id: { | |
primaryKey: true, | |
allowNull: false, | |
unique: true, | |
type: Sequelize.UUID, | |
defaultValue: Sequelize.UUIDV4 | |
}, | |
name: { | |
allowNull: false, | |
type: Sequelize.STRING | |
} | |
}); | |
var Credit = sequelize.define('Credit', { | |
id: { | |
primaryKey: true, | |
allowNull: false, | |
unique: true, | |
type: Sequelize.UUID, | |
defaultValue: Sequelize.UUIDV4 | |
}, | |
PersonId: { | |
unique: 'composite', | |
allowNull: false, | |
type: Sequelize.UUID | |
}, | |
RoleId: { | |
unique: 'composite', | |
allowNull: false, | |
type: Sequelize.UUID | |
} | |
}); | |
var Movie = sequelize.define('Movie', { | |
id: { | |
primaryKey: true, | |
allowNull: false, | |
unique: true, | |
type: Sequelize.UUID, | |
defaultValue: Sequelize.UUIDV4 | |
}, | |
name: { | |
allowNull: false, | |
type: Sequelize.STRING | |
} | |
}); | |
Movie.belongsToMany(Credit, { through: 'MovieCredits', allowNull: false }); | |
Credit.belongsToMany(Movie, { through: 'MovieCredits', allowNull: false }); | |
Credit.belongsTo(Person); | |
Credit.belongsTo(Role); | |
sequelize | |
.sync() | |
.then(function (person, role) { | |
var credit = Credit.build({ | |
Person: { name: 'John Doe' }, | |
Role: { name: 'Photographer' } | |
}, { | |
include: [Person, Role] | |
}); | |
return credit.save(); | |
}) | |
.then(function (credit) { | |
var movie = Movie.build({ name: 'Fight Club' }); | |
movie.addCredit(credit, { save: false }); | |
return movie.save(); | |
}) | |
.then(function (movie) { | |
// Fetch the movie just to check | |
// what was saved in the database | |
return Movie.find({ | |
where: { id : movie.getDataValue('id') }, | |
include: [{ | |
model: Credit, | |
include: [Person, Role] | |
}] | |
}); | |
}) | |
.then(function (movie) { | |
console.log(JSON.stringify(movie.toJSON(), null, ' ')); | |
}) | |
.catch(function (err) { | |
console.log(err); | |
console.log(err.stack); | |
}); |
This file contains hidden or 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
Executing (default): CREATE TABLE IF NOT EXISTS `People` (`id` UUID NOT NULL UNIQUE PRIMARY KEY, `name` VARCHAR(255) NOT NULL, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL); | |
Executing (default): PRAGMA INDEX_LIST(`People`) | |
Executing (default): PRAGMA INDEX_INFO(`sqlite_autoindex_People_1`) | |
Executing (default): CREATE TABLE IF NOT EXISTS `Roles` (`id` UUID NOT NULL UNIQUE PRIMARY KEY, `name` VARCHAR(255) NOT NULL, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL); | |
Executing (default): PRAGMA INDEX_LIST(`Roles`) | |
Executing (default): PRAGMA INDEX_INFO(`sqlite_autoindex_Roles_1`) | |
Executing (default): CREATE TABLE IF NOT EXISTS `Credits` (`id` UUID NOT NULL UNIQUE PRIMARY KEY, `PersonId` UUID NOT NULL REFERENCES `People` (`id`) ON DELETE SET NULL ON UPDATE CASCADE, `RoleId` UUID NOT NULL REFERENCES `Roles` (`id`) ON DELETE SET NULL ON UPDATE CASCAD | |
E, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, UNIQUE (PersonId, RoleId)); | |
Executing (default): PRAGMA INDEX_LIST(`Credits`) | |
Executing (default): PRAGMA INDEX_INFO(`sqlite_autoindex_Credits_1`) | |
Executing (default): PRAGMA INDEX_INFO(`sqlite_autoindex_Credits_2`) | |
Executing (default): CREATE TABLE IF NOT EXISTS `Movies` (`id` UUID NOT NULL UNIQUE PRIMARY KEY, `name` VARCHAR(255) NOT NULL, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL); | |
Executing (default): PRAGMA INDEX_LIST(`Movies`) | |
Executing (default): PRAGMA INDEX_INFO(`sqlite_autoindex_Movies_1`) | |
Executing (default): CREATE TABLE IF NOT EXISTS `MovieCredits` (`createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, `CreditId` UUID NOT NULL REFERENCES `Credits` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, `MovieId` UUID NOT NULL REFERENCES `Movies` (`id`), PRIM | |
ARY KEY (`CreditId`, `MovieId`)); | |
Executing (default): PRAGMA INDEX_LIST(`MovieCredits`) | |
Executing (default): PRAGMA INDEX_INFO(`sqlite_autoindex_MovieCredits_1`) | |
{ [SequelizeValidationError: notNull Violation: PersonId cannot be null, | |
notNull Violation: RoleId cannot be null] | |
name: 'SequelizeValidationError', | |
message: 'notNull Violation: PersonId cannot be null,\nnotNull Violation: RoleId cannot be null', | |
errors: | |
[ { message: 'PersonId cannot be null', | |
type: 'notNull Violation', | |
path: 'PersonId', | |
value: null }, | |
{ message: 'RoleId cannot be null', | |
type: 'notNull Violation', | |
path: 'RoleId', | |
value: null } ] } | |
SequelizeValidationError: notNull Violation: PersonId cannot be null, | |
notNull Violation: RoleId cannot be null | |
at /private/tmp/association/node_modules/sequelize/lib/instance-validator.js:153:14 | |
at tryCatch1 (/private/tmp/association/node_modules/sequelize/node_modules/bluebird/js/main/util.js:45:21) | |
at Promise._callHandler (/private/tmp/association/node_modules/sequelize/node_modules/bluebird/js/main/promise.js:571:13) | |
at Promise._settlePromiseFromHandler (/private/tmp/association/node_modules/sequelize/node_modules/bluebird/js/main/promise.js:581:18) | |
at Promise._settlePromiseAt (/private/tmp/association/node_modules/sequelize/node_modules/bluebird/js/main/promise.js:713:18) | |
at Promise._settlePromiseAt (/private/tmp/association/node_modules/sequelize/lib/promise.js:76:18) | |
at Promise._settlePromiseAtPostResolution (/private/tmp/association/node_modules/sequelize/node_modules/bluebird/js/main/promise.js:335:10) | |
at Async._consumeFunctionBuffer (/private/tmp/association/node_modules/sequelize/node_modules/bluebird/js/main/async.js:85:12) | |
at Async.consumeFunctionBuffer (/private/tmp/association/node_modules/sequelize/node_modules/bluebird/js/main/async.js:40:14) | |
at process._tickCallback (node.js:442:13) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment