|
| 1 | +--- |
| 2 | +title: "Drizzle setup guide" |
| 3 | +sidebarTitle: "Drizzle setup guide" |
| 4 | +description: "This guide will show you how to set up Drizzle ORM with Trigger.dev" |
| 5 | +icon: "D" |
| 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 [Drizzle ORM](https://orm.drizzle.team/) 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 | +- Drizzle ORM [installed and initialized](https://orm.drizzle.team/docs/get-started) 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 (optional) |
| 28 | + |
| 29 | +Follow these steps if you don't already have Trigger.dev set up in your project. |
| 30 | + |
| 31 | +<Steps> |
| 32 | + <CliInitStep /> |
| 33 | + <CliDevStep /> |
| 34 | + <CliRunTestStep /> |
| 35 | + <CliViewRunStep /> |
| 36 | +</Steps> |
| 37 | + |
| 38 | +## Creating a task using Drizzle and deploying it to production |
| 39 | + |
| 40 | +<Steps> |
| 41 | +<Step title="The task using Drizzle"> |
| 42 | + |
| 43 | +First, create a new task file in your `trigger` folder. |
| 44 | + |
| 45 | +This is a simple task that will add a new user to your database, we will call it `drizzle-add-new-user`. |
| 46 | + |
| 47 | +<Note> |
| 48 | + For this task to work correctly, you will need to have a `users` table schema defined with Drizzle |
| 49 | + that includes `name`, `age` and `email` fields. |
| 50 | +</Note> |
| 51 | + |
| 52 | +```ts /trigger/drizzle-add-new-user.ts |
| 53 | +import { eq } from "drizzle-orm"; |
| 54 | +import { task } from "@trigger.dev/sdk/v3"; |
| 55 | +import { users } from "src/db/schema"; |
| 56 | +import { drizzle } from "drizzle-orm/node-postgres"; |
| 57 | + |
| 58 | +// Initialize Drizzle client |
| 59 | +const db = drizzle(process.env.DATABASE_URL!); |
| 60 | + |
| 61 | +export const addNewUser = task({ |
| 62 | + id: "drizzle-add-new-user", |
| 63 | + run: async (payload: typeof users.$inferInsert) => { |
| 64 | + // Create new user |
| 65 | + const [user] = await db.insert(users).values(payload).returning(); |
| 66 | + |
| 67 | + return { |
| 68 | + createdUser: user, |
| 69 | + message: "User created and updated successfully", |
| 70 | + }; |
| 71 | + }, |
| 72 | +}); |
| 73 | +``` |
| 74 | + |
| 75 | +</Step> |
| 76 | +<Step title="Configuring the build"> |
| 77 | + |
| 78 | +Next, in your `trigger.config.js` file, add `pg` to the `externals` array. `pg` is a non-blocking PostgreSQL client for Node.js. |
| 79 | + |
| 80 | +It is marked as an external to ensure that it is not bundled into the task's bundle, and instead will be installed and loaded from `node_modules` at runtime. |
| 81 | + |
| 82 | +```js /trigger.config.js |
| 83 | +import { defineConfig } from "@trigger.dev/sdk/v3"; |
| 84 | + |
| 85 | +export default defineConfig({ |
| 86 | + project: "<project ref>", // Your project reference |
| 87 | + // Your other config settings... |
| 88 | + build: { |
| 89 | + externals: ["pg"], |
| 90 | + }, |
| 91 | +}); |
| 92 | +``` |
| 93 | + |
| 94 | +</Step> |
| 95 | + |
| 96 | +<Step title="Deploying your task"> |
| 97 | +Once the build configuration is added, you can now deploy your task using the Trigger.dev CLI. |
| 98 | + |
| 99 | + <CodeGroup> |
| 100 | + |
| 101 | + ```bash npm |
| 102 | + npx trigger.dev@latest deploy |
| 103 | + ``` |
| 104 | + |
| 105 | + ```bash pnpm |
| 106 | + pnpm dlx trigger.dev@latest deploy |
| 107 | + ``` |
| 108 | + |
| 109 | + ```bash yarn |
| 110 | + yarn dlx trigger.dev@latest deploy |
| 111 | + ``` |
| 112 | + |
| 113 | + </CodeGroup> |
| 114 | + |
| 115 | + </Step> |
| 116 | + |
| 117 | +<Step title="Adding your DATABASE_URL environment variable to Trigger.dev"> |
| 118 | + |
| 119 | +In your Trigger.dev dashboard sidebar click "Environment Variables" <Icon icon="circle-1" iconType="solid" size={20} color="A8FF53" />, and then the "New environment variable" button <Icon icon="circle-2" iconType="solid" size={20} color="A8FF53" />. |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | +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. |
| 124 | + |
| 125 | + |
| 127 | + |
| 128 | +</Step> |
| 129 | + |
| 130 | +<Step title="Running your task"> |
| 131 | + |
| 132 | + To test this task, go to the 'test' page in the Trigger.dev dashboard and run the task with the following payload: |
| 133 | + |
| 134 | +```json |
| 135 | +{ |
| 136 | + "name": "<a-name>", // e.g. "John Doe" |
| 137 | + "age": "<an-age>", // e.g. 25 |
| 138 | + "email": "<an-email>" // e.g. "john@doe.test" |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +Congratulations! You should now see a new completed run, and a new user with the credentials you provided should be added to your database. |
| 143 | + |
| 144 | +</Step> |
| 145 | + |
| 146 | +</Steps> |
| 147 | + |
| 148 | +<UsefulNextSteps /> |
0 commit comments