first commit
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
// src/prisma/prisma.service.ts
|
||||
import {Injectable, OnModuleInit, OnModuleDestroy} from '@nestjs/common';
|
||||
import {PrismaClient} from '../src/generated/prisma/client';
|
||||
import {PrismaPg} from "@prisma/adapter-pg";
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class PrismaService extends PrismaClient implements OnModuleInit, OnModuleDestroy {
|
||||
constructor() {
|
||||
const adapter = new PrismaPg({
|
||||
connectionString: process.env.DATABASE_URL!,
|
||||
});
|
||||
super({adapter});
|
||||
}
|
||||
|
||||
async onModuleInit() {
|
||||
try {
|
||||
await this.$connect();
|
||||
console.log('✅ Đã kết nối Database thành công');
|
||||
} catch (error) {
|
||||
console.error('❌ Lỗi kết nối Database:', error);
|
||||
}
|
||||
}
|
||||
|
||||
async getManyAndCount<T>(
|
||||
model: any,
|
||||
args: any
|
||||
) {
|
||||
const [data, total] = await this.$transaction([
|
||||
model.findMany(args),
|
||||
model.count({ where: args.where }),
|
||||
]);
|
||||
return { data, total };
|
||||
}
|
||||
|
||||
async onModuleDestroy() {
|
||||
await this.$disconnect(); // Ngắt kết nối khi tắt app
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user