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

Some middleware does not have access to operation options #13633

Closed
2 tasks done
juona opened this issue Jul 19, 2023 · 2 comments · Fixed by #13708
Closed
2 tasks done

Some middleware does not have access to operation options #13633

juona opened this issue Jul 19, 2023 · 2 comments · Fixed by #13708
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@juona
Copy link

juona commented Jul 19, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

7.4.0

Node.js version

16.19.1

MongoDB server version

5

Typescript version (if applicable)

4.8.3

Description

It is not possible to access the operation options in the following middleware functions:

  • document pre/post updateOne / deleteOne
  • document post save
  • pre/post insertMany

I know that the pre middlewares of the above operations do in fact get a reference to the options object supplied by the client. Unfortunately, these objects are not included in the type definitions of the middleware functions, so it is not possible to access them without overriding the types.

Steps to Reproduce

schema.pre("updateOne", { document: true, query: false }, function(next) {
    // no way to access operation options here
})

schema.pre("updateOne", { document: true, query: false }, function(next, options) {
    // does not compile
})

schema.post("save", function(res, next) {
    // no way to access operation options here
})

schema.post("save", function(res, next, options) {
    // does not compile
})

schema.pre("insertMany", function(next, docs) {
    // no way to access operation options here
})

schema.pre("insertMany", function(next, docs, options) {
    // does not compile
})

Same with post updateOne, pre/post deleteOne, post insertMany

Expected Behavior

Ideally I'd like to be able to always access operation options like this can be done with query / aggregation middleware:

schema.pre("find", function(next) {
    this.getOptions()
})

schema.pre("aggregate", function(next) {
    this.options
})

I understand that this might not be possible with Document middleware, in which case it would be nice to receive the options as the last argument to the middleware. Matter of fact, this is already the case, only the types do not reflect that.

@IslandRhythms IslandRhythms added the enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature label Jul 19, 2023
@vkarpov15 vkarpov15 added this to the 7.4.3 milestone Jul 22, 2023
@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Jul 22, 2023
@vkarpov15
Copy link
Collaborator

With #13708, everything except the following will compile.

schema.post("save", function(res, next, options) {
    // does not compile
})

That's because post save hooks don't get options. As a matter of fact, function(res, next, options) will unfortunately end up as error handling middleware, so will execute with (err, res, next).

To access options in post hooks, do the following:

schema.pre('save', function(next, options) {
  this.$locals.saveOptions = options;
});

schema.post('save', function() {
  this.$locals.saveOptions; // Get options from initial save
});

vkarpov15 added a commit that referenced this issue Aug 9, 2023
types: allow accessing `options` from pre middleware
@juona
Copy link
Author

juona commented Aug 21, 2023

Migrating to v7 at the moment, tested this out - looks good. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants