Loading
Current section: Querying Data 5 exercises
solution

Using Prisma to Retrieve User Data from a SQLite Database

Transcript

00:00 Let's go to our username and here we're going to swap out our in-memory database for our Prisma actual SQLite database. So Prisma and so now we're going to say Prisma Prisma and this is async now so we're going to add a wait Prisma user and we're going to do find unique because we have that

00:18 unique constraint. We are still going to keep a where clause so it is a little bit similar but instead of this equals thing we're just going to say where the username is equal to that's a params username. Okay great and so now if we look at user dot we will find created at

00:37 email updated at username a bunch of things we don't want right so we're going to just select the things that we do want. What we do want includes the name and the username and the image but the image is not on this model it is a separate model and so if I do this I'm going to

00:54 say user dot image dot and now I've got a bunch of other things from the image too so I don't want all that stuff all I need from the image is the id so we'll say a sub select id of true and now I have only the stuff that I care about oh but thank you TypeScript you just let me know that I was

01:13 missing something so I can really easily just add that right here and that is what is so cool about being able to use Prisma which is type safe to the database with our type safe to the client

01:27 as well like it's all the way from database to client full type safety it's amazing super super duper cool and so then we can get rid of this stuff right here because we're just selecting what we want to and we do still want to keep that save that and Cody looks awesome well done

01:48 so yeah the select right here I don't know why but I always put it after the where clause it just it feels more natural here even though in actual SQL select always comes first yeah I don't know you're going to see me do both but yeah that's that's it the thing that is just

02:04 super cool is if we decided you know what I want to display the user's id right here user dot id well we're not selecting the user's id and so we get some type safety there and then we can say id true and then we say you know what I don't want to display the user's id anymore that's kind of

02:21 weird so we delete that and then we get red underlines for all the places where we need to get rid of it that just like is the coolest thing I love this super super cool so in review all that we did was swap our in-memory database for Prisma and then we use Prisma's user find unique from our

02:39 Prisma client all this stuff is type safe like if we tried something else true then yeah we're going to get a red underline there as well we select where the username equals the params username that works because the username is a unique field in our Prisma schema so we can do find unique

02:58 and the user may not come back still so like we do still have the no user with username codies exists right so that all still works so this invariant response is still useful there but then we return just the thing that we queried from the database because we can query exactly the fields that we

03:18 want so and then of course it's all type safe into our ui which is so sick so there you go your first real application query with Prisma