Using Prisma in development

Preventing too many database connections during development

Due to hot reloading you might overload your database with too many connections during development.

Instead of directly importing the Prisma client from @prisma/client, import it this way instead.

import { PrismaClient } from "@prisma/client";
export * from "@prisma/client";

let prisma: PrismaClient;

if (process.env.NODE_ENV === "production") {
  prisma = new PrismaClient();
} else {
  // Ensure the prisma instance is re-used during hot-reloading
  // Otherwise, a new client will be created on every reload
  globalThis.prisma = globalThis.prisma || new PrismaClient();
  prisma = globalThis.prisma;
}

export default prisma;

If you enjoyed this post please leave a like or two below!!!