ncrudify
Version:
Configurable CRUD module for NestJS and Mongoose.
50 lines (45 loc) • 1.36 kB
text/typescript
import { Controller, Get } from "@nestjs/common";
import { ApiOperation, ApiResponse, ApiTags } from "@nestjs/swagger";
import { InjectConnection } from "@nestjs/mongoose";
import { Connection } from "mongoose";
import { HealthResponse } from "./health-response.entity";
("Health Check")
("health")
export class HealthController {
constructor(() private readonly connection: Connection) {}
()
({ summary: "Check application and database health" })
({
status: 200,
description: "Health check successful",
type: HealthResponse,
})
({ status: 500, description: "Internal server error" })
checkHealth() {
const appStatus = "OK";
const dbStatus = this.getDatabaseStatus();
const timestamp = new Date().toISOString();
return {
status: appStatus,
database: dbStatus,
timestamp,
};
}
private getDatabaseStatus() {
const readyState = this.connection.readyState;
switch (readyState) {
case 0:
return "Disconnected";
case 1:
return "Connected";
case 2:
return "Connecting";
case 3:
return "Disconnecting";
case 99:
return "Uninitialized";
default:
return "Unknown";
}
}
}