Skip to content

Commit

Permalink
feat: add DrizzleOrmHealthIndicator
Browse files Browse the repository at this point in the history
  • Loading branch information
dloranc authored and k1eu committed Sep 26, 2024
1 parent 023ac9d commit 28110c0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {
HealthCheckService,
HttpHealthIndicator,
HealthCheck,
TypeOrmHealthIndicator,
} from "@nestjs/terminus";
import { Public } from "src/common/decorators/public.decorator";
import { DrizzleOrmHealthIndicator } from "./indicator/database/drizzleorm.health";

@Controller("health")
export class HealthController {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { Module } from "@nestjs/common";
import { TerminusModule } from "@nestjs/terminus";
import { HttpModule } from "@nestjs/axios";
import { HealthController } from "./health.controller";
import { DrizzleOrmHealthIndicator } from "./indicator/database/drizzleorm.health";

@Module({
imports: [TerminusModule, HttpModule],
providers: [DrizzleOrmHealthIndicator],
controllers: [HealthController],
})
export class HealthModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Inject, Injectable } from "@nestjs/common";
import {
HealthIndicator,
HealthIndicatorResult,
HealthCheckError,
} from "@nestjs/terminus";
import { sql } from "drizzle-orm";
import { DatabasePg } from "src/common";

@Injectable()
export class DrizzleOrmHealthIndicator extends HealthIndicator {
constructor(@Inject("DB") private readonly db: DatabasePg) {
super();
}

async pingCheck(key: string): Promise<HealthIndicatorResult> {
try {
await this.db.execute(sql`SELECT 1`);

return this.getStatus(key, true);
} catch (error) {
const result = this.getStatus(key, false);
throw new HealthCheckError("Database check failed", result);
}
}
}

0 comments on commit 28110c0

Please sign in to comment.