Skip to content

Conversation

@arkist
Copy link
Contributor

@arkist arkist commented Nov 2, 2022

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe: sample/06-mongoose

What is the current behavior?

Currently, techniques/mongodb doc uses intersection type for defining the mongoose document interface.

import { Document } from 'mongoose';

export type CatDocument = Cat & Document;

it was okay before [email protected] but after that, they replaced the use of this with generics(T) in many Document methods so it infers incorrectly now.

// before
toObject(options?: ToObjectOptions): LeanDocument<this>;
toObject<T = DocType>(options?: ToObjectOptions): T;

// latest
toObject<T = LeanDocument<DocType>>(options?: ToObjectOptions): Require_id<T>;

// from: https://github.com/Automattic/mongoose/commit/659436de47d55aaa0a984b8f4cc99232c5ca8a16
type CatDocument = Cat & Document;

class CatsService {
  constructor(@InjectModel(Cat.name) private catModel: Model<CatDocument>) {}

  create() {
    const cat = await this.catModel.create(createCatDto);
    const catObj = cat.toObject();
    // const catObj: LeanDocument<any> & Required<{_id: unknown}>

    return catObj;
  }
}

What is the new behavior?

Mongoose introduced HydratedDocument from 6.0.13.

import { HydratedDocument } from 'mongoose';

export type CatDocument = HydratedDocument<Cat>;

defining document interface as HydratedDocument of Cat so can infer the type from Document methods correctly.

type CatDocument = HydratedDocument<Cat>;

class CatsService {
  constructor(@InjectModel(Cat.name) private catModel: Model<CatDocument>) {}

  create() {
    const cat = await this.catModel.create(createCatDto);
    const catObj = cat.toObject();
    // const catObj: LeanDocument<Cat> & {_id: Types.ObjectId}

    return catObj;
  }
}

refs:
Automattic/mongoose@8120c8f
https://github.com/Automattic/mongoose/blob/6.7.0/docs/typescript.md#creating-your-first-document

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

from nestjs/docs.nestjs.com#2517

@coveralls
Copy link

Pull Request Test Coverage Report for Build 8a43a703-0cb9-4cfb-bcbe-d57efea9080d

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 93.788%

Totals Coverage Status
Change from base Build 96726f7e-4402-44a7-8868-aa46bec194e5: 0.0%
Covered Lines: 6115
Relevant Lines: 6520

💛 - Coveralls

@kamilmysliwiec kamilmysliwiec merged commit 276e824 into nestjs:master Nov 3, 2022
@kamilmysliwiec
Copy link
Member

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants