
Lesson Description
The "Using Prisma with tRPC" Lesson is part of the full, Fullstack TypeScript, v2 (feat. Zod) course featured in this preview video. Here's what you'd learn in this lesson:
Steve refactors the tRPC taskRouter to use Prisma. When the PrismaClient is initialized, database APIs for any custom objects are available and type-safe. These APIs include methods like create, delete, findOne, findMany, etc.
Transcript from the "Using Prisma with tRPC" Lesson
[00:00:00]
>> Steve Kinney: So now, ideally, what we should be able to do is, we can do it, I guess we can do it on the REST server or the TRPC server. I actually think doing this in TRPC is going to be easy and there I just jinxed myself. Sue will import the Prisma
>> Steve Kinney: From Prisma client, and we've got that in place, and with any luck, actually, we don't want in this file, sorry.
[00:00:39]
Only for one second? We want it in the context so I can pass it in to all of those GRPC methods. We'll do it here and with any luck we should see stuff like
>> Steve Kinney: So not dissimilar from my little task client here. We can make a Prisma client.
[00:01:06]
If I was really fancy, I would probably just call it tasks, but we'll see, and hopefully.
>> Steve Kinney: You can see that it's got task on it as a method that it knows about About. That's interesting.
>> Steve Kinney: And if I go to task, look at this, all the things that I might ever want to possibly do.
[00:01:29]
Create, create many, create many and return, so on and so forth, right? Those are all things that I can now do. For free, cuz all of the different variations have been generated for me, right? So what I am able to do at this point is we could say,
>> Steve Kinney: I was about to do something reckless.
[00:01:56]
I'm not going to, but then I'm gonna Going to later. Let's go ahead, let's grab it here actually. I could argue for either way. We'll pass it into the context. And then now instead of the tasks coming in, we can say, We'll just let my arrow key go from the context.
[00:02:28]
And now we could return prisma-task.findMany.
>> Steve Kinney: Swap it out. And so the important part here is, there's I don't have to do any Zod parsing. I don't have to do anything, right? It knows the schema of my database. It generated a client for me that did all of that already.
[00:03:03]
Right, and so here we've got return many. I wonder if I just swap it in without changing any code on the client if it'll work. I literally don't know, actually. I'm not convinced it's going to.
>> Speaker 2: It's a different database, so it shouldn't. To get ready into one reading.
[00:03:22]
>> Steve Kinney: But even if the API is that compliant?
>> Steve Kinney: Not totally, but that part is also it's pro, no, it's a promise or was it return.
>> Steve Kinney: It, It's interesting, cuz it did not crash.
>> Speaker 2: You're writing to one database file and reading from another.
>> Steve Kinney: That'll do it.
[00:03:49]
I should probably finish what I started. Yeah, if I the whole thing, will let me protect myself and make it a different database you can write to one. So let's at least get the write, read, cycle, go and how about that? So here we'll pull that in off the context as well, and here we'll return and again.
[00:04:15]
The nice part is the squiggly lines will guide me create.
>> Steve Kinney: And again, no squiggly lines. I'm feeling pretty good, right? So we'll go ahead.
>> Steve Kinney: Hey, it works. Obviously, edit and delete won't work, but you can imagine not a lot I have to do.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops