forked from t3-oss/create-t3-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix too many clients error on Postgres due to HMR with Drizzle (t…
…3-oss#1766) Co-authored-by: Julius Marminge <[email protected]>
- Loading branch information
1 parent
276dde4
commit d7695df
Showing
4 changed files
with
43 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
cli/template/extras/src/server/db/index-drizzle/with-mysql.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters