数据库网页钩子
Trigger external payloads on database events.
数据库 Webhooks 让你在表发生事件时,把实时数据从数据库发送到另一个系统。
🌐 Database Webhooks allow you to send real-time data from your database to another system whenever a table event occurs.
你可以监听三个表事件:INSERT、UPDATE 和 DELETE。所有事件都会在数据库行被更改 之后 触发。
🌐 You can hook into three table events: INSERT, UPDATE, and DELETE. All events are fired after a database row is changed.
网页钩子 vs 触发器 #
🌐 Webhooks vs triggers
数据库 Webhooks 和触发器非常相似,这是因为数据库 Webhooks 是使用 pg_net 扩展对触发器的方便封装。这个扩展是异步的,因此不会因为长时间运行的网络请求而阻塞你的数据库更改。
🌐 Database Webhooks are very similar to triggers, and that's because Database Webhooks are a convenience wrapper around triggers using the pg_net extension. This extension is asynchronous, and therefore will not block your database changes for long-running network requests.
这个视频演示了每次在 profiles 表中插入一行时,如何在 Stripe 中创建一个新的客户:
🌐 This video demonstrates how you can create a new customer in Stripe each time a row is inserted into a profiles table:
创建一个网络钩子 #
🌐 Creating a webhook
- 在仪表板中创建一个新的 数据库 Webhook。
- 给你的 Webhook 起个名字。
- 选择你想要连接的表。
- 选择一个或多个你想要钩子的事件(表插入、更新或删除)。
由于 webhooks 是数据库触发器,你也可以直接从 SQL 语句创建一个。
🌐 Since webhooks are database triggers, you can also create one from SQL statement directly.
1create trigger "my_webhook" after insert2on "public"."my_table" for each row3execute function "supabase_functions"."http_request"(4 'http://host.docker.internal:3000',5 'POST',6 '{"Content-Type":"application/json"}',7 '{}',8 '1000'9);我们目前支持 HTTP 网络钩子。这些可以作为 POST 或 GET 请求发送,携带 JSON 负载。
🌐 We currently support HTTP webhooks. These can be sent as POST or GET requests with a JSON payload.
有效载荷 #
🌐 Payload
有效负载是从底层表记录自动生成的:
🌐 The payload is automatically generated from the underlying table record:
1type InsertPayload = {2 type: 'INSERT'3 table: string4 schema: string5 record: TableRecord<T>6 old_record: null7}8type UpdatePayload = {9 type: 'UPDATE'10 table: string11 schema: string12 record: TableRecord<T>13 old_record: TableRecord<T>14}15type DeletePayload = {16 type: 'DELETE'17 table: string18 schema: string19 record: null20 old_record: TableRecord<T>21}监控 #
🌐 Monitoring
Webhook 调用的日志历史可以在你数据库的 net 架构下找到。更多信息,请查看 GitHub 仓库。
🌐 Logging history of webhook calls is available under the net schema of your database. For more info, see the GitHub Repo.
本地开发 #
🌐 Local development
在本地 Supabase 实例上使用数据库 Webhooks 时,你需要注意 Postgres 数据库是在 Docker 容器里运行的。这意味着你在 webhook URL 里的 localhost 或 127.0.0.1 指的会是容器本身,而不是运行你应用的宿主机器。
🌐 When using Database Webhooks on your local Supabase instance, you need to be aware that the Postgres database runs inside a Docker container. This means that localhost or 127.0.0.1 in your webhook URL will refer to the container itself, not your host machine where your application is running.
要针对在你的主机上运行的服务,可以使用 host.docker.internal。如果那不起作用,你可能需要改用你机器的本地 IP 地址。
🌐 To target services running on your host machine, use host.docker.internal. If that doesn't work, you may need to use your machine's local IP address instead.
例如,如果你想在 webhook 触发时调用边缘函数,你的 webhook URL 会是:
🌐 For example, if you want to trigger an edge function when a webhook fires, your webhook URL would be:
1http://host.docker.internal:54321/functions/v1/my-function-name如果你在本地使用 webhooks 时遇到连接问题,请确认你使用的是正确的主机名,而不是 localhost。
🌐 If you're experiencing connection issues with webhooks locally, verify you're using the correct hostname instead of localhost.
资源 #
🌐 Resources
- pg_net:Postgres 的异步网络扩展