From 2ee6d5a74f4707306b3e7f2b4d4d201f4e917b46 Mon Sep 17 00:00:00 2001 From: Livio Brunner Date: Sun, 26 Nov 2023 19:43:38 +0100 Subject: [PATCH] refactor(typeorm): improve mongodb checker --- .../database/typeorm.health.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/health-indicator/database/typeorm.health.ts b/lib/health-indicator/database/typeorm.health.ts index d6b0372b4..c8f5eaa4d 100644 --- a/lib/health-indicator/database/typeorm.health.ts +++ b/lib/health-indicator/database/typeorm.health.ts @@ -72,16 +72,22 @@ export class TypeOrmHealthIndicator extends HealthIndicator { private async checkMongoDBConnection(connection: any) { return new Promise((resolve, reject) => { - const driver = connection.driver as any; - // Hacky workaround which uses the native MongoClient + const driver = connection.driver; + const url = connection.options.url + ? connection.options.url + : driver.buildConnectionUrl(connection.options); + + // FIXME: Hacky workaround which uses the native MongoClient driver.mongodb.MongoClient.connect( - connection.options.url - ? connection.options.url - : driver.buildConnectionUrl(connection.options), + url, driver.buildConnectionOptions(connection.options), ) .catch((err: Error) => reject(new MongoConnectionError(err.message))) - .then((client: TypeOrm.MongoClient) => client.close().catch(() => {})) + .then((client: TypeOrm.MongoClient) => client.close()) + + // Noop when trying to close a closed connection + // eslint-disable-next-line @typescript-eslint/no-empty-function + .catch(() => {}) .then(() => resolve()); }); }