Transcript from the "Setup Prisma ORM" Lesson
>> So we got our database running. We got our app running, like to put all these to the left like that. Let's get started with Prisma, okay. With Prisma, it's actually pretty simple. We just need to npm install @prisma/client, so we'll install that. And then we're gonna do another npm install of just prisma.
[00:00:34] But the reason I'm doing this several because I want this to be a devDependency, so I'll say save-dev. So Prisma the tool, and then Prisma the SDK. So you got the @prisma client, and you got the prisma that's a save-dev. Cool, and then from there you just wanna run npx prisma init.
[00:01:15] Hit Enter. And that's just gonna scaffold out a few things for us, we can go check it out. So first thing it's gonna do is gonna create a folder called prisma with a file in it called schema.prisma. There's a plugin for this file, if you want it to look nice, it's called prisma.
[00:01:35] So go install that, otherwise, it's just gonna look bad. And you won't get an icon, you won't get that icon right there. If you want that icon, go install the extension. So we got that. So go into that file, one thing we wanna change, you can see where it says PostgreSQL, we don't want this to be PostgreSQL.
[00:01:56] We want this to be mysql, like that. And then the other thing we wanna add here in the datasource db, is we want to add a relationMode = and then put prisma. Don't worry about that, that's something advanced. If we get to it, I'll talk about it, but you don't really need to know what it means.
[00:02:25] You could look it up from the docs if you want, but yeah, just put prisma there, otherwise this is not gonna work on PlanetScale. TLDR just means prisma is gonna handle our relationships and not the database. How the service database works, prevents it from handling relationships like it would normally would.
[00:02:48] Cool, the other thing we need to do is we need to add a database URL in our environment variable file, so this thing could pick it up. And this is the tool that I said that is going to automatically load the .env file, whereas Next.js loads the .env.local file.
[00:03:00] And in fact, it already created one, it created a .env file here, with a Postgres URL in it. We don't want that Postgres URL, so we can get rid of that. Instead, we're gonna just add our local host MySQL one, which will be, yeah, it's 3309, but I don't know why it's escaping me with a port will be.
[00:03:22] I'm sorry, not the port, the protocol. All right, they're gonna have it here. So what we wanna do is basically add our database like this. I'm gonna blow that up so you can see it. Just MySQL, in this case, 127, that's just localhost, 3309, and then it has to be the same name as the database that's on PlanetScale.
[00:03:53] So in my case it will be mood. So I'm just going to copy this and put it in my .env, like that. Get rid of database name and put mood. I think it works if you put localhost here too, but, I don't know, we'll see. And remember, your database name, whatever database you have on Prisma.
[00:04:30] Cool, everybody following me so far? All righty, and then what we wanna do is just make, we just wanna go on and actually make some schemas, so we can push this up to our database and sync it. The sweet thing about using Prisma and PlanetScale is that they both kinda do similar jobs.
[00:04:45] So Prisma actually does migration management too, and it kinda makes it seamless. But PlanetScale also does migration management, so Prisma knows that. So what it does is it opts out of doing any migration management for you if you're doing PlanetScale, and instead just let PlanetScale do it. So it actually becomes really easier to work with Prisma because you don't have to worry about any migration CLI commands.
[00:05:08] You just push it to PlanetScale, and PlanetScale will figure out the migrations for you. Whereas if you use another database that wasn't PlanetScale, you would have to use Prisma to figure out those migrations for you. Which is actually still really easy, but that's just one thing to keep in mind.
[00:05:22] So if you follow another tutorial where they use another database, you might find yourself doing Prisma migrate. You will never do that with PlanetScale, you'll never run Prisma migrate. You'll just say push this to the database, and PlanetScale itself will figure out the migrations. Let's just write a simple version of our user.
[00:05:44] So to do that is really simple, we just say model User, like so. It's a very simple syntax, it looks like objects, it looks like GraphQL. If you know any language, this is gonna look familiar. So model User, we're gonna give it an ID, that's gonna be type string.
[00:06:06] We're gonna say this is the ID field by using this directive @id. And then because we don't wanna have to create the IDs ourself, we're gonna ask the database to do that for us by giving it a default, which is kinda like a function. So you have a function there.
[00:06:22] And then we can say we want that to default to uuid. So uuid just stands for universally unique identifier, which just means it's a string that's almost guaranteed to be unique. I mean, nothing can be guaranteed to be unique, but, yeah, I can go to this generator right here.
[00:06:44] Every time I refresh the page, I get a new uuid, so this is a uuid. So the database will just create one for us every time for the ID versus us having to manage that ourselves, that's all I'm doing. You don't have to use uuid. I think they have CUID as well, which is something very similar.
[00:07:02] You can also just use the integer if you want and just make this go up by one, auto increment, but I hate that, I don't know why. I hate seeing IDs that are just one, two, three, I don't know why, I just can't stand it. So I think it's also a security risk too.
[00:07:20] It's like, now I know which one you were. You're number 3 or 300, I don't know, it's weird. All right, we'll just put that for now and then we'll come back and make the schema, let's just see if all this works, we didn't break anything, how about that?
[00:07:31] So what we'll do is, one, make sure our PlanetScale is running, which mine still is. Make sure we got that environment available, hopefully that's right. We have our schema here, all we have to do now in another terminal window is just run npx prisma db push. And viola, couple things happen here, so what?
[00:08:00] Basically, I mean, it tells us right here, loaded the environment variable. It loaded the schema. It saw the data source was MySQL and that's the URL to it. And then it synced our local schema with the database that we have on PlanetScale. And that's what it did, your database is now in sync with your Prisma schema.
[00:08:18] And then lastly, the most important part, is that it generated an SDK based off of our schema that we can now import from @prisma/client. So it literally went and generated that so that the types for that SDK, and the methods on that SDK, match the models that we created in our schema.
[00:08:38] So this means I can go into the schema and do prisma.user and it will have the same fields and expectations on it as the user in my schema. If I make a model for airplanes, I can do prisma.airplanes, and it will be type check. So it will generate a new SDK for you every time you push your schema up to PlanetScale.
[00:08:57] You can also manually do it just by running npx prisma generate and it will manually do it for you as well. So we can double-check this. If we go back to PlanetScale, this is where it's really cool, it already did it. You can see it's like, yeah, somebody just added a user table here, and here's all the SQL you did not have to write because of Prisma.
[00:09:18] So you would have had to written that SQL yourself if you didn't have Prisma, right? So now we have that, And it's only on this branch. So if I view these branches, if I go to the main branch, that main branch doesn't have that schema.