2026-06-13 10:15:23 +08:00
|
|
|
import { drizzle } from "drizzle-orm/node-postgres";
|
|
|
|
|
import pg from "pg";
|
2026-03-22 02:51:57 +08:00
|
|
|
import { env } from "../config.js";
|
|
|
|
|
import * as schema from "./schema.js";
|
|
|
|
|
|
2026-06-13 10:15:23 +08:00
|
|
|
const pool = new pg.Pool({
|
|
|
|
|
connectionString: env.DATABASE_URL,
|
|
|
|
|
max: 10,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
pool.on("error", (err) => {
|
|
|
|
|
console.error("Unexpected idle Postgres client error", err);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const db = drizzle(pool, { schema });
|
|
|
|
|
export { pool, schema };
|
|
|
|
|
|
|
|
|
|
let ended = false;
|
|
|
|
|
export async function closeDb(): Promise<void> {
|
|
|
|
|
if (ended) return;
|
|
|
|
|
ended = true;
|
|
|
|
|
await pool.end();
|
2026-06-06 16:05:59 +08:00
|
|
|
}
|