NestJS: CRUD API & Typeorm with Observables | Blog Project V-02

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi and welcome to our second video from the serious block with Nestor s and angular so in this video we are setting up a feature module for our user entity we will have some basic crud endpoints and we will also use the type of em repositories and do everything with observables and set of promises as it's usually done with nest Reyes basic structure of the video is like in the first one so first we have a look at the video outcome then we will write a user story for it and then we will implement it so as promised we will first have a look at what we built in this video and here we have our user feature module and it has a controller as models it has service and it is working so it's starting and we have some routes and so we can see it here very easily what we are doing we can create a new user we can say x free the API response be new object if we do it again it returns an error cause it's already there we can get all users that we have created we can find one user for example this one we can delete one user so the number six by ID and we have affected was one so it's elite it so if you run this again you would see if this user is no longer here and it's removed and we can also update one user for example we can update user number four and we then have to provide a new name for it can call it Maximus terminal which is a famous tram name for this and it's max one and you see before it was this and if we now update it we get affected last one don't we go again and get all we see now it is changed hi everyone and welcome back to this video now we are in the second part of our block tutorial and today we want to have like adding a little feature module for the user entered here and for this we also start off writing a card that we or a user story that we could name as follows like creative working user API and we could say as a technical lead I want to have basic crud behavior for the user entity and we can say the acceptance criteria are as follows we want to use type RM and the repository repository from type RM so we don't have to code everything by ourselves we want to use observables observables and not let's say instead of promises so we will use come on use ethics trainers to work around it we also want to create our pizza module user and our user entity should have some properties and this should be very basic one name and username this should be unique and also ID which is also unique and our primary key so this is very basic and the last thing that we want to is for now we are starting to use skit flow so we are not working on master anymore in our git repository but we use we get flow of branching model and so we will have like our own branch for every ticket path with you and so we can say we want to use git know all this we have to give it a name and we can say video - I select self-initiating we get flow and for this we can just use the command git flow in it and this is already coming with your basic get installation on windows and I will have to do - F because however you did it once and this faucet had to do it again but you can just use git flow in it so now it's asking us which branch would be for production and we will go to us with the standards so we will just enter everything so we will use for feature around us an empty tree are for bug fixes bug fix or release release for how to fix or fix and for support support and we don't want to have the version tag prefix so now everything is initialized and now we want to create our first story or our first feature and we can for this use wicket flow or we can say git flow feature start and now we name it and we have seen here we named it video - and so we will also name it video - now you see we have a new branch based on develop and now we are on switch to the speaker branch video - I mean I can now start committing things we are done we can finish the speech at lunch and now we are on our feature branch and we can stop implementing our user feature module so now first we can see the into our API then we can go into our source folder like here and we can use Vaness CLI again to regenerate our module you can just say generate module and we want to have an end user and this does two things for us first it generates our user module file here and it also imports it here and we use it as an import next we can go into our user folder and we also want to generate a service user and we also want to generate our controller but first we have to name this service and we want to name this controller and you see why attack key users because now it's same user service and it's user service and we have here our user controller to user controller so everything is fine we also have generate our spec files to it so now that we have the controller and from the service words need our entity or our interface and so we can create a new folder named model or we can rename it to models and then we want to have our interface user interface of TS we can just say export interface user and now we can just think what should our user have and we said it should have an ID we can make it optional it's of the type number it he also has a name optional which is a string and we have a user name which is optional and also string so a second model that we need is our user entity so which we will store on the database for this we can just say export class use entity and we can annotate it with entity yes from type or M and this has the same properties as we user interface so we do need an ID of a type number and we can also untied it with its primary generated column from type RM we have our name which is a string and it is a column and at last we have a user name which is also a string and this is the column but it should also be unique so every user should just have or there should just be one user name for every user so it we you can also both named Thomas but the user name would be Thomas one at Thomas two or something like this so it cannot be the same and for this we can pass in here like an option unique and this is true so it should be unique so now we have our user interface and our user entity and for this we can use our user module because we will import here our type or M feature and what you want and for this we add our imports array and here goes in the type or my hewn from type RM and not for rude but four feet across we are now in the user and where we want to add our user entity now we do have our interface and our entity so we can start with the user service you can go into fire we have our class user service here and the first thing that we will need is a constructor and where we need to inject our repository and we can use a notation at inject posit or e for mysterious type om and we want to inject the user entity and this is private read-only user repository it's of type repository from type where m and it's a type user so now we can do some stuff with this and we can say we want to create a user we need an import user from type user and be returned and observable you type user we can just say to return normally we could say this user repository dot save and we want to save our user into it but since we are returning in observe when they use user repository is returning promise we will have to do it quite differently so we can use the from operator from Eric stress then we need some other methods like fine draw we can use here there we don't need anything has an input we want to return observable also and this is now user array and here we also return this user repository don't find we also need here from so we can convert the promised into an observer and now I will do this with all this one we can say we want to delete one and to delete one we need our ID of the user which is the number we want to return observable of the type any so later we can also reflect through this name an appropriate type and we want to return from this user repository delete and we want to delete by ID next we want to say we want to update one and for this we need our ID which is a number and we need the user of type user and a one two that we want to change and we observable should also return any we can use can return from this point user repository don't update and the first input is here IV ID which we want to update the second one is what we want to change so the user where so we're just switching it so this was a service where we make a connection to our repository and the next thing that you need to do is we need to add in endpoints and here in our controller it's named user but we don't have the plural form of it and we need also here and constructor to inject our user service and we import and when we have like the same methods or the same names and who also have in our service so we want to create one we want to find one you want to find oil users we want to delete one and we also want to update one user so now we have all our basic methods so we can start by implementing the first correctly so we can say we have here it's a post for create and we would extract from the body our user which is from the tab user we will return and observe the which is also from type user and here we can just I return this don't use a service don't create and we have to give it the user and define one we have like I get requests and this should go for the ID so you have to look here for the column but make Colin think it's named in English so that it's goes for the route service would be on anything for you and here we want to have our param because we are in the URL and we are also returned on observer from v type user and we return parts of the user service defined find one method we don't have so we have to add it and we want to find in by ID number this is an observer we will read from user and for this we can also use our posit Orion if this should have a method like find one and we want to inject our ID so now we can use this method in the controller and we inject our ID into it that we get from the you URL so we can just go for param start ID so why isn't it working here another user 12 this would use a service to find one so no it's okay okay so to find oil we have our also will get rope out VI D and we won't trust your return if we call it and observe the you type user area we can also return this user service dot find all if we wanted to lead one we make a delete request and here we are also using our ID params and we can make it maybe a little bit better than when you find one so we can set our ID and we can just say ID type string cause it's coming from our URL and when we return this user service don't delete one and we convert the ID to a number and at last we have our update one where we will need two things cause we need a param from our URL and we also need a body so this will be a put request answer to our idea and here we now need our param ID and we need the body from it where our user object will be this is from the top user we will return our observed this should be of type any and this and we can just use again our user service and we can update one and now we have again to pass our ID to a number and we also have to insert our user so this should be it so we have a like let very basic behavior so we can create one user we can find one we can find all users certain the database we can delete one user by ID and we also can update one user so we can change the name or something like this and we have preached this without hype or am not here and our user entity now we can test if it's working now that we have our code ready we want to test if it's all or if it's working so we can just start our app we can say npm run start death and now it's starting up on we can see if there's any failure or any mistake or error already that we have everything is connected we have mapped our user routes here you can see it and beside successfully and now we can go into postman to test our API and we can create some requests here so we can say we are going to localhost 5,000 users and we want to make a post request so we want to create one and for this we need our body and we want to have it raw and in tracing now we have to name it like we name to be entered here so we have a name and this could be Thomas test and we also have a username you know this unique thing that we named and this could be Thomas one now we make this post request against VN API should we turn our user so you see it returned Thomas test user named Thomas one and it's ID six so if we do it again we should get a mistake because we username is already taupe and taken and you see it's internal server error I don't we go with Thomas - it should work and we have the ID 8 next thing that we want to do is we have our get request and this will just get all users or should get all users we send it you see we have here our array we have our 4 suppose just an example by me we have our Thomas test Thomas one and we have our Thomas 2 then what we also implemented was the find one so we have again our localhost 3000 and now we have said the next thing that is here is the ID so for this we could use the ID 8 because you know here we have our ID 8 we want to get this user and we send it and we get exactly just this user object next thing is we can say we want to delete one user this will be the exact same URL but it's a delete request and now we have one affected and OFIR again refresh this request via this should be disappear because it's deleted you see scan and now we can update for example this user we want to make put requests for to update it we use this and we can say it's user six and now we have to provide in the body the new user or the change user object and so we have the name here and the user name here and you see before refuse the six it was Thomas test and Thomas one and now we can just call it for example is a booster and we call it like please no one so we update user six and we should get ya affected one if we now refresh this this should be changed you see now it's Lisa mustard and user name is Lisa one but when ID stays the same so now that we've created to request you want can make it a little bit prettier and we can say we want to save it in a collection and we create a new collection for this and we can just name it block so it's very basic or we can say block postman request and we saved here we name it create user so it's here same talk postman request create user we also have here or get all users we can name it like this get all users we have our user find one find one user we have our delete delete one user and we have our update method or update request update one user so now we have saved them all to this folder and now we can make it we can export it and we will save it to our YouTube public into our block and now we can insert the collection here like you see and so it's an our git repository and if you download the repository you can just import them with posner and you can just run them and so you have them there so we exported it now you see here we have our postman block requests and I will create a new folder for this not inside the API and we named it postman and we can just do it so now at last we can commit everything and we give it a message like video - and what we did was create use a feature what you use a serve user service and use a controller edit postman requests and user entity and the user interface now we can push it so we have to set our upstream so I didn't do it you can do this and now we have finished our story but we can check again so we can again go to into my store you can say we use type or m and this is done we use cin server and set of promises already done we have our feature module user it's also done and we have our properties name user name ID and who used our get flower so what we can do now we can get into our code and we send get flow feature finish cost me are already another feature branch and what was we'll do now it will merge or better we base our features or our comet now we have to look what it's asking us so much this seems everything fine now you see what it did we said finish we switch to a branch develop we update our branch with our trainers with it and we merge our feature a video branch they can develop it was deleted locally and it was real deleted remote on origin and now we are back on develop so we can send it at us and we are back on develop now we can go into our get left folder and if we refresh the master there should be nothing changed but for trainers we have to go to our develop no we yeah so we didn't push we develop right now so we have to push it again or we have to push it the upstream now if you refresh it we have our master which should not be affected and we have here our developed branch where our postman has where our API has the source and should have our use a feature module and our user entity everything we did here so what we can do now to make it little bit clearer we are all read me so the first thing is you have to add your own database to run it and the second thing would be switch to brand develop to get the latest error the latest changes we can now add this again change with me and we also want to push it and now what we can do here with our project over here we can have like a merge request and we just push to develop and we can create a mercury class and normally someone else review it but we say we want to merge from develop into master we have our two commits here one off finishing our feature branch and the second one changing to read me we submit the merge request and now since I'm the only owner of the repository I can just accept it or I can merge it now it's checking to bear any merge conflicts and normally there should not be one and when it's merged so now we also have our trainers here all on the master branch so before we end this video let's have a look at what we will implement in the next video and here we will make a look-in endpoint for authentication with email and password and this will locate each password in our database is the same that we provide here as password and if it does so we will generate a trade WT with all user properties which will make off indicate us against our API we will also add a role property to the user so every user will have a role and we will protect some endpoints with Trey WT sum with Trey WT and with or will require a specific role so some routes can just be caught by an admin user or some just by an you normal user yeah so it will be more complicated in the next video so thank you for watching and see us in the next video
Info
Channel: Thomas Oliver
Views: 17,309
Rating: undefined out of 5
Keywords: javascript, typescript, nest, nestjs, nest.js, angular, typeorm, git, gitflow, node, blog, development, api, nestjs7, rxjs, observables
Id: Z6kw_aJHJLU
Channel Id: undefined
Length: 32min 59sec (1979 seconds)
Published: Thu May 07 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.