Skip to main content

Prisma Client — src/lib/prisma.ts

Singleton PrismaClient using the native PrismaPg adapter. Shared across all server-side code. Requires DATABASE_URL to be set before first import.

Fields

NameTypeVisibilityDescription
adapterPrismaPgprivatePostgreSQL adapter constructed from DATABASE_URL.
prismaPrismaClientpublicORM client. Import this everywhere you need database access.

Models

ModelTablePK typeNotes
UseruserStringemail is unique. Cascade-deletes Session and Account.
SessionsessionStringtoken is unique. Expires at expiresAt.
AccountaccountStringOAuth or credential record. FK to User.
VerificationverificationStringShort-lived token for email verification.
InfraTestKvinfra_test_kvBigIntInfra connectivity test only. Do not use in application logic.

Preconditions

  • process.env.DATABASE_URL must be a valid PostgreSQL connection string.
  • All calls must be awaited.

Error handling

ErrorWhenRecommended response
PrismaClientKnownRequestError P2002Unique constraint violated (e.g., duplicate email).409 Conflict
PrismaClientKnownRequestError P2025Record not found on update/delete.404 Not Found
PrismaClientInitializationErrorDB unreachable or DATABASE_URL missing.Log + 500 Internal Server Error

upsert, create, and update are not idempotent by default. Callers that need idempotency must catch P2002 and handle it.

Example

import { prisma } from "@/lib/prisma";

const user = await prisma.user.findUnique({
where: { email: "alice@example.com" },
});

await prisma.infraTestKv.upsert({
where: { id: BigInt(1) },
update: { val: "updated" },
create: { id: BigInt(1), val: "created" },
});