Skip to content

Latest commit

 

History

History
75 lines (50 loc) · 4.95 KB

File metadata and controls

75 lines (50 loc) · 4.95 KB

Turborepo monorepo demo with Trigger.dev and Prisma packages

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.

Overview & features

  • 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.

Relevant files and code

Database package

Tasks package

Note: 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.

A Next.js app apps/web

  • 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 the package.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.

How to use

  1. After cloning the repository, install the dependencies in the root of the monorepo:

    pnpm install
  2. Create .env files in apps/web, packages/database and packages/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.

  3. Set up the database and run migrations:

    pnpm turbo db:generate   # Generate Prisma client
    pnpm turbo db:migrate    # Run migrations
  4. Update the Trigger.dev project ref in the trigger.config.ts file.

  5. 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 the packages/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.

  6. 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

  7. Go to the Trigger.dev dashboard to see the task being executed

Learn more