Concept Flow - CRUD operations with Prisma
Start
Create
→Read
End
This flow shows the four main database actions: create new data, read existing data, update data, and delete data, done step-by-step.
Jump into concepts and practice - no test required
import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); async function main() { const user = await prisma.user.create({ data: { name: 'Ana' } }); console.log(user); } main() .catch(e => { throw e }) .finally(async () => { await prisma.$disconnect() });
| Step | Action | Prisma Method | Input Data | Result | Next Step |
|---|---|---|---|---|---|
| 1 | Start main function | - | - | No data yet | Create user |
| 2 | Create user | prisma.user.create | { data: { name: 'Ana' } } | User object with id and name 'Ana' | Read user |
| 3 | Read user | prisma.user.findUnique | { where: { id: user.id } } | User object with id and name 'Ana' | Update user |
| 4 | Update user | prisma.user.update | { where: { id: user.id }, data: { name: 'Anna' } } | User object with updated name 'Anna' | Delete user |
| 5 | Delete user | prisma.user.delete | { where: { id: user.id } } | Deleted user object | End |
| 6 | End main function | - | - | All CRUD operations done | Stop |
| Variable | Start | After Create | After Read | After Update | After Delete | Final |
|---|---|---|---|---|---|---|
| user | undefined | { id: 1, name: 'Ana' } | { id: 1, name: 'Ana' } | { id: 1, name: 'Anna' } | { id: 1, name: 'Anna' } (deleted) | undefined |
CRUD with Prisma in Next.js:
- Create: prisma.model.create({ data }) adds new data.
- Read: prisma.model.findUnique/findMany fetches data.
- Update: prisma.model.update({ where, data }) changes data.
- Delete: prisma.model.delete({ where }) removes data.
Each method returns the current state after operation.prisma.user.create() method do in Next.js with Prisma?create() is used to add new data in Prisma.prisma.user.create() adds a new user.update() with where and data keys to update records.update() and proper keys where and data.const users = await prisma.user.findMany({ where: { active: true } });
console.log(users.length);console.log(users.length) output?findMany() method returns an array of records matching the where condition.active: true are returned, so users.length is count of active users.await prisma.user.delete({ id: 10 });where key specifying the record to delete.{ id: 10 } directly, missing where: { id: 10 }.upsert() fits perfectly.