Clicky

How to Build a Rest API with NestJS and MongoDB?

Published on June 21st, 2022

Share

NestJS is a scalable, server-side progressive JavaScript framework for building efficient, scalable Node.js web applications. It is built with TypeScript that still preserves compatibility with JavaScript and uses ExpressJS and fastify, which makes it an effective tool for building efficient and reliable backend applications. It has a modular architecture that provides a mature, structural design pattern to the Node.js development world.

Nestjs is heavily inspired by angular. Nest.js uses a modular syntax. Nest.js applications have a modular design; it comes installed with a single root module, which is often sufficient for a small application. But when an application starts to grow, Nest.js recommends a multiple-module organization, splitting the code into related features.

NestJS uses a decorator pattern similar to writing an API using Spring Boot. However, due to single-threaded asynchronous execution Nest JS performance is higher than any other server-side rendered framework like Next JS or Nuxt JS. 

Few assumptions about you

If you’re reading this article, our assumption is that 

  • You’re familiar with Nest.js, and Javascript in general
  • Prior experience with MongoDB
  • Basic know-how of REST API
  • Basic typescript knowledge

How to get started?

A module in Nest.js is identified by the @Module() decorator and takes in an object with properties such as controllers and providers. Each of these properties takes an array of controllers and providers, respectively.

For creating a default nest project, we can use nest:

nest-mongodb-blog-1

Now nest will create a basic project for us.

For starting the nest project in development mode, we need to use the command:

nest-mongodb-blog-2

To start building the todo REST API, we need to create a new module. For creating a new module, we can use the nest to generate a module for us.

With similar commands, we can create Controllers and Services for our Module.

Get connected to MongoDB

For this project, we are using MongoDB as a database for storing Todos. For connecting to MongoDB, we need to install a few more dependencies.

After this, we need to connect to MongoDB from our root module, the app Module.

To use this in our todo module, we have to import this into our todo module.

Now we can create a schema for our Todo Module. Nest supports two methods for integrating with the MongoDB database. You can use the built-in TypeORM module described here, a connector for MongoDB, or use Mongoose, the most popular MongoDB object modeling tool. This chapter will describe the latter using the dedicated @nestjs/mongoose package.

We can start creating routes for our REST API in the controllers’ file. In the todo controller file, we first need to import to do service in the constructor of the controller class.

Create routes for the REST API

Now we can create routes for the REST API

Now that we have successfully created routes for our REST API, we can start implementing the business logic in the services file.

First, we will create a to-do for that. We first need to create a DTO(Data transfer object) for our body.

Now that we have created a DTO for the body of Todo. We can start implementing the create Todo logic. For that, we first need to inject our Model into Todo services.

Adding business logic REST API

Now let’s start implementing the business logic of our REST API.

The logic for getting all todos and creating a todo:

The logic for getting Todo by ID, updating a todo, and deleting a todo:

Now let’s start using these services in our controllers to complete our API.

Now our REST API is complete….

Conclusion

Creating a quick REST API with MongoDB NestJS is the way to go. It’s not only good for “quick” implementation but also awesome for huge projects as it encourages developers to use Domain-Driven Design.

About the Author

Debasis Pattanaik

A software engineer at Firstprinciples, Debasis specializes in full-stack and is responsible for backend and frontend for developing progressive systems and applications, including accounting software, mass communication procedures, and organizational apps. In his free time, Debasis likes to travel, crochet, and play video games.

Schedule a Personalized Strategy Session to Get More MQLs!

Ebooks

As the top choice for SaaS SEO, we believe in delivering quality results with the best suitable SEO Packages. It is proven that the top 3 results in Google get 92% of the traffic so we have a team of passionate digital marketers who are experts in search engine optimization.

The Ultimate Guide to Keyword Research and Content Strategy 2022

The Ultimate Guide to Keyword Research and Content Strategy 2022

The Advanced Guide to Analytics and Ranking 2022

The Advanced Guide to Analytics and Ranking 2022

The Advanced Guide Link Building For SaaS 2022

The Advanced Guide Link Building For SaaS 2022

The Ultimate Guide to SaaS SEO Audit 2022

The Ultimate Guide to SaaS SEO Audit 2022

Become a Part of the
FirstPrinciples Family!

At FirstPrinciples, we empower our people to express ideas that drive success. Our open and collaborative work culture motivates our people towards building their dream career.

I’m Happy to join!