Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(model): filter applying static hooks by default if static name conflicts with mongoose middleware #14908

Conversation

dragontaek-lee
Copy link
Contributor

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

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

I used a Set() to eliminate duplicates in the following three function names: updateOne, deleteOne, and validate.

  1. Document middleware's updateOne and Query middleware's updateOne
  2. Document middleware's deleteOne and Query middleware's deleteOne
  3. Document middleware's validate and Query middleware's validate

Examples

The following example demonstrates the newly implemented filtering applied to model and document middleware.

schema.statics.save = function() {
  ...
};

schema.statics.insertMany = function() {
  ...
};

Suppose that the static method overwrites the built-in Mongoose save / insertMany function.

schema.pre('save', function(next) {
    console.log(this);
    next();
});

schema.post('save', function() {
    console.log(this);
});

schema.pre('insertMany', function(next) {
    console.log(this);
    next();
});

schema.post('insertMany', function() {
    console.log(this);
});

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.

Copy link
Collaborator

@vkarpov15 vkarpov15 left a 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
...[
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 👍

@vkarpov15 vkarpov15 added this to the 8.6.4 milestone Sep 25, 2024
@vkarpov15 vkarpov15 merged commit cc60077 into Automattic:master Sep 25, 2024
23 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants