DEV Community

Cover image for What the heck is prisma?
Joshua Olajide
Joshua Olajide

Posted on

What the heck is prisma?

Introduction

"Prisma" is a new kind of 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 & auto-completion.

Getting Started

To get started with Prisma, you must be familiar with it's toolset listed below

Prisma Client
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 major component of the Prisma database toolkit, and it is so important that it is often referred to simply as "Prisma

At its core, 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 which will be covered below in this article and it takes just a few minutes, and 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.

Prisma Setup / Installation

  • To get started you will first need to install it into your project
npm install prisma --save-dev
npx prisma
Enter fullscreen mode Exit fullscreen mode
  • Then go ahead and 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
}
Enter fullscreen mode Exit fullscreen mode

In this example, we've defined a datasource 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. You can do this by running the following command:

Install Prisma Client

npm install @prisma/client
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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('test@example.com', 'password123')
  .then(user => console.log(user))
  .catch(error => console.error(error))
Enter fullscreen mode Exit fullscreen mode

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.

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

In case you need to roll back the migration for any reason, you can use the following command:

npx prisma migrate reset
Enter fullscreen mode Exit fullscreen mode

For Informations on MongoDB migration Read about it here

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
Enter fullscreen mode Exit fullscreen mode

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.

In Conclusion

Although, Prisma is not the first ORM tool but 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 building what they do best - great applications.

References

Top comments (2)

Collapse
 
ayoyimika6 profile image
Fiery eyes

Great article 👏👏 Well detailed

Collapse
 
joshtom profile image
Joshua Olajide

Thanks Vic