PrismaP
Prisma2y ago
11 replies
Nik Revenco

Get type of prisma client before it is defined

In my next.js app I have to use the following approach to prevent multiple instances of PrismaClient being created

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

const extension = {
  model: {
    user: {
        example: () => "example",
      },
  },
};

const globalForPrisma = globalThis as unknown as { db: PrismaClient };
const db = globalForPrisma.db || new PrismaClient().$extends(extension);

export default db;

if (process.env.NODE_ENV !== "production") globalForPrisma.db = db;


The above approach however is problematic because it doesn't generate the correct PrismaClient type. E.g. in my project TS will complain that db.user.example doesn't exist.
I tried solving this with typeof new PrismaClient().$extends(extension) but I need globalForPrisma to be defined before the PrismaClient has been defined
Was this page helpful?