@trigger.dev/supabase: You can now trigger on multiple database events in the same job
This commit is contained in:
@@ -38,8 +38,8 @@ client.defineJob({
|
||||
supabase,
|
||||
},
|
||||
run: async (payload, io, ctx) => {
|
||||
const { data: users, error } = await io.supabase.runTask("find-users", async (db) => {
|
||||
return db.from("users").select("*");
|
||||
const { data: todos, error } = await io.supabase.runTask("find-todos", async (db) => {
|
||||
return db.from("todos").select("*");
|
||||
});
|
||||
},
|
||||
});
|
||||
@@ -60,8 +60,8 @@ client.defineJob({
|
||||
supabase,
|
||||
},
|
||||
run: async (payload, io, ctx) => {
|
||||
const users = await io.supabase.runTask("find-users", async (db) => {
|
||||
const { data, error } = await db.from("users").select("*");
|
||||
const todos = await io.supabase.runTask("find-todos", async (db) => {
|
||||
const { data, error } = await db.from("todos").select("*");
|
||||
|
||||
if (error) throw error;
|
||||
|
||||
@@ -100,15 +100,15 @@ client.defineJob({
|
||||
supabase,
|
||||
},
|
||||
run: async (payload, io, ctx) => {
|
||||
const users = await io.supabase.runTask("find-users", async (db) => {
|
||||
const { data, error } = await db.from("users").select("*");
|
||||
const todos = await io.supabase.runTask("find-todos", async (db) => {
|
||||
const { data, error } = await db.from("todos").select("*");
|
||||
|
||||
if (error) throw error;
|
||||
|
||||
return data;
|
||||
});
|
||||
|
||||
// users is now typed as User[] instead of any[]
|
||||
// todos is now typed as Todo[] instead of any[]
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
@@ -126,7 +126,7 @@ client.defineJob({
|
||||
id: "supabase-trigger",
|
||||
name: "Supabase Trigger",
|
||||
trigger: db.onInserted({
|
||||
table: "users",
|
||||
table: "todos",
|
||||
}),
|
||||
run: async (payload, io, ctx) => {
|
||||
// payload is the database webhook body (see https://supabase.com/docs/guides/database/webhooks#payload)
|
||||
@@ -137,20 +137,6 @@ client.defineJob({
|
||||
You can add additional filters to the trigger by passing a `filter` object:
|
||||
|
||||
```ts
|
||||
client.defineJob({
|
||||
id: "supabase-trigger",
|
||||
name: "Supabase Trigger",
|
||||
trigger: db.onUpdated({
|
||||
table: "users",
|
||||
filter: {
|
||||
country: ["USA", "Canada"], // This will only trigger the job if the user.country is USA or Canada
|
||||
},
|
||||
}),
|
||||
run: async (payload, io, ctx) => {
|
||||
// payload is the database webhook body (see https://supabase.com/docs/guides/database/webhooks#payload)
|
||||
},
|
||||
});
|
||||
|
||||
client.defineJob({
|
||||
id: "supabase-trigger",
|
||||
name: "Supabase Trigger",
|
||||
@@ -172,6 +158,31 @@ client.defineJob({
|
||||
});
|
||||
```
|
||||
|
||||
You can also listen for multiple different events using the `on` trigger:
|
||||
|
||||
```ts
|
||||
client.defineJob({
|
||||
id: "supabase-trigger",
|
||||
name: "Supabase Trigger",
|
||||
trigger: db.on({
|
||||
table: "todos",
|
||||
events: ["INSERT", "UPDATE"] // Trigger on both insert and update events
|
||||
filter: {
|
||||
record: {
|
||||
is_completed: [false],
|
||||
},
|
||||
},
|
||||
}),
|
||||
run: async (payload, io, ctx) => {
|
||||
if (payload.type === "INSERT") {
|
||||
// payload will be typed as the INSERT payload
|
||||
} else {
|
||||
// payload will be typed as the UPDATE payload
|
||||
}
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
<Note>
|
||||
We will only create at most 1 database webhook per table, to limit resource usage when writing to
|
||||
your database. This means we cannot support scoping updated triggers to specific columns.
|
||||
@@ -196,10 +207,10 @@ client.defineJob({
|
||||
id: "supabase-trigger",
|
||||
name: "Supabase Trigger",
|
||||
trigger: db.onUpdated({
|
||||
table: "users",
|
||||
table: "todos",
|
||||
}),
|
||||
run: async (payload, io, ctx) => {
|
||||
// payload.record and payload.old_record are now correctly typed to match the users table
|
||||
// payload.record and payload.old_record are now correctly typed to match the todos table
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user