Nest.js Crash Course #4 - Providers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to talk about providers in sjs in the last video we created a ninjas controller that defines our routes for our ninjas API however like I mentioned it's very lean it doesn't have much logic and when we really think about where should that logic live that's what providers or services are usually for providers are really just a class just like anything else in Nest but they specifically have an injectable decorator that means that this provider is something that can be injected into any class that depends on it now what does that even mean let's maybe write some code first that I think will help explain this concept better so we mentioned that our API is meant to manage an Army of Ninjas now that collection of ninjas probably lived somewhere right in a real application you might save it in a database in our case here we're just going to create a simple array so this array contains representations of our Ninjas for the controller you can probably assume that we have an ID and we said that they also have a name from the create route and then I also added a weapon property here just for fun right they might have uh ninja stars or nunchucks whatever and then let's provide a method that simply returns this collection and it allows us to optionally filter by the weapon now ultimately we want to use that method in our get ninjas controller right so instead of filtering by type here this probably should have been weapon so let's update that query and then instead of returning a mock array here we'll return the actual collection of ninjas but first how do we get that collection or the service in general into this controller so if you weren't already familiar with Nest yes you might imagine that oh this is a class that means that in order to get access to this method I probably need to instantiate an instance of this class so let's try that let's do con service equals new ninja service and then we'll return a service dot get ninjas pass in the optional weapon filter by the way notice that we have a type problem here because this is expecting a specific Union of strings so let's update this to be stars and nunchucks and now it's good so we should be able to go to our Local Host again and do request on get slash ninjas it's send there and we should get back our array of ninjas and we can go ahead and test our filter actually as do weapon equals Stars and that should limit it down to just the one ninja that has stars as the weapon now as you might imagine as we start moving you know the logic for the rest of these routes into our ninja service it might get kind of annoying to keep instantiating a new instance of the Ninja service across all of our routes you know it would be nice if that was just you know the instance was just created for us and injected into our Ninja's controller and that is actually what nestr can do for you so what we're going to do in our Ninja's controller is we're going to add a new Constructor here and let's do private read only ninja service we're going to provide this with the type of our service then we can actually comment this out and just replace this with this.ninja service and if you test that out it should work the exact same way so what's going on here basically what's happening is that because our service is a provider right it's an injectable it's got this decorator up here we're telling Nest that this is a class that hey you're in charge of instantiating this class and you can automatically inject it to anything that depends on it so we're saying that the ninjas controller depends on the ninja service and you can tell Nest that by simply providing it as a parameter to the Constructor and Nest will look at the type of this and automatically sort of instantiate for you behind the scenes right and similarly we never instantiate the Ninja's controller class right Nest is doing that for us so you can imagine that behind the scenes what Nest is doing is it's doing something like this you know it's doing something like that and then you see that our ninjas controller requires the ninja service right so it's also doing something like this behind the scenes instantiating that service and providing that service automatically into the controller and that's really what we're talking about in terms of injection right that's why the The Decorator for our provider here or service is called injectable because behind the scenes an sjs is automatically instantiating it for you and injecting it to your controllers and anything else that is depending on it and you know you can inject a provider into a service right you can use multiple services that inject other services controllers can inject multiple Services it's really up to what you're trying to build but understand that at a super high level this is what's happening behind the scenes this is dependency injection so basically you need to remember that you shouldn't pretty much ever instantiate classes in SGS a lot of it will just happen automatically for you alright so from here what we really need to do is let's start to implement the logic behind the rest of our ninja crud into our ninja service so we probably want a method to find a single ninja that's pretty easy let's just do a get Ninja that takes in a number ID and tries to find that from our collection and then we'll have a special case of what if we don't find a Ninja uh we'll throw an error here and we'll talk about exception handling probably in the next video but for now throw the error and then otherwise we'll just return the found ninja next we probably want to be able to create ninjas and we said that we already have an object that represents you know the the thing that we pass in so let's do create ninja detail we'll just reuse that here and we're simply gonna add to our collection of ninjas however we have a type error here because we have a couple things that are missing in our uh on our object one is that we said we're providing weapons right so we need to update our create ninja dto to have the weapon and then next this is still expecting an ID so we need to generate an ID so let's do something like this right so we'll just generate an ID for that ninja and then we'll pass it in into our collection and return that Ninja now just to save you some time I went ahead and actually implemented the update ninja method in our service as well as the remove ninja go ahead and pause the screen if you'd need to copy it but basically we're doing this that ninjas and replacing it with you know a map of the original collection simply overwriting uh the ninja with the same ID as the thing that we passed in and then remember similarly as the create ninja dtl we have the update ninja dto so that's how we're sort of doing the update of the original record and then we're simply returning uh we're using the get Ninja ID here so that we get back that single item in the collection and then for remove ninja similarly we query that ninja that we're looking for and then we update our collection to basically filter it out and then we return that ninja that we found uh you know just prior to it being filtered out now we need to remember to go back back to our controller and update the rest of these so forget what Ninja this should return this dot ninja service that get Ninja passing the ID now notice that we have a type area here because it's expecting a number not a string and coming from the URL it is a string technically so we'll Typecast this by adding the Plus in front of it and that basically turns it into a number for create ninja same thing we do this dot in your service dot create ninja and then we just forward the dto again same thing pass in the number ID and the update dto and for delete let's wrap this up all right so that pretty much wraps up our basic implementation of a crud API right we got our read great read update and delete let's go ahead and test our application so far so again if we do a get on slash ninjas we have as before our two Ninjas and we should be able to query a single Ninja by providing the ID up here let's try adding a new Ninja so remember that needs to be a post request on slash ninjas and you need to provide a request body we said that our create ninja dto requires a name to call this ninja c and a weapon or maybe this ninja just has a stick for a weapon let's hit send we get back a 201 creative response with that name and that weapon and a new ID that means that if we provide this as a parameter to the URL and do a get we should be able to get that same ninja back right that we just added to our collection and similarly we can go and update this ninja by changing their weapon to Stars let's hit send here so put request on ninja slash ID and we get back uh the updated record so just to make sure we're doing this correctly if we do a get on slash ninjas we should have uh the three ninjas and then the last one should have a weapon of stars because we just updated it although uh you might catch that we have a problem right because our application kind of describes that there's only two weapons there's only stars or nunchucks but we were able to add a ninja with a weapon of stick so ideally there's validation there right so we'll cover that in the upcoming video but let's keep moving and testing our application for now let's go ahead and test deleting this ninja to make sure that our last route is working as expected so we'll do a delete on ninjas slash ID we don't need a request body in this hit send and it's going to respond with the deleted record again just to make sure that we're doing it correctly let's go to slash ninjas and do a get to make sure that it's no longer in our collection of ninjas right our army
Info
Channel: Net Ninja
Views: 33,141
Rating: undefined out of 5
Keywords: nest.js, nest js, nestjs, nest, nest js tutorial, nest.js tutorial, nestjs tutorial, tutorial, nest js vs express, node tutorial, node frameworks, what is nest js
Id: MPYNkau4Bgg
Channel Id: undefined
Length: 11min 15sec (675 seconds)
Published: Thu Feb 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.