import { MongoClient } from 'mongodb' ; import MongoConfig from './MongoConfig' ; export class MongoClientFactory { private static clients : { [ key ] : MongoClient ; } = { } ; static async createClient ( contextName , config ) { let client = MongoClientFactory . getClient ( contextName ) ; if ( ! client ) { client = await MongoClientFactory . createAndConnectClient ( config ) ; MongoClientFactory . registerClient ( client , contextName ) ; } return client ; } private static getClient ( contextName ) { return MongoClientFactory . clients [ contextName ] ; } private static async createAndConnectClient ( config ) { const client = new MongoClient ( config . url , { ignoreUndefined : true } ) ; await client . connect ( ) ; return client ; } private static registerClient ( client , contextName ) { MongoClientFactory . clients [ contextName ] = client ; } }