API with NestJS #32. Introduction to Prisma with PostgreSQL

image

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:

docker-compose.yml

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

docker.env

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 :

.env

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

schema.prisma

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 .

prisma.service.ts

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

prisma.module.ts

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.

schema.prisma

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.

posts.service.ts

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.

posts.service.ts

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.

postNotFound.exception.ts

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

posts.service.ts

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

createPost.dto.ts

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

posts.service.ts

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.

updatePost.dto.ts

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.

prismaError.ts
posts.service.ts

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.

posts.controller.ts

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

findOneParams.ts

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.

package.json

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.

.gitignore

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

baseSchema.prisma
postSchema.prisma

We end up with the pursuit file structure:

Summary

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 >>

Comments are closed.