37 lines
808 B
JavaScript
37 lines
808 B
JavaScript
import postgres from 'postgres'
|
|
import { getRuntimeEnvValue } from '../utils/runtime-env'
|
|
|
|
let client = null
|
|
|
|
/**
|
|
* 현재 실행 환경이 운영인지 확인한다.
|
|
* @returns {boolean} 운영 환경 여부
|
|
*/
|
|
const isProductionRuntime = () => process.env.NODE_ENV === 'production'
|
|
|
|
/**
|
|
* PostgreSQL 클라이언트 조회
|
|
* @returns {ReturnType<typeof postgres> | null} PostgreSQL 클라이언트
|
|
*/
|
|
export const getPostgresClient = () => {
|
|
const databaseUrl = getRuntimeEnvValue('DATABASE_URL', 'databaseUrl').trim()
|
|
|
|
if (!databaseUrl) {
|
|
if (isProductionRuntime()) {
|
|
throw new Error('DATABASE_URL_REQUIRED')
|
|
}
|
|
|
|
return null
|
|
}
|
|
|
|
if (!client) {
|
|
client = postgres(databaseUrl, {
|
|
max: 5,
|
|
idle_timeout: 20,
|
|
connect_timeout: 10
|
|
})
|
|
}
|
|
|
|
return client
|
|
}
|