lib/db.ts

taxonomy · lib/db.ts · typescript · MIT
database, Database / ORM

Provides a singleton Prisma client instance for database access.

// Source: https://github.com/shadcn-ui/taxonomy | License: MIT | Copyright 2022 shadcn
// Provided via justclaw.it.com; keep this notice with the code.
import { PrismaClient } from "@prisma/client"

declare global {
  // eslint-disable-next-line no-var
  var cachedPrisma: PrismaClient
}

let prisma: PrismaClient
if (process.env.NODE_ENV === "production") {
  prisma = new PrismaClient()
} else {
  if (!global.cachedPrisma) {
    global.cachedPrisma = new PrismaClient()
  }
  prisma = global.cachedPrisma
}

export const db = prisma

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key