Integrate mongodb to a nestjs monorepo using docker containers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello guys welcome to this video where we try to integrate mongodb to an sgs and angular application this video is a follow-up video on how to work using a mono repo with angular for your frontend and sgs on the backend and if you want to see how i set up this project you can consider the previous videos that i have already uploaded and if today is your first time here please don't forget to subscribe and like this video so to get started with mongodb let's head to nexius documentation for mongodb they have very nice documentation outline mean how you can use mongodb with net gs so the first thing we have to do here is to install uh this package mongoose let's do the same thing so the package is being installed and also it's very handy that last time we we made the integration uh to docker that way we can use a docker for for the connection to the to the database so we don't have to install a mongodb on our on our machine we later used the docker compose integration with with mongodb as well uh so first thing let's just wait for this package to be installed then after we can continue following the documentation of nesgus so then the next thing we'll have to do also is uh uh to have to import the mongoose module in app module file so the package has completed to be installed so now let's go to app module and we try to um to import these so we can add it here let's import the package okay this one is done now let's continue by following the the documentation [Music] so now what we have to do is to create a schema uh or add database uh so here we have a cat schema we we would like to create um a schema but we will we call it um it to a to-do schema because uh later on we try to to to to do a to-do app so we try to modify this schema a little bit so now let's go to our project and uh inside our app so here we can create a new a new folder and call it to do and inside this uh this folder you can create another one call it schema and now inside this uh this new folder we can can create a to-do schema maybe let's just copy the schema they have here and then we okay let's go with this here and then we can make some some some changes to it so we call uh document a juju document so we can now leave only the name and remove the other properties we don't need them that much for now okay so now we have a schema so let's continue uh so now they are just explaining what the property decorator means uh and if you want to do more advanced things can also add a reference to another another table so so far so good for us so here we we created a module uh and then they call it cat.module.ts so we also try to do likewise but we call module into the module so here we go we have to do so we try to create to do module.ts so we can paste this one and then we make the changes so we don't have you can move this one and then just change and this i don't yet have the controller and we don't yet have a controller i need that do we have the service yet but we have this schema i should find it okay let me check is the name okay if the name is not correct yeah so normally should find this one okay okay and then yeah is also it to do so now let's continue with the documentation again so here we have a service and that is uh that is created now let's also copy these this service then we create a new file and call it we can paste everything they just make the changes so we haven't yet created this detail so this one will be missing for now and as we're following the document documentation we need to create it so now we we have the a service that is that is done um now the documentation is talking about the additional step that can be that can that can be done for other configuration but uh as for for what we are trying to do uh i guess we are set let me just try to see yes we have everything uh in case we need to inject a particular connection for the database then we will have to to use this this injector for for that connection but that's not our case here okay now we have this uh the dto that we haven't talked about it in the documentation but we can try to create our own uh our own detail is uh just an interface between uh uh application and the in the in the back end and the database so now let's go and create the the dto so we create a new folder and can call it dto just in a normal environment interface testing benefits i said the property to be read only so since we are not going to modify it okay now i guess if we come here we can say that this is a create to do dto should be able to find it okay it's done what this is missing now you need to have a controller and in this controller you can define the oh yeah you don't yet have a controller okay now you can import okay we don't have it cut service but we had a habit to do service and now we need to to define these uh this controller here and i can call it a to-do controller okay i created it to do more during the schema so let's write that movie to try to do folder likewise for the services as well now inside it to do folder i will create it to the controller i mean unfortunately they don't talk about this one in the documentation but this one you can write it you can write them easily so let's create class here as expert class but but you can also [Music] look to the github page and then they also talk about you have a a project that is already uh set up with the the configuration that they've done so if you if you go below you can look to this working example here and then in this working example uh the you have the controller or the cat controller you have uh the dtu as well that is created but they didn't mention it here yes maybe it's for the documentation to be much simpler anyway we can write the controller by ourselves uh yeah just need to mention here that this one is a controller using the controller decorator and here we can create a message create using the body of the request of that created we also need the constructor to call this service we need to cut it to the service here that we can we can use can use it here to create this to create a a to do so this one this dot to do service as it creates yes it creates yeah let's also use a decorator to say that this one will use it on a post request now let's add another not only get request and can call it a fine all metal so seriously also how you find oh yeah so yeah meanwhile is nice because okay uh we have fine then i need to have an think okay now you have created um the methods creates that we call when we when there is a post request and then a final finder that should be called when there is a guess request so there is see some errors here let's try to see you can solve them uh okay now is we have a 2d controller and now you have to do service as well you can import them about missing inputs okay now in this service now we do have some arrows why is this the case uh wait let's just try to remove this one and you can add all the missing info to see if it solves the issue okay it's fine so now what we now need to do uh is to um is to register this controller in uh the to do module is already that done but now we need to to register a to-do module in app module so we now add another import here and let's call it it's not finding these uh okay what is the name we gave to it that's because we call it cut module okay let's let's change the name now if you go back here you should find it easily okay so now you have a a module that is registered in a in the ppp module so now what we now need to do uh for everything to work is not to define um a new um a new service in the in a docker compose file for the free database so now let's add a new service let's call it db we use the mongodb image and the port we use the the default part of mongodb that is the 27 or 17. okay now it seems that everything is uh is okay now let's try to um to run to build uh our containers uh using the compose okay it's telling me that there is an error here okay it's fine so there is a special so i service double search sign so that's perfect so maybe you should only for the first time it might take it might take longer maybe to pull the the the mongodb image from from the repository from a report remote repository so maybe it might take longer but uh now it's fine uh okay but now you have this arrow here is telling us that it's unable to connect to the database okay let's check out why that is the case so what what what url did we use so in this service here the module okay okay in vp module okay we are using a mongodb local host to connect to the database nest but but this local host is wrong because uh in um it is different inside a docker compose network actually here [Music] it should rather be let's go and check again it should be db so let's go again to fp module and make this change uh okay is it pretty directly let's check you just continues seems that is is working properly so we have a database that is launched the service of the database that is launched and then have a service web that is launched for an sgs server okay so now what we try to do is uh try to use uh uh this extension that is called thunderclan i really like this extension because uh right here i can still uh in my in my uh in vs code i can still make app requests i find it to be very practical i don't need to have an and i post an additional postman for for me before i can be able to to test um to test api um so we try to use this extension [Music] and so we try to make two requests we try to make a get request and try to see if we can fetch all the um the to to to-do's that we have in our database currently we don't have any and we try to also do a post request and try to see uh if you can um update a database or add a tool in our database okay us but first thing let's try to see if you have already add this this url to our controller because i think i haven't done it yet okay so let's go to our controller to do controller so here i mean you can define a global name for the whole controller you can call it to do and that way you can make it post on to do and get onto loop and with this one uh now let's try to see if uh our requests uh work so now back to uh iphone that client extension so if you go to the get request [Music] i can do this one directly uh instead of having a json response would you have this uh seems like there is something wrong here okay let me copy this one here it's a get request it's fine i can copy and paste it in the browser as well i returned something wrong is that what i'm expecting actually okay so let's go back to this um if you go to this actually controller again okay we have uh okay the contributed to do control is expecting this to to do and but then if you go to the epp module so you have a middleware consumer and what happens here is that you can go to this this this confront and middleware and if the url doesn't contain api then it goes directly and it sells index.html i mean it's not exactly what you want okay so and just to solve to solve this issue we can just go directly in our to-do controller to do controller and then let's just add uh api here okay see to be enough for the moment okay now if you go to our browser and then if you add the pc here okay yeah that's fine so it's works okay now if you go back here um it means that we can now do post requests uh and an api to do [Music] and and add a body request so you can add the name we don't have anything here so let's add my first rule okay if i send this request and now if i go to the get request and then if i send no this one is to say it's wrong then i can add an api here and here i have my my responses perfect i it means that our connection to the back end uh is working uh as we wanted so everything is is set perfectly uh so this integration uh of mongodb uh you know with an sds backend uh is done so maybe the next thing what we'll try to do next time would be to to create uh a front-end application where we can we can directly make this post request directly from the user interface and also uh being able to query all the to do that we have already in the in the system so we try to to do this user interface maybe in the in the next video that i will try to upload so i hope you guys will you like this video and i can't wait maybe to to see you in in our next video bye
Info
Channel: kedevked
Views: 320
Rating: undefined out of 5
Keywords:
Id: LAXQRHbX2Dk
Channel Id: undefined
Length: 35min 32sec (2132 seconds)
Published: Sat Jul 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.