Spaces:
Runtime error
Runtime error
| import { INestApplication, Injectable, OnModuleDestroy } from '@nestjs/common'; | |
| import { PrismaClient } from '@prisma/client'; | |
| import { PrismaPg } from '@prisma/adapter-pg'; | |
| import { Pool } from 'pg'; | |
| () | |
| export class PrismaService implements OnModuleDestroy { | |
| public prisma: PrismaClient; | |
| constructor() { | |
| // Prisma 7: Use adapter for Supabase/Neon PostgreSQL with SSL configuration | |
| const isCloudDatabase = | |
| process.env.DATABASE_URL?.includes('supabase') || | |
| process.env.DATABASE_URL?.includes('neon') || | |
| process.env.NODE_ENV === 'production'; | |
| const pool = new Pool({ | |
| connectionString: process.env.DATABASE_URL, | |
| ssl: isCloudDatabase ? { rejectUnauthorized: false } : undefined, | |
| }); | |
| const adapter = new PrismaPg(pool); | |
| this.prisma = new PrismaClient({ adapter }); | |
| } | |
| getClient(): PrismaClient { | |
| return this.prisma; | |
| } | |
| async onModuleDestroy() { | |
| await this.prisma.$disconnect(); | |
| } | |
| async enableShutdownHooks(app: INestApplication) { | |
| // Replace the Prisma beforeExit with Node.js process beforeExit | |
| process.on('beforeExit', async () => { | |
| await app.close(); | |
| }); | |
| } | |
| } | |