NestJS Crash Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] this video is sponsored by dev Mountain if you're interested in learning web development iOS or UX design dev Mountain is a 12-week design and development boot camp intended to get you a full-time position in the industry to learn more visit dev mountain comm or click the link in the description below hey what's going on guys welcome to mine SJ s crash course so I've had a bunch of requests to do a video on nest I recently got into it I really liked it I think there's a bunch of great features and it can it can benefit a lot of people building full stack applications or building api's so what we're gonna do is talk a little bit about what it is and then jump in and create a REST API and we're also going to hook up a MongoDB database and we're gonna use Mongoose which interacts really well with nest Jay s so nest is a as you can see a progressive nodejs framework for building efficient reliable and scalable server-side applications so basically it sits on top of express expresses a very very minimalistic framework which is great by itself however it doesn't give you much structure so nest is a framework to use if you want to have a lot of structure on your on your back-end the syntax the structure it's very similar to angular which is a front-end framework so it uses typescript it uses services it uses dependency injection in the same way that angular does it uses modules controllers you can generate controllers on the fly using the command line interface so it's really powerful and like I said just gives you a lot of structure to your server side so we're gonna build a REST API we're gonna use postman to make our requests so if you're gonna follow along you may want to download that will be using MongoDB Atlas for our database so I've already logged in and created a cluster so if I click connect and then connect to your application it'll give me my string here that I can use later on alright so let's go ahead and jump in now if we go to the documentation here which is it's pretty good actually there's a lot of it's pretty clear there's a lot of good examples what we need to do first is and to all the CLI globally so that we can generate a project and with that CLI we can we can generate controllers and providers or services stuff like that so let's go to a terminal here and of course you have to have no js' installed which gives you NPM and we're gonna say npm install - g and then it's gonna be at nest j s slash CLI okay i already have it installed so I'm not gonna run it but that's what you would do to install it alright and then once you have that installed you just run nest actually you can just make sure you have it installed with - - version and then you can just do nest new and then your project so I'm just gonna call this nest call it nest REST API okay so it's gonna scaffold up our project it's going to ask us which package manager we want to use I'm going to use NPM you can use yarn if you want and if you've used angular at all you can already tell by looking at these files that it looks very very similar even as the app dot module dot TS and all that stuff alright so let's go ahead and CD into nest rest API and I'm just gonna go ahead and open up vs code alright so let's take a look let's close that up let's take a look over here at the files if we look at our package Jason you can see that this is a pretty big file as far as dependencies we have some of the core framework dependencies like Ness J s common SJ s core as I said it sits on top of Express so we have this platform Express reactive extensions so we can use things like observables for dev dependencies we have a bunch of types for Express gest which is a testing platform node Mon which will allow us to run our server and and not have to keep restarting it'll just constantly watch it and it even comes with pretty air it comes with obviously typescript TS lint all that stuff and if we look at our scripts up here we have a start script we have start dev which will run no come on that's what we'll be using a debug script production stuff for testing down here end-to-end testing using just a build script which uses the typescript compiler to build you build out your files format which uses prettier so lots of lots of stuff in here then obviously I'm we're just scratching the surface this is a crash course so we're not gonna get in depth with every single thing that's available so let's close that up and the rest of the stuff in the root is basically just config so we have our typescript config file we have a node maaan config we have our basic CLI config so that's pretty much it for the route so let's jump into the source folder which is where we create everything and notice that we have all typescript files so everything's going to be dot TS and if you look at main TS this is the entry point okay so we have our bootstrap function notice it's using a sync await so we use a lot of a sync await with nest and this is just going to start the server on port 3000 okay and just calls that bootstrap function so that's the entry point and then we have our app dot module TS which is kind of like a meeting place for our any controllers or services that we create just like angular if you're familiar with angular a lot of this stuff is gonna look really really familiar and you're already gonna basically know how it works now if we generate a controller or service with the CLI it'll automatically get put in here it will get imported and then it will get put in controllers or providers okay so that's apt module TS now we also have app dot controller TS and app dot service TS but we're not going to really deal with these because we're we have a resource where we're dealing with which is going to be items will have an items folder that will have an items controller an item service module all that stuff will be encapsulated so we don't we're not really going to use these files here ok so let's go ahead and generate a controller so I'm going to open up my terminal I'm going to use the vs code integrated terminal and I'm gonna run nest G for Gen a controller and I want a controller called items ok so we're just dealing with items which will have a name a description and a quantity now if we look in the source folder it created a folder for us called items and anything that we create now that has to do with items is going to go in this folder and it created a controller file so items dot control a TS and then a spec file which is for testing but we're not going to deal with that so the controller file here you can see it's just importing controller from Ness J's common when you see this @ symbol here this is a decorator you're gonna see these all over the place when you're using typescript they're basically used for our metadata and stuff like that so this just has the name of our control or items and then we have our class here and we'll put out we'll create functions in this class now to run the server we can go down here we could do NPM start however that that's it's not going to constantly watch the server and it won't update our changes so what we want to do is run NPM start I'm sorry NPM run start : dev and that will use node maan to constantly watch the server and update it now if I go to postman I can make a get request to HTTP I could do this in the browser as well because it's a get request but if I just go to localhost 3000 I get hello world back so that's just the default now what we want to do is have all of our functionality with items we want to be able to go to slash items right now we're getting a 404 not found now in order to create an endpoint we use decorators okay so we don't have like a like a routes file or anything like that it'll just know automatically so if I go into my class here actually first thing I want to do is I want to import from nest Jas common get okay and then I'm gonna create a get decorator by doing at get parentheses and then right under that I want to create a function which I'm going to call find all because that's what I want this to do is find all and for now I'm going to return a string that says get all items now I'm using typescript okay or I want to use typescript so this function as you can see is returning a string so I'm gonna put colon string and if you use typescript before if you've used angular then this should look very familiar so and if I change this to let's say a number I'm gonna get this linting error right here it says type 1 is not assignable to type string in that case I would have to do number like that all right but I'm returning a string so I'm gonna put that back and save and automatically since I use this get right here it's gonna tell nest to create an endpoint for slash items so now if we go back to postman and I send again we get this get all items okay so we didn't have to manually create this endpoint it just knows now let's say we want to handle a post request to create a new item so what we could do is up here we could bring in post while we're at it let's just bring input and delete as well and we'll go down here and let's do at post and underneath that let's create a function called creates okay now this creates for now I'm just gonna have return a string and let's just for now overturn creates what are we using items create item so just by doing that I should have the endpoint whoops up in here okay so that's the get I'm gonna open up a new tab make a post request to just copy this so localhost 3000 slash items and we get create item now usually when you send a post requests you're you're actually creating something in this case an item so we need to send data and in order to do that in nest we need to create what's called the DTO a data transfer object and if we go to the documentation here we go to controllers and let's see I'm just gonna search for DTO right here so request payloads so if you use typescript we need to determine the data data transfer object schema so a dto is an object that defines how the data will be sent over the network so we need to create a detail like this it's a class with some fields so I'm gonna go into my items folder here and I'm gonna actually create a folder inside they're called DTO and inside dto I'm going to create a file called create - item dot DTO dot ts okay everything is going to be typescript and we're gonna just export a class called create item dto and we're gonna mark these as read-only because we shouldn't be able to directly modify these so we're gonna say read-only name and then the type is gonna be a string okay we put a semicolon and then read-only let's do description which will be a string and let's do read-only quantity so QT Y which will be a number so just a map of the fields that we want to be able to send so we'll save that and let's go back to our controller here and we're gonna want to bring this this DTO into women and what is this and poor create from domain I'm not sure why that's there I'll get rid of that so let's import create item DT oh and that's gonna be from and let's see we're in the same folder so slash and then into the DTO folder and then we want create item DT oh so down in our create we're going to want to add into the the parentheses right here we're going to want to use the body decorator which we actually have to bring in okay so just like in Express you would do requests dot body to get your fields that's basically what we're doing here we're going to use this at body decorator and we want to put in the create whatever we want to call us which I'm just going to use create item DTO and then set the type to capital C create item DT oh okay so what we brought in here this right here is this so we're setting this type and then we should have access to this inside the function which will have any data that's sent so for instance create item DTO dot name or dot description or dot quantity we should have access to that so just to check it out let's do a return we'll use backticks here and let's say name colon and then say create item DTO dot name and we'll say description and let's do create item DTO dot description all right so now if I go back to postman I want to make the same post requests however I'm gonna add to the headers a content type because I'm sending data so I'm gonna add a content type of application slash Jason and in the body I'm gonna send raw Jason so let's do raw and let's in name let's see so these are items I'm just gonna say item one and let's to QT y so quantity we'll say 100 and description I'll say this is item one all right so I'm gonna go ahead and send that and if we look at our response we get named item one description this is item one so we can access the the fields that are sent in the body now using this DTO now I want to take a little side route here for a second and show you how you you can get access to the standard request and response object like usually in Express you'll do something like app app dot gets you'll have your route and then you'll have a function and you'll have this request response okay so you may be wondering how do we get this these objects so it's not really the nest j/s way to do this but you can't you can do it so I'm going to show you that real quick so in addition to the stuff we brought in up here from common we're gonna bring in req re s and then we need to also an import request response from X directly from Express ok and then I'm gonna give you this example in this find all so let's say we want to get the request and response object within this function so I would do at req I'll call it req and then this for the type it's going to be Qwest that I brought in a up top okay and then if I want the response we'll do at our es call it our es type is response okay so instead of just returning a string here I'm going to give you an example of how I can use the request object by just console logging requests dot URL okay the URL is just a property within that object and then I can actually use the response by doing like let's say res dot send just like you wouldn't express we'll just say hello world now this I'm gonna get a typescript error because actually I want to return this but I'm gonna get a typescript error because this is not a string this is a response so what I would do is change this to the type of response now if I save that you'll see prettier just kind of formatted this top part for me but if I go ahead and make that get request now to slash items I get hello world okay so res dot send in hello world just like we would in regular Express so you can access these objects just like this however it's not really the the nest way of doing things so I'm just gonna put this back to a string that and get rid of this and this and then request and response up here okay so if I go back and send we get yet all items again now I'm just returning strings for now because ultimately we're gonna want to call methods from a service that's where we want to do everything that has to do with our database all that stuff that's going to happen in a service so right now I'm just showing you how to structure a controller this stuff what we're doing inside these functions is going to ultimately change alright so let's move on to getting URL params okay because again I'm not using the request and response directly so I'm gonna bring in up here / Ram and I'm going to have a method here let's use get and I'm going to have a method called find one and we just want this to return one now we need an ID so inside the decorator I'm going to pass in : ID and inside find one I'm gonna pass in @ / am and then / am okay and then let's just return I'm actually gonna use back ticks here and we're gonna say item and then I can access / am dot ID okay we're returning a string here so let's save that and go to postman I'm just gonna grab that URL make make a get request to items slash 100 or 1 will do 1000 that's fine and send and we get item 1000 so whatever I pass in right here 44 that's going to show up right here because we were accessing it with this / am all right now we could bring in the you know the request object and we could say request stop params but that's not really though the way that we do it within with nest j/s all right now you can shorten this up if you want by passing in ID like this and then putting uh instead of this parameter in ID so then down here we could just simply put an ID so if I save that oops we go back to postman and send we still get item 44 okay so it's just a cleaner way of doing it all right so let's add our last two methods which are going to be a put in a delete we already brought in up here put and delete so let's do at let's do our delete okay now to delete we're gonna need an ID right so we're gonna pass in here just like we did with the get up above : ID and inside here we're going to use the @ / M decorator we'll say actually I'm going to just just do it like this will do / am ID but we need to pass in ID okay and then I'll just go ahead and return delete ID and just say right here we're returning a string okay we'll try that out so if we make a delete request - item / 44 we get delete 44 okay so last one is the update so let's do at put so this is also going to need to know which one so we're gonna pass in : ID I'm still on that so let's call this update okay so for update this is gonna have a parameter but we also we're also adding data so we also need the body okay we also need body and just like we did with create we're going to use that create item DTO okay but I'm going to call this update item DTO what I'm still going to use create item DTO so we can just add that as the type but you could create a different data transfer object if you wanted to for updates now in addition to that we need the params so let's do at program pass in here ID and ID and then down here let's just say return and let's say update ID and just to make sure we have access to the data that we send let's just output will say name and update item to do dot name okay so now if we were to make a put request to let's say this right here slash 44 let's add in our content type and it's gonna be application Jason and then for the body let's do raw and say we'll just copy it from the post actually so this right here let's paste that in we'll do item 2 so this would essentially update the data but let's send and we get item 44 that's because it's a get we want to do a put there we go so update 44 and then we get name item 2 so we were able to access the data we send along with the parameter so everything here is all set except for what we have happening inside here obviously this isn't what we want to return but we're gonna need a service in order to deal with the database and and get the the actual data and stuff like that so I'm gonna go ahead and actually just open up a new tab here and I'm going to generate a service so I'm gonna do nest G service and let's say item items all right now if we look over on the side here we have an item's dot service dot TS file and by default it's just going to bring an injectable what this does is it allows us to inject this as a dependency into a constructor all right now inside this item service we're gonna have a bunch of functions that we're gonna call from the controller but before I do that I also want to create a module on everything to be wrapped up in a module so inside the items folder I'm going to create a new file called items dot module dot TS and this is going to be set up very similar to the app dot module so I'm actually going to just copy that so we'll copy that will go to our items module and paste that in now all we need is you don't need the app service or controller we just need the items controller which is gonna be in the same folder and then the service which is in the same folder and we can get rid of this and get rid of this okay and we'll call this items module so now everything is encapsulated in this module so close that up let's start on our service ok so for our service I we're gonna ultimately work with data from MongoDB but for now I'm just gonna have hard coded data just to kind of get you guys to to understand how this works now we're gonna need an interface which explains what fields an item has okay similar to the DTO but we're going to use use the interface within the service so let's create a new folder inside items and we're gonna call this interfaces and inside this interfaces we're going to create a file let's call this item dot interface dot TS okay and this is just a type script interface if you've worked with angular you've probably worked with these before we're gonna say export interface item so we're gonna have an ID now I'm gonna put a question mark which means that it's optional because we're not always going to include the ID because it gets included by the database automatically and I'm gonna just call that a string say that an ID is a string name string we have description which I'm gonna make optional as well which is a string and then the quantity which is a number and that's it that's an interface pretty simple it's just the different fields that you want for for this particular resource all right so let's close that up and inside of our service let's bring that interface in so we'll import item and that's gonna be from dot slash interfaces slash item dot interface so inside of our service here like I said I'm just gonna deal with hard-coded data for the moment so I'm gonna say that this is a private property read-only items call it items and the type is going to be the interface of item now it's an array so I'm gonna put brackets here okay we're gonna set it to an array and let's say this first one will give it an ID now MongoDB gives you a long ID I'm just gonna go ahead and just put a bunch of numbers in for the ID now notice these are the red lines here this is because I labeled this items array as the type of item okay or an array of items and that has to match all the fields that are in the interface which it doesn't you can see right here is missing the following properties item I'm sorry name and quantity okay because those are required fields so it's a name item one and quantity let's say 100 yeah whoops sending me to do that so quantity and you'll see that now the red line goes away because description I put is optional so description will say this is alright and then I'm just gonna go ahead and copy this it will put one more - let's say quantity 50 and we'll say this is item 2 alright so we just have this this hard-coded item now back in our controller this find all I'm gonna want this to return an item right so in our service we're also going to have a find all function so right under where we define this let's create find all and what I want this to return is an item ok we created the interface of item we brought it in I'm gonna say this find all has to return an item and I'm sorry an array of items so I'm gonna return this dot items okay I can access this right here with this dot items I can use this because we're inside of a class and this is a property of that class so within any method within the class I can simply reference it with this dot items now this find all I want to call from my controller close that up let's go into the controller and go and defined all in order to call that though I have to bring in the service so up here we'll go ahead and import items service and that's gonna be from dot slash items dot service I also want to bring in the interface or the item type so we'll bring in item from dot slash interfaces slash item dot interface now before I can actually use the service I have to inject it as a dependency and this works pretty much exactly like it does in angular so within the class right here I'm gonna add a constructor and I don't even actually need anything in the constructor body but in within the parameters here is where we inject our dependencies so I'm gonna say private read-only and then let's call it items service and then set it to uppercase I items service now this can be absolutely anything you want this the second one here is what we brought in up here the actual item service so if you wanted to call this something different you can but now that I have injected this as a dependency we can access it with this dot item service dot and then any method within the service such as find all alright so inside the controller instead of return actually we just we still want to return but instead of the just returning a string we're gonna say this dot item service dot find all now I'm gonna get this typescript error because of the the string type here a string declaration this is not a string anymore it's gonna be an array of items so let's go ahead and save that and now if we go back to postman and we go to our original get request to slash items and I send there they are okay so we get our hard-coded items now let's say for this one where we want to quantum by its ID so in our service we'll go ahead and create find one and this is going to return a single item so we don't want the brackets here and I'm just gonna we're not dealing with a database yet so I'm gonna go ahead and return this start items so I can access the items with this dot items and I'm going to use dot find which is a high order array method that takes in arrow functions so I'll say for each item we want the one where the item dot ID is equal to the ID that's passed in and this will get an ID passed in which will be a string because remember IDs are strings and as far as what this will return is an item which I already have here so that should be good we'll save that get rid of that and we should be able to call this from our controller so back in our controller under find one instead of doing this let's say this dot item service dot find even C we even have some intellisense with the drop with the available methods and we want find one which takes in an ID which we can just pass in ID because we already get that from the parameter okay now we're getting an error here because it's not going to be a string it's gonna be item okay no brackets because it's a single item so let's save that and let's go back to this get requests we're actually getting a an actual item and we need one that hat we need an ID that actually is a real item so I'm gonna grab this one right here so this is item one's ID and if I pass this in as a parameter and I send it gets me item one alright so hopefully you can see the pattern here we just have our controller which defines the endpoints gets the parameters or the body if we're creating or updating something and then simply calls a service method and that's where we do all of our anything to do with our data okay now instead of just creating more service methods where I'm just manipulating hard-coded data I want to get into using a database because that's what you're going to be doing this is just you know you're not going to have hard coded data like this so this is where we get into MongoDB and Mongoose so I'm actually going to go to the documentation because it explains it pretty well if I can find where it is actually I think it's under providers no see web sockets fundamentals it's probably right in front of my face some of the controllers know you know what I'm just gonna search for a nest j/s database okay so here it is alright it was under techniques and then database now this this link here is actually more for using a relational database so you can use something called the type o Rh and you can connect to MySQL or post grad I'm actually gonna click on this next link right here which is ok and this will basically tell us everything that we need to do so we need to install nest j/s slash mongoose as well as mongoose so let's go ahead and do that real quick so we'll do npm install it's gonna be at nest j s slash mongoose and then mongoose okay and then once we get that installed what we have to do is add the mongoose module to the root application module so remember the app dot module TS that's like the meeting place for everything we have to bring in this right here this mongoose module so i'll just go ahead and copy that and let's go to our app dot module TS and paste that in so we're importing the mongoose module and we need to put that down here and well one thing i forgot to do is remember we created the items module so whereas it items dot module TS we actually have to bring that in to this file as well so let's import items module and we want to bring that in from dot slash items slash items dot module now both the items module and the mongoose module we want to add to imports so I'm going to say items module and mongoose module now we don't just do mongoose module we have to do dot for route and then inside this for route is where we put the URI of our MongoDB database so if I go back to the docs you can see right here imports mongoose module for route and then the the uri string now instead of just putting the uri string directly in there you should never really do that so i'm gonna create let's see i'm gonna go in my source folder and i'm gonna create a new folder called config and inside config i'll create a file called keys dot TS alright and this this keys file will have any like api keys or anything like that but in our case we just have the uri so i'm going to export default and st. URI now you're gonna want to put your own MongoDB URI in here I'm just gonna grab mine real quick oops looks like I closed up Atlas so let's say sign in okay so I'm going to say connect connect to your application and I'm going to just copy this string here and paste that in change put the password in and I'm deleting this after anyway so I don't care if you guys see this so I'm gonna save that now we have this in our keys ts file so what we're gonna want to do now is bring that in to our app dot module TS so I'll say import actually we export a default so we don't need the curly braces so import config from dot slash config slash keys and then right inside here i'll just do config dot uri instead of just putting it right in there so I think yeah I think that should be good as far as setting that up so let's save this and I just want to go back to the term to the server here make sure I'm just gonna reset it real quick so ctrl C and then we'll run NPM run start dev again alright so it looks like everything's ok if it didn't connect actually what I'll do is just change something in here to make it wrong so I'll just I'll mess the password up ok so now you can see unable to connect to the database and it just keeps retrying so put that back and save and now we're all set so you can see how easy it is to implement Mongoose and MongoDB which is one of the reasons I really like this framework it's not so you know manual as it is with just regular Express alright so we have our Mongoose model module we can close up app dot module TS and see next thing we need to do if we look at the docs is we need to create a schema all right so let's create a folder inside items so this items folder right here I'm gonna create a folder called schemas and let's see inside schemas we're gonna have one file for our item schema so new file call it items let's do items dot or actually let's do items singular item dot schema dot t-- s alright so our schema we want to import Mongoose now we're using the import syntax here not the not the common J s that you know Const Mongoose require so we need to do import all as Mongoose from Mongoose alright and then we're gonna export let's say Const item schema set it to new Mongoose dot schema and pass in here an object with our fields so we want name should be string quantity number and description string so a very simple schema and we'll save that alright so we'll close that up next thing we want to do and just to show you in the documentation so inside our module so our items module we're gonna want to are going to want to do this so bring in mongoose module and add it to imports and we just want to call this for feature method and pass in an array with the name and then the schema name okay we also want to bring in the schema so let's go to our items module and i know this seems like a lot of setup but once this is done it's done and you can then you know make your queries and and and go on so inside our items module let's bring in say imports item schema so item schema is coming from dot slash schemas slash item dot schema why is that not coming up automatically that I know it schema item dot schema dot e s yeah so that should be fine and then we also want to bring in the Mongoose module so I'm gonna just bring that in up here so that's gonna be from nest J s slash Mongoose alright then we just want to go into our imports just like it says in the docs and say Mongoose module dot for feature and that's gonna take in an array with an object with the name which is gonna be item singular and then schema which is gonna be our item schema that we brought in all right and I think that should be it I think we should be ready to go so let's save that and let me just make sure there's nothing else here yeah so now in our service we can bring in our inject model model from Mongoose and then we can inject that as a dependency and we can then use Mongoose so let's go back to our service and we can clear out this whole just this hard-coded property right here I can just completely get rid of that and then up here let's bring in let's say imports on an import model from Mongoose and we want to import inject model from nest Jas Mongoose all right and then down in our class we need a constructor okay because we're using dependency injection so in this constructor we're going to do inject or add inject model and we pass in here item and let's see after that we're going to say private read-only call this item model and we're gonna set that to model that we brought in which is going to have the type of item okay so now we should be able to say this dot item model dot and then any Mongoose method like find create or whatever find one all that stuff we should now have back access to ok so within this find all here we're gonna call the we're gonna call dot fine now mongoose returns a promise and i want to use a sink of weight just to keep this neat so we're gonna mark this as a sink and down here instead of returning this dot items which no longer exists we're gonna say return a weight this dot item model dot find okay now this returns a promise so this right here I'm going to get rid of that and we're gonna say promise and it's gonna be the type of an item array okay and let's see while we're here we might as well do this find one as well so takes in an ID that's not going to change instead of just doing this find which had to do with the hard-coded data we're gonna first of all label this a sink and then return a weight and let's say this dot item model and we're going to use find one okay so find one which takes in an object we want to match the ID okay Hmong mongodb always gives an underscore ID value which is an object ID we're gonna mass it match it to the ID that's passed in okay and it's going to return a promise which will have the type of an item a single item okay so now that we've done that lets go to our controller now because this is no longer we're getting getting this error because it's now returning a promise however we marked it as this item so all we have to do here is change this to promise item array and then down here promise item okay now in the controller we also want to mark this as a sink so a sink find all he sank find one and I think that should do it so let's save this and let's go to post man now if I make a get request we should just get an empty array because there's nothing in the database which we do if I go while I can't even try they get one to find one because we don't have anything to find all right so now we want to do is work on being able to create a new item so let's go back let's go to our service and we're gonna want a method to to create a new item in our database so let's go right here and let's say async create this will take in an item with the type of item and it's gonna we're gonna return a promise of an item and let's first create a variable we'll just call this new item set it to new this dot item model and pass in that item that's passed in okay and then as far as the return we're just going to return a weight take that new item dot save and what this will do is return the new whatever that new item is it's gonna get saved and it's going to return along with the ID that's automatically added by MongoDB now we need to call this from the controller so let's make sure we save this service go back to the controller where we have our create and we need to we need to change this up a little bit so we're still dealing with this with the data transfer object however of course we don't want to do this we want to instead return this dot item service dot create remember it takes in an item that item is gonna be the DTO okay instead of the string here we want promise it's gonna return that new item alright so we'll save that let's go to I keep forgetting what this is postman and go to our post request so we have item 1 quantity 100 now once I send this it should actually get added to the database so let's go ahead and try it we'll send we get a response back with the new item with the ID that MongoDB added let's go ahead and add another one will say item 2 quantity will do 33 this is item 2 if I send that and let's do one more 3 and send good so now we should have three items in our database so I'm going to go back to the get request to slash items and send and now we have all three items now we should be able to search by one so let's grab one of these IDs I'm gonna go back to my get request here where I pass in an ID and we'll go ahead and send that and we get item 1 all right so all those endpoints are working and we're getting the data so all we have left really is the delete and the update so let's go back to our service and let's create those methods okay so when our service will go under the create and let's do let's do the delete first it's a little bit easier so we'll say async delete it's gonna take in an ID which is a string it's gonna return a promise it's gonna be an item and let's do return a weight we're gonna use the model so this dot item model and I'm gonna use a method a mongoose method called find by ID and to remove so it'll do just that i'll find it by the ID and remove the IDS getting passed in right here so let's save this go back to the controller to where we have our delete and obviously we want to change that so for this let's label this actually I'm gonna leave it actually we don't need to label these a sync do we think we do so for the delete we have the ID it's gonna come from the URL and then as far as the return we're gonna use the service so this dot items service and then we're gonna call the delete method which takes in the ID and this is going to give us a promise item so let's try to delete something so we'll go back and so this is item one let's delete item three so we'll grab this ID copy that go to where we have the delete request go ahead and paste that in and send so we get the deleted item back however it should be gone if I go back to the get request and I send again notice we only have item one an item two okay so we're now able to delete an item so the last thing we need to do is the update so back in the service we're gonna go ahead and create let's say a sync update and this is gonna take in two things it's gonna take in an ID it's also gonna take in the new item so item which will have a type of item all right and then as far as what this returns is a promise item and let's return a weight this dot I in a model okay we're dealing with the model we're gonna call the Mongoose method find by ID and update so find by ID and updates and we're gonna pass in the ID we're gonna pass in the item and then it also takes in some options and we're going to set new to true so basically if there's not one already it'll create a new one so save and you can see how clean this file is how nice and clean this is so let's go back to the controller and this last update method is the last thing that we need to update so let's see instead of doing this let's say return this dot item service dot update we want to pass in two things one is the item I'm sorry not the item the ID needs to know which one to update and then the data is gonna be in this in the DTO so this right here so I can simply pass that in and then change the string to a promise with item all right so let's try to update something we'll go back to let's see so we have item 1 let's say we want to update the quantity of item 1 so we'll grab the ID right now it's a hundred so I'm gonna go make a put request to item slash and then that ID and all I'm gonna say send here is the quantity and let's change it to I don't know 30 and send so it gives us the item back with the change quantity and if I go back to the get get request and send you'll see that now item one has quantity 30 so we have a complete crud create read update and delete rest api alright so that's it we don't need this async in the controller I don't know why I thought that but we shouldn't need that but yeah you can see how organized everything is now how nice and neat so all your resources would be encapsulated in a folder like this with your interfaces your data transfer object source schemas your controllers all that stuff services and it's all packaged up nice and neat so you know do you need to use Ness j/s no you can just use Express and create your endpoints and stuff like that but if you want something that's a little more organized especially if you're using angular on the front end you can see how similar it is in terms of how everything flows together so I think that having a full stack application with angular on the front-end Ness jeaious and the backend I think is a really good idea and very powerful so that's it guys hopefully you liked this crash course if you did please leave a like and I will see you next time
Info
Channel: Traversy Media
Views: 149,947
Rating: 4.9512196 out of 5
Keywords: nestjs, nest js, express, javascript, node.js, nodejs
Id: wqhNoDE6pb4
Channel Id: undefined
Length: 60min 0sec (3600 seconds)
Published: Mon Mar 25 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.