feat: Initialize NestJS backend project with Prisma, core modules, configuration, and common utilities.
This commit is contained in:
2
src/prisma/index.ts
Normal file
2
src/prisma/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './prisma.module';
|
||||
export * from './prisma.service';
|
||||
9
src/prisma/prisma.module.ts
Normal file
9
src/prisma/prisma.module.ts
Normal 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 {}
|
||||
45
src/prisma/prisma.service.ts
Normal file
45
src/prisma/prisma.service.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
@Injectable()
|
||||
export class PrismaService
|
||||
extends PrismaClient
|
||||
implements OnModuleInit, OnModuleDestroy
|
||||
{
|
||||
constructor() {
|
||||
super({
|
||||
log:
|
||||
process.env.NODE_ENV === 'development'
|
||||
? ['query', 'info', 'warn', 'error']
|
||||
: ['error'],
|
||||
});
|
||||
}
|
||||
|
||||
async onModuleInit() {
|
||||
await this.$connect();
|
||||
}
|
||||
|
||||
async onModuleDestroy() {
|
||||
await this.$disconnect();
|
||||
}
|
||||
|
||||
async cleanDatabase() {
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
throw new Error('cleanDatabase is not allowed in production');
|
||||
}
|
||||
|
||||
const models = Reflect.ownKeys(this).filter(
|
||||
(key) => typeof key === 'string' && !key.startsWith('_') && !key.startsWith('$'),
|
||||
);
|
||||
|
||||
return Promise.all(
|
||||
models.map((modelKey) => {
|
||||
const model = this[modelKey as string];
|
||||
if (model && typeof model.deleteMany === 'function') {
|
||||
return model.deleteMany();
|
||||
}
|
||||
return Promise.resolve();
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user