Skip to content

Latest commit

 

History

History
154 lines (110 loc) · 4.59 KB

File metadata and controls

154 lines (110 loc) · 4.59 KB
title Drizzle setup guide
sidebarTitle Drizzle setup guide
description This guide will show you how to set up Drizzle ORM with Trigger.dev
icon D

import Prerequisites from "/snippets/framework-prerequisites.mdx"; import CliInitStep from "/snippets/step-cli-init.mdx"; import CliDevStep from "/snippets/step-cli-dev.mdx"; import CliRunTestStep from "/snippets/step-run-test.mdx"; import CliViewRunStep from "/snippets/step-view-run.mdx"; import UsefulNextSteps from "/snippets/useful-next-steps.mdx";

Overview

This guide will show you how to set up Drizzle ORM with Trigger.dev, test and view an example task run.

Prerequisites

  • An existing Node.js project with a package.json file
  • Ensure TypeScript is installed
  • A PostgreSQL database server running locally, or accessible via a connection string
  • Drizzle ORM installed and initialized in your project
  • A DATABASE_URL environment variable set in your .env file, pointing to your PostgreSQL database (e.g. postgresql://user:password@localhost:5432/dbname)
If your Postgres lives in a private AWS VPC (e.g. RDS without a public endpoint), connect it via [Private networking](/private-networking/overview) instead of opening it to the public internet (enterprise plans).

Initial setup (optional)

Follow these steps if you don't already have Trigger.dev set up in your project.

Creating a task using Drizzle and deploying it to production

First, create a new task file in your trigger folder.

This is a simple task that will add a new user to your database, we will call it drizzle-add-new-user.

For this task to work correctly, you will need to have a `users` table schema defined with Drizzle that includes `name`, `age` and `email` fields.
import { eq } from "drizzle-orm";
import { task } from "@trigger.dev/sdk";
import { users } from "src/db/schema";
import { drizzle } from "drizzle-orm/node-postgres";

// Initialize Drizzle client
const db = drizzle(process.env.DATABASE_URL!);

export const addNewUser = task({
  id: "drizzle-add-new-user",
  run: async (payload: typeof users.$inferInsert) => {
    // Create new user
    const [user] = await db.insert(users).values(payload).returning();

    return {
      createdUser: user,
      message: "User created and updated successfully",
    };
  },
});

Next, in your trigger.config.js file, add pg to the externals array. pg is a non-blocking PostgreSQL client for Node.js.

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.

import { defineConfig } from "@trigger.dev/sdk";

export default defineConfig({
  project: "<project ref>", // Your project reference
  // Your other config settings...
  build: {
    externals: ["pg"],
  },
});
Once the build configuration is added, you can now deploy your task using the Trigger.dev CLI.
```bash npm
npx trigger.dev@latest deploy
```

```bash pnpm
pnpm dlx trigger.dev@latest deploy
```

```bash yarn
yarn dlx trigger.dev@latest deploy
```

</CodeGroup>

</Step>

In your Trigger.dev dashboard sidebar click "Environment Variables" , and then the "New environment variable" button .

Environment variables page

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.

Environment variables page

To test this task, go to the 'test' page in the Trigger.dev dashboard and run the task with the following payload:
{
  "name": "<a-name>", // e.g. "John Doe"
  "age": "<an-age>", // e.g. 25
  "email": "<an-email>" // e.g. "john@doe.test"
}

Congratulations! You should now see a new completed run, and a new user with the credentials you provided should be added to your database.