|
| 1 | +--- |
| 2 | +title: "Prisma setup guide" |
| 3 | +sidebarTitle: "Prisma" |
| 4 | +description: "This guide will show you how to setup Prisma with Trigger.dev" |
| 5 | +icon: "Triangle" |
| 6 | +--- |
| 7 | + |
| 8 | +import Prerequisites from "/snippets/framework-prerequisites.mdx"; |
| 9 | +import CliInitStep from "/snippets/step-cli-init.mdx"; |
| 10 | +import CliDevStep from "/snippets/step-cli-dev.mdx"; |
| 11 | +import CliRunTestStep from "/snippets/step-run-test.mdx"; |
| 12 | +import CliViewRunStep from "/snippets/step-view-run.mdx"; |
| 13 | +import UsefulNextSteps from "/snippets/useful-next-steps.mdx"; |
| 14 | + |
| 15 | +## Overview |
| 16 | + |
| 17 | +This guide will show you how to set up Prisma with Trigger.dev, test and view an example task run. |
| 18 | + |
| 19 | +## Prerequisites |
| 20 | + |
| 21 | +- An existing Node.js project with a `package.json` file |
| 22 | +- Ensure TypeScript is installed |
| 23 | +- A [PostgreSQL](https://www.postgresql.org/) database server running locally, or accessible via a connection string |
| 24 | +- Prisma ORM [installed and initialized](https://www.prisma.io/docs/getting-started/quickstart) in your project |
| 25 | +- A `DATABASE_URL` environment variable set in your `.env` file, pointing to your PostgreSQL database (e.g. `postgresql://user:password@localhost:5432/dbname`) |
| 26 | + |
| 27 | +## Initial setup |
| 28 | + |
| 29 | +<Steps> |
| 30 | + <CliInitStep /> |
| 31 | + <CliDevStep /> |
| 32 | + <CliRunTestStep /> |
| 33 | + <CliViewRunStep /> |
| 34 | +</Steps> |
| 35 | + |
| 36 | +## Creating a task using Prisma and deploying it to production |
| 37 | + |
| 38 | +<Steps> |
| 39 | +<Step title="Writing the Prisma task"> |
| 40 | + |
| 41 | +First, create a new task file in your `trigger` folder. |
| 42 | + |
| 43 | +This is a simple task that will add a new user to the database. |
| 44 | + |
| 45 | +<Note> |
| 46 | + For this task to work correctly, you will need to have a `user` model in your Prisma schema with |
| 47 | + an `id` field, a `name` field, and an `email` field. |
| 48 | +</Note> |
| 49 | + |
| 50 | +```ts /trigger/prisma-add-new-user.ts |
| 51 | +import { PrismaClient } from "@prisma/client"; |
| 52 | +import { task } from "@trigger.dev/sdk/v3"; |
| 53 | + |
| 54 | +// Initialize Prisma client |
| 55 | +const prisma = new PrismaClient(); |
| 56 | + |
| 57 | +export const addNewUser = task({ |
| 58 | + id: "prisma-add-new-user", |
| 59 | + run: async (payload: { name: string; email: string; id: number }) => { |
| 60 | + const { name, email, id } = payload; |
| 61 | + |
| 62 | + // This will create a new user in the database |
| 63 | + const user = await prisma.user.create({ |
| 64 | + data: { |
| 65 | + name: name, |
| 66 | + email: email, |
| 67 | + id: id, |
| 68 | + }, |
| 69 | + }); |
| 70 | + |
| 71 | + return { |
| 72 | + message: `New user added successfully: ${user.id}`, |
| 73 | + }; |
| 74 | + }, |
| 75 | +}); |
| 76 | +``` |
| 77 | + |
| 78 | +</Step> |
| 79 | +<Step title="Configuring the build extension"> |
| 80 | + |
| 81 | +Next, configure the Prisma [build extension](https://trigger.dev/docs/config/extensions/overview) in the `trigger.config.js` file to include the Prisma client in the build. |
| 82 | + |
| 83 | +This will ensure that the Prisma client is available when the task runs. |
| 84 | + |
| 85 | +For a full list of options available in the Prisma build extension, see the [Prisma build extension documentation](https://trigger.dev/docs/config/config-file#prisma). |
| 86 | + |
| 87 | +```js /trigger.config.js |
| 88 | +export default defineConfig({ |
| 89 | + project: "<project ref>", // Your project reference |
| 90 | + // Your other config settings... |
| 91 | + build: { |
| 92 | + extensions: [ |
| 93 | + prismaExtension({ |
| 94 | + version: "5.20.0", // optional, we'll automatically detect the version if not provided |
| 95 | + // update this to the path of your Prisma schema file |
| 96 | + schema: "prisma/schema.prisma", |
| 97 | + }), |
| 98 | + ], |
| 99 | + }, |
| 100 | +}); |
| 101 | +``` |
| 102 | + |
| 103 | +<Note> |
| 104 | + [Build extensions](/config/config-file#extensions) allow you to hook into the build system and |
| 105 | + customize the build process or the resulting bundle and container image (in the case of |
| 106 | + deploying). You can use pre-built extensions or create your own. |
| 107 | +</Note> |
| 108 | + |
| 109 | +</Step> |
| 110 | + |
| 111 | +<Step title="Optional: adding Prisma instrumentation"> |
| 112 | + |
| 113 | +We use OpenTelemetry to [instrument](https://trigger.dev/docs/config/config-file#instrumentations) our tasks and collect telemetry data. |
| 114 | + |
| 115 | +If you want to automatically log all Prisma queries and mutations, you can use the Prisma instrumentation extension. |
| 116 | + |
| 117 | +```js /trigger.config.js |
| 118 | +import { defineConfig } from "@trigger.dev/sdk/v3"; |
| 119 | +import { PrismaInstrumentation } from "@prisma/instrumentation"; |
| 120 | +import { OpenAIInstrumentation } from "@traceloop/instrumentation-openai"; |
| 121 | + |
| 122 | +export default defineConfig({ |
| 123 | + //..other stuff |
| 124 | + instrumentations: [new PrismaInstrumentation(), new OpenAIInstrumentation()], |
| 125 | +}); |
| 126 | +``` |
| 127 | + |
| 128 | +This provides much more detailed information about your tasks with minimal effort. |
| 129 | + |
| 130 | +</Step> |
| 131 | +<Step title="Deploying your task"> |
| 132 | +With the build extension and task configured, you can now deploy your task using the Trigger.dev CLI. |
| 133 | + |
| 134 | + <CodeGroup> |
| 135 | + |
| 136 | + ```bash npm |
| 137 | + npx trigger.dev@latest deploy |
| 138 | + ``` |
| 139 | + |
| 140 | + ```bash pnpm |
| 141 | + pnpm dlx trigger.dev@latest deploy |
| 142 | + ``` |
| 143 | + |
| 144 | + ```bash yarn |
| 145 | + yarn dlx trigger.dev@latest deploy |
| 146 | + ``` |
| 147 | + |
| 148 | + </CodeGroup> |
| 149 | + |
| 150 | + </Step> |
| 151 | + |
| 152 | + <Step title="Adding your DATABASE_URL environment variable to Trigger.dev"> |
| 153 | + |
| 154 | +In the sidebar select the "Environment Variables" page, then press the "New environment variable" |
| 155 | +button.  |
| 156 | + |
| 157 | +You can add values for your local dev environment, staging and prod. in this case we will add the `DATABASE_URL` for the production environment. |
| 158 | + |
| 159 | + |
| 161 | + |
| 162 | + </Step> |
| 163 | + |
| 164 | + <Step title="Running your task"> |
| 165 | + |
| 166 | + To test this task, go to the 'test' page in the Trigger.dev dashboard and run the task with the following payload: |
| 167 | + |
| 168 | +```json |
| 169 | +{ |
| 170 | + "name": "John Doe", |
| 171 | + "email": "john@doe.test", |
| 172 | + "id": 12345 |
| 173 | +} |
| 174 | +``` |
| 175 | + |
| 176 | +Congratulations! You should now see a new completed run, and a new user with the credentials you provided should be added to your database. |
| 177 | + |
| 178 | +</Step> |
| 179 | + |
| 180 | +</Steps> |
| 181 | + |
| 182 | +<UsefulNextSteps /> |
0 commit comments