Skip to content

Commit

Permalink
Merge pull request #14908 from dragontaek-lee/skip-applying-static-ho…
Browse files Browse the repository at this point in the history
…oks-model-document-middleware

fix(model): �filter applying static hooks by default if static name conflicts with mongoose middleware
  • Loading branch information
vkarpov15 authored Sep 25, 2024
2 parents f9ca745 + 1f7c742 commit cc60077
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 9 deletions.
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 @@ module.exports = function applyStaticHooks(model, hooks, statics) {
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

0 comments on commit cc60077

Please sign in to comment.