import { Db , MongoClient } from 'mongodb' ; export default class Mongo { private static instance : Mongo ; public db : Promise < Db > ; private client : Promise < MongoClient > ; private connect ( ) { return this . client . then ( ( client ) => client . db ( ) ) . catch ( ( ) => this . connect ( ) ) ; } private constructor ( ) { this . client = MongoClient . connect ( process . env . MONGO_URL as string , { useUnifiedTopology : true } ) ; this . db = this . connect ( ) ; } public static getInstance ( ) { if ( ! Mongo . instance ) Mongo . instance = new Mongo ( ) ; return Mongo . instance ; } public exit ( ) { return new Promise ( ( resolve ) => { this . client . then ( ( client ) => setTimeout ( ( ) => { client . close ( ) . then ( ( ) => { console . log ( 'Manually disconnected from mongo' ) ; resolve ( true ) ; } ) ; } , 3000 ) ) ; } ) ; } }