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

Commit 2c924949 by Felix Becker Committed by Mick Hansen

ES6 refactor of hooks.js (#6287)

let, const, arrow functions, method shorthands, exports
1 parent cb245674
Showing with 46 additions and 50 deletions
'use strict'; 'use strict';
const Utils = require('./utils') const Utils = require('./utils');
, Promise = require('./promise') const Promise = require('./promise');
, debug = Utils.getLogger().debugContext('hooks'); const debug = Utils.getLogger().debugContext('hooks');
/** /**
* Hooks are function that are called before and after (bulk-) creation/updating/deletion and validation. Hooks can be added to you models in three ways: * Hooks are function that are called before and after (bulk-) creation/updating/deletion and validation. Hooks can be added to you models in three ways:
...@@ -14,11 +14,11 @@ const Utils = require('./utils') ...@@ -14,11 +14,11 @@ const Utils = require('./utils')
* // Method 1 * // Method 1
* sequelize.define(name, { attributes }, { * sequelize.define(name, { attributes }, {
* hooks: { * hooks: {
* beforeBulkCreate: function () { * beforeBulkCreate() {
* // can be a single function * // can be a single function
* }, * },
* beforeValidate: [ * beforeValidate: [
* function () {}, * function() {},
* function() {} // Or an array of several * function() {} // Or an array of several
* ] * ]
* } * }
...@@ -75,6 +75,7 @@ const hookTypes = { ...@@ -75,6 +75,7 @@ const hookTypes = {
beforeBulkSync: {params: 1}, beforeBulkSync: {params: 1},
afterBulkSync: {params: 1} afterBulkSync: {params: 1}
}; };
exports.hooks = hookTypes;
const hookAliases = { const hookAliases = {
beforeDelete: 'beforeDestroy', beforeDelete: 'beforeDestroy',
...@@ -83,6 +84,7 @@ const hookAliases = { ...@@ -83,6 +84,7 @@ const hookAliases = {
afterBulkDelete: 'afterBulkDestroy', afterBulkDelete: 'afterBulkDestroy',
beforeConnection: 'beforeConnect' beforeConnection: 'beforeConnect'
}; };
exports.hookAliases = hookAliases;
/** /**
* get array of current hook and its proxied hooks combined * get array of current hook and its proxied hooks combined
...@@ -94,10 +96,10 @@ const getProxiedHooks = (hookType) => ( ...@@ -94,10 +96,10 @@ const getProxiedHooks = (hookType) => (
); );
const Hooks = { const Hooks = {
replaceHookAliases: function(hooks) { replaceHookAliases(hooks) {
var realHookName; let realHookName;
Utils._.each(hooks, function(hooksArray, name) { Utils._.each(hooks, (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
...@@ -111,10 +113,9 @@ const Hooks = { ...@@ -111,10 +113,9 @@ const Hooks = {
return hooks; return hooks;
}, },
runHooks: function(hooks) { runHooks(hooks) {
var self = this const fnArgs = Utils.sliceArgs(arguments, 1);
, fnArgs = Utils.sliceArgs(arguments, 1) let hookType;
, hookType;
if (typeof hooks === 'string') { if (typeof hooks === 'string') {
hookType = hooks; hookType = hooks;
...@@ -129,34 +130,33 @@ const Hooks = { ...@@ -129,34 +130,33 @@ const Hooks = {
// run hooks as sync functions if flagged as sync // run hooks as sync functions if flagged as sync
if (hookTypes[hookType] && hookTypes[hookType].sync) { if (hookTypes[hookType] && hookTypes[hookType].sync) {
hooks.forEach(function(hook) { for (let hook of hooks) {
if (typeof hook === 'object') hook = hook.fn; if (typeof hook === 'object') hook = hook.fn;
debug(`running hook(sync) ${hookType}`); // log sync hooks debug(`running hook(sync) ${hookType}`); // log sync hooks
hook.apply(self, fnArgs); return hook.apply(this, fnArgs);
}); }
return; return;
} }
// run hooks async // run hooks async
var promise = Promise.each(hooks, function (hook) { const promise = Promise.each(hooks, hook => {
if (typeof hook === 'object') { if (typeof hook === 'object') {
hook = hook.fn; hook = hook.fn;
} }
if (hookType && hook.length > hookTypes[hookType].params) { if (hookType && hook.length > hookTypes[hookType].params) {
hook = Promise.promisify(hook, self); hook = Promise.promisify(hook, this);
} }
debug(`running hook ${hookType}`); // log async hook debug(`running hook ${hookType}`); // log async hook
return hook.apply(self, fnArgs); return hook.apply(this, fnArgs);
}) }).return();
.return();
return promise; return promise;
}, },
hook: function() { hook() {
return Hooks.addHook.apply(this, arguments); return Hooks.addHook.apply(this, arguments);
}, },
...@@ -169,7 +169,7 @@ const Hooks = { ...@@ -169,7 +169,7 @@ const Hooks = {
* *
* @alias hook * @alias hook
*/ */
addHook: function(hookType, name, fn) { addHook(hookType, name, fn) {
if (typeof name === 'function') { if (typeof name === 'function') {
fn = name; fn = name;
name = null; name = null;
...@@ -183,7 +183,7 @@ const Hooks = { ...@@ -183,7 +183,7 @@ const Hooks = {
Utils._.each(hookType, (type) => { Utils._.each(hookType, (type) => {
this.options.hooks[type] = this.options.hooks[type] || []; this.options.hooks[type] = this.options.hooks[type] || [];
this.options.hooks[type].push(!!name ? {name: name, fn: fn} : fn); this.options.hooks[type].push(!!name ? {name, fn} : fn);
}); });
return this; return this;
...@@ -195,9 +195,9 @@ const Hooks = { ...@@ -195,9 +195,9 @@ const Hooks = {
* @param {String} hookType * @param {String} hookType
* @param {String|Function} name * @param {String|Function} name
*/ */
removeHook: function(hookType, name) { removeHook(hookType, name) {
hookType = hookAliases[hookType] || hookType; hookType = hookAliases[hookType] || hookType;
let isReference = typeof name === 'function' ? true : false; const isReference = typeof name === 'function' ? true : false;
if (!this.hasHook(hookType)) { if (!this.hasHook(hookType)) {
return this; return this;
...@@ -208,15 +208,15 @@ const Hooks = { ...@@ -208,15 +208,15 @@ const Hooks = {
// check for proxies, add them too // check for proxies, add them too
hookType = getProxiedHooks(hookType); hookType = getProxiedHooks(hookType);
Utils._.each(hookType, (type) => { for (const type of hookType) {
this.options.hooks[type] = this.options.hooks[type].filter(function (hook) { this.options.hooks[type] = this.options.hooks[type].filter(hook => {
if (isReference && typeof hook === 'function') { if (isReference && typeof hook === 'function') {
return hook !== name; // check if same method return hook !== name; // check if same method
} else { } else {
return typeof hook === 'object' && hook.name !== name; return typeof hook === 'object' && hook.name !== name;
} }
}); });
}); }
return this; return this;
}, },
...@@ -228,13 +228,25 @@ const Hooks = { ...@@ -228,13 +228,25 @@ const Hooks = {
* *
* @alias hasHooks * @alias hasHooks
*/ */
hasHook: function(hookType) { hasHook(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;
function applyTo(target) {
Utils._.mixin(target, Hooks);
const allHooks = Object.keys(hookTypes).concat(Object.keys(hookAliases));
for (const hook of allHooks) {
target[hook] = function(name, callback) {
return this.addHook(hook, name, callback);
};
}
}
exports.applyTo = applyTo;
/** /**
* A hook that is run before validation * A hook that is run before validation
* @param {String} name * @param {String} name
...@@ -472,21 +484,21 @@ Hooks.hasHooks = Hooks.hasHook; ...@@ -472,21 +484,21 @@ Hooks.hasHooks = Hooks.hasHook;
* @name afterInit * @name afterInit
*/ */
/** /**
* A hook that is run before a connection is created * A hook that is run before a connection is created
* @param {String} name * @param {String} name
* @param {Function} fn A callback function that is called with config passed to connection * @param {Function} fn A callback function that is called with config passed to connection
* @name beforeConnect * @name beforeConnect
*/ */
/** /**
* A hook that is run before Model.sync call * A hook that is run before Model.sync call
* @param {String} name * @param {String} name
* @param {Function} fn A callback function that is called with options passed to Model.sync * @param {Function} fn A callback function that is called with options passed to Model.sync
* @name beforeSync * @name beforeSync
*/ */
/** /**
* A hook that is run after Model.sync call * A hook that is run after Model.sync call
* @param {String} name * @param {String} name
* @param {Function} fn A callback function that is called with options passed to Model.sync * @param {Function} fn A callback function that is called with options passed to Model.sync
...@@ -506,20 +518,3 @@ Hooks.hasHooks = Hooks.hasHook; ...@@ -506,20 +518,3 @@ Hooks.hasHooks = Hooks.hasHook;
* @param {Function} fn A callback function that is called with options passed to sequelize.sync * @param {Function} fn A callback function that is called with options passed to sequelize.sync
* @name afterBulkSync * @name afterBulkSync
*/ */
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[hook] = Model.prototype[hook] = function(name, callback) {
return this.addHook(hook, name, callback);
};
});
}
};
...@@ -1261,6 +1261,7 @@ Sequelize.useInflection = Utils.useInflection; ...@@ -1261,6 +1261,7 @@ Sequelize.useInflection = Utils.useInflection;
* and on Sequelize/sequelize methods e.g. Sequelize(), Sequelize#define() * and on Sequelize/sequelize methods e.g. Sequelize(), Sequelize#define()
*/ */
Hooks.applyTo(Sequelize); Hooks.applyTo(Sequelize);
Hooks.applyTo(Sequelize.prototype);
/** /**
* 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!