UNPKG

mongoose

Version:
42 lines (36 loc) 1.57 kB
'use strict'; const { middlewareFiltersFromLastArg } = require('../buildMiddlewareFilter'); const { queryMiddlewareFunctions, aggregateMiddlewareFunctions, modelMiddlewareFunctions, documentMiddlewareFunctions } = require('../../constants'); const middlewareFunctions = Array.from( new Set([ ...queryMiddlewareFunctions, ...aggregateMiddlewareFunctions, ...modelMiddlewareFunctions, ...documentMiddlewareFunctions ]) ); module.exports = function applyStaticHooks(model, hooks, statics) { hooks = hooks.filter(hook => { // If the custom static overwrites an existing middleware, don't apply // middleware to it by default. This avoids a potential backwards breaking // change with plugins like `mongoose-delete` that use statics to overwrite // built-in Mongoose functions. if (middlewareFunctions.indexOf(hook.name) !== -1) { return !!hook.model; } return hook.model !== false; }); for (const key of Object.keys(statics)) { if (hooks.hasHooks(key)) { const original = model[key]; // Only read the `middleware` option from the static's last argument if // the static opts in via `supportsMiddlewareOption`. Custom statics have // arbitrary signatures, so the last argument may be an object with an // unrelated `middleware` property. const wrapperOptions = original.supportsMiddlewareOption === true ? { getOptions: middlewareFiltersFromLastArg } : undefined; model[key] = hooks.createWrapper(key, original, null, wrapperOptions); } } };