kindergarten/reading-platform-backend/src/database/prisma.service.ts
tonytech 7f757b6a63 初始提交:幼儿园阅读平台三端代码
- reading-platform-backend:NestJS 后端
- reading-platform-frontend:Vue3 前端
- reading-platform-java:Spring Boot 服务端
2026-02-28 17:51:15 +08:00

39 lines
1.2 KiB
TypeScript

import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';
@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit, OnModuleDestroy {
async onModuleInit() {
await this.$connect();
console.log('✅ Database connected successfully');
}
async onModuleDestroy() {
await this.$disconnect();
console.log('👋 Database disconnected');
}
// 清理测试数据的辅助方法
async cleanDatabase() {
if (process.env.NODE_ENV === 'production') {
throw new Error('Cannot clean database in production');
}
// 按照外键依赖顺序删除
await this.studentRecord.deleteMany();
await this.lessonFeedback.deleteMany();
await this.lesson.deleteMany();
await this.tenantCourse.deleteMany();
await this.courseScriptPage.deleteMany();
await this.courseScript.deleteMany();
await this.courseActivity.deleteMany();
await this.courseResource.deleteMany();
await this.course.deleteMany();
await this.student.deleteMany();
await this.class.deleteMany();
await this.teacher.deleteMany();
await this.tenant.deleteMany();
await this.tag.deleteMany();
}
}