import * as mongoose from 'mongoose' ; import { MongoMemoryServer } from 'mongodb-memory-server' ; import * as chalk from 'chalk' ; const connString = process . env . MONGO_CONNECTION || '' ; const mongod = new MongoMemoryServer ( ) ; export const dbConfig = { useMongoClient : true } ; export async function connectDb ( appStart ) { ( mongoose as any ) . Promise = global . Promise ; const uri = await mongod . getConnectionString ( ) ; const mongooseOpts = { useNewUrlParser : true , autoReconnect : true , reconnectTries : Number . MAX_VALUE , reconnectInterval : 1000 , useMongoClient : true } ; try { const db = await mongoose . connect ( uri , mongooseOpts ) ; mongoose . set ( 'debug' , true ) ; if ( appStart ) appStart ( ) ; } catch ( error ) { console . error ( chalk . red ( 'Could not connect to MongoDB!' , error ) ) ; } } ;