/**
* PostgreSQL Database Client
*/
import pg from 'pg'
import { config } from './config.js'
const { Pool } = pg
let pool: pg.Pool | null = null
export function getPool(): pg.Pool {
if (!pool) {
pool = new Pool(config.db)
}
return pool
}
export async function query(
text: string,
params?: any[]
): Promise<pg.QueryResult> {
const pool = getPool()
return pool.query(text, params)
}
export async function healthCheck(): Promise<{
connected: boolean
error?: string
}> {
try {
const result = await query('SELECT NOW()')
return { connected: true }
} catch (error) {
return {
connected: false,
error: error instanceof Error ? error.message : 'Unknown error',
}
}
}
export async function closePool(): Promise<void> {
if (pool) {
await pool.end()
pool = null
}
}