/** * Database utility for Prisma client * * Provides a global Prisma instance to avoid connection issues */ const { PrismaClient } = require('@prisma/client'); // Global Prisma instance let prisma = null; /** * Get the global Prisma database instance */ async function getDB() { if (!prisma) { prisma = new PrismaClient(); // Test the connection try { await prisma.$connect(); console.log('✅ Database connection established'); } catch (error) { console.error('❌ Database connection failed:', error.message); throw error; } } return prisma; } /** * Close the database connection */ async function closeDB() { if (prisma) { await prisma.$disconnect(); prisma = null; console.log('✅ Database connection closed'); } } module.exports = { getDB, closeDB };