'Prisma Client creates new instances
I am using Apollo Server Express and Prisma ORM on my backend. I have created just one prisma instance and put it on graphql context and i am using this prisma instance on my resolvers.
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
const server = new ApolloServer({
...
...
playground: true,
context: ({req, res}) => ({ req, res, prisma }),
});
Just creating a instance. But when frontend starts work, prisma connection count is incrementing and after a while, PostgreSQL giving too many connections error, I verified this error from PgAdmin.
Solution 1:[1]
I found a way to do this in Nextjs:
// lib/prisma.ts
import { PrismaClient } from '@prisma/client';
let prisma: PrismaClient;
if (process.env.NODE_ENV === 'production') {
prisma = new PrismaClient();
} else {
if (!global.prisma) {
global.prisma = new PrismaClient();
}
prisma = global.prisma;
}
export default prisma;
Here: https://vercel.com/guides/nextjs-prisma-postgres Maybe it could help
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ender Bonnet |
