/

How to Use Prisma: A Comprehensive Tutorial

How to Use Prisma: A Comprehensive Tutorial

Prisma is a powerful ORM (Object-Relational Mapping) tool that provides an abstraction layer over databases. In this tutorial, we will explore how to get started with Prisma and use it to build a React application based on Next.js.

To begin, create a new Next.js app in a folder using the following command:

1
npx create-next-app

Next, include Prisma in your dev dependencies by running:

1
npm install -D prisma

Now, you have access to the Prisma CLI utility. Try running the following command to see the instructions on how to use it:

1
npx prisma

Next, set up Prisma for your project by running:

1
npx prisma init

This will create a prisma folder with a schema.prisma file. The schema file defines the data model and database configuration. It also creates a .env file with the DATABASE_URL environment variable. Make sure to replace the value of DATABASE_URL with the connection URL of your database.

Prisma supports various relational databases such as PostgreSQL, SQLite, MySQL, AWS Aurora, and MariaDB. You can choose the one that fits your needs.

Now, let’s add a model to the schema. For example, let’s create a Car model to store information about cars we want to buy. Here’s an example schema for the Car model:

1
2
3
4
5
6
7
model Car {
id Int @id @default(autoincrement())
brand String
model String
created_at DateTime @default(now())
bought Boolean @default(false)
}

The model defines five fields: id, brand, model, created_at, and bought, each with its respective type. The id field has the @id attribute, which specifies it as the primary key. The other fields have default values.

Once you have defined the schema, you need to sync the database with the schema. Run the following command to create the necessary migration:

1
npx prisma migrate dev

This will create a Car table in the database based on the schema definition.

Now you can use Prisma to perform database operations. Install the @prisma/client package by running:

1
npm install @prisma/client

Create a lib folder and inside it, create a prisma.js file. In this file, initialize the PrismaClient object as shown below:

1
2
3
4
5
6
7
8
9
import { PrismaClient } from '@prisma/client'

let global = {}

const prisma = global.prisma || new PrismaClient()

if (process.env.NODE_ENV === 'development') global.prisma = prisma

export default prisma

This code initializes the PrismaClient object and adds it to a global variable to avoid excessive instantiations when running in development mode.

To use Prisma in any file, import it as follows:

1
import prisma from 'lib/prisma'

Now you can perform various database operations using Prisma. For example, to retrieve all cars, you can use the prisma.car.findMany() method:

1
const cars = await prisma.car.findMany()

You can also filter the data by adding an object to the findMany() method:

1
2
3
4
5
const cars = await prisma.car.findMany({
where: {
brand: 'Ford',
},
})

To retrieve a single car by its id, use the prisma.car.findUnique() method:

1
2
3
4
5
const car = await prisma.car.findUnique({
where: {
id: 1,
},
})

To create a new car, use the prisma.car.create() method:

1
2
3
4
const car = await prisma.car.create({
brand: 'Ford',
model: 'Fiesta',
})

To delete a car, use the prisma.car.delete() method:

1
2
3
await prisma.car.delete({
where: { id: 1 },
})

To update the data of a car, use the prisma.car.update() method:

1
2
3
4
5
6
await prisma.car.update({
where: { id: 1 },
data: {
bought: true,
},
})

These are just the basics of using Prisma. You can explore more advanced features in the Prisma schema reference documentation.

In summary, Prisma is a powerful ORM tool that simplifies database operations in your applications. By following this tutorial, you should be able to get started with Prisma and leverage its capabilities to build robust applications.

tags: [“Prisma”, “ORM”, “React”, “Next.js”, “database operations”, “CRUD application”]