# Bun workspaces (Prisma ORM v7) (/docs/guides/v7/deployment/bun-workspaces) > 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. Learn step-by-step how to integrate Prisma ORM in a Bun workspaces monorepo through a shared database package Location: Guides > v7 > Deployment > Bun workspaces ## Introduction [#introduction] This guide shows you how to use Prisma ORM in a [Bun Workspaces](https://bun.sh/docs/install/workspaces) monorepo. You'll set up a shared database package with Prisma ORM, then integrate it into a Next.js app in the same workspace. ## Prerequisites [#prerequisites] - [Bun](https://bun.sh/docs/installation) installed - A [Prisma Postgres](https://www.prisma.io/docs/postgres) database (or another [supported database](https://www.prisma.io/docs/orm/v7/reference/supported-databases)) ## 1. Set up project [#1-set-up-project] Before integrating Prisma ORM, 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: #### npm ```bash mkdir my-monorepo cd my-monorepo npm init ``` #### pnpm ```bash mkdir my-monorepo cd my-monorepo pnpm init ``` #### yarn ```bash mkdir my-monorepo cd my-monorepo yarn init ``` #### bun ```bash mkdir my-monorepo cd my-monorepo bun init ``` Next, add the `workspaces` array to your root `package.json` to define your workspace structure: ```json title="package.json" { "name": "my-monorepo", // [!code ++] "workspaces": ["apps/*", "packages/*"] // [!code ++] } ``` Finally, create directories for your applications and shared packages: ```bash mkdir apps mkdir -p packages/database ``` ## 2. Set up database package [#2-set-up-database-package] This section covers creating a standalone database package that uses Prisma ORM. The package will house all database models and the generated Prisma ORM client, making it reusable across your monorepo. ### 2.1. Install dependencies [#21-install-dependencies] Navigate to the `packages/database` directory and initialize a new package: #### npm ```bash cd packages/database npm init ``` #### pnpm ```bash cd packages/database pnpm init ``` #### yarn ```bash cd packages/database yarn init ``` #### bun ```bash cd packages/database bun init ``` Install the required Prisma ORM packages and other dependencies: #### npm ```bash npm install prisma@prev typescript tsx @types/node @types/pg --save-dev npm install @prisma/client@7 @prisma/adapter-pg pg ``` #### pnpm ```bash pnpm add prisma@prev typescript tsx @types/node @types/pg --save-dev pnpm add @prisma/client@7 @prisma/adapter-pg pg ``` #### yarn ```bash yarn add prisma@prev typescript tsx @types/node @types/pg --dev yarn add @prisma/client@7 @prisma/adapter-pg pg ``` #### bun ```bash bun add prisma@prev typescript tsx @types/node @types/pg --dev bun add @prisma/client@7 @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](https://www.prisma.io/docs/orm/v7/core-concepts/supported-databases/database-drivers). ### 2.2. Set up Prisma ORM and schema [#22-set-up-prisma-orm-and-schema] Initialize Prisma ORM with an instance of [Prisma Postgres](https://www.prisma.io/docs/postgres) in the `database` package by running the following command: #### npm ```bash npx prisma init ``` #### pnpm ```bash pnpm prisma init ``` #### yarn ```bash yarn prisma init ``` #### bun ```bash bunx --bun 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 `prisma.config.ts` file for configuring Prisma. - 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: #### npm ```bash npx create-db ``` #### pnpm ```bash pnpm dlx create-db ``` #### yarn ```bash yarn dlx create-db ``` #### bun ```bash bunx create-db ``` Edit the `schema.prisma` file to add a `User` model. The default generator already sets `output = "../generated/prisma"`: ```prisma title="prisma/schema.prisma" generator client { provider = "prisma-client" output = "../generated/prisma" } datasource db { provider = "postgresql" } model User { // [!code ++] id Int @id @default(autoincrement()) // [!code ++] email String @unique // [!code ++] name String? // [!code ++] } // [!code ++] ``` If the generated `prisma.config.ts` comments mention installing `dotenv`, install it so environment variables load: #### npm ```bash npm install dotenv ``` #### pnpm ```bash pnpm add dotenv ``` #### yarn ```bash yarn add dotenv ``` #### bun ```bash bun add dotenv ``` Add a `scripts` section to your database `package.json` (Bun init may not add one by default): ```json title="database/package.json" { "scripts": { // [!code ++] "db:generate": "prisma generate", // [!code ++] "db:migrate": "prisma migrate dev", // [!code ++] "db:deploy": "prisma migrate deploy", // [!code ++] "db:seed": "prisma db seed", // [!code ++] "db:studio": "prisma studio" // [!code ++] } // [!code ++] } ``` Use [Prisma Migrate](https://www.prisma.io/docs/orm/v7/prisma-migrate) to migrate your database changes: #### npm ```bash npm run db:migrate ``` #### pnpm ```bash pnpm run db:migrate ``` #### yarn ```bash yarn db:migrate ``` #### bun ```bash bun run db:migrate ``` When prompted by the CLI, enter a descriptive name for your migration. After the migration completes, run generate so the Prisma ORM client is created: #### npm ```bash npm run db:generate ``` #### pnpm ```bash pnpm run db:generate ``` #### yarn ```bash yarn db:generate ``` #### bun ```bash bun run db:generate ``` Create a `client.ts` file to initialize the Prisma ORM client with a driver adapter: ```ts title="database/client.ts" import { PrismaClient } from "./generated/prisma/client"; import { PrismaPg } from "@prisma/adapter-pg"; const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL, }); // Use globalThis for broader environment compatibility const globalForPrisma = globalThis as typeof globalThis & { prisma?: PrismaClient; }; export 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 the Prisma ORM client and all generated types: ```ts title="database/index.ts" export { prisma } from "./client"; export * from "./generated/prisma/client"; ``` ### 2.3. Seed the database [#23-seed-the-database] Add a seed script to populate the database with sample users. Create `prisma/seed.ts` in the database package: ```ts title="database/prisma/seed.ts" import "dotenv/config"; import { PrismaClient } from "../generated/prisma/client"; import { PrismaPg } from "@prisma/adapter-pg"; const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL!, }); const prisma = new PrismaClient({ adapter }); async function main() { await prisma.user.createMany({ data: [ { email: "alice@example.com", name: "Alice" }, { email: "bob@example.com", name: "Bob" }, { email: "charlie@example.com", name: "Charlie" }, ], skipDuplicates: true, }); console.log("Seed complete."); } main() .catch((e) => { console.error(e); process.exit(1); }) .finally(async () => { await prisma.$disconnect(); }); ``` Add the `seed` option to the existing `migrations` config in your database package's `prisma.config.ts` (add the `seed` line inside `migrations`): ```ts title="database/prisma.config.ts" migrations: { path: "prisma/migrations", seed: "bun prisma/seed.ts", // [!code ++] }, ``` At this point, your shared database package is fully configured and ready for use across your monorepo. ### 2.4. Add root scripts [#24-add-root-scripts] Add the following scripts to the root `package.json` of your monorepo. They let you run database and app commands from the root: ```json title="package.json" "scripts": { // [!code ++] "build": "bun run --filter database db:deploy && bun run --filter database db:generate && bun run --filter web build", // [!code ++] "start": "bun run --filter web start", // [!code ++] "dev": "bun run --filter database db:generate && bun run --filter web dev", // [!code ++] "seed": "bun run --filter database db:seed", // [!code ++] "studio": "bun run --filter database db:studio" // [!code ++] } // [!code ++] ``` From the monorepo root, run `bun run seed` to add sample users. Run `bun run studio` to open [Prisma Studio](https://www.prisma.io/docs/studio) at [`http://localhost:5555`](http://localhost:5555) to view and edit your data. ## 3. Set up Next.js app [#3-set-up-nextjs-app] Now that the database package is set up, create a frontend application (using Next.js) that uses the shared Prisma ORM client to interact with your database. ### 3.1. Create Next.js app [#31-create-nextjs-app] Navigate to the `apps` directory: ```bash cd ../../apps ``` Create a new Next.js app named `web`: ```bash bun 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). When prompted for a package manager, choose **Bun** so the app uses Bun within the workspace. > > 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: ```json title="web/package.json" "dependencies": { "database": "workspace:*" // [!code ++] } ``` Run the following command to install the `database` package: #### npm ```bash npm install ``` #### pnpm ```bash pnpm install ``` #### yarn ```bash yarn install ``` #### bun ```bash bun install ``` ### 3.2. Add database to app [#32-add-database-to-app] Modify your Next.js application code to use the Prisma ORM client from the database package. Update `app/page.tsx` as follows: ```tsx title="app/page.tsx" import { prisma } from "database"; export default async function Home() { const user = await prisma.user.findFirst({ select: { name: true, }, }); return (
{user?.name &&

Hello from {user.name}

} {!user?.name &&

No user has been added to the database yet.

}
); } ``` This code demonstrates importing and using the shared Prisma ORM client to query your `User` model. ### 3.3. Run the app [#33-run-the-app] Then head back to the root of the monorepo: ```bash cd ../../ ``` Start your development server by executing: #### npm ```bash npm run dev ``` #### pnpm ```bash pnpm run dev ``` #### yarn ```bash yarn dev ``` #### bun ```bash bun run dev ``` Open your browser at [`http://localhost:3000`](http://localhost:3000) to see your app in action. You can run `bun run studio` to open [Prisma Studio](https://www.prisma.io/docs/studio) at [`http://localhost:5555`](http://localhost:5555) to view and edit your data. ## Next steps [#next-steps] 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](https://www.prisma.io/docs/guides/v7/deployment/turborepo) guide. ## Related pages - [Cloudflare D1](https://www.prisma.io/docs/guides/v7/deployment/cloudflare-d1): Learn how to use Prisma ORM with Cloudflare D1 - [Cloudflare Workers](https://www.prisma.io/docs/guides/v7/deployment/cloudflare-workers): Learn how to use Prisma ORM and Prisma Postgres in a Cloudflare Workers project - [Docker](https://www.prisma.io/docs/guides/v7/deployment/docker): Learn step-by-step configure a Prisma ORM app in Docker - [pnpm workspaces](https://www.prisma.io/docs/guides/v7/deployment/pnpm-workspaces): Learn step-by-step how to integrate Prisma ORM in a pnpm workspaces monorepo through a shared database package - [Turborepo](https://www.prisma.io/docs/guides/v7/deployment/turborepo): Learn step-by-step how to integrate Prisma ORM with Turborepo through a shared database package