TypeORM Relations Tutorial - FULL details!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey how's it going in this video we're going to talk about relations in the context of type orm so on the screen here we have an example uh erd relationship diagram and this is what we're going to build in this tutorial on the right we have if you're not familiar with the notation of this diagram you know you can take a look if you want to follow along i suggest you maybe pause the video at this point and draw this diagram on a piece of paper so that you can kind of understand what i'm doing in the code as i'm typing it out i also wrote this set of requirements to kind of for people not as familiar with what this diagram means here's basically what we're trying to build right we're gonna have an employee table which is self-referencing so an employee for example could be a manager right and a manager can have direct reports but those all those different types of people are represented in our database as an employee so we need to be able to have a self-referencing column here where an employee can have a manager hands manager id and then an employee also has a one-to-one relationship with contact info right there shouldn't be anyone else with the same contact info as them so there's there's there's one and only one contact info per employee and then next an employee also has one too many tasks right maybe they have projects or stuff to do on the top here you can see that there is a many-to-many relationship between employees in meetings right so an employee can belong to multiple meetings and meetings can have you know any number of employees as attendees and that relationship is represented via this uh kind of intermediary relationship table so i do kind of expect that you have a little bit of fundamentals when it comes to sql and relational databases so i'm not going to go into too much detail describing you know what are the differences between these relationships just know that this is what we're going to try and emulate this is our requirements and i'm going to show you how to do that in typo rm so with those requirements out of the way let's jump right into the code and what i have here is a basic nesjs application now you don't necessarily need to use sjs or type or m with sjs but people that do use nest js there's a high tendency that they'll probably use type rm so i figured it it kind of makes sense to do it in this context now i'm not gonna go over how to set up everything from scratch in terms of database connections and stuff so i already kind of pre-configured this to have a connection to a sqlite database and i have another video specifically on the fundamentals here of how exactly you would set up these connections these configurations this is probably a good video as a follow-up to that first video so if you're completely new to type rm and sjs you might want to check out that video first so real quick let's walk through the code that we have so like i said we simply just have some configuration here to spin up a basically a fake database right using sqlite and it's just locally saved in a file in my desktop you'll notice that our basic application here has this app service this is where we're going to put our database logic our querying and then this is referenced in appcontroller which just allows us to kind of trigger this logic to run right so that's kind of our bones that's what we're going to get started with all right so first we need to create our database tables right and if you've seen my last video on type 4m you know that usually the way to do that is by creating entities which represent your database tables all right so why don't we start with the employee entity so i'm going to create a new file called employee dot entity dot ts and this is just going to be a class called employee and remember that this needs to be an entity we're going to add a primary generate column this just means that we have an auto incrementing int from the database perspective so each time you insert an employee it's automatically gonna use a number that increments by one each time next we'll just add a basic name column which is just a string or a varchar from the database perspective um and that's it for now we'll start with that all right from here let's talk about our our next table which is the contact info table which i'm gonna create a new file for contact info.ntts and i'm just gonna copy the same exact structure here and of course we're just gonna change the things that we need to change so make this contact info it's gonna have its own id as well that is also primary generated but let's change this name column to what do we have in our diagram i think we had phone let's make this one nullable and let's add another string column which is email which is not nullable right so you have to have an email for every employee now this is where we start to set up our first relationship which is this one-to-one relationship between contact info and employee right so the way you do that is you add a new property here which represents that relationship in this case we're going to say employee right type employee ie our employee entity that we just created and then we're going to tell type 4m what this is exactly which is a one-to-one relationship and the thing you have to pass in here is a function which returns the type which is basically the same as this right so we just put employee in here and then the next thing you need to add is a join column now the join column basically tells us where is the relationship id created right if we go back to our diagram you'll see that our contact info has an employee id right so that's there because we have join column over here if we were to put the join column in the employee table what we will have is an employee table with a contact info id and this is also mentioned in the documentation for one to one if my explanation didn't make much sense but you'll see it'll say that we also added the join column which is required and must be set on one side of the relation so whoever side that is on will have the relation id and the foreign key for that relationship so what this means basically with the joint column is type 4m is going to add a employee id column to our table automatically without us having to specify it as a column like these guys right you can also add it in like this if you wanted to which is gonna give you sort of direct access but it's think of it as if it's implicitly already defined for you you don't have to do it manually but there is benefits to sometimes putting it in here manually anyways for example if maybe you want to query a contact info and be able to tell um what the employee id is without having to make that join with the employee to then get the id so it's it's pretty useful to have it in your model like this so that you can reference it if you wanted to so what we have here effectively is a unidirectional relationship meaning that the the relationship that we wanted out of this diagram is already established however from type orm's perspective there's no easy way to query what the contact info is from the users table so what we can instead do here to kind of improve upon that is we can make this a bi-directional relationship and the way to do that is we're going to add employee here and we're going to specify basically on the other side of the entity the employee entity what is the property that relates to that maps to this so let me type this out and then i think it'll make more sense in a second here so on the employee entity it's going to be contact info now notice that there's a red squiggly there because we don't have that yet so to fix that we're going to add contact info here which has a type of contact info now similarly we're going to add that one to one decorator here and we also need that function which tells us what's the type a little bit redundant but that's how you're supposed to do it and then similarly we provide another function to tell us all right in that entity what property does it map to which we already have so it's just a contact info.employee so putting the side by side you'll see that what this is saying is that we have a contact info which has a type of this guy on the right and we we kind of know that it maps to this employee field over here right and notice that i don't have to put the join column on this side because that's already on this side it only needs to be on the side which you want the relationship id to be in right so if we go back to our requirements here we made the employee table um and we also created the info table and there is a one-to-one relationship with employee it i know i'm kind of jumping around here but we'll get back to these other guys in a little bit and we also have the bi-directional relationship between you know coming from the employee side now you might be wondering what scenarios would you do unidirectional versus bi-directional well if you don't know i would suggest you just do the bi-directional which is going to be more flexible for you as you'll see later in this video when we start doing querying but basically for example if you're on the employee table and you want to be able to query employees with their contact info you probably want to have that here if you don't need that then you can do a unidirectional relationship now there's one more requirement that we didn't get to which is we said that we want to be able to make sure that contact info is deleted if the employee itself is deleted we don't want to have contact info that's kind of just floating in space right so the way you can do that is on the contact info entity you can pass a third argument to this on the one to one decorator and you can add extra configuration here so for example you can add on delete we'll do cascade right and what is this what does this mean is saying that on delete if this employee got deleted cascade that delete to this record this contact info record so if the employee got deleted their contact info gets deleted as well if you know your sql well i i bet that already makes a lot of sense to you so we'll just move forward from there alright next let's talk about task which in our diagram is this guy on the bottom here which just has a name and you can see there's this relationship with the employee id right so we're going to create another file task.entity.ts to save you some time i just type this out so kind of same thing right it's got its own id primary generated it's got a name column now let's add the relationship between this task table with the employee table so we said that a an employee effectively has one too many tasks it might be simpler for us to start adding it on the employee entity here so we're going to add tasks which has a type of task but as an array and then we're going to say that we have a one-to-many relationship here and similarly we specify what's the type and what does it map to on the other table or entity in this case we're going to do task dot employee all right again it's red because it doesn't exist yet on this side the task side so we're going to add a employee property here of type employee and we're going to say this is a many to one write the inverse of one to many right again provide your type i really wish it was able to determine this automatically based on the typescript type here but it doesn't so there you have it so let's do employee hopefully.tasks the thing we just added earlier so you're probably starting to see the patterns here right and then on the third thing here we said that earlier we said that if an employee gets deleted we want to set the employee the assigned person to this task to be null right so we're going to do on delete set no which makes sense if you think about it right if you're in a company and perhaps you have a set of tasks if one of those employees left you don't want to just delete their tasks right you'll probably want to unassign them and then reassign them later to someone else who is still in the company so that's why we're doing set no all right so that was pretty straightforward back in our rel our requirements here we made a task table we made a many-to-one relationship with employee and we said employee id becomes null if employee is deleted and then on the employee side we said that we have a 120 relationship with tasks as you can see here all right so pretty simple right so now that we kind of have a pattern for how to do one too many many to one uh this is probably a good time to introduce the self-referencing uh relations so we said that employees represents you know uh different types of people including managers or direct reports uh that means that an employee needs to have a manager field which is a type of employee right it's self-referencing it's gonna have this manager is going to have the same exact uh fields as the employee itself right they'll have their own contact info they might have their own tasks they'll have their own id and their own name now if a employee is a manager they might also have direct reports right so and that can go both ways you could have a manager and you can also have direct reports so you know in a hierarchy in a typical company you'd have something like that so how do we do this we're gonna do many to one here right because typically you would only have one manager right so many employees mapped to uh typically one manager and at least in most companies you're not gonna have more than one manager so this makes sense to be a many to one relationship so we're gonna do again same thing you provide type and then we specify so if you think about it what does a manager have it's got direct reports which is why we added it down there we're also going to add here on delete uh set no again similarly if imagine you're in a company right and your manager decides to leave it for another company we want to make sure that when that manager's record gets deleted it doesn't just you know delete the rest of their direct reports record so you don't want cascade in here we just want to set no because likely in the future where you're probably going to get a new manager for that set of reports direct reports all right and then what do we do for direct reports you're probably seeing where this is going right which we're going to have one too many on this side and why is that because any given manager has one too many direct reports and again what does this map to in our entity is manager all right and i know we're kind of doing a lot of setup here but we're also going to i'm going to show you how to insert data with these relationships as well as how to query them but for right now it's it's much easier to begin with the end in mind right you you start with your data model and then you kind of fully implement it and then you can do your your queries and stuff so back in our requirements we did the self-referencing managers and direct reports and then we're going to do on delete set the manager to null now the last thing we need to accomplish here is a many-to-many relationship with a meetings table so again we're going to create a meeting dot entity dot yes and just to save you some time because you've already seen what i'm going to type here right uh i just made a new entity class called meeting it's got its id it's got the zoom url as a column right meetings usually you know that's a virtual meeting maybe and then uh the thing that's probably new at this moment is we've got a many too many relationship with uh employees so which we're going to call attendees now again similar thing what does this map to on the employee entity it's going to map to a meetings property which is a list of meetings now similarly we're going to add a many to many here and then we're going to add a join table let's make sure we got these imported now to explain this joint table a little bit i pulled up the documentation and you'll see that for the many-to-many docs that says that joint table is required for many-to-many relationships you must put it on one of the owning side relation and what this effectively will accomplish is our diagram here basically when you do it that way it's automatically going to create for you type or m is automatically going to create for you the uh the relationship the relationship table like this now why did i now why did i put that joint table on the employee side and not the meeting side well i kind of think of it in a way that an employee can probably own a meeting but it doesn't make as much sense to me to say that a meeting owns an employee they do have this kind of intermediary table which represents the attendees and you can configure that via join table here if you look at joint table there's stuff that you can pass in here like uh what is the name of that uh table that's gonna be created if you don't pass anything type four and we'll kind of just automatically name it for you now another question to ask is what about the deleting behavior well um with many too many it it automatically kind of cascades by default so you don't really have to configure it so for example uh you know obviously this table represents the the relationship between the two if if an employee were to be deleted it should automatically just remove their record in this table right there's not much more you have to do there it does that automatically and similarly if a meeting got deleted it's gonna delete all of those uh records that has that meeting id so it does that already for you automatically we don't have to configure anything there all right so at this point assuming i didn't make any mistakes which we'll find out as we go through but i'm pretty sure we have implemented everything that we need to represent this er diagram we did the manual painting relationship with meetings and we created a meeting table and the many-to-many relationship with employees and then uh we said that this part is actually automatically handled for us so we don't actually have to write any extra code so there you have it we got our schema now let's transition to how do we insert data into our database you know knowing that we've got foreign key constraints right on some of these how do you create new records for each of these tables as well as the relationships between them how do you do that in typo rm so that's what we're going to cover right now so if you watched my previous type ram video you probably know what we need to add here right which is a typeware module for feature which basically provides a list of the entities that we want to be able to access within our service so let me just make sure that these are all imported right so we're getting all of our entities we're gonna add it to typo module for feature and what that is gonna enable us to do is within our services we're going to be able to add injection here of our individual repositories which allows us to query our individual tables so the way you do that is usually in within the constructor here there's a shorthand that you just do these inject repositories with the type and then you just specify a name for that repository and then you know it has this type right and i know i'm going kind of kind of faster but this is stuff that i did cover in my first type 4 and video again ideally you watch that before this because it gives you a lot of the fundamentals of how do you do this stuff in the nest landscape all right so let's say that we wanted to perhaps create a function to seed our database now obviously you would never do this in a real application right this is a tutorial so i'm just trying to show you guys how to create uh records for each of these tables and how to relate them together in a more realistic scenario you'd probably see your database using you know migrations or maybe you'd actually call your api to add data so just know that this is not representative of uh you know a typical application but let's let's just let's just get started here perhaps we want to create a new employee we'll call him the ceo and again you might know from my previous video is the way to do that is we do employee repo which we have injected up here dot create and then within this we can provide the fields that we want to give that record so let's call this person mr ceo right and what what is this exactly this is kind of equivalent to doing ceo equals new employee and then doing uh ceo dot name equals mr ceo so you can do it that way too right but that kind of gets ugly really fast with um large entities so it's it's better to just do do it via create it's much easier because our entity classes don't have a constructor right it doesn't have a way for us to provide it in you know like this so the create is very useful in this scenario so it will stick with that now create doesn't actually save it in the database and make this async to actually save it in the database what do we do we do a weight it's that employee repo that save and then we pass in that object now once it does that save that's when it kind of turns into an actual database record which means at this point the ceo now has an id right it is now actually saved in the database so you can do all sorts of stuff with that so perhaps let's start with a you know we said that there is a one-to-one relationship between employees and contact info so let's create a ceo contact info so in this case we'll use the contact info repo here and we'll also do create and we said that email is required so we'll do you know i'll just do email that email right and at this point we don't have the relationship yet right and you know that we're we're probably gonna do a save as well here let me just type this out real quick right which does the actual insert into our table but how does it create the relationship between the two well you've got two options since we already saved the ceo here and we said that we kind of have access to that id since our contact info also has this employee id property one way to assign that relationship is to do employee id and then we pass in our ceo that id so that's one way to do it basically manually kind of relating via the id columns now what i would actually recommend you kind of start getting familiar with is another way to do this is by working with these objects directly so if i do ceo contact info dot employee equal ceo that will achieve the and when you say that that will achieve the same exact thing it's automatically going to figure out that because we're saying this relation exists a contact info has a relation employee and we're saying that's going to be the ceo it's automatically going to map that to have the correct employee id on save right and this is a pattern that you'll see throughout this video and in general probably as you know when you're typically using the the repository api with type or um right so at this point we've got a new ceo he has a contact info and we've made the relationship between that contact info and the ceo let's go ahead and create another employee so let's create a new employee as you know the next person in the chain maybe the manager will give that let's make that me basically and then and we can also assign the manager of the manager to be the ceo right the report to somebody the only one that doesn't really have a manager is the guy at the very top right and you'll notice i just added manager down here instead of outside like this you can do it either way really but i'm kind of just showing you the the different options right so um i'll show you guys when you run this i'll show you what it looks like in a database in a second here but let's just create a bunch of things um to kind of show you how to do a lot of stuff so we also said that an employee can have one-to-many tasks so let's start creating a couple of tasks to assign to this manager aka me so again you're probably seeing the repository api pattern by now right you do a create so here's a task one i hire people and then you can insert that in the in the database directly we can do save task one and similarly we're adding a second task which just you know maybe i need to make a presentation to the ceo i will add that and also save it insert it to a database now notice in this case we haven't saved the manager yet right so the manager is not yet in the database at this point that's because i i want to create a couple more things in here so we've you know we're effectively going to assign these tasks to the manager and how do you do that doing this kind of notation right so we have the manager object you can just via the the property of the relation you can just assign him like that right so at this point if i were to save the manager right now what's gonna happen is it's gonna insert that new manager record and then it's gonna update these two tasks to have the employee id of the manager right what's another example of this uh we said that we have a many-to-many relationship with a meeting so let's create a new meeting now perhaps the meeting originally only has one person in it which is the ceo so we can do meeting one dot attendees and it's just the ceo right and at this point if i wanted to i could just save the meeting directly and what that's going to do is it's going to create a new record in our employees meeting meeting table which is going to have the ceo's id and the meetings id to kind of say that the ceo is an attendee of this meeting now what happens if we want our manager to also join that meeting and it's already been created previously right well because we have this bi-directional relationship between meeting and employee that we created you can also do meeting that manager.meetings equals meeting one and by the time you save the manager it's also going to create that relationship for you all right so hopefully that makes sense let's try to kind of summarize what we created here right we made our employee one which is the ceo we made and we gave them contact info and then we created our employee 2 which is the manager and we gave the manager two tasks and we created one meeting which started out with just having the ceo but eventually the manager also joined it right so we're gonna run this this all of this code and we're gonna see what our database looks like from here so the way i'm going to run this is since we already have this basic app controller which has access to that app service what i'm going to do is i'm just going to add the call to seed here again this is not representative of an actual application but this is just an easy way for me to demo this stuff so i'm going to call seed and then we'll just pre print out uh seed complete so that by the time it returns we know that our logic already ran so i think at this point we're ready to run our our seating logic here there is one typo that i noticed i had which is in our config here the path should not have src here because the way that our code is set up it's not going to have that src folder in this so just watch out for that if you're following along alright so from here we should be able to now run our application via npm run start dev right so our application run and i should be able to trigger this i should be able to trigger our seating code by going to localhost 3000 right which is just our basic route and once it completes it's going to tell us see complete now let's go ahead and verify that happened notice that i'm using the beaver here as our way to look at our database which is just this file that got created on the desktop so i'm going to create a new connection in here sqlite and i'm just going to select db in the desktop right and just reminder that's coming from this config right here which just says save it in the folder outside of this project which is just our desktop in this case so i'm going to select db here hit open and hit finish if we take a look at tables we'll see the tables that we talked about real quick you can also do view tables and er diagram and you should get to the same exact [Music] er diagram that we had that we started with that we had in the beginning of this recording but let's take a look at the the data that got saved and we'll also compare that side by side with the code that we wrote so we said the first thing we created an employee which is the ceo and also eventually we made a manager down the code so if i were to query the employee table you'll see mr ceo with id1 and the manager me with id2 and it has manager id as one so just at that you know that there was a relationship created when we did the manager ceo on line 34 here that's what it kind of automatically mapped it to have main energy manager id one which is the ceo's id all right let's also take a look at contact info so you'll notice that we have a single record in here with the email and employee id as one right so again that maps back to our ceo who has an id of one what else right we created two tasks and assigned them to our manager so if i were to query tasks there's two things in here that has employee id of two and then finally our meeting right there should be a single meeting created which is just a meeting dot com and then we can see who the attendees are by looking at the relationship table here which you'll see there's a record which represents the ceo attending that meeting which we have in the code on the right here on line 45 and then there's also a record here which represents uh the manager joining the meeting as well as an attendee from what we did here in line 48 right so to summarize right i hope you kind of get the point of uh you can do effectively you can do relations by you know saving and creating individual things and then basically just assigning them as if they were regular objects like that and then when you hit save on those it's gonna make them real right it's gonna auto assign the necessary ids that it needs to assign wherever all right so hopefully that gives you a pretty good overview of how to insert and kind of make the relationships between those inserts let's go ahead and start talking about all right now that we have these related items how do we go about uh querying things together right perhaps i want to be able to query a manager with their direct reports right how do we do that so in our app service let's go ahead and create a let's make this get employee by id which takes an employee id and remember from my previous video we said that you can do a select all using find right this will do select all from the employee table and you can also do find one and pass an id there to get a very specific person now how do you get the related things with this query well what are the relations that the query has or what are the relations that the employee has so if we take a look at our employee entity you know pretty much any of these things that are many to one one-to-many one-to-one any of these relations are things that we can include so for example we can say we can add a relations array here and we can just add in here all the things that we want to pull in so for example i want to get the manager direct reports what tasks do they have contact info and finally what meetings are they in right and these are strings that represent these relations right meaning so the name you provide here is the name that you need to provide as a string to that relations array so that's saying that you wanna kind of behind the scenes you want type rm to do all the joining and whatever to pull these in so let's take a look at what this looks like if we were to update our controller to now instead return this dot app service dot get employee by id and we'll just pass in a one here remember that our ceo our very first employee has an id of one so that's so that's who we expect to get back when we run this so again i'm gonna run the application npm run start dev and once that's up let's go back to our localhost 3000 hit refresh here and we'll get back the information of our ceo right so um on its own if we didn't have that relations array it should just return us the id and the name but because we added that relations it pulls in the manager which is now in this case it pulls in the direct reports the tasks the contact info and the meetings right and just to kind of prove this is working let's switch to the managers record so i'm going to give it an id of two that's their id in the database if i hit refresh here you'll see now it's the manager we can see who their manager is and then we didn't assign any direct reports yet so this is empty but we did assign them tasks and they also belong to that same meeting with the ceo now that's one way to do it which is the defined api right that's not necessarily your only option so if you know type rm well enough you know that you can also create custom queries using the query builder so an alternative way to do this and usually when you have more complex queries you'd use the the query builder so you might do something like this dot employee repo create query builder and we pass in an alias here we'll just call this employee and then the way you pull in relations is by by actually just doing joins right so maybe if you're more familiar with sql this should just make sense to you so you should be able to do a join and select where we use that we use that alias and we want to pull in again the relation and we'll provide an alias for that as well so this is going to pull in the employees direct reports and we'll also add in our meetings and tasks just as an example right and then you'll notice that there is an alias for each one of these so it's still kind of represented by some kind of string and that's useful in scenarios where perhaps you want to add you know a where clause right maybe you want to do where employee dot id equals employee id again i'm kind of going through this quickly because hopefully you have some of the type 4 and fundamentals already i'm just giving you the how to do it so and then don't forget to add get many here or i'm sorry this should be get one right and that should get us a similar result back in the browser when i refresh we get back that same object except in this case we only pulled in direct reports meetings and tasks right so those are your two main ways to get uh related data into your query which is if you're using the find uh methods you know you use the relations which is very convenient by the way but for more complex queries where you might have you know more uh additions like you know grouped by where clauses order by stuff like that where you really need to utilize those aliases uh you'll probably want to use the equality create query builder and specifically if you really know the sql well uh you'd probably be able to map the sql to the javascript api much easier than this find abstraction so it's up to you right so pretty simple right and now just to kind of prove that our uh delete or on delete constraints are working properly let's go ahead and do a let's add a delete employee method which again takes a number as an id and we'll just do return this dot employee repo dot delete and we pass in that all right so let's go ahead and trigger our ceo to be deleted and change this back to one i'm going to change this to delete employee and then we'll take a look at what happens after that deletion so i'm gonna refresher to trigger or delete you can ignore that response from now so back in a database if i query employee now it's just the manager ceo is there right but we set that manager's um manager id to null because that employee you know left or got whatever and contact info should be empty now because we deleted the ceo and that automatically cascaded the deletion to the contact info and in the relationship table here for the many too many with meetings effectively our attendees list we now have uh just the one record because again the ceo got removed so automatically we said that typo ram is going to handle that for us and it did right and then one last thing let's make sure it's also working for the tasks we said that tasks should stay there if they get deleted so in our code let's delete our last employee which has an id of two and let me refresh this browser again to trigger that now back again the beaver employee is not now empty and what i expect to see in the task table is that the tasks are still there but the employee id is no and again just a reminder what is that well when we go back to our task entity right we said that on delete referencing this employee we should set the ids to no all right so that i think that just about covers the fundamentals that you need to know in terms of how do you create relations how do you represent the schema for that how do you do inserts and actually relate the table the the records via the saving and stuff like this how do you query it and how do you kind of make sure deletes are doing things correctly uh we talked about all of that now there are some other things that you want to take a look at for example you might have noticed that we individually have to save everything here right each entity we have to manually save there is a way to prevent that or improve upon that which is via cascades if you want to look into it yet there's this property that you can assign cascade true and it's going to allow you to only have uh basically one save like in this example that they have in the docs here instead of them having to manually save each category right you can kind of just assign it and it's going to cascade that saving across uh those categories if you kind of compare that with that with our code notice that in our task 1 and our task 2 we're manually saving each one of those right before we can do this thing if we were to have cascade true you can remove this and by the time that you save the manager it's automatically going to save those two tasks or in this case when we say save it's going to insert in the database now i didn't explicitly try to go over that too much because uh in my opinion it it might make it confusing um because it to me it adds a little bit of magic you don't know when things are actually being saved or not so for me i i personally prefer the you know more explicit way of calling save on each thing that's fine with me even if it adds a little bit of code personally but you should know that you have a way out of that if you don't like it right and cascade also has some stuff about you know maybe you want to only do that in terms of like inserting updating deleting or everything um there's a little bit of configurability there that i recommend you check out if you're interested in that one last thing i want to mention is that you can also do eager and lazy relations so for example if we go back to our code so as an example back in our code uh again remember that we're kind of explicitly saying what do we want to be included with the employee data when we when we find them for example you know it includes this thing now the eager what this allows you to do is says what if you want to always include tasks for example regardless of whether or not you included in the relations list you can do eager true here and it's always going to pull in the tasks every time you query the uh the employee so you have the option to to do that there's also the option of doing lazy relations which i'm gonna be honest i haven't had a good use case for in my own work so you know just know that it exists it's there it basically allows you to lazily pull in the relation later via async and await like this like with promises um i don't know i i personally have not had to use that plus it makes the the type be a promise like this so i don't know if that's a good thing or a bad thing for you in my opinion it it makes it a little bit more confusing to work with but i'm sure some people will probably have a specific use case for that uh with that said guys hopefully that was a useful tutorial for you make sure to let me know in the comments if you have any good or bad feedback i'd love to hear it if you've made it this far in the video perhaps you like the content maybe it's relating value to you make sure you subscribe hit the like button that said i'll catch you on the next video thanks for watching
Info
Channel: Marius Espejo
Views: 7,363
Rating: 4.9870548 out of 5
Keywords: typescript, typeorm, typeorm tutorial, typeorm migrations, nestjs tutorial, nestjs graphql, nestjs microservices, nestjs, typeorm relations, typeorm nestjs, typeorm sqlite, mysql, postgres, sqlite, database, relational database design, relational database, typeorm nestjs tutorial, nestjs typeorm, typeorm typescript, javascript, typeorm relation, typeorm tutorial nodejs, typeorm tutorial for beginners, many-to-many, one-to-many, many-to-one, one-to-one, graphql, nestjs tuto
Id: rKgZLVgdvAY
Channel Id: undefined
Length: 49min 2sec (2942 seconds)
Published: Sat May 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.