Build Complete REST API in NestJs #1 - with TypeScript & MongoDB

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so welcome back to this channel so in this video we will see that how we can basically create our API in nest.js so if you don't know Nas Json or JSF stream we're going to help us to uh build scalable and fast apis okay so in this video we will see that how we can create a API from scratch we will add all the uh routes get route post route delete and put route and then moving on next we will add the error handling authentication unit testing e to e testing everything in this video series but in this video we will discuss that how we can create a simple rest API in last JS okay so this is the official method of Nas JS I simply go to documentation so here you can see that this is Introduction you can read that and here we have to install uh nest.js CLI globally so that we can create our Nest project with the uh Nest okay so simply copy it from here Nest install Dash G which will install nest.js globally which is the CLI I will go back to my vs code create a new terminal here and you can see that I am in my library Nest API folder okay so I will simply paste it here and install the nest CLI globally so I get here some permission errors so I add here sudo so I type here my password and this is for Mac only okay so now an SDS is installed globally here now we have to create a new Nest project and to create a new Nest project we will use the nest that we have installed globally if I go back you can see that here we just have to type here Nest then the new then the project name so simply go back and type here nest new then I am in my library Nest API folder so I type here Dot and Dot will give this current directory name to this project you can also type here name manually if you want I press here enter now it will create a new Nest project I type here npm and now it will install all the required packages okay so our project is getting successful here you can see that if I clear my terminal and let me explain the project structure first of all and the folder structure of nest.js so first of all we have bunch of conversation files for a typescript we know that in Nash JS we use typescript most of the time then we have here package.json there contains all the uh scripts that we can run here for the test for the development and for the production and for the build and we will discuss that later then we have our dependencies Dev dependencies and all that stuff that we have in our package.json file and next we have here our main folder which is the source folder but also we have here a test folder in which we usually write our e to e test and we will discuss about e to e test later in this video series okay so our main marking folder here is the source so in the source we have all our project files in this case we only have few files first of all the main.ts which is the entry point of this application so in this file we have a function called bootstrap in which we will simply use Nest Factory that will use this create function will pass the app module which is the root module present right here and it will listen on Port 3000 and we will call that function and in this way the project will start so this is the entry point of nest.js and moving on next we have here a controller file a module file and a service file and this is a testing file so I will delete it now because we will discuss about testing in our future videos okay so first of all here we have our app.controller.ts so in this file we will handle all our incoming requests and also we will send back the response to the user from this file okay so in this file you can say that we will handle all our routes like get posts another and then we have our app dot module.ts so Nest uses the modules to structure the project for example this is the app module so in one module we have three files one is the module file which one is the controller file and one is the service file so the the main file which is the module file contains uh you can say that the configurations of that module like we can import here uh different modules and then we can Define it our controllers providers so this way this is a main file which is the app dot module.ts which is the main module file and then we have the app.service.ts now in this file we write our logic we category data from the database we save data in the database and we will handle all the logic in our service file now in this project we have two modules one is the book module and one is the auth module in this video we will only discuss about the book module and we will learn about the authentication in our upcoming videos so I close all these three files now I have to create my own module which is the book module and to create a module isonotype here nest G module and then the name of the module in this case the name of the module is book so I create a new module here you can see that a book module is created in the source here that contains only one file that is book dot module.ts which is empty right now and also you can see that the app.module.ts is updated if I go to that file you can see that in the Imports the book module is automatically added so we have to add each module in our main app dot module.ts file like this okay so if you use Nest it will automatically do that for you and now we have created our module now we have to create the controller and also the service file for this book module and to create that diesel type here Nest G uh controller and then the name of the module which is the book so you can see that we have here our two files one is book.controller.ts and one is the book dot controller.spec.ts which is the testing file and again we will discuss about unit testing and E to e testing in our upcoming videos so I delete this file in this video and we will create this file again in our upcoming videos and now we have to create our service file and for that I use here Nest G service name of the module which is the book now I don't want to create the testing files or types here dash dash no dash spec now it will not create the testing file and you can see that this service file is also created right here so now we have successfully created our book module we have created our controller file service file and now we have two first of all connect to our database uh before writing up a code so for that I close this file in the root I create a new file that is dot EnV file in that I have to write my database connection string you can also provide here the online Atlas URL but I will use local database I can load this EnV file now we have to install some modules first of all I type here npmi Mongoose which is the plugin for mongodb then we have to install here at nest.js slash mongoose then we have to install here at nest.js slash config Dash the save so all packages are installed successfully now let's connect our database now first of all you have to add this uh EnV file in our project so I simply go to the app.module.ts which is main file and here I have to first of all configure my EnV file and for that I will have to write the code in this Imports array so first of all I have to type here config module that I have installed from nest.js slash config then I use here dot for root now in that I have to pass multiple options first of all the name of the file or the path of the file I type here EnV file path and that is present in the root that is dot EnV then I add one more option here that is the is global so that you can use the environment variables globally okay so in this way you can add the config file in your nest.js project and now we have added the EnV file now we want to connect to our database so again we are using the mongodb in this video if you want me to make another video with other databases like postgres I will also do that if you want so now let's type here Mongoose module and I have to import the Mongoose module from the nas JS Mongoose dot for root and then here I have to pass the URI so now I have my URI in the EnV file so process dot env.tb underscore URI like this now to run my project I simply type here npm run start and I type here Dev that will start the project in the development mode so I press here enter um let me see you can see that we have zero errors here if I change anything here like I type here I remove this B from here Simply Save It and now if I restart my server you can see that we will get here the error unable to connect to database so this minute our successfully connecting to our database okay all right so now we are connected to our database now let's create our schema for our book module so simply go to the source The Source in the book I create a new folder that is called schemas in that I create a new file that is called book dot schema dot TS Now to create a schema we have to use the schema decorator so I have to import the schema from the nest.js slash Mongoose now on the schema I will pass here one option that is timestamps that will I will set to true now it will automatically create the timestamps for each document in the database okay now I type here export a class now to create a schema we have to create a class with that name so in this case the name of the class is going to be book and we will also export that here okay now we have to type all the fields that we want in our schema first of all we need here the title and the title is going to be string and now you can um sorry you can type here one more that is ADD prop okay so on the probe you can pass multiple options you can type that uh the type is going to be string required is true and other configurations for that particular field but I will leave it empty right now and after that I will simply copy it from here I will add here title then the description of that book okay and then I have here the author of that book and then we have here the price now price is going to be number and then I also want more field I want to add one more field here that is category okay so now I have to give the type to this category and the type is going to be an enum and I will create an enum here so export enum that is going to be category in that I will pass all the categories so I simply copy that just to save some time so these are all the categories that I have in my category enum Adventure Classics crime and fantasy okay so I will give this category as a type to this category like this okay and also in the I type here um add probs if you want to pass multiple options okay you can also see the documentation if you want to learn more about the prop Simply Save it and now I have successfully created my book schema now you also have to export that book schema so as I type here export const book schema is going to be schema Factory now you have to import the schema Factory from last year slash Mongoose and I have to type here create from fourth class in that I have to type the classroom that is the book so now it will create our book schema in this way Simply Save this one and now let's close this file and go to our book Dot service.ts and then here we have to inject our model so that we can use that and get the data from the database and for what I use here Constructor and in the Constructor um I will first of all use here inject model decorate oops that is inject model from Nast JS Mongoose nothing that I have to type the name of the model so the name of the model is going to be book I will import that from schema and type here dot name okay and then here I will create a private book model and I give here type which is the mongoose dot model and the model is going to be book okay net I have here the body of the Constructor and I have to import all as Mongols from the Mongoose so by default the book service is a class and that is decorated with the injectable decorator and the Constructor we have to inject our model so I use here inject model decorator from the nas JS Mongoose pass and the name of the model then create a private book model variable and the type is going to be this okay so now we have a book model now we can access all the functions like find find all of the mongos in this file so first of all let's create a function async I type here find all okay and now in that I will simply type here cons books is going to be a weight this dot bookmodel dot find and I simply have to return from here the books okay that is going to be written and I will give here a type now we know that it will have done a promise so I type here promise and I type here that it is going to be book array of book okay so in our service file we have successful created our first function that is find all and we will simply uh use this uh find Mongols function we will call our this dot bookmodel dot find that will get all the books from the database and then we will simply return the books from here and we have some error here that is bulk modules are nested Nest JS module okay so we have this error here and to avoid that I simply go to my book Dot module.ts and then here I will add the Imports because we have to also add our model here that we are using so we know that Imports is an array I will use here Mongoose module called the dot for feature now in that I have to pass the array of models so I type here array and the first model is going to be name that is going to be book okay then you have to type here the schema that is going to be book schema that we have to import from the book schema as well okay so now the error will go okay make sure to register your model in the main module file in this case that is book dot module.ts like this okay so now if I go to my Postman create a new tab here let me close this it is running on Port 3000 so I type here localhost Port 3000 slash book if I click on send you will see that we get here cannot get slash book and I simply go back actually um and the controllers or we also have to actually create the controller function for this so I go to my book Dot controller.ts and then here I will create my Constructor now we have to inject our book service here so in the Constructor I will type here private foreign book service and the type is going to be book service that we have here in the same folder okay and then we can simply create here our first route that is the get so user add get dankulator and we have to import the get from the nest.js slash common okay so I use here get decorator and type here my first function that is async get all books okay and it will return promise book array of books and then I will simply type here return this dot bookservice dot find all and it will call the find all and then the find all will call this part and get all the books from the database and return those books and we have some error here uh return type of public method from export plus has been is using private name book and we have to import here the book as well like this Simply Save it and now if I go back my post manual click on send you will see that we get here an empty array because we have nothing in the database right now and you can also change this route if you want if I go back I can also change it out from here I type here s now it will be books so if we go back to postman type in s now it will give me the empty array okay so now let's create our second route that will create the book and save that in the database so I go back in the service file and then here I type here async create and in that we have to pass the book that we want to create and type is also going to be book okay now it will return promise that is going to be book and now in this we have to first of all type here cons res is equal to await this dot bookmodel dot create and the create we will pass the book uh that we want to create okay [Music] address and I will simply return from here the res okay so now we will pass the book in this function we will get the book from the body of the request then you will pass the book in the create function which is the Mongoose function that will create and return back that book Simply Save this one now let's go back to our controller and now type here the post route we will also import the post decorator from nasda slash common async then I type here create book and that will return promise that is going to be booked and now you can also change your route if you want so in this case the route is going to be slash books if you want to Define some separate route for this that is going to be slash book you can also type here like new so this is going to be slash books Slash new okay so in this way you can also change the route if you want but I will keep it as it is and now we want to Simply return this dot bookservice dot create and now in the create we know that we have to pass here the book and we will get our books from the body of the request and to get the body I type here in the create book I use here the body decorator and I also have to import the body from the national common and from the body I will get the body of that request so I type here the board is going to be book and we have to also type here the book okay simply say up this one and now let's go back to our Postman and create here a new route I quickly do that now in the body I have to type here row Json let me put here a dummy data so now if I click on send from here you will see that we get here our book that contains the all the details and also the ID and the timestamps created and updated ad okay so this minute our book is created successfully if I go back and click on send you will see that we get back here array of all the books okay so now I simply go back to my vs core now here we have simply type the book but we also have to type here the type of that book network type of data we are expecting from the user okay so for that we use something called dto now dto is called data transfer object we don't add user will pass some type of data in the body so I have to also make sure that data has a correct type and and for that I will create a new dto and create a dto I go in the book create a new folder here that is called dto and the dto I create a new file that is create Dash book Dot dto.ts and to create a dto I simply have to type here export I have to create a class here with the name create book dto and in that I have to type all the fields that I am expecting uh from the user so that is going to be read only I type here title that is going to be string okay and then I have here the description category and the type is going to be the category e number that we have created in our schema so I will import that from there okay so now I have created my create book video so I will simply go back to my controller and I simply type here the type that is going to be create book dto okay and I have here that create book dto is not assignable to parameter book property author is missing from create book dto so the create book um I have to also type here the author of the book so let's say this one and now the error will go and now we can simply create our book okay so now let's get our book by ID so I simply go to my book.service.ts I will simply copy this function from here just to save some time and that is going to be a find by ID and in that we have to pass here the ID of that book so ID that is going to be string okay and it will return a book and I type here this dot bookmodel dot find by ID and I pass in here the ID of that book then I will return that book from here okay and I will call it book Simply Save it now go back to my controller and I simply copy this from here okay and I have to call here find by ID and I have to pass in here the ID of that book and I have to get the ID from the parameters of that URL or the endpoint so I simply first of all remove this array because it will return only one book and I call it uh get book okay and I have to get the parameter from the ID so I type here add decorator at Prime now you also get the param from last year slash common and the pram is going to be the ID okay so I store it in the ID and the type is going to be string and also we have to Define our route for that that is going to be slash books then slash the ID of that book so in the get I consider type here colon ID so now it will be slash book slash the ID of that book okay Simply Save this one and now if I go back to my Postman click on send here we have this book okay I will get it by ID so I simply copied from here put here and I have to put here the ID of that book so I type here this ID and click on send now I will get the book by ID okay I also have to check that if I type here wrong guard like this um it will throw uh nothing here so you have to also check that so I go back and I got in the service.ts and then here I checked that if notebook then I will simply throw new knot found exception and I will import the note found exception from nashja slash common in that I will simply pass the message that book not found so let's get this one and now if I go back and click on here send you will see that we get here a proper error message 404 book not found okay and this is also 404. so now we are successfully getting our single book by ID and now let's update the book by ID so I simply go back and I go in my controller or sorry in the service I will simply um copy this function from here okay and I will simply type here uh update by ID and I will pass here the ID of the book and I have also passed the book okay so book and I will remove this one also from here and I will remove this as well and I will simply return from here um uh await this dot bookmodel dot find by ID and update in which I will pass the ID of the book and then I have to pass the book itself that I want to update so type here book and then I pass in here new to true and the Run validators so now we'll Simply Save this one and I um also have to type here comma okay so please save this one now I go back to my book controllers and I simply uh copy this from here and I have to uh type here put because this is a put request I want to update the book and I have to get the ID of the book from the pram so I type here ID and also I copy this from here and put that here so I will get the pram from the ID and get the book uh from the body and now uh we have to type create one more video here that is update book dto so I simply copy all from here create a new file in a video that is update dto.ts so update Dash D update book dot dto dot TS and I put that here and I change it to update okay I simply save it from here and I go back and I simply type here update and I use here the update video so update book dto and I pass in here dot update by ID I pass in here the ID of the book then I pass here the book okay I simply save it and now if we go back to my Postman I copied from here create a new tab that is a put request I go to row Json then I pass in here I want to update the title so I type here Book 2 like this okay now if I click on send from here you see that we get here nothing um I think so the book is present so book not found I type here one so this is the correct idea copy this from here put that here if I click on send from here you can see that the book is successfully updated here okay okay so now if I go back and now we have successfully updated our book by ID and you may got a question in mind that uh why we have really two separate details for create book and update book because both contains the same code because in upcoming videos we are going to add the validations in our details so we have different validation for create book and different validation for the update book okay so in that video it will make more sense that why we have two different dtos for the book okay right now it has same code but in our upcoming videos we will add the validations different validations in both files okay so I close these files from here now let's handle our last endpoint which is to delete a book I simply go here in the service copy this from here and I type here delete by ID remove this book from here and I also remove this and this is the uh find by ID and delete okay passing the ID and simply save it from here go back to the controller and I copy this from here okay and that is a delete decorator okay so I will import that from uh Nest here slash common and then I will type here delete book call here delete by ID Simply Save this one now if I go back to the postman and I create a new book here that is this one click on send so I will copy this ID I paste it here so this is the book that we want to delete I copy the link from here put that here delete click on send and now the book is deleted successfully here you can see that book not found okay so now we have successfully created our API that performs all the grad operations we have created our schema we have added our dtos and also we have learned about the folder structure of Nast JS so in my next video I will add the pagination and the search functionality in this API okay so make sure to subscribe to this channel to get the updates of the upcoming videos and also if you want to check out my udemy course of nest.js which is an in-depth course about how to make a powerful API in sjs you can also find link of that course in the description of this video okay again if you have any question post that question in the comment section I will definitely reply there so I will see you in the next lecture
Info
Channel: Coding With Abbas
Views: 14,670
Rating: undefined out of 5
Keywords: nestjs, nestjs crud typeorm, nestjs tutorial, nestjs microservices, nestjs graphql, nestjs with mongodb, nestjs crash course, nestjs authentication, nestjs project, nestjs course, nestjs prisma, nestjs jwt authentication, nestjs testing, nestjs in hindi, nestjs typeorm, crud
Id: 4oLUddZVL80
Channel Id: undefined
Length: 31min 5sec (1865 seconds)
Published: Tue Jan 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.