Check out a free preview of the full API Design in Node.js, v4 course

The "Prisma & Render Setup" Lesson is part of the full, API Design in Node.js, v4 course featured in this preview video. Here's what you'd learn in this lesson:

Scott explains how Prisma, a database-agnostic ORM, will be used in the application. A PostgreSQL database is created on Render.com. Prisma communicates between the application and Render.com using the external database URL located in the Render.com connection settings.

Preview
Close

Transcript from the "Prisma & Render Setup" Lesson

[00:00:00]
>> So what is the ORM that we're gonna use? We're gonna use something called Prisma. So Prisma is a database agnostic, type-safe ORM. What does type-safe mean? It basically means that this functionality here is aware of the shape of the data that you described. So it can give you autocomplete for the data that you're interacting with, which is actually really cool.

[00:00:27]
Cuz typically you don't get that because your data's dynamic, how would the code know what the data looked like? Well, Prisma does, and it's really cool. So we'll be using that. It definitely supports most databases out there, Mongo Postgres, SQLite, even something like PlanetScale which is a service database which is really awesome, it supports that too.

[00:00:46]
And not only has an SDK for doing basic to advanced querying of a database but it also handles schemas, migration, seeding. And pretty sophisticated rights, that we probably won't get to today because that would just be like a database course. But it's definitely slowly but surely becoming the ORM choice for most Node.js projects.

[00:01:06]
I think most people just start a project and use Prisma at this point, and they also have like really good documentation. I swear they're not paying me for this, I just like the product, it's really cool. And like I said before, we'll be using Postgres for the database.

[00:01:20]
Now, before we get started, you're gonna have to set up a database so this can actually work, and there's two ways. I'm gonna give you the recommended way and I'm gonna give you the way that's not recommended. The recommended way is just go to render.com and spin up a Postgres database for free.

[00:01:33]
You can make a free account. You can go get a database, it's hosted. You can pick the version. It kind of just guarantees that you're gonna be able to get through this course without any trouble. If you already have Postgres on your computer, you can use that as well.

[00:01:44]
Just make sure you have the latest version, which I think is 14 something. If you don't have it on your computer, just go to Render, make an account, and spin it up. But if you absolutely want to install Postgres locally, you can go to their website and they have an installer that allows you to install it.

[00:02:01]
And then you can spin up a Postgres and manage it locally yourself. I will not be doing that. I actually I do have Postgres locally, so I could just turn it on. But I'm just gonna go to Render and I'm just gonna spin one up there, and I'm gonna use that one.

[00:02:14]
I already have one, so I'm gonna show you how that looks like. Just gonna go to render.com. I'm already signed in so I'm gonna go to my dashboard. And you can see here, I already have a database to just show you what it looked like if you had to make one.

[00:02:30]
You can just Click Create and then they literally have an option for PostgreSQL. You just click that, a little blue elephant, or purple elephant, that's what you want. You wanna click that right there. It might look different since you're a new customer, your UI might look different, but you still wanna click a PostgreSQL database.

[00:02:50]
Make sure you pick the free one, and let me show you what I mean. When you go down here it automatically selects the starter one, that's 7 bucks a month. Go to the left and click the free one. Because it says right here, you can't downgrade it later.

[00:03:04]
So make sure you pick the lowest one at first, which is the free one. You can name it whatever you want. Don't worry about the database name. Don't worry about the user. Don't worry about the region. In fact, you don't have to worry about any of this stuff.

[00:03:16]
Just put a name and leave everything else and click Create. Once you have that, you'll have a database and you can scroll down and you'll see all these different links. Make sure you copy the external database URL link. You do not want the internal one. The internal one is probably only functional within inside of a firewall or a VPC.

[00:03:50]
It doesn't allow external IP addresses. It might rotate its URL frequently so it changes. So you don't wanna use the internal one, it pretty much means it won't work externally. Use the external one. And this is something that Heroku had a problem with, they had rotating databases. So even if you use it for external stuff, you would have to go change it all the time, and that was really annoying.

[00:04:15]
Cool, so everyone should be able to do that. Just make sure you get that copied. We will be using it right now. Okay, so once you get your database, what you wanna do now is we wanna install Prisma. So to install Prisma there's also some other dependencies that we're going to use.

[00:04:43]
We're gonna use type script, like I said at the beginning of the course. And the reason we wanna use type script because that enables Prismas type checked stk, which I think is half the benefit of Prisma. It is actually quite powerful. Because without that, what you're gonna do is you're gonna find yourself like tabbing back between your schema.

[00:05:02]
And the handler that you're in like what is that? What does the user need again? Or what's that field that's required? I forgot, you can just do it in TypeScript, it'll just show you. So we're gonna switch over to TypeScript. Which means for TypeScript, we need other dependencies.

[00:05:15]
So we got to download all the TypeScript stuff and then we can download Prisma. And then we're gonna switch to the project which only has two files at this point to TypeScript, so you'll see what that looks. Okay, cool so you can copy this command right here, which will give you the whole thing you need to install.

[00:05:28]
So let's go ahead and do that. I'm gonna go to my terminal. I'm going to stop the server. You can stop the server or you can open up another tab, it's up to you. I'm actually just going to stop the server. And I'm gonna paste this in. So MPM install typescript ts node.

[00:05:47]
So TypeScript, it's actually the transpiler that's going to convert the TypeScript to JavaScript. TS node is literally the same thing as running node in the terminal, pointing it at a file, but doing it in a TypeScript environment. So it doesn't have to build first. And @types node are just the types for the node runtime, so we get global types.

[00:06:10]
And then Prisma's Prisma. So we're gonna install those. Make sure you save dev as all of these are dev dependencies. Okay, should have those installed. Now that we do, we need to create a TS config file. If you've never used TypeScript before, this is just a configuration file for TypeScript.

[00:06:37]
TypeScript is a compiler, it transforms code, and you can have a bazillion different options on how you wanted to do it. So we're gonna paste this in and we're going to do that. So on the root of this project just make a new file, tsconfig.json and paste this in like so.

[00:06:58]
I know I put it in here, but I highly recommend removing strict true. Otherwise, you will be writing a lot of types [LAUGH]. If you put strict true TypeScript won't build unless you do all the hard work. So I highly recommend just getting rid of that strict true inside the compiler.

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