-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprisma.ts
More file actions
66 lines (53 loc) · 1.92 KB
/
Copy pathprisma.ts
File metadata and controls
66 lines (53 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { PrismaClient } from "@prisma/client";
import { PrismaPg } from "@prisma/adapter-pg";
import pg from "pg";
const { Pool } = pg;
/** Bump when EstimatorLead / SiteSettings schema changes to bust hot-reload cache */
const PRISMA_SCHEMA_VERSION = 2;
const globalForPrisma = globalThis as unknown as {
prisma: PrismaClient | undefined;
prismaSchemaVersion: number | undefined;
};
function isStalePrismaClient(client: PrismaClient): boolean {
if (globalForPrisma.prismaSchemaVersion !== PRISMA_SCHEMA_VERSION) {
return true;
}
return (
typeof client.siteSettings === "undefined" ||
typeof client.siteSettings.findUnique !== "function"
);
}
const createPrismaClient = () => {
const connectionString = process.env.DATABASE_URL;
// Initialize the pool with an explicit max size and idle timeout
// to prevent connection exhaustion in serverless or highly concurrent environments
const pool = new Pool({
connectionString,
max: 10, // Maximum number of clients in the pool
idleTimeoutMillis: 30000, // Close idle clients after 30 seconds
});
const adapter = new PrismaPg(pool);
return new PrismaClient({
adapter,
log:
process.env.NODE_ENV === "development"
? ["query", "error", "warn"]
: ["error"],
});
};
function resolvePrismaClient(): PrismaClient {
const cached = globalForPrisma.prisma;
// After schema changes + `prisma generate`, Next.js hot reload can keep an old
// singleton whose delegates (e.g. siteSettings) were never attached.
if (cached && isStalePrismaClient(cached)) {
void cached.$disconnect().catch(() => {});
globalForPrisma.prisma = undefined;
}
const client = globalForPrisma.prisma ?? createPrismaClient();
if (process.env.NODE_ENV !== "production") {
globalForPrisma.prisma = client;
globalForPrisma.prismaSchemaVersion = PRISMA_SCHEMA_VERSION;
}
return client;
}
export const prisma = resolvePrismaClient();