You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: "Triggering tasks from Supabase Database Webhooks"
3
3
sidebarTitle: "Database webhooks"
4
-
description: "This guide shows you how to trigger a task when a row is added to a table, using a Supabase Database Webhook and Edge Function."
4
+
description: "This guide shows you how to trigger a task when a row is added to a table in a Supabase database, using a Database Webhook and Edge Function."
Generate a transcription from a video URL using [Supabase](https://supabase.com), [FFmpeg](https://www.ffmpeg.org/) and [Deepgram](https://deepgram.com) and Trigger.dev.
19
+
This workflow generates a transcription from a video URL and updates a table in a database, using [Supabase](https://supabase.com), [FFmpeg](https://www.ffmpeg.org/) and [Deepgram](https://deepgram.com) and Trigger.dev.
20
20
21
-
In this walk-through guide you will learn how to set up and deploy a Supabase Edge Function that is triggered by an `insert` row action in a table via a Database Webhook.
21
+
You will learn how to set up and deploy a Supabase Edge Function that is triggered by an `insert` row action in a table via a Database Webhook.
22
22
23
23
The Edge Function triggers a deployed Trigger.dev task which takes a payload from the new inserted data from the table. This is then processed using FFmpeg and Deepgram. The resulting string is then `updated` back into the original table row in Supabase.
24
24
@@ -46,165 +46,6 @@ The Edge Function triggers a deployed Trigger.dev task which takes a payload fro
46
46
<CliInitStep />
47
47
</Steps>
48
48
49
-
## Create and deploy the video processing Trigger.dev task
50
-
51
-
Before setting up your Edge Function and Database Webhook, you'll need to create your Trigger.dev task. This can be tested independently from the rest of the workflow.
52
-
53
-
Create a new task file in your `/trigger` folder (the same place your 'Hello World' task is). Call it `videoProcessAndUpdate.ts`.
54
-
55
-
This task with take a video from a public video url, extract the audio using FFmpeg and transcribe the audio using Deepgram.
56
-
57
-
<Note>We will add the Supabase `update` step further on in the tutorial.</Note>
[Build extensions](/config/config-file#extensions) allow you to hook into the build system and
164
-
customize the build process or the resulting bundle and container image (in the case of
165
-
deploying). You can use pre-built extensions or create your own.
166
-
</Note>
167
-
168
-
You'll also need to add `@trigger.dev/build` to your `package.json` file under `devDependencies` if you don't already have it there.
169
-
170
-
### Adding the Deepgram environment variable
171
-
172
-
You will need to add your `DEEPGRAM_SECRET_KEY` as an environment variable in your Trigger.dev project. You can do this in the Trigger.dev dashboard under the 'environment variables' tab.
### Create a new Edge Function using the Supabase CLI
223
64
224
-
Now create an Edge Function using the Supabase CLI. Call it `video-processing-handler`. This will trigger your task using the data received from the Database Webhook.
65
+
Now create an Edge Function using the Supabase CLI. Call it `video-processing-handler`. This function will be triggered by the Database Webhook.
225
66
226
67
```bash
227
68
supabase functions new video-processing-handler
@@ -232,30 +73,15 @@ Replace the `video-processing-handler` placeholder code with the following code:
232
73
```ts functions/video-processing-handler/index.ts
233
74
// Setup type definitions for built-in Supabase Runtime APIs
// Import the Trigger.dev SDK - replace "<your-sdk-version>" with the version of the SDK you are using, e.g. "3.0.0". You can find this in your package.json file.
Tasks in the `trigger` folder use Node, so they must stay in there or they will not run,
255
-
especially if you are using a different runtime like Deno. Also do not add "`npm:`" to imports
256
-
inside your task files, for the same reason.
257
-
</Note>
258
-
259
85
### Deploy the Edge Function
260
86
261
87
Now deploy your new Edge Function with the following command:
@@ -321,16 +147,28 @@ and select the Edge Function you have created: `video-processing-handler`.{" "}
321
147
322
148
Your Database Webhook is now ready to use.
323
149
324
-
## Triggering the task
150
+
## Creating and deploying the Trigger.dev task and updating the edge function
151
+
152
+
### Creating and deploying the Trigger.dev task
325
153
326
-
### Adding the logic to update the table row
154
+
Create a new task file in your `/trigger` folder (the same place your 'Hello World' task was created). Call it `videoProcessAndUpdate.ts`.
327
155
328
-
First, you must go back to your `videoProcessAndUpdate` task code from earlier and add in the Supabase logic. This will:
156
+
This task with take a video from a public video url, extract the audio using FFmpeg and transcribe the audio using Deepgram. The transcription summary will then be added back to the original row in the `video_transcriptions` table in Supabase.
329
157
330
-
- Create a Supabase client (you must update your environment variables in order for this to work)
331
-
- Create a function which updates the table row with the exact match of the `video_url` payload with the new generated transcription.
158
+
#### Generate the Database type definitions for your Supabase table
159
+
160
+
First, you need to [generate the type definitions](https://supabase.com/docs/guides/api/rest/generating-types) for your Supabase table using the Supabase CLI. This will allow you to use TypeScript to interact with your table.
161
+
162
+
```bash
163
+
npx supabase generate types --project-url <your-project-url> --project-key <your-anon-key> --schema public
164
+
```
165
+
166
+
<Note> Replace `<your-project-url>` with your Supabase project URL and `<your-anon-key>` with your `anon``public` API key. </Note>
167
+
168
+
#### Create the transcription task
332
169
333
170
```ts /trigger/videoProcessAndUpdate.ts
171
+
// Install any missing dependencies below using npm or yarn
project: "<project ref>", // Replace with your project ref
284
+
// Your other config settings...
285
+
build: {
286
+
extensions: [ffmpeg()],
287
+
},
288
+
});
289
+
```
290
+
291
+
<Note>
292
+
[Build extensions](/config/config-file#extensions) allow you to hook into the build system and
293
+
customize the build process or the resulting bundle and container image (in the case of
294
+
deploying). You can use pre-built extensions or create your own.
295
+
</Note>
296
+
297
+
<Note>
298
+
You'll also need to add `@trigger.dev/build` to your `package.json` file under `devDependencies`
299
+
if you don't already have it there.
300
+
</Note>
301
+
302
+
### Add your Deepgram and Supabase environment variables to your Trigger.dev project
303
+
304
+
You will need to add your `DEEPGRAM_SECRET_KEY`, `SUPABASE_PROJECT_URL` and `SUPABASE_SERVICE_ROLE_KEY` as environment variables in your Trigger.dev project. This can be found in the 'Environment Variables' page in your Trigger.dev project dashboard.
You can now deploy your task using the following command:
311
+
312
+
<CodeGroup>
313
+
314
+
```bash npm
315
+
npx trigger.dev@latest deploy
316
+
```
317
+
318
+
```bash pnpm
319
+
pnpm dlx trigger.dev@latest deploy
320
+
```
321
+
322
+
```bash yarn
323
+
yarn dlx trigger.dev@latest deploy
324
+
```
325
+
326
+
</CodeGroup>
327
+
328
+
#### Update the Edge Function to trigger the task
329
+
330
+
Finally, we need to replace your `video-processing-handler` Edge Function code with the below to trigger the `videoProcessAndUpdate` task when it is called.
331
+
332
+
```ts functions/video-processing-handler/index.ts
333
+
// Setup type definitions for built-in Supabase Runtime APIs
// Import the Trigger.dev SDK - replace "<your-sdk-version>" with the version of the SDK you are using, e.g. "3.0.0". You can find this in your package.json file.
Your `video-processing-handler` Edge Function is now set up to trigger the `videoProcessAndUpdate` task every time a new row is inserted into your `video_transcriptions` table.
0 commit comments