import { Injectable } from '@nestjs/common' ; import { ReturnModelType } from '@typegoose/typegoose' ; import { InjectModel } from 'nestjs-typegoose' ; import { NoteModel } from '~/modules/note/note.model' ; import { PageModel } from '~/modules/page/page.model' ; import { PostModel } from '~/modules/post/post.model' ; @ Injectable ( ) export class DatabaseService { constructor ( @ InjectModel ( PostModel ) private readonly postModel , @ InjectModel ( NoteModel ) private readonly noteModel , @ InjectModel ( PageModel ) private readonly pageModel ) { } public getModelByRefType ( type ) ; public getModelByRefType ( type ) ; public getModelByRefType ( type ) ; public getModelByRefType ( type ) ; public getModelByRefType ( type ) ; public getModelByRefType ( type ) ; public getModelByRefType ( type ) { type = type . toLowerCase ( ) as any ; const map = new Map < any , any > ( [ [ 'post' , this . postModel ] , [ 'note' , this . noteModel ] , [ 'page' , this . pageModel ] , ] as any ) ; return map . get ( type ) as any as ReturnModelType < typeof NoteModel | typeof PostModel | typeof PageModel > ; } public async findGlobalById ( id ) { const doc = await Promise . all ( [ this . postModel . findById ( id ) . populate ( 'category' ) . lean ( ) , this . noteModel . findById ( id ) . lean ( ) . select ( '+password' ) , this . pageModel . findById ( id ) . lean ( ) , ] ) ; const index = doc . findIndex ( Boolean ) ; if ( index == - 1 ) { return { document : null , type : null , } ; } const document = doc [ index ] ; return { document , type : [ 'Post' , 'Note' , 'Page' ] [ index ] , } ; } }