NestJs - Modules [04]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in the previous episodes we covered controllers and providers and we also touched a little bit on modules this video is going to be fully dedicated to modules so sit back and let's get started modules are the backbone of any sjs application you can think of them as different Lego pieces where each of them has its own purpose and contributes to the whole structure of our application they are treated as building blocks for one whole application every nsjs application has at least one module which is the app module which is the root module of our whole application which if we take a look at main.ts whenever we're creating a new nest.js application we are passing this app module class as a parameter which is the root module so every nsjs application ever has at least one module but of course in most cases you're going to have different modules because the purpose of modules is to encapsulate a set of functionality to have a clean code separated feature based and easy to scale now if we take a look at what a module is it's simply a class with that module decorator which takes an object of four different properties the first one being Imports which takes an array of different modules if we hover over Imports it's a list of imported modules that export the providers which are required in this module and if you recall in the last video whenever in cats we needed to inject the customer service inside of the controller what we had to do was go to customer module since the customer service belongs to that module and we had to add it to the export array and then what we needed to do was go to the cats module and now import the customer module the controller's property takes an array of controllers so as you can recall in customer module for example where we have the customer controller we were not able to make any request to this controller before actually registering it inside of the controller's array and of course after that importing the whole customer module to our root module or app module provider is used to register providers so for example in the customer module we had a customer service which of course is decorated with ad injectable and which we used as a dependency inside of the customer controller for it to be handled by the ioc container in sjs we had to actually register it inside of the provider's array if we take a look here it's a list of providers that will be instantiated by the nest injector and that may be shared at least across this module so we can see that a provider is not Global based however it's module based meaning I am not able to use this provider customer service outside of this module in which it is registered without actually exporting it which takes me to the fourth property exports exports takes an array of providers that we want to be used outside of this module in which it belongs to so customer service this provider belongs to the customer module however if you wanted to be used outside of this module for example in cats or in any other different module we need to export it so export it takes a list of providers that are provided by this module so it is registered in this volume module and provided by this module okay and should be available in other modules which import this module and that is correct whenever we needed to use the custom service this provider inside of the cats module specifically inside of the cat's controller before being able to actually inject it we had to import the whole customer module inside of the cats module so that we can have access to the exports array or the different exported providers by the customer module so now we've seen how those different properties actually shape the way our application works necess.js creators highly recommend that you create different modules for different features in your application so for example we have a catch feature hence why we created a cat's module and we have a customer feature which is why we created a separate customer module this way you can maintain your code keep it clean easily testable and easily scalable and every module should encapsulate its own functionalities now let's say you have a new feature in your app you would go ahead and create a new module of course you can create a module class manually and then use the module decorator however we can use an sjs command such as Nest G or generate module and then we say the module name for example let's say product and as you can see Nest has created a product folder and inside of that folder we can see a product module class with the module decorator now whenever we complete our module if you want it to be used or instantiated by nest what we we would need to do is take that product module class name go to app module and then add it to the Imports array however when we use the NSG module product command nest.js was smart enough to actually add it to the root module AKA app module if you remember in the last video we used a different command which was nest generate resource and then we passed it the resource name cats or customer which also LED Nest to create a cats folder containing a cast controller a cat's module and a cat service there's also an important concept that you should know exists in sjs which is module re-exporting so we can see here we have a core module we know it's a module because it's decorated with ADD module and we can see that this core module Imports a module called common module meaning that it will have access to all the providers exported by Common module since we imported it here but if we take a look at exports we see something strange we see that we are exporting common module which we just imported now previously whenever we use export it was to actually export providers and not modules but now we can see that we can actually also export different modules and not just providers and it's good to know that it has the same effect so before whenever we used to export a provider any module that would import core module would have access to any provider registered inside of the core module and exported by it and it works the same for modules so now since the core module exports common module literally any module in our application that has core module in its Imports array will also have access to the common module and every single provider exported by Common module let's see that in action so I have here a product module and I have a controller and a service let's say inside of the controller I don't want to inject the product service provider instead I want to inject the customer service provider this one so let's go ahead and try to do that all right perfect now we have customer service and now I've created a new Handler and I'm making use of the customer service which is the provider created and registered inside of the customer's module inside of the provider's array which is this one so I'm trying to inject it in products controller and then I'm calling a method on it so let's see what happens if I try to run my service and as you can see we get this error that we've seen previously that Nest cannot resolve the dependency of the products controller and if you take a look here it tells us that the error is at index 0 meaning the first parameter in the controller here and it asks us is product module a valid in sjs module indeed it is and it is imported inside of app module and then it asks us if customer service as a provider is it part of the current product module so if you remember we said that providers are module scoped so is it inside the current product module well no it says inside the customer module now it is exported from the customer module but the customer module isn't imported here in product module so of course we cannot make use of that provider previously to fix it what we have to do was simply import the customer module sets the customer module injects and registers the customer service so let's go ahead and save and as you can see we don't have any problems so now let's see how we can actually solve the same issue but with a different approach which is the module re-exporting approach so now instead of importing customer module directly what I'm going to do is go to cats module and then here whenever we are importing customer module I want to go ahead and Export the customer module so now cats module is importing customer module meaning it has access to the export array meaning it has access and it can inject the customer service provider but now it's also exporting the customer module so if I go back to product modules and this time instead of actually importing customer module right away I'm going to try to import cats module and run the application again and as you can see it is still working I am still able to inject the customer service which is in the customer module without having to actually import the customer module explicitly instead we imported the cats module which exports the customer module after of course importing it so now if I remove the exports array and I read on the servers so now what I'm doing is I'm simply importing cat's module but cats module does not Export customer module it just Imports it so it can use it internally as you can see the error comes back so now you can see how module exporting works now if you have a provider such as we've seen here for example customer service and you need to use it everywhere in your app it would get very ugly if you have to import the customer module everywhere so in the catch module we had to import customer module and then in product mode you'll be had to either import customer module explicitly or import a module that exports customer module so it could get very ugly very quickly if we need to import the same module everywhere instead what we could do is create a new module and I called it Global module but you could call it anything you want as long as on top of that module you specify Global and of course you need to import import it from nest.js common marking this module as a global module and now inside of this module we can save providers and inside of this array we can actually register the customer service provider that we've been using all along in different places and now after doing that we can go to customer module and then remove customer service from the export you can also remove the registration of the customer service from this module and in cats module we no longer need to input and Export the customer module since before we will do we were simply doing it to get access to the customer provider but now we're gonna make it Global because we're using it inside of the global module marked with at Global however there is still one thing that we need to do which is register the global module so what we need to do is just with any usual module we need to add it and the Imports array of our root module which is the app module which is here for example now if I go ahead and run my application it seems that we still have the same error well let's take a look again at our Global module what we did was we registered customer service but we forgot to actually export it as well so here we need to export it and now if we save it as you can see it's working so I am able to inject the customer service inside of the customer controller which belongs to customer module without actually needing to register it inside of that module anymore and then inside of the cache controller as well I was able to inject the customer service without having to import any other module and inside of the product as well inside of product module I can remove the cats module import distorted and still the app is still working even though inside of product controller I'm making use of customer service and that's because we have injected or we have registered the customer service and then exported it inside of a global module there's also one important feature in Nest which is dynamic modules where you can actually create a module dynamically this feature enables you to easily create customizable modules that can register and configure providers dynamically now there's another section in the documentation outside of the overview where they actually cover that inside of fundamentals if you want to take a look a deeper look I'm not going to be covering that in this video but really quickly here you can see that they have created a class database module which is a module and then they have a static for root method which returns Json which contains module providers and exports and they can also add Global to it making it Global or not now we're going to be using some similar modules in our application when we reach the database episode but if you want to read about those feel free to go to documentation and read them so in this video we covered how modules are the building blocks of an sjs application how each module encapsulates a set of capabilities or functionalities to keep our application clean we have seen that a module is simply a class decorated with ADD module which has Imports controller providers and exports Imports is used to import different modules so that we can have access to their functionality or to their exports array controllers is used to register to different controllers to be instantiated by Nest providers is simply used to register our different providers and by default providers are a module scoped so they live or they belong to a certain module and can be used inside of it but we've seen how we can use the export property to export a specific provider and then if we import the module inside of a different module we can use it so we've seen that we can have modules depending on other modules this is also known as using a shared module we've seen how we can do module exporting so we import a module and then we export it and then any module that Imports the module that has this modularly exportation will have access to the functionality of that module important inside of it and Export it and we've also seen how we can make a provider Global by adding it inside of a global module which has the global decorator and then of course we would need to register that Global module inside of one module typically the app module here so that it can be used this way we will be able to use any exported provider inside of different modules because it is global now if this video was helpful please leave a like subscribe and I'll see you in the next one
Info
Channel: Computerix
Views: 1,889
Rating: undefined out of 5
Keywords:
Id: eN8ok5yQOTc
Channel Id: undefined
Length: 16min 2sec (962 seconds)
Published: Sun May 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.