fix(model): skip applying static hooks by default if static name conflicts with aggregate middleware #14904
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.
Fix #14903
Summary
Make it so that if the custom static overwrites an existing aggregate middleware, don't apply middleware to it by default. This should be achieved by filtering custom static-named aggregates in
applyStaticHooks.js
.Furthermore, aside from this PR, I believe we should extend this filtering not only to query and aggregation middleware but also to other Mongoose middlewares, including Document and Model middleware. Does that make sense? If so, I would like to create another PR for this work.
Examples
Assume that the static method overwrites the built-in Mongoose aggregate function.
In this situation, also assume that there are pre and post hooks for aggregate.
With this PR, when a custom static method overwrites an existing aggregate middleware as described above, the middleware is not applied by default. As a result, the
this
context type in pre/post hooks is correctly set to Aggregate, as intended.