mirror of
https://git.coastit.co.za/caitlin/CRM_ENVR.git
synced 2026-07-10 19:17:16 +02:00
30 lines
713 B
TypeScript
30 lines
713 B
TypeScript
import { Pool } from "pg"
|
|
|
|
const dbUrl = process.env.DATABASE_URL
|
|
if (!dbUrl) {
|
|
throw new Error("DATABASE_URL environment variable is required")
|
|
}
|
|
const pool = new Pool({
|
|
connectionString: dbUrl,
|
|
max: 20,
|
|
idleTimeoutMillis: 30000,
|
|
connectionTimeoutMillis: 5000,
|
|
ssl: dbUrl.includes("localhost") || dbUrl.includes("127.0.0.1") ? false : { rejectUnauthorized: false },
|
|
})
|
|
|
|
pool.on("error", (err) => {
|
|
console.error("Unexpected database pool error:", err)
|
|
})
|
|
|
|
export async function query(text: string, params?: unknown[]) {
|
|
const client = await pool.connect()
|
|
try {
|
|
const result = await client.query(text, params)
|
|
return result
|
|
} finally {
|
|
client.release()
|
|
}
|
|
}
|
|
|
|
export default pool
|