CodingNic

Introduction to ORMs and Prisma Setup

Installing Prisma

Introduction to ORMs and Prisma Setup 10 min read

Installing Prisma

Objectives

By the end of this lesson, you should be able to:

  • Install Prisma in a Node.js project
  • Run prisma init and explain what it generates
  • Point Prisma at a real PostgreSQL database

๐Ÿ’ก Why this matters: Every Prisma feature in this course, models, migrations, queries, relationships, is built on top of the project structure this lesson sets up.

โš ๏ธ A note on verification: the Prisma CLI (prisma init, prisma generate, prisma migrate) needs to download a query engine binary the first time it runs, and this course’s own tooling runs in a sandboxed environment with no route to that download. So unlike every other lesson in this course, the commands and output below are not this course’s own live output, they’re Prisma’s standard, current, documented behavior, stable across recent Prisma versions. Run them yourself following this lesson, they’ll behave exactly as shown, on your own machine, with normal internet access.

Project File Structure

text
prisma-intro/
โ”œโ”€โ”€ package.json
โ”œโ”€โ”€ .env
โ”œโ”€โ”€ prisma/
โ”‚   โ””โ”€โ”€ schema.prisma
โ””โ”€โ”€ node_modules/

Installing Prisma

bash
mkdir prisma-intro && cd prisma-intro
npm init -y
npm install prisma --save-dev
npm install @prisma/client

prisma is the CLI, used at development time (migrate, generate, and so on). @prisma/client is the library the application code actually imports and calls at runtime. Both are needed, they do different jobs.

Running prisma init

bash
npx prisma init --datasource-provider postgresql
text
โœ” Your Prisma schema was created at prisma/schema.prisma
  You can now open it in your favorite editor.

Next steps:
1. Set the DATABASE_URL in the .env file to point to your existing database.
2. Run prisma db pull to turn your database schema into a Prisma schema.
3. Run prisma generate to generate the Prisma Client.

More information in our documentation:
https://pris.ly/d/getting-started

This creates two files:

text
// prisma/schema.prisma
generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}
text
# .env
DATABASE_URL="postgresql://johndoe:randompassword@localhost:5432/mydb?schema=public"

schema.prisma is where every model in this course is defined, starting next module. .env holds the connection string, exactly the same pattern used with the raw pg driver in Module 1.

Pointing It at a Real Database

Replace the placeholder DATABASE_URL with the real connection string for the school database created back in Module 1:

text
# .env
DATABASE_URL="postgresql://postgres:postgres@localhost:5432/school"

Prisma reads this same DATABASE_URL for every command from here on, migrations, queries, everything.

Try It

  1. Install Prisma in a new project, and run prisma init --datasource-provider postgresql.
  2. Confirm prisma/schema.prisma and .env were created, matching what’s shown above.
  3. Update DATABASE_URL in .env to point at a real PostgreSQL database on your machine.
  4. Open schema.prisma and identify the two blocks it starts with, generator and datasource, and explain what each one is for.

Recap

  • prisma (dev dependency) is the CLI, @prisma/client (regular dependency) is the runtime library, both are installed separately.
  • prisma init creates prisma/schema.prisma (the schema file) and .env (holding DATABASE_URL).
  • The schema file starts with a generator block (how the client gets generated) and a datasource block (which database, and how to connect to it).

Next lesson: the Prisma schema file itself, and what the generator and datasource blocks actually configure.