A Next-Generation ORM: Prisma

What is Prisma?

Prisma is an open-source next-generation ORM. It consists of the following parts:

  • Prisma Client: Auto-generated and type-safe query builder for Node.js & TypeScript
  • Prisma Migrate: Migration system
  • Prisma Studio: GUI to view and edit data in your database

Prisma Client can be used in any Node.js (supported versions) or TypeScript backend application (including serverless applications and microservices). This can be a REST API, a GraphQL API, a gRPC API, or anything else that needs a database.

1. Prisma Client

Prisma Client JS is a type-safe database client that replaces traditional ORMs like Sequelize, Bookshelf, and Mongoose.

It allows us to access the database through plain JavaScript methods and objects without having to write the query in the database language itself.

In short, it acts as an abstraction in front of the database, so it’s easier to write CRUD (create, read, update, and delete) applications.

2. Prisma Migrate

Prisma Migrate is a powerful database schema migration tool. It uses a declarative data modeling syntax to describe our database schema.

Prisma Migrate also stores our entire migration history and easily lets us revert and replay migrations.

It also allows us to run before-and-after hooks to execute scripts while migrating so we can populate the database with required values during migration.

3. Prisma Studio

Prisma Studio allows us to visualize data via an Admin UI.

Here, we can perform CRUD (create, read, update, and delete) operations on our data. This is the easiest way to visualize data from our database as well as manipulate it.

How does Prisma work?

The Prisma schema

Every project that uses a tool from the Prisma toolkit starts with a Prisma schema file. The Prisma schema allows developers to define their application models in an intuitive data modeling language. It also contains the connection to a database and defines a generator:

Getting started

Set up Prisma

As a first step, navigate into it your project directory that contains the package.json file.

Next, add the Prisma CLI as a development dependency to your project:

You can now invoke the Prisma CLI by prefixing it with npx:

Next, set up your Prisma project by creating your Prisma schema file template with the following command:

This command created a new directory called prisma which contains a file named schema.prisma and a .env file in the root of the project. schema.prisma contains the Prisma schema with your database connection and the Prisma Client generator. .env is a dotenv file for defining environment variables (used for your database connection).

Connect your database

To connect your database, you need to set the url field of the datasource block in your Prisma schema to your database connection URL:

In this case, the url is set via an environment variable which is defined in .env:

You now need to adjust the connection URL to point to your own database.

The format of the connection URL for your database depends on the database you use. For PostgreSQL, it looks as follows (the parts spelled all-uppercased are placeholders for your specific connection details):

DATABASE_URL="postgresql://madhusha:1234@localhost:5432/mydb?schema=public"

postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA

Install and generate Prisma Client

To get started with Prisma Client, you need to install the @prisma/client package:

Notice that the @prisma/client node module references a folder named .prisma/client. The .prisma/client folder contains your generated Prisma client, and is modified each time you change the schema and run the following command:

This command reads your Prisma schema and generates your Prisma Client library.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Madhusha Prasad

Madhusha Prasad

Open Source Enthusiast | Full Stack Developer👨🏻‍💻 | JavaScript & Type Script , Git & GitHub Lover | Undergraduate — Software Engineering‍💻 | SLIIT👨🏻‍🎓