AskHandle

AskHandle Blog

How to Successfully Integrate Prisma with Next.js

June 5, 2025Lillian Kim3 min read

How to Successfully Integrate Prisma with Next.js

Are you a developer looking to level up your Next.js project by seamlessly integrating it with Prisma, the modern database toolkit? If so, you're in the right place. In this comprehensive guide, we'll walk you through the steps to effortlessly connect Prisma with Next.js, enabling you to leverage the full power of both tools in your web applications.

Before we jump into the nitty-gritty details, let's quickly discuss what Prisma and Next.js bring to the table individually. Prisma is a cutting-edge database toolkit that simplifies database access in your backend services by providing a type-safe and auto-generated query builder. On the other hand, Next.js is a popular React framework known for its efficiency in building fast and scalable web applications.

Setting Up Your Next.js Project

To get started, ensure you have Node.js installed on your machine. If not, you can download it from the official website. Next, create a new Next.js project using the following command:

bash
1npx create-next-app my-next-prisma-app

Navigate into your project directory and install the required dependencies:

bash
1cd my-next-prisma-app
2npm install @prisma/client prisma

Configuring Prisma

Now, let's set up Prisma in your project. Run the following command to initialize Prisma in your project directory:

bash
1npx prisma init

This command will create a prisma directory containing your schema.prisma file, where you can define your data model. Update the schema.prisma file with your desired data model, and don't forget to run the migration to apply these changes to your database:

bash
1npx prisma migrate dev --name init

Once the migration is successful, you can generate Prisma client by running:

bash
1npx prisma generate

Adding Prisma Client to Your Next.js Application

To connect Prisma with Next.js, you need to add the generated Prisma client to your Next.js project. Create a new file prisma.js in the root of your project and initialize the Prisma client:

javascript
1import { PrismaClient } from '@prisma/client';
2
3let prisma;
4
5if (process.env.NODE_ENV === 'production') {
6  prisma = new PrismaClient();
7} else {
8  if (!global.prisma) {
9    global.prisma = new PrismaClient();
10  }
11  prisma = global.prisma;
12}
13
14export default prisma;

You can now import and use this prisma object in any of your API routes, pages, or components within your Next.js application.

Querying Data with Prisma in Next.js

With Prisma successfully integrated into your Next.js project, you can start querying your database using Prisma's powerful query builder. Here's a simple example of fetching a list of users from your database and displaying them in your Next.js application:

javascript
1// pages/index.js
2import prisma from '../prisma';
3
4export default function Home({ users }) {
5  return (
6    <div>
7      <h1>List of Users</h1>
8      {users.map((user) => (
9        <div key={user.id}>{user.name}</div>
10      ))}
11    </div>
12  );
13}
14
15export async function getServerSideProps() {
16  const users = await prisma.user.findMany();
17  
18  return {
19    props: { users },
20  };
21}

In this example, we're fetching a list of users from the database using Prisma's findMany method and passing them as props to our Next.js page component.

Updating Data with Prisma in Next.js

Apart from querying data, you can also utilize Prisma to update or create new data in your database. Here's a simple example of how you can create a new user in your database using Prisma within a Next.js API route:

javascript
1// pages/api/users/create.js
2import prisma from '../../../prisma';
3
4export default async function handler(req, res) {
5  if (req.method === 'POST') {
6    const { name, email } = req.body;
7
8    const newUser = await prisma.user.create({
9      data: {
10        name,
11        email,
12      },
13    });
14
15    res.status(201).json(newUser);
16  } else {
17    res.status(405).end();
18  }
19}

By defining API routes like the example above, you can easily handle data creation, updates, and deletions within your Next.js application using Prisma.

Integrating Prisma with Next.js opens up a world of possibilities for building robust and efficient web applications. By following the steps outlined in this guide, you can seamlessly connect Prisma to your Next.js project, enabling you to leverage the best of both worlds in terms of data management and frontend development.

If you encounter any issues during the integration process, be sure to consult the official documentation of Prisma and Next.js for detailed explanations and troubleshooting tips.

Feel free to experiment with different queries, mutations, and data models to tailor your database interactions to suit your specific project requirements. With Prisma and Next.js at your disposal, the possibilities are endless for creating cutting-edge web applications that deliver exceptional performance and user experiences.