### Install Mongest Service Library - Bash Source: https://github.com/oodelally/mongest-nestjs-service/blob/master/README.md Installs the mongest-service library itself using npm. ```Bash npm install --save mongest-service ``` -------------------------------- ### Install Peer Dependencies - Mongest Service - Bash Source: https://github.com/oodelally/mongest-nestjs-service/blob/master/README.md Installs the necessary peer dependencies for using mongest-service, including mongodb, mongoose, and @nestjs/mongoose. ```Bash npm install --save mongodb mongoose @nestjs/mongoose ``` -------------------------------- ### Using Mongoose populate Directly on Model (JavaScript/TypeScript) Source: https://github.com/oodelally/mongest-nestjs-service/blob/master/README.md Provides an example of how to bypass the service's lean document behavior and use Mongoose's `populate` method directly on the underlying model instance when needed for features like virtual fields or population. ```JavaScript service.model.findOne().populate('myRefField').exec() ``` -------------------------------- ### Define Entity Schema and Service - NestJS/TypeScript Source: https://github.com/oodelally/mongest-nestjs-service/blob/master/README.md Defines a Mongoose schema for a 'Cat' entity and creates a NestJS service 'CatsService' by extending the BuildMongestService helper. Includes an example of overriding the constructor and adding a custom method. ```TypeScript @Schema() export class Cat { _id!: ObjectId; @Prop({ required: true, type: String }) name!: string; } @Injectable() export class CatsService extends BuildMongestService(Cat) {} // or... @Injectable() export class CatsService extends BuildMongestService(Cat) { constructor(@InjectModel(Cat.name) public model: Model) { // If you ever need to override the constructor (e.g. to add additional dependencies), // dont forget to explicitely inject the model to super(). super(model); } async myCustomMethod(): Promise { return await this.find({ name: 'pogo' }); } } ``` -------------------------------- ### Query Documents with Options - Mongest Service - TypeScript Source: https://github.com/oodelally/mongest-nestjs-service/blob/master/README.md Demonstrates how to use the find method of a Mongest service with query filters, projection, pagination (skip, limit), and sorting options. ```TypeScript const cats = await catService.find( { name: /pogo/i }, { projection: { name: 1 }, skip: 1, limit: 1, sort: { name: 1 } }, ); ``` -------------------------------- ### Demonstrating Polymorphism and Projection with find and isEntityInstanceOf (TypeScript) Source: https://github.com/oodelally/mongest-nestjs-service/blob/master/README.md Shows how to insert multiple entity types, query with projection, and use `isEntityInstanceOf` to correctly type projected documents based on their original class, even when fields are not projected. Highlights the type behavior of projected fields. ```TypeScript const strayCat: StrayCat = { name: 'Billy', kind: 'StrayCat', territorySize: 45 } const homeCat: HomeCat = { name: 'Pogo', kind: 'HomeCat', humanSlave: 'Pascal' } await catService.insertMany([strayCat, HomeCat]) const cats = await catService.find( {}, { projection: { name: 1, territorySize: 1 }, } ); for (const cat of cats) { cat // <= Type is { name: string, territorySize: unknown } if (isEntityInstanceOf(cat, StrayCat)) { cat // <= Type is { name: 1, territorySize: number } cat.territorySize // <= number } if (isEntityInstanceOf(cat, HomeCat)) { cat // <= Type is { name: 1 } cat.humanSlave // <= Error (not projected) } } ``` -------------------------------- ### Query Single Document with Projection - Mongest Service - TypeScript Source: https://github.com/oodelally/mongest-nestjs-service/blob/master/README.md Shows how to use the findOne method with a projection. Highlights how Mongest service provides lean documents and type safety based on the projection, preventing access to non-projected fields. ```TypeScript const cat = await catService.findOne( { name: /pogo/i }, { projection: { name: 1 }, }, ); if (cat) { const isCatInstance = cat instanceof Cat; // true const age = cats.age; // << TypeError: Property 'age' does not exist on type '{ name: string; _id: ObjectId; }' } ``` -------------------------------- ### Correct Projection Syntax to Avoid Type Widening (TypeScript) Source: https://github.com/oodelally/mongest-nestjs-service/blob/master/README.md Illustrates common pitfalls and correct approaches when defining projections in TypeScript to prevent type widening (`true` to `boolean`) that can hinder type inference. Recommends using `as const` or the numeric `1 | 0` syntax for projections. ```TypeScript // BAD // Type `true` is widened to `boolean`. Impossible to guess. this.catService.find({}, {projection: { name: true } }) const projectName = true; // Widened to `boolean`. Impossible to guess. this.catService.find({}, {projection: { name: projectName } }) // GOOD // Type `true` is not widened to `boolean`. Projection can be guessed. this.catService.find({}, {projection: { name: true } as const }) // BETTER // Type `1` is not widened to `number`. Projection can be guessed. // This works because projections accept `1 | 0` and not `number`. this.catService.find({}, {projection: { name: 1 } }) ``` -------------------------------- ### Handle Polymorphic Entities - Mongest Service - TypeScript Source: https://github.com/oodelally/mongest-nestjs-service/blob/master/README.md Illustrates how to define polymorphic schemas using Mongoose discriminators with Mongest service. Shows how documents are automatically cast to their specific subclass type based on the discriminator key, enabling type-safe access to subclass-specific properties. ```TypeScript export enum CatKind { StrayCat = 'StrayCat', HomeCat = 'HomeCat', } @Schema({ discriminatorKey: 'kind' }) export class Cat { kind!: CatKind; @Prop({ required: true, type: String }) name!: string; } @Schema() export class StrayCat extends Cat { @Prop({ required: true, type: Number }) territorySize!: number; } @Schema() export class HomeCat extends Cat { @Prop({ required: true, type: String }) humanSlave!: string; } const strayCat: StrayCat = { name: 'Billy', kind: 'StrayCat', territorySize: 45 } const homeCat: HomeCat = { name: 'Pogo', kind: 'HomeCat', humanSlave: 'Pascal' } await catService.insertMany([strayCat, homeCat]) const cat = await catService.find({}); for (const cat of cats) { cat // <= Type is Cat cat instanceof Cat; // true cat.name // <= Available for all cats. cat.kind // <= Available for all cats. if (cat instanceof StrayCat) { cat // <= Type is StrayCat cat.territorySize // <= Now available ! } if (cat instanceof HomeCat) { cat // <= Type is HomeCat cat.humanSlave // <= Now available ! } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.