Project Setup
Prisma Fundamentals
Chapter 2 · Project Setup
This chapter builds a working Prisma 7 project from an empty folder: a TypeScript project, the Prisma packages, the schema, the configuration file, a first table, and a small script that writes and reads a row. It uses SQLite, so there's no database server to install. Every later chapter starts from this project.
latest tag for the prisma package currently points to a
Prisma 8 release candidate (Chapter 1). Installing it without a version gives you Prisma 8, which is set up
differently. This course pins every Prisma package to version 7 with @7.
Step 1: Create a TypeScript Project
You'll need Node.js 20.19 or later (22 is recommended).
tsx runs TypeScript files directly, without a separate compile step. Prisma 7 is published as ES
modules, so tell Node your project uses them too, in package.json:
And create tsconfig.json:
Step 2: Install Prisma
| Package | Role |
|---|---|
prisma | The command-line tool: init, migrate, generate, studio |
@prisma/client | The runtime library the generated client is built on |
@prisma/adapter-better-sqlite3 | The driver adapter that connects Prisma to SQLite through the better-sqlite3 driver |
dotenv | Loads settings such as the database URL from a .env file |
Step 3: Initialise Prisma
This creates three things:
prisma7.config.ts, so that it doesn't clash with
Prisma 8's prisma.config.ts, which uses a different format. Earlier Prisma 7 releases, and many
guides, call it prisma.config.ts. Use whichever name your prisma init creates.
The schema
- The generator block says what to generate.
prisma-clientis Prisma 7's new client generator;outputis where the generated code goes, relative to the schema file. In Prisma 7 the output path is required. - The datasource block says which kind of database you're using. The connection details don't go here any more.
The configuration file and .env
The config file tells the Prisma CLI where the schema and migrations live and how to reach the database. The
import "dotenv/config" line matters: Prisma 7 doesn't load .env by itself, so without it
DATABASE_URL would be empty.
.env will hold real database passwords once you move beyond SQLite. Add .env, the
generated client folder, and your SQLite database file to .gitignore.
Step 4: Add a Model and Create the Table
Add a first model to the schema (Chapter 3 explains every part):
schema.prisma, run prisma generate
again, or your TypeScript types will describe the old schema.
Step 5: Create the Client and Use It
Put the client in one shared file, so the whole application uses a single instance:
Notice the import path: the client comes from your own generated/prisma folder, not from
@prisma/client as in older versions. Now a first script:
Run it a second time and it fails, because email is @unique and Ada already exists.
That's the database protecting your data, and Chapter 5 shows how to handle it.
Using PostgreSQL Instead
The structure is the same for other databases; only the provider, the URL, and the adapter change. For
PostgreSQL, install the pg driver and its adapter, and change three things:
Hands-On Exercises
Build the project in this chapter from scratch and run script.ts successfully. Then check which Prisma versions were actually installed, and explain why that check matters right now.
Remove the import "dotenv/config" line from the config file and run npx prisma migrate dev. What happens, and why? Put it back afterwards.
Write a .gitignore for this project, and explain why each entry is there. Then modify script.ts so that running it twice doesn't fail.
Chapter 2 Quick Reference
- Node 20.19+;
"type": "module"inpackage.json; TypeScript run withtsx - Pin Prisma 7:
prisma@7,@prisma/client@7, and the adapter@7— plainprismainstalls a Prisma 8 release candidate npx prisma init --datasource-provider sqlite --output ../generated/prisma- Generator
prisma-clientwith a requiredoutput; the datasource has only the provider prisma7.config.ts(prisma.config.tsbefore 7.10) holds the schema path, migrations path and database URL; it mustimport "dotenv/config"npx prisma migrate dev --name init, thennpx prisma generateafter every schema change- Create one client in
lib/prisma.tswith a driver adapter; import it fromgenerated/prisma/client