import { Db, MongoClient } from 'mongodb'; import { ClassType } from 'mongodb-typescript'; import logger from '../../Logger/Logger'; import { IDocument } from './ADocument'; import Repository from './Repository'; import CryptManager from '../../Crypt/CryptManager'; import { ApplicationInstall } from '../../Application/Database/ApplicationInstall'; import ApplicationInstallRepository from '../../Application/Database/ApplicationInstallRepository'; import DIContainer from '../../DIContainer/Container'; export default class MongoDbClient { private readonly _client: MongoClient; constructor(private _dsn: string, private _cryptManager: CryptManager, private _container: DIContainer) { this._client = new MongoClient(this._dsn, { connectTimeoutMS: 10000, keepAlive: true }); } get client(): MongoClient { return this._client; } public async down(): Promise { await this._client.close(true); } public async reconnect(): Promise { try { await this._client.connect(); logger.info('⚡️[server]: MongoDB Connected.', {}); } catch (err) { if (err instanceof Error) logger.error(err.message, {}); } } public async db(name?: string): Promise { await this._client.connect(); return this._client.db(name); } public async getRepository(className: ClassType): Promise> { try { const repo = this._container.getRepository(className); await repo.createIndexes(true); return repo; } catch (e) { // Ignore and create new repo } const repo = new Repository( className, this._client, (className as unknown as IDocument).getCollection(), this._cryptManager, ); await repo.createIndexes(true); return repo; } public async getApplicationRepository(): Promise> { return await this.getRepository(ApplicationInstall) as ApplicationInstallRepository; } }