This guide will show you how to trigger tasks from database changes using Sequin
Sequin allows you to trigger tasks from database changes. Sequin captures every insert, update, and delete on a table and then ensures a task is triggered for each change.
Often, task runs coincide with database changes. For instance, you might want to use a Trigger.dev task to generate an embedding for each post in your database:
In this guide, you’ll learn how to use Sequin to trigger Trigger.dev tasks from database changes.
You are about to create a regular Trigger.dev task that you will execute when ever a post is inserted or updated in your database. Sequin will detect all the changes on the posts
table and then send the payload of the post to an API endpoint that will call tasks.trigger()
to create the embedding and update the database.
As long as you create an HTTP endpoint that Sequin can deliver webhooks to, you can use any web framework or edge function (e.g. Supabase Edge Functions, Vercel Functions, Cloudflare Workers, etc.) to invoke your Trigger.dev task. In this guide, we’ll show you how to setup Trigger.dev tasks using Next.js API Routes.
You’ll need the following to follow this guide:
If you don’t have one already, follow Trigger.dev’s Next.js setup guide to setup your project. You can return to this guide when you’re ready to write your first Trigger.dev task.
posts
table.Start by creating a new Trigger.dev task that takes in a Sequin change event as a payload, creates an embedding, and then inserts the embedding into the database:
In your src/trigger/tasks
directory, create a new file called create-embedding-for-post.ts
and add the following code:
This task takes in a Sequin record event, creates an embedding, and then upserts the embedding into a post_embeddings
table.
Register the create-embedding-for-post
task to your Trigger.dev cloud project by running the following command:
In the Trigger.dev dashboard, you should now see the create-embedding-for-post
task:
You’ve successfully created a Trigger.dev task that will create an embedding for each post in your database. In the next step, you’ll create an API endpoint that Sequin can deliver records to.
You’ll now create an API endpoint that will receive posts from Sequin and then trigger the create-embedding-for-post
task.
This guide covers how to setup an API endpoint using the Next.js App Router. You can find examples for Next.js Server Actions and Pages Router in the Trigger.dev documentation.
Add a route handler by creating a new route.ts
file in a /app/api/create-embedding-for-post
directory:
This route handler will receive records from Sequin, parse them, and then trigger the create-embedding-for-post
task.
You’ll need to set four secret keys in a .env.local
file:
The SEQUIN_WEBHOOK_SECRET
ensures that only Sequin can access your API endpoint.
The TRIGGER_SECRET_KEY
is used to authenticate requests to Trigger.dev and can be found in the API keys tab of the Trigger.dev dashboard.
The OPENAI_API_KEY
and DATABASE_URL
are used to create an embedding using OpenAI and connect to your database. Be sure to add these as environment variables in Trigger.dev as well.
You’ve successfully created an API endpoint that can receive record payloads from Sequin and trigger a Trigger.dev task. In the next step, you’ll setup Sequin to trigger the endpoint.
You’ll now configure Sequin to send every row in your posts
table to your Trigger.dev task.
If you need to connect to a local dev database, flip the use localhost switch and follow the instructions to create a tunnel using the Sequin CLI.
Sequin will connect to your database and ensure that it’s configured properly.
If you need step-by-step connection instructions to connect Sequin to your database, check out our quickstart guide.
Now, create a tunnel to your local endpoint so Sequin can deliver change payloads to your local API:
local_endpoint
) and flip the Use localhost switch. Follow the instructions in the Sequin console to install the Sequin CLI, then run:Authorization
and the value to Bearer SEQUIN_WEBHOOK_SECRET
.Create a push consumer that will capture posts from your database and deliver them to your local endpoint:
posts
table (i.e public.posts
).You can also use changes for this particular use case, but rows comes with some nice replay and backfill features.
updated_at
and start at the beginning of the table. We won’t apply any filters:posts_push_consumer
) and in the HTTP Endpoint section select the local_endpoint
you created above. Add the exact API route you created in the previous step (i.e. /api/create-embedding-for-post
):npm run dev
npx trigger.dev@latest dev
sequin tunnel --ports=3001:local_endpoint
In the Sequin console, navigate to the Trace tab and confirm that Sequin delivered the event to your local endpoint:
In your local terminal, you should see a 200
response in your Next.js app:
Finally, in the Trigger.dev dashboard, navigate to the Runs page and confirm that the task run completed successfully:
Every time a post is created or updated, Sequin will deliver the row payload to your API endpoint
and Trigger.dev will run the create-embedding-for-post
task.
With Sequin and Trigger.dev, every post in your database will now have an embedding. This is a simple example of how you can trigger long-running tasks on database changes.
From here, add error handling and deploy to production: