Mastering Sequelize Typescript in Node.js

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys in the last video we created migrations for travels and tour stables in this video we will use sqlized models to create and view data in those tables don't forget to hit that like button if you are finding these sqlized tutorials helpful and informative liking the video not only lets us know you're enjoying the content but also helps other developers like yourself discover these valuable resources and if you haven't already make sure to click that subscribe button so you'll stay up to date with our latest tutorials let's start by cloning API service starter Repository now let's go ahead and open the project in BS code let's go ahead and install dependencies first and let's create the EnV file and in the EnV file let's put the database credentials they will be the same as from the previous project let's go ahead and save it and let's go ahead and test the API you can do yarn Dev and we can go to API test HTTP file right and we can test the health endpoint looks like it's working as well as the test message endpoint and it gives us back hello world so our API project is working let's go ahead and install the dev dependencies needed for sqlize as well as the sqlize dependencies needed for the project so and here we're going to be using sqlize typescript right also in order to use sqlize typescript we're going to make sure that the following lines are present in tsconfig.json right target is 6 or higher experimental decorator is true and integrator metadata true as well in the SRC folder let's go ahead and create a database folder in the database folder let's create connection.cs file in connection.cs file we're going to import sqlize from sqlize typescript we create a sqlized connection or we put a database in our database dialect username password all we're going to be taking it from environment variables in the host and we're going to put the number there we're going to test it to number and then the models directory which will be models and we will create the directory shortly and we're going to export defaults equalize now let's go in a server.ts file and import this connection file now let's go ahead and create models folder in the database folder and in this models folder we're going to create a new file called travel .cs this will be our model let's go ahead and create the code necessary for this model so we'll do some exports from sqlized typescript and they will be the decorators we're also going to import tour from the tour model however it doesn't exist yet we'll create it later so first we're going to create table decorator and we call timestamps true table name travels model name travel all right and now we can Define travel class it extends model the First Column will be primary key true is going to have data type uuid and default value will be set as uuid before which will be created automatically when we create a travel now we're going to declare ID as a string in JavaScript okay another column will be name which will be string as well as a data type string another column will be text which will be a description another column we're going to create a slug and we're going to generate the slog and the next column will be is public which will be Boolean and the number of days will be the next column which will be small integer and type of number in JavaScript and now we're going to do created at decorators and update it at decorators and in our table created it and updated it has an underscore so now we're going to create a relationship because our travel has many tours and we declare tours as a tour array and now we're going to create a hook before create to generate the slog right what we're going to do we're gonna get the count of travel where the name is whatever name is the user is putting in right and then we're gonna Define the suffix as empty and but if the count is more than zero then our suffix will be the count plus one so and now we assign the instance name to lowercase and we'll replace uh spaces with the hyphens and put the suffix it's going to be more clear when we look at the example here you can also notice that we query a database and check the name right so every time before we creating a record we're going to be query by that name to know how many counts of entries with the same name in the table we have so therefore name is a good candidate for indexing so you may want to put a index on the name and that will just export default travel so the next table we're going to be creating is tor.ts let's create code for Torah model so we're going to import again decorators from sqlite typescript and we're also going to be importing travel from the treble model the table again timestamps true table name tools and model Name Tour and we're going to have a class to our primary key true uh data type uuid again default value will be data type uuid before that will get generated for us by sqlize and declare ID is a string so now I'm going to have Define also foreign key we have to let the sqlize know that this is going to be a foreign key and we're going to have one-to-many relationship between travel and tours all right and then a column I'm going to go is also going to be data type uuid and the travel ID will be string in JavaScript let's create other columns the name String the starting date is date only and date in JavaScript and ending date is the same now we're gonna put price which will be the decimal as we talked in the previous video it's going to have a length total of 10 and we'll have four decimal places but in JavaScript this type will be a number and created it and updated it okay and let's export a tour now let's go ahead and add the code to server.ts file I'm going to be importing our models and at the end of the file let's create a few routes to test the models all right let's create the post route to create travel so we're going to be using the model and we'll take the payload and create a travel then return travel with a status 201 2 will be creating tours right for each travel and we'll Define the ID and we're going to be using request parameters ID and then add the body again and we're going to create tour and return the tour with the tool one of course this is just a test code so in real life you're going to be validating your inputs and having try catch statements in case something fails and now finally we're going to create a get travels route so to lease the travels when we get the travels we will also include a tour relationship so one travel can have many tours right that does it for the code let's go ahead and save it finally we're going to add requests to API test HTTP file so the first route will be create travel which will be a post request with a content type application Json don't forget to put that to read the body over the post and the body will be just a name it's public true number of days five right the next route will be to create a tour and here we'll have travels in the travel ID in there we have to put the correct travel ID in order to create a tour for that travel again consent type will be application Json and the payload will be named hi starting date and ending date and finally we're going to have a route to get travels let's go ahead spin out the dev server and test the route running on Port 3000 let's go ahead and create a travel and as you can see a travel got created with the name hello and it is public it gives us number of days and updated it created it and it gave us a slog as well already generated slug automatically for us now you also can see on the bottom log of my MySQL Query if you would like to turn off logging you can go into connection.cs file right and add another property logging as false all right let's go ahead and create another treble and now you don't see any login going on and the slog is hello Dash 2 because um you know hello already exists we can do check if the hyphens are working right we can do hello world let's create couple of the entries in here and then again and now we're going to have hello world with a hyphen and we have two as a slug now let's go ahead and create a couple tours for this travel so we copied the ID and we're gonna paste it right over here instead the ID that we had okay so let's go ahead and create a tour with a name high end starting date and ending date right so let's send the request and it looks like the tour was created successfully and we got an ID let's create a couple more tours all right now one of our travels should have three tours right and now we can use the get request to get travels and as you can see we're receiving uh an array of travels right the tour is one of them like we said the last one actually has one two and three tours you know the other ones the two that we created they don't have tours right there's an empty race here and we can also check the database right if we go to mySQL workbench right we can see that we created four travels right and those are our slugs that it got generated everything looks good and then if we go to tours right we can just check and see that we created those three tours and this is how you integrate sqlize in your node.js API project I hope you like this tutorial thank you for watching and I will see you in the next video
Info
Channel: Alex Rusin
Views: 15,410
Rating: undefined out of 5
Keywords: Sequelize in Node.js, Node.js ORM, Sequelize Tutorial, Database Management, Data Modeling, CRUD Operations, Sequelize Associations, Sequelize Hooks, Sequelize Querying, Web Development, Backend Development, JavaScript Development, Node.js Development, Sequelize Examples, Sequelize Setup
Id: J8l21W9dEk4
Channel Id: undefined
Length: 10min 37sec (637 seconds)
Published: Sun Aug 20 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.