|
| 1 | +--- |
| 2 | +title: "Turborepo monorepo with Prisma" |
| 3 | +sidebarTitle: "Turborepo monorepo with Prisma" |
| 4 | +description: "Two example projects demonstrating how to use Prisma and Trigger.dev in a Turborepo monorepo setup." |
| 5 | +--- |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +These examples demonstrate two different ways of using Prisma and Trigger.dev in a Turborepo monorepo. In both examples, a task is triggered from a Next.js app using a server action, which uses Prisma to add a user to a database table. The examples differ in how Trigger.dev is installed and configured. |
| 10 | + |
| 11 | +- Example 1: Turborepo monorepo demo with Trigger.dev and Prisma packages |
| 12 | +- Example 2: Turborepo monorepo demo with a Prisma package and Trigger.dev installed in a Next.js app |
| 13 | + |
| 14 | +<Note> |
| 15 | + |
| 16 | +You can either fork the repos below, or simply check out the project structures and code to get an idea of how to set up Trigger.dev in your own monorepos. |
| 17 | + |
| 18 | +</Note> |
| 19 | + |
| 20 | +## Example 1: Turborepo monorepo demo with Trigger.dev and Prisma packages |
| 21 | + |
| 22 | +This simple example demonstrates how to use Trigger.dev and Prisma as packages inside a monorepo created with Turborepo. The Trigger.dev task is triggered by a button click in a Next.js app which triggers the task via a server action. |
| 23 | + |
| 24 | +### GitHub repo |
| 25 | + |
| 26 | +Fork the GitHub repo below to get started with this example project. |
| 27 | + |
| 28 | +<Card |
| 29 | + title="Check out the Turborepo monorepo demo with Trigger.dev and Prisma packages" |
| 30 | + icon="GitHub" |
| 31 | + href="https://github.com/triggerdotdev/examples/tree/main/monorepos/turborepo-prisma-tasks-package" |
| 32 | +> |
| 33 | + Click here to view the full code for this project in our examples repository on GitHub. You can |
| 34 | + fork it and use it as a starting point for your own project. |
| 35 | +</Card> |
| 36 | + |
| 37 | +### Features |
| 38 | + |
| 39 | +- This monorepo has been created using the [Turborepo CLI](https://turbo.build/repo), following the official [Prisma and Turborepo docs](https://www.prisma.io/docs/guides/turborepo), and then adapted for use with Trigger.dev. |
| 40 | +- [pnpm](https://pnpm.io/) has been used as the package manager. |
| 41 | +- A tasks package (`@repo/tasks`) using [Trigger.dev](https://trigger.dev) is used to create and execute tasks from an app inside the monorepo. |
| 42 | +- A database package (`@repo/db`) using [Prisma ORM](https://www.prisma.io/docs/orm/) is used to interact with the database. You can use any popular Postgres database supported by Prisma, e.g. [Supabase](https://supabase.com/), [Neon](https://neon.tech/), etc. |
| 43 | +- A [Next.js](https://nextjs.org/) example app (`apps/web`) to show how to trigger the task via a server action. |
| 44 | + |
| 45 | +### Project structure |
| 46 | + |
| 47 | +Simplified project structure for this example: |
| 48 | + |
| 49 | +``` |
| 50 | +| |
| 51 | +| — apps/ |
| 52 | +| | — web/ # Next.js frontend application |
| 53 | +| | | — app/ # Next.js app router |
| 54 | +| | | | — api/ |
| 55 | +| | | | | — actions.ts # Server actions for triggering tasks |
| 56 | +| | | | — page.tsx # Main page with "Add new user" button |
| 57 | +| | | | — layout.tsx # App layout |
| 58 | +| | | — package.json # Dependencies including @repo/db and @repo/tasks |
| 59 | +| | |
| 60 | +| | — docs/ # Documentation app (not fully implemented) |
| 61 | +| |
| 62 | +| — packages/ |
| 63 | +| | — database/ # Prisma database package (@repo/db) |
| 64 | +| | | — prisma/ |
| 65 | +| | | | — schema.prisma # Database schema definition |
| 66 | +| | | — generated/ # Generated Prisma client (gitignored) |
| 67 | +| | | — src/ |
| 68 | +| | | | — index.ts # Exports from the database package |
| 69 | +| | | — package.json # Database package dependencies |
| 70 | +| | |
| 71 | +| | — tasks/ # Trigger.dev tasks package (@repo/tasks) |
| 72 | +| | | — src/ |
| 73 | +| | | | — index.ts # Exports from the tasks package |
| 74 | +| | | | — trigger/ |
| 75 | +| | | | — index.ts # Exports the tasks |
| 76 | +| | | | — addNewUser.ts # Task implementation for adding users |
| 77 | +| | | — trigger.config.ts # Trigger.dev configuration |
| 78 | +| | | — package.json # Tasks package dependencies |
| 79 | +| | |
| 80 | +| | — ui/ # UI components package (referenced but not detailed) |
| 81 | +| |
| 82 | +| — turbo.json # Turborepo configuration |
| 83 | +| — package.json # Root package.json with workspace config |
| 84 | +``` |
| 85 | + |
| 86 | +### Relevant files and code |
| 87 | + |
| 88 | +#### Database package |
| 89 | + |
| 90 | +- Prisma is added as a package in [`/packages/database`](https://github.com/triggerdotdev/examples/tree/main/monorepos/turborepo-prisma-tasks-package/packages/database/) and exported as `@repo/db` in the [`package.json`](https://github.com/triggerdotdev/examples/tree/main/monorepos/turborepo-prisma-tasks-package/packages/database/package.json) file. |
| 91 | +- The schema is defined in the [`prisma/schema.prisma`](https://github.com/triggerdotdev/examples/tree/main/monorepos/turborepo-prisma-tasks-package/packages/database/prisma/schema.prisma) file. |
| 92 | + |
| 93 | +#### Tasks package |
| 94 | + |
| 95 | +<Note> |
| 96 | + |
| 97 | +to run `pnpm dlx trigger.dev@latest init` in a blank packages folder, you have to add a `package.json` file first, otherwise it will attempt to add Trigger.dev files in the root of your monorepo. |
| 98 | + |
| 99 | +</Note> |
| 100 | + |
| 101 | +- Trigger.dev is added as a package in [`/packages/tasks`](https://github.com/triggerdotdev/examples/tree/main/monorepos/turborepo-prisma-tasks-package/packages/tasks) and exported as `@repo/tasks` in the [`package.json`](https://github.com/triggerdotdev/examples/tree/main/monorepos/turborepo-prisma-tasks-package/packages/tasks/package.json) file. |
| 102 | +- The [`addNewUser.ts`](https://github.com/triggerdotdev/examples/tree/main/monorepos/turborepo-prisma-tasks-package/packages/tasks/src/trigger/addNewUser.ts) task adds a new user to the database. |
| 103 | +- The [`packages/tasks/src/index.ts`](https://github.com/triggerdotdev/examples/tree/main/monorepos/turborepo-prisma-tasks-package/packages/tasks/src/index.ts) file exports values and types from the Trigger.dev SDK, and is exported from the package via the [`package.json`](https://github.com/triggerdotdev/examples/tree/main/monorepos/turborepo-prisma-tasks-package/packages/tasks/package.json) file. |
| 104 | +- The [`packages/tasks/src/trigger/index.ts`](https://github.com/triggerdotdev/examples/tree/main/monorepos/turborepo-prisma-tasks-package/packages/tasks/src/trigger/index.ts) file exports the task from the package. Every task must be exported from the package like this. |
| 105 | +- The [`trigger.config.ts`](https://github.com/triggerdotdev/examples/tree/main/monorepos/turborepo-prisma-tasks-package/packages/tasks/trigger.config.ts) file configures the Trigger.dev project settings. This is where the Trigger.dev [Prisma build extension](https://trigger.dev/docs/config/extensions/prismaExtension) is added, which is required to use Prisma in the Trigger.dev task. |
| 106 | + |
| 107 | +<Info> |
| 108 | +You must include the version of Prisma you are using in the `trigger.config.ts` file, otherwise the Prisma build extension will not work. Learn more about our [Prisma build extension](/config/extensions/prismaExtension). |
| 109 | + |
| 110 | +</Info> |
| 111 | + |
| 112 | +#### The Next.js app `apps/web` |
| 113 | + |
| 114 | +- The app is a simple Next.js app using the App Router, that uses the `@repo/db` package to interact with the database and the `@repo/tasks` package to trigger the task. These are both added as dependencies in the [`package.json`](https://github.com/triggerdotdev/examples/tree/main/monorepos/turborepo-prisma-tasks-package/apps/web/package.json) file. |
| 115 | +- The task is triggered from a button click in the app in [`page.tsx`](https://github.com/triggerdotdev/examples/tree/main/monorepos/turborepo-prisma-tasks-package/apps/web/app/page.tsx), which uses a server action in [`/app/api/actions.ts`](https://github.com/triggerdotdev/examples/tree/main/monorepos/turborepo-prisma-tasks-package/apps/web/app/api/actions.ts) to trigger the task with an example payload. |
| 116 | + |
| 117 | +### Running the example |
| 118 | + |
| 119 | +To run this example, check out the full instructions [in the GitHub repo README file](https://github.com/triggerdotdev/examples/tree/main/monorepos/turborepo-prisma-tasks-package/README.md). |
| 120 | + |
| 121 | +## Example 2: Turborepo monorepo demo with a Prisma package and Trigger.dev installed in a Next.js app |
| 122 | + |
| 123 | +This example demonstrates how to use Trigger.dev and Prisma in a monorepo created with Turborepo. Prisma has been added as a package, and Trigger.dev has been installed in a Next.js app. The task is triggered by a button click in the app via a server action. |
| 124 | + |
| 125 | +### GitHub repo |
| 126 | + |
| 127 | +Fork the GitHub repo below to get started with this example project. |
| 128 | + |
| 129 | +<Card |
| 130 | + title="Check out the Turborepo monorepo demo with a Prisma package and Trigger.dev installed in a Next.js app" |
| 131 | + icon="GitHub" |
| 132 | + href="https://github.com/triggerdotdev/examples/tree/main/monorepos/turborepo-prisma-tasks-trigger" |
| 133 | +> |
| 134 | + Click here to view the full code for this project in our examples repository on GitHub. You can |
| 135 | + fork it and use it as a starting point for your own project. |
| 136 | +</Card> |
| 137 | + |
| 138 | +### Features |
| 139 | + |
| 140 | +- This monorepo has been created using the [Turborepo CLI](https://turbo.build/repo), following the official [Prisma and Turborepo docs](https://www.prisma.io/docs/guides/turborepo), and then adapted for use with Trigger.dev. |
| 141 | +- [pnpm](https://pnpm.io/) has been used as the package manager. |
| 142 | +- A database package (`@repo/db`) using [Prisma ORM](https://www.prisma.io/docs/orm/) is used to interact with the database. You can use any popular Postgres database supported by Prisma, e.g. [Supabase](https://supabase.com/), [Neon](https://neon.tech/), etc. |
| 143 | +- A [Next.js](https://nextjs.org/) example app (`apps/web`) to show how to trigger the task via a server action. |
| 144 | +- Trigger.dev initialized and an `addNewUser` task created in the `web` app. |
| 145 | + |
| 146 | +### Project structure |
| 147 | + |
| 148 | +Simplified project structure for this example: |
| 149 | + |
| 150 | +``` |
| 151 | +| |
| 152 | +| — apps/ |
| 153 | +| | — web/ # Next.js frontend application |
| 154 | +| | | — app/ # Next.js app router |
| 155 | +| | | | — api/ |
| 156 | +| | | | | — actions.ts # Server actions for triggering tasks |
| 157 | +| | | | — page.tsx # Main page with "Add new user" button |
| 158 | +| | | — src/ |
| 159 | +| | | | — trigger/ |
| 160 | +| | | | — addNewUser.ts # Task implementation for adding users |
| 161 | +| | | — trigger.config.ts # Trigger.dev configuration |
| 162 | +| | | — package.json # Dependencies including @repo/db |
| 163 | +| | |
| 164 | +| | — docs/ # Documentation app |
| 165 | +| | — app/ |
| 166 | +| | — page.tsx # Docs landing page |
| 167 | +| |
| 168 | +| — packages/ |
| 169 | +| | — database/ # Prisma database package (@repo/db) |
| 170 | +| | | — prisma/ |
| 171 | +| | | | — schema.prisma # Database schema definition |
| 172 | +| | |
| 173 | +| | — ui/ # UI components package |
| 174 | +| |
| 175 | +| — turbo.json # Turborepo configuration |
| 176 | +| — package.json # Root package.json with workspace config |
| 177 | +``` |
| 178 | + |
| 179 | +## Relevant files and code |
| 180 | + |
| 181 | +### Database package (`@repo/db`) |
| 182 | + |
| 183 | +- Located in [`/packages/database/`](https://github.com/triggerdotdev/examples/tree/main/monorepos/turborepo-prisma-tasks-trigger/packages/database/) and exported as `@repo/db` |
| 184 | +- Schema defined in [`schema.prisma`](https://github.com/triggerdotdev/examples/tree/main/monorepos/turborepo-prisma-tasks-trigger/packages/database/prisma/schema.prisma) |
| 185 | +- Provides database access to other packages and apps |
| 186 | + |
| 187 | +### Next.js app (`apps/web`) |
| 188 | + |
| 189 | +- Contains Trigger.dev configuration in [`trigger.config.ts`](https://github.com/triggerdotdev/examples/tree/main/monorepos/turborepo-prisma-tasks-trigger/apps/web/trigger.config.ts) |
| 190 | +- Trigger.dev tasks are defined in [`src/trigger/`](https://github.com/triggerdotdev/examples/tree/main/monorepos/turborepo-prisma-tasks-trigger/apps/web/src/trigger/) (e.g., [`addNewUser.ts`](https://github.com/triggerdotdev/examples/tree/main/monorepos/turborepo-prisma-tasks-trigger/apps/web/src/trigger/addNewUser.ts)) |
| 191 | +- Demonstrates triggering tasks via server actions in [`app/api/actions.ts`](https://github.com/triggerdotdev/examples/tree/main/monorepos/turborepo-prisma-tasks-trigger/apps/web/app/api/actions.ts) |
| 192 | + |
| 193 | +### Running the example |
| 194 | + |
| 195 | +To run this example, check out the full instructions [in the GitHub repo README file](https://github.com/triggerdotdev/examples/tree/main/monorepos/turborepo-prisma-tasks-trigger/README.md). |
0 commit comments