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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hang on guys were doing and welcome to brain evil trial from my peers today we're gonna take a look on sequence and how you can use this awesome Aurum for creating your own database schemas models and interaction with the whole database systems what is sequence sequence is basically an ORM is an object relational mapping so he actually allows you to interact with multiple databases and clearance in his supports post degrees my see who sequel line MSS sequel and anything actually works with the sequel so it has that pretty much support most of the people works with my sequel as I doing here so adds all the support all in one single object relational mapping library in here so it's actually more like a framework that allows you to work with databases you give it your you can actually connect to your database very very easily without any problems and you define your schemas can probably in here or you define your models to be sir better saying you give it your skin out like a username whatever think it is like a string and a date and you can easily confirm all of the systems let's say you were working right now using my sequel and for some reason your application your software needs to like pivots and then change it to completely something different like you know to choose a local offline stores for example likes equal lines so you can easily do it without changing any line of code you only just go ahead and change the delicate in here you change your password and username and whatever information concerning your database system and there we go everything's gonna be working pretty much work for you fine some of you might ask why would I work with sequencing to sequence although rather like other frameworks or other forums available out there and the other thing why don't just go with the basic my sequel set up like using normal queries in creating my normal tables like I would define them in queries and create them and all different basic things that you would probably do when interacting with the sequel database so first things first an ORM gives you an awesome API to work with so you're not gonna be bored or you're not gonna feel a little bit outdated using this API the second things it provides you with a tool set and reach API functions and methods behind the scenes that allows actually to interact with databases as you not exactly doing any queries so you don't really have to care about queries like type your queries like select users or everything user name ID and stuff from the user for example like to grab the data from the table and so on and so forth you don't really have to care about any of that queries you just like for example you define that and you can just go ahead and do I like user create this one is just gonna create it and see if it in a database for you and so on and so forth follow the third things needs to be done so just gonna jump into our projects in here and here we've got add like normal or simple node.js project it's super simple it doesn't have anything really complicated or anything in here just like we've got everything in here and you can just go in and add a saucy folder so especially for putting all of our source files and then up top genius what does the Apple just does is basically just run the Express server for us so simple enough nothing really complicated or anything if you don't know any of that you can just go and watch my no disco series so we're just gonna jump right now to install sequel eyes so you need to install sequels in your projects in order to work pretty much fine so just type in sequel eyes and that pretty much it so just go ahead and install it and save it as a dependency it's gonna be installed and everything working pretty much fun the other thing you need to install is actually the you know the daemon server or the actual client adapter that binds with a sequel so let's say for example in our case and our story we're gonna work with my sequel so we need to install my sequel specific adapter that works with sequel eyes and pretty much you can connect to the database other normal clients so we need the my sequel version to not version one because the version is outdated it doesn't work pretty much very well so you need to select and choose the second version and you can go ahead and easily install it so just save with that as well as a dependency and there we go you can just gonna choose that post degrees or whatever system you're using and you can install this for it the other thing we're gonna use is actually the command-line interface or this from sequel eyes so you just type in sequel eyes - CLI and make sure this one is actually gonna be saved as a global so does G and it's gonna be saved globally so whatever you actually are you can access it and you can run the script without any problems we're gonna use this in line to do migrations and you know connects to the database and interacts with its and stuff like this so this is all you pretty much need to do this sequence the other thing you probably need to do most of you must have done and know about this it's actually running at the daemon server of my sequel so it just needs to be running otherwise it will fail and there we go so everything is working pretty much fine that's gonna try now to start our application and first things first let's start by setting up the sequel eyes or using the sequel IC unless I'm just gonna do sequel eyes actually you install it using sequence CLI command from NPM but it's the name is actually completely different when you try to use it so just sequel eyes and here if you just type in dance you're gonna give you a list of commands a list of available commands and clearly seen here so you can do AB Debbie's migration you can create on migration initialize and stuff like this so first things first what we need is just to actually initialize our project since it's like a branding your projects we don't really need to do anything rather than just initialization so just type in sequence initialized and make sure you are inside of the projects and a lot of things gonna be initialized so you're gonna have a models folder you're gonna have a migrations folder and you're gonna have a new config folder so this conflict folder represent all the configuration representing your database and three stages like development testing and a production we're gonna just probably use development because we are inside of the development stage in here the other thing in here you've got a models and it has a pretty much a pretty self-explanatory model code already being put for you you don't really need to use this necessarily you can just go in and put that which we're gonna do and I'm gonna just go ahead and delete it but you can keep this and use it it's gonna work pretty much fine for you but I just want to show you everything from the ground up and how to work with the sequel lines and learning in one single video so after watching this video you hopefully you can go ahead and pick up your project and work with secret relies on your problems putting your models together your migrations and schemas you know trying to use the API with eagerly Odin you're gonna see all of this in sign of this video so hopefully you like it guys and you're gonna try to start in here let me just put the first file it's gonna be the bootstrap so this one is gonna be representing or pretty much it's gonna take care of bootstrapping and actually initializing the application so this is all its gonna pretty much be doing I'm gonna have another folder it's gonna be the models and you're gonna put them all those inside of the SRC because it's a little bit much more convenient to work with models that's why I'm putting it inside of here and yeah so this is this is pretty much what we need for now and we're gonna start off by putting also another folder and it's gonna be database folders so we're gonna just name this database and instead of this database I'm gonna name it connection again this is just my optional thing so I would like to organize my files and my projects structure as it is I can clearly see in here of course everyone has his own opinion everyone has his own preferred project structure so you probably have much better project structure than I do in here so you can just go and put that in the comments of course I'll be very happy to know about this or you can just go to put it in your projects and be very happy about it with all your problems so this is gonna be actually be able to connect to the database and open up the connection between sequel lines in the actual demon server that is running on our local machine and first things first it's gonna do sequel eyes in here and we're gonna just go ahead and import it or require it pretty much so I'm gonna just go ahead and require seek wise and now we need actually to open up the actual connection so I'm gonna say sequence and it's gonna be representing the actual instance of our sequence so this is actually the actual framework the main framework a library and here we've cast the sequence so this is the main entry points and this is our instance of the connection to the database and I'm gonna create a new instance in here so I'm gonna say new it's gonna take a sequence and it's actually a constructor taking a couple of parameters first first we give it our database so for example it's gonna be we're gonna name our database let's say it's gonna be on a social network all right so we're gonna discrete it social network database or small Twitter clone thing on our here and the second thing it takes is pretty much the usernames are gonna be root in our case in my case pretty much it's gonna be rude the password is gonna be empty so I'm just gonna leave this empty and there's a couple of options or you can first like a host which house is actually running on and you can give it one 27001 which pretty much represents a local host so you can just put a local host in here and now you've got a delicate what do you mean by a delicate is which system you are gonna interact with so my C will post agreed or sequel lines or whatever in our case I'm gonna just use my sequel of course and you can just go in and disable this of course like operators aliases and you just set false because it is deprecated and it's gonna be removed in the last in the next version major release so make sure to do this real quick and everything gonna be pretty much working fine simple enough normally just coming in quickly export this as a default so I'm gonna see seek wise in here simple so I'm just gonna you know simple export in here also what we also need is this is actually a dependence file so I was gonna be like cold for example it's have the up top Jason here so I'm gonna require it and it's gonna have this initialization and the connection is gonna connect with all your problems but later on we're gonna need this instance the sequences that is being returned from the connection can structure itself we're gonna need this to create small tools to actually interact with a database like say from a database and insert and fetch data from it so we need these two acts as a global variable so we don't really need to in a it rich time to require this over and over over again we can just go in and export it as a global variable again global variables when no GS aren't really good but in our case scenario are better than just going and requiring every single time and so on and so forth so you can find a better architecture for running on your application there's a framework some boiler palettes specifically made for this but I'm just gonna work with global variables we're just gonna say global the main global variable I'm gonna set a sequence on it and it's gonna equal our sequence instance in here really simple nothing really complicated or anything simple enough so we're just gonna save this and it's gonna have this one I'm gonna just jump into the up dog genius in here and let's say I'm just gonna be putting it inside of here so DB connection so I'm just gonna put the connection in here and I'm gonna require it this is all I'm probably gonna need and it's gonna need to go to cytosol see get a basic connection simple enough so it's gonna like actually require it connect to the database and export this to the global variable now since we have connected and everything is broken pretty much fine there's one thing is actually missing we haven't created database yet which we need to go ahead and create this database so let me just go and use of the my sequel CLI or command-line interface to actually interact with the database itself who's gonna say that username is gonna be roots in the password is actually empty so leave it as this and I'm gonna create a database I'm just going to execute a command real quick so create database and rename it's the same as we need our database so it's gonna be social network and yeah there we go now we've created a database and everything is looking pretty much fine we can just go ahead and now increase our first model so gonna go inside mortals and create our first model since we're gonna create a social network and as I've signed and gotta create something like a Twitter clone so simple model will be the first simple model is gonna be likely a tweet that represents a single tweet being the tweeted by a single user of course and I'm gonna create that so we're gonna say tweets and my naming convention from modules or models in pretty much any like API ran MVC architecture is actually just putting the first one as a capital letter so to make it look a little bit like a model and something special or something so yeah you can just kind of use your own convention a lot of problems yeah there you go and here I'm gonna just gonna require the framework it's all because it has kind of types and stuff that we need I'm just gonna say require sequel eyes once again and I'm gonna explore so I'm gonna do more little exports and here we need to define our schema so we're defining schema we need the actual instance the SSL that we have just exported as a global variable remember that's inside of the connection we did global sequence and we exported this as ice equalised so now we can either access it through the actual global objects which is pretty boring and we can just go ahead and do it directly because now it is an actual global variable we don't really need to use any other references to us just the name of it and there we go so another capital letter because the instance is just like with a lowercase s and everything will pretty much fun so we need to go in and do a define and defining is actually a method it takes first the name of the model that you wanna create in this case it's gonna be tweets and the old things are here the case structure or pretty much the schema that you want to assign into this particular model what I mean by schema is basically what you actually see as tables or columns inside of the table so this one is gonna be the actual table which can have you know work with sequels and sequels gonna name this by default by adding it ask to make it a pillar like you put your model name as a tweet and later on when you actually migrate them and stuff so we're gonna just name this tweets with an S at the end to make it look like a plural since we're gonna have multiple records inside of this particular table so this represents a table and all the schema attributes inside of here gonna represent each column assigned to this particular table so for this one that's gonna try let me see so for the tweets it has to be really simple but we can put first the author is gonna be like an association between the tweets and again clean another model so for now I'm just gonna have a Content all the tweets so the tweets gonna have too much of contents and we can go in and use sequel eyes and this is the main part so you from seek wise or through the sequence main exploring here you access which type you want like a text of time integer or something and we just want to string and here if you just try to put this as a function constructor you can put in some parameters so the parameter is one of the main problems is actually the length of the string you want for example we want to string about I don't know the actual 300 characters I'm max so Twitter for example I don't know how much Twitter actually the limitation of characters but it's gonna try to put 304 now with on your problems and the first thing that's we all chilli forgot about is the main idea box I'm gonna identify each row alone so we're gonna have that as an ID so it's gonna be hungry you can either put in some objects in here or you can directly put the time so I better put the objects in here I'm gonna say type equals C equal eyes dot string or pay much this is not a strings it's gonna be an integer so integer with 11 this is actually a standard thing for the IEDs I'm gonna have a low knoll to be set to false so there will be no no inside of the IDs and all two increments is gonna be such a true and primary key also is gonna be such a true so this is gonna represent our primary key and it's gonna be auto incrementing whenever the new value is gonna be stored inside of this table simple enough so let me just put that in here and that's all pretty much we need to anchor a tweet so we just means you need to make it very simple very very you know straightforward without any complicated things or stuff like running on behind the scenes so remember we have out of the tweet in here but we haven't added any migration yes what you mean by red migrations for some of you who doesn't know what our migrations are basically a piece of like tasks that runs and creates the structure of your table inside of your database system so this is all basically is this model is gonna represent it and it's gonna actually be able to insert the DNA inside of the table but if you don't have the table you won't be able to ensure the DNA at the model DNA inside of it so first you need to on create a migration that is responsible for creating a single table with its columns on the database then later on you can go and increase that so I'm just gonna copy this attribute because it's pretty much copy pasting and I'm gonna use now time to use actually the CLI so I'm gonna say C equal eyes and I'm gonna do a DB or just like migration and I'm gonna create and you my equation so this is this is pretty much all you need and you can give it a name attribute it's actually required so a name attribute is actually what you want to name your migrations for the naming convention for migrations a lot of people actually in love and II use is actually you make it like a verb or like a to do things so once each migration is gonna pretty much do for example is gonna create tweets table so this is how you want to name your migrations to make it straightforward for anybody actually reads your code gonna be very easy to define or identify which each migration is actually doing so running this commands gonna pretty much go inside of the migrations folder and I'm gonna be creating that's when you see new migrations with a timestamp in here if you open up this you're gonna find two methods one is down and the other one is up so what does the app does is when you run the migration and the table structure hasn't been migrated yet it's just gonna go ahead grab this migrations or grab the attributes you put inside of this app and it's gonna submit it to the database if we try to in do or but roll back your migrations what's gonna happen the down methods gonna run in here and it's gonna for example drop the table if users or drop any other tables specifying here so let me just get rid of fats and get with that comments here it's just like for self explanatory and I'm just gonna use the query interface now so the query interface allows you to create tables columns and interacts with like interacting with a database itself so I'm gonna do create table and the create table what it does it takes first the table names to clear your scene here so what is the table name I'm gonna need MIT weeks the second thing it takes the actual schema I'm just gonna paste in this came out with an ID the content and there is one more other thing very importantly that's actually sequel eyes when you try to insert any document into it it's gonna be automatically assigned into attributes one is actually created adds and the other one is updated at so whenever you create something is gonna put the date and time inside of that attribute and whenever you update the particular record it's gonna you know update the updated time or update its energy beauteous out of its so I'm gonna put that too in here so created adds and it's gonna represent a sequel eyes dots dates and the same thing goes for the updated that's sequel eyes doc days again but we still haven't got sequel eyes in here so I'm just gonna require really quick or yeah we've got that so yeah we don't really need it anyway so yeah maybe second here and pretty much all we need in here very simple very very straightforward nothing really complicated in the down what we want is to get rid of the table so we're gonna call great interface dots drop table and for dropping table we're just gonna give it the table name that you want to drop and you choose tweets the same thing that's we've put it up there and that pretty much always gonna be doing in here simple enough now if you try to run your migrations how you can do is you run sequel eyes throughout the CLI you did EB migrate so this is this is basically all you need to do for running a single migration that is newly created inside of the migrations folder and just run this command yeah here it tells us like known development database because we haven't updated the convict JSON which you probably need to do so i'm just gonna update this really quick it's also showing that's work and it's gonna be routes the password is known and the delicate is my sequel you can also update this for the production if you win in therese this but for now it's it's enough for us so I'm just gonna run this again the migration and you should migrate so yeah I think I've got a error in migration yeah I've completely messed up so I need to return a promise because this is actually gonna return a promise and this up method expects you to return a promise so just return this also make sure to return this otherwise it can gives you this really weird error and running the migrations for the last time right now and then we can just go in and take a look on that so now it's clear to see the credit tweets table migration has been run successfully we can go ahead and do describe and tweets when this let me just first select a database which is gonna be social network okay using a database and then we can actually describe the tweets and there we go I've got created at the constants the ID and this is our table so describing is just basically telling which what is the structure of your table simple enough now we've created the tweets we have got another model to be created it's gonna be pretty much the same so I'm just gonna be copy/paste to name and that's gonna be the e user so use your gonna have a single or pretty much multiple tweets to be put on tweets on your and his account but even each tweet is gonna be belonging to a single user and a single user only I can't have a tweet that belongs to multiple users or something like two or three users that's that's completely not how it works so you need to understand how your system works in order to be able to do this kind of things and you know set the database architecture of it I'm gonna have the user genius you can name this author because it's the same thing for putting the tree as an author and stuff but it doesn't really matter that much we're gonna name this to user it's gonna have the same ID so no change happening here but in this it's gonna be another change in here I'm gonna have for example only a user name for that and I'm gonna open up objects it's gonna be sequel eyes dots so the user names gonna be a string let's say I'm gonna give him about 25 characters to represent his username well let's say I don't know 35 characters okay that seems good so for alone ol it's gonna be false we won't be able to allow and no and also we're gonna add a unique constraint to make sure that each user name is actually unique so there's no two users having the same user name we can can actually afford that and there's a couple of other options actually add in here for each attribute so you might wanna check the documentation for how that works I'm also going to add a password I'm just gonna see passwd I'm gonna call the most of these and have a string again for the password we're just gonna try to limit it to 20 and unique of course knots and and that pretty much it so you know nothing really excited about this let me just gonna now generate the migration so I'm gonna use C pole eyes again I'm gonna do migration creates and I'm gonna put the name of the migration in this case creates a user's table and you should actually does the job for us perfectly I'm just gonna copy this because it's gonna have the same fields nothing really complicate or anything so I'm just gonna copy this get me to the create users migration pasting it again and let me just go back in here also to copy the skin of this and the whole attribute so we don't really want to just keep putting them I remember the Creator has in the updater that's always are in there so we don't really need to mess up with this and we just pasted all things in there so yeah there we go it works and here we can change it to users and of course your users and you should work pretty much fine now so I'm just gonna try to run the new migration of users one other thing that you really forgot about is actually one single or a user has multiple tweets and a tweet belongs to a single user only what does that mean is actually we store the user ID this specific user ID on every single tweet he actually tweets out so this is what we need and we totally forgot about theme adding this and each tweet in here has to have inside of the skin ID cell where is the skimmer inside of the actual migration schema we need to end like a user ID field in here and in order to make it work so it's gonna be sequel eyes dot integer it's gonna have for example a logo integer because it's gonna be the actual user ID and it's gonna be a foreign key that is being put inside of that now simple enough this is this is all you need to do but we already a migrated to twist table so I'm just gonna roll back this I'm gonna do sequel eyes and - it's gonna be database my greets but we are gonna just in do the last migration just this is gonna be in doing last migration you can do and do all for in doing all the migrations that has been submitted to the database but we just want the last one and run dance so to see and there we go you've got yourself like she all the migrations have been gone and nothing left so pretty much so it working pretty much fine without problems and we can redo the migration now because you know you have to drop it first then you can just go in and do it so I'm gonna put sequel eyes DB my grades and now we should work pretty much fine with the user ID inside of s and it migrates both of them the users and the tweets good enough now we've got all of those let's go and try to work with the API and try to fetch or put some dia look at the bootstrap and we are gonna just put some fake DNR for our database inside of the bootstrap using this equalized ap on for this one it's gonna export in something's gonna do export it's gonna export an async function so I'm gonna use the modern async/await and I'm just gonna interpret be exploring this particular function nothing more nothing less so first things first we're just going to try require the both of the models so it suite model it's gonna be required I'm gonna be a set of models actually tweets the other thing he needs is actually the user model so I'm gonna require again and models user and there we go so both of these are gonna be actually the model instances and we can actually call the methods inside of that and we can interact with a whole API so we'll first we create a fakie user then me and some fake tweets to it so if gradient user we access the actual model from throughout the user model and we do create so this one what it does is just gonna create the schema using a particular object that's we're gonna give it to the attributes and it's gonna save it automatically for us it's pretty much it's gonna return a promise for us a promise that gets resolved with the actual DN I created if you works pretty much fine without your problems or it's gonna throw an arrow and the catch method of course if anything bad happens with an error of course so here I'm gonna give it the DN a-- that we want of course if you take a look on the user would have the idea is gonna be also filled for us that we don't really have to care about this got user name and password so I'm gonna put a user name it's gonna be for example say Alex DMC or something on something ridiculous and for the password I think they put the password this way alright and I'm just gonna put from nine or from one to zero and there we go so this is simply how you create things and as I've said is gonna return a promise for you so you need to catch the promise so I'm gonna say error and if there's any errors or pretty much it's gonna try to create an air handler first like a custom or a generic error handler for also error handler and it's gonna be taken in error and all it does is just do console dot error it says error just passing the error again simple enough nothing recom placated and on the couch we can just pass the reference to this handler and you should work pretty much fine in this case since I've marked this asynchronous I want to use the weight if you don't really want to use a weight you can go in and use the thin function and you can receive the DNR like you can you receive there in here that I has been saved to the database and you can work with it normally but I really like working with a sinking a weight for just making it look like a little bit syntactical sugar and now just supports it now so it's pretty great so I'm gonna put the weight in here and it's gonna return like the actual user instance so yeah there we go simple enough no try to add as well a tweet so I'm gonna do tweets equals bullets or creating a new tweet you threw out the model and it's going to be pretty much the same thing so between what we have we've kind of just accountant to be filled in so it's gonna be constant and here I don't know this is actually the tweets constant and let's try to add like tweeted from I found that's that's pretty ridiculous anyway so we just enter the contest in here and remember each tweet has a relation to the user so each tweet has to be keeping track of the actual user ID so remember that we need to every time a sign of credit to reach we give it which a user has created this suite which is throughout the ID itself so I'm gonna do a user ID and we give it a user ID throughout the model we access the ID so pretty much how you do it is actually you can imagine this as a normal object that has attributes inside of its user name and should be the password RGB and of course the ID that's automatically fueled for us so we can throw out this objects access this ID or any other apps you use pretty much you can also use the get method then like go get and get whatever attribute or the name in here by really prefer just using it as a normal object notation in here simple enough of course you need to catch this if there's any errors or something and we can pass the air handler so yeah there we go and for the username in here is gonna be unique so if we try to like assign this or create two instances of the main the same username and save it to the database you're definitely gonna get an error and this error handler gonna be executed for us so yeah we just need to make sure that we run our application only ones just for having a clean code and stuff or a clean outputs there we go I've got a tweet in here and everything should pretty much work fine so this trees is being linked to this user and this is how you can pretty much go ahead and actually link to tweet using associations but the other lens we've created as we've created the user of credit tweet but we haven't associated them yes so after creating the models and stuff you need to always associate both of the models that has relations between them so since the user has multiple tweets and a single tweet has only one single user so we need to assign or let's equalize know about this association throughout the model so we say user has many so this method actually is pretty defined for you you give it which target that has many and of course it has many tweets you give it a model instance in here and the second object it takes some options so you can put which are pretty great so it takes like an ass objects or as option so what are gonna be the name is out of this actual user so when you put in inside of the tweets they're gonna be tweets so gonna put in like advance and let me just gonna put this as in capture ledger and gonna be a foreign key so and wait by the foreign key is the usual key can I be put it in some of the treats so what are the name of the the column name that's gonna be put it inside of that model and of course we're gonna keep naming it use your ideas we've created that in here also we created inside of the migrations scanner so this is what we want to call it on the other hand we also need to link it from the tweet itself so we need to do a tweet belongs to now since a tweet has one single user only so we need to belongs to relation or Association and we give it where it belongs to the user some options as well like as and as actually is is completely optional so you can get rid of it as well as the foreign key so it's automatically gonna assign the for a key by joining the name of the model plus the its actual actual user or primary key name which is ID in this case so this is how it pretty much works so there are optional but it's better to add them for better understanding of the schema and here I'm gonna add like user the foreign key is gonna pretty much be the same thing because they only it's actually one-to-many relationship it's not many too many or one to one it's one-to-many relationship and that's how you feel the assigned while some of you might ask why we don't really do just one single Association why do like both ways Association simply because if you try to access from the tweet and we actually grab all the users we will be able to as well as on the other side we try from the user to grab all the tweets but won't get to this particular user will also be able to do so so yeah that's that's pretty much how it works and there we go now we've got the user I've got the tweet created for us and stuff now how we can go ahead and fetch some DNA from the database so really simple actually just we've created the user and created the tweet and both are now associated throughout the user IDs clearly seen here throughout now the tweet model so let's say you want to grab all the tweets born into a specific user or better say let's try to like usually use your model and grab every tweet that belongs to this particular user so I'm gonna do user-defined oh and here it takes an object to filter the user lock which a user finds and it's gonna find every single user that has this condition in here at the object condition with the attributes of course since we have got a unique user name so it's gonna only find us a single user that with this particular user names is gonna put before this I need to put where so user name equals Alex DMC and here it's gonna find this test and we are gonna include our association so we gonna you can include this foreign model to it so this includes I hidden away if you have like multiple attributes and stuff you can include like I don't know if you have multiple associations with a tweet with a portfolio with an options tag or with another things actually on the Twitter happens you can just add all the associations in here in order to be able to eater Leo them so it's what eagle the audience pretty much is or in other words they can also call it as populating from the database so here I'm gonna add objects who's gonna represent this I'm gonna add the model so it's gonna be the tweet model this one this is what we need and we need to end the ass so that's why we add the as option in here or the as attribute in order to later on when we give Leo them using the include so we can easily just specify this name and it's gonna grab this for us so here we need to specify it the same name so it's gonna be tweets make sure to do cell the same name here and everything gonna work pretty much fun now it tries to do is find the user with this username the Alex DMC once it finds it it goes using this include it goes in it finds us all the tweets related to that particular user and of course it got gravis add all the three stars related to the user because we told it throughout this include model it's gonna put it all inside a single object for us and give it that to us and of course we need to catch this too so I'm gonna just put your handle and another less I'm also gonna be able to much to use a weight in here because it's a promise and they're gonna promise to give us all the DNA inside of that it's gonna be array of DNA or pay my grave users so I'm gonna have this in here simple enough if you try to fetch from the tweeds fetch all the user or the single user that he has gonna do pretty much the same thing find all with some condition to that tweet and you can include the mode of user instead of the tweet in here and you give it the ass relation like the user in here and and that's pretty much it so yeah it's not really that much of complicate or anything and yeah so now we're going to do like counsel the log I likes TMC tweets and it's got grab-ass this I'm just gonna do so just gonna just with all the users with their tweets I'm gonna be console logs for us in here really quick I'm just gonna save this so hopefully now we also need to go to the audience and run that too so after the requiring the connection make sure to do so after requiring the connection I'm gonna also go ahead and require a saucy bootstrap and just the bootstrap and it takes a function so we need to execute this and there we go so this is all you need to do run that again one more time and there we are now actually works pretty much fun it pretty much just gives you some very boss information about what's happening behind the scenes once equalized so like inserting into tweets or other deals being ensured is just like shows you the query the low-level query of my sequel the actual sequel queries and in here we've got our tweets so we've got Alex DMC tweets there we go I've got an array and it gives you like the ID use your name the password and create an ad and time stamps like updated ads and lastly we've got our tweets using the eagerly Odin as we have created of course got an ID contents whatever tweet as we have gotten here like tweeted from iPhone and of course other attributes like the updated and created us and most likely the user ID which links us to pretty much this user so this is how eagerly Odin works and how you can include one model to another really easily to speed up the work in progress and gets or fetch edn up really really quickly without any problems and and put in a lot of code together to gets this kind of things so that was like pretty much actually guys just trying to explain basic things really basic because sequence is just just like a huge framework and I gave this video to just explain it to you guys really quick so hopefully guys you've enjoyed it make sure to check out the documentation because a lot of things I'm not saying in this particular video simply because they don't have that much of time to do so so again think guys for watching I really hope you guys have enjoyed push that like button if you really liked it if you really want more videos like this to learn every like single framework or something or master it in a single video and I will see you it sure hopefully in the next one
Info
Channel: CoderOne
Views: 149,648
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
Id: pxo7L5nd1gA
Channel Id: undefined
Length: 42min 31sec (2551 seconds)
Published: Sat Feb 16 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.