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

Commit e90e7d37 by overlookmotel

Hooks on Sequelize()

hooks9

hooks10
1 parent 1f4e7fca
Showing with 29 additions and 1 deletions
......@@ -55,7 +55,9 @@ var hookTypes = {
beforeFindAfterOptions: {params: 1},
afterFind: {params: 2},
beforeDefine: {params: 2, sync: true},
afterDefine: {params: 1, sync: true}
afterDefine: {params: 1, sync: true},
beforeInstantiate: {params: 2, sync: true},
afterInstantiate: {params: 1, sync: true}
};
var hookAliases = {
beforeDelete: 'beforeDestroy',
......@@ -362,3 +364,21 @@ Hooks.beforeDefine = function(name, fn) {
Hooks.afterDefine = function(name, fn) {
return Hooks.addHook.call(this, 'afterDefine', name, fn);
};
/**
* A hook that is run before Sequelize() call
* @param {String} name
* @param {Function} fn A callback function that is called with config, options, callback(err)
*/
Hooks.beforeInstantiate = function(name, fn) {
return Hooks.addHook.call(this, 'beforeInstantiate', name, fn);
};
/**
* A hook that is run after Sequelize() call
* @param {String} name
* @param {Function} fn A callback function that is called with sequelize, callback(err)
*/
Hooks.afterInstantiate = function(name, fn) {
return Hooks.addHook.call(this, 'afterInstantiate', name, fn);
};
......@@ -122,6 +122,12 @@ module.exports = (function() {
}
}
var config = {database: database, username: username, password: password};
Sequelize.runHooks('beforeInstantiate', config, options);
database = config.database;
username = config.username;
password = config.password;
this.options = Utils._.extend({
dialect: 'mysql',
dialectModulePath: null,
......@@ -189,6 +195,8 @@ module.exports = (function() {
this.connectionManager = this.dialect.connectionManager;
this.importCache = {};
Sequelize.runHooks('afterInstantiate', this);
};
Sequelize.options = {hooks: {}};
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!