-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
fix(model): filter applying static hooks by default if static name conflicts with mongoose middleware #14908
fix(model): filter applying static hooks by default if static name conflicts with mongoose middleware #14908
Conversation
…licts with model,document middleware
…licts with model,document middleware
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made one quick refactor but otherwise LGTM 👍
|
||
const middlewareFunctions = [ | ||
...queryMiddlewareFunctions, | ||
...aggregateMiddlewareFunctions | ||
...[ |
There was a problem hiding this comment.
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()
.
There was a problem hiding this comment.
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 👍
Additional Fix for PR: #14904
Summary
Ensure that if a custom static method overwrites an existing mongoose middleware, the middleware is not applied by default, similar to how it works with query and aggregate middleware.
applyStaticHooks.js
I used a
Set()
to eliminate duplicates in the following three function names: updateOne, deleteOne, and validate.Examples
The following example demonstrates the newly implemented filtering applied to model and document middleware.
Suppose that the static method overwrites the built-in Mongoose
save
/insertMany
function.In this case, also suppose that there are pre and post hooks for save, insertMany.
With this PR, when a custom static method overwrites an existing model or document middleware as described above, the middleware is no longer applied by default. As a result, the this context type in pre/post hooks is correctly set as intended.