An overview of Nest.js with Prisma ORM

Daniel Brum
White Prompt Blog
Published in
5 min readSep 23, 2022

--

Introduction

Nest.js is a consolidated and widely used framework on the Node.js environment for building applications and services. It also has great support and integration with a lot of different tools like ORMs, or Object-Relational Mapping, message queues, logging, and more.

Prisma is a great ORM that has been gaining traction lately. This framework has great documentation, type safety and it is fairly easy to grasp. When creating an application, Nest.js and Prisma are a great combination.

The purpose of this article is to provide an overview and show some of the benefits of these tools through the building of a simple backend application.

At White Prompt, we are a next-level software development company who are collaborators and big-picture thinkers. We create out-of-the-box solutions for our clients while working to understand your big goals and then drill down to the details on how to get there. If you are looking to build an application or find a solution, contact us today. We want to hear all about it.

Problem to solve

Now, let’s understand what our problem is that we would like to solve in the article. The problem at hand is to design a simple CRUD application to show the benefits of using Nest.js combined with Prisma ORM. As you follow along, we will set up the application. Let’s get started.

Requirements

Before we begin the application development process, we need to have a few applications installed:

  • Node.js,
  • NPM,
  • Nest.js,
  • Nest.js CLI,
  • PostgresSQL (or any other database that Prisma supports).

Journey

First, we have to set up the environment to begin application development. The idea here is to build something simple that can be used in more complex projects and show some functionalities of these tools.

In order to run the project, we must create a few applications. First, we start by creating the Nest.js application with the Nest.js CLI. To do so, run this command:

nest new

If you don’t have the CLI installed, run this command:

npm i -g @nestjs/cli

Now that the application is bootstrapped with the basic foundation, let’s start to code our application.

First, we have to define our schema on the Prisma schema file. To generate this file, install prisma with npm i prisma,and then run npx prisma init.

Prisma will create the schema and will add the database URL right away on an env file, replacing it with your database URL. Now you will be able to connect to your database.

Prisma schema

If you forgot to add a field, you can always run a migration to update your schema.

For instance, in this case I forgot to add the title field on the book model, so let’s change it and run a migration to reflect the change on the database:

Prisma schema after change

Now that we have the field, lets run a migration with the command: prisma migrate dev --name title-migration.

Successful Prisma migration

Now that we applied the migration and have the table on the database, let’s code our application.

First, let’s create a controller and define some routes for the application:

After creating the controller, we need to create a service for our application.

To use the Prisma functions on our service layer, we have to install the prisma client with npm install @prisma/client.

The application is finished, so let’s do some requests on Postman to check if everything is working properly.

Create a book:

Book creation request

Retrieve all of the books:

Retrieve all available books request

Retrieve a single book:

Retrieve a single book request

Edit a book:

Edit a book request

Delete a book:

Delete a book request

With the requests, we can validate that our application is up and running correctly.

Conclusion

As we can see, combining Nest.js and Prisma will speed up the development process of an application. There are advantages to utilizing both frameworks, and to highlight those, both frameworks have great documentation, they are easy to learn and both have a good community as a resource. In addition to that, Prisma has great tools for migrations as well as a great API.

Overall, Prisma and Nest.js work well together and provide an enjoyable experience, but there are some things to be aware of before choosing Prisma as your ORM. You can’t split your models into different schemas, and Prisma doesn’t support a huge number of databases yet. With that being said, it is worth trying and can add a lot of value to your development process.

Please go to the linked repository below to look further into the code.

We think and we do!

Do you have a business that requires an efficient and powerful data architecture to succeed? Get in touch with us at White Prompt, and we will make it happen!

References

Prisma

Prisma migrations

Prisma comparisons with other ORMs

Nest

Nest.js and Prisma recipe

Github repository

--

--