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

Commit f3c6b1c1 by overlookmotel

Universal hooks

Hooks defined on `sequelize` are applied universally on all models

Universal hooks 2

Universal hooks 2

Universal hooks 2

universal hooks 6
1 parent 7fb57adc
Showing with 17 additions and 2 deletions
......@@ -91,7 +91,9 @@ Hooks.runHooks = function(hooks) {
if (typeof hooks === 'string') {
hookType = hooks;
hooks = this.options.hooks[hooks] || [];
hooks = this.options.hooks[hookType] || [];
if (!Array.isArray(hooks)) hooks = hooks === undefined ? [] : [hooks];
if (this.sequelize) hooks = hooks.concat(this.sequelize.options.hooks[hookType] || []);
}
if (!Array.isArray(hooks)) {
......
......@@ -11,6 +11,7 @@ var url = require('url')
, QueryTypes = require('./query-types')
, sequelizeErrors = require('./errors')
, Promise = require('./promise')
, Hooks = require('./hooks')
, deprecatedSeen = {}
, deprecated = function(message) {
if (deprecatedSeen[message]) return;
......@@ -136,7 +137,8 @@ module.exports = (function() {
replication: false,
ssl: undefined,
pool: {},
quoteIdentifiers: true
quoteIdentifiers: true,
hooks: {}
}, options || {});
if (this.options.dialect === 'postgresql') {
......@@ -152,6 +154,8 @@ module.exports = (function() {
this.options.logging = console.log;
}
this.options.hooks = this.replaceHookAliases(this.options.hooks);
this.config = {
database: database,
username: username,
......@@ -187,6 +191,8 @@ module.exports = (function() {
this.importCache = {};
};
Sequelize.options = {hooks: {}};
/**
* A reference to sequelize utilities. Most users will not need to use these utils directly. However, you might want to use `Sequelize.Utils._`, which is a reference to the lodash library, if you don't already have it imported in your project.
* @property Utils
......@@ -231,6 +237,13 @@ module.exports = (function() {
Sequelize.prototype.Transaction = Sequelize.Transaction = Transaction;
/**
* 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()
*/
Utils._.extend(Sequelize, Hooks);
Utils._.extend(Sequelize.prototype, Hooks);
/**
* A general error class
* @property Error
* @see {Errors#BaseError}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!