import { AssemblerDeserializer , AssemblerSerializer , Class , getQueryServiceToken } from '@nestjs-query/core' ; import { FactoryProvider } from '@nestjs/common' ; import { getModelToken , ModelDefinition } from '@nestjs/mongoose' ; import { Model , Document } from 'mongoose' ; import { MongooseQueryService } from './services' ; export type NestjsQueryModelDefinition < Entity extends Document > = { document : Class < Entity > ; } & ModelDefinition ; AssemblerSerializer ( ( obj ) => obj . toObject ( { virtuals : true } ) ) ( Document ) ; function createMongooseQueryServiceProvider < Entity extends Document > ( model ) { return { provide : getQueryServiceToken ( model . document ) , useFactory ( ModelClass ) { AssemblerDeserializer < Entity > ( ( obj ) => new ModelClass ( obj ) ) ( model . document ) ; return new MongooseQueryService < Entity > ( ModelClass ) ; } , inject : [ getModelToken ( model . name ) ] , } ; } export const createMongooseQueryServiceProviders = ( models ) : FactoryProvider [ ] => models . map ( ( model ) => createMongooseQueryServiceProvider ( model ) ) ;