Reader small image

You're reading from  Nest.js: A Progressive Node.js Framework

Product typeBook
Published inNov 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781800204737
Edition1st Edition
Languages
Tools
Right arrow
Authors (5):
Greg Magolan
Greg Magolan
author image
Greg Magolan

Greg Magolan is a senior architect, full-stack engineer, and Angular consultant at Rangle. He has been developing enterprise software solutions for over 15 years for companies such as Agilent Technologies, Electronic Arts, Avigilon, Energy Transfer Partners, FunnelEnvy, Yodel and ACM Facility Safety.
Read more about Greg Magolan

Patrick Housley
Patrick Housley
author image
Patrick Housley

Patrick Housley is a lead technologist at VML. He is an IT professional with over six years of experience in the technology industry. He is capable of analyzing complex issues spanning multiple technologies while providing detailed resolutions and explanations. He has strong front-end development skills with experience leading development teams in maintenance and greenfield projects.
Read more about Patrick Housley

Adrien de Peretti
Adrien de Peretti
author image
Adrien de Peretti

Adrien de Peretti is a full-stack JavaScript developer. He is passionate about new technologies and is constantly looking for new challenges. He is especially interested in artificial intelligence and robotics. W
Read more about Adrien de Peretti

Jay Bell
Jay Bell
author image
Jay Bell

Jay Bell is the CTO of Trellis. He is a senior Angular developer who uses Nest.js in production to develop industry-leading software to help non-profit organizations and charities in Canada. He is a serial entrepreneur who has developed software for a large range of purposes, from helping combat wildfires with drones to building mobile apps.
Read more about Jay Bell

David Guijarro
David Guijarro
author image
David Guijarro

David Guijarro is a front-end developer at Car2go Group GmbH. He has a wide experience working within the JavaScript ecosystem. He has successfully built and led multicultural, multifunctional teams.
Read more about David Guijarro

View More author details
Right arrow

Chapter 4. Dependency Injection system of Nest.js

This chapter provides an overview of the Dependency Injection (DI) pattern, which is frequently used today by the biggest frameworks. It is a way to keep code clean and easier to use. By using this pattern you end up with fewer coupled components and more reusable ones, which helps accelerate the development process time.

Here we examine the method that used the injection before the pattern existed, and how the injection changed in time to use Nest.js injection with a modern approach using TypeScript and decorators. You will also see snippets that show the advantage of this type of pattern, and modules provided by the framework.

Nest.js is based on Angular in terms of architecture, and is used to create testable, scalable, loosely-coupled and easily maintainable applications. As is the case with Angular, Nest.js has its own dependency injection system, which is part of the core of the framework, meaning that Nest.js is less dependent...

Overview of Dependency Injection

Since Typescript 1.5 introduces the notion of the decorator, you can do meta-programing using the added metadata provided by using a decorator on different objects or properties, such as class, function, function parameters or class property. The meta-programing is the ability to write some code or program using the metadata describing an object. This type of program allows you to modify the functioning of a program using its own metadata. In our case this metadata is of interest to us, because it helps inject some object into another object, whose name is Dependency Injection.

By using the decorator, you can add metadata on any object or property linked to those decorators. This will define, for example, the type of object that takes the decorator, but it can also define all of the parameters needed by a function that are described in its metadata. To get or define metadata on any object, you can also use the reflect-metadata library in order to manipulate...

Nest.js Dependency Injection

From the @nestjs/common you have access to the decorators provided by the framework and one of them is the @Module() decorator. This decorator is the main decorator to build all of your modules and work with the Nest.js Dependency Injection system between them.

Your application will have at least one module, which is the main one. The application can use only one module (the main one) in the case of a small app. Nonetheless, as your app grows, you will have to create several modules to arrange your app for the main module.

From the main module, Nest will know all of the related modules that you have imported, and then create the application tree to manage all of the Dependency Injections and the scope of the modules.

To do this, the @Module() decorator respects the ModuleMetadata interface, which defines the properties allowed to configure a module.

export interface ModuleMetadata {  
    imports?: any[];  
    providers?: any[];  
    controllers?: any...

The difference between Nest.js and Angular DI

Even if Nest.js is widely based on Angular, there is a major difference between them. In Angular, each service is a singleton, which is the same as Nest.js, but there is a possibility to ask Angular to provide a new instance of the service. To do that in Angular, you can use the providers property of the @Injectable() decorator to have a new instance of a provider registered in the module and available only for this component. That can be useful to have to avoid overwriting some properties through different components.

Summary

So to recap, we have seen in this chapter how it was unflexible and hard to test an object without using the Dependecy Injection. Also, we have learned more about the evolution of the method to implement the dependencies into the dependent, first by implementing the dependencies into the dependent, then changing the method by passing them manually into the constructor to arrive with the injector system. This then resolves the dependencies, injecting them in the constructor automatically by resolving a tree, which is how Nest.js uses this pattern.

In the next chapter we will see how Nest.js uses TypeORM, an Object Relational Mapping (ORM) that works with several different relational databases.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Nest.js: A Progressive Node.js Framework
Published in: Nov 2019Publisher: PacktISBN-13: 9781800204737
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (5)

author image
Greg Magolan

Greg Magolan is a senior architect, full-stack engineer, and Angular consultant at Rangle. He has been developing enterprise software solutions for over 15 years for companies such as Agilent Technologies, Electronic Arts, Avigilon, Energy Transfer Partners, FunnelEnvy, Yodel and ACM Facility Safety.
Read more about Greg Magolan

author image
Patrick Housley

Patrick Housley is a lead technologist at VML. He is an IT professional with over six years of experience in the technology industry. He is capable of analyzing complex issues spanning multiple technologies while providing detailed resolutions and explanations. He has strong front-end development skills with experience leading development teams in maintenance and greenfield projects.
Read more about Patrick Housley

author image
Adrien de Peretti

Adrien de Peretti is a full-stack JavaScript developer. He is passionate about new technologies and is constantly looking for new challenges. He is especially interested in artificial intelligence and robotics. W
Read more about Adrien de Peretti

author image
Jay Bell

Jay Bell is the CTO of Trellis. He is a senior Angular developer who uses Nest.js in production to develop industry-leading software to help non-profit organizations and charities in Canada. He is a serial entrepreneur who has developed software for a large range of purposes, from helping combat wildfires with drones to building mobile apps.
Read more about Jay Bell

author image
David Guijarro

David Guijarro is a front-end developer at Car2go Group GmbH. He has a wide experience working within the JavaScript ecosystem. He has successfully built and led multicultural, multifunctional teams.
Read more about David Guijarro