In this article, you will learn how to add Prisma to a Next.js project. You will learn how to set up Prisma and connect it to a MySQL database. Let’s get started!
Contents
- What is Prisma?
- Prerequisites
- Step 1: Create a Next.js project
- Step 2: Add Prisma to the project
- Step 3: Create a database schema
- Step 4: Push the database schema
- Step 5: Install Prisma Client
- Step 6: Add Prisma to the Next.js project
- Query the database
- Conclusion
What is Prisma?
Prisma is an open-source database toolkit that makes it easy to query data from a database inside a TypeScript application. For example, you can use Prisma to query data from a MySQL database inside a Next.js application. Instead of writing plain SQL like this:
You can write the same query using Prisma’s query builder like this:
And did I mention that Prisma is TypeScript-first? That means that you get full type-safety and auto-completion in your editor when writing queries. It’s cool!
Prerequisites
This tutorial assumes that you have the following tools installed:
And a MySQL database. If you don’t have a MySQL database, you can use PlanetScale to get a free database.
Step 1: Create a Next.js project
First, let’s create a Next.js project. I will use the Next.js Starter for this tutorial. You can use any Next.js project.
You now have a Next.js project with TypeScript and ESLint set up. Let’s add Prisma to this project.
Step 2: Add Prisma to the project
Next, let’s add Prisma to the project. First, install the Prisma CLI:
We need to initialize Prisma in the project. Run the following command:
This will create a prisma
directory in the project. The prisma
directory contains a schema.prisma
file. This file contains the database schema. You can edit this file to change the database schema.
Also, it will create a .env
file in the root of the project. This file contains the database connection URL. You can edit this file to change the database connection URL. In PlanetScale, you can find the database connection URL after creating a database.
Click connect in the dashboard to get the connection URL.
In the modal, select the Prisma
option and copy the connection URL.
Now, open the .env
file and paste the connection URL in the .env
file.
DATABASE_URL='mysql://************:**********************@us-east.connect.psdb.cloud/mini-shop?sslaccept=strict'
Now, you can connect to the database using Prisma.
Step 3: Create a database schema
The schema.prisma
file contains the database schema. You can edit this file to change the database schema. For our mini shop, we need a Product
model. Let’s add a Product
model to the schema.prisma
file.
The SQL equivalent of this schema is:
Now you won’t regret using Prisma ?.
And oh, you need to change
to
Because we are using MySQL.
Step 4: Push the database schema
Now that we have a database schema, let’s push it to the database. Run the following command:
It will push the database schema.
Step 5: Install Prisma Client
Now that we have a database schema, let’s install Prisma Client. We need Prisma Client to query the database. Run the following command:
And generate the types for Prisma Client:
Step 6: Add Prisma to the Next.js project
Create a folder called lib
and create a file called prisma.ts
in the lib
folder. This file will contain the Prisma Client instance. Add the following code to the prisma.ts
file:
This code will create a Prisma Client instance and export it.
For including the prisma client in the project easily, we will add a typescript path alias. Add the following code to the tsconfig.json
file:
Now, we can import the Prisma Client instance using import prisma from "db"
.
Query the database
Run the following command to start the development server:
We will create the API routes for creating a product and getting all the products. Add the following codes to the pages/api
folder:
pages/api/createProduct.ts
This API route will create a product in the database.
pages/api/getProducts.ts
This API route will get all the products from the database.
Now let’s create a page to create a product and get all the products. Add the following code to the pages
folder:
pages/index.tsx
This page will show all the products in the database. We will also add a button to create a product.
pages/add.tsx
This page will allow us to create a product. We will add a button to return to the home page.
Now, add some products to the database from the /add
page.
After adding some products, you should see something like this:
It works! Now, you can improve some stuff like adding a cart page, adding a delete button, etc.
Conclusion
You can find the complete code on GitHub. And if you have any questions, feel free to ask them in the comments or on Twitter.
If you liked this tutorial, please consider supporting me on Buy Me a Coffee.
Thanks for reading!