feat: 初始化DevOps平台后端
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { PrismaService } from './prisma.service';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
providers: [PrismaService],
|
||||
exports: [PrismaService],
|
||||
})
|
||||
export class PrismaModule {}
|
||||
@@ -0,0 +1,51 @@
|
||||
import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { EnvConfig } from '../config/env.schema';
|
||||
|
||||
@Injectable()
|
||||
export class PrismaService
|
||||
extends PrismaClient
|
||||
implements OnModuleInit, OnModuleDestroy
|
||||
{
|
||||
constructor(private readonly config: ConfigService<EnvConfig, true>) {
|
||||
super({
|
||||
log:
|
||||
config.get('NODE_ENV', { infer: true }) === 'development'
|
||||
? ['warn', 'error']
|
||||
: ['error'],
|
||||
});
|
||||
}
|
||||
|
||||
async onModuleInit(): Promise<void> {
|
||||
if (
|
||||
this.config.get('DATABASE_URL', { infer: true }) &&
|
||||
this.config.get('PRISMA_CONNECT_ON_BOOT', { infer: true })
|
||||
) {
|
||||
await this.$connect();
|
||||
}
|
||||
}
|
||||
|
||||
async onModuleDestroy(): Promise<void> {
|
||||
await this.$disconnect();
|
||||
}
|
||||
|
||||
async databaseHealth(): Promise<{
|
||||
status: 'not_configured' | 'ok' | 'unavailable';
|
||||
message?: string;
|
||||
}> {
|
||||
if (!this.config.get('DATABASE_URL', { infer: true })) {
|
||||
return { status: 'not_configured' };
|
||||
}
|
||||
|
||||
try {
|
||||
await this.$connect();
|
||||
return { status: 'ok' };
|
||||
} catch (error) {
|
||||
return {
|
||||
status: 'unavailable',
|
||||
message: error instanceof Error ? error.message : 'Unknown database error',
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user