ERP-node/backend-node/src/config/database.ts

45 lines
972 B
TypeScript

import { PrismaClient } from "@prisma/client";
import config from "./environment";
// Prisma 클라이언트 인스턴스 생성
const prisma = new PrismaClient({
datasources: {
db: {
url: config.databaseUrl,
},
},
log: config.debug ? ["query", "info", "warn", "error"] : ["error"],
});
// 데이터베이스 연결 테스트
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 default prisma;