Sequelize ORM Tutorial (all in one video)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello class in this video we're gonna be learning how to sqlize our database and i mean how to use sqlize to access our database and i mean it's not my fault that they called it sqlize guys it's a verb it should be either to sqlize or sqlizer they should rename it anyway so we're going to learn how to use sqlize and what's equalized is basically is an orm uh what that is is an object relational mapper which essentially is just a library that lets us connect to our database and perform operations without having to write raw sql queries it abstracts away all of the raw sql stuff and gives us some javascript functionality to work with which is pretty cool it makes it easier to interact with our database models uh tables as objects instead of just just having to map all of that ourselves so like i said sql um sorry sqlize is sql based so it works with all the sql based databases and i'm going to be using postgres but uh feel free to use my sql uh if you prefer or anything else that's sql based before we start please do me a favor and hit the like button i'm trying to grow the channel and subscribe and hit the bell button if you haven't done that yet so let's actually get to the code [Music] all right so in the sqlizer.org website let's go to v6 documentation and here let's go to getting started and here it tells us to install sqlize and whatever database driver that we need i'm using postgres so i'll need these two so i'm just going to copy them and let's head over to vs code i've already created the folder called sqlize i'm um inside of it with vs code so here i'm going to create an entry file i'll call this app app.js and here let's install our dependencies so open the terminal again i'll say npm install sqlize and those two things pg and pgh store and there are a couple of things how to set up our connection if you go back to the getting started guide it can tell you how to create that instance yourself and do all of that but we can also use something called the the sqlite cli which is pretty cool i'll show you so back in vs code in our terminal let's install uh this dependency and let's install it globally because we're going to need it uh later so let's say npm install g sqlize cli hit enter and let it install and this is a nice tool that lets us do so many things with sqlize so the first thing that we're going to do is we're going to say sqlize init which is going to initialize sqlize in our project so it's going to create a bunch of stuff first thing let's go top to bottom we have config config.json which holds a couple of database connections so there's the development one the test one and the production one and of course we're in development so let's populate this one for me the username is classed for our for my postgres and my password is root now fill them with what matches your credentials and for the database let's create a database and let's call it sqlize underscore db be careful in sql dash doesn't work so it has to be an underscore and the host that's fine and the dialect i'm going to change that to postgres now if you're using something other than than that you can look at these and use whatever that is relevant to you so i'm going to save that and let's go through the rest we have a migrations folder which we're going to cover later so let's not worry about it now and we have a models folder and inside of there we have this index.js and what this does basically is it imports the configuration that we just looked at and it uses the environment that we're using right now which is development gets the credentials and then goes here and creates a sqlize instance which is kind of like a database connection uh instance that we'll use and then what it does it's gonna loop through this directory and import any other file which is a database model if it's inside this for a folder and initialize it and add it to array and then export that array alongside with the sqlize instance the connection it actually also exports sqlize with uppercase s which is the library itself but i'm not gonna i'm never gonna import it from this so i'm just gonna remove that second line so let's save this now let's actually connect to our database actually let's create it first it doesn't exist so without actually going to psql and creating it there's al already a command that lets us do this from the sqlite cli so if we type sqlize it's going to show us all the commands that we can use and i'm interested in this db create so let's say sqlize dv colon create and hit enter and now it's actually created that database and to double check i'm going to open a new terminal i'm going to say psql connect to my postgres server you can connect to yours and here i'll say backslash c to connect to sqlize oops c equalize underscore db and hit enter and now i'm connected to that db so it was actually created so that's cool let's start to create a table in our database so let's say i wanted to create a users table and the users each user has a name a role and an email for example so for this we need to create a database model so if we go back to the other terminal here we can use the sqlize cli to create a model and by the way you can always go back to the documentation and go here model basics and it's going to tell you a lot more about models and how you can create them uh one thing you have to be careful with the sqlize documentation is that they show you how to do the same thing multiple ways for example here they show you how to create the model using the fine and they show you how to create it using es6 classes which is the preferred way so i'll stick with the cli because it it kind of like maintains the same convention if you just use the cli so using the cli i'll say sqlize model colon generate and here we'll need two flags the first one is the name so dash dash name and this will be user with uppercase u and here the attributes so dash dash attributes so we have three attributes so there's the name and we do name colon and after the colon we say the type which is a string string and then comma to separate them so i need also the email which is a string as well and also the role which is a string as well and hit enter and by the way you can always hit enter and add more attributes if you want later you don't have to put them all here so let's close this and now we have this model inside of our models and here it basically just imports the model from sqlize and then it exports this function that has this class user inside of it and it also initializes the user as this function is executed so if you go back to the index.js you'll see that once it imports that model from that file it straight away passes it these two things the connection instance and also the data by um data types from sqlize so that the model can use them to initialize itself so let's actually go back to our app.js and let's import that sqlizeconnection instance so here let's say const and d structure sqlize with lowercase s equals the require from the same directory slash models and because it's index.js we don't need to say slash index.js of course so here let's create an async function main and here we're going to say await sqlize dot sync which what it does is basically it looks at our models and it creates database tables in our database depending on what models we have so here we have to call the main of course so domain and then save and now let's go back to the terminal and let's say node app.js and hit enter and now i actually created that table you see it's logging the sql queries to the console so if we go back to p sql and say backslash dt we'll see that there is a users table and if we say backslash d users actually i just realized it named it uppercase users i need to change that so by the way you need to wrap it with double quotations if there is an uppercase letter in there i hit enter we do indeed have that we have an id and those three fields that we specified and some timestamps as well so that's nice that was easy uh one thing that two things i want to change is i want to change the table name to under lowercase u here users because that's more of a sql convention and also i want to make the name the email and the role not nullable because right now they are nullable so let's go back to our model so user.js and here we can so as you see here there's init the first thing you pass it is the fields and the second thing is options and with the options you can add another option called table name table name and this i'll call it i'll give it the value of users with lowercase u so let's save that and also here for our fields they're not going to be just simply a type i'm going to cut that i'm going to make this into an object and here let's say type and pass that datatypes.string and here we're going to use a variable called allow null and set it to false by default is true so actually i'm going to i'm just going to copy this and paste it two more times for the other two fields and save and now um let's go back to our js and actually if we start our object app.js i don't think it's gonna work but i'm just gonna check double check again yeah it didn't change anything like i thought so here in sync if you don't pass it anything if the table exists it doesn't change anything so we need to actually pass it some options and we need to pass it uh actually it's either alter or force so i'm going to try alter i think this allows it to alter the table if it exists so node app and then we go back and we check in sqlize let's check what tables oh okay so he created the new table and he left the old one so one thing what we can do is we can use another option called force and set that to true and save and now if we go back to the other terminal and do a node app node up yeah it it actually drops all the tables that there are actually not all the tables sorry it doesn't drop the users with uppercase u because we didn't use force earlier so we go back and we do backslash dt yeah we still have this uppercase users table so we can actually manually delete it so we'll say drop table uppercase uppercase u users actually use this hit enter now it's actually dropped it and now if we describe the users with lowercase u we actually get that those fields do exist and they're not null and of course the table is called users with lowercase u let's start to use the sql as api to access our database and perform operations on that to do that i'm actually going to go back to my other terminal i'm going to install express so that it makes it easier to have multiple endpoints to test different things so let's install let's say npm install express and then minimize that and here in our app.js let's uh tab that down and here let's say const express equals require express and let's create a new express app so let's say const app equals express and call it as a function like that and here i'm going to create the first route that will allow us to create a user in our database so here i'll say app.post and then open that and do this will be to slash users and this will have an async handler so async request response and it's going to do something do an arrow function and here i'm actually going to take stuff from the request body so i'll say this structure name email and role and say equals request dot body actually i think we need to use the json parser so that we can pass the body if it's a json so here let's say app.use and say express dot json as a function like this so we need to use that json middleware so here we're going to get the name the email the role from the body and here let's do a try catch block so let's try to create a new user we're going to need to say await user and actually we need to import user from the models so here we'll say user like that now we'll say await user dot create and we'll pass it an object with all those keys now of course the values are the same uh the same as the name of the properties so we can just say like this named comma email comma roll actually i want to return this so let's make it into a variable so const user equals that and let's return it so let's return res.json json user of course we need to say catch here and if there's an error we're just going to console log the error and we're just going to return res.status 500. i'm actually going to return the error as well the json error i scroll down and here inside of our main we need to actually start that up actually uses a callback so we can actually get rid of this main get rid of that and say up dot listen let's pass it an object with the port port and i'll give it the port 5000 and this takes a callback and i'm going to make it an async callback because we need to call the sqlize in sync in there and then i'm just going to move this line inside of there i'm going to console.log before that and say server up on http colon slash local oops local host 5000 and after that we can console log and say database synced save that and go back to the terminal and here let's say node app and hit enter there we go we get uh where is it server up on localhost 5000 and we get database synced so if we open an http client so i'll open insomnia you can use insomnia or postman they're both kind of the same and here i'm gonna yeah i'm gonna actually use the same request and i'm gonna change this to localhost 5000 and change this into a post request and in the body i'm going to select jason and i'm going to give it a json body with a name and let's say this is john doe and an email let me actually make this bigger email and this will be john email.com and we'll give it a roll of let's say john is an admin so admin now let's run that okay we get uh not found oh because my bad actually that was slash users so to slash users hit enter there we go we actually get the response with a with an id and a created ad an updated ad meaning that it actually was persisted in the database and if we go and check in our database and we say select star from users call a semicolon hit enter there we go we indeed get john doe in there and it's persisted in our database so creating is actually working now let's add another field to the user because we don't want to use the uh the regular natural number id because that's a bad practice you don't want to tell the the public how many users you have so to do that we'll go to our user model and let's add a uid field here so here at the top i'll say uid and this will have a type of data types dot uid like that and we can also give it a default value so that it generates automatically and we'll say datatypes.uuidv4 and now this each time we create a user it's going to generate a uid for it so let's save and now if we actually restart our app so restart our app so say node app and hit enter it should actually edit our table so if we do d uh backslash d users it actually has a new field of view id so that's cool that was added now the problem with this approach by the way is if i select from my users i'll see that that user was deleted so each time we sync the database is dropping the table and we're losing all of our data so that's not a good practice so what's better than using sync of course in production you're going to use something called migrations which is going to create the tables that you need based on your models and based on whatever you put yourself so actually if you go to migrations you'll see it already generated a create user migration when we generated the user model so that's cool we can use this to create the table instead of syncing so i'm going to change here the table name from up case users both up there and down here and by the way if you don't know how migrations work they have an up function which when you run the migration it executes it so in this case it creates the table with all these fields and if you want to undo the migration it runs the down which in this case drops the table so it basically just reverse the changes that this migration is operated on the table so here i'm gonna as well add to these fields that they're not nullable so add to all of them allow oops allow null of false and also add the uid field so i'm just going to go and copy it from there and paste it here one thing by the way in migrations uh the c the database is called sqlize which is strange i don't know why they have it different from the models to the migrations maybe it has a purpose but i'm just going to change the sequel eyes to data types because it's just representing data types here so i'll change this to date types like that so it's less confusing and if you want to copy something from the model to the migration it it doesn't error on you so if we go back actually if we go to the terminal let's drop our database so let's say sqlize db drop and hit enter okay it says the database is being accessed yeah because i'm connected to it in my p sql so i'm just going to connect to the default postgres database and now i'm going to run the same command now let's drop the database let's create it again so db create hit enter and now go back to our app.js let's remove this sqlize sync and instead let's say sqlize authenticate which is a method that does exactly that authenticates to the database and here instead of saying sync we'll say database connected now of course if we run our app it's not going to do anything it's not going to create the tables our database our database is still empty so if we connect back to it so sqlize underscore db and we do backslash d we have no relations no tables or anything so to have the table we just simply run the migration so go back to our terminal and say sqlize db colon migrate and hit enter it's now migrated the create user migrations and if we go back to our database and run the same command we see that we have a table called users and so it's created and we also have a table called sqlize meta and if we actually select from it so let's say select star from and wrap it with double quotations and hit enter we see that it actually stores every migration that you run so that it keeps track and knows what migrations have run and which ones that they haven't now each time we run our app it doesn't actually touch the database which is a good practice so now let's actually test that again let's uh actually i forgot now i'm going to use nodemon so i don't have to keep restarting the server i have it installed globally so i'm going to say node1 app.js and hit enter now it started our app and here if i run this again we get a uid and we get the id and we get that user so that's cool one thing of course if you're creating an api you want to hide this id from your user you don't want the public to know this actual id in the database so the way we do that is easy we go back to our user model and we know each time we fetch the user in our app we can use the id but once we return it to the user we need to hide it so there is this function that is actually in the models in every model class there is a function called to json so to json and we can override it here so this basically each time you return a user in an api response as a json this is executed and this decides which fields are returned so here if we override it and say return and do an object like this and then spread this dot get which is a function that gets exactly just the fields of this object and then the and then we override the id and we say that the id is undefined and we save now if we go back here and let's create create a different user jane doe and here change this to jane and hit enter there we go now we don't get the id even though it's actually created and exists on the database it's hidden away from our users now let's create an endpoint where we fetch all of our users so let's go back to our app.js i'm actually going to close everything and here in app.js i'm going to create another endpoint so i'll say app.get and this will be slash users users and this will have an async resolver async request response and do an arrow function here let's do a try catch block let's say const users equals await user dot find now defined usually you give it a condition of which field um which fields you want to filter by but if you don't give it any condition it's just going to find all of the users so here we can say return rest.json and we pass it the users of course let's do a catch block and error if there is an error we're just gonna console.logger and then we're gonna return a rest.status 500 and then we're going to return a json of error something went wrong wrong let's save that and now because i have nodemon running it's going to restart the app automatically let's go back to insomnia i'm actually going to create save this as a as a request and let's save this as create i'll create actually create a new one new request and this will be read create that i'm just going to copy this endpoint so localhost 5000 slash users run that okay we get something went wrong let's check our console so user dot find is not a function actually my button is find all so let's go back this is find all so what happens when you change switch text stacks all the time let's run there we go we get an array of two users and of course the id is hidden as well because each time we fetch it it's hidden in any resolver so that's cool let's create an endpoint which i like to call the find which is when you want to find only one object one user so let's just duplicate one of these so the read i'm going to duplicate it into a find and let's go back here i'm actually just going to copy this one more time and here i'll say slash colon uuid because we just need to find one by a uid and here let's get that from the parameters so we'll say const uid equals request dot params dot uid and here in the user we need to use find one and then pass it a condition so here inside the conditional the condition is aware and here we'll say where uid is uid but because they're the same name we can just say it like this and this is user and this is user and let's just save this should work and now go back to insomnia now let's go back to the read let's say we wanted to fetch john doe so let's copy his uid and go to the find here get users slash that uid paste it run that and there we go we get only john so that's the find endpoint working now let's say we want to create another database model called posts and users can create posts in our database and then they can fetch them so let's go back to our terminal let's close it let's say sqlize model generate and let's give it a name so dash dash name post and let's give it some attributes attributes and it's just gonna have a body which is a string i hit enter and let's go to our models so here we have a post model oh close that i'm going to change so the here let's give it a table name table name and make that posts all lower case put a comma there and the body let's make that not nullable so make that into an object say type uh database data types or string by the way there are like all the types there there's integer i don't know if there there's no yeah there's no intelligence here but you can go back to the documentation and you can go to data types and you can see how you can use all the sql data types like this for now i'm only using a string but you can use anything that you need from this and also if you use postgres you're going to have access to enums and jsons and all advanced types so let's go back so that's still a string we need to tell it that allow null it needs to be false and actually i want to give the post the uid as well so i'm just going to open the user model i'm going to copy the uid from here and then paste that here save let's go to the migration that was created for creating posts and here we need to add also to the body that allow no is false and then also paste that uid and let me change that sqlize to dates types here so data types and then change this post to lowercase posts and save and let's open the terminal and let's run this migration and by the way you can also run sqlize db db colon migrate colon status and hit enter and it's going to tell you that we have this migration that we need to actually no we have this migration that has already run up and then we have create post is down we haven't run it yet so if we do db migrate and hit enter it's going to run the create post migration and it's going to create the posts table one thing i actually forgot to add is that i want inside the post model or inside the post table to have an id of a user because every post belongs to a user it can't be just linked to no user and also the user can have a lot of posts and if this is called an association or a relation in general in sql and this particular one is one-to-many so the way we can set up this relation is we can go to associate or association in this framework they call it so here in the associate this associate get gets all the models so we can straight away destructor from it the user model and we can say this which refers to the post class dot belongs to belongs to and then we can pass it the user model now what the way sqlize works is by default if you don't pass it which foreign key that it needs to look for it's going to look for the name of the model plus the primary key which is so the name of the model is user and then plus the primary key is id so it's going to look for this field but i want to name it our user with lowercase u like that so camel cased so to override the default behavior we can simply add some options to this and we can say foreign key and then say user id like that and then let's set up the other side of the relation or the association so let's go to the user and inside the associate we can destructure the post and here we'll say this dot has many as many and then we pass actually we pass the model post and then we pass the uh options so foreign key will be user id like that so let's save now actually i forgot we need to add that to the migration as well so here in the migration let's add another field the user id and this will be a type of data types dot integer and of course allow null here is false and let's put a comma there let's save now we need to rerun that migration so we need to revert to this migration and run it again so the way we can do that we can say sqlize db migrate undo and hit enter so this will actually revert the latest migration and if you want to revert all of them you can do undo all but i'm not going to do that right now so now it reverts it now if i migrate again it's going to migrate that same migration but in this time it's going to create the user id to check if we go to postgres and we do describe post and hit enter we get that our post has a user id with indeed lowercase user id because that's what we created and this will refer to the other table so that's cool now let's create an endpoint to create a post so here we can say app.post and this will be to slash posts and this will have an async handler async request response and then inside of here i'm going to do a try catch block and let's assume that we have authentication and we can know who this user is just by from the body so we can destructure from the body the user uuid and then we're going to get the body from request body so request dot body don't confuse yourself this is a body variable inside the request body not the request body itself so here actually let's go back up and import the post model and let's go back down and here we need to fetch the user based on this user uid so it's a const user equals a weight user dot find one and then we're going to pass it a condition of where and where the uid equals user uuid and now we can create that post we'll say cons const post equals await post dot create and then we'll pass it the body body and we'll pass it a user id of user dot id now let's return that post so return rest.json post here let's do a catch if there's an error we're just going to console.log it and then let's actually return it so return rest.status 500.json error let's save let's make sure our server is running actually i'm going to run it again nodemon app if we go back to insomnia i'm going to create i'm going to create a new request i'm just going to copy that endpoint new request create post and it's a it's post create that and here i'm going to paste that and change that to slash posts and here i'm going to add a json body and here we'll have a body which is the body of the the post i can say this is a new post and now we're gonna have a user uuid and then let's actually take for example jane's uh uuid let's go back to the create post and paste that here and let's run that and indeed we get a response back with a post with the user id of that post which is 2 which is indeed jane's id and then we get also the uid of the post the body and the id of the post now of course we want to hide these two fields so we can simply go back to our post model and here override d to json and then we'll say return return an object and we spread the this dot get and then we say id is undefined and also user id is undefined save that now let's create a second one a second post and then run that and indeed we don't get the id and the user id as well so those two are hidden away now let's say we wanted to get the post and we also wanted to get the user of the post without the user id so to do this we need to use the relations to fetch those relations excuse me i keep saying relations but their associations in sqlize but they're kind of the same so let's create another get endpoint to get all the posts so i'm just going to copy this paste it change the post to get and then remove we're not going to have a request body remove that and then here we're going to say const posts equal await post dot find all and then we return those posts posts save now if we duplicate this and let's say it's read posts create that change this to a get remove the uh the body and then run that we get the posts so if we want to get the user of the post inside the find all we're going to pass some options and we can say include and then we pass it an array of models and we could just say the user model like that and that should work and indeed it does work so every post now has its user attached to it so that's cool i want to change this user to lowercase because it doesn't make sense it's a variable it should be lowercase so the way we can do that we can simply say here inside the include make this into an object and say model is user and then we can say as and then pass it user like that just with lower case and now it fetches it actually no sqlize eager loading error okay let me check why that happened there's no match the earliest is defined okay i know why you have to define your association with that alias so here in the associate belongs to user here we can we need to say as user here so that we can use it as user in the other in the resolver or in the handler so if we run this now it actually works and it's lowercase user another advantage of uh defining your association here and giving an alias here is that each time you want to fetch the user you don't need to write all of this you can simply say include and then give it as a string like that user and now using the alias it knows which relation to fetch so that's cool i think you can pass a singular one as well let me try that yeah okay that's cool if you need multiple relations uh associations you pass an array and then you pass user and the other ones but if you need one you could just pass it as a string like that now let's do the same for user let's say when we fetch a user we want to fetch their posts as well so let's go back to the user and let's give this an alias so as and this is posts because there's multiple ones save that go back to abjs now where we find the user here we can say include and then actually without the array posts like that save and then go back to insomnia uh go to find the user run that actually john doesn't have any uh posts so if we get jane's uoid and we go back to the find uh paste that there and run that we get jane's object and also with the posts that she posted so that's cool and now we are fetching both sides of the uh association both from the post side and also from the user side so that's nice one thing that i wanted to demonstrate as well is right now we have no validation so if we run the create with just empty fields actually not empty fields because there we have the actually it does create them with empty fields that's strange oh because they're not null it does create them so obviously this is behavior that we don't want to happen we want to have some sort of validation to prevent this from happening so we go back to our code base we can of course write javascript validation before we run the where is it the create method here but obviously we want to leverage as much of sqlize as we can so if we go back to our user model here inside of our fields we can actually add for each field a key called validate and here inside the validate we can have multiple validation rules so if you click control space you see that there are so many different validation rules and if you want to understand more about them you can go back to the documentation and go to um is actually model model querying actually now validations and constraints so here if you scroll down uh you'll see it shows you how to use that validate key actually now this is showing you how to use the unique and if you scroll down you'll see that there are so many different validation rules that you can use and these are actually applied before the sql query is run so that's equalized doing its magic and you can also create your own custom validation rules which is pretty nice so for this well i'm gonna just use some simple validation rules and here let's say i want to use the not null validation rule which prevents sending null values and you can also instead of just setting it to true which does the job you can also give it an object with an msg for message field to specify the exact error message that you want to occur once this happens so let's say the mass and the message here will say user must have a name name and also we need to do the same for not empty because this can be an empty string so we'll say not empty and here we'll say name must not be empty and let's say actually let's copy that for all the other ones so copy that here and replace this with email and then copy that here replace that with role now let's save that and if we go back to insomnia and we run this we indeed get errors and if you go back if you go here you see we have an array of errors here the error has a path name and the message name must not be empty and if we remove name completely and we run that it says user must have a name so this is saying it's triggering the not null uh validation rule so that's cool if we scroll down we see as well the other ones email and also is it just these two yeah roll is valid so if i run that now uh we'll get the ones for roll as well so that's nice now we have validation which makes sure that we uh we can't give like some uh wrong like data so let's say here we want to create a user called user actually for email we can also add the other validation rule for checking for emails so is email and here i'll give it a message and it says must be a valid email address uh save that and let's go back now even if the email is not empty now if i just say user it's gonna complain so here it says email must be a valid email address and so now if i say at email it's still going to be invalid so if i say add email.com which is a valid email address and i run that now indeed it actually creates that and it passes the validation rules we don't have to actually write our customer validation we can just leverage the validation that's equalized gives us we've pretty much covered like most of the things that you will use i actually want to just show you how to do the other api endpoints like delete and update so let's say we want to perform those on users so let's go down here i'm going to copy this and let's say this is the delete so i'll change this to up dot delete and this is going to be the same it's going to be the same and once we find the user find user which i should have kept that eu id so where uid and we don't need to include the posts because we're just going to delete this user and here we'll say await user.destroy destroy like that and then of course the user doesn't exist so it's just going to return a message user deleted like that and let's also do the update so here i'll paste that again and this will be app.put and this will be to the uid what can we change we can change let's say the name actually we can change everything so let's say const name email and roll equal request dot body and here we're going to fetch that user so fetch the user um like that and here we'll say con user dot name equals name and then we're going to do that twice and then we'll say email and we'll say roll and here we'll say await user.save which will uh update the user and here we'll actually no yeah we'll return the user like that so both of these should work uh so let's go back to our insomnia i'm just going to re duplicate this and i'll call this delete delete like that and i'll change this to a delete method and here let's actually grab the user at email user uuid actually let's edit this user before we delete him so let's say i wanted to change his name to user name let's actually double check in postgres so let's go to psql and say select star from users run that so you see we have this user here with the user at username with the name user so now if you say his name is user name i'm saying the same user a bunch of times and his email is user at again user why not and the role is actually user not an admin and run that oh actually i deleted my bad i wanted to actually update it so now i run delete so if i run that select indeed that user is gone so he was id4 now he's gone let's say we want to update this one that doesn't have any name and email but overall admin so let's grab this uid and actually so i'm gonna remove this body so i'm going to duplicate this and i'll call this update create and now this will be a put and we're going to have a json body and let's give it a name of user or new user and an email of new at email.com and then a role of just uh maybe super admin run that okay i guess something went wrong let's check our terminal currently property name of null huh is it null let's try to just return this turn rest.json user save that run this oh it's actually null oh maybe i was given the wrong oh my bad i forgot to paste the new id alright so let's run that okay so we get the same user and it's not updating it oh because i'm just returning it my bad let me save that now let's run that cool it's actually updated now to super admin and the name is new user and the email is new at email.com and if we double check in the database uh we select oops oh no i was there so select all right so we get new user new ad email and super admin so indeed it's actually updated in the database all right so i think we checked off everything let me check actually i see this all right let me show you how to use series this is a nice feature that not a lot of rms pack so what cds are basically is just a file that allows you to create some dummy data or any type of data that you want in your database so let's say sqlize let's say for example we wanted to use to create two dummy users each time we're developing to use them as an example so you can say use sqlize seed colon generate and then dash dash name create fake users like that hit enter and now we get this cedar and it has like a migration and up and down as well so we can copy actually these commented lines so all of this paste that here remove the stars so here we have this query interface object uh which allows us to insert data to the database so let's say we wanted to insert into the users table and we have a name of john doe and we want to give it a email as well one thing by the way about the query interface it's not like the model instance it doesn't generate the auto the other fields so we actually need to give it the values so i'm actually going to copy this uid and here say let's give an email so john at email.com and then a uid of that uid and also actually we need to give the timestamp so let's copy this created ad and let's say created add and give it that create that duplicate that and make this into updated at updated at uh actually i forgot about the role the role is admin all right and also in the down let's copy this and paste it here and this basically just deletes all of the users so make change that into users let's uh add another one so here do comma paste and this other one will be jane doe jane change the email to jane and then maybe change a couple of these numbers and everything should be fine save i'm gonna open now uh in psql and just delete all the users so delete from users with colon hit enter and now we indeed have no users so now if i oh i go back to the other terminal and say sqlize db seed all it's going to run all the seeders so let's run the seeders so now if we go back to the database and we select we indeed have those two users so they're created so now each time if you want to reset your database you can just reset it and run the seeder to have that data to work with and you don't have to manually create them again so that's a nice feature all right so that was basically 90 or 95 of the things that you use when you use equalize uh that should cover all the fundamentals and i hope you learned something in this video uh please let me know in the comments if you like sqlize and if you're gonna use it i really like it i'm going to use it in a massive course that i'm working on and you're going to really really love it again please do smash the like button if you haven't and subscribe of course and i'll see you in the next one cheers
Info
Channel: Classsed
Views: 62,615
Rating: undefined out of 5
Keywords: learn sequelize, node orm, nodedjs orm tutorial, sequelize tutorial, sequelize orm tutorial, postgres sequelize tutorial
Id: 3qlnR9hK-lQ
Channel Id: undefined
Length: 49min 59sec (2999 seconds)
Published: Mon Nov 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.