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

Commit 5fccccb4 by overlookmotel

beforeFind and afterFind hooks

1 parent 57ecd46c
Showing with 74 additions and 24 deletions
......@@ -49,7 +49,10 @@ var hookTypes = {
beforeBulkDestroy: {params: 1},
afterBulkDestroy: {params: 1},
beforeBulkUpdate: {params: 1},
afterBulkUpdate: {params: 1}
afterBulkUpdate: {params: 1},
beforeFind: {params: 1},
beforeFindAfterExpandIncludeAll: {params: 1},
afterFind: {params: 2}
};
var hookAliases = {
beforeDelete: 'beforeDestroy',
......@@ -290,3 +293,30 @@ Hooks.beforeBulkUpdate = function(name, fn) {
Hooks.afterBulkUpdate = function(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,38 +693,58 @@ module.exports = (function() {
tableNames[this.getTableName()] = true;
options = optClone(options || {});
if (typeof options === 'object') {
options = Utils._.defaults(options, {
hooks: true
});
return Promise.bind(this).then(function() {
conformOptions(options);
if (options.include) {
hasJoin = true;
expandIncludeAll.call(this, options);
validateIncludedElements.call(this, options, tableNames);
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) {
hasJoin = true;
validateIncludedElements.call(this, options, tableNames);
if (options.attributes) {
if (options.attributes.indexOf(this.primaryKeyAttribute) === -1) {
options.originalAttributes = options.attributes;
options.attributes = [this.primaryKeyAttribute].concat(options.attributes);
if (options.attributes) {
if (options.attributes.indexOf(this.primaryKeyAttribute) === -1) {
options.originalAttributes = options.attributes;
options.attributes = [this.primaryKeyAttribute].concat(options.attributes);
}
}
}
// whereCollection is used for non-primary key updates
this.options.whereCollection = options.where || null;
}
// whereCollection is used for non-primary key updates
this.options.whereCollection = options.where || null;
}
if (options.attributes === undefined) {
options.attributes = Object.keys(this.tableAttributes);
}
mapFieldNames.call(this, options, this);
if (options.attributes === undefined) {
options.attributes = Object.keys(this.tableAttributes);
}
mapFieldNames.call(this, options, this);
options = paranoidClause.call(this, options);
options = paranoidClause.call(this, options);
return this.QueryInterface.select(this, this.getTableName(), options, Utils._.defaults({
type: QueryTypes.SELECT,
hasJoin: hasJoin,
tableNames: Object.keys(tableNames)
}, queryOptions, { transaction: (options || {}).transaction }));
return this.QueryInterface.select(this, this.getTableName(), options, Utils._.defaults({
type: QueryTypes.SELECT,
hasJoin: hasJoin,
tableNames: Object.keys(tableNames)
}, 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
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!