import { AssemblerSerializer , getQueryServiceToken } from '@nestjs-query/core' ; import { FactoryProvider } from '@nestjs/common' ; import { ReturnModelType , DocumentType , mongoose } from '@typegoose/typegoose' ; import { Base } from '@typegoose/typegoose/lib/defaultClasses' ; import { getModelToken } from 'nestjs-typegoose' ; import { isClass } from 'is-class' ; import { TypegooseQueryService } from './services' ; import { TypegooseClass , TypegooseClassWithOptions , TypegooseDiscriminator } from './typegoose-interface.helpers' ; type ClassOrDiscriminator = TypegooseClassWithOptions | TypegooseDiscriminator ; type TypegooseInput = TypegooseClass | ClassOrDiscriminator ; const isTypegooseClass = ( item ) : item is TypegooseClass => isClass ( item ) ; const isTypegooseClassWithOptions = ( item ) : item is TypegooseClassWithOptions => isTypegooseClass ( item . typegooseClass ) ; AssemblerSerializer ( ( obj ) => obj . toObject ( { virtuals : true } ) ) ( mongoose . Document ) ; function ensureProperInput ( item ) { if ( isTypegooseClass ( item ) ) { return { typegooseClass : item } ; } if ( isTypegooseClassWithOptions ( item ) ) { return item ; } return undefined ; } function createTypegooseQueryServiceProvider < Entity extends Base > ( model ) { const inputModel = ensureProperInput ( model ) ; if ( ! inputModel ) { throw new Error ( `Model definitions ${ model } is incorrect.` ) ; } const modelName = inputModel . typegooseClass ?. name ; return { provide : getQueryServiceToken ( { name : modelName } ) , useFactory ( ModelClass ) { AssemblerSerializer ( ( obj ) => obj . toObject ( { virtuals : true } ) ) ( ModelClass ) ; return new TypegooseQueryService ( ModelClass ) ; } , inject : [ getModelToken ( modelName ) ] , } ; } export const createTypegooseQueryServiceProviders = ( models ) : FactoryProvider [ ] => models . map ( ( model ) => createTypegooseQueryServiceProvider ( model ) ) ;