import { PrismaClient } from "@prisma/client"; import config from "./environment"; // Prisma 클라이언트 생성 함수 function createPrismaClient() { return new PrismaClient({ datasources: { db: { url: config.databaseUrl, }, }, log: config.debug ? ["query", "info", "warn", "error"] : ["error"], }); } // 단일 인스턴스 생성 const prisma = createPrismaClient(); // 데이터베이스 연결 테스트 async function testConnection() { try { await prisma.$connect(); } catch (error) { console.error("❌ 데이터베이스 연결 실패:", error); process.exit(1); } } // 애플리케이션 종료 시 연결 해제 process.on("beforeExit", async () => { await prisma.$disconnect(); }); process.on("SIGINT", async () => { await prisma.$disconnect(); process.exit(0); }); process.on("SIGTERM", async () => { await prisma.$disconnect(); process.exit(0); }); // 초기 연결 테스트 (개발 환경에서만) if (config.nodeEnv === "development") { testConnection(); } // 기본 내보내기 export = prisma;