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 / 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});
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();
});
});
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.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
};
@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
}, {