fix(model): filter applying static hooks by default if static name conflicts with mongoose middleware #14908
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.