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.
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:
[!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:
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
[!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:
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:
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.
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:
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:
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.