Fastest Way to Create APIs? | NestJS Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey how's it going it's a snowy day in seattle today and i thought this would be a good time to show some techniques that i've learned with nesjs specifically on how to create crud apis as fast as possible what you're looking at on the screen here is what we're gonna make today notice that it's completely documented you see all of the crud routes you got your create read update and delete as well as a few extra ones and it also shows you the different schemas of the resources that we're working with so for example right now we have a crud for a a user entity that has a first name and a last name and we've got all the documentation here on how to interact with that resource and get information about it so to show you guys that this is completely working as an example i can do a post here to create a new user uh maybe i'll give that a name of john doe okay and when i execute that you'll notice that it responded with the resource that was created it also has that id generated over there we can also give the bulk create a chat so that one's kind of similar you can just provide an array of those same objects so maybe you can do jane doe and you know maybe we got a family of those here let's add jack and jill all right execute notice that we just created three more resources and when we go over to the get routes notice that we have a bunch of options here that i'll talk about in a second but if i just straight up execute that we'll get back all of the users that we created so far you can also do get by id so for example we know that the first one we created was john doe if i provide an id of one here execute we get john doe and then back into the the get all here uh so i said i was going to talk about these there's a bunch of these options that you can add so for example maybe you just wanted the first name and maybe let's do some pagination so i want to do i want to get the first page and on each page i just want to show two people let's take a look at what that produces so you'll notice we get back an array with two people and then it shows the details about the remaining pages and then also notice that now we're only pulling in the first name so you can almost kind of make this work similar like graphql where you can request the very specific um data attributes that you want and then there's even things like filters and all that stuff so for example maybe i'm trying to filter the list down to just the ones that have the name john so if i execute that we'll notice that we get back john and there's only one user in our database that has john in it so you only get back one date at one page total one and then of course we got our other routes like for example we got updating maybe i want to update john to have a different name um different though i don't know right execute now that we'll notice john got updated oh now he's not john he's different so if we do another get all we should be able to notice that you know we still have our four users but now you know you got different gel different dough on the first one right so this is a fully functional crud rest api with documentation and all of those features that you just saw and i didn't even even cover all of these there's deleters there's patching all of that we're gonna do and probably 10 5 to 10 minutes or maybe less i don't know we'll see but it's very quick i'll show you in just a second so make sure to stick around and let's figure out how to do this so i mentioned to you we're going to use nesjs it's an awesome framework to use for node.js backend development all right so i'm going to assume that we're starting from scratch so let's take a look at the documentation here and i'll kind of point you on uh where you should look but basically we're going to install a couple things here so you can see on the left here it says that if you haven't used nest before you should install the nest cli so that's just npm install dash g at nesjs cli i already have this installed so it should go pretty quick and then once you have that installed you just do nest new and in the name of your project we're going to do we're going to call this one craw demo and it's going to ask you what do you want to use as your package manager i'm just going to go with yarn i think a lot of people sorry i'm gonna go i meant we're gonna go with npm i think a lot of people already know how to use npm and it's gonna take a moment to install here there we go once it's done i am just going to cd into the newly created project and i'm going to open up a visual studio code from there all right so this is the generated project from nest you can take a look what that made it just has a basic app module and a controller in there if we run npm run start dev it's going to start running the application on localhost 3000 so if you go to localhost 3000 you should see a hello world there so now we're kind of back from our skeleton application here and we're going to build up to the thing i showed you in the beginning of this video uh first we're going to do a little bit of cleanup we don't really need these app controller and app service you can delete that all right so now we just got basically an empty module next we're going to use the nscli to generate our users module so this is one of the really nice things with nest and their cli is it just kind of auto generates a lot of the the files and stuff for you and wires it all up all right it really saves you a lot of time so we can do an sg g is kind of short for generate you can also do generate but to use the shorthand so i'm going to do an sg module users and you'll notice as this generates that file it also updates my app module with the users module as an import okay and then we're also going to do an sg module sorry next g controller users and then sg service users all right and what did that do you'll notice that it added a bunch of new files into our users folder and then it also wired it up all perfectly you know we now have this module this user's module updated with that controller and the service and if we open up the user's controller there's not nothing really here yet so um just as a quick make to make sure that this is working we can do get get users all right we're just going to do a sample thing here and then we're going to run our application and npm run start dev and if you go back to our browser i refresh this since i deleted the since i deleted the app controller this is now giving them a 404 but when i go to slash users which we now have a user's controller on the right we've got the users right so that's just making sure that it works you can also delete this app controller uh test file by the way all right from here the next thing we're going to add is in main.ts we're going to add our swagger configuration i'm not going to spend too much time kind of explaining this part but it is very well documented in in the nest website so if you go to the recipes on the left here and then head on over to open api swagger everything i'm gonna do in the next uh two minutes or so is documented here so first we're gonna npm install all of this the swagger module and the ui express i'm going to put that in my terminal and then i'm also going to copy some of this initial configuration here and we'll just manipulate it a little bit all right so i'm going to put that in the middle here and i've got some missing imports so i'm going to do import document builder and import swagger module right and what that does is it just adds it up here and then let's update this to a title of user's crud and you know some description let's clean it up a little bit we don't need this tag and this string here is kind of the position where the documentation will be hosted in i'm just going to make that a slash so that when we go to localhost 3000 it's going to show us our documentation so let's take a look at what this generates so when i go back and run npm start dev when we go back to our browser on localhost just a base host now we've got our swagger documentation right it's already working now we're still pretty far from what i showed you in the beginning of the video but we'll get to that in a second but this is just setting it up so that as our routes get built you know there we have completely documentation that is usable here and you can also use the swagger here to kind of use it as a tester you know it's not only a documentation it's also a place for testing your work so kind of like you know you might do this in postman or insomnia so you can execute on the documentation directly all right next what we're going to do is we're going to do our configuration for the database what we're going to use today is a sqlite database you absolutely can use whatever database that you would like but we're for simplicity we're going to stick with sequel light so again i'm going to show you the documentation so that you kind of know how to set this up in the future on your own so under the techniques here on the left there's a section there on database which is very well written like honestly just follow this and you'll be good to go you'll notice that they're they're very big on recommending type orm as the orm of choice but you can use whatever you want really uh but the typo rm has really good integration with nest so i highly recommend it as well so i'm gonna copy the installation here but i'm not going to include the mysql i'm going to use sqlite so in my terminal i'm going to paste that in and then i'm going to add sqlite 3. while that's installing if we if you take a look at this documentation it shows you that to create your database connection you import this type or a module and put it into the imports of your app module so we're going to do something similar except that the connection object that we're going to pass in here is for sqlite instead of mysql alright so now i'm in the app module app.module.ts and we're going to add our database connection so i'm just going to paste some code in here again the documentation kind of talks about this but basically you do type or module for root and then within that function call you're gonna add your database connection details so now that we have our database connection the next thing we're gonna do is create a new entity file we're going to call it user.entity.ts and what this is is effectively it's just a class that represents uh the user table that we're gonna create in our database so from when we started with the video we you might have noticed that there is an id which is a number and then there's a first name which is a string and then a last name which is also string right and you can add whatever other details that you want here we're going to keep it pretty simple and then with to kind of officially make this a type or entity we're going to add add entity up here so i had it auto import for me and then each of these needs to have a column i'm hitting tab here just so you know of how that import is being added okay so this is kind of marking these fields as columns in our user table the id i'm going to put something kind of special there i'm going to call i'm going to use primary primary generated column what this will do by default is kind of turn it into a incrementing integer in a database so as you create a user for example the first one will be one the second one will be two if you kind of look back from our demo earlier you you probably have noticed that and then one last thing we're going to add here this one might not make sense right away but i'll i'll talk about it in a little bit we're going to add api property here from um that's just swagger this is just so that when you look at the documentation you can see the shape of our schema there as well alright so now we have our entity class when you run the application at this point it should make a new user table in the database automatically next what we're going to talk about here is how do we generate that crud all of those you know create read update delete routes how do we do that in as little time as possible if you're familiar with nest you might know that the cli actually also has a crud generator which works if you the nsg resource user is going to generate a bunch of stuff for you like the user's controller and the user service and the module but it's not going to be hooked up to the database so there's still quite a bit for you to implement so you'll notice in the documentation it says that it says that the generated service classes are not tied to any specific orm so that basically means that it's going to generate the bones for you but it's not going to be fully implemented you still have quite a bit of work so we're not going to go that route what we're actually going to use is this library called sjs crud and you can kind of see the features from this package right a lot of this stuff i kind of went over you know the ability to kind of do it very quickly easily and then it has all of those different features like pagination filtering sorting right all the things that you likely need in a restful api when you click on the docs here it's going to open up to this page and it's going to show you the other things that you need to install so we're going to go ahead and do that so i'm going to use i'm going to copy this so back to my terminal here i'm going to install that the card package cloud class transformer and class validator and then it also says that if you're using type oram uh you gotta install these things so i'm also going to do npm install the crud type or these two packages i think we actually already installed that earlier so technically i don't need that but now that we've got all of those installed here's where the stuff starts becoming real so going back to our users controller now let's make this real right we don't really need this we're going to delete that what we're going to add here is a new decorator simply call it crud and that you can pass in an object there for the configuration first let's import this crud from uh that package that we just installed and then what you want to provide in here the minimal thing you need to provide is the model that it represents so we're saying that this controller is for the resource of user which is our entity so this is still a base class there's one more thing we need to add in here we want to add that it implements crud controller with a generic type of user all right and also add that into our imports now this just kind of says that it's implementing this interface and you'll notice you get a red squiggly here because it's expecting a a service property which needs to be injected via the constructor so let's add a constructor here that has a public service property and that is the type of user service this might not make sense as you're following me but it's going to make sense in a little bit here and we want to make sure that that's imported and at first it's still going to stay that the type of service is incorrect so you see this red squiggly here because we also need to update our user service to kind of hook it into um the package that we just installed so within the user service we're going to add a couple other things here we're going to do extends type orm crud service that also has a type of user so this is where it starts to kind of have the magic behind the scenes and then also needs a constructor we're just gonna do inject repository of type user um so the repo and then we're just going to pass that to the super right um again probably does not make a ton of sense but just bear with me here so what this effectively does is it kind of injects the repository from typo or m into the constructor of this type of ram crud service and this comes from the thing we installed earlier now with all of that out of the way let's go and start and see what we have npm run start dev so as i run this it's telling me that user service is missing a dependency because i forgot one piece here so we're going to correct that because we're trying to inject a repository of user in a service i really should have added a new import here imports type orm module that for feature user and i realize if if it is fairly new to nest none of this probably made any sense to you uh just a quick side note you know if you go into the documentation for the database so under techniques database it talks about a lot of this stuff where if you were doing your database connection right we did the the connection object and then once you do that it tells you that for the modules that is looking to import the repository for that entity that you have so for example user it tells you that in the module of that you need to specify an import with the entities that you're looking to access in that module so in our example we got the users module we want to access the users table so you want to make sure that you have your type or module for feature user and then when the service you're able to do your inject repository and from that repository you can start using the different methods that nest provides for accessing that database table so back in our code i ran npm start dev again everything's working let's take a look at the browser and see what we've got so if we refresh on localhost 3000 now we're back to where we started right so everything that i showed in the beginning it's here it's working let's give it a shot let's do a bulk insert of some users um maybe let's do test a and test b execute notice that we get a 201 that means it worked and when we go to our get users we can try it out execute we got an array we can do get by id so we know that the first one has id of one if i do user slash one execute we got our test a right it's working let's try our update so let's do a patch here on user1 i'm going to change the first name to john last name doe execute it's working and just to show you if i do a get users again execute now we got john doe test b and let's try to delete i'm going to delete john doe id 1 execute and then if we go back to again our get all execute now we just got one object because we just deleted right so we're back where we started didn't spend a lot of time right and we generated a fully working crud api that is fully documented it has all these features of being able to specify what field you want you can filter by certain values like maybe you're looking for a person with a specific name you can do sorting like for example you want to do maybe you want to do alphabetical order of by name you can do joins so this is like if you have relationships in your database entities so for example maybe a user has one-to-many pictures you know you can do a join here so that you can get the user's object along with their pictures and then we kind of showed earlier that you can do pagination so there you have it a fully working crud in just a few minutes i'm probably going to edit the video to cut off some of the points where i'm installing and kind of just pausing reading documentation so you can kind of see that if you if you know how to do this well it can really speed up your process if you're making crud apis very often there are some things that you can also add to this if you want to further customize it so it the nice thing with the crud package that we used is it doesn't box you in if you take a look at the documentation you'll notice that it does allow you to further customize it to your needs so for example maybe you want to do authentication maybe you want to do some validation it has all that handle so for example you know maybe in your entity only some things are are required or optional depending on on if you're updating or creating there's documentation on here for maybe you want to completely customize each route so you'll see that it allows you to do overrides like this and then you can add in your own logic all right so that's it for today hope you enjoyed that video um i'm really interested in making more content like this you know this is full stack stuff front and back and you'll see more of that stuff in my channel testing algorithms data structures those are a lot of things that i'm interested in teaching so if you if you like this kind of content make sure to subscribe and also make sure to hit that thumbs up you know it really helps the youtube algorithm to find my video and kind of share it to people like you who might have a lot of interest in this kind of content anyways that's it have a good day and i'll see you on the next video [Music] [Music]
Info
Channel: Marius Espejo
Views: 8,870
Rating: 4.9302325 out of 5
Keywords: nestjs, nestjs tutorial, nestjs microservices, typescript, javascript, nestjs swagger, nestjs typeorm, sqlite, typeorm sqlite, database, crud api, crud api nodejs, node js tutorial, node js express, nodejs microservice, node js swagger
Id: WZtHM4Ph-K8
Channel Id: undefined
Length: 27min 49sec (1669 seconds)
Published: Sat Feb 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.