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

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