Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(model): filter applying static hooks by default if static name conflicts with mongoose middleware #14908

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,30 @@ const aggregateMiddlewareFunctions = [
];

exports.aggregateMiddlewareFunctions = aggregateMiddlewareFunctions;

/*!
* ignore
*/

const modelMiddlewareFunctions = [
'bulkWrite',
'createCollection',
'insertMany'
];

exports.modelMiddlewareFunctions = modelMiddlewareFunctions;

/*!
* ignore
*/

const documentMiddlewareFunctions = [
'validate',
'save',
'remove',
'updateOne',
'deleteOne',
'init'
];

exports.documentMiddlewareFunctions = documentMiddlewareFunctions;
22 changes: 13 additions & 9 deletions lib/helpers/model/applyStaticHooks.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
'use strict';

const promiseOrCallback = require('../promiseOrCallback');
const { queryMiddlewareFunctions, aggregateMiddlewareFunctions } = require('../../constants');
const { queryMiddlewareFunctions, aggregateMiddlewareFunctions, modelMiddlewareFunctions, documentMiddlewareFunctions } = require('../../constants');

const middlewareFunctions = [
...queryMiddlewareFunctions,
...aggregateMiddlewareFunctions
];
const middlewareFunctions = Array.from(
new Set([
...queryMiddlewareFunctions,
...aggregateMiddlewareFunctions,
...modelMiddlewareFunctions,
...documentMiddlewareFunctions
])

Check failure on line 12 in lib/helpers/model/applyStaticHooks.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Trailing spaces not allowed
);

module.exports = function applyStaticHooks(model, hooks, statics) {
const kareemOptions = {
useErrorHandlers: true,
numCallbackParams: 1
};

model.$__insertMany = hooks.createWrapper('insertMany',
model.$__insertMany, model, kareemOptions);

hooks = hooks.filter(hook => {
// If the custom static overwrites an existing query/aggregate middleware, don't apply
// 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.
Expand All @@ -25,9 +32,6 @@
return hook.model !== false;
});

model.$__insertMany = hooks.createWrapper('insertMany',
model.$__insertMany, model, kareemOptions);

for (const key of Object.keys(statics)) {
if (hooks.hasHooks(key)) {
const original = model[key];
Expand Down
48 changes: 48 additions & 0 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5891,6 +5891,54 @@ describe('Model', function() {
assert.equal(called, 1);
});

it('custom statics that overwrite model functions dont get hooks by default', async function() {

const schema = new Schema({ name: String });

schema.statics.insertMany = function(docs) {
return model.insertMany.apply(this, [docs]);
};

let called = 0;
schema.pre('insertMany', function(next) {
++called;
next();
});
const Model = db.model('Test', schema);

const res = await Model.insertMany([
{ name: 'foo' },
{ name: 'boo' }
]);

assert.ok(res[0].name);
assert.ok(res[1].name);
assert.equal(called, 1);
});

it('custom statics that overwrite document functions dont get hooks by default', async function() {

const schema = new Schema({ name: String });

schema.statics.save = function() {
return 'foo';
};

let called = 0;
schema.pre('save', function(next) {
++called;
next();
});

const Model = db.model('Test', schema);

const doc = await Model.save();

assert.ok(doc);
assert.equal(doc, 'foo');
assert.equal(called, 0);
});

it('error handling middleware passes saved doc (gh-7832)', async function() {
const schema = new Schema({ _id: Number });

Expand Down
Loading