All files / mongoose/lib/helpers/model applyHooks.js

67.79% Statements 40/59
69.04% Branches 29/42
66.66% Functions 4/6
67.24% Lines 39/58

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151    1x 1x           1x           1x                         6x                       8x   8x           8x   8x 8x 32x 32x 32x 2x 30x 2x   28x     4x       4x 4x                         8x   72x     72x 16x   56x     56x       72x       72x     8x   8x   8x 8x 20x 20x   20x   8x       8x 8x             8x                                                  
'use strict';
 
const symbols = require('../../schema/symbols');
const promiseOrCallback = require('../promiseOrCallback');
 
/*!
 * ignore
 */
 
module.exports = applyHooks;
 
/*!
 * ignore
 */
 
applyHooks.middlewareFunctions = [
  'deleteOne',
  'save',
  'validate',
  'remove',
  'updateOne',
  'init'
];
 
/*!
 * ignore
 */
 
const alreadyHookedFunctions = new Set(applyHooks.middlewareFunctions.flatMap(fn => ([fn, `$__${fn}`])));
 
/**
 * Register hooks for this model
 *
 * @param {Model} model
 * @param {Schema} schema
 * @param {Object} options
 * @api private
 */
 
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;
    }
 
    Iif (childModel.$appliedHooks) {
      continue;
    }
 
    applyHooks(childModel, type.schema, { ...options, isChildSchema: true });
    Iif (childModel.discriminators != null) {
      const keys = Object.keys(childModel.discriminators);
      for (const key of keys) {
        applyHooks(childModel.discriminators[key],
          childModel.discriminators[key].schema, options);
      }
    }
  }
 
  // Built-in hooks rely on hooking internal functions in order to support
  // promises and make it so that `doc.save.toString()` provides meaningful
  // information.
 
  const middleware = schema.s.hooks.
    filter(hook => {
      Iif (hook.name === 'updateOne' || hook.name === 'deleteOne') {
        return !!hook['document'];
      }
      if (hook.name === 'remove' || hook.name === 'init') {
        return hook['document'] == null || !!hook['document'];
      }
      Iif (hook.query != null || hook.document != null) {
        return hook.document !== false;
      }
      return true;
    }).
    filter(hook => {
      // If user has overwritten the method, don't apply built-in middleware
      Iif (schema.methods[hook.name]) {
        return !hook.fn[symbols.builtInMiddleware];
      }
 
      return true;
    });
 
  model._middleware = middleware;
 
  objToDecorate.$__originalValidate = objToDecorate.$__originalValidate || objToDecorate.$__validate;
 
  const internalMethodsToWrap = options && options.isChildSchema ? ['save', 'validate', 'deleteOne'] : ['save', 'validate'];
  for (const method of internalMethodsToWrap) {
    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);
 
  // Support hooks for custom methods
  const customMethods = Object.keys(schema.methods);
  const customMethodOptions = Object.assign({}, kareemOptions, {
    // Only use `checkForPromise` for custom methods, because mongoose
    // query thunks are not as consistent as I would like about returning
    // a nullish value rather than the query. If a query thunk returns
    // a query, `checkForPromise` causes infinite recursion
    checkForPromise: true
  });
  for (const method of customMethods) {
    if (alreadyHookedFunctions.has(method)) {
      continue;
    }
    if (!middleware.hasHooks(method)) {
      // Don't wrap if there are no hooks for the custom method to avoid
      // surprises. Also, `createWrapper()` enforces consistent async,
      // so wrapping a sync method would break it.
      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);
  }
}