Build a TypeScript Rest API using Nest.js, Mongo and Docker with @codeSTACKr

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so first of all we can install globally the nestjs CLI nestjs is basically angular for for back end okay in the sense that it has like controllers Services modules you will see a very similar structure we have this basically this uh this command which is Nest Das Das version this is the nest CLI so we can do something very similar when you create a new project so we can do Nest new live nest and this will scaffold you up in a few seconds I don't know if that's true and I'll use mpm because I want just to do something simple seems that has done something so now let's step into this folder for example we can do this here you can see a module a service a controller The Nest Das C.J so something which is familiar let's say if you are familiar with angular I want to install some dependencies in this project because this not all the nest JS projects they have to be connected with ADB so now we are going to install the dependencies which is of course an mpm package to connect with PR that well I hope yes yes this the the the type so you're us not only Nest but so is Nest typescript by default yes okay powerful yes to start we can do just nest and start and uh usually takes a while okay yeah we have this fabulous application so if so but okay this means that at least it's working so I'm happy I can just stop this we are going to create a controller a service a module and a model file typescript file you can do Nest G which stands for Generate control users it has created a users folder with of course doc. ts5 this is this also angular is also this one so we have this empty controller we can create service then we have a module now we can okay so now as you can see we have this controller service and module file so we also need to to create a a simple file this is not is not an SVS but touch is a and so in the source users we call this user. model. okay we have these four files the structures let's start by the way I'm using um GitHub copilot I hope that this can help me there we go yeah if you know what GitHub copilot is doing is great for example here I know that this is correct here we create a creating basically a model just a name email and password and also we can export some interface it looks here we can have I think the same stuff this is this is great just I don't know you but much l typing and then of course mongod will help us with IDs right now we are in the service so we are trying we are building a bottom up so now now the service will use the model Port inject model the model this is this is super nice model and this is just user we don't need to add TS because we are in typescript so we don't need that we have this uh this service this is amazing but let's see let's see that let's try to create the the model maybe a little bit by hand otherwise stream it will be too too too short of course so we have a name an email and a password the interesting thing is this uh this save this this basically will be handled by Mong package okay get all user select that okay nice perfect so in this case we are getting the user model again and we just basically find everything okay so in this demo we will just use this because we have other stuff to do we have to set the DB the docker file and so lazy so let me copy just the import so we have this and the controller will use the service we will use the dependency injection let's import it first of all as you can see here we have this at post and of course this stands for post verb in the HTTP request so I think create maybe we can remove this because I want to yeah I want to creating this in this because I think it's a bit better for the for the example so we can and maybe in this case it's easier to understand what's going on so let me this we get the username I don't know if it was a username or not name perfect so we generate the ID and then we also return this perfect okay so in this case we want to return this when we create the the get we don't want to get do the single get but let's just do a get all I remember the thing literally it writes it by itself this will be a get request and this will be post request if you want to conc concatenate in the URL I think you have to add something here but we will have something at slash users so this will be just a get request to get them all to create something we will make a post request of the same URL the module is basically a single package that you can use and it has some functionalities in this next project in a similar very very similar way as angular you you have a default module which is the one which basically runs your application then you can have other modules inside the project so in this case we will have this users model so we have to import some stuff here we don't use this but we use user schema we need the module we are importing this module this is a module can you see inside the this is what we have stored before so maybe in this module there is not all Al just the the types of typescript but also the the module of mongi itself it has a very similar uh structur so for example we can have the controllers and providers in a s similar way so we have the users controller the users controller and user service it's going well for now let's go in the app. module. so something that we need to remove because this is just an automatic import that I know that this doesn't work so this saved me some head hes during the live stream because it's know why it has added this this mod default but I'm have another module so no I know why because I've added them the controller and the service before writing the module so they have been added in the default one but I don't want this I want to import them as a module for the user model I still have a a bonus right so Mong modu okay now we are at a very important part and I think I'll start to ask your help just I've copied this very long line for people who are used to mongodb Atlas this will sound very familiar and as you can see I have already the my environment variable have a user a password and a DB okay just so now it's your turn to jump on stage and help me there you go well there we go so can you explain a bit what is DB Atlas how it work Atlas is the the cloud database platform so instead of installing mongodb locally and having to manage it and manage upgrading it and security and all that sort of stuff on your own server can use the cloud uh platform and let us manage it all for you it's so much easier it's completely free uh free forever you just sign up and and get a databas basically just connect to it with the connection string which will show you how to how to get to that connection string this is a very clean interface I like that for this demo I'll just start with this one which is this this free one starting with free so I can create this instance and then it's uh multicloud so you you can pick between whichever Cloud you're you're uh you're used to at w Google Cloud Azure uh and then you can pick whichever region is closest to you and your users I like highand so I I take the Highland one usually I use frankurt we have this free theer cluster the additional settings cluster name I don't have much fantasy so it would be cluster Z default yeah yeah it's uh it's better can you explain the difference between this local and Cloud environment oh yeah so it depends on like where your deployment uh that you're trying to access is deployed you know like so if you're just coding locally versus if you have something deployed on the cloud what you're trying to connect to and that's that's the difference there nice perfect okay so let's close it can you tell us how can we connect to this uh Cloud DB yeah the atlas Cloud database platform so first of all when mongodb is set up it's completely locked down you can't access it from anywhere so that that was the first things that we just did there we created our database user to access it and we created our network access so that's why it was asking local versus Cloud uh so it's basically asking where are you connecting from and right now we're connecting locally so it was using your IP address your actual IP and so now only you or or computers on your network can access this database and only and not only that but you also have to have the username and password so if anyone on the live stream somehow saw your username and password they still wouldn't be able to access it because they're not connecting from your network super secure and then the only other thing that you need after that is your connection string which you can get by clicking on that connect button there now but I think I think we can show the connection scen yeah you can yeah it's not going to show the password in there nice so now you have different ways of connecting you can connect through the shell uh you can connect through your application or through mongod Tob Compass so if you go ahead and click on the connect your application one in the middle there then it's going to show the connection string uh which you can then copy into that's what you just showed there the the mongodb plus SRV and then that whole string there has your username password and the database you want to connect to yeah so right there browse collection click the class yes if I click the cluster here in the collection I want to show that here we have the bases zero collections zero because we still have not connected our application with this DB so I want to show that later if I don't do any any typos we will have something different here let's kick in with the docker part and I'm not I'm not hiding that this is my favorite part how do we containerize this application and why for example do we need do we want to use do use Docker for this application so in this case we are we are not using um an image from Docker because this Atlas service it's already deployed on on the cloud so we we need something super simple and this makes my life easier of course so we have this application locally but we have done a couple of things so for example in this case we have installed the nest CLI and we need no installed on this machine we are going to create three files this three files are the first file is called Docker ignore let me create the files and then I'll explain everything of course just if you want to ask me anything yeah know this is the fun part because I I love to learn and so and I don't know much about this so this is why I invited you so uh so we have three files Docker ignore a Docker file and a Docker Das compose yml file the docker ignore file if you know Git it works in a very very similar way as a git ignore so if you know git and git ignore you know that what is written here it will not be copied for example when you push on GitHub right so the doore works in a very similar way we can write folders and files that we don't want in the image for example just mod modules for now we will use a Docker file to build an image Docker file with no extension Docker file not no extens exension with capital D of course you can use a different name but this is the default file the docker already expects so you don't have to define the name of the docker file okay so we start from the good thing is that you don't have to start from scratch so you can start from B image this open something and dockerhub is a very similar as GitHub but it stores Docker images instead of GitHub repositories for example the the node team has created this the node image is very very well done on doab it also has support for different architectures now here we'll do you can do some stuff and this is interesting because kabell is suggesting something that we are not going to use but we can for example in production here for example we do something that you usually don't see because you don't code for onest GU application so exactly so you've got to get all of these things installed on the in the image first before you can actually start using them okay exactly so I don't have to remember because let's say that I want to run this project on another machine but that don't have Nest don't have the nest CLI I'll install the nest CLI another important command for the docker file is the copy command the copy command copies unsurprisingly folders and files from here the local system inside the image file system so we are basically copying these files inside the the image system maybe can say Okay so let's copy everything but there is a a better way to do this we can copy just the package.json dot by the way is it means inside the FES so this is the source this is the destination but in this case we do this the Run mpm install inside the image of system so before copying everything else copying the rest of the code we copy just the package ofj and then we'll do a around mpm install so this of course depends on the language on the technology for the JS uh it's very straightforward you're getting your base set up and then now now you're adding your your custom files yes now we can do this and now this this means super simple but it means copy everything inside the image F system if we do this without the docker ignore we will copy our local node modules inside the image for system and this is not something that we want to do but for example maybe we won't still have these not modules in our folders then and expose port and by the way just tell me this not not many people will tell you this this is not really mandatory so even without this it will work anyway this is more to inform that we will use this port but uh we are going to we will see where we will really open open the port internally and externally Docker uses something called networks to make the containers communicate each other okay okay we isolate them from the host machine and now something which is called CMD which stands for command this is the default command that we want to use when we run a container we'll basically do what I was doing before on the CLI so next ah okay this can be replaced we can do different stuff but this is if I do Docker run it will be the same as doing Nest start and this is very important because this can be really can really depend on the type of the application that we are about to run if this is a node application probably it's like node in this JS if have a flask we have flask don't know start I can't remember even the run this is basically makes the application agnostic so I do just Dock and run I not use Dock and run but something similar instead of uh running by command line the the commands to build the image and run the image we will just do a very fast forward uh here and we will create Docker compos so Docker compos it's mainly two things either is a CLI tool written in go now and it also has something called Docker compos file this will make our life easier makes the creation of the services more convenient so usually you use a Docker compos when you have multiple Services instead here we have just have one one service here we can have like three four five Services it depends if your front end back end in this case since the service the mongodb atlas it's on the cloud we will just have one service which is okay for this application so this will allow you to have like multiple images but all at the same time basically you will it allows you to yes more or less yes them all up at the same time yeah yes in the doer compost they'll call the services just to make a little bit of confusion okay but you you will have yes we'll have one one service which is basically a container based on the image container name is to define the name of the container if you don't Define a name of the container that will get that a random name but we we want always the same name image is the image defined for the container so let's change a little bit just to don't make it much since we have defined this image but this is not an official image on Docker Hub this is something that we we will have on our local machine we need to define something which is called build and here it we have a context and a Docker file the context is basically the folder the docker compos will look for the docker file it's like this I would have called this path the docker file is the name of the file so in this case we can also remove this but I leave this like it is and here we have to define the ports we want to show uh the interface between the external port and internal port because the containers they communicate in the same networks but of course they have to communicate on the outside in some ways so in this case we'll just have report I think it was at 3,000 externally and internally but for example I can also use a different port inside and maybe use a different port outside so in this case we just use the same port but we need we need to open a port otherwise we the port the service will not be visible from the outside I'm writing something that I don't really need to write but I'll do this anyway because by default the docker compost file it looks for a do M file in this folder but I want to be very explicit here so I'm doing this which is basically the default but as you can see here we have the user the password and the Atlas DB let me show that we don't have any images here okay so now a very interesting command is Docker compose build we can skip this but just to have an idea so if we do this Docker compose build we'll check the docker compos if it if some some images we just have one if some Services have the build key and then it will build the image this is by the this is done by the way by default if you do do compos app but I want to show that in this way we are showing the this step and then it's easy once you get the idea it's easy to just skip that also with another command we also have an error of course check ah there we go okay one little typo yeah so that makes sense yeah I've always just used Docker compose up makes sense like you build it that's like the steps build the docker compose up does so many things that if you have if you have never done this you don't you don't know what's going on for example here we have this error and I'm sure that it's about the build and not the the run of the container do compos up it checks if it has the image if it doesn't have the image it builds that okay so it seems that has done something now let's do Docker images again this one is this one so it has is this image this one on the line seven repository and tag is basically this image value this is the image ID when it has been created the sdes okay nothing is running on our machine if we do Docker PS which stands for all we don't have any container so now what we can do is we run a container based on this image the good thing is that we we will not use Docker run but since we have Docker compos and we already have this image we just have to do Docker compose up so that checks to see if it has an image it's going to already find it and so then it's going to start loading it I see good good things I'm still not sure but let's see so let's try Docker PS sh again I want to show you a little command just to have an idea of containers and images so now I am on my machine and if I do node dash dash version my version of node is 16 now I do this command EXA which is to to execute a different command for a container so if I do stop so instead of using this Nest start I want to do something in different for example bash this will execute a terminal for the container no no I need to add D so I need to make this interactive and open the terminal gotcha the options can you see here we have this root this is the ID of the container and if I do s here we are inside the container and check this if I do node version 14 version 14 because we are inside the container now so you're you're basically on another computer exactly virtually yeah if you are not familiar with containers but we have a uh admin background we was basically sshing inside the container I do this in quotes but just to have an idea of what you can do nice okay so this was just a little bit more just to have an idea now let's go let's do the final test yeah we need a so first of all first of all we are again here on the cluster zero let's refresh nice and here you see now we have a collection this is a database and this is already the this is a collection okay collection yep okay so we have we have this so creates a schema uh and that's that's really on your application side right now in mongodb there's technically no schema set up um so you're you're setting up those boundaries within your application so right now there's nothing in the in the collection there's no data there so there's really no no way to see anything right now I want to just do a couple of things I want to populate this collection and then I want to get them back let's try a new a new request this is Postman yeah so uh let's try Local Host because we are still accessing our Local Host let's do here yes can you see here first of all we don't get an error so this is a good thing yeah so we are getting an empty array which is correct because there are no users in our no SQL DB to create a user we can just do Post request I forgot the format so let me go back on the application here name email there we go okay I want to do the safe the safe one okay so we have a name got to format it properly with all the quotation marks let's use your your name uh your email I don't know this one but the way there is no anyil validation here it will accept any string at this point I don't know if you have I don't know if you have this one that will be ni and and of course you should store an encrypted password in your DB and I hope this is not your your password of your your for your credit card let's send nice and here we see something which seems that it's working that we have this object has an em name email password and other stuff it's just this underscore ID which is very useful for hongo database let me add also me here so so if you don't specify an an ID then mongodb will automatically uh create an object ID it's Json but it's technically bon bon yeah so it creates an an actual object which is called object ID um in in in bson for you have a more secure password than you so also the seven there okay if you try to get all the users again I will have these two users so this is already looks fine to me let's also check this on the Let me refresh here here we have the two entries
Info
Channel: Francesco Ciulla
Views: 17,029
Rating: undefined out of 5
Keywords: programming, software, coding, mongodb tutorial, mongo db, mongodb course, nestjs tutorial, nestjs crash course, nestjs microservices, rest api tutorial, typescript
Id: qFnYYqCKDow
Channel Id: undefined
Length: 27min 18sec (1638 seconds)
Published: Sat Mar 30 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.