class MongoConnectionManager { public connected: boolean; public mongoConnectionString: string; private mongoose: any; private mongoConnectionStringInternal: string; constructor(mongoose: any, mongoConnectionString: string, username: string = '', password: string = '', dbName: string = '') { mongoose.set('strictQuery', false); const parsed = new URL(mongoConnectionString); if (username) parsed.username = username; if (password) parsed.password = password; parsed.pathname = `/${dbName}`; this.connected = false; this.mongoConnectionStringInternal = parsed.toString(); // redact username/password from publicly available connection string parsed.username = '__user__'; parsed.password = '__pass__'; this.mongoConnectionString = parsed.toString(); this.mongoose = mongoose; } public async connect(): Promise { console.log(`Attempting to connect to Mongo DB address [${this.mongoConnectionString}]`); try { await this.mongoose.connect(this.mongoConnectionStringInternal, { useNewUrlParser: true, }); console.log(`Connected to MongoDB: ${this.mongoConnectionString}`); return true; } catch (e) { console.error(`There was an error connecting to MongoDB: ${this.mongoConnectionString}`); console.error(e); console.error('Exiting...'); process.exit(1); return false; } } } export default MongoConnectionManager;