Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion apps/database/src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,30 @@ if (!process.env.DATABASE_URL) {
throw new Error("DATABASE_URL environment variable is not set");
}

/*
* Cached on globalThis so hot reloads reuse one pool.
*
* Next dev re-evaluates this module on every HMR pass. Without the cache each
* reload opened a brand-new pool and leaked the previous one, so a long editing
* session walked straight into Postgres's 100-connection ceiling and every
* query — including the auth session lookup — started failing with
* `53300: too many clients already`.
*/
const globalForDb = globalThis as unknown as { __forumDbClient?: ReturnType<typeof postgres> };

// Disable prefetch as it is not supported for "Transaction" pool mode
const client = postgres(process.env.DATABASE_URL, { prepare: false });
const client =
globalForDb.__forumDbClient ??
postgres(process.env.DATABASE_URL, {
prepare: false,
// Bounded so one process can't monopolise the server's connection slots.
max: 10,
idle_timeout: 20,
});

if (process.env.NODE_ENV !== "production") {
globalForDb.__forumDbClient = client;
}

export const db = drizzle(client, { schema });

Expand Down
Loading