Spaces:
Paused
Paused
| import { PrismaClient } from '@prisma/client'; | |
| // Singleton pattern for Prisma client | |
| const globalForPrisma = globalThis as unknown as { | |
| prisma: PrismaClient | undefined; | |
| }; | |
| export const prisma = | |
| globalForPrisma.prisma ?? | |
| new PrismaClient({ | |
| log: process.env.NODE_ENV === 'development' ? ['query', 'error', 'warn'] : ['error'], | |
| }); | |
| if (process.env.NODE_ENV !== 'production') { | |
| globalForPrisma.prisma = prisma; | |
| } | |
| // Helper to check if Prisma is connected | |
| export async function checkPrismaConnection(): Promise<boolean> { | |
| try { | |
| await prisma.$queryRaw`SELECT 1`; | |
| return true; | |
| } catch (error) { | |
| console.error('Prisma connection failed:', error); | |
| return false; | |
| } | |
| } | |
| // Graceful shutdown | |
| export async function disconnectPrisma(): Promise<void> { | |
| await prisma.$disconnect(); | |
| } | |
| export default prisma; | |