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

model.create does not have a lean option, and using toObject and toJSON does not produce similar typing #15077

Open
2 tasks done
Amoodaa opened this issue Dec 5, 2024 · 1 comment · May be fixed by #15097
Open
2 tasks done
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. typescript Types or Types-test related issue / Pull Request

Comments

@Amoodaa
Copy link

Amoodaa commented Dec 5, 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.8.3

Node.js version

22.11.0

MongoDB server version

7.0

Typescript version (if applicable)

4.9.5

Description

While upgrading to the new 8.8.3 due to a dependabot alert, types broke for my app,
when doing a find, if not found create scenario and reusing the let variable, which has a type inferred already, as a LeanDocument (only exists on find/Query type) which is compatible with lots of other types previously, but is not anymore due to the addition of the __v to it, and not in toObject and toJSON which are my usual workarounds to get a type from a newly created Document (Document type)
Doing the following workarounds that i usually use does not work anymore, (except casting which i want to avoid):

  • use document.toObject this used to produce a type identical
 let currentRecord = await recordModel.findById(recordId).lean().exec();

    if (!currentRecord) {
      let newRecord = {
      ... datapoints
      };

      const createdRecord = await recordModel.create(newRecord);

      currentRecord = createdRecord.toObject(); // used to work with no type errors in 8.7.3
    }

TS error using 8.8.3:

src/file.ts(253,7): error TS2322: Type 'T & Required<{ _id: ObjectId; }>' is not assignable to type '{ _id?: ObjectId; ... 38 more ... } & Required<...> & { ...; }'.
Property '__v' is missing in type 'T & Required<{ _id: ObjectId; }>' but required in type '{ __v: number; }'.

Output of toObject:
T & Required<{ _id: mongoose.Types.ObjectId; }>

Output of lean.exec which gives a raw-er datatype (which used to work fine):
{ _id?: mongoose.Types.ObjectId; ... 38 more ...; } & Required<{ _id: mongoose.Types.ObjectId; }> & { __v: number; }

Steps to Reproduce

https://github.com/Amoodaa/mongoose-8-8-type-error

On 8.8.3:

index.ts:43:5 - error TS2322: Type 'Foo & { _id: ObjectId; }' is not assignable to type '{ state: "on" | "off"; } & { _id: ObjectId; } & { __v: number; }'.
  Property '__v' is missing in type 'Foo & { _id: ObjectId; }' but required in type '{ __v: number; }'.

43     foundFoo = createdFoo.toObject(); // this errors
       ~~~~~~~~

  node_modules/mongoose/types/index.d.ts:143:13
    143     : T & { __v: number };
                    ~~~
    '__v' is declared here.


Found 1 error in index.ts:43

On 8.7.3: no errors, check readme for more

Expected Behavior

I would expect there would some kind of type similarity between the different lean versions? like toObject
A seamless way to get away from mongoose types and go back to my defined types, or at least type that is very compatible with that,
I've migrated from 6 -> 7 -> 8 recently and have faced many problems with mongoose typing solution, but its obviously better than being blind so i really appreciate your work on typings, thank you so much

@Amoodaa
Copy link
Author

Amoodaa commented Dec 5, 2024

currently using the following workaround

      currentRecord = createdRecord.toObject() as typeof currentRecord;

Happy to work on this after discussion concludes

@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. typescript Types or Types-test related issue / Pull Request labels Dec 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants