Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 36 ยท Getting Started

Configuring the Datasource, Generator and prisma.config.ts

Understand the generator and datasource blocks, the prisma.config.ts file, environment variables, multiple databases and which adapter matches PostgreSQL, MySQL or SQLite.

Every Prisma project is configured in three places: the generator and datasource blocks inside schema.prisma, and the prisma.config.ts file next to it. Knowing which setting lives where saves a lot of confusion, especially when you move from a local SQLite file to PostgreSQL in production.

The generator block

The generator decides what Prisma builds from your schema:

PRISMA
generator client {
  provider     = "prisma-client"
  output       = "../generated/prisma"
  moduleFormat = "esm"
  runtime      = "nodejs"
}
Setting Meaning
provider "prisma-client" is the current generator (the older "prisma-client-js" wrote into node_modules)
output Folder the client is written to; required, and relative to the schema file
moduleFormat "esm" or "cjs", matching your project
runtime Target runtime such as nodejs, bun, deno or an edge runtime

You may have several generators in one schema, for example one for the client and one for a tool that draws diagrams. Each has its own name (client above is only a label).

The datasource block

PRISMA
datasource db {
  provider = "postgresql"
}

In Prisma 7 the datasource block says only which kind of database you use (postgresql, mysql, sqlite, sqlserver, cockroachdb, mongodb). The connection URL moved out of the schema into prisma.config.ts, and at runtime the connection comes from the driver adapter. Older tutorials show url = env("DATABASE_URL") inside this block; that was the Prisma 6 way. If you maintain an older project, keep that form until you upgrade.

prisma.config.ts

TypeScript
import "dotenv/config";
import { defineConfig, env } from "prisma/config";

export default defineConfig({
  schema: "prisma/schema.prisma",
  migrations: {
    path: "prisma/migrations",
    seed: "tsx prisma/seed.ts",
  },
  datasource: {
    url: env("DATABASE_URL"),
  },
});
  • schema can be a single file or a folder (see multi-file schemas below).
  • migrations.path is where migration folders are stored; migrations.seed is the command prisma db seed runs.
  • datasource.url is what the CLI uses for migrate, db push and studio. Your application passes its own connection settings to the adapter, so both read the same environment variable in practice.
  • Prisma 7 no longer loads .env automatically, which is why the first line imports dotenv/config.

env("DATABASE_URL") throws a clear error when the variable is missing, which is better than a silent undefined.

Choosing the adapter

The adapter for your application must match your database:

Database Adapter package Typical DATABASE_URL
SQLite @prisma/adapter-better-sqlite3 file:./dev.db
PostgreSQL @prisma/adapter-pg postgresql://user:pass@localhost:5432/blog
MySQL / MariaDB @prisma/adapter-mariadb mysql://user:pass@localhost:3306/shop
SQL Server @prisma/adapter-mssql a connection string

PostgreSQL example:

TypeScript
import "dotenv/config";
import { PrismaPg } from "@prisma/adapter-pg";
import { PrismaClient } from "./generated/prisma/client";

const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL });
export const prisma = new PrismaClient({ adapter });

Switching databases means three edits: the provider in the datasource block, the adapter import, and the URL. Model definitions rarely change, though some column types and features differ per database (the next chapter covers native types).

Multiple environments

Use a different .env per machine, and set real environment variables in production (never ship a .env file). A common pattern is one variable per environment: a local SQLite or Docker database for development, a separate test database, and a managed server in production. This stand-in picks the URL by NODE_ENV and fails loudly when it is missing, the same behaviour you want from env().

Node.js Stand-in: env() that fails loudly

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Multi-file schemas

Large projects split the schema into several .prisma files in one folder, for example user.prisma, post.prisma and base.prisma (the one with generator and datasource). Point schema in prisma.config.ts at the folder:

TypeScript
export default defineConfig({
  schema: "prisma/schema",   // a folder of *.prisma files
});

Prisma merges them into one schema. Models can reference each other across files without imports. It is a good idea once a schema passes a few hundred lines.

Validating and formatting

Terminal
npx prisma validate   # checks the schema for errors
npx prisma format     # re-indents and aligns the schema

Run format before committing so diffs stay small. The VS Code extension "Prisma" formats on save and highlights errors as you type.

This second stand-in shows the flavour of validate: it checks that every model has an identifier, one of the most common schema errors.

Node.js Stand-in: a tiny schema validator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In Prisma 7, where does the CLI read the database URL for migrations from?

    Prisma 7 moved the URL out of schema.prisma into prisma.config.ts (usually via env('DATABASE_URL')).
  2. 2. What does the output setting of the generator block control?

    output is the folder, relative to the schema, where prisma generate writes the client code.
  3. 3. You switch from SQLite to PostgreSQL. Which three things must change?

    The provider, the driver adapter and the connection URL are database-specific; most models stay the same.
  4. 4. Why import dotenv/config at the top of prisma.config.ts?

    Without it, env('DATABASE_URL') would not see values from your .env file.

Summary

  • generator builds the client; datasource names the database type; prisma.config.ts holds CLI settings and the URL.
  • Pick the driver adapter that matches your database.
  • env() fails loudly for missing variables; load .env yourself with dotenv/config.
  • Use prisma validate and prisma format, and consider a multi-file schema for big projects.

Next: Your First Prisma Queries