How to Create, Update, and Delete Records in Next.js

SivaA
4 min readApr 29, 2024

Introduction

In this tutorial, we will learn how to create, update, and delete records in Next.js using API routes. We will be using Prisma client to connect to the database and perform these operations. By the end of this tutorial, you will have a clear understanding of how to implement these functionalities in your Next.js applications.

Setting Up the Database Connection

To get started, we need to establish a connection to the database. For this tutorial, we will be using Prisma client. First, install the Prisma client by running the following command:

  • npm install @prisma/client

Next, create a new file called “prisma.js” and add the following code:

const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();

module.exports = prisma;

In this file, we import the PrismaClient from the “@prisma/client” package and create an instance of it. Finally, we export the Prisma client instance to be used in other files.

Creating the “todos” Table

Before we can perform any CRUD operations, we need to create a table in the database. For this tutorial, let’s create a table called “todos” with the following columns:

  • id (auto-generated)
  • name
  • username
  • created (auto-generated)

We can create this table using the Prisma client. Open the terminal and run the following command:

npx prisma migrate dev --name init

This command will create the “todos” table in the database based on the schema defined in the Prisma client. Once the migration is complete, you can verify the table in your Postgres database or by using Prisma Studio, a visual interface for exploring and managing your database.

Creating the Delete Method

Now, let’s implement the delete functionality for the “todos” table. In the API route file for the “todos” table, duplicate the existing method and change its name to “delete”. This method will handle the deletion of a record.

else if (method === 'delete') {
const { id } = req.body;
const deletedRecord = await prisma.todos.delete({
where: {
id: Number(id),
},
});
res.json(deletedRecord);
}

In this code, we extract the “id” from the request body and use it to delete the corresponding record from the “todos” table. The deleted record is then returned as the response.

To test the delete functionality, make a request to the API route with the method set to “delete” and provide the “id” of the record you want to delete. After successful deletion, verify the changes in the database or in Prisma Studio.

Creating the Post Method

Next, let’s implement the create functionality for the “todos” table. Duplicate the existing method in the API route file and change its name to “post”. This method will handle the creation of a new record.

else if (method === 'post') {
const { name, username } = req.body;
const newRecord = await prisma.todos.create({
data: {
name,
username,
},
});
res.json(newRecord);
}

In this code, we extract the “name” and “username” from the request body and use them to create a new record in the “todos” table. The newly created record is then returned as the response.

To test the create functionality, make a request to the API route with the method set to “post” and provide the “name” and “username” for the new record. After successful creation, verify the changes in the database or in Prisma Studio.

Creating the Put Method

Finally, let’s implement the update functionality for the “todos” table. Duplicate the existing method in the API route file and change its name to “put”. This method will handle the update of an existing record.

else if (method === 'put') {
const { id, name, username } = req.body;
const updatedRecord = await prisma.todos.update({
where: {
id: Number(id),
},
data: {
name,
username,
},
});
res.json(updatedRecord);
}

In this code, we extract the “id”, “name”, and “username” from the request body and use them to update the corresponding record in the “todos” table. The updated record is then returned as the response.

To test the update functionality, make a request to the API route with the method set to “put” and provide the “id” of the record you want to update, along with the updated “name” and “username”. After successful update, verify the changes in the database or in Prisma Studio.

Conclusion

In this tutorial, we learned how to create, update, and delete records in Next.js using API routes. We used Prisma client to connect to the database and perform these operations. By following the steps outlined in this tutorial, you should now be able to implement these functionalities in your own Next.js applications.

If you have any questions or need further clarification on any of the topics discussed in this tutorial, please feel free to reach out to us. We hope you found this tutorial informative and helpful. Thank you for watching!

Made with VideoToBlog

--

--

SivaA

I am a Software Engineer and YouTube Content Creator, from Dallas Texas