diff --git a/lib/health-indicator/database/typeorm.health.ts b/lib/health-indicator/database/typeorm.health.ts index 9bf3d3d418..13c51253fa 100644 --- a/lib/health-indicator/database/typeorm.health.ts +++ b/lib/health-indicator/database/typeorm.health.ts @@ -2,18 +2,14 @@ import { Injectable, Scope } from '@nestjs/common'; import { ModuleRef } from '@nestjs/core'; import type * as NestJSTypeOrm from '@nestjs/typeorm'; import type * as TypeOrm from 'typeorm'; -import { HealthIndicator, type HealthIndicatorResult } from '../'; -import { - TimeoutError, - ConnectionNotFoundError, - MongoConnectionError, -} from '../../errors'; -import { HealthCheckError } from '../../health-check/health-check.error'; +import { HealthIndicator } from '../'; +import { MongoConnectionError } from '../../errors'; import { TimeoutError as PromiseTimeoutError, promiseTimeout, checkPackages, } from '../../utils'; +import { HealthIndicatorService } from '../health-indicator.service'; export interface TypeOrmPingCheckSettings { /** @@ -36,12 +32,10 @@ export interface TypeOrmPingCheckSettings { */ @Injectable({ scope: Scope.TRANSIENT }) export class TypeOrmHealthIndicator extends HealthIndicator { - /** - * Initializes the TypeOrmHealthIndicator - * - * @param {ModuleRef} moduleRef The NestJS module reference - */ - constructor(private moduleRef: ModuleRef) { + constructor( + private readonly moduleRef: ModuleRef, + private readonly healthIndicatorService: HealthIndicatorService, + ) { super(); this.checkDependantPackages(); } @@ -127,10 +121,11 @@ export class TypeOrmHealthIndicator extends HealthIndicator { * @example * typeOrmHealthIndicator.pingCheck('database', { timeout: 1500 }); */ - async pingCheck( - key: string, + async pingCheck( + key: Key, options: TypeOrmPingCheckSettings = {}, - ): Promise { + ) { + const check = this.healthIndicatorService.check(key); this.checkDependantPackages(); const connection: TypeOrm.DataSource | null = @@ -138,39 +133,22 @@ export class TypeOrmHealthIndicator extends HealthIndicator { const timeout = options.timeout || 1000; if (!connection) { - throw new ConnectionNotFoundError( - this.getStatus(key, false, { - message: 'Connection provider not found in application context', - }), - ); + return check.down('Connection provider not found in application context'); } try { await this.pingDb(connection, timeout); } catch (err) { if (err instanceof PromiseTimeoutError) { - throw new TimeoutError( - timeout, - this.getStatus(key, false, { - message: `timeout of ${timeout}ms exceeded`, - }), - ); + return check.down(`timeout of ${timeout}ms exceeded`); } if (err instanceof MongoConnectionError) { - throw new HealthCheckError( - err.message, - this.getStatus(key, false, { - message: err.message, - }), - ); + return check.down(err.message); } - throw new HealthCheckError( - `${key} is not available`, - this.getStatus(key, false), - ); + return check.down(`${key} is not available`); } - return this.getStatus(key, true); + return check.up(); } }