백엔드 config 파일들 생성
This commit is contained in:
45
backend-node/src/config/database.ts
Normal file
45
backend-node/src/config/database.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
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();
|
||||
console.log("✅ 데이터베이스 연결 성공");
|
||||
} 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;
|
||||
Reference in New Issue
Block a user