Build a bullet proof REST API with Typescript, Express.js and Sequelize with Sqlite3 |CRUD REST API

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so welcome back to deliver coding in today's video we're going to learn typescript by billing to do crack operation with express.js sqlite and the sqlite3 and also i'm going to talk about the best practice with express and also how to improve the performance using the express validator and also this is what we're going to do for this in this video and so this is i'm sharing what i know everything's about typescript and everything is about express and to connect with sqlite all right i hope you enjoy it so let's get into the video all right so the first thing first that we're gonna do is we're going to set up typescript globalproject so here i'm using vs code so you can use any ticket that you want and i'm going to run the npm init y so this is going to generate the package.json for us and i'm going to use yawn to install typescript so you can use npm as well i'm going to install this as a dave dependency so type script and then we're going to install the ts node.dave so this one is just uh similar to nodemons for for javascript so whatever chain in our typescript file it's going to watch this that right now we have installed this one so i'm going to create a folder source right here that inside here i'm going to do server so we're going to do.ts and we work with typescript so right now i'm gonna do the consoles.log hello all right so to run this one and then i'm gonna go into script right here i'm going to delete this one um i'm going to do this one as a dave so we're going to do ts note dave and then we want to target the file so source right here of source slash server dot ts and right now you can do yawn run dave so this one is going to all right let's see i think i misspelled the one this one it should be server.ts all right so right now you can see we have the hello world but it's not watch so we want to whatever change in the ts file so it keep watching the file so to do that we want to run a command response right here so right now we can run again you're on dave this one it's going to watch whatever chain right here so right now we're changing hello to hello one two three so you can see the hello one two three down below all right it's great so the next thing is we want to set up the configs file and we want to build our typescript to javascript so when we want to deploy to production so before doing that so we need to create a type script config file so i'm going to run npx tsc.init so this is going to generate the typescript config file so as you can see there's a lot of things right here and you can read the comment right here what is say the target right here is like when we compile our typescript to javascript and which wasn't that well we want to target so for this i'm going to use the esnex so it's going to give me the latest versions and then we use common.js yes and right right now we're going to do the router so the route 33 right here is going to be it's going to tell where the typescript live so typescript leaf is in the root directory and the outer right here is where do we want to let's say compile that we want to compile the typescript to javascript where do we want to put it so in this case i'm going to put this in folder called dish right here and that's it so when we run a bill the typescript it's gonna go generate the folder dish and put everything inside that one and the last one is i'm gonna search this one real quick is modules uh modules resolutions this one right here this one is recommend for notes i'm just uncommon this one all right so i think that pretty much it for in our case but you can read the comment what it has to need to add if you're working with dom that's also need to slightly a little a little bit different but for now in our case we're working with express i think this is good so i'm going to close this one so we go back to package.json and we want to add some more command so we want to build so we just run the tsc command that's it and for the start so this is for the productions uh we can do just node we let's say we have the spec the output our javascript is going to be dish so when we deploy we want to use the javascript file instead of typescript file so we want to dish and source slash server dot js so right now if i were to run the command you want run bill so it's going to create a dish folder for us and inside the dash folder it's going to have source start server as you can see we have hello 123 right here and i guess that's pretty much it also when you can yawn run start so this is just a normal node.js it just run this one all right guys things uh that's it for this one is gonna be done setup typescript for our development server and the next thing is we're going to do is to set up the basic express server with tetris all right see you in the next part all right so in the previous part we have set up typescript already so in this part we're going to write the basic express server so first we need to install the express toolbar project so i'm going to go here i'm going to do yawn at express and then i'm gonna go back into our server.ts i can remove this one so we can import express from express so right now you can see we have an error right here so the reason is that we need to have the type folders modules so you can see we have they also suggest that we need to install this one for our project uh for so for some module it's need required for the type for it so in order to know that it required type or not you can come here so as you can see we have the express right here so if this one just show you ts so it so it means it doesn't need to install the type for it but it's just to show you dt right here it means we need to install the type for it so you can click right here it's going to show you the app type right here so we can copy this one and then we can add this one from yawn at the i'm going to bring this down a little bit and then so we can install that dave dependency so after we install this one the error should be gone all right so wait for a moment great so right now it should be gone so you can see and then we can just write about basic server express right here so we're going to do app.ok so when we go into the root right here and i request and respond so this one i should be half a type for this one uh it's okay to put it and it's it's it's uh it's okay not to put as well but right now we're working with typescript just give this one a type to this one i just get come from the typescript so um from the express right here so we can do here and then we have the respond great now we can put it right here this is going to be type respond and then we're going to do just return the rest then hello great and then we going to have a port so the port is going to be i'm going to run on port 9000 and then we're going to do apt dot listens on port and which is console.log that's where it's running let's say we do server is running on port 3000 uh plus port right here so it's going to be port 9000 all right so then i'm going to go here i'm going to do yawn run dave so this one is going to run development mode so you can see right now our server is running port 9000 and then we can go here go to localhost thousands and there you go so right now we can see our hello world so that's pretty much it so then i can click come here so then we are done set up express basic express server next up we're gonna set up a database and then we're going to create the model so all right see you guys in the next part all right so in the previous part we have set up basic express server so in this part we're going to set up a database and create a model for it so before doing that you need to install the sqlite right here so as you can see uh for the sqlite package it comes with the typescript so you don't need to install the type for this one so please go ahead and install this one i have already done that and also we need to install the sqlite three as well so since we working with the this database all right so you have to install both of that so come back here and then i'm going to create uh the folder right here in source i'm going to go config and inside the config right here i'm going to create a database dot config dot ts to make sure that ts are we seems to be working with typescript so i'm going to do import right here oops not this one all right so i'm gonna do import from sqlite uh notebook not sqlite it's gonna be sqli and now we're gonna get the sqlite right here and now we can do cons db is equal to new sqli and then we have some option right here so i'm going to give a database name let's say called apps and then we have this this one so this one is going to be past username and password user name this one is going to be password so since we're working with the sqlite so we don't need this one so if you are working with let's say mysql or postgres so we need to add this one so for now i'm just i'm going to clear this one i'm just empty string and then we're going to have some option right here so the option right here is we're going to let's say our storage are we going to use the passover storage so since we're using with the sqlite so we're going to do database dot sqli and we're going to do the dialect right here we're going to choose to use sqlite so as you can see right here we have some options that you can use so this is all the database that you can do so i'm shooting as your light and also for the logging for this i'm going to do fall so the logging right here just show you the logs of the database so which we don't want to see it so then i'm going to do export defaults bb all right that's it and right now where the database is done so we can go back in here so we can import that so i'm going to import db from config slash database config right here so what i can do in this one just do the db dot thing and then do then after it's done we can do just console.log let's say i connect to database so if you working with uh postgrade it might be a little bit different so you need to do the db authenticate uh db authenticate right here so you need to provide the password and the username it's going to go into here so you can re check document for more of that so right now this should be done and right now you can see we have the database right here so if we set ups correctly you would have the database with sqlite right here as well all right cool so right now we are done set up a database so in the next part we're going to set up the creator model for to do to do so since we're working with corrupts to do so the next part we're going to create the motor for it all right so see you guys in the next part all right all right on this part we're going to create a model for our to-do so to do that i'm going to create now in another folder from model and inside here i'm going to do the index.ts for our model all right so right in here i'm going to import model from sqlite all right so and then we can just to create the class so to do instance i'm going to call this call to do instance we're going to extend from the model class right from the model right here and for this one i'm just i want to give the model attribute for this one as well so for that i'm going to create an interface for our to do so let's say to do attribute tribute right here uh and then we can have uh so you know what to do we're gonna have an id that is gonna be a string so this is the type string and then we're gonna have the title our title is gonna be type string as well and also we going to have the completed that the completed is going to be boolean types great and then we can just give the type uh this one to in here so to do a tribute so i can just copy this one and there you go so right now we have our model of extended moda so we can uh initial of a to-do instance so we can do to do instant.init right here so and right now you can when we're doing this it's going to give us intelligence of what is attribute of the our to do so we're going to have an id so as you can see right now we have some errors so because we need to give some option for our indeed as well so right now the error is gone so right now we just give the types of forward id right here so we can do the type for this one it's gonna be data types so the data type right here is going to import from the sqlite as well dot strings to do this one and then all right let's see why we have some errors i'm missing scope string all right i do this one and then i'm going to do the primary key it's going to be true but this one is allowed no to fall and then next is we're going to have the title and the title is going to be types it is going to be the string as well so i'm going to copy the same things from here and the allowed null to be fall as well great and also we're going to have a completed this is our last property it's going to be type for this one it's going to be a data type is going to be the boolean value and for also a lot north to 4 and we want to have set up the default value as well so it's going to be default value is going to be full so when you create the to do it's going to be false yeah so i think that's pretty much it so in here we have to connect to the database so it's going to be sqlite so we're going to use the sqlite right here so we can import our db um right here so you can see we have a importable db and also we need to give the oops i'm gonna give the table names so the table name we can just call to do's right here and then we can export the class right here so we can use it to create to do all right i think that's pretty much it for our model to do and so we are done with this one in the next part we are going to create a route in our express route and then and now we are going to use postman to post the uh object what to do so then we can create the to do all right so see you guys in the next part all right so in the previous part we have to create a model so in this part we're going to create the to-do route so that we can create to do so to do that i'm going to go back into the server.js so right here so we don't need this one so i'm going to remove this one so and then we're going to create the post request so for this one i'm going to do a post so when we want to create something so we will use the post method so and i'm going to call this one create so you can call this whatever you want and and then i'm going to give the type of this one it's going to be request and this one is going to be respond and then we so when we post this one so we're going to have the body that from the client they're going to post the objects for that so we can read that from request.body so for now i'm just console.log the and for this i'm going to just return rest.send none so rest empty so when we post this one just we want to see this one so to do that i'm using postman right here so we can go into http so which we're going to go into port 9000 and we're going to slash create and we're going to post the body and so this is our body that we're gonna do uh as a type of json and then we're gonna post this one so when we hit send to this one we should be able to see uh those objects so as you can see right here we have undefined so the reason that we have undefined so we need to use express middleware so to read those body so you can do pather body pathway but right now you can do this with express excel so you can do the apps.use oops express.jsons and so for this one as when you post again so i'm going to post this one again or we go back in here so as you can see right now we have this object right here all right that's great um right now what i wanted to do so seems reposing this one so we want to generate an id for this one so to do that we're going to install the uuid for our id so it's going to be uuid right here all right so you can install this one and also we need to install the type for this one as well so you can see we have uh it's not building with the uh ts type script so we need to add a tab for this one as well i'm gonna add on uid and then also right here i'm gonna use the version 4. i'm going to copy here i'm going to import right here so right now it's going to fail because we need to install the type for it so we click here it's going to show us the type for for this one and you can add this one as well yawn at uh as the dave dependency so right now the error should be gone so we need to generate the id for alongside with the request.body and also for that we need to change slightly a little bit different so for our model so go back into a model instead of type string right here we're gonna type uuid version four so this one is uh it needs to be much uh within using uuid version four so we need to do this one great and we go back in here so we have request.body i can do cons body is going to be equal to uidv4 and to create it to do it actually really simple is we're going to um no we're going to import our to-do instant that we just create and now we just do that create right here and then we pass the object that we want to create so the object is going to be so i'm going to spread from the request dot body so it's going to be object so then we can like spread this one and then we just add the id to this one as well so it is going to be the async's weight so it's going to give us promise so i'm going to use a single weight right here and then when it's done it's going to return us the record and then we can just send the record right here so right here i'm going to do the jsons and then for this json we're going to have the record and also i'm going to give the message for this one it's going to say successfully create to do and also if you want to catch an error so it's going to be really simple is we need to do let's say try and catch right here so potential errors can be right here so it's maybe like some our to do like our internal error is going to be has some problem so we can do return rest.json returns rest.json so let's say the message is going to be bales to create and then we're going to go with the status but this one is going to be 500 and 5000 and then also we won't have the route for this one so it's easy for our developer to know that which route is uh happened like which one routers get an error all right so right now we should uh see this one so let's actually try to create it so go back in here i'm gonna hit that right here so as you can see it's successfully created to do so we successfully create it and if you want to look into the database so right here we can view the database as well so i can click in here i can open the database so as you can see below right here we have this uh sqlite explorer and then you can click in here and you can see we have our traduce that we just tabled and we click in here as you can see we have our id and we have task and complete work here perfect if you want to see this one as well you need to install the extensions called the actual light right here so this one does allow us to view the database great um so for now we are done create so i'm gonna handle for this one a little bit so we will do that in the next part so right now as you can see when we create in this one this uh we need to validate the object that being passed so if we look at our model we have this attribute right we have the title and complete completed and the id right here and so when we when we pass this object to our create create route so we need to validate that we need to make sure that this need to pass this right property let's say if i were to change this test right here and i hit send it's going to have some error right here so it it's fine so right now we handle an error but the drawback of redoing this one without doing the extra validate on this one is that when we send the wrong or like you know invalid uh objects you have we have to hit the database first to check if it's error or not so it's not really good it's not good for performance the reason is that let's say you have the we don't want to it to hit the database right so we pre run it well before accessing the database first so that way we can boost uh our formant a little bit all right so for that we will do that in the next video in the next part so all right so see you guys in the next part alright so in the previous part i have mentioned that we need to have the way to validate an object that being passed rather and so that we can pre-run the error before hitting the database first so to do that we need to install uh use the dependency it call express validator you have to at this point i have done that and as you can see this one billing with typescript so we we don't need to install the type for this one as well all right cool um so for that i'm going to create a folder another folder right here i'm going to call this one wally daters and then i'm going to call the index.ps and so for this i'm going to create a class for it so i'm going to class wallet data so let's say we're working with to do so i'm going to call to do validator and then we're gonna have some the function for this one it's gonna be i'm gonna call this one checked to do check create to do check create to do and then we have the function so this is going to return the array so the array of checks so we need to use the but so as you can see we have to check the body object that being passed it whether it's right prop or like attribute for our to-do instead or not so to do that we use the body so this body is come from the express validator so it also has a property if you want to check the param it also has param and also it has a query as well if you want to check the query params so right now we're interested in the body so we want to check so all right so let's see what the property what is the attribute of our to do so it's going to be this three value right here right so the main things that we want to have is title so you need to pass the title the id can be optional and the completed right here can be optional as well so you can either pass the complete or not it's up to you it's up to you and also for the id as well so let me show you here so right now i'm going to remove the complete and then i'm going to change this one back to the correct attribute it's going to be titled i'm going to do the title 2. so when we hit send so as you can see it's okay as well so you mean to complete it it's going to be optional and the title right here is going to be required so for that we can check this one that the title so we can say the title is should be not empty so we need to then this one like hey uh it's not empty you need to provide and if they don't if they do not provide us we're gonna give them the error message let's say the title value should be and it should not should not be empty so this one and then also we need to check it i think that pretty much it i guess all right for this one and for now i'm going to show you what does it look like so we're going to use this one and also we need to use the middleware to handle an error so all right so i'm going to export this one so let's see export defaults i'm going to do new to do validator so i'm going to export as the object of this class so that we don't have to initiate the new object and right here so in here i can do the to-do validator so i need to import this one first import to do while the data from quality data cool and then inside the validator right here so this is not a middleware it actually like they checked but this one so we need to call creatordo as a function so it's going to return the array for us so as you can see we need to put this one in an array all right so right now i'm going to go back in here so right now we need to so as you can see when before we're hitting this route it's going to go into right here and it's going to check whether they're going to provide the right property or not if it's if it doesn't provide the right gut let's say the property so we're going to handle an arrow right here so this one is going to be the just the middleware so all right so let's see so this is another middleware for this one and also i'm going to keep the type for this one it's going to be request and this one is going to be the response and for the next right here is going to be the next function cool and then we can check if we can count the errors is going to be called validate the validation result is from the request right here so this validation right here a result right here is going to be come from the express validator as well and then we go we're going to have this one so we can check if it's not empty so let's say if uh it's not is empty i think this empty right here if it's not empty it means it has an error so we can just return rest.json so i'm going to send the entire thing right here the error right here and l if it's okay i can just do the next function so it means right here if it have an error we're going to throw an arrow to the front end developer to check it and right now if it's okay we're going to go next it's going to go right here cool so right now i'm going to change this one to test instead when we hit send this one so you can see right now we have this one that the the value should be not empty because we didn't provide this one so right now if i provide a right one and i hit send right now you can see it's okay great so the next thing is we need to validate the complete as well so it's it's optional but when if they want to pass it so we need to check it as well so let's say when they have to complete it so to complete it the value cannot be true so when you want to create it to do it cannot be true right so it it should be false so that we need to check this one as well and also the id it needs to be the uuid v4 it's going to be optional as well so for now we need to validate this complete first so go back in here i can add more property say body completed complete it make sure that you spell it correctly uh completed and then we do is like um optional we're gonna pass the option off for this one and if they pass this one it's not this should be bullied and then we're gonna split the message if they do not pass the boolean which we give them the message the value should be uh boolean then also when they pass the boolean and also we need to check the boolean it should be the uh this could be it's need to be false so that is in so it's going to be array 0 of 1 and zero or four and then you need to if they do not pass this value we're gonna go with the message right here the value should be should be zero or false all right that's it and also we need to check for the id as well but an id if they pass we can do like if it's optional and also for this one is uuid so we're gonna pass the version of this one since we're using version four so we need to pass the version four and then also we need to give them the message right here the value should be um do you id v4 all right i think that's pretty much it so right now we go back and i uh set crit try to create a do with the value or to complete it right here true it's gonna yell at us that hey this one should be either 0 or 4 so i'm going to change this one to zero but for now i'm going to check this string first we hit send as right now you can see we have this right here the arrow right here great um also right now i'm gonna change this one to zero so it's mean fall so i'm gonna change this one to task four we hit send so right now everything is great we uh be able to create to do and also id is gonna work the same way so let's see yeah for the id you're just passing the uuid it's gonna work so i don't have that so i'll leave that to you all right so right now we'd be able to validate this one and all right great great um but right now it's going to be a little bit messy for this part so in the next part before going to read to do with pagination i am going to clean this up a little bit so we don't want to put everything inside the server file right here we're going to separate this one into separate folder and all right i think um that's pretty much it for this part so see you guys in the next part all right so in this part i'm going to clean up this a little bit so it's kind of right now you can see in this route there's a lot of things right here so we should be separate this one this one is going to be reusable into for the other route as well so for that i'm going to create the folder right here called the middleware so this is our middleware and i'm going to go let's say validation to do let's say handle validations the handle handle validations if i cannot spell it validation.ts and in here i'm going to create a class for this one called middleware and then we're going to create a folder actually oh it's not called this one it should be index.ts right so because we put every middleware into this class so it's just the index of ts is great so for this we can give a function called handle validations validation errors and there you go and go back to server i'm going to cut this one right here and paste it all right so right now it's a little but we need to pass this one to this one and we're not using the all right cool cool let's see what cell do we need to change so we need to in uh get this one from the express so for that i can do import request so first i'm going to do express so it's going to give us the intellisense so let's do here request response respond and then next functions great and for the validation result here we need to import it from here and then i'm going to remove it from the this one so we don't need this one all right great so right now we have our middleware and the next thing is what we wanted to do is we want to export this one it's about the full new middleware so same thing export the objects of middleware so right here i can just import middleware front form it's from it's gonna go into the middleware and now we can do just middleware right here dot and validation error so it's not an it's not we not call this one that's why the middleware great so for this i'm gonna we'll you can create a controller folder for this one as well so for that we will do that in the next part uh maybe in the by the end of the video for now we going to go back to over here we're going to agree to do with tarjanation the first we just read to do first so right now we are done with this one close this one so in order to read the data so from our server we will use the get request so i'm going to call this one read and it's going to be the same thing i'm going to copy this guy right here so we don't want to retype it again right so right now i'm going to close this one so we right now i just i'm going to read from the database and then send it to the um to the to the client so i'm going to try and catch so similar thing i'm going to copy this one as well try right here everything from here i'm going to remove this one and for this one it's going to route is going to be read if there are some any errors fail to read this one and then for the try right here what we can do is try to read to do the instant dot let's say find all and then we're gonna do where right here and then if you want to get everything you just pass the empty right here it's gonna be a promise we need to wait this one and so we're gonna get all the records for this one all right cool so now we just returned rest adjacent and then we can pass the record so that we have read great so that's pretty much it for the read and then we go back in here i'm going to create another one and then i want to copy from here from this one or the get request make sure that you select the get one now post and then chain right here to read as well and when you hit send it's gonna send uh get us all the record so this one is great it worked but imagine if you have a database that have the record for 10 000 records right so you don't want to get like you know appear both uh allow people to actually query those ten thousand record with just one request it's gonna be slow down the network you have to crawl all your databases performance is really bad for this one so for that we will do the passion agent we're gonna give them let's say um right now we're gonna do the limit right here we can pass the limit let's say be able to let them how many record that they can get and then yeah so that's what we will do in the next part so for now i'm going to end this part right here all right so see you guys in the next part all right so in this part we're going to start our pagination so in order to do that so i have mentioned you in the previous part we're going to pass the limit right here so the limit right here for in my case i'm going to let people's query the maximum they can get is 10 so they can go below 10 or like below 10 from 1 to 10 but it's about 10 i will not give them so that's what i'm gonna do so for that we need to read this value so for now i'm going to read this value first so in order to do that we can let's see so we can count query so this one is going to be the limit i'm going to get the value is going to be request.query and then we can see the value of this one right for now i'm gonna cancel for you guys to see that so the limit right here oops i cannot spell it limit and right now when i pass this limit i hit send so all right so it works oh okay this one is wrong because before this one it should be the question marks when i hit send it's working fine if we go and check at this one uh you can be able to see the number 10 right here perfect so right now we can use this one so we can pass this one to the limit right here so this one has the limit or like property that we can limit our request so i'm gonna do limit right here so right now we have some error right here because the type of this limit it should be the number either number undefined but from the request of query we have to type uh this type right here so let's see if you hold one this one it can be string or something like that so we need to cast this one to either number or it should be the um undefined so we have this one as number or undefined so we can do all undefined right here i cannot spell it all right let's see is there something wrong with this one great so we then i i will not destructure this one and i will do the dot limit right here great and then i will do question marks well in case if they did not pass anything right here so right now um we be able to do this one so and as you can see right now we have the test four tests right so i'm gonna limit so let's say we'll do limit two so we're gonna send them only two tasks only all right perfect that works uh and the next thing is we wanted to do is to validate this one right so we need to make sure that people need to pass this limit as a number and also in my case i want to limit them only the value from one to ten so for that i'm going to go into our validator class right here and then i'm going to do right below this i'm going to create another funding function called checks read to do um this one and then also we're going to return so this time return an array so this time instead of the body for we're going to check the query instead so this is uh the query parameter but are we going to need to pass the limit so when they go into that route this one need to be up they need to be passed required so this one is be not empty all right let's see why this did not give me all right as you can see we need to get a query from here not from the express and should give me intellisense is not empty and then we if they do not pass this one we need to pass we need to tell them uh the query let's say limit should not be empty so you can give any message you want so should be not empty and when they pass the value for us it should be is numeric uh ashing it's not number is in is integer it should be this integer and also we need to i have an option right here so the min uh the minimum of this one it should be one and the maximum should be 10 so if you want to let's say increase the value let's say a 20 is up to you right here and if they do not pass right here we tell them like the value the limit value should be twins should be in the years it should be number right now and between 1 to 10. great so right now we uh check this one now we go back into our server.ts and we can insert it right here between right here we can do um to do validator dot check to do check read to do and also we need to handle an error for that one so we can do just the middleware dot handle arrow right here perfect so right now when i whenever i don't pass this limit right here i hit send so this one should be uh this one i should just show them like the message but in this case uh the error right here should be uh is an array uh we gonna make life a lot easier for front end so i'm gonna send them the first error only so for that i'm going to go to middleware i'm going to do dot array and then gonna send the first error for them so when we hit send so hey the limit should be not empty so right now when they pass the limit right here let's say they pass as a string hello and they hit send again and they can see it say the value hello something like that should be number so and then when i pass the 20 it's gonna fail because it should be between one and ten so if i pass nine right here it's gonna work all right perfect so right now we done this one but in order to complete the pagination we want to get the offset of it right so let's say i want to get the two i'll limit only two so i'll get the test one and two so and then the next thing is we need to let's say get the another two that after this two right here right so we need to pass the offset of this one that i want another two after the first two so that's what we will do so we will get the next um let's say you hit the next page you will be able to get the task three and four so for that we were working on the offset so you need to provide us the offset right here all right so let's add the offset two so this one and then we need to check this one and also write some code to make sure that it worked so this one is going to be exactly the same for this one instead but we're going to change this one to offset instead of limit and then we're going to pass the offset right here as well but not order it's going to be offset and right now as you can see right when i do this one when i hit send as you can see we have the three and four so this one is like uh offset two i like after the first two record give me another two record so if you can do like after the first two give me like the first three it's also fine as well but since we have only four records so that's why we have this one so i'm going to create another record right here so right now i'm saying limit three right here as you can see right now after the first two give me uh another three so this one we have task three and test five but uh lastly we need to check the offset right here as well so right now you can pause the video and try to do this on your own so we're gonna go to offset and offset is should be optional like you know it's you can't either have this one or not it's up to you but when you pass it should be is numerix it is it should be number that you have to pack this one so we need to give them the message let's say the value should be um let's say the value should be number so right now when i pass the offset let's say the string and then i hit send so right now as you can see we have failed this one and also you can absolutely pass any number you want but this one is gonna return empty so that's gonna be fine we don't have to handle this one all right so i think that pretty much it for the retweet paginations and then we will move on next so this one is going to be really easy as well so i will do this one now so in the next part we will do the update to do so going back to the over server we will do the read with the um id so i'm gonna uh this one i'm gonna paste this one it's same thing but instead of passing the query we're going to pass the with the id for this one as you can see you have the route and then you give us the id and i'm going to remove this one so i'm going to remove this one as well and fail to read and then this one we need to give them a path right here slash id great so what we wanted to do in this part is to query where so we need to read the param instead so cons id is going to be equal to request params so this one is param and the uh this one the question mark and the at the end sign right here is called query right now we have the id param so we want to query this one we want to await to do instant dot fine one where so it's gonna be where it's gonna be pass an id so you can see it's give us intelligent right here so we find the id so we give them the id right here that's great and then we're gonna have the record and then we can do just send a record for them um return rest adjacent and then we can pass the record great um alright so i think that pretty much it so right now we're going to go into this one i'm going to copy i'm going to create this let's say we're going to read from to do so wheat and then we pass the uh let's say the id so right now i'm gonna go into offset i'm gonna get the id from this one and we just read this one so this one is task two if it shows task two right here it mean it working fine great so you can see right now it's be able to read this one with an id perfect um so the next thing is we need to validate this id so whatever they pass in here it should be like wallet things so to do that it's the same similar so for this one we i think we're going to reuse this one so we're going to do this one exactly the same for the program for the update as well so for that i'm going to create a function that can be reusable the name for that i'm going to call it say checks let's say id params and then for this one it's going to return so right now we're working with param so we go to get a param from express validator and our param right here is going to be call id and for an id so we need to say it should be not empty first of all it should be not empty you have to pass this one and with the message the value should be not empty and also when they pass it should be is uuid and we need to pass the version of this one as well version 4 for this and then we will uh if they did not put it correctly we can show them the message let's say hey um value should be uid v4 perfect and go back into here same things so it's going to be to do all your data and then we're going to check rams and then we're going to have the middleware to handle the validation error perfect so right now when i go back in here when i say uh this one is not a valid ui dv4 do i hit send it's going to say this is not a valid id so but if they do let's see right here great let's say what if i provided wrong so it's going to be null so it's fine so because we do not have any uh this id right here so let's say if i were to pass an empty right here what it should be so if it's empty so it's gonna go back into this route right here so it's get it's fine first as well so if they do not pass any parameter it means they want to read so that if they want to read they need to have the limit query so i guess that's pretty much it for our reads right here so right now we are done with our required id as well so in the next part we gonna focus on the update to do and if it's fast enough we will do the delete as well all right so see you guys in the next part all right so in this part we're gonna update what to do so to do that i'm gonna go back in here our server.js so i'm gonna uh get the put method so in order to update this one so we need to use the put method so i'm gonna call this one update i'm going to copy a little something from here right so it's going to be a little similar and then we can remove this one and also for this one is going to be update so when we update so we need to pass an id so which one which um up which one that we want to update which to do so in this one we can get a param same things it's going to be id it's going to be equal to request.params and then first we need to find if that to do it exists or not so to do that i'm going to let's take times it's going to be exactly the same as this one right so as you can see we need to find one we will get the record i'm going to copy this one so after this we can check if not record if not record we just returned rest.json that let's say the message is going to be cannot find existing record and that's it and also if it's half it so if the record is exist what we can do is just await record dot update and then what value that we want to update so what we want to update is the completed value so it's going to be update completed right here and then the value should be come from the recorded cells it's going to be the record right here dot get value uh and then we just pass the complete right here so we will get the value from here and then we do the question mark right here so this one is be able to toggle the value uh completed so if this complete first it's fall so when the update is gonna be true so it's true it's gonna go back into full and then we can have the update record so it's going to be updated record right here and then we can do the return rest.json updated right click updated right so right now we should uh be able to see uh we should be able to update our record so go back in here i'm going to all right all right so all right so i'm going to go back in here i'm going to create another request so this one should be put request instead of the uh get request so it's going to be exactly the same with this one i'm going to copy this one not exactly the same update and then we're going to pass our id so id and let's see i'm gonna update the task two right here to true so i'm gonna copy the id and then i pass this one and i hit send this should be able to update the id to the true right here so if i were to send again they should toggle to four all right perfect and the next thing is we want to validate this one so we have already done that before so we can do just to let's say to do validator that checks id params and also we just put a middleware dot handle those error and that's done so right now let's say if i were to put the wrong id it should show us that the id if the recorders do not exist if i do not invalid id it say hey this one is not valid all right so perfect so it's done so right now we are done updated to do and so in the next part we will do the delete all right so see you in the next part all right so in this part we're going to delete our to-do so that is going to be really simple so i'm going to copy from the update right here so basically it's going to be do almost exactly the same as this one so instead of doing the update we're going to do delete and also for the method we're going to do is delete as well if you want to delete you need to use this method and basically the same thing it's just going to hey get the params check the param as well and also this one is going to be not up this it's just this one right here instead of update we're gonna call this one destroy so we're gonna destroy this record and let's say this one we got the deleted deleted record and then we can send this one right perfect and that's it and for we have if we have some error one thing that we can do we can do the delete right here this route right that's pretty much it so this one is really simple um let's see so right now instead of put right here so i can copy this one and go on to here and then i choose the method so this one is going to be delete and we hit send right now you can see we have some errors so right all right let's see because we putting the wrong route so it should be deleted and as you can see right now the value should be idv4 so right now i'm going to get the idv4 so because we're putting the wrong id forward delete for this that's good so right now we don't hit the database so we just hit from the validator right now you can see our task 2 is being deleted if we check all the record right here if we run right here as you can see we have the task 2 is gone as well all right i think that pretty much it for this so we right now we have completed our crop our crud operations create weed to do we have handles and error um so and the last things that i wanted to do is probably clean this one a little bit we want to put this one into another folder so for that i can create another folder called controller and i want to do the index of ts right here so this one let's say class name to do controller to do controller and then we have the functions right here let's say read for this one that's going to be read to do controller.create and update and something like that so i can put right now to create right here so we can cut i can cut this one and i'm going to paste it right here so we're gonna change this one a little bit it's gonna be read all right so i need to remove some things from here let's see all right cool and now we need to import all of the uh and put everything into this place so instead of this place so let's see we have the middleware we have this one that should be gone right should be coming here let's see all right perfect uh this one should be go out one directory right great so let's see why we still have this area right here so because we need to pass the type for this one let's see and then this should be gone i'm gonna remove the next functions right here we don't we don't need to use it right here because it's not a middleware and also this one need to be async functions so a things great so right now we are done with this one we can create uh three more functions here right here and this one is going to be called the update rate with id read with passionate recognition you can call this name whatever you want but i like i think like one of the hardest things about coaching is like you know giving the right names um let's see this one is not the reader it's gonna be create this read with paginations with um id put it by id and also this one is going to be update and the last one is going to be delete so i'm not putting everything the functionality just yet i'm gonna put the name for this one first and for the read with paginations we can come back here first i'm gonna remove this one oh we need to import this one in here it's that i'm sorry so let's bring back this one right here and put it right here great and now we can hear what you do oh we i'm not export this one just yet uh let's see all right perfect i'm gonna go at the bottom right here i'm gonna do export default new to do controller and then inside here i can do the to-do controller so i need to import it first so import to do controller from controller perfect um all right so and then i'm gonna go back here to do controller.create so this is um right here perfect so this one is we read this one with pagination so one thing that i can do i can cut this one and go back in here read with pagination so i can remove this one and yeah right that's correct so then i can remove this one as well and we can do to do controller dot um read with passionations and this one is gonna be read by id so let's see i'm gonna cut this one and then we'll paste this one right here right with by id all right so we're going to do to do controller dot read by id all right and then for the put um so i'm gonna cut this one go into the update right here remove paste this one and also the last one is gonna be the delete all right cut this one also this one as well i make sure that everything is working fine great and then we can just remove this one and we can call to do controller dot this one is going to be delete and also this one is going to be to do controller dot read um read write dot update all right perfect so right now we clean up this one so let's test it out so let's actually create this one i'm going to create attack uh task six as you can see it's working fine so let's do our pagination it working fine we read by id so let's say i'm going to get this id right here all right with it with the id i'm going to paste paste the id right here so you can see we be able to read with id and also i'm going to update this one right here i'm going to remove this id paste this id in remove perfect and also last one we're going to delete this one all right cool so right now we be able to do all of this so i'm gonna um say something about this one so as you can see right here i put the status right here instead of the status right here you can put the status right here as well but if you are working with react if you put the status right here so let's say 5 500 um it's going to refuse it's going to get cannot get the objects that fail right here and react because it need to have 200 so that's why i put the status right here instead of putting right instead of in this one so i guess that's pretty much it so we we can do like uh more into this one like we can remove this one out of the server.js completely by creating the route for this one so for that i can do the route right here and inside the route i can do index.ts and we can do the router like so this one import express from express right here and then we can do con router it's going to be express dot router and then you can copy everything from here so you cut this one out and then you put it right here and then you instead of this app right here you can change this one to router right and then you need to import everything inside this one so this three right here should be coming here so it's going to be the folder right here great all right this one should be router all right this one should the import right here is wrong because we need to put this on about perfect and then we need to export default router and then we can use this router right here so we can do app.use um router that we have so and this one if you working with to do you can do actually like say to do router and if you like you can pass to put the pass before it as well let's say you work with the api you work for a bill and api you can do api version one so this one is looking really good so let's come back here when you create you need just to let's say uh put the api v1 inside this one api slash v1 and you can create this one as well so it's going to be exactly the same thing all right so i think that pretty much it um so last one i'm going to talk about the folder structure a little bit so right now you can see everything you should put index.js so for that i'm going to make this one let's see i'm going to put let's say we work with to do i'm going to put to do right here so i'm going to put every folder inside the to-do right here so i'm going to save this one first and then we're going to put our controller so the controller right here is for to do as well it's going to change some things so make sure that ladder auto change for you so it's going to be easier and for the model i'm going to put inside to do as well hit save save save this one and i think that pretty much it right the validator it could be the thing i'm calling this one to do while a data i'm going to put this one in to do as well all right great so we hit save this one so everything should working fine right now all right so our motor so we have some error right here so let's see what error that we have let's see our model let's see when let's try to create another this one all right so we have some error right here so let's see what is the arrow that we have to do rather it's fine and then we have the this one right here this one i should be not out one two folder right so the middle oh to middleware as two folders great so let's see where do i use the moda so let's see we have the error right here so as you can see we inside cannot find the modules inside the controller so the controller right here we cannot find the model all right i don't know why it should be because the model is in right here should be out one folder it's going to go to motor it's working should working fine but let's see we need to rerun this one again yeah as you can see it's working fine so let's create that to do all right that's it so right now we are done all right so for the folder structure right here um there's no right or wrong way to structure your folder so this is probably my way of structure things so it's not the right way to do it so this might i just feel right to do this one if you doesn't feel right to do it just don't do it do it your own way and so yeah that's how you structure your folder you need to be able to you you doesn't you have to you need to change it all right so in order to like you know make it feel right for you and that is the right folder structure so all right so the last one is to just to make sure that you you don't forget to build our typescript to into our javascript so as you can see our production script right here is gonna taking from the this folder right here because right now you can see we have only this one so for now what i can do is i'm gonna do yawn run build so this one is going to compile our everything inside here is going to go into this one so as you can see we have uh this one and then i'm going to start the server and let's start our production and do your run start and it should be working fine and when you post to create a test let's say task number 10 and as you can see great so it's working fine yeah i guess that's pretty much it for this video so we been doing a lot of this one and all right i hope you learned something from this one i hope you enjoyed this one and thank you you guys for watching until this end of the video and i hope you have a good day alright see you in the next video peace bye bye
Info
Channel: Daily Web Coding
Views: 23,342
Rating: undefined out of 5
Keywords: sequelize, create a nodejs rest api with express and sequelize, nodejs and express with mysql, sequelize tutorial, how to build a rest api with node.js, how to build a rest api with mysql, sequelize & express, sequelize orm, node.js express and mysql, node.js sequelize, typescript, node typescript, typescript setup with node & express, typescript with nodejs, typescript rest api, typescript nodejs, typescript node, typescript tutorial, typescript express, typescript node express
Id: yFgrSJGNj0E
Channel Id: undefined
Length: 72min 29sec (4349 seconds)
Published: Wed Apr 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.