diff options
author | Linnnus <[email protected]> | 2025-02-17 20:28:59 +0100 |
---|---|---|
committer | Linnnus <[email protected]> | 2025-02-17 20:28:59 +0100 |
commit | 2b309097ca145651618234476160fb30405eabe7 (patch) | |
tree | 20321cf83d18c0c3c3a0a745626565074ea69a41 /app/src/lib/server/db.ts |
Initial commit
Diffstat (limited to 'app/src/lib/server/db.ts')
-rw-r--r-- | app/src/lib/server/db.ts | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/app/src/lib/server/db.ts b/app/src/lib/server/db.ts new file mode 100644 index 0000000..dcfcfd0 --- /dev/null +++ b/app/src/lib/server/db.ts @@ -0,0 +1,20 @@ +import pg from "pg"; +import { env } from "$env/dynamic/private"; + +const pool = new pg.Pool({ + database: env.POSTGRES_DB || "postgres", + user: env.POSTGRES_USERNAME || "postgres", + host: env.POSTGRES_HOST || "localhost", + port: Number(env.POSTGRES_PORT || 5432), +}); + +// Prevent errors in idle clients from terminating Node process. +// See: https://node-postgres.com/features/pooling +// See: https://nodejs.org/api/events.html#error-events +pool.on("error", (err, client) => { + console.error("Database error: ", err, client); +}); + +export function getDbConnection(): Promise<pg.PoolClient> { + return pool.connect(); +} |