Skip to content

Commit 8655c6c

Browse files
fix(model): skip applying static hooks by default if static name conflicts with model,document middleware
1 parent f9ca745 commit 8655c6c

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

lib/constants.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,30 @@ const aggregateMiddlewareFunctions = [
4444
];
4545

4646
exports.aggregateMiddlewareFunctions = aggregateMiddlewareFunctions;
47+
48+
/*!
49+
* ignore
50+
*/
51+
52+
const modelMiddlewareFunctions = [
53+
'bulkWrite',
54+
'createCollection',
55+
'insertMany'
56+
];
57+
58+
exports.modelMiddlewareFunctions = modelMiddlewareFunctions;
59+
60+
/*!
61+
* ignore
62+
*/
63+
64+
const documentMiddlewareFunctions = [
65+
'validate',
66+
'save',
67+
'remove',
68+
'updateOne',
69+
'deleteOne',
70+
'init'
71+
];
72+
73+
exports.documentMiddlewareFunctions = documentMiddlewareFunctions;

lib/helpers/model/applyStaticHooks.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
'use strict';
22

33
const promiseOrCallback = require('../promiseOrCallback');
4-
const { queryMiddlewareFunctions, aggregateMiddlewareFunctions } = require('../../constants');
4+
const { queryMiddlewareFunctions, aggregateMiddlewareFunctions, modelMiddlewareFunctions, documentMiddlewareFunctions } = require('../../constants');
55

66
const middlewareFunctions = [
7-
...queryMiddlewareFunctions,
8-
...aggregateMiddlewareFunctions
7+
...[
8+
...queryMiddlewareFunctions,
9+
...aggregateMiddlewareFunctions,
10+
...modelMiddlewareFunctions,
11+
...documentMiddlewareFunctions
12+
].reduce((s, hook) => s.add(hook), new Set())
913
];
1014

1115
module.exports = function applyStaticHooks(model, hooks, statics) {
@@ -14,8 +18,11 @@ module.exports = function applyStaticHooks(model, hooks, statics) {
1418
numCallbackParams: 1
1519
};
1620

21+
model.$__insertMany = hooks.createWrapper('insertMany',
22+
model.$__insertMany, model, kareemOptions);
23+
1724
hooks = hooks.filter(hook => {
18-
// If the custom static overwrites an existing query/aggregate middleware, don't apply
25+
// If the custom static overwrites an existing middleware, don't apply
1926
// middleware to it by default. This avoids a potential backwards breaking
2027
// change with plugins like `mongoose-delete` that use statics to overwrite
2128
// built-in Mongoose functions.
@@ -25,9 +32,6 @@ module.exports = function applyStaticHooks(model, hooks, statics) {
2532
return hook.model !== false;
2633
});
2734

28-
model.$__insertMany = hooks.createWrapper('insertMany',
29-
model.$__insertMany, model, kareemOptions);
30-
3135
for (const key of Object.keys(statics)) {
3236
if (hooks.hasHooks(key)) {
3337
const original = model[key];

0 commit comments

Comments
 (0)