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

Commit 5fccccb4 by overlookmotel

beforeFind and afterFind hooks

1 parent 57ecd46c
Showing with 53 additions and 3 deletions
...@@ -49,7 +49,10 @@ var hookTypes = { ...@@ -49,7 +49,10 @@ var hookTypes = {
beforeBulkDestroy: {params: 1}, beforeBulkDestroy: {params: 1},
afterBulkDestroy: {params: 1}, afterBulkDestroy: {params: 1},
beforeBulkUpdate: {params: 1}, beforeBulkUpdate: {params: 1},
afterBulkUpdate: {params: 1} afterBulkUpdate: {params: 1},
beforeFind: {params: 1},
beforeFindAfterExpandIncludeAll: {params: 1},
afterFind: {params: 2}
}; };
var hookAliases = { var hookAliases = {
beforeDelete: 'beforeDestroy', beforeDelete: 'beforeDestroy',
...@@ -290,3 +293,30 @@ Hooks.beforeBulkUpdate = function(name, fn) { ...@@ -290,3 +293,30 @@ Hooks.beforeBulkUpdate = function(name, fn) {
Hooks.afterBulkUpdate = function(name, fn) { Hooks.afterBulkUpdate = function(name, fn) {
return Hooks.addHook.call(this, 'afterBulkUpdate', name, fn); return Hooks.addHook.call(this, 'afterBulkUpdate', name, fn);
}; };
/**
* A hook that is run before a find (select) query
* @param {String} name
* @param {Function} fn A callback function that is called with attribute, where, callback(err)
*/
Hooks.beforeFind = function(name, fn) {
return Hooks.addHook.call(this, 'beforeFind', name, fn);
};
/**
* A hook that is run before a find (select) query, after any { include: {all: ...} } options are expanded
* @param {String} name
* @param {Function} fn A callback function that is called with attribute, where, callback(err)
*/
Hooks.beforeFindAfterExpandIncludeAll = function(name, fn) {
return Hooks.addHook.call(this, 'beforeFindAfterExpandIncludeAll', name, fn);
};
/**
* A hook that is run after a find (select) query
* @param {String} name
* @param {Function} fn A callback function that is called with attribute, where, callback(err)
*/
Hooks.afterFind = function(name, fn) {
return Hooks.addHook.call(this, 'afterFind', name, fn);
};
...@@ -693,12 +693,27 @@ module.exports = (function() { ...@@ -693,12 +693,27 @@ module.exports = (function() {
tableNames[this.getTableName()] = true; tableNames[this.getTableName()] = true;
options = optClone(options || {}); options = optClone(options || {});
if (typeof options === 'object') { options = Utils._.defaults(options, {
hooks: true
});
return Promise.bind(this).then(function() {
conformOptions(options); conformOptions(options);
if (options.hooks) {
return this.runHooks('beforeFind', options);
}
}).then(function() {
expandIncludeAll.call(this, options);
if (options.hooks) {
return this.runHooks('beforeFindAfterExpandIncludeAll', options);
}
}).then(function() {
if (typeof options === 'object') {
if (options.include) { if (options.include) {
hasJoin = true; hasJoin = true;
expandIncludeAll.call(this, options);
validateIncludedElements.call(this, options, tableNames); validateIncludedElements.call(this, options, tableNames);
if (options.attributes) { if (options.attributes) {
...@@ -725,6 +740,11 @@ module.exports = (function() { ...@@ -725,6 +740,11 @@ module.exports = (function() {
hasJoin: hasJoin, hasJoin: hasJoin,
tableNames: Object.keys(tableNames) tableNames: Object.keys(tableNames)
}, queryOptions, { transaction: (options || {}).transaction })); }, queryOptions, { transaction: (options || {}).transaction }));
}).tap(function(results) {
if (options.hooks) {
return this.runHooks('afterFind', results, options);
}
});
}; };
//right now, the caller (has-many-double-linked) is in charge of the where clause //right now, the caller (has-many-double-linked) is in charge of the where clause
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!