Loading
Current section: Seeding Data 4 exercises
solution

Nested Queries in Database Operations

Transcript

00:00 All right, let's nest the heck out of this thing. So I'm going to take all this create stuff from our note create, and we're going to add notes and create. And here we can provide an array of notes. And I'm just going to stick all that right there. And now we no longer need to provide the owner ID.

00:18 We don't need that, and we don't need this. Ta-da, that's it. We're done. And so we can double check that this works with npx prisma db seed. And that will run our seed script. Ta-da, that's it. So like I said, not everybody loves this. It reminds them of the Pyramid of Doom days.

00:36 But that was very different. If you're thinking back of your Pyramid of Doom with a node and callback heck and all that stuff, this is very different from that. We're not handling errors or any of that stuff. That's a bit of a throwback. So yeah, to me, this actually is really beautiful,

00:56 and I love it. But I understand if you'd rather keep it the way it was. I just wanted you to learn that this is how you accomplish that. So you can, as part of the process of creating records, you can also create their relational records as well, which is quite nice.

01:13 You can even do a connect or create. So if there's already a record in the database you want to connect it to, so like setting the foreign keys and stuff, then you can use connect. And you also have the option to connect or create. That will allow you to say, oh, if there's

01:31 a thing that has this property, like you add a where clause, then we're going to connect it. Otherwise, we'll create it. And so you'll have two options there. So that is nested queries, and they're great. So I hope that you had an awesome time with this one

01:47 and that you make your own decisions on whether you prefer to have this deep of nesting.