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

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 107 additions and 169 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,109 +67,112 @@ var hookAliases = { ...@@ -66,109 +67,112 @@ var hookAliases = {
afterBulkDelete: 'afterBulkDestroy' afterBulkDelete: 'afterBulkDestroy'
}; };
Hooks.replaceHookAliases = function(hooks) { var Hooks = {
var realHookName; replaceHookAliases: function(hooks) {
var realHookName;
Utils._.each(hooks, function(hooksArray, name) { Utils._.each(hooks, function(hooksArray, name) {
// Does an alias for this hook name exist? // Does an alias for this hook name exist?
if (realHookName = hookAliases[name]) { if (realHookName = hookAliases[name]) {
// Add the hooks to the actual hook // Add the hooks to the actual hook
hooks[realHookName] = (hooks[realHookName] || []).concat(hooksArray); hooks[realHookName] = (hooks[realHookName] || []).concat(hooksArray);
// Delete the alias // Delete the alias
delete hooks[name]; delete hooks[name];
} }
}); });
return hooks;
};
Hooks.runHooks = function(hooks) {
var self = this
, fn
, fnArgs = Array.prototype.slice.call(arguments, 1)
, hookType;
if (typeof fnArgs[fnArgs.length - 1] === 'function') { return hooks;
fn = fnArgs.pop(); },
}
if (typeof hooks === 'string') { runHooks: function(hooks) {
hookType = hooks; var self = this
hooks = this.options.hooks[hookType] || []; , fn
if (!Array.isArray(hooks)) hooks = hooks === undefined ? [] : [hooks]; , fnArgs = Array.prototype.slice.call(arguments, 1)
if (this.sequelize) hooks = hooks.concat(this.sequelize.options.hooks[hookType] || []); , hookType;
}
if (!Array.isArray(hooks)) { if (typeof fnArgs[fnArgs.length - 1] === 'function') {
hooks = hooks === undefined ? [] : [hooks]; fn = fnArgs.pop();
} }
// run hooks as sync functions if flagged as sync if (typeof hooks === 'string') {
if (hookTypes[hookType] && hookTypes[hookType].sync) { hookType = hooks;
hooks.forEach(function(hook) { hooks = this.options.hooks[hookType] || [];
if (typeof hook === 'object') hook = hook.fn; if (!Array.isArray(hooks)) hooks = hooks === undefined ? [] : [hooks];
return hook.apply(self, fnArgs); if (this.sequelize) hooks = hooks.concat(this.sequelize.options.hooks[hookType] || []);
}); }
return;
}
// run hooks async if (!Array.isArray(hooks)) {
var promise = Promise.each(hooks, function (hook) { hooks = hooks === undefined ? [] : [hooks];
if (typeof hook === 'object') {
hook = hook.fn;
} }
if (hookType && hook.length > hookTypes[hookType].params) { // run hooks as sync functions if flagged as sync
hook = Promise.promisify(hook, self); if (hookTypes[hookType] && hookTypes[hookType].sync) {
hooks.forEach(function(hook) {
if (typeof hook === 'object') hook = hook.fn;
return hook.apply(self, fnArgs);
});
return;
} }
return hook.apply(self, fnArgs); // run hooks async
}).return(); var promise = Promise.each(hooks, function (hook) {
if (typeof hook === 'object') {
hook = hook.fn;
}
if (fn) { if (hookType && hook.length > hookTypes[hookType].params) {
return promise.nodeify(fn); hook = Promise.promisify(hook, self);
} }
return promise; return hook.apply(self, fnArgs);
}; }).return();
Hooks.hook = function() { if (fn) {
return Hooks.addHook.apply(this, arguments); return promise.nodeify(fn);
}; }
/** return promise;
* Add a hook to the model },
*
* @param {String} hooktype hook: function() {
* @param {String} [name] Provide a name for the hook function. This serves no purpose, other than the ability to be able to order hooks based on some sort of priority system in the future. return Hooks.addHook.apply(this, arguments);
* @param {Function} fn The hook function },
*
* @alias hook /**
*/ * Add a hook to the model
Hooks.addHook = function(hookType, name, fn) { *
if (typeof name === 'function') { * @param {String} hooktype
fn = name; * @param {String} [name] Provide a name for the hook function. This serves no purpose, other than the ability to be able to order hooks based on some sort of priority system in the future.
name = null; * @param {Function} fn The hook function
} *
* @alias hook
*/
addHook: function(hookType, name, fn) {
if (typeof name === 'function') {
fn = name;
name = null;
}
hookType = hookAliases[hookType] || hookType; hookType = hookAliases[hookType] || hookType;
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!