This commit is contained in:
2026-04-02 17:50:10 +08:00
parent 60697d80be
commit ce0aa4f789
373 changed files with 82520 additions and 46 deletions

View File

@@ -0,0 +1,33 @@
'use strict';
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];
model[key] = hooks.createWrapper(key, original);
}
}
};