Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix(model): skip applying static hooks by default if static name conf…
…licts with model,document middleware
  • Loading branch information
dragontaek-lee committed Sep 24, 2024
commit 8655c6c8ac72e3acb083a6539f50f6c538b22fe6
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;
18 changes: 11 additions & 7 deletions lib/helpers/model/applyStaticHooks.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
'use strict';

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

const middlewareFunctions = [
...queryMiddlewareFunctions,
...aggregateMiddlewareFunctions
...[
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just do Array.from(new Set([...middleware1, ...middleware2])) to avoid the need for reduce().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think your refactoring is a better approach! Thank you 👍

...queryMiddlewareFunctions,
...aggregateMiddlewareFunctions,
...modelMiddlewareFunctions,
...documentMiddlewareFunctions
].reduce((s, hook) => s.add(hook), new Set())
];

module.exports = function applyStaticHooks(model, hooks, statics) {
Expand All @@ -14,8 +18,11 @@ module.exports = function applyStaticHooks(model, hooks, statics) {
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