Skip to content

Commit cc60077

Browse files
authored
Merge pull request Automattic#14908 from dragontaek-lee/skip-applying-static-hooks-model-document-middleware
fix(model): �filter applying static hooks by default if static name conflicts with mongoose middleware
2 parents f9ca745 + 1f7c742 commit cc60077

File tree

3 files changed

+88
-9
lines changed

3 files changed

+88
-9
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: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
'use strict';
22

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

6-
const middlewareFunctions = [
7-
...queryMiddlewareFunctions,
8-
...aggregateMiddlewareFunctions
9-
];
6+
const middlewareFunctions = Array.from(
7+
new Set([
8+
...queryMiddlewareFunctions,
9+
...aggregateMiddlewareFunctions,
10+
...modelMiddlewareFunctions,
11+
...documentMiddlewareFunctions
12+
])
13+
);
1014

1115
module.exports = function applyStaticHooks(model, hooks, statics) {
1216
const kareemOptions = {
1317
useErrorHandlers: true,
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];

test/model.test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5891,6 +5891,54 @@ describe('Model', function() {
58915891
assert.equal(called, 1);
58925892
});
58935893

5894+
it('custom statics that overwrite model functions dont get hooks by default', async function() {
5895+
5896+
const schema = new Schema({ name: String });
5897+
5898+
schema.statics.insertMany = function(docs) {
5899+
return model.insertMany.apply(this, [docs]);
5900+
};
5901+
5902+
let called = 0;
5903+
schema.pre('insertMany', function(next) {
5904+
++called;
5905+
next();
5906+
});
5907+
const Model = db.model('Test', schema);
5908+
5909+
const res = await Model.insertMany([
5910+
{ name: 'foo' },
5911+
{ name: 'boo' }
5912+
]);
5913+
5914+
assert.ok(res[0].name);
5915+
assert.ok(res[1].name);
5916+
assert.equal(called, 1);
5917+
});
5918+
5919+
it('custom statics that overwrite document functions dont get hooks by default', async function() {
5920+
5921+
const schema = new Schema({ name: String });
5922+
5923+
schema.statics.save = function() {
5924+
return 'foo';
5925+
};
5926+
5927+
let called = 0;
5928+
schema.pre('save', function(next) {
5929+
++called;
5930+
next();
5931+
});
5932+
5933+
const Model = db.model('Test', schema);
5934+
5935+
const doc = await Model.save();
5936+
5937+
assert.ok(doc);
5938+
assert.equal(doc, 'foo');
5939+
assert.equal(called, 0);
5940+
});
5941+
58945942
it('error handling middleware passes saved doc (gh-7832)', async function() {
58955943
const schema = new Schema({ _id: Number });
58965944

0 commit comments

Comments
 (0)