blob: b35f85309178b773d4dd51ce233f4516bdfb980f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
import pg from "pg";
import { env } from "$env/dynamic/private";
import type { Handle } from "@sveltejs/kit";
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 const dbHandle = (async ({ event, resolve }) => {
const dbClient = await pool.connect();
event.locals.dbClient = dbClient;
try {
return await resolve(event);
} finally {
dbClient.release();
}
}) satisfies Handle;
|