Current section: SQL 6 exercises

User Search with Prisma and SQL


00:00 So this is our user index page, let's pull that up. And we're gonna swap out this, get rid of that nonsense. And we'll come up here, switch this for Prisma. And then we can say our users, cost users equals await Prisma query raw.

00:19 And here we go, we've got our raw query going. And what we're gonna do is first select ID, username and name from the table called user. You can put it in quotes, you don't have to, but it will work either way. Where the username is like the search term or the name is like the search term.

00:39 That to me looks super duper funny. And so what I do instead is I'm gonna make a like variable that is a string that interpolates the search term. And that just, to me that looks a little bit nicer. So now I can say where username like the like or name is like the like.

00:57 So with that, we now can just add the limit of 50 and we've got our query. Now TypeScript's not gonna be super jazzed about all of this, what we're doing right here. Also, we don't have the image quite yet, we're gonna get to that later. So we'll just pass this along. And again, like I said,

01:16 TypeScript's not gonna like this a whole lot just because the way that we have our, there's, you cannot get a type from a raw query with Prisma like this. It just is not possible with Prisma. So we're gonna have to deal with that later. So we're getting no user found

01:34 because the search term is gonna be null because we don't have a search term. But if I search for KO and we're gonna get Cody there. So let's fall back to an empty string. So if there isn't a search term, then it will be like empty string. And now we're gonna get all of our users there.

01:52 Ta-da, looks great. And we're not getting any images right here because we're not selecting our images, that's for a future step. So just to review the stuff that we've done, we created a like, which includes these percentage signs that is a special syntax in SQL to say

02:13 that this is like a string containing this value. That's what we're looking for. And then we have our query raw with our template literal. We have select ID, username and name from user where username is like that like syntax or the name is like that. And then we're gonna limit to the top 50.

02:32 Of course we could limit to two. And if we do that and we're only gonna get two. But I think 50 results on this page is fine. That's what our product manager said. And again, our TypeScript's not super jazzed, but we'll deal with that later. So we're all ready for the next step.