Loading
Current section: Querying Data 5 exercises
solution

Efficient Data Retrieval with Prisma and Subselects

Transcript

00:00 Let's start out by swapping this with Prisma and then we're going to say await Prisma user find first. No, not find first anymore. This is find unique. And then that needs to change the syntax a little bit because our Prisma query engine

00:18 is a little bit different. And then we want to select the notes for this user. So we're going to say select and here we're going to have a couple of red spots here that we're going to have to deal with now because we're not selecting everything we need. So yet the name is going to be needed.

00:37 We display that right there. We display the username as a fallback. So we're going to need the username. We also displayed the user's image. So we'll do that sub select for the image ID right there. And then we have our notes. We're going to do a sub select for all the notes. We want all of their IDs and titles.

00:53 We don't want anything else from the notes for this list right here. So we can get rid of all this nonsense. And then we just return the owner. We no longer have the notes. The owner is going to have the notes. We say owner.notes, right? So great. Now let's take a look because we changed that a little bit. We no longer have data.notes.

01:12 It'll be data.owner.notes. And here's the other thing that's just totally rad. Check this out. We're in a totally different file. We're importing the notes loader because we're using that in our meta function, if you recall from a previous workshop.

01:27 And it is telling us, yeah, Lidley is pretty super duper excited about this, but it's telling us that we have a type error for our meta on the notes index, which is just so sick. So it's no longer .notes, it's owner.notes. That is just awesome.

01:46 So with all that done, we've got Cody and all of Cody's notes. The funny thing about this is that even though this is coming from the real database, I click on this, this is coming from our in-memory database. And the reason it works is because they have the same IDs on both. So that is an accidental functioning thing.

02:05 So if I were to delete this, it's still there on the left nav, and it's going to say it's not there when I try to select it, which, yeah, that's just a byproduct of the fact that we're only halfway through our migration here from our in-memory database to the real one. So, yeah, let's talk really quickly about these subselects.

02:23 It's really awesome that you have these relationships, you can subselect into them and select only the things that you actually care about for those things. And then we had this update automatically tell us that like, hey, you changed something. We got to fix it over here. I just love that TypeScript rocks. Yeah. OK, there you go.

02:43 Well done on this subselect.