100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

Prisma ORM Cheat Sheet

Prisma ORM Cheat Sheet

Covers Prisma schema syntax, migrations, the type-safe query client, and relation queries for building Node.js/TypeScript data layers.

2 PagesIntermediateMar 10, 2026

Prisma Schema File

Datasource, generator, enum, and relation definitions.

prisma
datasource db {  provider = "postgresql"  url      = env("DATABASE_URL")}generator client {  provider = "prisma-client-js"}enum Role {  USER  ADMIN}model User {  id    Int    @id @default(autoincrement())  email String @unique  role  Role   @default(USER)  posts Post[]}model Post {  id        Int      @id @default(autoincrement())  title     String  published Boolean  @default(false)  author    User     @relation(fields: [authorId], references: [id])  authorId  Int  @@index([authorId])}

Prisma CLI Workflow

Common commands for schema, migrations, and tooling.

bash
# Create and apply a migration, generating SQL from schema changesnpx prisma migrate dev --name add_user_role# Regenerate the type-safe client after any schema changenpx prisma generate# Push schema to DB without creating a migration (prototyping only)npx prisma db push# Open a GUI to browse/edit datanpx prisma studio# Apply pending migrations in production (no shadow DB / no prompts)npx prisma migrate deploy

Prisma Client Queries

Create, read with relations, and transactions.

typescript
import { PrismaClient } from '@prisma/client';const prisma = new PrismaClient();// Create with a nested relation writeconst user = await prisma.user.create({  data: {    email: 'a@example.com',    posts: { create: [{ title: 'Hello World' }] },  },});// Query with relation include and filteringconst published = await prisma.post.findMany({  where: { published: true },  include: { author: true },  orderBy: { id: 'desc' },  take: 10,});// Interactive transaction across multiple writesawait prisma.$transaction(async (tx) => {  await tx.user.update({ where: { id: 1 }, data: { role: 'ADMIN' } });  await tx.post.updateMany({ where: { authorId: 1 }, data: { published: true } });});

Key Concepts

Core pieces of the Prisma toolchain.

  • schema.prisma- Single source of truth defining the datasource, generator, and data models; migrations and the client are generated from it
  • Migration history- Stored as timestamped SQL files under prisma/migrations, applied in order and tracked in a _prisma_migrations table
  • Prisma Client- Auto-generated, fully typed query builder based on your schema; regenerate with `prisma generate` after every schema edit
  • Relation queries- `include` eagerly loads related records; `select` picks specific fields, both prevent over-fetching or under-fetching
  • $transaction- Batches multiple queries atomically; sequential array form or the interactive callback form for dependent operations
  • Shadow database- A temporary database Prisma uses in dev to detect drift and generate migrations safely without touching your real data
Pro Tip

Never run `prisma db push` against production — it bypasses the migration history entirely and can silently drop columns/data on divergence; reserve it for local prototyping and always use `migrate dev` / `migrate deploy` for anything that needs a reviewable, reversible migration trail.

Was this cheat sheet helpful?

Explore Topics

#PrismaORM#PrismaORMCheatSheet#Database#Intermediate#PrismaSchemaFile#PrismaCLIWorkflow#PrismaClientQueries#KeyConcepts#Databases#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet