TypeOrm Beginners Tutorial - Setting Up

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey guys how's it going i'm back here with another video and today i decided to bring a tutorial where i'm going to be teaching you guys how to kind of get started on using type arm as your arm in any backhand application that you're creating so basically i've already made a video on how to get started and install sqlize into an application however in this video i'm going to be talking about typeform so typeform is a as i mentioned an arm which is compatible with um both typescript and also with um all the different sql languages and even like mongodb that kind of stuff um so it is a really nice arm i'm actually using it a lot more than sqlize nowadays just because i like all of their small features it's very compatible as i mentioned before with sql which i really like and i wanted to bring on this tutorial so that um if you guys are interested then you guys can learn from it so this won't actually be focused on a specific database i will be showing how to actually configure it for mysql however the difference between installing configuring for mysql and configuring for something else like postgres it's it's almost not no no difference right so i'm just going to show you some parts of the tutorial will include mysql however overall i would just talk about how typewarm works and you guys can take on from that so basically as you can see right here i have a very empty application it is a it is a typescript um express api however it doesn't it is compatible with javascript as well i just use typescript because um i usually use typescript for making apis but there's no difference over here so what we need to do now is we actually need to install our type arm package so i'm going to open here my terminal as you can see and i'm going to push it over here let me do it like this and i'm just going to clear my server and just write here yarn add type orm like this and if you're using npm just write npm install typeform but i'm just going to write it like this so when i press enter you can see it will start installing our packages and i'll be back in a second when it finishes so it finished downloading and now we actually need to install the database we're going to be using so my case it is mysql however there's actually two libraries for mysql i one of them is called mysql and the other one is called mask clone 2. if you want to research on the difference between them you can do both are going to work the same way i prefer mysql 2 because of all its features that the normal mysql doesn't have and it's more compatible with improved versions of javascript so i'm gonna install mysql too if you're using postgres this is the time that you install postgres or any other database you are using okay so now that we installed our database into our um to our api um we need to actually start configuring typeform to work in application so the first thing i'm going to do is i'm actually going to import one thing specifically from typeform so i'm going to say import from typeform and this thing will be the create connection function and this will basically be the function we call when we want to obviously create the connection to our database so usually when i work with databases and setting up everything setting up our application i create a function here which is asynchronous however for the sake of making this tutorial not um too hard i'm just gonna just create it over here i'm just gonna call create connection and over here i have to actually pass an object containing all the pieces of information um related to our connection so what do i mean by that well basically when you create when you connect it to your database in your computer you have to have a port a user a database name a username and a password right so and also the type right i forgot about that because the type will be basically be the which database you're using so as i mentioned before they are compatible with as you can see many different databases um in our case i'm going to be using mysql so i'm just going to put here mysql but next to this we actually have to put all the different connection details right so in my case i haven't actually created the database yet but i'm going to call this database something like type arm oh actually i just need to say database over here i need to pass the database name so if you have a project here is the part where you put the database name so i'm going to call it typeform tutorial just like this but you can put it whatever you want and after this i'm gonna put my username usually the username is um just root especially if you're using mysql um root is just meaning that it's the local host like it's the it's what it comes with when you initialize the mysql server in your computer and then we also need to pass a password in my case i i called this password i actually changed the password and it's called password but um most computers um comes with this pass rate and it's actually empty so if you're in doubt like on what if this is actually giving you errors then let me know i can definitely make a video on this i actually have other videos on how to install different sql databases into your computer so if you're interested in that um i can link them in the description but this is basically what we're going to be using to connect to our database and then we can actually start passing um some additional information which is which are going to be actually pretty nice to um that comes with typeform and one of them is actually called logging which we need to say set equal to true so that it basically when we set this equal to true whenever we make an sql statement or whenever we make a statement or a query to our database it's going to console log to our terminal so it's really nice because we can actually see all the underlying queries that we're making to our database and not only just use pre-built functions like we are going to be using here but also be able to understand what's going behind right and then we can also use um pass a property called synchronize equal to true which basically means that we don't actually need to create our tables um if you're using table as i mentioned but if we if we're using an sql database we don't actually need to create our table on our own right we can actually create these things called entities which when we set synchronize equal to true it will automatically translate that logic into um a sql an sql statement which is to create the table and when we say synchronize equal to true it basically will run this and create the table when we run our application which is really nice and then we can also pass entities but i'm not going to pass this right now because we actually haven't created any entity and as i mentioned before an entity is almost like a table right it's just a collection um as a yeah it's basically just like for example if you're if you have a social media you'll have an entity or a table called users right or an entity or table called i don't know likes and followers that kind of stuff right it's just where you hold your data so now that we actually set our connection to to be connected to our database we have to come here to our application and set up a folder called entities like this um and inside of here is where we're going to pass all the files that are going to like which are going to represent the different tables we're going to have in our api for example let's take the example for for for the social media if i want to create an application which is going to hold a table um for user information i can come over here and create this folder called user.es and this is going to be the user entity and inside of here is where most of the typeform code actually works right so instead of here i can actually just come and import a bunch of stuff from typeform so i'm just going to say import from typeform like this and initially i'll just import two things um three things actually i'm going to import entity i'm going to import primary column generator primary generated column sorry and then i'm also going to import column like this yeah so we're actually going to be importing more stuff this is actually just different decorators that we can use to define our entity and you guys will see exactly how is how the syntax for this works i know it's weird when you look at the first time but you definitely get used to it pretty quick um to define an entity you actually just have to come over here and use the entity decorated like this right and then you have to export a class um and then the name of your entity so in this case we're going to call it um user right so this kind of going to be the name of your table so user and we need to say that it extends on base entity and we actually haven't imported base entity yet but it automatically imported at the top here so this is going to be how it's going to work and instead of here we're going to actually start by declaring all the different columns which means all the different properties we're going to have in this entity so a user probably wants an id right so how do we actually define an id in typeform well you can use the primary generated column um i think that decorator that i imported above and right below it you can just pass the name of the column which is id and then the type of your column right so for example number that would be a a type right so as in this is where i mentioned that it goes pretty well with typescript because it uses you have to define the type for each column like this and you can also use decorators to help define the different columns that you're going to have in your project and this is pretty much the format right down below here if we actually want to add more columns which we will we can actually use the column decorator that i mentioned above which um oh i also forgot to put the the parentheses for entity so for every decorator you need to put a parenthesis at the end and right below it you can actually pass the column so or the field right so for example a user might need a first name which is going to be of type string also a last name like this and last name and of type string as well and then we can just let me see um what else should we have just username something like that username right i'm just going to create a column like this and let's pass a user name of type string i'm actually going to my my server is currently running and it's definitely giving me some errors because i haven't actually created the database yet but um i'm going to run it and show you guys how to actually create the database if you're using mysql so if you're interested in that stick around for a little bit later so um finally let's just create another column which is going to be called password we can also pass string so this is the format right as i mentioned before we have the the type of column so the primary generated column just means that your id will be automatically the primary key in your database in your table and it will automatically increase as well that's something that comes with this decorator so um let me see experimental support for decorators oh if you're getting this error over here which you probably are and you're using typescript then it is just a fix in your ts config which will definitely um solve this i'm going to show you guys how to fix this because i know a lot of people will have the same error you just come here to your ts config and you can just change here let me find for decorators um you can just re like comment out the experimental decorator um functionality from your test config or just use it right and let me see if the error is still here um property id has no initializer and it is not definitely assigned in the constructor and that's exactly what i mean um this error will go out while we write in our code and let's just continue moving forward actually to fist to fix this error over here we can pretty much just fix it by setting a exclamation point right next to it as you can see and you do this for every single column as well this is just something that i forgot as i mentioned um the syntax is pretty weird but you get used to it so it's definitely okay if you make small mistakes like this one um the idea is um you just you just set up the the type of column and also the name of the the column and the type right so basically this is pretty much it for our user entity we're just going to create a column a table which has a first name a last name a username and a password that's pretty simple right nfd as well but that comes with every single table and how to actually make this work right how do we set up our table how do we um test to see for our database actually work um that's the thing i'm actually gonna right now create my database and using mysql workbench as you can see right here and i'm gonna call it obviously typearm tutorial but if you're not using mysql you can skip ahead in the video which i'm going to show you guys how to make simple queries using typeform after the connection is done that's the part of the tutorial where i show you guys a little bit of the beginner aspects of typeform right so to create the database i'm just going to come over here create a database called typeform tutorial and literally just apply like this and i'm going to close this and now my database is actually done as you can see right here it has no tables but when i run my application it should actually create the table however not yet because we haven't passed user as an entity to our um create connection so to define one of the tables the last thing you need to do is you need to pass a property here called entities and you pass an array of all the different columns the different tables you want to have in your database right so for example um i'm going to have user so i need to import user here at the top so import um let me just actually yeah i'll just do it like this i'll import it from the file so import from dot slash entity slash user and i'll just import user like this and now i can just pass user inside of here and as you can see if we run this application i'm going to clear my terminal i'm going to run my my server you'll see that it should run a bunch of sql queries and as you can see it actually ran all the queries um i don't know if there were any errors but you can see there's a bunch of queries being done right here and if we look at our mysql workbench and we refresh the tables it should probably appear there and as you can see it actually did we now have a table called user and if i select the the user table you can see it has an id of first name a last name a username and a password exactly how we defined in our entities file so pretty much it's working as you can see and it console logs every query you make because you set logging equal to true if you want to create one of your columns just create new entities and just put them over here in the entities array so this is the basic idea of how to configure um typeform into your application okay so as you can see i actually created here um two um api requests as you can see i have made a get request and a post request both of them are going to be pretty simple they'll get a request i'll just show you guys how to make a query to return every single element in the database and a post request i'll show you guys how to create a query to insert stuff into the database right so to get stuff from the database um we actually have to use the user dot find and this is basically it yeah this is literally it um you run this function and it should return a promise containing the data or for every single element in your database so what do i mean by a promise well to get the data you have to say dot then and grab the data through the arguments of a function like this and if you want to catch any errors you can also obviously go and write dot catch but i'm gonna not gonna show error handling here so you can see we can just for example return from this api request the data like this and it should actually return every single element in our database and actually have here an api tester which i can run both the get request and a post request so that we can test to see if it's working right so if i run this request you can see it's running on again request on the localhost 3001 and which is exactly the server the the endpoint which i'm running my server um if i run this you'll see it returns uh first of all it is working the the endpoint and it's also returning an empty array because our database is empty as you can see right here but if we want to insert into our database we can come here to the post request and we can just use the user dot insert function and over here you just have to pass an object i actually already have an object just because i didn't want to waste time in the video you can see this is the object i'm actually inserting it has to have every single field or every single column we defined in the entities file over here so a first name of last name and a username and password however it doesn't need to have an id this will be because it is a primary generated column it will automatically be generated and it will obviously increase by one because it's an id right so we can see we can just pass here um a first name a last name a username and a password just a simple object um into the insert function and this should be it um it we don't if we want to catch errors we can catch errors but we're not returning any data so we don't have to do the same thing we did over here we can actually just do something like end the request when we insert the data and let's test this um you can see that if i run the get request nothing happens oh the server is restarting so nothing happens it returns an empty array now if i change this to a post request you'll see nothing is returned but now let's change it back to get request and there should be data as you can see right here there's data if you want to insert many of the same elements i can just run this post request a bunch of times and just return as you can see there's many of the same person um there's other stuff to your your columns for example to your entities which you can define just to make sure um that you want to have all the different properties that a column might have for example you can set them to be unique you can set them to be um not null that kind of stuff and i'm going to show you guys how to actually set them to be unique right now so if you want to set any of these columns to be unique which from this one's over here i only i think that username should be the one that is unique you can just very simply pass over here an object containing the unique property and it should be equal to true right so there's many different properties you can add here as you can see you can make it unsigned you can make it nullable which is really important for some some of them but if you want to set a column to be unique you can just do it like this and obviously you need to synchronize your database again um so yeah that's literally it um typeform is actually pretty simple in my opinion after you get used to the syntax on how to generate the entities you'll get used to how how to work with it the methods you use here are actually a lot simpler than something like sqlize as i mentioned before um which i really enjoy and if you want to also implement relational database stuff it is extremely simple and i can make a video on that if you guys want to how to set up association between tables which is really interesting and yeah that's basically it i really hope you guys enjoyed this video if you enjoyed it please leave a like down below and comment what you want to see next subscribe because i'm posting three times a week and i would really appreciate it also give me feedback because i would really appreciate what you guys want to see i want to know what you guys are thinking about the videos it takes me a lot of time to make these videos so i would really appreciate the feedback right so that's basically it hope you guys enjoyed it and i see you guys [Music] you
Info
Channel: PedroTech
Views: 3,523
Rating: 4.9292035 out of 5
Keywords: computer science, crud, css, databases, javascript, learn reactjs, mysql, nodejs, programming, react tutorial, reactjs, reactjs beginner, reactjs tutorial, typescript, react js crash course, react js, node js, express js, pedrotech, traversy media, traversymedia, clever programmer, programming with mosh, tech with tim, freecodecamp, deved, pedro tech, sequelize, typeorm, typeorm tutorial, typeorm mysql, mysql tutorial, typeorm nodejs, nodejs mysql, typeorm beginner, typeorm entities
Id: rDj3AAFfMn0
Channel Id: undefined
Length: 19min 41sec (1181 seconds)
Published: Wed Feb 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.