不要怂,就是干,撸起袖子干!

Commit 63e80554 by Matt Broadstone

refactor Hooks mixin

The existing Hooks mixin explicitly defines methods for all hooks on
the Sequelize instance or a given Model. This modifies the Hooks module
to provide a single method "applyTo", which mixes in the base Hook
functionality (addHook, runHooks, etc), as well as autogenerates
declarative hooks for the object its mixing in to (Model.beforeCreate,
Sequelize.beforeDefine, etc).
1 parent 5bc2d11c
Showing with 34 additions and 96 deletions
...@@ -34,7 +34,7 @@ var Utils = require('./utils') ...@@ -34,7 +34,7 @@ var Utils = require('./utils')
* @see {Sequelize#define} * @see {Sequelize#define}
* @mixin Hooks * @mixin Hooks
*/ */
var Hooks = module.exports = function() {};
var hookTypes = { var hookTypes = {
beforeValidate: {params: 2}, beforeValidate: {params: 2},
afterValidate: {params: 2}, afterValidate: {params: 2},
...@@ -59,6 +59,7 @@ var hookTypes = { ...@@ -59,6 +59,7 @@ var hookTypes = {
beforeInit: {params: 2, sync: true}, beforeInit: {params: 2, sync: true},
afterInit: {params: 1, sync: true} afterInit: {params: 1, sync: true}
}; };
var hookAliases = { var hookAliases = {
beforeDelete: 'beforeDestroy', beforeDelete: 'beforeDestroy',
afterDelete: 'afterDestroy', afterDelete: 'afterDestroy',
...@@ -66,7 +67,8 @@ var hookAliases = { ...@@ -66,7 +67,8 @@ var hookAliases = {
afterBulkDelete: 'afterBulkDestroy' afterBulkDelete: 'afterBulkDestroy'
}; };
Hooks.replaceHookAliases = function(hooks) { var Hooks = {
replaceHookAliases: function(hooks) {
var realHookName; var realHookName;
Utils._.each(hooks, function(hooksArray, name) { Utils._.each(hooks, function(hooksArray, name) {
...@@ -81,9 +83,9 @@ Hooks.replaceHookAliases = function(hooks) { ...@@ -81,9 +83,9 @@ Hooks.replaceHookAliases = function(hooks) {
}); });
return hooks; return hooks;
}; },
Hooks.runHooks = function(hooks) { runHooks: function(hooks) {
var self = this var self = this
, fn , fn
, fnArgs = Array.prototype.slice.call(arguments, 1) , fnArgs = Array.prototype.slice.call(arguments, 1)
...@@ -131,13 +133,13 @@ Hooks.runHooks = function(hooks) { ...@@ -131,13 +133,13 @@ Hooks.runHooks = function(hooks) {
} }
return promise; return promise;
}; },
Hooks.hook = function() { hook: function() {
return Hooks.addHook.apply(this, arguments); return Hooks.addHook.apply(this, arguments);
}; },
/** /**
* Add a hook to the model * Add a hook to the model
* *
* @param {String} hooktype * @param {String} hooktype
...@@ -146,7 +148,7 @@ Hooks.hook = function() { ...@@ -146,7 +148,7 @@ Hooks.hook = function() {
* *
* @alias hook * @alias hook
*/ */
Hooks.addHook = function(hookType, name, fn) { addHook: function(hookType, name, fn) {
if (typeof name === 'function') { if (typeof name === 'function') {
fn = name; fn = name;
name = null; name = null;
...@@ -157,18 +159,20 @@ Hooks.addHook = function(hookType, name, fn) { ...@@ -157,18 +159,20 @@ Hooks.addHook = function(hookType, name, fn) {
this.options.hooks[hookType] = this.options.hooks[hookType] || []; this.options.hooks[hookType] = this.options.hooks[hookType] || [];
this.options.hooks[hookType].push(!!name ? {name: name, fn: fn} : fn); this.options.hooks[hookType].push(!!name ? {name: name, fn: fn} : fn);
return this; return this;
}; },
/* /*
* Check whether the mode has any hooks of this type * Check whether the mode has any hooks of this type
* *
* @param {String} hookType * @param {String} hookType
* *
* @alias hasHooks * @alias hasHooks
*/ */
Hooks.hasHook = function(hookType) { hasHook: function(hookType) {
return this.options.hooks[hookType] && !!this.options.hooks[hookType].length; return this.options.hooks[hookType] && !!this.options.hooks[hookType].length;
},
}; };
Hooks.hasHooks = Hooks.hasHook; Hooks.hasHooks = Hooks.hasHook;
/** /**
...@@ -176,36 +180,24 @@ Hooks.hasHooks = Hooks.hasHook; ...@@ -176,36 +180,24 @@ Hooks.hasHooks = Hooks.hasHook;
* @param {String} name * @param {String} name
* @param {Function} fn A callback function that is called with instance, options, callback(err) * @param {Function} fn A callback function that is called with instance, options, callback(err)
*/ */
Hooks.beforeValidate = function(name, fn) {
return Hooks.addHook.call(this, 'beforeValidate', name, fn);
};
/** /**
* A hook that is run after validation * A hook that is run after validation
* @param {String} name * @param {String} name
* @param {Function} fn A callback function that is called with instance, options, callback(err) * @param {Function} fn A callback function that is called with instance, options, callback(err)
*/ */
Hooks.afterValidate = function(name, fn) {
return Hooks.addHook.call(this, 'afterValidate', name, fn);
};
/** /**
* A hook that is run before creating a single instance * A hook that is run before creating a single instance
* @param {String} name * @param {String} name
* @param {Function} fn A callback function that is called with attributes, options, callback(err) * @param {Function} fn A callback function that is called with attributes, options, callback(err)
*/ */
Hooks.beforeCreate = function(name, fn) {
return Hooks.addHook.call(this, 'beforeCreate', name, fn);
};
/** /**
* A hook that is run after creating a single instance * A hook that is run after creating a single instance
* @param {String} name * @param {String} name
* @param {Function} fn A callback function that is called with attributes, options, callback(err) * @param {Function} fn A callback function that is called with attributes, options, callback(err)
*/ */
Hooks.afterCreate = function(name, fn) {
return Hooks.addHook.call(this, 'afterCreate', name, fn);
};
/** /**
* A hook that is run before destroying a single instance * A hook that is run before destroying a single instance
...@@ -214,13 +206,6 @@ Hooks.afterCreate = function(name, fn) { ...@@ -214,13 +206,6 @@ Hooks.afterCreate = function(name, fn) {
* *
* @alias beforeDelete * @alias beforeDelete
*/ */
Hooks.beforeDestroy = function(name, fn) {
return Hooks.addHook.call(this, 'beforeDestroy', name, fn);
};
Hooks.beforeDelete = function(name, fn) {
return Hooks.addHook.call(this, 'beforeDelete', name, fn);
};
/** /**
* A hook that is run after destroying a single instance * A hook that is run after destroying a single instance
...@@ -229,49 +214,30 @@ Hooks.beforeDelete = function(name, fn) { ...@@ -229,49 +214,30 @@ Hooks.beforeDelete = function(name, fn) {
* *
* @alias afterDelete * @alias afterDelete
*/ */
Hooks.afterDestroy = function(name, fn) {
return Hooks.addHook.call(this, 'afterDestroy', name, fn);
};
Hooks.afterDelete = function(name, fn) {
return Hooks.addHook.call(this, 'afterDelete', name, fn);
};
/** /**
* A hook that is run before updating a single instance * A hook that is run before updating a single instance
* @param {String} name * @param {String} name
* @param {Function} fn A callback function that is called with instance, options, callback(err) * @param {Function} fn A callback function that is called with instance, options, callback(err)
*/ */
Hooks.beforeUpdate = function(name, fn) {
return Hooks.addHook.call(this, 'beforeUpdate', name, fn);
};
/** /**
* A hook that is run after updating a single instance * A hook that is run after updating a single instance
* @param {String} name * @param {String} name
* @param {Function} fn A callback function that is called with instance, options, callback(err) * @param {Function} fn A callback function that is called with instance, options, callback(err)
*/ */
Hooks.afterUpdate = function(name, fn) {
return Hooks.addHook.call(this, 'afterUpdate', name, fn);
};
/** /**
* A hook that is run before creating instances in bulk * A hook that is run before creating instances in bulk
* @param {String} name * @param {String} name
* @param {Function} fn A callback function that is called with instances, options, callback(err) * @param {Function} fn A callback function that is called with instances, options, callback(err)
*/ */
Hooks.beforeBulkCreate = function(name, fn) {
return Hooks.addHook.call(this, 'beforeBulkCreate', name, fn);
};
/** /**
* A hook that is run after creating instances in bulk * A hook that is run after creating instances in bulk
* @param {String} name * @param {String} name
* @param {Function} fn A callback function that is called with instances, options, callback(err) * @param {Function} fn A callback function that is called with instances, options, callback(err)
*/ */
Hooks.afterBulkCreate = function(name, fn) {
return Hooks.addHook.call(this, 'afterBulkCreate', name, fn);
};
/** /**
* A hook that is run before destroying instances in bulk * A hook that is run before destroying instances in bulk
...@@ -280,13 +246,6 @@ Hooks.afterBulkCreate = function(name, fn) { ...@@ -280,13 +246,6 @@ Hooks.afterBulkCreate = function(name, fn) {
* *
* @alias beforeBulkDelete * @alias beforeBulkDelete
*/ */
Hooks.beforeBulkDestroy = function(name, fn) {
return Hooks.addHook.call(this, 'beforeBulkDestroy', name, fn);
};
Hooks.beforeBulkDelete = function(name, fn) {
return Hooks.addHook.call(this, 'beforeBulkDelete', name, fn);
};
/** /**
* A hook that is run after destroying instances in bulk * A hook that is run after destroying instances in bulk
...@@ -295,100 +254,80 @@ Hooks.beforeBulkDelete = function(name, fn) { ...@@ -295,100 +254,80 @@ Hooks.beforeBulkDelete = function(name, fn) {
* *
* @alias afterBulkDelete * @alias afterBulkDelete
*/ */
Hooks.afterBulkDestroy = function(name, fn) {
return Hooks.addHook.call(this, 'afterBulkDestroy', name, fn);
};
Hooks.afterBulkDelete = function(name, fn) {
return Hooks.addHook.call(this, 'afterBulkDelete', name, fn);
};
/** /**
* A hook that is run after updating instances in bulk * A hook that is run after updating instances in bulk
* @param {String} name * @param {String} name
* @param {Function} fn A callback function that is called with options, callback(err) * @param {Function} fn A callback function that is called with options, callback(err)
*/ */
Hooks.beforeBulkUpdate = function(name, fn) {
return Hooks.addHook.call(this, 'beforeBulkUpdate', name, fn);
};
/** /**
* A hook that is run after updating instances in bulk * A hook that is run after updating instances in bulk
* @param {String} name * @param {String} name
* @param {Function} fn A callback function that is called with options, callback(err) * @param {Function} fn A callback function that is called with options, callback(err)
*/ */
Hooks.afterBulkUpdate = function(name, fn) {
return Hooks.addHook.call(this, 'afterBulkUpdate', name, fn);
};
/** /**
* A hook that is run before a find (select) query * A hook that is run before a find (select) query
* @param {String} name * @param {String} name
* @param {Function} fn A callback function that is called with options, callback(err) * @param {Function} fn A callback function that is called with options, callback(err)
*/ */
Hooks.beforeFind = function(name, fn) {
return Hooks.addHook.call(this, 'beforeFind', name, fn);
};
/** /**
* A hook that is run before a find (select) query, after any { include: {all: ...} } options are expanded * A hook that is run before a find (select) query, after any { include: {all: ...} } options are expanded
* @param {String} name * @param {String} name
* @param {Function} fn A callback function that is called with options, callback(err) * @param {Function} fn A callback function that is called with options, callback(err)
*/ */
Hooks.beforeFindAfterExpandIncludeAll = function(name, fn) {
return Hooks.addHook.call(this, 'beforeFindAfterExpandIncludeAll', name, fn);
};
/** /**
* A hook that is run before a find (select) query, after all option parsing is complete * A hook that is run before a find (select) query, after all option parsing is complete
* @param {String} name * @param {String} name
* @param {Function} fn A callback function that is called with options, callback(err) * @param {Function} fn A callback function that is called with options, callback(err)
*/ */
Hooks.beforeFindAfterOptions = function(name, fn) {
return Hooks.addHook.call(this, 'beforeFindAfterOptions', name, fn);
};
/** /**
* A hook that is run after a find (select) query * A hook that is run after a find (select) query
* @param {String} name * @param {String} name
* @param {Function} fn A callback function that is called with instance(s), options, callback(err) * @param {Function} fn A callback function that is called with instance(s), options, callback(err)
*/ */
Hooks.afterFind = function(name, fn) {
return Hooks.addHook.call(this, 'afterFind', name, fn);
};
/** /**
* A hook that is run before a define call * A hook that is run before a define call
* @param {String} name * @param {String} name
* @param {Function} fn A callback function that is called with attributes, options, callback(err) * @param {Function} fn A callback function that is called with attributes, options, callback(err)
*/ */
Hooks.beforeDefine = function(name, fn) {
return Hooks.addHook.call(this, 'beforeDefine', name, fn);
};
/** /**
* A hook that is run after a define call * A hook that is run after a define call
* @param {String} name * @param {String} name
* @param {Function} fn A callback function that is called with factory, callback(err) * @param {Function} fn A callback function that is called with factory, callback(err)
*/ */
Hooks.afterDefine = function(name, fn) {
return Hooks.addHook.call(this, 'afterDefine', name, fn);
};
/** /**
* A hook that is run before Sequelize() call * A hook that is run before Sequelize() call
* @param {String} name * @param {String} name
* @param {Function} fn A callback function that is called with config, options, callback(err) * @param {Function} fn A callback function that is called with config, options, callback(err)
*/ */
Hooks.beforeInit = function(name, fn) {
return Hooks.addHook.call(this, 'beforeInit', name, fn);
};
/** /**
* A hook that is run after Sequelize() call * A hook that is run after Sequelize() call
* @param {String} name * @param {String} name
* @param {Function} fn A callback function that is called with sequelize, callback(err) * @param {Function} fn A callback function that is called with sequelize, callback(err)
*/ */
Hooks.afterInit = function(name, fn) {
return Hooks.addHook.call(this, 'afterInit', name, fn); module.exports = {
hooks: hookTypes,
hookAliases: hookAliases,
applyTo: function(Model) {
Utils._.mixin(Model, Hooks);
Utils._.mixin(Model.prototype, Hooks);
var allHooks = Object.keys(hookTypes).concat(Object.keys(hookAliases));
allHooks.forEach(function(hook) {
Model.prototype[hook] = function(callback) {
return this.addHook(hook, callback);
};
});
}
}; };
...@@ -2179,7 +2179,7 @@ module.exports = (function() { ...@@ -2179,7 +2179,7 @@ module.exports = (function() {
}; };
Utils._.extend(Model.prototype, associationsMixin); Utils._.extend(Model.prototype, associationsMixin);
Utils._.extend(Model.prototype, Hooks); Hooks.applyTo(Model);
return Model; return Model;
})(); })();
...@@ -268,8 +268,7 @@ module.exports = (function() { ...@@ -268,8 +268,7 @@ module.exports = (function() {
* Allow hooks to be defined on Sequelize + on sequelize instance as universal hooks to run on all models * Allow hooks to be defined on Sequelize + on sequelize instance as universal hooks to run on all models
* and on Sequelize/sequelize methods e.g. Sequelize(), Sequelize#define() * and on Sequelize/sequelize methods e.g. Sequelize(), Sequelize#define()
*/ */
Utils._.extend(Sequelize, Hooks); Hooks.applyTo(Sequelize);
Utils._.extend(Sequelize.prototype, Hooks);
/** /**
* A general error class * A general error class
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!