Skip to content

Commit f9ca745

Browse files
authored
Merge pull request Automattic#14904 from dragontaek-lee/skip-applying-static-hooks-aggregate-middleware
fix(model): skip applying static hooks by default if static name conflicts with aggregate middleware
2 parents 0b81c6e + cf768dc commit f9ca745

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

lib/constants.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,13 @@ const queryMiddlewareFunctions = queryOperations.concat([
3434
]);
3535

3636
exports.queryMiddlewareFunctions = queryMiddlewareFunctions;
37+
38+
/*!
39+
* ignore
40+
*/
41+
42+
const aggregateMiddlewareFunctions = [
43+
'aggregate'
44+
];
45+
46+
exports.aggregateMiddlewareFunctions = aggregateMiddlewareFunctions;

lib/helpers/model/applyStaticHooks.js

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

3-
const middlewareFunctions = require('../../constants').queryMiddlewareFunctions;
43
const promiseOrCallback = require('../promiseOrCallback');
4+
const { queryMiddlewareFunctions, aggregateMiddlewareFunctions } = require('../../constants');
5+
6+
const middlewareFunctions = [
7+
...queryMiddlewareFunctions,
8+
...aggregateMiddlewareFunctions
9+
];
510

611
module.exports = function applyStaticHooks(model, hooks, statics) {
712
const kareemOptions = {
@@ -10,7 +15,7 @@ module.exports = function applyStaticHooks(model, hooks, statics) {
1015
};
1116

1217
hooks = hooks.filter(hook => {
13-
// If the custom static overwrites an existing query middleware, don't apply
18+
// If the custom static overwrites an existing query/aggregate middleware, don't apply
1419
// middleware to it by default. This avoids a potential backwards breaking
1520
// change with plugins like `mongoose-delete` that use statics to overwrite
1621
// built-in Mongoose functions.

test/model.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const assert = require('assert');
1010
const { once } = require('events');
1111
const random = require('./util').random;
1212
const util = require('./util');
13+
const model = require('../lib/model');
1314

1415
const mongoose = start.mongoose;
1516
const Schema = mongoose.Schema;
@@ -5861,6 +5862,35 @@ describe('Model', function() {
58615862

58625863
});
58635864

5865+
it('custom statics that overwrite aggregate functions dont get hooks by default (gh-14903)', async function() {
5866+
5867+
const schema = new Schema({ name: String });
5868+
5869+
schema.statics.aggregate = function(pipeline) {
5870+
return model.aggregate.apply(this, [pipeline]);
5871+
};
5872+
5873+
let called = 0;
5874+
schema.pre('aggregate', function(next) {
5875+
++called;
5876+
next();
5877+
});
5878+
const Model = db.model('Test', schema);
5879+
5880+
await Model.create({ name: 'foo' });
5881+
5882+
const res = await Model.aggregate([
5883+
{
5884+
$match: {
5885+
name: 'foo'
5886+
}
5887+
}
5888+
]);
5889+
5890+
assert.ok(res[0].name);
5891+
assert.equal(called, 1);
5892+
});
5893+
58645894
it('error handling middleware passes saved doc (gh-7832)', async function() {
58655895
const schema = new Schema({ _id: Number });
58665896

0 commit comments

Comments
 (0)