0
0
NextJSframework~30 mins

CRUD operations with Prisma in NextJS - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Write three async functions using Prisma client methods: findMany, update, and delete.