What if you could skip writing complex database queries and still manage your data perfectly?
Why CRUD operations with Prisma in NextJS? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine building a web app where you have to add, read, update, and delete user data by writing raw database queries every time.
You must write long SQL commands manually and connect them to your app code.
Manually writing database queries is slow and easy to mess up.
It's hard to keep track of all queries, and small mistakes can break your app.
Updating queries when your data changes is a big headache.
Prisma lets you work with your database using simple JavaScript/TypeScript commands.
It automatically handles the complex queries behind the scenes, so you focus on your app logic.
const result = await db.query('SELECT * FROM users WHERE id = ?', [userId]);const user = await prisma.user.findUnique({ where: { id: userId } });Prisma makes database work easy and safe, so you can build features faster and with fewer bugs.
When building a blog, Prisma helps you quickly add new posts, update content, show lists of posts, and delete old ones without writing complex SQL.
Manual database queries are slow and error-prone.
Prisma simplifies database operations with easy commands.
This lets you build apps faster and more reliably.
Practice
prisma.user.create() method do in Next.js with Prisma?Solution
Step 1: Understand the method name
The methodcreate()is used to add new data in Prisma.Step 2: Match method to CRUD operation
Creating means adding new records, soprisma.user.create()adds a new user.Final Answer:
It adds a new user record to the database. -> Option AQuick Check:
Create method = Add new record [OK]
- Confusing create() with findMany() which reads data
- Thinking create() updates existing records
- Assuming create() deletes records
Solution
Step 1: Identify Prisma update syntax
Prisma usesupdate()withwhereanddatakeys to update records.Step 2: Check option correctness
Only prisma.user.update({ where: { id: 5 }, data: { name: 'Alice' } }) uses correct methodupdate()and proper keyswhereanddata.Final Answer:
prisma.user.update({ where: { id: 5 }, data: { name: 'Alice' } }) -> Option CQuick Check:
Update method uses where and data keys [OK]
- Using non-existent methods like modify(), change(), edit()
- Missing where or data keys in update()
- Passing id directly without where object
const users = await prisma.user.findMany({ where: { active: true } });
console.log(users.length);What will
console.log(users.length) output?Solution
Step 1: Understand findMany with where filter
ThefindMany()method returns an array of records matching thewherecondition.Step 2: Analyze the filter condition
Only users withactive: trueare returned, sousers.lengthis count of active users.Final Answer:
The number of active users in the database. -> Option DQuick Check:
findMany with where returns filtered array [OK]
- Thinking findMany returns undefined or error
- Ignoring the where filter effect
- Assuming it returns all users without filter
await prisma.user.delete({ id: 10 });Solution
Step 1: Check delete() method syntax
Prisma's delete() requires an object with awherekey specifying the record to delete.Step 2: Identify missing where key
The code passes{ id: 10 }directly, missingwhere: { id: 10 }.Final Answer:
Missing the 'where' key wrapping the id. -> Option AQuick Check:
Delete needs where key with id [OK]
- Omitting where key in delete()
- Using remove() which does not exist in Prisma
- Thinking id type must be string always
- Believing delete() can't be awaited
Solution
Step 1: Understand the upsert method
Upsert updates if record exists, else creates new one in Prisma.Step 2: Match use case to method
Since we want to update or create based on existence,upsert()fits perfectly.Final Answer:
prisma.user.upsert({ where: { email }, update: { email }, create: { email } }) -> Option BQuick Check:
Upsert = update or create [OK]
- Using update() alone which fails if user missing
- Using create() alone which fails if user exists
- Using findUnique() which only reads data
