27 lines
1.0 KiB
TypeScript
27 lines
1.0 KiB
TypeScript
import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
|
|
import { PrismaClient } from '@prisma/client';
|
|
import { PrismaPg } from '@prisma/adapter-pg';
|
|
import { Pool } from 'pg';
|
|
import * as dotenv from 'dotenv';
|
|
import * as path from 'path';
|
|
|
|
dotenv.config({ path: path.join(__dirname, '..', '.env') });
|
|
|
|
@Injectable()
|
|
export class PrismaService extends PrismaClient implements OnModuleInit, OnModuleDestroy {
|
|
constructor() {
|
|
if (!process.env.DATABASE_URL) {
|
|
dotenv.config({ path: path.join(__dirname, '..', '..', '.env') });
|
|
}
|
|
console.log('--- [PRISMA CHECK] ---');
|
|
console.log('DATABASE_URL nhận được:', process.env.DATABASE_URL ? 'ĐÃ ĐỌC THÀNH CÔNG ✔️' : 'VẪN BỊ UNDEFINED ❌');
|
|
console.log('----------------------');
|
|
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
|
|
const adapter = new PrismaPg(pool);
|
|
super({ adapter });
|
|
}
|
|
|
|
async onModuleInit() { await this.$connect(); }
|
|
async onModuleDestroy() { await this.$disconnect(); }
|
|
}
|