import { getModelForClass } from "@typegoose/typegoose" ; import mongoose , { Connection } from "mongoose" ; import { MG_MODEL_METADATA } from "../core/decorator/ServiceDecorator" ; import { defaultConfig } from "../config" ; export interface MongoConnectionOptions { uris : string ; options : mongoose . ConnectionOptions ; } export default class Mongoer { static instance : Mongoer ; static getInstance ( ) { if ( ! Mongoer . instance ) { Mongoer . instance = new Mongoer ( ) ; } return Mongoer . instance ; } static getCurrInstance ( ) { return Mongoer . instance ; } private connections : Map < string , Connection > = new Map ( ) ; async addConnect ( opts ) { const conn = await mongoose . createConnection ( opts . uris , opts . options ) . catch ( ( err ) => { throw err ; } ) ; if ( conn ) { this . connections . set ( opts . uris , conn ) ; console . log ( `${ opts . uris } connected` . green ) ; } return conn ; } async inject ( ) { const services = Reflect . getMetadata ( MG_MODEL_METADATA , Mongoer ) ; if ( services ) { for ( const service of services ) { const { key , target , model , options } = service ; if ( target [ key ] ) { return ; } const opts = options || defaultConfig . mongo ; const conn = this . connections . has ( opts . uris ) ? this . connections . get ( opts . uris ) : await this . addConnect ( opts ) ; target [ key ] = getModelForClass ( model , { existingConnection : conn || undefined , } ) ; } } } } param property param validationOptions