How to build a scalable, maintainable application with NestJs + MongoDB apply the design patterns and run in Docker(Part 1)

Phat Vo
5 min readAug 17, 2020

--

There are many kinds of Node Js frameworks that are empowered to enable the developers to build a scalable application based on it. For those developers who tend to gravitate towards OOP (Object Oriented Programming) then Typescript is an approach suitable to build a highly scalable and maintainable application. And NestJS framework has introduced to make things more simple.

Nest (NestJS) is an MVC framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with, and fully supports TypeScript.

So, in this section, we gonna talk about 3-Tier architecture and how to custom MVC to a 3-Tier architecture in NestJS.

A 3-tier architecture is a type of software architecture that is composed of three “tiers” or “layers” of logical computing. They are often used in applications as a specific type of client-server system. 3-tier architectures provide many benefits for production and development environments by modularizing the user interface, business logic, and data storage layers. Doing so gives greater flexibility to development teams by allowing them to update a specific part of an application independently of the other parts. This added flexibility can improve overall time-to-market and decrease development cycle times by giving development teams the ability to replace or upgrade independent tiers without affecting the other parts of the system.

Let’s have a look at figure 1.1 illustrates the architecture of a typical 3-tier monolithic application.

Figure 1.1: The architecture of a typical three-tier monolithic application

Each tier provides services to the tier above: The data tier provides a persistent state, the logic tier executes useful work, and the presentation layer presents the results back to the end-user.

To contact into a real-world project. Let’s going to build our application with NestJS and MongoDB, and produce some underlying APIs to help us dive dig into the 3-tier architecture.

There are some services and packages which are required to set up in your local machine before installing the Nest framework.

Those services are NodeJs, MongoDB, Docker, PM2, and some relative tools that are using to manage the services. Please refer to those links below if you are not familiar with those services mention above.

Those services that are mentioned above are paramount importance to take your application running up. We will have an overview of those services and which roles it charge of in the application in the next sections. In this tutorial, I imagine you have installed all services needed in your local machine and ready to start an application.

Finally, let’s create our application with Nest via the following commands shows as below:

  • Install Nest CLI as a global mode: npm i -g @nestjs/cli
  • Create a project with Nest CLI command: nest new "project-name"

There are two packages manager for the JavaScript programming language that is shown in creating project processing. So, you can choose a management tool that you’re frequently using and familiar with you the most. I would recommend using npm.

After creating the project you will see the Nest have initialized some packages and underlying architecture as shown in figure 1.2 below.

Figure 1.2: Nest underlying architecture from scratch

Let’s take a look at figure 2.1 that we can see into the folder src the folder shows some files which are respective for MVC architecture. Nest framework is highly inspired by Angular with each functionality are bundled in a module. So, the Controller and Service need to be declared in the module before exposure to another module in the application.

Let’s have a look into these other files existing in the project and dig in each file being charge of the role in the application.

  • Test folder performs the Unit testing functionalities, the TDD performs APIs testing to approach corresponding requirements. Nest integrates Jest, a testing framework to performs the testing in the application.
  • The .eslintrc.js is a configuration file for a static code analysis tool for identifying problematic patterns found in JavaScript code.
  • The .prettierrc is a configuration file for opinionated code formatted.
  • The nest-cli.json is a configuration file for command-line interface tool that helps you to initialize, develop, and maintain your Nest applications
  • The package.json is a configuration file and on the other hand, we can say this is the heart of your application. All the scripts like an `npm run start` and all dependencies packages like `Jest` are config and declare in this file.
  • The package-lock.json is automatically generated for any operations where npm modifies either the node_modules tree, or package.json. It describes the exact tree that was generated, such that subsequent installs are able to generate identical trees, regardless of intermediate dependency updates.
  • The tsconfig.json file in a directory indicates that the directory is the root of a TypeScript project. The tsconfig.json file specifies the root files and the compiler options required to compile the project.

After digging into the based application architecture and configuration files. You finally can start run the application via this command npm run start . The application will start as shown in the figure below.

There will be a dist folder created after running the command. This folder contains all our coding in thesrc folder that is compiled all Typescript to Javascript.

What Next?

Conclusively:
In this section, we have dig in the 3-tier architecture and have an overview of underlying Nest architecture.
We also create an application with Nest Framework successfully.
So, in the next section, we will learn how to connect to MongoDB in our application and custom our application to a 3-tier architecture, which applies some design patterns.

Let’s have a look at the next section.
Thanks for reading!

The next section (part2): https://medium.com/@phatdev/how-to-build-a-scalable-maintainable-application-with-nestjs-mongodb-apply-the-design-patterns-50112e9c99b4

Github source: https://github.com/phatvo21/nest-demo

List of content:

Part 1: https://medium.com/@phatdev/how-to-build-a-scalable-maintainable-application-with-nestjs-mongodb-apply-the-design-patterns-2f71c060652

Part 2: https://medium.com/@phatdev/how-to-build-a-scalable-maintainable-application-with-nestjs-mongodb-apply-the-design-patterns-50112e9c99b4

Part 3: https://medium.com/@phatdev/how-to-build-a-scalable-maintainable-application-with-nestjs-mongodb-apply-the-design-patterns-7b287af61354

Final Part: https://medium.com/@phatdev/how-to-build-a-scalable-maintainable-application-with-nestjs-mongodb-apply-the-design-patterns-789df9782959

--

--