# Turborepo (/docs/guides/deployment/turborepo) > For the complete Prisma documentation index, see [llms.txt](https://www.prisma.io/docs/llms.txt). A markdown version of any docs page is available by appending `.md` to its URL. Share one Prisma 8 database package across the apps in a Turborepo monorepo, with contract emit and migrations wired into turbo tasks. Location: Guides > Deployment > Turborepo ## Introduction [#introduction] This guide shows you how to set up Prisma 8 as a standalone package in a [Turborepo](https://turborepo.dev/docs) monorepo, so that every app shares one database client, one contract, and one migration workflow. You scaffold a monorepo, add a `packages/database` package that owns the Prisma 8 setup, wire its tasks into `turbo.json`, and render users from the `web` Next.js app. To start a new project from a ready-made Turborepo instead, run `npm create prisma@latest -- --template turborepo`. It creates a `packages/database` package with a `User` model and three starter users it adds on the first request, a Node.js HTTP server in `apps/server` that lists the users, and the scripts to deploy the app to [Prisma Compute](https://www.prisma.io/docs/compute). This guide builds the setup by hand, which is the way to add Prisma ORM to a monorepo you already have. > [!NOTE] > Using Prisma 7? > > Prisma 8 is a [release candidate](https://www.prisma.io/docs/orm/release-status) of Prisma ORM. Prisma 7 remains fully supported; the Prisma 7 version of this guide is at [/guides/v7/deployment/turborepo](https://www.prisma.io/docs/guides/v7/deployment/turborepo). ## Prerequisites [#prerequisites] - [Node.js](https://nodejs.org) 22.18 or newer on the 22 line, 24.11 or newer on the 24 line, or 26 or newer (24 recommended) - [pnpm](https://pnpm.io) (this guide uses pnpm; the Turborepo scaffold also supports npm, yarn, and Bun) - A PostgreSQL connection string, or nothing at all: `npx create-db@latest` can create a [Prisma Postgres](https://www.prisma.io/docs/postgres) database for you ## 1. Scaffold the monorepo [#1-scaffold-the-monorepo] Create a Turborepo monorepo named `turborepo-prisma`: #### npm ```bash npx create-turbo@latest turborepo-prisma ``` #### pnpm ```bash pnpm dlx create-turbo@latest turborepo-prisma ``` #### yarn ```bash yarn dlx create-turbo@latest turborepo-prisma ``` #### bun ```bash bunx create-turbo@latest turborepo-prisma ``` When asked which package manager to use, pick `pnpm`. The scaffold creates two Next.js apps and three shared packages, then installs dependencies: ```text no-copy >>> Creating a new Turborepo with: Application packages - apps/docs - apps/web Library packages - packages/eslint-config - packages/typescript-config - packages/ui >>> Success! Created your Turborepo at turborepo-prisma ``` Move into the project root: ```bash cd turborepo-prisma ``` ## 2. Create the `database` package [#2-create-the-database-package] ### 2.1. Create the package [#21-create-the-package] Create a `database` directory inside `packages` with a minimal `package.json`: ```bash mkdir -p packages/database ``` ```json title="packages/database/package.json" { "name": "@repo/db", "version": "0.0.0", "private": true } ``` ### 2.2. Allow the build scripts Prisma needs [#22-allow-the-build-scripts-prisma-needs] pnpm does not run dependency build scripts until you allow them. Since pnpm 11 it fails the install when it finds scripts it was not told about; pnpm 10 only warns and skips them unless you set `strictDepBuilds: true`. The `allowBuilds` key exists since pnpm 10.26, so use that version or later. Prisma ORM 8's toolchain ships one package with a build script, `esbuild`, so declare it in `pnpm-workspace.yaml` before you install anything: ```yaml title="pnpm-workspace.yaml" packages: - "apps/*" - "packages/*" allowBuilds: # [!code ++] esbuild: true # [!code ++] ``` Skip this step on npm, yarn, or Bun. If you forget it, `orm init` in the next step stops with `CLI.INIT_INSTALL_FAILED`; run `pnpm approve-builds`, then re-run the `pnpm add` commands it printed and `pnpm contract:emit`. ### 2.3. Initialize Prisma 8 [#23-initialize-prisma-8] Run `orm init` inside the package. It is the existing-project path: it adds Prisma 8 to the package you just created instead of scaffolding a new app. ```bash cd packages/database ``` #### npm ```bash npx prisma@latest orm init --yes --target postgres --authoring psl ``` #### pnpm ```bash pnpm dlx prisma@latest orm init --yes --target postgres --authoring psl ``` #### yarn ```bash yarn dlx prisma@latest orm init --yes --target postgres --authoring psl ``` #### bun ```bash bunx prisma@latest orm init --yes --target postgres --authoring psl ``` `--yes` accepts the defaults for the remaining prompts (the Prisma schema language for the contract, and `src/prisma/contract.prisma` as its path); drop it to answer them yourself. ```text no-copy ✔ pnpm add @prisma/orm-postgres dotenv pg ✔ pnpm add @prisma/orm-family-sql@8.0.0-rc.28 @prisma/orm-framework@8.0.0-rc.28 @prisma/orm-target-postgres@8.0.0-rc.28 @prisma/orm-toolchain@8.0.0-rc.28 '@types/pg@^8.20.0' 'temporal-polyfill@^1.0.8' ✔ pnpm add -D prisma@latest @types/node ✔ Emit the contract │ target: postgres │ authoring: psl │ schema: src/prisma/contract.prisma written ├─ src/prisma/contract.prisma ├─ prisma.config.ts ├─ src/prisma/db.ts ├─ prisma-8.md ├─ .env.example ├─ tsconfig.json ├─ .gitignore ├─ .gitattributes └─ package.json installed ├─ @prisma/orm-postgres@8.0.0-rc.28 ├─ @prisma/orm-family-sql@8.0.0-rc.28 ├─ @prisma/orm-framework@8.0.0-rc.28 ├─ @prisma/orm-target-postgres@8.0.0-rc.28 ├─ @prisma/orm-toolchain@8.0.0-rc.28 ├─ dotenv ├─ @types/pg@^8.20.0 ├─ pg ├─ temporal-polyfill@^1.0.8 ├─ prisma@latest (dev) └─ @types/node (dev) ✔ Done. Open prisma-8.md to get started. → Set DATABASE_URL in your environment (export it or add it to .env) → Edit your schema at src/prisma/contract.prisma, then emit again → Open prisma-8.md for a quick reference on writing your first typed query → Set up the Prisma agent skills for your coding agent: prisma init Prisma agent skills are out of date (installed @prisma/orm-postgres 8.0.0-rc.28, synced none). Run: prisma skills sync ``` The last line is a reminder from the Prisma CLI, and every `prisma` command in this package prints it from now on. Prisma ships instructions for coding agents, called skills, inside its packages, and none have been copied into the package yet. Run `pnpm prisma init` once inside `packages/database` to copy them in, as [Use with your agent](#use-with-your-agent) describes. The line never makes a command fail, and [The staleness check](https://www.prisma.io/docs/cli/skills#the-staleness-check) lists the ways to turn it off. `orm init` detects pnpm from the workspace, adds the packages to `@repo/db`, sets `"type": "module"`, and writes four files that matter for the rest of this guide: - `src/prisma/contract.prisma`: your schema. It starts with `User` and `Post` models. - `src/prisma/contract.json` and `src/prisma/contract.d.ts`: the emitted contract the runtime and the CLI read. They replace Prisma 7's generated client: there is no `prisma generate` step and no generated directory to ignore. Commit both files; `.gitattributes` already marks them as generated so they collapse in diffs. - `src/prisma/db.ts`: the client instance, typed by the contract. - `prisma.config.ts`: where the CLI finds the contract and the database connection. The generated `db.ts` reads `DATABASE_URL` from the environment and is the module every app will import: ```typescript title="packages/database/src/prisma/db.ts" import 'dotenv/config'; import postgres from '@prisma/orm-postgres/runtime'; import { contractFromJson } from '@prisma/orm-postgres/static'; import type { Contract } from './contract.d.js'; import contractJson from './contract.json' with { type: 'json' }; export const contract = contractFromJson(contractJson); export const db = postgres({ contract, url: process.env['DATABASE_URL']!, }); ``` ### 2.4. Set the database connection [#24-set-the-database-connection] Create `packages/database/.env` with your PostgreSQL connection string. Use your own, or create a Prisma Postgres database with `npx create-db@latest`, which prints a connection string and a claim URL. Claim the database with a free Prisma account if you want to keep it. ```bash title="packages/database/.env" DATABASE_URL="postgres://user:password@localhost:5432/turborepo_prisma" ``` `prisma.config.ts` and `db.ts` both load this file through `dotenv/config`, so every CLI command in this package and every query at runtime picks it up. The root `.gitignore` from the scaffold already ignores `.env` files. ### 2.5. Review the contract [#25-review-the-contract] Open `src/prisma/contract.prisma`. The starter contract already has the two models this guide renders, so leave it as it is for now; you will change it in step 7. ```prisma title="packages/database/src/prisma/contract.prisma" // use prisma-8 model User { id Int @id @default(autoincrement()) email String @unique username String? name String? posts Post[] createdAt DateTime @default(now()) @onCreate(now()) updatedAt temporal.UpdatedAt } model Post { id Int @id @default(autoincrement()) title String content String? author User @relation(fields: [authorId], references: [id]) authorId Int createdAt DateTime @default(now()) @onCreate(now()) updatedAt temporal.UpdatedAt } ``` ### 2.6. Add scripts and export the client [#26-add-scripts-and-export-the-client] Add the database scripts and a package entrypoint to `packages/database/package.json`. `orm init` already added `contract:emit`; the rest map to the Prisma 8 migration loop: ```json title="packages/database/package.json" { "name": "@repo/db", "type": "module", "version": "0.0.0", "private": true, "exports": { // [!code ++] ".": "./src/index.ts" // [!code ++] }, // [!code ++] "scripts": { "contract:emit": "prisma contract emit", "migration:plan": "prisma migration plan", // [!code ++] "migration:status": "prisma migration status", // [!code ++] "db:migrate": "prisma db migrate", // [!code ++] "db:deploy": "prisma db migrate --no-advance-ref", // [!code ++] "db:verify": "prisma db verify", // [!code ++] "check-types": "tsc --noEmit" // [!code ++] }, "dependencies": { "@prisma/orm-family-sql": "8.0.0-rc.28", "@prisma/orm-framework": "8.0.0-rc.28", "@prisma/orm-postgres": "8.0.0-rc.28", "@prisma/orm-target-postgres": "8.0.0-rc.28", "@prisma/orm-toolchain": "8.0.0-rc.28", "@types/pg": "...", "dotenv": "...", "pg": "...", "temporal-polyfill": "..." }, "devDependencies": { "@types/node": "26.4.1", "prisma": "8.0.0-rc.39" } } ``` Keep the package versions `orm init` wrote for you. The five `@prisma/orm-*` lines are the library and the platform packages it is built from, which your code never imports; [What your package.json lists](https://www.prisma.io/docs/orm/supported-databases#what-your-packagejson-lists) explains them. The scripts replace the Prisma 7 scripts `db:generate`, `db:migrate`, and `db:deploy`: - `contract:emit` compiles `contract.prisma` into `contract.json` and `contract.d.ts`. It is offline, and the same contract always produces the same files, which is what makes it cacheable in Turborepo. - `migration:plan` diffs the emitted contract against your migration history and writes a reviewable migration directory. Also offline. - `db:migrate` applies pending migrations to your development database. It also records the applied state in `migrations/app/refs/db.json`, so the next `migration:plan` knows where to start from and plans only your new change. - `db:deploy` applies pending migrations to any other database, such as staging or production, from CI or a deploy step. It runs the same command with `--no-advance-ref`, because the `db` ref describes your development database and a deploy must not move it. Then create the package entrypoint. It re-exports the client and the contract type so apps import one module: ```typescript title="packages/database/src/index.ts" export { db } from "./prisma/db"; export type { Contract } from "./prisma/contract.d.js"; ``` This follows Turborepo's [Just-in-Time packaging](https://turborepo.dev/docs/core-concepts/internal-packages#just-in-time-packages) pattern: the package exports TypeScript source and the consuming app's bundler compiles it. Next.js handles this for workspace packages without extra configuration. > [!WARNING] > If a consumer is not using a bundler, use the [Compiled Packages](https://turborepo.dev/docs/core-concepts/internal-packages#compiled-packages) strategy instead, and add `src/prisma/contract.json` to the files you ship. ## 3. Configure tasks in `turbo.json` [#3-configure-tasks-in-turbojson] Go back to the project root and wire the database tasks into `turbo.json`: ```bash cd ../.. ``` ```json title="turbo.json" { "$schema": "https://turborepo.dev/schema.json", "ui": "tui", "globalEnv": ["DATABASE_URL"], // [!code ++] "tasks": { "build": { "dependsOn": ["^build", "^contract:emit"], // [!code highlight] "inputs": ["$TURBO_DEFAULT$", ".env*"], "outputs": [".next/**", "!.next/cache/**", "!.next/dev/**"] }, "lint": { "dependsOn": ["^lint"] }, "check-types": { "dependsOn": ["^check-types", "^contract:emit"] // [!code highlight] }, "dev": { "dependsOn": ["^contract:emit"], // [!code ++] "cache": false, "persistent": true }, "contract:emit": { // [!code ++] "inputs": ["src/prisma/contract.prisma", "prisma.config.ts"], // [!code ++] "outputs": ["src/prisma/contract.json", "src/prisma/contract.d.ts"] // [!code ++] }, // [!code ++] "migration:plan": { // [!code ++] "dependsOn": ["contract:emit"], // [!code ++] "cache": false // [!code ++] }, // [!code ++] "db:migrate": { // [!code ++] "cache": false // [!code ++] }, // [!code ++] "db:deploy": { // [!code ++] "cache": false // [!code ++] } // [!code ++] } } ``` What each entry does: - `contract:emit` is a normal cached task. Its inputs are the contract source and the Prisma config, and its outputs are the two emitted files. When nothing changed, Turborepo replays the cache hit; when the contract changed, it re-emits before anything depends on it. - `build`, `dev`, and `check-types` depend on `^contract:emit`, so any app that depends on `@repo/db` gets a fresh contract before it compiles. In Prisma 7 this slot held `db:generate`; here the artifacts are committed, and the dependency keeps them from going stale when someone edits the contract and forgets to emit. - `migration:plan` depends on the package's own `contract:emit`, so a plan always diffs the current contract. Planning and migrating are never cached, because they change files on disk and the database. - `globalEnv` lets `DATABASE_URL` from your shell reach every task and makes it part of the task hash. The `.env*` input on `build` does the same for the per-app `.env` files. ## 4. Plan and apply the first migration [#4-plan-and-apply-the-first-migration] Plan the first migration from the project root. Turborepo runs `contract:emit` first, then passes `--name init` through to `migration plan`: ```bash pnpm turbo run migration:plan --filter=@repo/db -- --name init ``` ```text no-copy @repo/db:contract:emit: cache miss, executing 9c34f32395d2b5f5 @repo/db:contract:emit: $ prisma contract emit ... @repo/db:contract:emit: ✔ Emitted contract.json and contract.d.ts ... @repo/db:migration:plan: cache bypass, force executing 9290da8c56fbf832 @repo/db:migration:plan: $ prisma migration plan --name init ... @repo/db:migration:plan: ✔ Planned 6 operation(s) @repo/db:migration:plan: ℹ No db ref set — planning from an empty database. Run db init, db update, or db sign if a database already exists. @repo/db:migration:plan: @repo/db:migration:plan: migrations/app/20261120T1050_init @repo/db:migration:plan: ├─ Create schema "public" @repo/db:migration:plan: ├─ Create table "Post" @repo/db:migration:plan: ├─ Create table "User" @repo/db:migration:plan: ├─ Add unique constraint on "User" (email) @repo/db:migration:plan: ├─ Create index "Post_authorId_idx_e47547ed" on "Post" @repo/db:migration:plan: └─ Add foreign key "Post_authorId_fkey" on "Post" @repo/db:migration:plan: @repo/db:migration:plan: from: (baseline) @repo/db:migration:plan: to: de0326a8209b6a1a2663b6c532079b8e86f21dae6728979fa9cb63826a867b5e @repo/db:migration:plan: app space: migrations/app/20261120T1050_init @repo/db:migration:plan: @repo/db:migration:plan: ℹ DDL preview @repo/db:migration:plan: @repo/db:migration:plan: CREATE SCHEMA IF NOT EXISTS "public"; @repo/db:migration:plan: CREATE TABLE "public"."Post" ( ... ``` Depending on your terminal, Turborepo shows the tasks in its terminal UI while they run, or prints each task's log with the task name in front of every line, as above. The Prisma CLI prints the same human-readable output either way, and in CI too. The command is offline: it writes `packages/database/migrations/app/_init/` with the migration as TypeScript (`migration.ts`), the compiled operations (`ops.json`), and the history marker (`migration.json`), and prints the exact DDL it will run. Review it, then apply it: ```bash pnpm turbo run db:migrate --filter=@repo/db ``` ```text no-copy @repo/db:db:migrate: cache bypass, force executing cf874fc76510a7ec @repo/db:db:migrate: $ prisma db migrate @repo/db:db:migrate: ✔ Running migration plan across spaces @repo/db:db:migrate: app 20261120T1050_init de0326a8209b6a1a2663b6c532079b8e86f21dae6728979fa9cb63826a867b5e 6 @repo/db:db:migrate: │ migrations: migrations @repo/db:db:migrate: │ database: postgres://****:****@localhost:5432/turborepo_prisma @repo/db:db:migrate: @repo/db:db:migrate: ✔ Applied 1 migration(s) (6 operation(s)) across 1 contract space(s) @repo/db:db:migrate: @repo/db:db:migrate: App space @repo/db:db:migrate: ├─ Create schema "public" @repo/db:db:migrate: ├─ Create table "Post" @repo/db:db:migrate: ├─ Create table "User" @repo/db:db:migrate: ├─ Add unique constraint on "User" (email) @repo/db:db:migrate: ├─ Create index "Post_authorId_idx_e47547ed" on "Post" @repo/db:db:migrate: ├─ Add foreign key "Post_authorId_fkey" on "Post" @repo/db:db:migrate: └─ signed with contract de0326a8209b6a1a2663b6c532079b8e86f21dae6728979fa9cb63826a867b5e @repo/db:db:migrate: @repo/db:db:migrate: ✔ Advanced ref "db" → de0326a8209b6a1a2663b6c532079b8e86f21dae6728979fa9cb63826a867b5e @repo/db:db:migrate: → Check every space against the database: prisma migration status ``` The tab-separated line near the top lists the migration that was applied, its start and end hashes, and its operation count. The CLI prints it because a Turborepo task has no terminal of its own; you can ignore it. Commit the `migrations/` directory with the rest of the package. CI or a deploy step runs `pnpm turbo run db:deploy --filter=@repo/db` against staging and production; it is the same `db migrate` command, with `--no-advance-ref`, so there is no separate `migrate deploy` command in Prisma 8. ## 5. Use the `database` package in the web app [#5-use-the-database-package-in-the-web-app] ### 5.1. Add the dependency [#51-add-the-dependency] Add `@repo/db` to `apps/web/package.json`: ```json title="apps/web/package.json" { // ... "dependencies": { "@repo/db": "workspace:*", // [!code ++] "@repo/ui": "workspace:*", // ... } // ... } ``` Link it from the project root: ```bash pnpm install ``` ### 5.2. Render users in a server component [#52-render-users-in-a-server-component] Replace `apps/web/app/page.tsx` with a server component that queries the database through the shared client: ```tsx title="apps/web/app/page.tsx" import { db } from "@repo/db"; import styles from "./page.module.css"; export const dynamic = "force-dynamic"; export default async function Home() { const users = await db.orm.public.User.select("id", "email", "name").all(); return (

Users

{users.length === 0 ? (

No users added yet

) : (
    {users.map((user) => (
  • {user.name ?? "Anonymous"} ({user.email})
  • ))}
)}
); } ``` On PostgreSQL you reach a model through its database schema, so the `User` model is `db.orm.public.User`, where `public` is the PostgreSQL schema, and `select(...)` narrows both the query and the row type. `dynamic = "force-dynamic"` tells Next.js to run the query per request; without it, `next build` prerenders the page once and freezes the user list at build time. ### 5.3. Give the app its connection string [#53-give-the-app-its-connection-string] Each app reads its own `.env`, so copy the one from the database package: ```bash cp packages/database/.env apps/web/.env ``` If you would rather keep a single value, export `DATABASE_URL` in your shell instead; the `globalEnv` entry from step 3 passes it into every task. Turborepo [recommends per-package `.env` files](https://turborepo.dev/docs/crafting-your-repository/using-environment-variables#use-env-files-in-packages); for one shared file across the monorepo, see the [`dotenvx` guide for Turborepo](https://dotenvx.com/docs/monorepos/turborepo). ### 5.4. Seed a couple of users [#54-seed-a-couple-of-users] The page will render "No users added yet" against an empty database. Add a small seed script to the database package so there is something to see. It runs with `tsx`, because Node.js needs file extensions on imports and the package uses extensionless, bundler-style imports: ```bash pnpm add -D tsx --filter @repo/db ``` ```typescript title="packages/database/src/seed.ts" import { db } from "./index"; const existing = await db.orm.public.User.select("id").all(); if (existing.length === 0) { await db.orm.public.User.create({ email: "alice@prisma.io", name: "Alice" }); await db.orm.public.User.create({ email: "bob@prisma.io", name: "Bob" }); } console.log(await db.orm.public.User.select("id", "email", "name").all()); await db.close(); ``` Add it as a script and run it: ```json title="packages/database/package.json" { "scripts": { // ... "seed": "tsx src/seed.ts" // [!code ++] } } ``` ```bash pnpm --filter @repo/db seed ``` ```text no-copy [ { id: 1, email: 'alice@prisma.io', name: 'Alice' }, { id: 2, email: 'bob@prisma.io', name: 'Bob' } ] ``` The script closes the client at the end because it is a one-off process; the web app never does, since its connection pool lives for the life of the server. ## 6. Run the project [#6-run-the-project] Start the web app from the project root: ```bash pnpm turbo run dev --filter=web ``` ```text no-copy ▲ Next.js 16.3.4 (Turbopack) - Local: http://localhost:3000 - Environments: .env ✓ Ready in 1005ms ``` Turborepo runs `contract:emit` for `@repo/db`, then starts Next.js. Its terminal UI lists both tasks; select `web#dev` to see the Next.js log. Open [http://localhost:3000](http://localhost:3000): the page lists the seeded users. ```text no-copy Users Alice (alice@prisma.io) Bob (bob@prisma.io) ``` `pnpm turbo run build --filter=web` and `pnpm turbo run check-types` go through the same `^contract:emit` dependency, so a production build or a type check never sees a stale contract. ## 7. Change the schema [#7-change-the-schema] The loop for every later change is: edit the contract, plan, review, apply. Add a `bio` field to `User`: ```prisma title="packages/database/src/prisma/contract.prisma" model User { id Int @id @default(autoincrement()) email String @unique username String? name String? bio String? // [!code ++] posts Post[] createdAt DateTime @default(now()) @onCreate(now()) updatedAt temporal.UpdatedAt } ``` Plan it. `contract:emit` sees the changed input and re-emits, and because `db:migrate` advanced the `db` ref, the planner starts from the applied state and plans only your new change: ```bash pnpm turbo run migration:plan --filter=@repo/db -- --name add-user-bio ``` ```text no-copy @repo/db:contract:emit: cache miss, executing 5f2858ee97897d39 @repo/db:contract:emit: $ prisma contract emit ... @repo/db:contract:emit: ✔ Emitted contract.json and contract.d.ts ... @repo/db:migration:plan: cache bypass, force executing ccab35224257df5a @repo/db:migration:plan: $ prisma migration plan --name add-user-bio ... @repo/db:migration:plan: ✔ Planned 1 operation(s) @repo/db:migration:plan: @repo/db:migration:plan: migrations/app/20261120T1050_add_user_bio @repo/db:migration:plan: └─ Add column "bio" to "User" @repo/db:migration:plan: @repo/db:migration:plan: from: de0326a8209b6a1a2663b6c532079b8e86f21dae6728979fa9cb63826a867b5e @repo/db:migration:plan: to: a906f5e114bac688ff8bccf497d0969f797e52d3b873e8a01d9acd848d5ab5de @repo/db:migration:plan: app space: migrations/app/20261120T1050_add_user_bio @repo/db:migration:plan: @repo/db:migration:plan: ℹ DDL preview @repo/db:migration:plan: @repo/db:migration:plan: ALTER TABLE "public"."User" ADD COLUMN "bio" text; ... ``` Apply it, then check where the database stands: ```bash pnpm turbo run db:migrate --filter=@repo/db pnpm --filter @repo/db migration:status ``` ```text no-copy $ prisma migration status │ migrations: migrations │ database: postgres://****:****@localhost:5432/turborepo_prisma ○ a906f5e @contract @db (db) │↑ 20261120T1050_add_user_bio de0326a → a906f5e 1 ops ✓ applied ○ de0326a │↑ 20261120T1050_init ∅ → de0326a 6 ops ✓ applied ○ ∅ ✔ Up to date ``` The next `pnpm turbo run dev --filter=web` re-emits the contract before Next.js starts, and `user.bio` is available in the page's types. For a local change you do not want to keep as a migration, `npx prisma db update` reconciles the database with the contract directly; see [db update](https://www.prisma.io/docs/cli/db-update). ## Common gotchas [#common-gotchas] > [!WARNING] > Without a `db` ref or `--from`, `migration plan` has no origin and refuses with `MIGRATION.PLAN_ORIGIN_UNKNOWN` once migrations exist, because a plan from an empty database would recreate every table. `db:migrate` keeps the ref current. If you applied a migration another way, such as with `db:deploy` or in CI, point the ref at that migration once: `npx prisma migration ref set db `. > [!WARNING] > In the web app, never call `db.close()` in a page or route handler; the client's connection pool is shared across requests. Close it only in one-off scripts like the seed. > [!NOTE] > `orm init` picks the package manager from the workspace. Run it after `create-turbo` has installed dependencies, so it finds `pnpm-lock.yaml` and adds packages with `pnpm add` instead of guessing. ## Use with your agent [#use-with-your-agent] To delegate this guide to your coding agent, copy the prompt below and hand it over: ```text Set up Prisma 8 as a shared database package in a new Turborepo monorepo and render users from the web app. 1. Scaffold: `npx create-turbo@latest turborepo-prisma` with pnpm. 2. Create `packages/database` with `{ "name": "@repo/db", "version": "0.0.0", "private": true }` as its package.json. Because pnpm refuses to run dependency build scripts until they are allowed, add `allowBuilds: { esbuild: true }` to `pnpm-workspace.yaml` first. Then run `npx prisma@latest orm init --yes --target postgres --authoring psl` inside `packages/database`. Then run `pnpm prisma init` inside `packages/database` so the Prisma agent skills are installed and stay current, and use them. 3. Get a database connection string: use the one I give you, or create a Prisma Postgres database with `npx create-db@latest` and show me the claim URL it prints. Write it as `DATABASE_URL` into `packages/database/.env`. 4. In `packages/database/package.json`, add `"exports": { ".": "./src/index.ts" }` and the scripts `contract:emit` (`prisma contract emit`), `migration:plan` (`prisma migration plan`), `db:migrate` (`prisma db migrate`), `db:deploy` (`prisma db migrate --no-advance-ref`), `migration:status` (`prisma migration status`), and `check-types` (`tsc --noEmit`). Create `src/index.ts` that re-exports `db` from `./prisma/db` and the `Contract` type from `./prisma/contract.d.js`. 5. In `turbo.json`, add `"globalEnv": ["DATABASE_URL"]`, a cached `contract:emit` task with inputs `src/prisma/contract.prisma` and `prisma.config.ts` and outputs `src/prisma/contract.json` and `src/prisma/contract.d.ts`, make `build`, `dev`, and `check-types` depend on `^contract:emit`, add `migration:plan` (dependsOn `contract:emit`, cache false), `db:migrate` (cache false), and `db:deploy` (cache false). 6. Run `pnpm turbo run migration:plan --filter=@repo/db -- --name init`, review the DDL preview, then `pnpm turbo run db:migrate --filter=@repo/db`. 7. Add `"@repo/db": "workspace:*"` to `apps/web/package.json`, run `pnpm install`, copy `packages/database/.env` to `apps/web/.env`, and replace `apps/web/app/page.tsx` with a server component that exports `const dynamic = "force-dynamic"` and renders `await db.orm.public.User.select("id", "email", "name").all()` as a list, following https://www.prisma.io/docs/guides/deployment/turborepo.md. 8. Start `pnpm turbo run dev --filter=web` in the background, wait until it reports ready, verify http://localhost:3000 renders, then stop the dev server. ``` The prompt above runs [`pnpm prisma init`](https://www.prisma.io/docs/cli/init) inside `packages/database`. If you followed the steps by hand, run it once yourself there: it installs the [Prisma 8 skills](https://www.prisma.io/docs/ai/tools/skills#available-skills-for-prisma-8) for your coding agent into the package and adds a `postinstall` script that keeps them matching your installed packages. Because `orm init` already wrote a `prisma.config.ts`, `init` leaves that file alone and warns `INIT.CONFIG_KEPT`, suggesting a `skills` setting for it. You can ignore the warning: the setting it suggests names the same four agents the CLI uses when there is no setting, so nothing in this guide changes. Add it only to choose fewer agents; see [Agent skills](https://www.prisma.io/docs/cli/configuration#agent-skills). Prompts you can give your agent after this guide: - "Using the prisma-8 skill, add a `apps/docs` page that lists posts with their authors through `@repo/db`." - "Add a `published Boolean @default(false)` field to Post, plan the migration, and show me the DDL before applying it." - "Add a `db:verify` task to turbo.json and run it in CI after `db:deploy`." ## Next steps [#next-steps] - [Learn the fundamentals](https://www.prisma.io/docs/orm/fundamentals/reading-data): filtering, sorting, pagination, and writes. - [How migrations work](https://www.prisma.io/docs/orm/migrations/how-migrations-work): the plan, review, apply loop your `turbo.json` tasks now run. - [Read the Prisma 8 overview](https://www.prisma.io/docs/orm) for the concepts behind contracts and typed queries. - [Turborepo docs](https://turborepo.dev/docs) and [Next.js docs](https://nextjs.org/docs) for the rest of the monorepo. ## Related pages - [Bun workspaces](https://www.prisma.io/docs/guides/deployment/bun-workspaces): Set up Prisma ORM in a Bun workspaces monorepo through a shared database package, seed it with Bun, and query it from a Next.js app in the same workspace. - [Cloudflare Workers](https://www.prisma.io/docs/guides/deployment/cloudflare-workers): Add Prisma ORM to a Cloudflare Worker, query PostgreSQL from the fetch handler with the nodejs_compat flag, and deploy it with Wrangler. - [Docker](https://www.prisma.io/docs/guides/deployment/docker): Build an Express app on Prisma ORM, run PostgreSQL from Docker Compose, then run the app and the database together in containers. - [pnpm workspaces](https://www.prisma.io/docs/guides/deployment/pnpm-workspaces): Set up Prisma 8 in a shared database package inside a pnpm workspaces monorepo and query it from a Next.js app. - [Vercel](https://www.prisma.io/docs/guides/deployment/vercel): Deploy a Next.js app that uses Prisma ORM to Vercel: commit the contract files, apply migrations before you deploy, set the connection string, and make pages that query render on each request.