Learn TypeOrm on Node.js with MySQL From Scratch in One Video

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
have joy Mangano doing anything to bring in territorial from Ibanez so the river got a really interesting story actually and topics talked about and we are actually gonna learn the type where I'm from scratch in one single video so I said before as I did another video about sequence is another know Jesus or him and a lot of people actually were so excited about it they like story all the lights the way I explain stuff and of course they wanted like more visitors walk or hands like other orders like type or M or prism or anything and today we got type over them which is gonna take type or I'm gonna just see what this type or M one should be use type RAM and what is the difference between type RM and other available or M's and most likely how to use and work with our M or the type or them and how to set up the projects and everything else around that so just gonna go ahead and start into that video tutorial and hopefully you guys are gonna be enjoying that so if you enjoyed the video tutorial push that like button I make sure to subscribe because this is gonna help me a lot doing more viewers like that so yeah so just gonna get started with type or first things first what is type 1 so type program is another program of course we all agree on that and too much can run on no genius browser Cordova PhoneGap ionic or even react native all the new native scripts it can also be run on expo cheese just like a react native container and of course on the electron platforms which is gonna be like and always separated thing and tag where i'm can run on that to you it is most specifically targeted to run on time script but also keep your run in javascript without any issues like es 5 es 6 7 & 8 so only the latest is a commercial craft versions because they support their creators and stuff like top i on and relies on to make everything or the whole system works perfectly and for that case what we need is we're gonna be using subscript because the whole solution of time program is actually from its name there's a type of the beginning which is like a typescript thing which means it is a typed or am that gonna hope to use the power of typescript and the in systems in general in order to create a really highly scalable and maintainable and as well as well like your code is gonna look just really simple and maintainable and everything's gonna be really simple using type or so had like a plenty of figures you're not gonna go ahead throw out all of these features because you know it's kind of kind of boring well you can go ahead and take a look on the features in here it'd be really really nice to do all of that and he has a really self-explanatory function or example side of the box so if you would really like to check out what it does and how we worked and everything else you can just go ahead and take a look on this examples the code snippets and everything you're gonna be understanding what is exactly how it is otherwise you can just gonna continue with my video and of course we hope we can learn a lot about type around so first things first is gonna start my installment so if it just gets a little bit down you're gonna find the installation section and you're gonna give you a lot of things so you can if you have like an existing projects where you could do the install type around and save it make sure to start reflecting me that data and of course I'm portal accurate data and of course it what install the types of node in order to have the type in you know the full features of types and type scripts and lastly you can install the driver gonna be using you can as well using with like my sequel MariaDB you can use it with Postgres krob you don't know how to spell that exactly you can use it with sequel i microsoft sequel server sequel generous rack or so on and so forth you can also use it with no sequel databases like MongoDB which is experienced more for now but hopefully against into like a full release out of a better experience more release and for surely has luckily MongoDB has the manuscript and react native script and Cordova from that and the whole documentation is actually listed in here so it's pretty pretty great actually to learn one single track program or one single or em and you can use it depending on all of these systems and craft systems pretty much our across I don't know like languages you can use from one database like my sequel to another another database like MongoDB and you can pretty much use all different stuff using a single Oram which is the type one so again start with that and how you came to set up or scaffold a new project from scratch you first need to install the Tempur MC align so house actually a CLI invest you know sonic globally then you can initialize a project with your CLI with your preferred gear based driver in order to start getting working with that obviously so we already created a project but I'm gonna show you how you can go and create a project for that so just go into open up the terminal and make sure to do NPM install type run has C sense so just do a type around the Jeep and everything's gonna be like and start globally afterwards you're gonna be initializing all project so I'm gonna say type around I'm gonna call the in it I'm gonna do name I'm gonna name my project in here because it's gonna put everything in the end of JSON and stuff so we need the name for that I don't know you can use life anything lifetime project for something and eventually you specify the database you're gonna be using so you specify the database from the list of databases available up here so if you go in here there's a sequel wise Microsoft sequel server my sequel murder DB commanding the version you're using or you specify that right here in all cases didn't use in my sequel the press enter and the installation is gonna be like started and running I already did got I recreated anyhow gonna create something like this like this is the project structure of the templates it's gonna be created for you by the time for nmcli so you're gonna have everything up in here like given or or am configuration which is the main configuration for the type or time and of course you can just set up everything from here and it's pretty self-explanatory it's nothing really hard or something now I've already opened up the projects in Visual Studio code so I got everything in helping here in the US are seein this is this is actual scaffold with projects so for that I only use John instead of NPM so I just prefer your own Berlin an NPM and yeah so for the configuration first things first to get started with a program is actually doing the configuration for your specific database so whether you're running in development you have your server running in like production you have to specify the configuration for each like for the production and for the development phase because they both have like specific configurations for each so if when you aren't doing developments it's gonna use the development configuration and vice versa for the different for production things so from that case I'm gonna be having is actually the Orem conflict or JSON so the on colleague dot JSON what it does actually has all the configurations and inside of that is actually a JSON file so it has everything up in here you can you can see the type we're using my sequel this localhost default ports and the username you have to put your username and the password of the database the database name is gonna be used and of course you have to manually create that and you can set synchronize to true false we gotta set this to true and it's by default came to true I'm gonna explain it to synchronize what it does in a second there is long in false just keep it up there and the migrations table name of out of this to like and the migrations table name to migrations in our database we got entities path so make sure to specify that specifically to where your our entities exist because this is a very important in order to like tell type program where I put my identities and where to find the entities in order to be able to manage and create instances and save and all the different stuff but type one can allow you to do using the API of course so with that in order to make sure you have that make sure to specify the entities path that C is inside of SRC for a slash entity and you're gonna find all the entities by default the project creates a user entity for us just like a quick example and has no migrations you have to specify the migrations folder and the subscriber we're not gonna just go through subscribers they're completely something different but we still can use subscribers in here you specify the directory finance now for the CLI there's two things this is how the type or MCOR more view how we actually the comic how it gets complicated and pretty much the CLI this is the configuration that is for the CLI so we got entities directory the directory for entities migration by directory and the subscriber die okay pretty much the same thing but you can specify different she's really different like from the CLI in the core but that doesn't really matter anything so that's pretty much it and make sure to open up I'm using my C equals I set before so you can use Postgres source equalizer or depending on your projects so I'm just gonna go ahead and login into my seat wall right here so I'm just gonna open up the connection and I'm gonna create a database for that so I'm gonna just do like the leads database and I'm gonna need this like social I don't know just like the example we introduced in sequel eyes so pretty much you're gonna use the same thing in here just to make it really perfect for you to actually understand what's going on I'm gonna change the database name here so it's going to be for an order to think and I'm gonna set the password as well for that in our case now after I've saved the Orang configuration and just put everything up in here and we created the database now it's just gonna explore the index dot time script Lee created for us but the index told us to type script but it has it has actually a really important function in how a program works so first you can call the global function which is create connection and this one is gonna create to you a connection between the type RM and pretty much the database you specify and of course it's gonna create the connection and return the connection objects which later on can allow you to manage the connection see the connection name you know different types of things that you can manage the connection between an ORM and a database so pretty much this is this and we're using a stink in here because the whole thing about type program is actually gonna allow you to use async awaits and promises and manage all your code much simpler than just working with callbacks and then catch on the promises of stuff like that so it's gonna make it super super simple for you to manage your code and pretty much interact with promises and resolving and everything along that so in our case in here has a pretty simple example so - pretty much to understand this example we're gonna go through it like line by line by line throughout the whole series but we're first gonna create something really important so more important the index doctype script is actually looking at some of the end so how we can create entity and entity in Thai program is pretty much like a model so model on entity they pretty much the same thing in Thai program and it represents a table so I represents actually a table instance on the ORM and of course on the database and for that which is actually on normal JavaScript or typescript class that is exported and annotated using the ax entity decorator so for every single type of entity you have to use the entity decorator if you just take a look on that this is a decorator user to mark or classes that is an entity a table or document dependent database type of course since we are actually using a sequel database which is my sequel it is a table in other case if it is MongoDB it is a document of course because it's not sequel it has no relations and no tables so with that we've got the entity in here most likely you can specify the name of the entity which gonna represent the name of the table we can just mean based on users by default it's going to use the name of the class like it's gonna put a user in here lower case of course bar you can specify that custom name from the table for sure so you can say name and we've got here is this all of them are fields so this is a normal room like a class field she's the ID we're specifying this as a number but in our case so most likely they're not gonna be finding that by default out of this just for testing purposes so this is actually the ID for the ID we have to use a specific decorator for that to annotate this is actually an ID so we can use the primary generated column which is gonna be primary unique and auto-generated common for us from the ornaments off or from the database itself so for that in our case we're using primary generate column and by default it's gonna generate an incremental integer number which is we know one two three four five and how my sequel works but in some cases you would like to use UID or different schema for the ID so here this is actually the available times increments which is by default selected row ID or UID we're gonna use UID is it going to generate us your ID a string or ID for that and he's gonna be puttin it right here pretty great actually this is this is really another thing this is what I like about this hi program we got also the column so the column annotation in here just like honor tase on normal column and it's gonna just take the type since it uses the typescript typing system you're gonna abstract the type of from here and it's gonna be sending it into your column on the users table so by default without specifying the type but you can also specify the type anything was gonna say type on the objects and you can say for example variable char and you say variable type if you wanted to specify the length we can do length and simply you can say for example it has 200 characters linked from a variable card or promote char tie for that but by default you gonna say that and you can have that could be folks time for us which is gonna be set to string same for the last name same for the age sister also has the number you can also specify the time as I said before and same for a roll column in here let's say we don't have the roll column in here for now so this is what it looks like they use your table or the user entity this is where it looks like so the very important step is actually to annotate as an entity and make sure to every single column it wants it to add to be added and managed by the ORM and saved to the database into the users table specifically you have to be entertaining that using the app column decorator otherwise it won't work and most likely you have to say the primary generated column using this decorator specifically better than using your decorator so this is pretty simple really well made and the entities really solve like explanatory it's not really that big and something mysterious or something it's pretty pretty simple to just work with it and jump into the point in here so nothing really complicated with that and yeah so this is this pretty much the answer see why you could pretty much be used for like the entity if you wants to use like specific custom options you can use like unknowable so you say is this is not by default not going to be knowable but you can just say false or you want like the first name to be knowable and if the user doesn't set the first name no constraint or no errors is gonna be thrown so you can have that as well as an option listed by a which is pretty pretty great now the entity by default since we before like in the configuration we specify that the entities directory is actually inside of the SRC slash entity and everything inside of that all the files inside of it is actually it represents a type or an entity so type around by default is gonna scrape that it goes throughout the folder I got grabs all the into T classes since we are actually exporting them and of course what it does it just actually gonna be added into your database and you can I cannot allow you to use that or manage them throughout a global instance so type program is gonna provide a global instance for you to be used in order to manage entities like find users or safe user to the database or just do like a query builder where you build sequel query from scratch to find or to like join two tables of stock all the entities instances but are gonna be automatically added to you on that connection without you specifically going through adding them by yourself so which is pretty great actually and let's go inside like we got the index start a type script and here where everything pretty much once so it should be pretty simple for that for saving and adding or pretty much for querying Vienna from the database and ty program has a really robust a simple API to work with so it pretty much allows you to work with promises and also to save to database really quickly and throughout it using async await it's gonna be much simpler and the code looks plus simple enough so here what it does for our example God like can see usual equals a new user so we got a first name last name an int sets to that so make sure to like construct that or create a new instance of the user what you can else do is pretty much you use the create method on the repository you're gonna see that in a second so we create all of that we set the fields now this user has pretty much like a normal object has this fuel values now what we can do is you can do like a weight connection manager we get the manager of the connection we do save and we passing it to the actual user so here we specify it save say it takes actually what entity you want to save from us and you account the user this is actually what we want to say you can also be passing the actual type of the entity then the entity data you want to save and that specific entities table but by default are gonna recognize the entity type since this actually has a user type so if ID for is gonna be saving that user into the database into the users table and since we're using a weight so you can look like a synchronous code so it waits for that to run in order to be continued and since you're actually running the create connection so we wait to create the connection between the ORM and pretty much the database then then what it does is actually gonna run all of that code to saying that it not necessarily you have to run your code in some of the van method after including the connection you have to do it after the create connection but not necessarily here you can create another model for example you can go set up like SRC I don't know for example like you can name this booster for something time script and here you can go in and grab the entity and run that without any issues so if you want to like test that here what we can do is actually you can have such a self-executing function which pretty much can help us allowance for that case so or pretty much let's have like a normal function we're gonna have nothing to export well export cost and this is actually gonna be a normal function is gonna be an async function most likely and here for this case just here what we did is actually grab the user we have to have access to user ID which is a user ID also generated for us and in this case we're just like finding the user we do like connection top manager don't find the user with on it just like a bunch of console logs and if we catch that's actually a promise if any errors happen we do catch and console market errors now let's say we want to save the user but we don't want to be using the manager because ty program has two different type of what entity management systems so it has like the manager pattern so where you use the connection manager in order to find a specific entity and work with any manager on the other hand has the better solution or the better parent I'd love personally is actually the repository pattern so the depository pattern they much can allow you to work for every single entity has its own repository that's gonna allow you to query on that entity or pretty much save on that entity without using the manager because the manager has to be specific or global to the whole entity so the whole entity share the same manager where each repository pretty much represents a specific entity or belongs to a specific entity so that's the actual difference between a manager pattern and pretty much the workers like repository pattern for simple now we can use the repository in here to add a new user so in our case what we do is actually we do like we first need to get the usual repository so if you like user repo equals gets repository and this is actually a global function exported from the time program and we have to specify the entity class that we want to be using and it is actually a user so we grab mask now we've got the user repo what we can do for example let's saying cost new user and we want to just create a new user without using the constructor here we can still use like the constructor we can do like new user and we send the fields and stuff or you can use the different way is using the create method and the create method in here it can take entity like and objects and you can specify all the things about the user so for example user has a first name last name and age take the same first I like saw something well last name can also cause rate hike and lastly we can have like an age for example this age is actually 22 so their regards in the user and the instance of it is actually a user now simply to save the user we're gonna do one user repo dog save any passive user illnesses I'll obviously have a force of cash so always make sure to catch your promises because it is very bad to just buck ignore the catch method is very important to catch here and comes along and here make sure to also areas where all use async we can't wait that's really really nicely now after saving that we can pretty much like this console.log specify the idea here so it uses LED and we can we can tell what you use your ID in here or pretty much like just like so let's just print the whole user with all the ID and yeah so here if you just want to like to save that's I can go in and say that way just getting comments the whole code or pretty much well I'm just gonna come in a whole code in here that would be very just to make sure that's I'm gonna be working at that bootstrap so eventually we got the create connection once we create a connection and everything else now we can go and call bootstrap function so the bootstrap function well I think I can stop this yeah and you want to cache so issues happen well get and of course this is actually a promise we have to use the weights in order to like wait for that to happen otherwise it's gonna be just like a huge problem for us now really nice now just go back into the back of JSON regards the stock scripts which uses the TS node in order to allo typescript in no jeans and it runs the index dot time script so fair enough that's going through your ass I'm just going to yarn start in order to start these scripts and pretty much like create a connection then wait for that and of course we got new user save it which is user with first name last name age and order generated ideas of sad and it has like a UUID you still to go back in here you can remove the ILD and it's gonna generate like normal numbers from here like he generates one two three and increments will integer IDs but it's better to use your IDs nowadays because it's just like much much easier to work with your ideas and much secure then incremental IDs so that's that's the way how it works so it's actually using the repository pattern and that's why I would personally recommend using because it's much better to work with repositories other than just a entity manager simple enough as appears in here check forum has a pretty simple and robust API to manage safe DNA create entities and stuff like that not say the user has just like one more thing to do and since we got like a social network so let's say he has a tweets entity and he can pay my street on his wall and he can lock submit these tweets and publish them so with that we have to create a new entity just like to excite the user entity I'm gonna name this tweets entity so I'm gonna just like tweet Times Square and of course going to use the entity annotation so we're going to import that I'm going to specify the name of the table it's gonna be naming it tweets simple enough and got exporting the class and make sure to always explore the class otherwise type run won't be able to like give the instances of the entity and important or not to allow you to work with repositories and stuff so he regard the three entity and as always without being the actual let me just look we still got this number and I should have changed this to strength sorry so this is actually the tweets and it's actually been your primary generator column and for this I'm gonna use your I visited before let's say you what a tweet has it has actually up title and you can have a column okay well let's just specify the type of the column in here it's not really necessary but just to do it and I'm gonna say the length of the title it just like you can hold the title by 80 characters okay I would like to specify in a number of set of the string especially when K is another column well this is actually gonna be the content so I'm gonna do like well very good character again and I'm gonna do the link fast is gonna be about 300 so 300 are they actually the maximum tweets pretty much that you can and if a user can be my submit and here look here the contents of the foods we can say test drink simple enough now the most important part is actually you on every single Orem whether it is sequence type around Prisma or even without organs is actually managing relationships between entities so relation is actually really important so that's how you tell if a user has one tweet or the user can pretty much you know submit a tweet or pretty much find its tweet and what tweak it has been tweeted by a single user and vice versa so all of that can be done and managed it bought a single tweet class and of course you have to rely or click both of these entities the user with a tweet in order to be able to do that with relationships so now let's go and create the relationship between the user and tweet and of course as we all know and user has multiple tweets so I can have lonely tweets as you want where a single tweet can only have a single user and belongs to a single user so in our case so we have to create like many treats for a user and a single user for a tweet so therefore we can do in here since like a user has many tweets so we can pretty much we can have like one say menu and therefore here we specify the type of the other relationship and the second part of the relationship which is a tweet and here how we can reverse the access the relationship like how we can access users from the tweets relationship so we have to create that first in there and the name of this lets gonna name it says he has many tweets so we can acclimate tweets it's gonna be just two tweets okay fair enough now go back to treating here it seems like a tree has a single user so what we can do we can have like many to one okay all the type of this one user and since we got here like many tweet to one user in our case in here we come one user to many tweets of course so here we can access it so it's absolutely you're gonna give you the user instance and how you can access it is pretty much through all this property is 12-piece properly you can say this is actually the user simple enough and if we go back in here we can have the tweets instance and we can say we can access to it instance through one we use your property fair enough now we got our relationship links one to many many-to-one you can use all the relationships of course there is like many too many and there is one-to-one which is although different relationships while here we're only many too many and many to one relationships and therefore me probably really meet to do right now it's actually it's not the bootstrap script but gonna create like the tweets and try to create the instance for that so I'm gonna first go and do a tweet repo I'm gonna get the repo I'm gonna get repository and I'm gonna get the treat repository for that second and then creating new tweets I'm going to just say new trees therefore this new tree instance I'm gonna say tweets equals well title equals okay finally got a new job so not to develop whole tweeting about his a new chomping it's so happy and I got content for example at the same well sure yeah pretty hilarious I'm just taking the well one here so I'm just gonna do well now we need to go in and save that using the tweet repo so I'm gonna just say fury Po and please save and we can do make sure to just wait for it to happen otherwise it's not gonna work for us perfectly so I got a Waits and we save that and everything else around that and eventually in here since we saved the to repo and we call the tweet save to the database now we need to assign the tweet to the actual user in order to maybe be able to work with that or most importantly you can do that way or what you can do on this other hand is actually save the tweet with the user ID because generally a user exists before the tweet exists so therefore once he has already been existed he wants to create a new twist once he creates a tweet the tree is gonna hold the user ID so simply until sign that we do like the tweets dr. user equals to a user who wants to pretty much create a tweet simple enough and of course of after saving everything's gonna be working you could also like end the tweets to the user from the user part like using the user repository and find the entity there or the entity instead of course add the tree to it but it's much better to add it this way because you know this is how it works and you have to always generally think about how your system works and what is the logical way to manage the system is actually adding the true user first then having the tweet with the user who created it first so simple enough right now let's go and do like you're starting in and pretty much we'll be able to start saving your user to the database and of course save a new tweet created by that user so if we go back in here we can like show tables of the count the other base okay having any tables now for sharing tables we've got treating like users we can go and do like describe tweets table and the tree table of course it has the user ID because every single tweets who holds the user ID and the other hand since he has like one single user button the other hand when it user can hold the tweet ID because it has many tweets so this is pretty much how it works like one too many or many to any relationship for many to many relationship you have to create like a foreign table that holds this like relationship between the both both entities keys or IDs you know to intensify tasks you can just stop check all the documentation has a pretty self-explanatory there I'm not gonna cover all the relationships in here because it's gonna take really long time and they are super simple to work with and we can also do like subscribe users and for the users we simply we got that as well so we got ID first name last name and age and everything around that corner now fair enough to work with all the different stuff right here we can go and do like show for a payment select okay all from I've gonna select all from the users and we got a couple of users in our case so if we just do that so we got first name last name ago Alex Preston 22 and if you do now select all of them tweets okay that's something with this is criminal and if you go back and work inside of the twitch we got a single tweet in here about the ID which is UID the title and the content and lastly we got the user ID in here which is gonna represent like a foreign key and of course and this is this is what the user ID represents in our case so it's pretty simple if you can look at it and here it's just gonna visualize it much much better and it shows how he works in in a better way now we have saved that's the tweet now we gotta the user got the tweet and everything else no this is pretty much how relationships works and it should be really simple to work with us especially using the repository API it makes it super super easier to work with now let's say we want actually to now find users on the database server creating and saving to the database now we need to clearly user from database and clear that specific like tweets and stuff so the query API is pretty simple on type program it's gonna create like another function log first query okay fine I'll just name this one we're gonna have an async first function since everything async and runs in the promises so that should be the best way to handle it fair enough we're just gonna do we need to lock use or guess the usury posture if you're gonna do user repo equals get repository and the repository fusion now to use the actual core API which is pretty simple house like you do pretty set builds of function into that and the API is pretty robust and straightforward but we can use fine method to pretty much find everything in here without any options but if you want to specify like specific options and select like specific users with specific user names or depending on the options you provide in here you can say for example the word closed so we're and here you can specify the user object for example ID equals to something like that but what will our anything or pretty much if the user age has something like I don't know you can you can use like something like a greater end or less than that or the age is actually 22 or something or you know something something like really simple in here let's say me one I guess the first name of the user and it's gonna be Alex so give me a user is doing that it doesn't find anything or any earnest happens along the way we can do it just don't want the error okay um well simple enough right in here and yeah so this is how all we need to do is usually find it and the find method in here it takes like you're gonna return a user array if you want to like find a single user with an on-ear something you can use 501 or you can use fine or fail so under fail what it does it looks for a user if it doesn't find no users you're gonna fail so it's gonna throw up an exception and this could be handy in some cases but we want to find socially a single user of course we're gonna use fine one let's use user so in this case is going to return a single user instead of like an array of users you can probably tell here in the promise and you can do like one user so well enough and the most important part to notice in here is actually eager loading so the find method of any fine method or either find one or find all of them or fine or fail so it's actually eager loaded by default what I mean be eager loaded so once you actually like early in the user since in a user has a relationship with all its Suites so once you access the tweets all of them are gonna be eager loaded and reported or pretty much load it to you on the fly with the user and you're gonna find it like inside of user doctory so I think I'll go and sounds like a user up tweets all should ease away in here sorry for that always used away because to promise let's get there like tweets use your dots okay there go okay avoid or user and make sure to like say it's a user that's very important as well and there you go now we can access the tweets and the trees are gonna be eager loaded which means it can I go to the Twitter entity and I grab everything in here and eager loading sometimes it's not good because you're gonna do like an overhead and the timing or the query time it's gonna be longer so sometimes you don't need the tweets because you just need the user but you don't need any relationship so eager loading or Vance is pretty bad so there is to actually solutions with eager loading and but you can go through them so first you can use the query builder so user has for example you can do it the user repo and you can do hurry and here you can create the query and pretty much like you can you can do whatever quarry an inline or row my sequel query or you can we can help you much the create query builder in here and the critical value has pain things like you can add guru pot add auto buy and where you can just like an end where close or something so how to approve the bust and pretty advanced query builder in here so yeah like so much your building and my sequel query from scratch so this one is gonna have no eager loading by default implements it so you have to do like eager loading by yourself which is great and it's gonna no cost no problem or the second solution which most likely I love is actually when we created a user so for example gonna here tweets so it's still having like two normal trees in here we can have like a promise so you do like a promise of trees are gonna be resolved and the same thing is gonna happen from the user so I'm gonna have that I've promised all the user is gonna be resolved of that so in this case we got lazy loading happening and lazy loading which means it's not gonna be lonely the user or the tweet till we explicitly tell me to load the tweets what I mean by that is specifically we go back into the bootstrap in here and seriously we got an error as well in here so what I mean by that so as here tweet becomes a promise so we won't be able to load a tweet till we explicitly telling to go in and resolve the promise and we can tell it to do that it's using that wait so you can tell in here I'm just so in a second and on one save in a promise you want like you use the promise resort because this promise is gonna be resorted with that value so you have to do what promise got resolved and you specify which of the value is gonna be resolved with in this case it's actually in a user inside of the tweet so make sure to do that this way in order to be able to use the lazy API or the lazy parents of eager loading pattern and you can take a look on the lazy or ego loading pattern on the documentation for I program it's pretty self-explanatory there so you can find it much more enjoyable too go through and understand what is going on behind us so here's saving the Boost track we're gonna run that again and haven't such a cold the fine method so we're just gonna do well it's gonna find like it use your I'm just gonna clear out the databases so what I'm gonna do is actually drop both tables I'm gonna drop the users database changes I'm gonna drop table users and I'm just gonna drop which beats is it's very important to trap the constraints and then dropping the users now both tables huffing drops and we can stop again so we first create that so we do the British create Alex then we grab Alex and we be able to see all of these sweets and stuff like that and here we are telling it to promise resolve for using lazy loading all of that so here once you do like bootstrap you can do the same thing you should copy paste this and you can use the find method good enough it's gonna do like Yan starts and instead of doing all that so I'm just gonna get rid of all that code because we don't need it and you can use the managers of stuff doesn't really need it out much now we created all of that and if you take a look closely on the log here what we got we first create a new user of course now we find an user we got the ID name last name age now tweets as close in here it tells this is treating has three BS then we can explicitly telling to go ahead and load to it because we aren't using a wait so this pretty much this is how lazy loading pretty much works and it's much much better it's not gonna lower the tweets by default so till you explicitly tell it to load tweets for that so you can also specify or you use ego loading just by removing the promise in here and the promise on the user and whatever it sees you would like to use either loading and you can remove the promised or resolved in here I never thought we should work for you pretty much well now fair enough this is about querying and everything else for the query builder as I've told you before you can go ahead and use the documentation because curries are pretty much like a normal sequel queries and we're not gonna go through that because it's a normal sequel queries and here we are actually for the API into the type Aurum so it should be much better to just go ahead and use the final and most people are gonna find reusing the find method in some of the repository are pretty self-explanatory enough for them so they don't need to go through anything anything more than that so pretty simple for that now the last thing we want to cover in this video is actually my creations so migrations are a really great and robust feature from tai program or any other overlap which also pretty much to keep track and all through databases on the fly especially on the production without any chances to lose dinner or without risking to lose the end up pretty much so migration is actually just like our query that runs but every single time you create a migration so for example you create a migration to add like a column to an existing table without changing or removing the database or pretty much removing a table that you want to alter of that and of course this can cause of course not losing your data because if you just like to remove the table you're gonna lose all of your data and my creations can help in that so you can even drop tables you can change the table keys or you can add columns change the type of columns and you can do pretty much a lot of things using migrations and it can help a lot using or throughout the what the process of production especially because the development we got synchronized set to true and once you want to go to production of course synchronous has to be set to false because you don't want losing or your informations and data inside of the tables pretty much the first you could start with migration we have to give an you use the migration to generate that so for example that's gonna do like I'm gonna use the type or am CLI although I always to create my creations to make your type around migration create and we can give it a name in here so for example add row column to users so here this migration rate does is gonna add like a row column to the users and it's gonna create migration yourself the migrations folder specify the configuration and you have to specify that at some of the configuration so if we take a look in here yeah I'm missing my key message to put in here okay there you go now migration has been added and of course automatically is gonna end like a timestamp to it so it knows when this my encryption has been created and has been submitted so you keep track of migrations the migration IPM in here is pretty simple and it hasn't really that much of things to be taken care of so what you need to do here literally the columns or the class name in here with the timestamp extends the migration interface and there are two main methods so this is the up method which sets up the migration if you like migrate bass or gets it down if you do like migration go down and it's gonna what it does it just like remove any changes the migration did throughout the up method in here so for example it's gonna say I want to add like a column to the users memorial so you can do that really easily if you go back into let's say you change your code base and in this case while you did you add in a new let's say you add a new roll number or a boolean okay it's gonna be a column in here and it's gonna have the type of the number without any issues of course so this is gonna be like the role of the user if we add advanced if we've got like synchronize chess teacher is gonna be automatically added but if it's set just false and not gonna be added till we explicitly add in it using the migration in here so we can use the migration we can go in and do like a query runner we can block for example to add a column and we supply it with a table name in here for example be doing users table equals again it gets table and the good tip when he's gonna get us a table by name and you can help specifying the table name explicitly in order to make that work so we have to specify this name you have set in here this is that sugar table name we're gonna get the users so afterward we can just like user's table and here we can submit another column so we have to create one column I'm going to be like twelve column equals new table column okay the options of you can specify a name of the column for example it's going to be a role and the top of the column and this case is gonna be an integer so you have to specify an engineer because if you take a look in here and types there is your couple things this integer to four depending on going for the integer so we just must be funny really small into here in dirt and here we can specify a width goal column to it and the query runner should pretty much actually run in here for God's promise table weights because this runs a promise and you have to wait for the promise and of course this one returns a promise as well so we have to wait for it otherwise you won't work and the other hand to here what we want is likely to get the users table but instead of like like adding a column to it so we have to remove a column from it so you can do the weight query runner or drop Kali and we're gonna be dropping a column and here just like supply the column name that we added the cheese roll and or pretty much you have to specify the table first usually stable and you have to specify the column second I'm just gonna do well fair enough so this is pretty much it and this is how my creationist works so the APA is pretty well made and the career on here the interface of a career I'm gonna order to go through all the tables add columns remove columns or a name or even drop databases or tables and sign up at the other base so you could all of that and done inside of here now since all migration has been added to submit or run the migration to take changes to the database what we need to do is be much use the slow I again so type RM and we can go on immigration and just run so just running like that of course most of the times it's gonna be failing like this so I wanted this to feel like that in order to show you how exactly to fix it because what it does it can't find or recognize the import because it tries to run this using no GS while we're using typescript so this can cut how things can work so for fixing the issue what we need to do is actually install at all things so we need to install globally time scrap mps no so make sure just all the scroll in order to be able to use them after installing these we can have like now we can use the typescript so we'll just paste the command which we need to use ts node then we access the node modules bin type program which is writing here type program Bane and we need to find the ty program like binary in order to be able to run it so we find that in order to like running throughout TS node then we can do like migration for example through the migration run and now should recognize the imported it should pretty much recognized TS node and everything and here we go we got here the whole curves will be running commit successfully if you take a look in here we can do or we can do like this crowd users if you take a look on the user got a roll has been added into it pretty successfully and it is an integer of course as we specify we want to revert or remove a migration we simply need to do is actually do like revert instead of like run and it's gonna revert the last sub minute or committed migration and the last one only so it's actually like a queue or a stack so if you take a look on that again describe your users you're not gonna have the rule number so or the rule column pretty much it has been disappeared so that's how much migrations pretty much work pretty simple for that if you take a look on here again for example we take a look on the help of the type Ram you're gonna find something really interesting so let me let me just take that up there is actually a migration generate and this one is gonna generate on your migration file with sequel needs to be executed to update so it's gonna generate the migration from the entity so you can run migration generates and any changes made to the entity or your entities pretty much it's gonna create every single migration for them all the migrations folder then you can run them on equations of course pretty simple now what's confusing here migrations are pretty pretty well made and very important actually to work with a database and every single one has migrations and type run migrations are pretty straight to the point and the API is just like simple simple and nothing really hard to work with it so I think that's for watching I know the video has gone too long for that but I just hope I got explained everything you guys wish about typewriting because I know it's actually a really robust and really really powerful or where I'm a lot of people love to work with it and a lot of people would love to learn or master the time around so I think that's for watching us at before hopefully guys have enjoyed the video tutorial and hopefully guys you can just submit that like button and subscribe they're gonna help me a lot so we are almost like 10k and I'm just like I can't can't stop thinking about reaching to in case you think cuz we're watching I've said before please you can just help the videos by sharing them pushing that like button and subscribing because most of the people watching my videos are not subscribed so that's that's quite unfortunate anyway so then just watch I said before for the 110 thousand time I'll see you guys in the next destroyers adios
Info
Channel: CoderOne
Views: 32,710
Rating: undefined out of 5
Keywords: coding train, coding tech, coding interview, coding challenge, coding phase, coding for kids, coding a game, coding apps, coding ai, r coding examples, r coding advanced, r coding 101, web dev, computer programming, programming, web developer, web designer, mysql, database, mongodb, nodejs, Node.js, HTML, CSS, Javascript, Sequelize, ORM, Sequelize ORM, Sequelize MySQL, PostgresSQL, PHP, Python, React.js, Angular.js, Vue.js, JQuery, Java, typeorm, electron database, sqlite, OracleDB, Nodejs ORM
Id: Reb7ISQZCvA
Channel Id: undefined
Length: 53min 44sec (3224 seconds)
Published: Mon May 04 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.