Skip to content

Commit

Permalink
fix: fix too many clients error on Postgres due to HMR with Drizzle (t…
Browse files Browse the repository at this point in the history
…3-oss#1766)

Co-authored-by: Julius Marminge <[email protected]>
  • Loading branch information
fuegoio and juliusmarminge committed Mar 1, 2024
1 parent 276dde4 commit d7695df
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-avocados-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-t3-app": patch
---

fix: cache database connection during development to prevent `Too many connections` due to HMR
20 changes: 13 additions & 7 deletions cli/template/extras/src/server/db/index-drizzle/with-mysql.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { drizzle } from "drizzle-orm/mysql2";
import mysql from "mysql2/promise";
import { createPool, type Pool } from "mysql2/promise";

import { env } from "~/env";
import * as schema from "./schema";

export const db = drizzle(
mysql.createPool({
uri: env.DATABASE_URL,
}),
{ schema, mode: "default" }
);
/**
* Cache the database connection in development. This avoids creating a new connection on every HMR
* update.
*/
const globalForDb = globalThis as unknown as {
conn: Pool | undefined;
};

const conn = globalForDb.conn ?? createPool({ uri: env.DATABASE_URL });
if (env.NODE_ENV !== "production") globalForDb.conn = conn;

export const db = drizzle(conn, { schema, mode: "default" });
13 changes: 12 additions & 1 deletion cli/template/extras/src/server/db/index-drizzle/with-postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,15 @@ import postgres from "postgres";
import { env } from "~/env";
import * as schema from "./schema";

export const db = drizzle(postgres(env.DATABASE_URL), { schema });
/**
* Cache the database connection in development. This avoids creating a new connection on every HMR
* update.
*/
const globalForDb = globalThis as unknown as {
conn: postgres.Sql | undefined;
};

const conn = globalForDb.conn ?? postgres(env.DATABASE_URL);
if (env.NODE_ENV !== "production") globalForDb.conn = conn;

export const db = drizzle(conn, { schema });
19 changes: 13 additions & 6 deletions cli/template/extras/src/server/db/index-drizzle/with-sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ import { drizzle } from "drizzle-orm/better-sqlite3";
import { env } from "~/env";
import * as schema from "./schema";

export const db = drizzle(
new Database(env.DATABASE_URL, {
fileMustExist: false,
}),
{ schema }
);
/**
* Cache the database connection in development. This avoids creating a new connection on every HMR
* update.
*/
const globalForDb = globalThis as unknown as {
conn: Database | undefined;
};

export const conn =
globalForDb.conn ?? new Database(env.DATABASE_URL, { fileMustExist: false });
if (env.NODE_ENV !== "production") globalForDb.conn = conn;

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

0 comments on commit d7695df

Please sign in to comment.