Fullstack TypeScript, v2 (feat. Zod)

Type-Safe Database Access with Prisma

Steve Kinney
Temporal
Fullstack TypeScript, v2 (feat. Zod)

Lesson Description

The "Type-Safe Database Access with Prisma" 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 introduces Prisma, which provides a TypeScript ORM and a declarative database migration system. Once a Prisma schema is created, database migration files are generated containing the new objects.

Preview
Close

Transcript from the "Type-Safe Database Access with Prisma" Lesson

[00:00:00]
>> Steve Kinney: So the one last thing we're gonna talk about is a cool little library called Prisma, not to be confused with Prismo from Adventure Time, even though it ends in.io. And all Prisma really is, is a nice abstraction for working with databases. If you have come from something like Ruby on Rails and you're used to having migrations kinda automated and just kinda defining a schema and letting the database stuff take care of itself, and getting nice little methods that are type safe.

[00:00:38]
I mean, not that you Ruby, because whatever, I love Ruby, that was not a Ruby dig, but Ruby is not obsessing about type safety all the time. And gives you a nice abstraction for that, and like, we'll also host a set databases for you for money, but to just run it kind of while prototyping.

[00:01:02]
It's like kinda the company I work at, which is like, we have a cloud offering so that I will pay my salaries, so I can keep working on the UI. But you can use it completely for free, same is true of Prisma. There's totally free options, you can do all sorts of stuff for free.

[00:01:16]
You can hook up to any Postgres database, SQLite, what have you. You can also pay them. I personally don't like managing databases, so I would probably do that, but some people like to do that. But yeah, so getting set up with Prisma. The final piece of like again, we started doing stuff by hand on either the client or the server side.

[00:01:40]
Then we did some stuff by hand, a little late in the game on the database side. But we also looked at, can you also just generate the contracts and get all the type safety for free? I will again repeat the same thing I've repeated like 78 times this workshop.

[00:01:53]
What is the right answer? The right answer is it depends. If you already have a bunch of like, bespoke database stuff, maybe it might be harder to retrofit this on than it is to just kinda build the client that I built real quick. Or maybe this is the better choice.

[00:02:08]
Or maybe it is new things and you migrate. There's probably a long course we should do one day on the tough decisions you have to make as an architect sometimes. Is where every option is terrible in different ways. We're not doing that today. So we're gonna look at how to get set up with Prisma, just to kind of see the wide range of concepts at this point.

[00:02:30]
So yeah, if you're using the example repos from the course, I went ahead and NPM and I put everything we would possibly need in the package.json, which is why we have not been NPM installing cuz that is one extra demo roller coaster I didn't wanna ride. So here we are.

[00:02:47]
So we do have Prisma in place, so we can do something like mpx prisma init. I am in the server repo, right, cool. Cool, you won't have that problem cuz it's not checked in, but I do. And then we're just gonna do npm prisma admit, the ones that catch you when you also practice are the fun ones.

[00:03:17]
Cool, cool, cool, sets up a basic schema in Prisma Schema, awesome. And here we've got a little bit of a configuration file. Let's hide this for a hot minute or just smallerize it. Not a lot set up here so far, we're gonna have to change a few things anyway.

[00:03:44]
If you do not have syntax highlighting, you just need an extension, if you're in VS Code, if you're in something else, I don't have to figure it out, which is there is a Prisma plug-in, you go and install it. I think it probably hassled you as a recommended extension when you open VS Code, or maybe you already have it.

[00:04:05]
But then you will get the syntax highlighting. If you see this without syntax highlighting, it's the same file, everything's okay, it's just not as pretty. So some things that we do need to change, the client is the same. I'm personally not using Postgres at this moment. We were using SQLite because playing with containers in Postgres just felt like, again, one extra demo roller coaster that I did not feel like writing.

[00:04:34]
So we'll change this over to SQLite. I don't think I checked the environment variables into version control, because you're not supposed to. This whole time we've been using a database called database.sqlite. I'm just gonna use a different one, because, again, I don't want to find out why when the weird things of switching database drivers midstream.

[00:05:03]
So I'm gonna call it, I don't know, development. Sure, that feels good. You can call it whatever you want. And the only other thing that we need to do here is talk about the stuff that's gonna be in our database, right? Very glamorously in the kind of like hard-coded version that's kinda defined here, right?

[00:05:33]
I don't wanna do that, I didn't enjoy writing that. In Prisma, it looks like this, model task, in this case, the ID is gonna be an integer. Sure, yeah, that's exactly what's gonna type. This is gonna be, again, the identifier, and we're gonna auto increment, right? So the first one will be one, two, so on and so forth.

[00:05:55]
The title, sure, is a string, okay, description. We make this an optional string, just like it was in the types earlier. And yeah, that seems great. There is stuff like created at those, all seem good too, but that's gonna be roughly what we've been working with the entire time.

[00:06:13]
And you can, again, if you had users or stuff like that, you can define all of these, you can relate them to each other, kinda set up all the relationships, not necessarily worry about the tables yourself, right? All of that will be handled for you, as well as generating a database client that will be strongly typed, and we love to see it.

[00:06:34]
And then we can say something like MPX. And again, you'd make these NPM scripts or something, or like VS Code tasks, or what have you. Prisma migrate the dev environment, and this does a whole bunch of stuff. We can have test databases, give the migration a name, if you've done Ruby on Rails, this is probably familiar.

[00:06:53]
There's probably other frameworks that do the same basic idea. It created some databases, it created a migration file. A migration is basically the how do you get from the original version you had your database through all the different steps to where you want it to be. Our first one is just an initial one, and then we'll just run that migration.

[00:07:22]
>> Steve Kinney: We ran the migration we need to generate. Cool, so we generated a client. That client is informed by the schema, right? The schema should theoretically match with some of the other things we've seen, right? And so they will do the kind of type validation that we were doing by hand with Zod earlier, we will get out of the box for free.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now