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
// The primary reason why hooks is not plug and play is the need for asynchronicity | |
// This is my proposal on how we might implement this | |
// It's written as kind of pseudo code with knowledge of sequelize internals | |
// To keep the method signatures easier to understand, i propose that all hook callbacks are called with the current model as context (if available), parameters are arguments with the final arguments being a next callback | |
// The hooks would then be able to be chained via the next callbacks, ala middleware | |
var User = sequelize.define('User', { | |
// fields | |
}, { |
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
sequelize.getQueryInterface().QueryGenerator.databaseConnectionUri = function(config) { | |
return { | |
user: config.username, | |
password: config.password, | |
database: config.database, | |
host: config.host, | |
port: config.port, | |
protocol: config.protocol, | |
ssl: true | |
}; |
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
var _ = require('lodash'); | |
sequelize.User.findAll().done(function (err, users) { | |
if (err) return res.json(403, {err: err.toString()}); | |
res.json(200, users.map(function (user) { | |
return _.omit(user.toJSON(), ['password']); | |
}); | |
}); |
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
sequelize.transaction(function (t) { | |
User.find({transaction: t, where: {id: 1}}).done(function (err, user) { | |
if (err) return t.rollback(); | |
user.updateAttributes({transaction:t, where: {foo: 'bar'}}).done(function (err) { | |
if (err) return t.rollback(); | |
t.commit(); | |
}); | |
}); |
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
var Sequelize = require('sequelize'), | |
sequelize = new Sequelize(); | |
var User = sequelize.define('User', {}), | |
Post = sequelize.define('Post', {}); | |
Post.belongsTo(User, {as: 'Creator', foreignKey: 'creator_id', foreignKeyConstraint: true}); | |
User.hasMany(Post, {foreignKey: 'creator_id'}); | |
User.hasMany(Post, {as: 'SharedPosts', joinTableName: 'SharedPosts', foreignKey: 'user_id', foreignKeyConstraint: true}); |
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
var Sequelize = require('sequelize'), | |
sequelize = new Sequelize('sequelize_test', 'sequelize_test', 'YDLvjZJv9GdvbM8G', { | |
syncOnAssociation: false, | |
logging: console.log | |
}); | |
var User = sequelize.define('User', {}), | |
Post = sequelize.define('Post', {}), | |
SharedPosts = sequelize.define('SharedPosts', {}) |
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
var Sequelize = require('sequelize'), | |
sequelize = new Sequelize('sequelize_test', 'sequelize_test', 'YDLvjZJv9GdvbM8G', { | |
logging: console.log | |
}); | |
var User = sequelize.define('User', {}), | |
Post = sequelize.define('Post', {}) | |
Post.belongsTo(User, {as: 'Creator', foreignKey: 'creator_id', foreignKeyConstraint: true}); | |
User.hasMany(Post, {foreignKey: 'creator_id'}); |
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
var HasMany = function(source, target, options) { | |
var self = this | |
this.associationType = 'HasMany' | |
this.source = source | |
this.target = target | |
this.options = options | |
this.sequelize = source.daoFactoryManager.sequelize | |
this.through = options.through | |
this.isSelfAssociation = (this.source.tableName === this.target.tableName) |
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
SELECT "As".*, "B"."id" AS "B.id", "B"."createdAt" AS "B.createdAt", "B"."updatedAt" AS "B.updatedAt", "B"."CId" AS "B.CId", "B.C"."id" AS "B.C.id", "B.C"."createdAt" AS "B.C.createdAt", "B.C"."updatedAt" AS "B.C.updatedAt", "B.C"."DId" AS "B.C.DId", "B.C.D"."id" AS "B.C.D.id", "B.C.D"."createdAt" AS "B.C.D.createdAt", "B.C.D"."updatedAt" AS "B.C.D.updatedAt", "B.C.D"."EId" AS "B.C.D.EId", "B.C.D.E"."id" AS "B.C.D.E.id", "B.C.D.E"."createdAt" AS "B.C.D.E.createdAt", "B.C.D.E"."updatedAt" AS "B.C.D.E.updatedAt", "B.C.D.E"."FId" AS "B.C.D.E.FId", "B.C.D.E.F"."id" AS "B.C.D.E.F.id", "B.C.D.E.F"."createdAt" AS "B.C.D.E.F.createdAt", "B.C.D.E.F"."updatedAt" AS "B.C.D.E.F.updatedAt", "B.C.D.E.F"."GId" AS "B.C.D.E.F.GId", "B.C.D.E.F.G"."id" AS "B.C.D.E.F.G.id", "B.C.D.E.F.G"."createdAt" AS "B.C.D.E.F.G.createdAt", "B.C.D.E.F.G"."updatedAt" AS "B.C.D.E.F.G.updatedAt", "B.C.D.E.F.G"."HId" AS "B.C.D.E.F.G.HId", "B.C.D.E.F.G.H"."id" AS "B.C.D.E.F.G.H.id", "B.C.D.E.F.G.H"."createdAt" AS "B.C.D.E.F.G.H.createdAt", "B.C.D. |
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
A.findAll({ | |
include: [ | |
{model: B, include: [ | |
{model: C, include: [ | |
{model: D, include: [ | |
{model: E, include: [ | |
{model: F, include: [ | |
{model: G, include: [ | |
{model: H} | |
]} |
OlderNewer