Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
CRUD operations with Prisma
📖 Scenario: You are building a simple Next.js app to manage a list of books in a library. You will use Prisma to connect to a database and perform CRUD (Create, Read, Update, Delete) operations on the books.
🎯 Goal: Build a Next.js API route that uses Prisma to create, read, update, and delete books in the database.
📋 What You'll Learn
Create a Prisma client instance
Define a book data object with title and author
Write a function to create a new book in the database
Write a function to read all books from the database
Write a function to update a book's title by ID
Write a function to delete a book by ID
💡 Why This Matters
🌍 Real World
Managing data in web applications is common. Prisma helps connect your Next.js app to a database and perform CRUD operations easily.
💼 Career
Many web developer roles require working with databases and APIs. Knowing Prisma with Next.js is a valuable skill for building modern full-stack apps.
Progress0 / 4 steps
1
Set up Prisma client
Import PrismaClient from '@prisma/client' and create a constant called prisma that is a new PrismaClient instance.
NextJS
Hint
Use import { PrismaClient } from '@prisma/client' and then const prisma = new PrismaClient().
2
Create a book data object
Create a constant called newBook that is an object with title set to 'The Great Gatsby' and author set to 'F. Scott Fitzgerald'.
NextJS
Hint
Define newBook as an object with the exact keys and values.
3
Write a function to create a book
Write an async function called createBook that uses prisma.book.create with data: newBook and returns the created book.
NextJS
Hint
Use async function createBook() and inside use await prisma.book.create({ data: newBook }).
4
Add read, update, and delete functions
Add three async functions: getBooks that returns all books using prisma.book.findMany(), updateBookTitle that takes id and newTitle and updates the book title using prisma.book.update, and deleteBook that takes id and deletes the book using prisma.book.delete.
NextJS
Hint
Write three async functions using Prisma client methods: findMany, update, and delete.
Practice
(1/5)
1. What does the prisma.user.create() method do in Next.js with Prisma?
easy
A. It adds a new user record to the database.
B. It fetches all user records from the database.
C. It updates an existing user record.
D. It deletes a user record from the database.
Solution
Step 1: Understand the method name
The method create() is used to add new data in Prisma.
Step 2: Match method to CRUD operation
Creating means adding new records, so prisma.user.create() adds a new user.
Final Answer:
It adds a new user record to the database. -> Option A
Quick Check:
Create method = Add new record [OK]
Hint: Create method adds new data, not read or delete [OK]
Common Mistakes:
Confusing create() with findMany() which reads data
Thinking create() updates existing records
Assuming create() deletes records
2. Which of the following is the correct syntax to update a user with id 5 using Prisma in Next.js?
Missing the 'where' key wrapping the id. -> Option A
Quick Check:
Delete needs where key with id [OK]
Hint: Delete needs where: { id } object, not just id [OK]
Common Mistakes:
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
5. You want to update a user's email only if the user exists, otherwise create a new user with that email. Which Prisma method best fits this use case in Next.js?