Skip to content

Commit be4d8e3

Browse files
committed
fix: limit prisma connections
1 parent 50bd730 commit be4d8e3

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

prisma-local/clientInstance.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,24 @@ import { PrismaClient } from '@prisma/client'
22

33
let prisma: PrismaClient
44

5+
const CONNECTION_LIMIT = 20
6+
7+
function buildDatasourceUrl (): string {
8+
const baseUrl = process.env.DATABASE_URL ?? ''
9+
const separator = baseUrl.includes('?') ? '&' : '?'
10+
return `${baseUrl}${separator}connection_limit=${CONNECTION_LIMIT}`
11+
}
12+
513
interface CustomNodeJsGlobal extends NodeJS.Global {
614
prisma: PrismaClient
715
}
816
declare const global: CustomNodeJsGlobal
917

1018
if (process.env.NODE_ENV === 'production') {
11-
prisma = new PrismaClient()
19+
prisma = new PrismaClient({ datasources: { db: { url: buildDatasourceUrl() } } })
1220
} else {
1321
if (global.prisma === undefined) {
14-
global.prisma = new PrismaClient()
22+
global.prisma = new PrismaClient({ datasources: { db: { url: buildDatasourceUrl() } } })
1523
}
1624

1725
prisma = global.prisma

0 commit comments

Comments
 (0)