Using Prisma in development

- 130 views

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;

Moved the page from under /snippets to /blog on the 10th of February 2024.