0:00 All right, so we wanna go ahead and optimize our findById so 0:03 that we're not 0:04 making two separate calls to the database to fetch this 0:07 related data. We've already set up the foreign key 0:11 for associating an item or an order item with an order. 0:16 And so now to take advantage of these relations inside of Drizzle, what 0:20 we have to do is actually go back to the order schema. 0:24 So go ahead and open up the order schema file. 0:28 And in here, what we need to define are these Drizzle relations. 0:31 So for example, we'll export const ordersRelations, 0:36 and this will use the relations function from Drizzle ORM, 0:41 where we pass in the table that we're defining the relation on, 0:45 so in this 0:45 case, the orders table, 0:48 and then a callback function where we destructure the 0:52 many argument, which allows us to define a many 0:55 relationship. 0:57 In this case, we wanna define a new relation called 1:01 items. 1:01 Since this is the order items, we could call this whatever we want, 1:06 and this is because we know that a single order can have 1:10 many items. And so that's what we need to define here 1:13 by passing 1:14 into the many function simply the order items table. 1:18 And that's all that we have to do to define that. 1:21 Now, to do it on the inverse so that, for example, we could pull back order 1:25 items and then its corresponding order in one query, we 1:29 can also set that up using a similar syntax. 1:32 So for here, we can define the orderItemsRelations. 1:36 This will use the same relations function, 1:40 but now we'll use the orderItems table. 1:43 And now instead of the many function, we'll destructure the one function. 1:48 And can you wonder why this is? Well, that's because in this 1:51 case, we can define the relation and call it order because an order item
The rest of the transcript is for members.
Sign in