Initialize Prisma in a TypeScript Node Project

Ryan Chenkie
InstructorRyan Chenkie
Share this video with your friends

Social Share Links

Send Tweet

The Prisma CLI comes with commands to initialize Prisma in a project, run migrations, generate Prisma Client and more. In this lesson, we install the relevant dependencies and initialize Prisma in a TypeScript Node project.

Ryan Chenkie: [0:00] Here's a simple web server using Express and TypeScript. We've got server.ts. We are bringing in Express, and then we've got a single endpoint here at /ping, which sends back a message in this object here.

[0:13] If we want to start sending back some dynamic data using Prisma, we first need to get Prisma installed. In the command line, do npm install as a dev-dependency prisma. This will give us the Prisma CLI, and it will give us access to all the commands that we need to get Prisma initialized and do other tasks as well.

[0:32] Next, let's install as a regular dependency @prisma/client. This will give us access to Prisma Client, which is what we use to access our database.

[0:42] With those in place, we can now do npx prisma init. This will initialize Prisma in this project. What that does is it gives us access to this directory here called prisma. Within the prisma directory, we have schema.prisma. This is the file where we describe our database, the relationships between tables, and other information so that we can have Prisma derive a type-safe database access client for us.

[1:09] Prisma supports several relational databases, including Postgres, MySQL, SQLite and SQL Server. For now, let's use SQLite. That will give us a very easy way to use a database on the file system. Instead of pointing to the database URL in the environment file that we've got, let's change this up to go directly to file. Then we'll put the colon dot, and it'll be dev.db.

[1:34] This file doesn't yet exist, but it will be created for us automatically once we run a command to get our initial migration in place.