feat: 初始化DevOps平台后端

This commit is contained in:
湛兮
2026-06-11 20:49:59 +08:00
commit caec5a618d
88 changed files with 11893 additions and 0 deletions
+9
View File
@@ -0,0 +1,9 @@
import { Global, Module } from '@nestjs/common';
import { PrismaService } from './prisma.service';
@Global()
@Module({
providers: [PrismaService],
exports: [PrismaService],
})
export class PrismaModule {}
+51
View File
@@ -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',
};
}
}
}