API with NestJS #32. Introduction to Prisma with PostgreSQL


So far, in this series, we’ve been using TypeORM to manage our data and connect to our Postgres database. In this article, we squint into Prisma, which is a capable alternative.

With Prisma, we describe our data using a Prisma schema file. It uses its own data modeling language and acts as a each source of truth. This differs from traditional ORMs that provide an object-oriented way of working with the database.

Every time we make changes to the schema file, we need to generate the Prisma client. In this process, Prisma parses our schema and creates a vendee withal with all the TypeScript typings. This ways that we no longer map SQL tables to model classes through the TypeScript lawmaking manually.

The Prisma vendee is a set of Node.js functions that we can undeniability from within our code. Under the hood, the vendee connects to a query engine written in Rust.

Although it is possible to migrate from TypeORM to Prisma, in this aricle we set up our project from scratch.

You can find all of the lawmaking from this vendible in this repository.

Setting up PostgreSQL and Prisma

In this series, we are using PostgreSQL. The most straightforward way to add Postgres to our project is to do it with docker. In the second part of this series, we set up PostgreSQL with TypeORM. Let’s reuse the docker-compose file provided there:


We moreover need to create a file that contains environment variables for our docker container.


To start using Prisma, we first need to install it.

Once that’s done, we can start using the Prisma CLI. The first thing we want to do is to initialize our configuration.

Doing the whilom creates two files for us:

  • : contains the database schema and specifies the connection with the database
  • : contains environment variables

We need to modify the whilom file to retread to the contents of :


Within the file, we have wangle to our environment variables. Let’s use the variable to set up the connection.


Installing and using the Prisma Client

To start using the Prisma Client, we first need to install it.

Let’s create a matriculation that initializes the .


We moreover need to create a module that exports the whilom service.


Managing vital tables with Prisma Vendee and Prisma Migrate

When we want to modify the structure of our database, we should create a migration. It consists of a SQL script that is supposed to make the necessary changes. Although we could write it by hand, Prisma can do it for us with the Prisma Migrate tool.

First, let’s pinpoint a simple model in our schema.


Above, we pinpoint the vital set of fields for our model. Whilom we use just the and types, but there are quite a few to segregate from.

Aside from defining fields, we moreover can pinpoint attributes. Their job is to modify the policies of fields or models.

With the attribute, we pinpoint a default value for a field. When using it, we have a set of functions to segregate from. Above, we use to create a sequence of integers.

In our model, we moreover use the attribute. Thanks to that, our field becomes the primary key.

Once we’ve got our model, we can create our migration.

Above we use the flag, considering the Prisma Migrate tool is still in preview. It ways that the functionality was validated, but there might be small bugs.

Aside from creating a file with the SQL script, running the whilom writ moreover reverted our database.

Using Prisma Vendee to manage the data

Once all of the whilom is done, we can start using the Prisma Client. Let’s start with the basics.


The property was created when we generated our Prisma Vendee based on our schema and it recognizes the properties our entity has.

The method without any arguments returns all entities from the queried collection.


Above, we use the method that aims to find a each entity from our collection. We need to provide it with the property to narrow lanugo the telescopic of our search.

We can moreover use the property with the method.

If the method returns , we can seem that it didn’t find the entity. If that’s the case, we can throw an exception.


Doing the whilom results in responding with the 404 Not Found status code.

If you want to know increasingly well-nigh error handling, trammels out API with NestJS #4. Error handling and data validation


Above, we use the method to save a new entity in the database. The matriculation describes all of the properties withal with data validation.


If you want to read well-nigh validation, trammels out API with NestJS #4. Error handling and data validation


Above, we use the method to modify an entity. It is worth noting that we ignore the property so that the users can’t transpiration it.


We need to use the to see what errors have been thrown. To do that properly, we can create an enum based on the official documentation.


Above with the method, we do a very similar thing in terms of error handling.

Once we’ve got our service ready, we can create a controller that uses it.


It is worth noting that whilom we use the that transforms ids from strings to numbers.


Using multiple schema files

Putting all of our schema definitions into a each file might make it difficult to maintain. Unfortunately, Prisma currently does not support multiple schema files.

To deal with this issue, we can create a simple whack script that merges multiple schemas into one.


Running merges all files into the directory. It traverses through all of the subdirectories of .

The writ is misogynist in Linux, Mac, and Windows Power Shell

We can moreover put the file into so that we don’t have multiple sources of truth.


Doing the whilom allows us to alimony separate schema files for explicit modules. Let’s split our existing schema into multiple files:


We end up with the pursuit file structure:


In this article, we’ve learned the nuts of Prisma. We’ve got to know its main principles and we’ve created a simple CRUD API with it. To do that, we’ve had to get familiar with the nuts of the data modeling language Prisma uses. It moreover required us to learn well-nigh the nuts of the Prisma Migrate tool. There is still quite a lot to learn well-nigh Prisma, so stay tuned!

Series Navigation

<< API with NestJS #31. Two-factor authentication>”>API with NestJS #33. Managing PostgreSQL relationships with Prisma >>

Author: Shantun Parmar

Leave a Reply

Your email address will not be published.