new PrismaClient() takes 3 seconds to connect to Db on AWS lambda
See original GitHub issueBug description
As this docs suggest I create new PrismaClient
outside handler
But Very first request takes 3 seconds to create prisma client. Then later requests use created prisma client. very fast.
This time does not include cold start time. as picture shows lambda logs are after lambda started.
Only difference and 3seconds execution in 1st request is url: postgresql://...
line which is new PrismaClient
code.
getPrismaClient.ts
import { PrismaClient } from "@prisma/client";
import { SecretsManager } from 'aws-sdk'
const sm = new SecretsManager()
let prisma: PrismaClient
let url: string
export const getPrismaClient = async () => {
if (prisma) return prisma
const dbURL = {.....}
const secretString = JSON.parse(dbURL.SecretString || '{}')
url = `postgresql://${secretString.username}:${secretString.password}@${secretString.host}:${secretString.port}/${secretString.dbname}?connection_limit=1`
console.log('url: ', url);
prisma = new PrismaClient({
datasources: {db:{url}}
});
return prisma
}
How to reproduce
deploy simple todo app to aws lambda
Expected behavior
No response
Prisma information
Click to expand!
```prisma generator client { provider = "prisma-client-js" binaryTargets = ["native", "rhel-openssl-1.0.x", "linux-arm64-openssl-1.0.x"] }datasource db { provider = “postgresql” url = env(“DATABASE_URL”) }
enum m_DoorOpenPermissionType { SHORT_OPEN // users allowed to user short open action LONG_OPEN // users allowed to use long open action }
model u_DoorOpenPermission { createdAt DateTime @default(now()) updatedAt DateTime @default(now()) cognitoUsername String // cognito company id doorId Int startTime DateTime @default(now()) // rule apply start time endTime DateTime? // rule end time type m_DoorOpenPermissionType
door m_Door @relation(fields: [doorId], references: [id]) @@id([doorId, cognitoUsername]) }
model m_Door { id Int @id @default(autoincrement()) createdAt DateTime @default(now()) updatedAt DateTime @default(now()) name String allowedUsers u_DoorOpenPermission[] imageUrl String isOpenDaily Boolean isHidden Boolean @default(false) }
</details>
### Environment & setup
- OS: Mac
- Database: Postgres
- Node.js version: 16
### Prisma Version
3.14.0
Issue Analytics
- State:
- Created a year ago
- Comments:10 (2 by maintainers)
Top GitHub Comments
thank you @cayter. Looks like prisma data proxy is only solution.
😦
@matart15 Sorry for my incorrect information. As documented here, Prisma client pooling doesn’t work with AWS RDS proxy.