- Refactored Prisma client initialization to differentiate between development and production environments. - Enhanced logging settings based on the environment. - Implemented graceful shutdown for Prisma client on process termination signals.
44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
import { PrismaClient } from '@prisma/client'
|
|
|
|
// 환경별 설정
|
|
const isDevelopment = process.env.NODE_ENV === 'development'
|
|
const isProduction = process.env.NODE_ENV === 'production'
|
|
|
|
const globalForPrisma = globalThis as unknown as {
|
|
prisma: PrismaClient | undefined
|
|
}
|
|
|
|
// Prisma Client 설정
|
|
export const prisma =
|
|
globalForPrisma.prisma ??
|
|
new PrismaClient({
|
|
log: isDevelopment ? ['query', 'error', 'warn'] : ['error'],
|
|
|
|
// 데이터소스 설정
|
|
datasources: {
|
|
db: {
|
|
url: process.env.DATABASE_URL,
|
|
},
|
|
},
|
|
|
|
// 에러 포맷팅
|
|
errorFormat: isDevelopment ? 'pretty' : 'minimal',
|
|
})
|
|
|
|
// 개발 환경에서만 글로벌 객체에 저장
|
|
if (!isProduction) globalForPrisma.prisma = prisma
|
|
|
|
// Graceful shutdown
|
|
const shutdown = async () => {
|
|
console.log('🚀 ~ shutdown ~ Shutting down Prisma Client...:')
|
|
await prisma.$disconnect()
|
|
process.exit(0)
|
|
}
|
|
|
|
process.on('SIGINT', shutdown)
|
|
process.on('SIGTERM', shutdown)
|
|
process.on('beforeExit', async () => {
|
|
console.log('🚀 ~ process.on ~ beforeExit:')
|
|
await prisma.$disconnect()
|
|
})
|