NestJS Microservices Full Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to the channel in this video we'll build a microservices example using sjs this video is meant to be combined with other videos so if you want to build this up with angular react or view i provided some links in the description below the backend of this app will be composed into two microservices the admin microservice and the main microservice they will be both built using sjs and they will have their own separate database the admin app will be using mysql database and the main app will be using mongodb database they will communicate with each other using rabbitmq events and internal api calls so we'll see the difference there so this is the app it's simplified compared to the other microservices that i built with different languages and i hope you learned a lot from this tutorial this is the app that we will build so there are two apps here this is the admin app and this is the main app so these apps will use two different backends and two different databases but they will communicate each other using rabbitmq event like you see on my right side so right now i will create a new product so new product and i will use an image so when i create the image we can see here the new product we will also see an event happening via rabbitmq right now because they will not use the same map and the same database the only way of communication is by rapidmq events and as we can see here the product is created so this is using a mongodb database and this is using a mysql database and if i like the product it will also reflect here we can see likes have increased but this time we won't use the rabbitmq events this time we will use http request so we will call one microservice we'll call the other directly so let's see how this works via coding so let's start creating our netgis application so go to nesjs.com and here click documentation so here we need only to install an sjs cli i already did it so let's create our project to create a project run nest new and now we need the project name i'll call it admin i'll add also another flag skip git because when we create files it will colorize them and i don't want that i will pick npm here and let's wait till it's completed now the project is created if we go to the folder and run npm start so we will start our app so it's run and now let's go to localhost port 3000 and we can see here hello world so this is our created netjs app now let's open it with our ide and this is a folder structure here i will remove the spec files because we won't be using tests in our app so we have a controller where we handle a request and this is a service where we get just a message hello world the service usually we will use it to connect with our database and retrieve data from the database the controller we can handle get post request and requests in general and the module is where we rappel together all the controllers all the services and all the other models we will wrap them here main.ts is the main file that we will it will bootstrap our app so right now we are listening to port 3000 but i will change this to port 8000 also i will close the terminal here and i will use it directly here so nest npm run start dev i will use this command because it will check all the logs here so is running now localhost port 3000 won't be available but localhost port 8000 will be so we can see hello world here now let's create a new module so i'll open the terminal now and here i will write just nest so as we can see this is a list of all the things that we can create so we create application class configuration controllers etc so every time you should check here what you want to create so right now i will create a model so nest generate module i'll call it product so i created the module also i updated the app module.ts so every time we create something the nearest module will be updated so if we go to app model now we have the product module here which this one is updated if we go to the product we have product module here which currently we don't have anything i will create now another controller so nest generate controller product so in the folder now we will create the product controller and also product controller spec.ts which i will remove it but now appmodul.ts is not updated but productmodulates is updated so now i have a problem with the styling and in now eslin.js i will remove these lines here because i use different styles and this won't complain anymore neither these so we have the product controller here and this prefix here is the end point so if i get a get request here all i'll name the function return all products this will be available at localhost port 8000 slash product so i forgot to add here get my bed so every time we want to add an endpoint we have to add here a decorator about the method that this function will be available so also don't forget to import cat from netjs common and if i refresh this we can see all products here so this is our first endpoint now let's uh connect with our database before doing that i will change this products product to products and i will add in the main.ts here i will add another option up set global prefix to api this means that every endpoint now that we will create product won't be available anymore now but api slash products it will return this message because now we added the api prefix and now the name now is products now let's migrate and connect to mysql now let's connect to the database go to technics database and you have here the documentation to install type orm and mysql so you can use any database that we you want you can change this mysql to postgres or mongodb so it will work the same and typeorm will be the package that will connect our models with the database tables so i copied and now let's install these packages also i made a connection here with this nest admin which currently is empty so now that we installed everything let's copy this code here and let's go to our app model.ts here i will add it on top so i added the type of ram model also i need to import it so import type or m from net js type or m the type is mysql host localhost port is the same username root password is root root my password you can put your password here database is nest admin and the entities here i will change it to auto load and it is to true don't do on this on the production environment because it will automatically remove the tables and recreate them so this is only for development purpose so it's completed and uh let's see our next js now if this is ever is okay here it means that the connection is successful now let's create a model so product entity dot yes so i'm renamed it wrong so this should be product so let's create a class export class product and this will be an entity so to make this an entity i'll put on the decorator here entity so don't forget also the import entity from typewrim and now let's add the fields for the product we have an id which is a number a title which is a string and we need an image which is a string also and the id with will be a primary key so we will add a decorator here which is primary generated column so this will be a primary key don't forget also to import it this will be a column and the image will be also a column so it's done so not sure why i'm having this highlight error here and now the product is done we still need to migrate it and to do it we have to add it in the product module we will add also an import here we will add now we don't have any tables but we will see it now so we will add here type or a module for feature an array here and we'll add the product and i will see now now it is refreshed and if i refresh this we have product here so the table is created with the columns that we wanted so this is how we create tables and we add migrations with the type rm and if i want to add another column so i will add here likes as a number this column i will add an option for this column default will be zero so these likes will every time we create the product we'll have the default value 0 if we don't set it so if i save this and open the database we can see likes is added automatically and equals zero so this is a default value so typeorm makes a creation of the tables very easy now that we created our product let's create a crowd for this product so first we have to add a new service so i will add here nest generate service product so this will be the connection between our database and our class here so let's remove the spec file and go to the product service so here we will add the constructor and inside we'll add injective repository inside we'll put the product that we have the product entity private read-only product repository and it will be a repository of type product so let me repository the name sounds good okay so this is how we this is a connection that we will make with the database so inside here we'll add all the methods that we need in order to retrieve data so let's i will add the first method which is all and this will be the sync method and to get all the products i will return this product repository find with without anything this is how we retrieve all the products the type will be a promise of product array here so with this let's use it to our productcontroller.es and instead of this we'll let so first let's add in a constructor here private product service product service so the product service that we just created here and now let's use this service we return here this product service all so the function that we just created so with this we will retrieve all the products it will be an empty array but that is our first endpoint first make sure to have postman installed because we will use it right now and the endpoint that we want is http localhost port 8000 slash api slash products and indeed we have an empty array here returned so this is how we get all the products but let's create a product to see if this works correctly so let's add another method here which is create and this will be a post request and we will send in this post request some parameters and we will send it to the product service first in the product service here we'll create we'll add create also here we'll have some data and what we'll do is we'll return this product repository save and inside we'll pass the data so this is how we create products and this will return a promise of product so this is a product creation let's use it here so in order to use it we have to retrieve the data and inside the create function here we'll pass body and we can pass here directly the body so these are the request data that we will send we can do it like this or even better we can do it with every property that we want so we can do it like this title and this is a title property which is a string and i will duplicate this and i want also the image so these are the two properties that we will send so in the request and what we'll do is return this product service create and we will pass the title and the image so the product is created with this let's try to create a product make this a post request and we'll send body we'll send a title i'll just write a random letter and an image let's send this request and we successfully created the product so we can see also likes is by default zero and also the id is one so if we check also all the products now send a request we'll see an array with one pro product inside so the creation works fine now let's retrieve a single product so i'll call the function here get so let's make all of this function asynchronous and this will be a get request but if this stays the same with this get request the first one will be always executed so we need to add an id here and uh to pass id in the product service we have to pass it via params so we need a param which is the id which is a number so this is how we get the params now let's add the method here which is a sync get id is a number this will return a promise of product and we will return this product repository find one and we'll pass here the id so this is how to get a product by id we'll return here this product service get this id so now that we got all the products if we send the request to get with id one we get this product that we just created so this works as well now let's add update so update here this is a put request and we need to pass id also here the id will be a param just like this one we'll pass it here and also we need the body parameters which is a title and an image so let's also add the function here update id is a number and also some data this will return a promise of any return this product repository update an id and the data let's go back return this product service update id and the title and an image so let's try to update the product then so this will become a put request to product slash id 1 and this will change to new and the image will change to new let's send this request and we don't return the product but if we send the request to get the product now the title is new and the image is new so this works the same the last method is delete so let's add it first to the product service delete we need to pass only the id and returns a promise of any return this products repository delete and we need to pass id here let's go back this is a delete request to id and we need to pass ids param here and we have to return this product service delete the id so let's delete the product so here we'll send delete request and it was affected now if we get the product we won't get it so all the products now will return again an empty area so these are the crud functionality for nest js now let's create the main app so i will write here nest new main and i will skip git also so i'll pick npm here and let's wait till it's completed now it's completed i will open it with my ide and this is a project so i'll delete the spec files and i will make some changes now the project will listen to port 8001 what else i will add from the other app is i will set also the global prefix to api also i will enable course so enable course and here we'll add the origin and here we can put our front-end localhost so localhost 4200 is for angular 3000 is for react and 88 is for view i'll put 4200 and i will add here double slash so i'll copy this code and add it also to the other app so now let's focus on the main app also i will delete the styles from eslin here so it doesn't highlight anything wrong now this project will be different than the other project because we will connect it with mongodb so to connect with mogodb let's go to the documentation and here in techniques we have the instructions how to set up so first we need to install these packages i'll install them now so i'll install mongoose and now we need to add this command to our imports in our app module.ts so here we need to add this command let's import also mongoose our endpoint so our url here is localhost port 27017 so make sure to install also locally and the table will be nest main i will also add an option auto create to true so this will add our models to the database directly now we set up the database and the database now doesn't have anything now let's create some documents so let's open the terminal and write nest generate model product we created the product module now we will create the controller and also service so they are here now let's delete the spec files here i will create also a product model and inside here i will export a class product so first we have to specify that this is a schema here and we'll need to import it from mongoose in sj mongoose so this will be different than typo rm and instead of entity now we put a schema here so we need a title as a string image as a string likes as a string also id is a number so let's add here this is a a property so prop here let's we need to import it property from mongoose here and all the fields are properties so we created them now we need to export constant product schema will be equal to schema factory create for class and we'll pass our product here so we need this product schema now and we need to add it to our product model here let's import an array here and inside we'll put mongoose module for feature and inside we'll put an array and i will open curly braces here we need a name which in our case i'll put products and we need a schema which is our product schema so with this let's see if we created anything my bed i forgot to start nest here so npm run start dev so once i start here we will create here a document so we can see nest main here and we here we have products so we don't have anything currently so we created everything manually now let's uh add the product service here we will add a constructor so inside the constructor we'll add inject model we need a name so i'll name it product that name here actually i'll do the same for the name here so in the module i'll use product.name here so just to keep it the same private here read-only product model and this is a model of type product now is complaining because it needs a document so let's go to the product model here and to create document we need first to import document from mongoose and we will export type product document will be equal to product and document so this is what we need right now and we will put it here product document so it's completed now let's add the [Music] all function so let's retrieve all the products and we will return here this product model find exec so this is how we retrieve all the products let's go to the product controller this will be products here let's add a sync all here first let's add the constructor inside we'll inject private product service product service and we will return it here so let's make first get request here and we'll return this product service all so everything is correct let's open postman now instead of api slash products with port 8000 now it's 8001. and as we as we can see we retrieve an empty array now these are two different calls port 8000 and port 8001 now when we create one product uh in the port 8000 we need to create it also to port a thousand now so to the main up and we'll do that via rabbitmq so now our apps will need to communicate with each other and we will use rabbitmq in this case so we will be using microservices so to use microservices first we have to install uh this uh package net js micro services in both of our apps so i will install it here and also the other app i will install it here and uh to listen to events we have to add this uh we have to change the way bootstrap function works so here we are at our main up and now here i will go to our main.ts and this will change so for the moment i'll commend this and i will stop the nest server here i'll create the app so is equal to weight nest factory create microservice and i will pass up module here also and i'll add an options array here so for the options i will not use tcp but i will use rabbitmq so click here for the arabic mq first we have to install also these packages so i'll install them in both of our apps i'll install them here also and this is the rabbit mq configuration that we need so i'll copy it and i will paste it here so let's import transport transport from net js microservices and the options here is transport transport error mq so rabbitmq options we need the urls so we will add it now q i will call this main queue and what we need to do now is up listen and we need our console.log here microservice is listening so that's it now let's add the url to listen to our events and we have to go to cloud ampq amqp.com so this is a rabbit service and we have a free tier here so we will use this free tier i already have an account so i log into my account and what we need here is this ampq url so i will copy this and i will paste it here to the urls and now i can start the server again so now instead of listening to port 8001 we have microservice is listening so this one is listening and the admin amp we need to send events here so let's send events from this other microservice and to do that we have to add the client module so i will copy this so till here and in our product model.ts in the typorem model here i'll paste this client's model so first we need to import here glance model from net.js microservices we need to import also transport and this is not math service now i will call it product service so this is just a name for the url we need to put the same url so i'll copy this url here and for the queue will be the main queue because we will send events from this uh admin microservice and we will receive it in the other microservice so we added the clients module now let's use it to work product controller here we will inject [Music] inject product so it should be uppercase product service private read-only client client proxy from net js microservices so this product service is the same name as uh this products service here and the client proxy we need to import it from net js microservices now let's use this so for the moment i will just keep it very simple i'll emit here so this client we need to emit an event so the events that i will emit is hello and we need to pass some data so the data hello from rabbit mq so this is a message that we will send so this is how we send messages and we are finished with the admin app now we need to listen here in the main app so let's go to the product controller here and here to listen i'll create a function hello and uh to get the event from rabbitmq we have to add here event pattern so let's import event pattern from js microservices and the pattern is hello the pattern is this pattern here when we emit the event and the data will be here so the data will it will be a string and what we will do now is very simple i will just console log the data so let's see microservices listening and this is also working fine now let's open postman and what we will do is just get the products so if we get the products as always let's send the request so nothing has changed but if we see here in our other app hello from rabbitmq so we dispatched an event where is it here and this event we send a message hello from rabbidmq to our admin app and we got it in our main app here and we console log the same message so this is how microservices work with the nest.js and this is how we send data via different microservices so we successfully send the event and everything works fine except we don't have now anymore port 8001 so if we try to go to localhost port 8001 is not available anymore because in our main.ts now we are running the microservice listener so what do we do now how do we serve the port 8001 and also serve the microservice because this cannot run at the same time so the way which i figured out how to do it is by creating another main.ts file so i will copy this file and i will paste it and i will rename it to i will name it listener dot yes so this is a same file now in mind that yes i will remove everything and i will uncomment what was before so now we can run our port 8001 there and this will work right now but how about the other listener so i'll remove the comments now how do we listen also to this file now so the way to do it is by this next cli here we automatically run this next cli and what i will do now is i will copy this also paste it and i will name it listener the json the difference here is this listener and an scli are the same the difference is we will add another option which is entry file and the entry file that we want is the listener so the this means that the source root is source and the entry file is listener so like this we specified the listener now we need to run a command to execute this listener.json so i will add this command in our package.json and in this start dev here i will add here listen and the command that we are trying to run is nest start watch so this is the same as start dev and we have to specify the config file which will be listener the json so this is the difference so we have to specify another config file so with this now in the other terminal tab i will run npm run listen and as we can see now microservice is listening here we don't see that message and if we go to the endpoints localhost 8001 is available and if i go to localhost 8000 api slash product this is also available and we will get the hello from rabbitmq again so this is how we also listen and also expose the port 8001 at the same project with net js now we center an event but this event is not useful anymore let's send events when we create update or delete products so when we create we want to return the the product first we have to get the product which is equal to this also wait here because it's asynchronous and in the end we want to return the product so we didn't change anything now but we have to send this client emit product created and we will send the product here so this is completed now let's do the same i will copy this and i will do it for the product updated so i will add here wait to update the product now we have to find the product which is updated so we have to use it like this so we'll copy this we'll get the product which is equal to wait to get the product with the id i'll copy this again paste it here now is product updated and in the end we'll return the product so this is completed also the last one is deleting the product we will await deleting the product we will also emit product deleted and we'll send id now and what we will return is we can return nothing so this is also acceptable now we finished and we need to handle all the cases in our main app so we have an event pattern the first one is product created and we will get a product here for the moment i will make it any so to create a product first we have to add it here in the product service so we'll add here sync create and to create a product will return new this product model inside i will pass the data here so we need some data and that's it so we have to save this and this will return also promise of product this returns also a promise of product array so let's create a product here we got the product and we will send it like this so this we don't return anything actually we will just call product service create and we will pass here the title which will be product title image and this is likes we need also to pass the id since we have to store the id as well before creating the other events we let's test this one so we will post to products let's create a new product product image and let's create this product send the request this product is created let's see if we got the event actually we didn't maybe we have to run the listener again let's see if port 8001 works actually it works so everything worked we didn't console anything so we have two ids here but it doesn't matter because this id here is needed to query the object because we must store it because everything will be confusing otherwise so title image and likes everything is correct and this is a mongodb unique id so this is created and let's add all the other events so this should be product created here product updated so now we will get also product but first we have to find it and then to update it let's go to the product service and here we'll get find one and to get one product we will wait this product model find one actually we have find by id here so we can put here the id as a number this is actually the idea of so i will use find one and i will pass here the id that we want so we'll get here the product we probably we don't need a weight here so this will return a promise of product and uh now that we got to find one let's let's make also update id is a number and also the data that we want to update promise of any here we will use return this product model find one and update so the first parameter is the id and the second parameter are the data that we want to update so let's use that function in the updated part so update here and the first parameter is the product id and the second parameter are the other fields so i will keep also the id here doesn't matter and let's try to update now so here now the product with id2 i will update the title to new product here and this is a put request and the new product is updated now let's see if we have a new product now so this is updated as well so just like this we update in one place also it's reflected to the other the last one is to delete a product i will copy this product deleted so this is really simple now we have only an ideas number and we need also only to delete so let's add first the function delete id is a number and this returns a promise of void and will return this products model actually won't return anything product model delete one with the ideas this idea so that's it we can use delete here and we'll pass id so let's delete this product here is deleted now a list of the products did we do anything anything wrong so it seems a product was not deleted so that's it that is a problem i forgot to add here product deleted so unfortunately i cannot handle the event now this product will stay here but the idea is this now that we handle events that will update our list of the products now the last functionality that we will make is adding the like functionality liking the product so let's create the function like and this is a post request to the product id slash like and this will have a parameter id as a number so this will be very simple we'll get the product is this product service find one by this id also wait here and in the end we will say product likes plus plus and product well we don't have a save functionality but we will do it like this this product service will return it update the id with the likes so let's remove this so it's very simple but it's not completed because we want to also update it in the admin app and the way to do it we won't do it via rabbitmq events but this time we'll send an http request directly to this microservice so let's implement it first in the app module i will add here http module and in the product controller here i will use the private http service http service and let's also add here in the product controller i will add also another route here which is like and this it will be similar like this and this will have id slash like and we will implement like here but the difference now is that we will get the product not find one but uh the function here is get so we get the product and we will update it like this so is almost the same now we will send the http request to this endpoint and to do it we have to do it like this here we'll send the http request so http service we will post to http localhost port 8000 slash api slash products slash slide d slash like and we'll send an empty data so this is a post request that we will make and in the end we need to subscribe and let's console log the response so that's it this is how we send http requests and let's see if it works first let's create a product i created this product we have likes 0 and we have this product now now i will copy this url paste it here is localhostport8001 and the id is 4 and we will like it because id is for let's send the it's not a get request it should be a post request so this is sent as we can see now in the database likes are increased how about the admin database we can see also here the likes are inclu are increased so this is how we send http requests via one microservice to the other so there are two ways one is by using uh rabbitmq events and the other way for them to communicate is by directly sending http requests so this was the tutorial i hope you liked it and don't forget to like and subscribe for more content thank you
Info
Channel: Scalable Scripts
Views: 32,197
Rating: undefined out of 5
Keywords: microservices, rabbitmq, event driven design, typescript, event driven architecture, javascript, nest.js, nestJS, express, node, node.js
Id: IsubcKdZPyE
Channel Id: undefined
Length: 61min 41sec (3701 seconds)
Published: Thu Jan 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.