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.
- This monorepo has been created using the Turborepo CLI, following the official Prisma and Turborepo docs, and then adapted for use with Trigger.dev.
- pnpm has been used as the package manager.
- A tasks package (
@repo/tasks
) using Trigger.dev is used to create and execute tasks from an app inside the monorepo. - A database package (
@repo/db
) using Prisma ORM is used to interact with the database. You can use any popular Postgres database supported by Prisma, e.g. Supabase, Neon, etc. - A Next.js example app (
apps/web
) to show how to trigger the task via a server action.
- Prisma is added as a package in
/packages/database
and exported as@repo/db
in thepackage.json
file. - The schema is defined in the
prisma/schema.prisma
file.
Note: to run
pnpm dlx trigger.dev@latest init
in a blank packages folder, you have to add apackage.json
file first, otherwise it will attempt to add Trigger.dev files in the root of your monorepo.
- Trigger.dev is added as a package in
/packages/tasks
and exported as@repo/tasks
in thepackage.json
file. - The
addNewUser.ts
task adds a new user to the database. - The
packages/tasks/src/index.ts
file exports values and types from the Trigger.dev SDK, and is exported from the package via thepackage.json
file. - The
packages/tasks/src/trigger/index.ts
file exports the task from the package. Every task must be exported from the package like this. - The
trigger.config.ts
file configures the Trigger.dev project settings. This is where the Trigger.dev Prisma build extension is added, which is required to use Prisma in the Trigger.dev task.
- The app is a simple Next.js app 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 thepackage.json
file. - The task is triggered from a button click in the app in
page.tsx
, which uses a server action in/app/api/actions.ts
to trigger the task with an example payload.
-
After cloning the repository, install the dependencies in the root of the monorepo:
pnpm install
-
Create
.env
files inapps/web
,packages/database
andpackages/tasks
with the correct environment variables. Copy the structure from the.env.example
files and use the correct values for your database and Trigger.dev project. If you don't have a Trigger.dev project yet, you can create one at here. -
Set up the database and run migrations:
pnpm turbo db:generate # Generate Prisma client pnpm turbo db:migrate # Run migrations
-
Update the Trigger.dev project ref in the
trigger.config.ts
file. -
Start the development server for the Next.js app:
pnpm turbo run dev --filter=web
and in a separate terminal window, run the Trigger.dev
dev
command, in thepackages/tasks
folder:cd packages/tasks pnpm dlx trigger.dev@latest dev
Note: when running the dev command, you will get some warnings in the console: "▲ WARNING The condition "default" here will never be used as it comes after both "import" and "require" package.json". These warnings can be safely ignored, and won't affect the functionality of the app. They will be fixed in an upcoming release.
-
Access the application at http://localhost:3000, and test the functionality by clicking the "Add new user" button on the web app to trigger the task
-
Go to the Trigger.dev dashboard to see the task being executed
- Trigger.dev docs - learn about Trigger.dev and its features.
- Turborepo docs - learn about Turborepo and its features.
- Prisma docs - learn about Prisma and its features.