Prisma ORM Cheatsheet
Quick reference for Prisma ORM: models, CRUD operations, filtering, sorting, relations, aggregation, transactions, and migrations.
| Feature | Description | Example | Category |
|---|---|---|---|
| model | Define a database model | model User { id Int @id @default(autoincrement()) name String } | Models |
| enum | Define an enum | enum Role { USER ADMIN } | Models |
| findMany() | Retrieve multiple records | prisma.user.findMany(); | CRUD |
| findUnique() | Retrieve one record by unique field | prisma.user.findUnique({ where: { id: 1 } }); | CRUD |
| create() | Create a record | prisma.user.create({ data: { name: "Alice" } }); | CRUD |
| update() | Update a record | prisma.user.update({ where: { id: 1 }, data: { name: "Bob" } }); | CRUD |
| delete() | Delete a record | prisma.user.delete({ where: { id: 1 } }); | CRUD |
| where | Filter records | prisma.user.findMany({ where: { name: "Alice" } }); | Filtering & Sorting |
| orderBy | Sort results | prisma.user.findMany({ orderBy: { name: "asc" } }); | Filtering & Sorting |
| take / skip | Pagination | prisma.user.findMany({ take: 10, skip: 20 }); | Filtering & Sorting |
| include | Include related models | prisma.user.findMany({ include: { posts: true } }); | Relations |
| select | Select specific fields | prisma.user.findMany({ select: { id: true, name: true } }); | Relations |
| count() | Count records | prisma.user.count({ where: { active: true } }); | Aggregation |
| aggregate() | Aggregation queries | prisma.user.aggregate({ _avg: { age: true } }); | Aggregation |
| groupBy() | Group records | prisma.user.groupBy({ by: ["role"], _count: { id: true } }); | Aggregation |
| $transaction | Run multiple queries atomically | prisma.$transaction([prisma.user.create({ data: {...} }), prisma.post.create({ data: {...} })]); | Transactions |
| prisma migrate dev | Run migration during development | npx prisma migrate dev --name init | Migrations & CLI |
| prisma db push | Push schema changes to database | npx prisma db push | Migrations & CLI |
| prisma studio | Open Prisma GUI | npx prisma studio | Migrations & CLI |