Skip to content

Commit 56e6288

Browse files
authored
Merge pull request #66 from call-0f-code/db-fix
multiple Prisma instances fix
2 parents 3666d40 + 33b4e46 commit 56e6288

1 file changed

Lines changed: 28 additions & 4 deletions

File tree

src/db/client.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
11
import { PrismaPg } from "@prisma/adapter-pg";
22
import { PrismaClient } from "../generated/prisma/client";
3+
import { Pool } from "pg";
34
export * from "../generated/prisma/client";
45

5-
const adapter = new PrismaPg({
6-
connectionString: process.env.DATABASE_URL,
7-
});
6+
const globalForPrisma = globalThis as unknown as {
7+
prisma?: PrismaClient;
8+
pgPool?: Pool;
9+
};
10+
11+
const pool =
12+
globalForPrisma.pgPool ??
13+
new Pool({
14+
connectionString: process.env.DATABASE_URL,
15+
});
16+
17+
const adapter = new PrismaPg(pool);
18+
19+
export const prisma =
20+
globalForPrisma.prisma ??
21+
new PrismaClient({
22+
adapter,
23+
log:
24+
process.env.NODE_ENV === "development"
25+
? ["query", "error", "warn"]
26+
: ["error"],
27+
});
28+
29+
if (process.env.NODE_ENV !== "production") {
30+
globalForPrisma.prisma = prisma;
31+
globalForPrisma.pgPool = pool;
32+
}
833

9-
const prisma = new PrismaClient({ adapter });
1034

1135
export default prisma;

0 commit comments

Comments
 (0)