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

TypeScript Regression: deleteMany, deleteOne, replaceOne, updateMany, updateOne no longer support arbitrary options #14341

Closed
2 tasks done
juona opened this issue Feb 8, 2024 · 5 comments
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@juona
Copy link

juona commented Feb 8, 2024

Prerequisites

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

Mongoose version

8.1.1

Node.js version

18.18.2

MongoDB server version

5

Typescript version (if applicable)

4.8.3

Description

Some of my custom plugins rely on parameters being passed to operations, like so:

await MyModel.updateMany(
    {},
    {
        $set: {
            a: "a"
        }
    },
    {
        disableAutoSession: true // <--- compilation error here
    }
);

After migrating from 7.6.7 to 8.1.1 this no longer compiles. This is true for deleteMany, deleteOne, replaceOne, updateMany and updateOne. Other methods, e.g. findOneAndDelete still accept arbitrary options:

await MyModel.findOneAndDelete(
    {},
    {
        disableAutoSession: true // <--- this is fine
    }
);

The corresponding Document methods (deleteOne, replaceOne, updateOne) accept arbitrary parameters as well.

Steps to Reproduce

See description.

Expected Behavior

No response

@vkarpov15 vkarpov15 added this to the 8.1.2 milestone Feb 8, 2024
@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Feb 8, 2024
@FaizBShah
Copy link
Contributor

@juona The reason for this is because in functions like updateMany, the typing of the options parameter contains MongooseQueryOptions<TRawDocType> instead of QueryOptions<TRawDocType> which is present in a function like findOneAndDelete. Now, the difference between the two is that MongooseQueryOptions<T> does not contain the field [other: string]: any; which is required for allowing other arbitary fields in the options object.

@FaizBShah
Copy link
Contributor

@vkarpov15 We can fix this by modifying this existing line:

options?: (mongodb.UpdateOptions & Omit<MongooseQueryOptions<TRawDocType>, 'lean'>) | null

to this:

options?: (mongodb.UpdateOptions & Omit<MongooseQueryOptions<TRawDocType>, 'lean'> & { [other: string]: any; }) | null

We can also add this field in MongooseQueryOptions, but Im not sure whether that will be a good choice

@FaizBShah
Copy link
Contributor

@vkarpov15 Can I create a PR for this to fix this? I actually started contributing to open-source, and just want to be a good devloper :D

@vkarpov15
Copy link
Collaborator

@FaizBShah sure. I'd recommend adding [other: string]: any; to MongooseQueryOptions so this change applies to all affected methods.

@vkarpov15
Copy link
Collaborator

Fixed by #14342

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

3 participants