io
This commit is contained in:
30
lib/db.ts
30
lib/db.ts
@@ -4,10 +4,36 @@ const globalForPrisma = globalThis as unknown as {
|
||||
prisma: PrismaClient | undefined;
|
||||
};
|
||||
|
||||
function getPrismaDatabaseUrl() {
|
||||
const databaseUrl = process.env.DATABASE_URL;
|
||||
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({
|
||||
log: process.env.NODE_ENV === "development" ? ["query", "error", "warn"] : ["error"],
|
||||
log: ["error"],
|
||||
datasources: {
|
||||
db: {
|
||||
url: getPrismaDatabaseUrl(),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = db;
|
||||
globalForPrisma.prisma = db;
|
||||
|
||||
Reference in New Issue
Block a user