Skip to main content
Prisma Documentation Docs

Search documentation

Type to search this documentation.

On this pageOverview

pnpm workspaces (Prisma ORM v7) (/docs/guides/v7/deployment/pnpm-workspaces)

For the complete Prisma documentation index, see llms.txt. A markdown version of any docs page is available by appending .md to its URL.

Learn step-by-step how to integrate Prisma ORM in a pnpm workspaces monorepo through a shared database package

Location: Guides > v7 > Deployment > pnpm workspaces

This guide shows you how to set up Prisma ORM in its own package within a pnpm Workspaces monorepo, so that every app in the workspace shares one Prisma Client and one set of generated types.

  • How to initialize a monorepo using pnpm Workspaces.
  • Steps to integrate Prisma as a standalone package.
  • How to generate and share the Prisma Client across packages.
  • Integrating the Prisma package into an application within your workspace.

1. Prepare your project and configure pnpm workspaces

Section titled “1. Prepare your project and configure pnpm workspaces”

Before integrating Prisma, you need to set up your project structure. Start by creating a new directory for your project (for example, my-monorepo) and initialize a Node.js project:

Bash
mkdir my-monorepo
cd my-monorepo
pnpm init

Next, create a pnpm-workspace.yaml file to define your workspace structure and pin the Prisma version:

Bash
touch pnpm-workspace.yaml

Add the following configuration to pnpm-workspace.yaml:

pnpm-workspace.yaml
packages:
  - "apps/*"
  - "packages/*"
catalogs:
  prisma:
    prisma: 7.10.0

[!NOTE] The catalogs help you pin a certain version of prisma across your repositories. You can learn more about them in the pnpm catalogs documentation. Explicitly pin prisma to 7.10.0 in the pnpm-workspace.yaml file.

Finally, create directories for your applications and shared packages:

Bash
mkdir apps
mkdir -p packages/database

This section covers creating a standalone database package that uses Prisma. The package will house all database models and the generated Prisma Client, making it reusable across your monorepo.

2.1. Initialize the package and install dependencies

Section titled “2.1. Initialize the package and install dependencies”

Navigate to the packages/database directory and initialize a new package:

Bash
cd packages/database
pnpm init

Add Prisma as a development dependency in your package.json using the pinned catalog:

database/package.json
"devDependencies": {  "prisma": "catalog:prisma"}

Then install Prisma:

Bash
pnpm install

Then, add additional dependencies:

Bash
pnpm add typescript tsx @types/node @types/pg -D
pnpm add @prisma/adapter-pg pg

[!NOTE] If you are using a different database provider (MySQL, SQL Server, SQLite), install the corresponding driver adapter package instead of @prisma/adapter-pg. For more information, see Database drivers.

Initialize a tsconfig.json file for your database package:

Bash
pnpm tsc --init

2.2. Setup Prisma ORM in your database package

Section titled “2.2. Setup Prisma ORM in your database package”

Initialize Prisma ORM with an instance of Prisma Postgres in the database package by running the following command:

Bash
pnpm prisma init

[!NOTE] prisma init creates the Prisma scaffolding and a local DATABASE_URL. In the next step, create a Prisma Postgres database and replace that value with a direct postgres://... connection string.

This command:

  • Creates a prisma directory containing a schema.prisma file for your database models.
  • Creates a .env file with a local DATABASE_URL.

Create a Prisma Postgres database and replace the generated DATABASE_URL in your .env file with the postgres://... connection string from the CLI output:

Bash
npx create-db

Edit the schema.prisma file to define a User model in your database and specify a custom output directory to generate the Prisma Client. This ensures that generated types are resolved correctly:

prisma/schema.prisma
generator client {  provider = "prisma-client"  output = "../generated/client"}datasource db {  provider = "postgresql"}model User {  id    Int     @id @default(autoincrement())  email String  @unique  name  String?}

Now, create a prisma.config.ts file in the database package to configure Prisma:

database/prisma.config.ts
import "dotenv/config";import { defineConfig, env } from "prisma/config";export default defineConfig({  schema: "prisma/schema.prisma",  migrations: {    path: "prisma/migrations",  },  datasource: {    url: env("DATABASE_URL"),  },});

[!NOTE] You'll need to install the dotenv package to load environment variables from the .env file:

Bash
pnpm add dotenv

Next, add helper scripts to your package.json to simplify Prisma commands:

database/package.json
{  "scripts": {    "test": "echo \"Error: no test specified\" && exit 1",    "db:generate": "prisma generate",    "db:migrate": "prisma migrate dev",    "db:deploy": "prisma migrate deploy",    "db:studio": "prisma studio"  }}

Use Prisma Migrate to migrate your database changes:

Bash
pnpm run db:migrate

When prompted by the CLI, enter a descriptive name for your migration.

Once the migration is successful, create a client.ts file to initialize Prisma Client with a driver adapter:

database/client.ts
import { PrismaClient } from "./generated/client/client";import { PrismaPg } from "@prisma/adapter-pg";const adapter = new PrismaPg({  connectionString: process.env.DATABASE_URL,});// Use globalThis for broader environment compatibilityconst globalForPrisma = globalThis as typeof globalThis & {  prisma?: PrismaClient;};// Named export with global memoizationexport const prisma: PrismaClient =  globalForPrisma.prisma ??  new PrismaClient({    adapter,  });if (process.env.NODE_ENV !== "production") {  globalForPrisma.prisma = prisma;}

Then, create an index.ts file to re-export the instance of Prisma Client and all generated types:

database/index.ts
export { prisma } from "./client";export * from "./generated/client/client";

At this point, your shared database package is fully configured and ready for use across your monorepo.

3. Set up and integrate your frontend application

Section titled “3. Set up and integrate your frontend application”

Now that the database package is set up, create a frontend application (using Next.js) that uses the shared Prisma Client to interact with your database.

Navigate to the apps directory:

Bash
cd ../../apps

Create a new Next.js app named web:

Bash
pnpm create next-app@latest web --yes

[!NOTE] important

The --yes flag uses default configurations to bootstrap the Next.js app (which in this guide uses the app router without a src/ directory and pnpm as the installer).

The flag may also initialize a Git repository in the web folder. If that happens, please remove the .git directory by running rm -r .git.

Then, navigate into the web directory:

Bash
cd web/

Copy the .env file from the database package to ensure the same environment variables are available:

Bash
cp ../../packages/database/.env .

Open the package.json file of your Next.js app and add the shared database package as a dependency:

web/package.json
"dependencies": {  "database": "workspace:*",  // additional dependencies  // ...}

Run the following command to install the database package:

Bash
pnpm install

3.2. Integrate the shared database package in your app code

Section titled “3.2. Integrate the shared database package in your app code”

Modify your Next.js application code to use Prisma Client from the database package. Update app/page.tsx as follows:

app/page.tsx
import { prisma } from "database";

export default async function Home() {
  const user = await prisma.user.findFirst({
    select: {
      name: true,
    },
  });

  return (
    <div>
      {user?.name && <p>Hello from {user.name}</p>}
      {!user?.name && <p>No user has been added to the database yet.</p>}
    </div>
  );
}

This code demonstrates importing and using the shared Prisma Client to query your User model.

3.3. Add helper scripts and run your application

Section titled “3.3. Add helper scripts and run your application”

Add the following scripts to the root package.json of your monorepo. They ensure that database migrations, type generation, and app builds run in the proper order:

JSON
"scripts": {  "build": "pnpm --filter database db:deploy && pnpm --filter database db:generate  && pnpm --filter web build",  "start": "pnpm --filter web start",  "dev": "pnpm --filter database db:generate && pnpm --filter web dev",  "studio": "pnpm --filter database db:studio"}

Then head back to the root of the monorepo:

Bash
cd ../../

Start your development server by executing:

Bash
pnpm run dev

Open your browser at http://localhost:3000 to see your app in action.

3.5. (Optional) Add data to your database using Prisma Studio

Section titled “3.5. (Optional) Add data to your database using Prisma Studio”

There shouldn't be data in your database yet. You can execute pnpm run studio in your CLI to start a Prisma Studio in http://localhost:5555 to interact with your database and add data to it.

You have now created a monorepo that uses Prisma ORM, with a shared database package integrated into a Next.js application.

To add task orchestration and caching on top of this setup, see the How to use Prisma ORM with Turborepo guide.

  • Bun workspaces: Learn step-by-step how to integrate Prisma ORM in a Bun workspaces monorepo through a shared database package
  • Cloudflare D1: Learn how to use Prisma ORM with Cloudflare D1
  • Cloudflare Workers: Learn how to use Prisma ORM and Prisma Postgres in a Cloudflare Workers project
  • Docker: Learn step-by-step configure a Prisma ORM app in Docker
  • Turborepo: Learn step-by-step how to integrate Prisma ORM with Turborepo through a shared database package
Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu