Supabase database operations using Trigger.dev
These examples demonstrate how to run basic CRUD operations on a table in a Supabase database using Trigger.dev.
Add a new user to a table in a Supabase database
This is a basic task which inserts a new row into a table from a Trigger.dev task.
Key features
- Shows how to set up a Supabase client using the
@supabase/supabase-js
library - Shows how to add a new row to a table using
insert
Prerequisites
- A Supabase account and a project set up
- In your Supabase project, create a table called
user_subscriptions
. - In your
user_subscriptions
table, create a new column:user_id
, with the data type:text
Task code
This task uses your service role secret key to bypass Row Level Security. There are different ways of configuring your RLS policies, so always make sure you have the correct permissions set up for your project.
Testing your task
To test this task in the Trigger.dev dashboard, you can use the following payload:
If the task completes successfully, you will see a new row in your user_subscriptions
table with the user_id
set to user_12345
.
Update a user’s subscription on a table in a Supabase database
This task shows how to update a user’s subscription on a table. It checks if the user already has a subscription and either inserts a new row or updates an existing row with the new plan.
This type of task is useful for managing user subscriptions, updating user details, or performing other operations you might need to do on a database table.
Key features
- Shows how to set up a Supabase client using the
@supabase/supabase-js
library - Adds a new row to the table if the user doesn’t exist using
insert
- Checks if the user already has a plan, and if they do updates the existing row using
update
- Demonstrates how to use AbortTaskRunError to stop the task run without retrying if an invalid plan type is provided
Prerequisites
-
A Supabase account and a project set up
-
In your Supabase project, create a table called
user_subscriptions
(if you haven’t already) -
In your
user_subscriptions
table, create these columns (if they don’t already exist):user_id
, with the data type:text
plan
, with the data type:text
updated_at
, with the data type:timestamptz
Task code
This task uses your service role secret key to bypass Row Level Security. There are different ways of configuring your RLS policies, so always make sure you have the correct permissions set up for your project.
Testing your task
To test this task in the Trigger.dev dashboard, you can use the following payload:
If the task completes successfully, you will see a new row in your user_subscriptions
table with the user_id
set to user_12345
, the plan
set to pro
, and the updated_at
timestamp updated to the current time.
Learn more about Supabase and Trigger.dev
Full walkthrough guides from development to deployment
Edge function hello world guide
Learn how to trigger a task from a Supabase edge function when a URL is visited.
Database webhooks guide
Learn how to trigger a task from a Supabase edge function when an event occurs in your database.
Task examples with code you can copy and paste
Was this page helpful?