Installing and Configuring Prisma
1. Installing Prisma CLI
| Package | Purpose | Install |
prisma | CLI tool (dev dependency) | npm i -D prisma |
@prisma/client | Runtime client (prod) | npm i @prisma/client |
tsx | TypeScript seed runner | npm i -D tsx |
npm install -D prisma
npm install @prisma/client
npx prisma --version # verify: prisma 6.x
2. Initializing Prisma Project
| Command | Effect |
prisma init | Creates prisma/schema.prisma + .env |
--datasource-provider postgresql | Sets DB provider in datasource block |
--url "<CONN>" | Inline DATABASE_URL |
--output ./generated | Custom client output (Prisma 6+) |
npx prisma init --datasource-provider postgresql
3. Configuring Database Connection
| Provider | URL Format |
| PostgreSQL | postgresql://USER:PASS@HOST:5432/DB?schema=public |
| MySQL | mysql://USER:PASS@HOST:3306/DB |
| SQLite | file:./dev.db |
| SQL Server | sqlserver://HOST:1433;database=DB;user=U;password=P |
| MongoDB | mongodb+srv://USER:PASS@HOST/DB |
Example: schema.prisma datasource
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
4. Setting Up Client Generator
| Field | Purpose | Default |
provider | Generator name | prisma-client-js |
output | Client output dir | node_modules/.prisma/client |
previewFeatures | Opt-in features | — |
binaryTargets | Engine binaries | ["native"] |
engineType | Engine type | library |
generator client {
provider = "prisma-client-js"
output = "../generated/prisma"
previewFeatures = ["fullTextSearchPostgres", "tracing"]
binaryTargets = ["native", "linux-musl-openssl-3.0.x"]
}
5. Configuring Environment Variables
| Variable | Purpose |
DATABASE_URL | Primary connection string |
SHADOW_DATABASE_URL | Migration shadow DB |
DIRECT_URL | Direct DB URL bypassing pooler |
PRISMA_HIDE_UPDATE_MESSAGE | Hide CLI update notices |
6. Setting Custom Output Directory
Note: Prisma 6+ requires explicit output path; the default node_modules/.prisma/client location is deprecated. Add the generated folder to .gitignore.
| Pattern | Use Case |
../src/generated/prisma | Inside source tree (committed-friendly) |
../node_modules/@prisma/client | Legacy default DEPRECATED |
7. Enabling Preview Features
| Feature | Description |
driverAdapters | Use external DB drivers (libsql, Neon, PlanetScale) |
fullTextSearchPostgres | Full-text search for PostgreSQL |
multiSchema | Multiple PostgreSQL schemas |
tracing | OpenTelemetry tracing |
views | Database views support |
relationJoins | Database-level joins for include/select |
8. Configuring Binary Targets
| Target | Platform |
native | Current OS (dev) |
linux-musl-openssl-3.0.x | Alpine Docker, AWS Lambda Node 20 |
debian-openssl-3.0.x | Debian/Ubuntu containers |
rhel-openssl-3.0.x | Amazon Linux 2, RHEL |
darwin-arm64 | Apple Silicon |
windows | Windows x64 |
9. Setting Engine Type
| Engine | Description | Use Case |
library | Node-API binding (default) | Standard apps |
binary | Separate child process | Sandboxed runtimes |
client NEW | Pure JS, no Rust engine | Edge/serverless via driver adapters |
10. Configuring Logging Levels
| Level | Outputs |
query | SQL queries with params |
info | Connection info, lifecycle |
warn | Non-fatal warnings |
error | Errors only (default) |
const prisma = new PrismaClient({
log: [
{ emit: "event", level: "query" },
{ emit: "stdout", level: "warn" },
{ emit: "stdout", level: "error" }
]
});