Why you should consider Prisma.io

Iben O'Neal Jr.
5 min readJun 15, 2022

--

Prisma is quickly becoming my go-to ORM and for good reason. Setting up a database has always been a more tedious task when creating a web application. ORMs have been around for a while and have streamlined the process a bit for the most part. However, they always seem to lack in one aspect or another. For every good thing a dev can say about an ORM, there is usually something the dev wishes the ORM did better. For me, this was schema design and the ability to view and edit your data quickly. Prisma solves these problems for me, while also tackling a number of problems I did not know I had.

What is Prisma?

Prisma describes itself as a

Next-generation Node.js and TypeScript ORM

Prisma Schema

At the core of Prisma is its approach to schema design. It is written in a property language called PSL (Prisma Schema Language) and takes minutes to become familiarized with. There are three main components to the schema file. First is your data source, this is where you provide the URL to your database as well as the provider. The provider in this case would just be the type of database you or using. This section would look something like this.

datasource db {
url = env("DATABASE_URL")
provider = "postgresql"
}

The next part is the generator field. This field specifies what client should be generated based on the data model. This feature is how you will actually query your database using Prisma. We will talk about it more later. This section would look like this.

generator client {
provider = "prisma-client-js"
}

Lastly, we have our model definitions, this will be the meat of this file. Here you create your models and their relations. This part will take the longest to get used to, however, it will still seem familiar. Instead of creating plain JS objects, PSL formats models a little differently, but with all the same fields you are used to interacting with. This is an example user model.

model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
email String @unique
name String?
role Role @default(USER)
posts Post[]
}

Here we have a User with six fields, the ‘id’ is an integer, and has a default value of autoincrementing the last id. We also have the standard, unique tags and a few more that you can explore in the official docs.

Your next question is probably “How do relations work?”, and thankfully they are pretty simple to understand. Here is the model for a post made by a user.

model Post {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
published Boolean @default(false)
title String @db.VarChar(255)
author User? @relation(fields: [authorId], references: [id])
authorId Int?
}

The relation is happening in the author field of the Post. The concept is rather simple, the author field is a User, which refers back to the User model. We then create a relationship where the field on this model “authorId” references the “id” field on a User.

The last big pillar to tackle is keeping your database in sync with your models, Prisma does this with Prisma Migrate. This is a simple command line, that migrates your database with any changes you may have.

Query

Rember the generator section of your Prisma schema file, here is where you will use that. Your generator block is what Prisma uses to decide how to build its client. It is built to make type-safe queries tailored to your data. This means the functions to query your data are a lot easier to understand and use.

const newUser = await prisma.user.create({
data: {
name: 'Alice',
email: 'alice@prisma.io',
},
})
const users = await prisma.user.findMany()

This is an example of creating and finding users. When generating a client for your data, Prisma adds methods to a Prisma object that correlate exactly to your data. Continuing on with the example of Users and Post, we would also be able to do this.

prisma.post.create()

This simple generation allows for powerful queries that many ORMs have a weird workaround to do. For a look into better queries check here.

Perks of Prisma

There are two features that make me at least consider it, for any project even ones where I was already using a different ORM.

Introspection

Prisma Docs

Introspection is a feature that allows you to use your already created database to generate a Prisma Schema that matches what you have. So if you have a database that is already set up as you like, but want to try Prisma this process is seamless and easy. Your Prisma Schema file is generated and you can begin using Prisma immediately. All done in a single command.

prisma db pull

Prisma Studio

Prisma also creates a locally hosted web view client that you can interact with to change or update data. Prisma Studio is great for quickly checking data or changing something to test. It may not be as advanced as something like PgAdmin or MySql Workbench but it still provides you with enough options for most database work. My only gripe is that you are not able to customize the look of the studio like you can with something like Sanity.

Summary

  • Prisma is a reimaging of what an ORM can be, and how it can function
  • The Schema is a refreshing way to model your data.
  • Introspection makes it easy to switch to Prisma from any other ORM.
  • Prisma Studio is the cherry on top.

Sources

--

--