Skip to content

Instantly share code, notes, and snippets.

View mickhansen's full-sized avatar

Mick Hansen mickhansen

  • Copenhagen, Denmark
View GitHub Profile
@mickhansen
mickhansen / sequelize_hooks_api_proposal.js
Last active December 15, 2015 19:19
A proposal for API design of Hooks in Sequelize
// 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
}, {
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
};
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']);
});
});
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();
});
});
@mickhansen
mickhansen / gist:7975176
Created December 15, 2013 16:45
Test-case for @dstanoev
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});
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', {})
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'});
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)
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.
A.findAll({
include: [
{model: B, include: [
{model: C, include: [
{model: D, include: [
{model: E, include: [
{model: F, include: [
{model: G, include: [
{model: H}
]}