Run Seeders with Node JS and Sequelize - Rest API with NodeJS and MySQL (2020)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we're going to learn how to run database sheets with node.js and we're going to use sqlize as the oram so let's get started hey guys it's coreosum here and this is the part 11 of the series building a rest api with node.js and mysql so if you are new here make sure to subscribe and stay with the channel to follow the rest of the series and some really cool stuff is on the way some new courses and videos so make sure to subscribe and stay with the channel all right normally we use database sheets to insert some large set of data some bulk set of data to a database in a single operation for example if you want to insert some default data to a project when it is deployed and for example some default users so some default categories or things like that in order to use seats we gonna have to use some orm so we're gonna use sqlize in this video so if you've been following this course from the beginning we have done so much with sqlize so far we have connected to database we ran migrations and we implemented crud and so much so if you are interested the links are in the description down below all right so with sqlize today we're gonna insert some data to the categories table so if you've been following this video series we completed post module uh comments module and user module and as you noticed uh for creating post in the post payload as you can see there is a field called category id but we don't have categories yet but we do have a model uh called category and also we have a table for categories as well so now it's time to insert some data to this category table because we need categories to create a post just for now we just we insert the category id a dummy id for creating the post but we should we should have a category id in order to create a post successfully all right so now you know the practical use case of database seeds and let's create a seed and insert some data to the categories table all right so to install equalize on your system you just need to run this command npm install dash dash save and package name equalize just like this and once you install it you can initialize sqlize uh using c equalize init with this command all right so we have already done those two steps we have already installed and configured sqlize on our system we just need to create the cds file all right to create a sheet file you just need to run this command sqlize seed generate and name option i'm gonna name this seed since we are using the seed to populate category table so i'm gonna name this seed category seed all right hit enter and this will create a new file new seed file inside your cds directory let's open that file up and let's take a quick look at what's inside here we have two functions up and down and this up method is the one that getting called when you run the seat and this down method is the one that is getting called when you undo the seed all right just like migrations in the up function i have to write some code to insert categories to the categories table all right for that i'm going to write some function return and here we have a parameter called query interface i'm going to use that parameter and in that parameter object we have a method called bulk insert so with this function we can insert a bulk of data to a table as we want all right so the table name is categories and then the records the data we want to insert to the table all right i'm gonna i'm gonna create object and let me show you the table structure of my table here in categories table we just have these four columns id name created and updated so here we just need to uh insert some data to the name field these other values will be auto populated all right so um here so we have the name column so i'm gonna have a key with name and then the value i want i'm gonna enter the first category i want as uh not js i don't know you can use any data you want maybe view this and i'm just copy these values few times and react this and finally let's use something called um flutter all right so these are the categories i want to enter to my categories tables so the posts can be created with these categories all right so this is for inserting the categories now i i need to write some function to delete the categories for that i'm gonna write return here i'm gonna use this query interface object and here we have a method called bulk delete let me correct this type of bulk delete all right uh here uh as the first uh argument i'm gonna pass the categories the table name categories and as the second parameter we can pass some options to filter out some data but here i'm just keep this empty and as the third parameter i have passed null you can check sqlite's documentation so this is what they instruct to do all right so this seed file is complete we have few categories and now let's run the seed file and let's see we can insert data to run the seed i'm gonna type sqlize db seed if you want to run all the sheets at once we can type this all keyword and if you just need to run one specific seed file you just need to enter it like this seed then the seed file name i'm gonna copy this file path and i just have to remove this and this hit enter so it will run the sheet all right so as you can see in our table we have successfully inserted our categories to the categories all right and if you think you made a mistake you can always undo a seed to under a seed you can write sqlize bbc undo with this command you can undo the most recent seed you just you just did and if you want to undo all the sheets you can use this old keyword just like this and if you need to undo a specific sheet file you just need to use this seat option then the seat name let me just copy this seat name and just like this then you can use this command to undo a specific seed just like this all right let me run this seed again since we need categories in our table all right all right we have categories back in our database all right so that's pretty much it for the cds functionality let me close this terminal and let me open up this post controller now we can insert a legal category id here in our request as the category id but we need to validate that before saving it to the database we need to check if this category id is a valid id before saving this post all right so let's implement that really quick i'm gonna use the category model models dot category and here we have a method called find by pk so we can find the record using its primary key here so as the argument of this function i'm gonna pass the category id in the request body just like this so this function returns a promise and then i have to use this then keyword so i'm gonna pass and an anonymous function here just like this so if this function returns a successful promise then i can get that result here in this annual function so if this result is not null then which means that this category id actually represent a valid category in the categories table so we can run the rest of the code here inside this if block and else which means that category id is not a valid one then we can simply return an error i'm gonna use 400 because it's a bad request i'm just gonna remove this other key and for the message let's include something that makes sense invalid category all right so that's pretty much it let's test this on our api testing client i'm gonna open up the terminal and run my node.js application nbm start let's open up the api testing client and send this request save post request we get the success response and let's change the category id to a to something that we don't have in the categories table now now we get the error message we put all right we can do the same for update method here we can check cat if the category id exists before running this update function i'm just gonna copy this function here here i'm just gonna paste down this update query instead of the save code just like this we can test that too in the update request let me send this request and we get a success response and let me change this to a invalid id and let's send that again and we get the same error message all right so that's pretty much it for the category now before winding up this video i just need to do a little more change with this user id currently we are just hard coding this user id because by the time we implemented this functionality we didn't have any authentication mechanism so we didn't we didn't have any users so we just hard coded user id in this save method here what i'm gonna do is i'm gonna open up our middleware this check code middleware well if you are new to this course what we are doing here is we are checking if the user is authenticated using this middleware we implemented this middleware using this json web token package so basically here we check if the token is a valid token by using this verify method and if it is a valid token then we can decode that token and append that data to the request object so that's the basic functionality of this middleware you can check that video i'll link it in the description down below all right so here what i'm gonna do is i'm gonna use this request object and since we appended those decoded data then i can use this user data key just like this here in this object and the user id since we included our user id inside that token so we can access the user id and the user email as well inside this token so let me test this on our api testing client let's create a post we don't have any errors let's try with the valid category id yes we create a new post with the user id we just used here let me check that in the post table here in the post table let's check for latest records and this is the latest record we just created and here we have we have a null user id why is that let me check that again let's uh console log this and let's see if that we have the key let's send the request again and we get the response and let's oops the actually the key is not just id it's user id it's my mistake let me correct that in the code so the this key should be user id just like this now let's um create another post now we have included the user id correctly in our request all right so now let's do the same for the other methods as well i'm just gonna copy this value and in our update method we have a hard-coded user id and in our destroy method let's replace those hard coded values all right so guys today we learned how to implement database seeds and insert some bulk set of data which is categories to the categories table and also we added some improvements to our api by fixing those hard coded values alright so i hope you enjoyed this video hit the subscribe button if you haven't already and i'll see you guys very soon in the next [Music] video you
Info
Channel: coder awesome
Views: 11,334
Rating: undefined out of 5
Keywords: run seeders with node js, run seeds with node js, how to run seeders with node js, seeding a mysql database with sequelize, use seeds with node js, use seeders with node js, sequelize seeders, sequelize migration, sequelize seeder example, sequelize seeder file, nodejs seeder, nodejs seed project, nodejs bulk insert mysql, undo seeds sequelize, coder awesome, coderawesome, coderaweso.me, express rest api, nodejs and express with mysql, node js sequelize mysql tutorial
Id: OPqRXKLLM1I
Channel Id: undefined
Length: 17min 47sec (1067 seconds)
Published: Thu Nov 12 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.