123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- 'use strict';
- const symbols = require('../../schema/symbols');
- const promiseOrCallback = require('../promiseOrCallback');
- module.exports = applyHooks;
- applyHooks.middlewareFunctions = [
- 'deleteOne',
- 'save',
- 'validate',
- 'remove',
- 'updateOne',
- 'init'
- ];
- function applyHooks(model, schema, options) {
- options = options || {};
- const kareemOptions = {
- useErrorHandlers: true,
- numCallbackParams: 1,
- nullResultByDefault: true,
- contextParameter: true
- };
- const objToDecorate = options.decorateDoc ? model : model.prototype;
- model.$appliedHooks = true;
- for (const key of Object.keys(schema.paths)) {
- const type = schema.paths[key];
- let childModel = null;
- if (type.$isSingleNested) {
- childModel = type.caster;
- } else if (type.$isMongooseDocumentArray) {
- childModel = type.Constructor;
- } else {
- continue;
- }
- if (childModel.$appliedHooks) {
- continue;
- }
- applyHooks(childModel, type.schema, options);
- if (childModel.discriminators != null) {
- const keys = Object.keys(childModel.discriminators);
- for (const key of keys) {
- applyHooks(childModel.discriminators[key],
- childModel.discriminators[key].schema, options);
- }
- }
- }
-
-
-
- const middleware = schema.s.hooks.
- filter(hook => {
- if (hook.name === 'updateOne' || hook.name === 'deleteOne') {
- return !!hook['document'];
- }
- if (hook.name === 'remove' || hook.name === 'init') {
- return hook['document'] == null || !!hook['document'];
- }
- if (hook.query != null || hook.document != null) {
- return hook.document !== false;
- }
- return true;
- }).
- filter(hook => {
-
- if (schema.methods[hook.name]) {
- return !hook.fn[symbols.builtInMiddleware];
- }
- return true;
- });
- model._middleware = middleware;
- objToDecorate.$__originalValidate = objToDecorate.$__originalValidate || objToDecorate.$__validate;
- for (const method of ['save', 'validate', 'remove', 'deleteOne']) {
- const toWrap = method === 'validate' ? '$__originalValidate' : `$__${method}`;
- const wrapped = middleware.
- createWrapper(method, objToDecorate[toWrap], null, kareemOptions);
- objToDecorate[`$__${method}`] = wrapped;
- }
- objToDecorate.$__init = middleware.
- createWrapperSync('init', objToDecorate.$__init, null, kareemOptions);
-
- const customMethods = Object.keys(schema.methods);
- const customMethodOptions = Object.assign({}, kareemOptions, {
-
-
-
-
- checkForPromise: true
- });
- for (const method of customMethods) {
- if (!middleware.hasHooks(method)) {
-
-
-
- continue;
- }
- const originalMethod = objToDecorate[method];
- objToDecorate[method] = function() {
- const args = Array.prototype.slice.call(arguments);
- const cb = args.slice(-1).pop();
- const argsWithoutCallback = typeof cb === 'function' ?
- args.slice(0, args.length - 1) : args;
- return promiseOrCallback(cb, callback => {
- return this[`$__${method}`].apply(this,
- argsWithoutCallback.concat([callback]));
- }, model.events);
- };
- objToDecorate[`$__${method}`] = middleware.
- createWrapper(method, originalMethod, null, customMethodOptions);
- }
- }
|