Under the Hood of Prisma 7: Migration & prisma.config.ts Setup

Author: Muntai

Under the Hood of Prisma 7: Migration & prisma.config.ts Setup

What is the difference between Prisma 6 and 7? Why should you upgrade?

The main architectural change is the separation of the connection configuration from the database schema itself.

Previously (in version 6), we were forced to hardcode the environment variables right inside schema.prisma using env("DATABASE_URL"). It worked, but as projects grew, problems emerged: difficulties with monorepos, inconveniences when writing custom CI/CD scripts, and hacky workarounds for connecting to different databases (e.g., for testing vs. production).

Developer benefits in Prisma 7:

  1. Clean schema: Your schema.prisma is now exclusively responsible for data models (tables) and generators. No connection logic at all!

  2. TypeScript-first configuration: The prisma.config.ts file has been introduced. This means you can now leverage the full power of TypeScript and Node.js to configure your connection: import dotenv, dynamically construct URLs, and fetch secrets from external services.

  3. Perfect for complex platforms: If your directory service scales up and requires complex migration logic or connections to connection poolers, the TS config makes this process highly controlled and transparent.

How to upgrade to Prisma 7?

The transition isn't difficult, so there's no need to be afraid of it. But you need to set everything up correctly, step by step. Since I use the pnpm package manager in my projects, all commands will be tailored for it.

1. Remove Prisma 6 and old dependencies

First, let's clean the project from the old versions. Open the terminal and type:

Bash

pnpm remove prisma @prisma/client

2. Install Prisma 7

Now let's install the fresh packages:

Bash

pnpm add -D prisma
pnpm add @prisma/client

3. Update configuration files

Now for the most important part — adapting the code. Open our good old prisma/schema.prisma file and ruthlessly delete the url line from the datasource db block.

Now it should look as minimalist as possible:

Фрагмент коду

// prisma/schema.prisma

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
}

// ... your models go here

Next, in the root of your project (where package.json lives), create a new configuration file prisma.config.ts and write the connection logic:

TypeScript

// prisma.config.ts

import 'dotenv/config'
import { defineConfig } from 'prisma/config'

export default defineConfig({
  schema: 'prisma/schema.prisma',
  migrations: {
    path: 'prisma/migrations',
  },
  datasource: {
    url: process.env['DATABASE_URL'],
  },
})

Note: we import dotenv/config, which guarantees that the variables from your .env file will be successfully read before Prisma initializes.

4. Generate the client

The final step. For Prisma to pick up the new config and regenerate the types for your TypeScript code, just run:

Bash

pnpm prisma generate

That's it! Your project has been successfully upgraded to Prisma 7. The database runs stably, the code has become cleaner, and the architecture is much more ready for scaling.

Write to me
Please fill out the form below to start a conversation with me.

This site is protected by reCAPTCHA. The Google Privacy Policy and Terms of Service apply.