39 lines
960 B
TypeScript
39 lines
960 B
TypeScript
import { PrismaClient } from "@/lib/generated/prisma";
|
|
import { PrismaPg } from "@prisma/adapter-pg";
|
|
|
|
const globalForPrisma = globalThis as unknown as {
|
|
prisma: PrismaClient | undefined;
|
|
};
|
|
|
|
function getPrismaDatabaseUrl() {
|
|
const databaseUrl = process.env.DATABASE_URL?.trim();
|
|
if (!databaseUrl) {
|
|
return undefined;
|
|
}
|
|
|
|
const url = new URL(databaseUrl);
|
|
|
|
// In dev, Next can spin up multiple workers. Keep each Prisma pool small so
|
|
// the database is not exhausted by parallel hot-reload processes.
|
|
if (!url.searchParams.has("connection_limit")) {
|
|
url.searchParams.set("connection_limit", "5");
|
|
}
|
|
|
|
if (!url.searchParams.has("pool_timeout")) {
|
|
url.searchParams.set("pool_timeout", "20");
|
|
}
|
|
|
|
return url.toString();
|
|
}
|
|
|
|
export const db =
|
|
globalForPrisma.prisma ??
|
|
new PrismaClient({
|
|
adapter: new PrismaPg({
|
|
connectionString: getPrismaDatabaseUrl(),
|
|
}),
|
|
log: ["error"],
|
|
});
|
|
|
|
globalForPrisma.prisma = db;
|