Prisma Schema Definition Guide
The schema.prisma file is the core configuration file for any Prisma project. It defines your database connection, determines how the client is generated, and outlines your data models.
1. Initializing Prisma in your Project
To get started, install the Prisma Command Line Interface (CLI) as a development dependency:
# Install Prisma CLI
npm install prisma --save-devNext, initialize the Prisma configuration folder in your project root:
# Initialize Prisma configuration files
npx prisma initThis command creates a new folder named prisma containing a template schema.prisma file, and appends a .env file to your project root.
2. Structural Breakdown of schema.prisma
Open prisma/schema.prisma. The configuration contains three main sections:
A. Generator Block
The generator block tells Prisma which client output formats to generate. The default generator is prisma-client-js, which compiles the custom TypeScript client library:
generator client {
provider = "prisma-client-js"
}B. Datasource Block
The datasource block configures the connection details for your database. You specify the database provider (such as postgresql, mysql, sqlite, or mongodb) and reference a connection string URL:
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}C. Data Models Block
The data models block defines your application database tables and relationships. Models are declared using the model keyword:
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
}3. Environment Variables Configuration
To avoid committing sensitive credentials (like passwords) to version control systems, Prisma reads the connection URL dynamically from the .env file using the env helper:
# .env file configuration example
DATABASE_URL="postgresql://johndoe:secretpassword@localhost:5432/mydb?schema=public"