Skip to content

When migrating from mongoose 5 to 7, how to get LeanDocument funcitonality? #13424

@EcksDy

Description

@EcksDy

Prerequisites

  • I have written a descriptive issue title

Mongoose version

7.2.0

Node.js version

18.16.0

MongoDB version

6.0

Operating system

macOS

Operating system version (i.e. 20.04, 11.3, 10)

No response

Issue

I'm trying to migrate our nestjs/mongoose codebase from version 5 to 7.

We've been using using T.lean() or T.toObject() and our return types would be LeanDocument<T>. Mongoose 7 no longer exports LeanDocument, and the existing migration guide suggests using the following setup:

// Do this instead, no `extends Document`
interface ITest {
  name?: string;
}
const Test = model<ITest>('Test', schema);

// If you need to access the hydrated document type, use the following code
type TestDocument = ReturnType<(typeof Test)['hydrate']>;

But this gives me HydratedDocument that I can get by HydratedDocument<T>, which is not what I want since it has all the document methods on it.
As an alternative I can use just T as my return type, but then any Document<T> is matching T.

I'd like to enforce that the result is a POJO, LeanDocument or just the documents fields and virtuals, and not a Document.
What approach can I take?

P.S.
I've asked the same question on stackoverflow, feel free to answer there also, I'd gladly accept the answer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    typescriptTypes or Types-test related issue / Pull Request

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions