Skip to content

Commit

Permalink
refactor(typeorm): Refactor to use new service
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunnerLivio committed Jan 31, 2024
1 parent 70dd272 commit d29499c
Showing 1 changed file with 16 additions and 38 deletions.
54 changes: 16 additions & 38 deletions lib/health-indicator/database/typeorm.health.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand All @@ -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();
}
Expand Down Expand Up @@ -127,50 +121,34 @@ export class TypeOrmHealthIndicator extends HealthIndicator {
* @example
* typeOrmHealthIndicator.pingCheck('database', { timeout: 1500 });
*/
async pingCheck(
key: string,
async pingCheck<Key extends string>(
key: Key,
options: TypeOrmPingCheckSettings = {},
): Promise<HealthIndicatorResult> {
) {
const check = this.healthIndicatorService.check(key);
this.checkDependantPackages();

const connection: TypeOrm.DataSource | null =
options.connection || this.getContextConnection();
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();
}
}

0 comments on commit d29499c

Please sign in to comment.