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

Commit 1f4e7fca by overlookmotel

Hooks on Sequelize#define

define hooks 2

define hooks 3

define hooks 4

define hooks 7
1 parent bc885159
Showing with 29 additions and 1 deletions
......@@ -53,7 +53,9 @@ var hookTypes = {
beforeFind: {params: 1},
beforeFindAfterExpandIncludeAll: {params: 1},
beforeFindAfterOptions: {params: 1},
afterFind: {params: 2}
afterFind: {params: 2},
beforeDefine: {params: 2, sync: true},
afterDefine: {params: 1, sync: true}
};
var hookAliases = {
beforeDelete: 'beforeDestroy',
......@@ -342,3 +344,21 @@ Hooks.beforeFindAfterOptions = function(name, fn) {
Hooks.afterFind = function(name, fn) {
return Hooks.addHook.call(this, 'afterFind', name, fn);
};
/**
* A hook that is run before a define call
* @param {String} name
* @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
* @param {String} name
* @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);
};
......@@ -465,9 +465,17 @@ module.exports = (function() {
}
options.sequelize = this;
options.modelName = modelName;
this.runHooks('beforeDefine', attributes, options);
modelName = options.modelName;
delete options.modelName;
var factory = new Model(modelName, attributes, options);
this.modelManager.addDAO(factory.init(this.modelManager));
this.runHooks('afterDefine', factory);
return factory;
};
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!