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

The "Prisma Overview" 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 installs Prisma and runs the CLI tool to generate the required files for the project. The prisma.schema file contains all the schemas for the data models. The database connection string is added to the .env file.

Preview
Close

Transcript from the "Prisma Overview" Lesson

[00:00:00]
>> The next thing we wanna do is we're going to initialize Prisma. So I know we just installed Prisma, right? But we're gonna use npx to initialize it. Well, I am, you don't have to, I'm gonna tell you why I'm doing it. So what is npx? npx is a tool that comes with npm, and it basically allows you to use CLIs on your computer without having to download them first.

[00:00:23]
That's basically what it is. Prisma is a CLI. And we did install it, but we did not install it globally. We installed it locally. So the weird thing about CLIs and Node is if you don't install them globally, you can't refer to them in the terminal like this.

[00:00:39]
Because they don't exist globally, you install them locally. So you would have to do something like node_modules bin, or I don't know what that was, node_modules/.bin maybe, there we go, prisma. And then you can refer to the local one. Who wants to type all that out? That's gross.

[00:00:58]
I don't wanna do that. So I can either just install Prisma globally, which is probably recommended, or I can choose npx, which will just run it like it is globally. So I'm gonna say npx, Prisma. And the command that I wanna run is init. So, npx prisma init, hit Enter, and boom.

[00:01:20]
Prisma did a few things here. I still think it's funny, there's a database called cockroachdb, right? And people use it, it's a good database, yes.
>> Just a couple of people in the chat were getting errors with the TS config.json.
>> Yeah, I get the same linting error as well, you can ignore it.

[00:01:39]
It's just basically TypeScript saying, hey, we don't know what files you want us to compile and we don't know what files you want us to ignore. We don't have to tell it, it's actually smart enough to know. So, and that's because we're not actually gonna be building. If we were making a library that had an output, then we would need this.

[00:01:59]
But right now, we're just going to be using ts-node, so it doesn't matter, we're fine. You can ignore it. Okay, so let's see what happened here. So if you ran prisma init, npx prisma init, you should have this prisma folder now. First thing first, highly recommend if you're on VS Code, installing the Prisma extension, highly recommend it.

[00:02:22]
It gives you the syntax highlighting, but it also does some really cool stuff for you that's super helpful. So I'm gonna get the exact name of that extension. But I think there's only one. It's literally called Prisma by Prisma. So, highly recommend installing that. Okay, I have another extension recommendation that we'll get to later, that's pretty cool.

[00:02:46]
But recommend installing that. And then looking at the prisma folder, you should have one file on there, schema.prisma, okay? This is exactly what it sounds like. This is our schema. This is where we're gonna write our database schema. So what's in here already? The first thing is this generated client thing.

[00:03:03]
Prisma has another package that we're going to install, that's the ORM. So far, we've installed the CLI that interacts with the database on an administrative level. But the SDK is a rational use in the code to do the querying and the writing. And this is just saying, what is the name of that?

[00:03:22]
I guess there's some plans in the future to allow you to use different types of clients, maybe. But there's only one Prisma client that I know of, and it's called this, but hey, that's why that's here. And then datasource db is because Prisma can use any type of database, it can use Mongo, CockroachDB, Postgres, SQLite.

[00:03:41]
So this is saying by default using Postgres and then the environment variable to that URL, okay? So before we start writing our schema, first, let's add our environment variable. You should also now have a .env file in the root of your project, Prisma should have created that for you.

[00:03:57]
So go there, and you should see something called DATABASE_URL=. Okay, get rid of that value in that string and paste yours. Mine is not cockroach, so let me not paste that. So remember you wanna get the External Database URL, and you wanna paste that in there, like that.

[00:04:18]
But we're looking at my URL. Use your own URL. All right, so next thing we wanna do is let's just convert these two files over to TypeScript right quick. So index.js is now index.ts, and server.js is now server.ts, like that. And I can use import now. So I'm gonna import express.

[00:04:48]
So I can type from express. And I'll probably get an error here because it's like, you don't have types for express, I guess it figured it out. That's cool. And then no module.exports anymore, just export default app, like that. It's changing the modules. And then for the index, it means pretty much the same thing.

[00:05:11]
Just import app from './server'. And that's it, just those two changes in the modules. And it actually probably might even work without those changes, but I don't wanna not have those if I'm using TypeScript. So, Okay, Any questions on this? One last stop for our TypeScript conversion is just, if you go to your package.json, go to the script section, let's make a command called dev.

[00:05:48]
And we're gonna use ts-node to point to src/index.ts. That way, we can actually start the server and do something.
>> What enables us to use import in place of require in this project?
>> TypeScript, TypeScript has support for that. It's going to transpile that back down to require for us, or in the case of ts-node, it'll jut do it on the fly in memory.

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