Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 4x 4x 4x 4x | import { MongoClient, Db } from 'mongodb'; import { DbConnectionConfig } from '../types/db'; import { Logger } from '../logger/Logger'; let db: Db; /** * @function connectDB * @description Connects to the MongoDB database. * @param {DbConnectionConfig} config - The database connection configuration. */ export const connectDB = async (config: DbConnectionConfig) => { try { const client = new MongoClient(config.url, config.options); await client.connect(); db = client.db(config.dbName); const logger = Logger.getInstance(); logger.info('Connected to MongoDB'); } catch (error) { const logger = Logger.getInstance(); logger.critical('Could not connect to MongoDB', error); process.exit(1); } }; /** * @function getDB * @description Gets the database instance. * @returns {Db} The database instance. */ export const getDB = (): Db => { if (!db) { throw new Error('DB not initialized'); } return db; }; |