Skip to content

Commit fc60947

Browse files
authored
Supabase database webhook example upgrade (#1386)
* Added overview for guides and examples section and split them all out * New supabase guide wip * Updated images and improved docs * Trimmed the supabase prereqs * Supabase guide wip * more updates * Replaced old database webhook guide * Created one intro page and removed snippets * Updated guide sidebar titles * Code updates * More improvements * Updates and added images * Compressed image * Updated guides descriptions and edge function basic * Removed bold * Updated redirects * Fixed broken links * Updated intro
1 parent 07f82ea commit fc60947

18 files changed

+501
-341
lines changed

docs/guides/frameworks/introduction.mdx

Lines changed: 0 additions & 20 deletions
This file was deleted.

docs/guides/frameworks/prisma.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "Prisma setup guide"
3-
sidebarTitle: "Prisma"
3+
sidebarTitle: "Prisma setup guide"
44
description: "This guide will show you how to setup Prisma with Trigger.dev"
55
icon: "Triangle"
66
---

docs/guides/frameworks/sequin.mdx

Lines changed: 118 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "Sequin database triggers"
3-
sidebarTitle: "Sequin"
3+
sidebarTitle: "Sequin database triggers"
44
description: "This guide will show you how to trigger tasks from database changes using Sequin"
55
icon: "database"
66
---
@@ -22,9 +22,11 @@ As long as you create an HTTP endpoint that Sequin can deliver webhooks to, you
2222
You'll need the following to follow this guide:
2323

2424
- A Next.js project with [Trigger.dev](https://trigger.dev) installed
25-
<Info>
26-
If you don't have one already, follow [Trigger.dev's Next.js setup guide](/guides/frameworks/nextjs) to setup your project. You can return to this guide when you're ready to write your first Trigger.dev task.
27-
</Info>
25+
<Info>
26+
If you don't have one already, follow [Trigger.dev's Next.js setup
27+
guide](/guides/frameworks/nextjs) to setup your project. You can return to this guide when
28+
you're ready to write your first Trigger.dev task.
29+
</Info>
2830
- A [Sequin](https://console.sequinstream.com/register) account
2931
- A Postgres database (Sequin works with any Postgres database version 12 and up) with a `posts` table.
3032

@@ -42,36 +44,36 @@ Start by creating a new Trigger.dev task that takes in a Sequin change event as
4244
import { OpenAI } from "openai";
4345
import { upsertEmbedding } from "../util";
4446

45-
const openai = new OpenAI({
46-
apiKey: process.env.OPENAI_API_KEY,
47-
});
48-
49-
export const createEmbeddingForPost = task({
50-
id: "create-embedding-for-post",
51-
run: async (payload: {
52-
record: {
53-
id: number;
54-
title: string;
55-
body: string;
56-
author: string;
57-
createdAt: string;
58-
embedding: string | null;
59-
},
60-
metadata: {
61-
table_schema: string,
62-
table_name: string,
63-
consumer: {
64-
id: string;
65-
name: string;
66-
};
67-
};
68-
}) => {
69-
// Create an embedding using the title and body of payload.record
70-
const content = `${payload.record.title}\n\n${payload.record.body}`;
71-
const embedding = (await openai.embeddings.create({
72-
model: "text-embedding-ada-002",
73-
input: content,
74-
})).data[0].embedding;
47+
const openai = new OpenAI({
48+
apiKey: process.env.OPENAI_API_KEY,
49+
});
50+
51+
export const createEmbeddingForPost = task({
52+
id: "create-embedding-for-post",
53+
run: async (payload: {
54+
record: {
55+
id: number;
56+
title: string;
57+
body: string;
58+
author: string;
59+
createdAt: string;
60+
embedding: string | null;
61+
},
62+
metadata: {
63+
table_schema: string,
64+
table_name: string,
65+
consumer: {
66+
id: string;
67+
name: string;
68+
};
69+
};
70+
}) => {
71+
// Create an embedding using the title and body of payload.record
72+
const content = `${payload.record.title}\n\n${payload.record.body}`;
73+
const embedding = (await openai.embeddings.create({
74+
model: "text-embedding-ada-002",
75+
input: content,
76+
})).data[0].embedding;
7577

7678
// Upsert the embedding in the database. See utils.ts for the implementation -> ->
7779
await upsertEmbedding(embedding, payload.record.id);
@@ -82,51 +84,55 @@ Start by creating a new Trigger.dev task that takes in a Sequin change event as
8284
embedding: JSON.stringify(embedding),
8385
};
8486
}
87+
88+
});
89+
90+
````
91+
92+
```ts utils.ts
93+
import pg from "pg";
94+
95+
export async function upsertEmbedding(embedding: number[], id: number) {
96+
const client = new pg.Client({
97+
connectionString: process.env.DATABASE_URL,
8598
});
86-
```
87-
88-
```ts utils.ts
89-
import pg from "pg";
90-
91-
export async function upsertEmbedding(embedding: number[], id: number) {
92-
const client = new pg.Client({
93-
connectionString: process.env.DATABASE_URL,
94-
});
95-
await client.connect();
96-
97-
try {
98-
const query = `
99-
INSERT INTO post_embeddings (id, embedding)
100-
VALUES ($2, $1)
101-
ON CONFLICT (id)
102-
DO UPDATE SET embedding = $1
103-
`;
104-
const values = [JSON.stringify(embedding), id];
105-
106-
const result = await client.query(query, values);
107-
console.log(`Updated record in database. Rows affected: ${result.rowCount}`);
108-
109-
return result.rowCount;
110-
} catch (error) {
111-
console.error("Error updating record in database:", error);
112-
throw error;
113-
} finally {
114-
await client.end();
115-
}
99+
await client.connect();
100+
101+
try {
102+
const query = `
103+
INSERT INTO post_embeddings (id, embedding)
104+
VALUES ($2, $1)
105+
ON CONFLICT (id)
106+
DO UPDATE SET embedding = $1
107+
`;
108+
const values = [JSON.stringify(embedding), id];
109+
110+
const result = await client.query(query, values);
111+
console.log(`Updated record in database. Rows affected: ${result.rowCount}`);
112+
113+
return result.rowCount;
114+
} catch (error) {
115+
console.error("Error updating record in database:", error);
116+
throw error;
117+
} finally {
118+
await client.end();
116119
}
117-
```
120+
}
121+
````
122+
118123
</CodeGroup>
119124
120-
This task takes in a Sequin record event, creates an embedding, and then upserts the embedding into a `post_embeddings` table.
125+
This task takes in a Sequin record event, creates an embedding, and then upserts the embedding into a `post_embeddings` table.
126+
121127
</Step>
122128
<Step title="Add the task to your Trigger.dev project">
123129
Register the `create-embedding-for-post` task to your Trigger.dev cloud project by running the following command:
124130
125-
```bash
126-
npx trigger.dev@latest dev
127-
```
131+
```bash
132+
npx trigger.dev@latest dev
133+
```
128134

129-
In the Trigger.dev dashboard, you should now see the `create-embedding-for-post` task:
135+
In the Trigger.dev dashboard, you should now see the `create-embedding-for-post` task:
130136

131137
<Frame>
132138
<img src="/images/sequin-register-task.png" alt="Task added" />
@@ -135,63 +141,69 @@ Start by creating a new Trigger.dev task that takes in a Sequin change event as
135141
</Steps>
136142

137143
<Check>
138-
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.
144+
You've successfully created a Trigger.dev task that will create an embedding for each post in your
145+
database. In the next step, you'll create an API endpoint that Sequin can deliver records to.
139146
</Check>
140147

141148
## Setup API route
142149

143150
You'll now create an API endpoint that will receive posts from Sequin and then trigger the `create-embedding-for-post` task.
144151

145152
<Info>
146-
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](https://trigger.dev/docs/guides/frameworks/nextjs).
153+
This guide covers how to setup an API endpoint using the Next.js App Router. You can find examples
154+
for Next.js Server Actions and Pages Router in the [Trigger.dev
155+
documentation](https://trigger.dev/docs/guides/frameworks/nextjs).
147156
</Info>
148157

149158
<Steps titleSize="h3">
150159
<Step title="Create a route handler">
151160
Add a route handler by creating a new `route.ts` file in a `/app/api/create-embedding-for-post` directory:
152161

153-
```ts app/api/create-embedding-for-post/route.ts
154-
import type { createEmbeddingForPost } from "@/trigger/create-embedding-for-post";
155-
import { tasks } from "@trigger.dev/sdk/v3";
156-
import { NextResponse } from "next/server";
157-
158-
export async function POST(req: Request) {
159-
const authHeader = req.headers.get('authorization');
160-
if (!authHeader || authHeader !== `Bearer ${process.env.SEQUIN_WEBHOOK_SECRET}`) {
161-
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
162-
}
163-
const payload = await req.json();
164-
const handle = await tasks.trigger<typeof createEmbeddingForPost>(
165-
"create-embedding-for-post",
166-
payload
167-
);
162+
```ts app/api/create-embedding-for-post/route.ts
163+
import type { createEmbeddingForPost } from "@/trigger/create-embedding-for-post";
164+
import { tasks } from "@trigger.dev/sdk/v3";
165+
import { NextResponse } from "next/server";
168166

169-
return NextResponse.json(handle);
167+
export async function POST(req: Request) {
168+
const authHeader = req.headers.get("authorization");
169+
if (!authHeader || authHeader !== `Bearer ${process.env.SEQUIN_WEBHOOK_SECRET}`) {
170+
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
170171
}
171-
```
172+
const payload = await req.json();
173+
const handle = await tasks.trigger<typeof createEmbeddingForPost>(
174+
"create-embedding-for-post",
175+
payload
176+
);
177+
178+
return NextResponse.json(handle);
179+
}
180+
```
181+
182+
This route handler will receive records from Sequin, parse them, and then trigger the `create-embedding-for-post` task.
172183

173-
This route handler will receive records from Sequin, parse them, and then trigger the `create-embedding-for-post` task.
174184
</Step>
175185
<Step title="Set secret keys">
176186
You'll need to set four secret keys in a `.env.local` file:
177187

178-
```bash
179-
SEQUIN_WEBHOOK_SECRET=your-secret-key
180-
TRIGGER_SECRET_KEY=secret-from-trigger-dev
181-
OPENAI_API_KEY=sk-proj-asdfasdfasdf
182-
DATABASE_URL=postgresql://
183-
```
188+
```bash
189+
SEQUIN_WEBHOOK_SECRET=your-secret-key
190+
TRIGGER_SECRET_KEY=secret-from-trigger-dev
191+
OPENAI_API_KEY=sk-proj-asdfasdfasdf
192+
DATABASE_URL=postgresql://
193+
```
184194

185-
The `SEQUIN_WEBHOOK_SECRET` ensures that only Sequin can access your API endpoint.
195+
The `SEQUIN_WEBHOOK_SECRET` ensures that only Sequin can access your API endpoint.
186196

187-
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.
197+
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.
198+
199+
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](https://trigger.dev/docs/deploy-environment-variables) in Trigger.dev as well.
188200

189-
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](https://trigger.dev/docs/deploy-environment-variables) in Trigger.dev as well.
190201
</Step>
191202
</Steps>
192203

193204
<Check>
194-
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.
205+
You've successfully created an API endpoint that can receive record payloads from Sequin and
206+
trigger a Trigger.dev task. In the next step, you'll setup Sequin to trigger the endpoint.
195207
</Check>
196208

197209
## Create Sequin consumer
@@ -253,11 +265,10 @@ You'll now configure Sequin to send every row in your `posts` table to your Trig
253265
</Frame>
254266
7. Click the **Create Consumer** button.
255267
</Step>
268+
256269
</Steps>
257270

258-
<Check>
259-
Your Sequin consumer is now created and ready to send events to your API endpoint.
260-
</Check>
271+
<Check>Your Sequin consumer is now created and ready to send events to your API endpoint.</Check>
261272

262273
## Test end-to-end
263274

@@ -301,10 +312,12 @@ You'll now configure Sequin to send every row in your `posts` table to your Trig
301312
<img src="/images/sequin-final-run.png" alt="Task run" />
302313
</Frame>
303314
</Step>
315+
304316
</Steps>
305317

306318
<Check>
307-
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.
319+
Every time a post is created or updated, Sequin will deliver the row payload to your API endpoint
320+
and Trigger.dev will run the `create-embedding-for-post` task.
308321
</Check>
309322

310323
## Next steps
@@ -314,4 +327,4 @@ With Sequin and Trigger.dev, every post in your database will now have an embedd
314327
From here, add error handling and deploy to production:
315328

316329
- Add [retries](/errors-retrying) to your Trigger.dev task to ensure that any errors are captured and logged.
317-
- Deploy to [production](/guides/frameworks/nextjs#deploying-your-task-to-trigger-dev) and update your Sequin consumer to point to your production database and endpoint.
330+
- Deploy to [production](/guides/frameworks/nextjs#deploying-your-task-to-trigger-dev) and update your Sequin consumer to point to your production database and endpoint.

docs/guides/frameworks/supabase-edge-functions-basic.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ This guide shows you how to set up and deploy a simple Supabase edge function ex
2626
## Prerequisites
2727

2828
- Ensure you have the [Supabase CLI](https://supabase.com/docs/guides/cli/getting-started) installed
29+
- Since Supabase CLI version 1.123.4, you must have [Docker Desktop installed](https://supabase.com/docs/guides/functions/deploy#deploy-your-edge-functions) to deploy Edge Functions
2930
- Ensure TypeScript is installed
3031
- [Create a Trigger.dev account](https://cloud.trigger.dev)
3132
- [Create a new Trigger.dev project](/guides/dashboard/creating-a-project)

0 commit comments

Comments
 (0)