Prisma is a new data mapper ORM tool that serves as an interface between your web application and your favorite database. It abstracts the complexities of communicating with the server and allows you to focus on building your application rather than thinking about the next SQL query thanks to its intuitive data model, automated migrations, type-safety and auto-completion. Here’s how it works.
Prisma Explained
Prisma is an ORM tool that serves as an interface between your web application and database. It utilizes data modeling, automated migrations, type-safety and auto-completion to simplify the data management process and eliminates the need for SQL queries.
How Does Prisma Work?
To get started with Prisma, you must be familiar with its toolset listed below
Prisma Client is a major component of the Prisma database toolkit, and it is so important that it is often referred to simply as Prisma. It enables developers to interact with their databases in a seamless and efficient way.
Prisma Client is a type-safe database client that is generated based on your Prisma Schema. This means that every query you make using Prisma Client is validated by the schema, ensuring that you’re always working with valid data. Prisma Client provides a wide range of features for working with your database, including basic CRUD operations, filtering, sorting, and aggregation.
Prisma Schema is like the instructions for a puzzle that tell you where all the pieces go. The schema tells Prisma what your database looks like and how it’s organized.
One of the key benefits of Prisma Client is its ease of use. Generating the Prisma Client is a straightforward process that will be covered below and takes just a few minutes. Once you have it set up, you can start interacting with your database using familiar JavaScript/TypeScript syntax. With Prisma Client, you don’t need to worry about writing complex SQL queries or dealing with low-level database details. Instead, you can focus on building amazing applications that make the most of your data.
How to Set Up Prisma
To get started you will first need to install it into your project:
npm install prisma --save-dev
npx prisma
Then create a prisma.schema
file. This file defines your database schema and serves as the basis for generating your Prisma Client.
Here’s an example of what a Prisma schema file might look like for an authentication system:
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
model User {
id Int @id @default(autoincrement())
email String @unique
password String
}
In this example, we’ve defined a data source called db that uses PostgreSQL as the provider. We’ve also defined a generator called client that uses the Prisma Client provider. Finally, we’ve defined a model called User
that has an ID
field that auto-increments, an email
field that is unique and a password
field.
To use this schema in your code, you’ll first need to generate your Prisma Client.
How to Install Prisma Client
You can do this by running the following command:
npm install @prisma/client
This command also runs the prisma generate
command, which generates the Prisma Client then you can use Prisma Client to send queries to your database. Below is a snippet
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
// use `prisma` in your application to read and write data in your DB
To use this schema in your code, you’ll first need to generate your Prisma Client. You can do this by running the following command:
npx prisma generate
Once you’ve generated your Prisma Client, you can use it in your code to interact with your database. Here’s an example of how you might use the Prisma Client to create a new user:
const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()
async function createUser(email, password) {
const newUser = await prisma.user.create({
data: {
email: email,
password: password
}
})
return newUser
}
createUser('[email protected]', 'password123')
.then(user => console.log(user))
.catch(error => console.error(error))
In this example, we’ve created a function called createUser
that takes an email
and password
as arguments. We use the Prisma Client to create a new user with the specified email
and password
, and then return the new user. We’ve also included some error handling in case anything goes wrong.
How to Set Up Prisma Migrate
Prisma Migrate is a powerful tool that makes it easy to manage database migrations in your Prisma projects. With Prisma Migrate, you can easily create, modify, and revert database schema changes in a safe and controlled manner.
To create a new migration, you can run the following command:
npx prisma migrate dev --name init
This will create a new directory called migrations in your project root directory, along with a new migration file named "init"
. You can edit this migration file to define your desired schema changes.
// migrations/20220227150000_init.js
If you need to roll back the migration for any reason, you can use the following command:
npx prisma migrate reset
How to Set Up Prisma Studio
Prisma Studio is the visual tool that allows you to interact with your database using a graphical user interface. Here’s how to set up Prisma Studio and apply it to the schema we created earlier
Prisma Studio is actually included as part of the Prisma CLI, which means that it is installed automatically when you install Prisma using npm or yarn.
To launch Prisma Studio, you simply need to run this in your terminal:
npx prisma studio
This will open the Prisma Studio UI in your default web browser. This command will work as long as you have Prisma installed and you are in a directory that contains a Prisma schema file.
Once you’re connected to your database, you'll see a graphical representation of your schema in the left-hand pane of Prisma Studio. You can click on each model to view and edit its data, as well as add and delete records.
For example, let’s say we want to add a new user to our database. We can do this by clicking on the User
model in the left-hand pane, and then clicking the “Create new User” button. This will bring up a form where we can enter the new user's information.
Once we’ve entered the user's information and clicked the “Create” button, Prisma Studio will add the new user to our database and update the graphical representation of our schema accordingly.
Advantages of Prisma
Although Prisma is not the first ORM tool, it is a game-changer for developers looking to streamline their database workflows and build amazing applications faster than ever before. With its intuitive data modeling capabilities, type-safe database client generation, and powerful query features, Prisma has revolutionized the way we interact with databases. By eliminating the need for tedious SQL queries and simplifying database management, Prisma allows developers to focus on what they do best — building great applications.
Frequently Asked Questions
What is Prisma and why use it?
Prisma is a useful object-relational mapping (ORM) tool for web developers that can be used as an interface between your web application and database. It provides type-safe database client generation and intuitive data modeling, which eliminates the need for SQL queries and simplifies the database management process for developers.
How do you set up Prisma?
To set up Prisma, first install the project:
npm install prisma --save-dev
npx prisma
Then, create the Prisma schema file to look like this:
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
model User {
id Int @id @default(autoincrement())
email String @unique
password String
}