Commit 30a7cde4 authored by Hendrik Garske's avatar Hendrik Garske

fix: Datenbank-Connection lazy initialisieren für Build-Kompatibilität

parent b092c6c3
Pipeline #23 passed with stages
in 1 minute and 33 seconds
import { Pool } from 'pg' import { Pool } from 'pg'
const connectionString = process.env.DATABASE_URL let pool: Pool | undefined
if (!connectionString) { // Lazy initialization of the database pool
throw new Error('DATABASE_URL environment variable is not set') function getPool(): Pool {
} if (!pool) {
const connectionString = process.env.DATABASE_URL
if (!connectionString) {
throw new Error('DATABASE_URL environment variable is not set')
}
// Create a singleton pool instance pool = new Pool({
const pool = new Pool({ connectionString,
connectionString, ssl: process.env.NODE_ENV === 'production' ? { rejectUnauthorized: false } : false,
ssl: process.env.NODE_ENV === 'production' ? { rejectUnauthorized: false } : false, })
})
// Handle pool errors // Handle pool errors
pool.on('error', (err) => { pool.on('error', (err) => {
console.error('Unexpected error on idle client', err) console.error('Unexpected error on idle client', err)
process.exit(-1) process.exit(-1)
}) })
}
return pool
}
export { pool as db } export { getPool as db }
// Helper function to query the database // Helper function to query the database
export async function query(text: string, params?: unknown[]) { export async function query(text: string, params?: unknown[]) {
const dbPool = getPool()
const start = Date.now() const start = Date.now()
const res = await pool.query(text, params) const res = await dbPool.query(text, params)
const duration = Date.now() - start const duration = Date.now() - start
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
console.log('Executed query', { text, duration, rows: res.rowCount }) console.log('Executed query', { text, duration, rows: res.rowCount })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment