This article is for enthusiasts who strive to write clean, scalable, and more importantly refactorable code. It will give an idea about how Nest.JS can help us write clean code and what underlying architecture it uses.
Implementing a clean architecture with Nest.JS will require us to first comprehend what this framework is and how it works.
Nest or Nest.JS is a framework for building efficient, scalable Node.js applications (server-side) built with TypeScript. It uses Express or Fastify and allows a level of abstraction to enable developers to use an ample amount of modules (third-party) within their code.
Let’s dig deeper into what is this clean architecture all about.
Well, you all might have used or at least heard of MVC architecture. MVC stands for Model, View, Controller. The idea behind this is to separate our project structure into 3 different sections.
1. Model: It will contain the Object file which maps with Relation/Documents in the DB.
2. Controller: It is the request handler and is responsible for the business logic implementation and all the data manipulation.
3. View: This part will contain files that are concerned with the displaying of the data, either HTML files or some templating engine files.
To create a model, we need some kind of ORM/ODM tool/module/library to build it with. For instance, if you directly use the module, let’s say ‘sequelize’, and then use the same to implement login in your controller and make your core business logic dependent upon the ‘sequelize’. Now, down the line, let’s say after 10 years, there is a better tool in the market that you want to use, but as soon as you replace sequelize with it, you will have to change lots of lines of code to prevent it from breaking. Also, you’ll have to test all the features once again to check if it’s deployed successfully or not which may waste valuable time and resource as well. To overcome this challenge, we can use the last principle of SOLID which is the Dependency Inversion Principle, and a technique called dependency injection to avoid such a mess.
Still confused? Let me explain in detail.
So, what Dependency Inversion Principle says in simple words is, you create your core business logic and then build dependency around it. In other words, free your core logic and business rules from any kind of dependency and modify the outer layers in such a way that they are dependent on your core logic instead of your logic dependent on this. That’s what clean architecture is. It takes out the dependency from your core business logic and builds the system around it in such a way that they seem to be dependent on it rather than it being dependent on them.
Let’s try to understand this with the below diagram.
Source: Clean Architecture Cone
You can see that we have divided our architecture into 4 layers:
1. Entities: At its core, entities are the models(Enterprise rules) that define your enterprise rules and tell what the application is about. This layer will hardly change over time and is usually abstract and not accessible directly. For eg., every application has a ‘user’. What all fields the user should store, their types, and relations with other entities will comprise an Entity.
2. Use cases: It tells us how can we implement the enterprise rules. Let’s take the example of the user again. Now we know what data to be operated upon, the use case tells us how to operate upon this data, like the user will have a password that needs to be encrypted, the user needs to be created, and the password can be changed at any given point of time, etc.
3. Controllers/Gateways: These are channels that help us to implement the use cases using external tools and libraries using dependency injection.
4. External Tools: All the tools and libraries we use to build our logic will come under this layer eg. ORM, Emailer, Encryption, etc.
The tools we use will be depending upon how we channel them to use cases and in turn, use cases will depend upon the entities which is the core of our business. This way we have inverted the dependency from outwards to inwards. That’s what the Dependency Inversion Principal of SOLID implies.
Okay, by now, you got the gist of Nest.JS and understood how clean architecture works. Now the question arises, how these two are related?
Let’s try to understand what are the 3 building blocks of Nest.JS and what each of them does.
- Modules: Nest.JS is structured in such a way that we can treat each feature as a module. For eg., anything which is linked with the User such as models, controllers, DTOs, interfaces, etc., can be separated as a module. A module has a controller and a bunch of providers which are injectible functionalities like services, orm, emailer, etc.
- Controllers: Controllers in Nest.JS are interfaces between the network and your logic. They are used to handle requests and return responses to the client side of the application (for example, call to the API).
- Providers (Services): Providers are injectable services/functionalities which we can inject into controllers and other providers to provide flexibility and extra functionality. They abstract any form of complexity and logic.
- We have controllers that act as interfaces (3rd layer of clean architecture)
- We have providers which can be injected to provide functionality (4th layer of clean architecture: DB, Devices, etc.)
- We can also create services and repositories to define our use case (2nd Layer)
- We can define our entities using DB providers (1st Layer)
Nest.JS is a powerful Node.JS framework and the most well-known typescript available today. Now that you’ve got the lowdown on this framework, you must be wondering if we can use it to build a project structure with a clean architecture. Well, the answer is -Yes! Absolutely. How? I’ll explain in the next series of this article.
Till then, Stay tuned!
About the Author:
Junaid Bhat is currently working as a Tech Lead in Mantra Labs. He is a tech enthusiast striving to become a better engineer every day by following industry standards and aligned towards a more structured approach to problem-solving.
Read our latest blog: Golang-Beego Framework and its Applications
Knowledge thats worth delivered in your inbox