Hooks
A hook is an edge function the API calls around a write. It can reject the write, rewrite what gets stored, or react once it has happened. The handler is ordinary TypeScript, and its argument is typed from the model it belongs to.
import type { LoggedIn, Model, Public, Timestamp, UUID } from "@supatype/types"
export type Post = Model<{ id: UUID title: string body: string created_at: Timestamp}, { tableName: "posts" access: { read: Public; create: LoggedIn; update: LoggedIn } hooks: { beforeChange: "moderate-post" afterChange: "index-post" }}>supatype push checks that each named handler exists and fails if not: a hook that never fires
because of a typo would otherwise leave the write it was meant to guard succeeding, looking normal.
A hook is not a security boundary
Say this before anything else, because the feature invites the opposite assumption.
The hooks that can stop a write, beforeChange and beforeDelete, fire for writes through the
API. Direct SQL, psql, seeds and migrations bypass them entirely. So a before-hook cannot enforce
an invariant; it can only enforce it for callers who arrived over HTTP.
The service-role key is not an exception. It changes what Postgres permits, not whether the API runs your hooks, so a service-role write over HTTP fires them like any other, including one your own hook makes. See when a hook writes.
After-hooks are the other way round. They are fed from the database’s change log, so they see
every committed write whatever made it: the API, Studio, plain SQL, a function’s own connection.
That makes afterChange the right place for “whenever a post changes, reindex it”. It still cannot
stop anything: by the time it runs, the write has committed. See after-hooks.
Invariants belong where every writer meets them:
| You want | Use |
|---|---|
| Only the owner may edit | Access rules: RLS, every path |
| Only some roles may read a column | Field access |
| A value must be in range, unique, non-null | Between, Unique, a string-literal union: a real constraint |
| A value derived from other fields | Slug<"title">, ComputedFrom: a trigger, applied to every writer |
A hook is for the work that is genuinely application logic: enrichment, notification, indexing, a validation the database has no way to express.
The four hooks
| Hook | When | Can reject | Can change the write |
|---|---|---|---|
beforeChange | before an insert or update reaches Postgres | yes | yes |
beforeDelete | before a delete reaches Postgres | yes | no |
afterChange | after an insert or update commits, from any writer | no | no |
afterDelete | after a delete commits, from any writer | no | no |
There are no read hooks. A hook on every row of every response is a latency multiplier on the hottest path, and hiding data is what field access already does inside Postgres.
Writing one
npx supatype hooks new moderate-postnpx supatype push # generates hooks/_supatype/hooks.tsHooks live in hooks/, next to functions/ and separate from it: a function is a public endpoint
anyone with your anon key can invoke, while a hook’s route is refused from outside and only the API
server calls it.
// docs:no-check: hooks/moderate-post/index.ts, typechecked in your project against generated typesimport { hook, type BeforeChange } from "../_supatype/hooks.ts"
const moderate: BeforeChange<"posts"> = async (ctx) => { if (ctx.operation === "insert") { if (ctx.rows.some((row) => row.title.trim() === "")) { return { reject: "A post needs a title" } } return { rows: ctx.rows.map((row) => ({ ...row, title: row.title.trim() })) } }
if (ctx.patch.title !== undefined && ctx.patch.title.trim() === "") { return { reject: { message: "A post needs a title", status: 422 } } } return {}}
export default hook(moderate)ctx is a discriminated union on operation, which is what makes narrowing pay: an insert has
rows (the model’s Insert shape), an update has patch (its Update shape) and filter. The
generated HookedTable contains only tables that declare a hook (or a field validator), so
BeforeChange<"comments"> is a compile error if Comment declares neither.
One handler per directory is the runtime’s rule, so a model’s whole lifecycle in one place uses the multiplexer:
import { hooks, type AfterChange, type BeforeChange } from "../_supatype/hooks.ts"
const moderate: BeforeChange<"posts"> = async (ctx) => ({})const reindex: AfterChange<"posts"> = async (ctx) => { await fetch("https://search.example.com/index", { method: "POST", body: JSON.stringify({ table: ctx.table, rows: ctx.rows }), })}
export default hooks({ beforeChange: moderate, afterChange: reindex })Point each event at that same name in the schema: hooks: { beforeChange: "post-hooks", afterChange: "post-hooks" }.
One call per request, not per row
A REST write is not “a row”. POST may carry an array. PATCH /posts?status=eq.draft is a partial
patch plus a filter that may match a thousand rows. DELETE is a filter alone.
So a before-hook is called once per request, with what the request actually contains, and an
update therefore gets a patch and a filter rather than rows. The filter is not rewritable: a hook that
could widen ?id=eq.1 into ?id=gt.0 would turn a one-row update into a table-wide one with nothing
in the caller’s code to suggest it.
After-hooks run once the change is committed, so they get the rows themselves instead: see below.
After-hooks
afterChange and afterDelete read committed changes from the database’s change log (logical
replication), not from the API’s reply. So they fire for every writer, and the rows are always there.
One call carries every row one transaction changed in one table with one operation. A bulk insert
of 500 rows is one call with 500 rows, and a transaction that inserts into posts and updates
authors makes one call for each.
| Field | afterChange insert | afterChange update | afterDelete |
|---|---|---|---|
rows | the inserted rows | the rows as they are now | none |
old | none | the same rows as they were before, old[i] being rows[i] | the deleted rows |
deliveryId | always | always | always |
Values are in the JSON shape PostgREST returns for the same row: a timestamp is an ISO string, a
bigint or numeric is a number, a JSON column is its JSON. So the generated row types hold, and a
row reads the same here as in the client.
ctx.user is the caller when the write came through the API, and null for a write made outside it,
such as SQL in psql or a migration.
import { hook, type AfterChange } from "../_supatype/hooks.ts"
const notify: AfterChange<"posts"> = async (ctx) => { if (ctx.operation === "update") { if (ctx.rows.every((row, i) => ctx.old[i]?.title === row.title)) return } await fetch("https://chat.example.com/notify", { method: "POST", body: JSON.stringify({ deliveryId: ctx.deliveryId, rows: ctx.rows }), })}
export default hook(notify)Delivery is at least once
A call is delivered until your hook answers, with retries and backoff, and the change log only moves
on once it has. Nothing is lost if the server restarts, but the same call can arrive twice. Each
carries a deliveryId, commitLSN:schema.table:operation, the same on every delivery of that call.
Dedupe per call: a hook whose effect must not repeat (an email, a charge, a counter) records the
ids it has handled and skips one it has seen, for example with a unique key on a table of handled ids.
A repeat repeats the whole call, every row of it, so record the id once the call’s work is done, not
per row. A hook that only overwrites something, such as a search index entry, needs nothing extra.
Restricted columns stay out
The change log is not filtered by row or field rules, so the server strips columns before your hook
sees a row: every column with a field read rule (field access) is
left out of rows and old. The generated types match, so reading one is a compile error. That
stops a hook forwarding a row to email or chat from leaking a column its readers could never see.
A hook that genuinely needs those columns opts in:
hooks: { afterChange: { function: "audit-salary", columns: "all" }}Hooks already hold the service-role key, so this is about accidental leaks rather than privilege: opt in only for a hook that will not pass the column on.
Seeds and push data migrations do not fire them
supatype seed marks its transaction so after-hooks skip it. Loading fixtures should not send a
hundred welcome emails.
A push’s own data migrations (a column converted to a new type, blocks moved, rows rewritten for a
kind change) skip them the same way, since a conversion over a whole table is not something a hook
should hear about row by row. supatype push --run-hooks keeps them firing for that push.
Databases without a change log
Some external Postgres cannot do logical replication (wal_level is not logical, the wal2json
plugin is not allowed, or the server’s role lacks REPLICATION). There, after-hooks fall back to the
request path: after an API write succeeds, the server reads the written rows back as the service role,
strips restricted columns the same way, and calls your hook. That works for every write through the
API and misses writes made outside it. supatype db check and supatype doctor say which path
your database gives you, and why.
A table with an after-hook logs whole old rows to the change log (REPLICA IDENTITY FULL), so that
old can be complete. That costs extra WAL on those tables only.
Reading what is about to change
ctx.previous() fetches the rows a write will overwrite. Only if you ask: a hook that never looks
costs nothing:
import { hook, type BeforeChange } from "../_supatype/hooks.ts"
const guard: BeforeChange<"posts"> = async (ctx) => { if (ctx.operation === "insert") return {}
const { rows, truncated } = await ctx.previous() if (truncated) return { reject: "Refusing to patch more rows than this hook can check" } if (rows.some((row) => row.title.startsWith("[locked]"))) { return { reject: { message: "That post is locked", status: 409 } } } return {}}
export default hook(guard)Three things to know about those rows. They are the rows the write will change: the request’s own
filter plus the caller’s own update or delete rule, so a hook cannot widen it. They come back as
stored, with no field masking, which is what makes them useful for validation and means you should
not echo them into a rejection message, since that message reaches the caller. And they are capped (100 by default),
with truncated telling you the cap bit, so you can refuse rather than act on a prefix you did not
choose.
Identity
ctx.user is already verified: the server checked the token’s signature before invoking your
hook. null means an anonymous caller, or, for an after-hook, a write made outside the API. That is
not “no token supplied”, but no verified subject:
// docs:no-checkif (ctx.user === null) return { reject: { message: "Sign in first", status: 401 } }console.log(`${ctx.user.sub} (${ctx.user.role}) is writing to ${ctx.table}`)That is a real difference from a public edge function, which has to establish identity itself.
Hooks also receive the service-role key without being listed in functions.serviceRole, because
they are only reachable by the API server. A public function must opt in.
Rejecting, and what the caller sees
The HTTP status is the answer, so a hook can pick one:
| Your handler returns | The caller gets |
|---|---|
{} | the write proceeds |
{ rows: [...] } / { patch: {...} } | the write proceeds with your version |
{ reject: "message" } | 422 with that message |
{ reject: { message, status: 409, code } } | 409, your message and code |
A rejection is always a 4xx: a status outside that range is answered as 422, because
anything else would read as the hook being broken rather than saying no.
A replacement body is substituted and then travels the normal path, with the same token and the same rules. So a hook
that sets author_id to somebody else still fails that row’s own policy; nothing is applied with
elevated privilege.
When a hook breaks
A 4xx is your hook saying no. Anything else (a timeout, a crash, an unreachable worker) is your
hook not answering, which is a different thing and governed separately:
hooks: { beforeChange: { function: "moderate-post", timeout: 2000, onUnavailable: "reject" }}onUnavailable defaults to "reject" before a write and "log" after one. Before: a validation hook
that silently stopped running must not pass writes through, so the caller gets a 503 and the write
does not happen. After: the write already happened, so there is nothing left to fail.
An after* hook’s failure never reaches the caller, and it only runs at all for a committed write. A
delivery it does not answer is retried with backoff, then logged.
When a hook writes
A hook gets the same context as any function, flat on ctx: ctx.url, ctx.anonKey, ctx.serviceRoleKey (when the project granted the function the service role), ctx.dbUrl (when the operator set SUPATYPE_FUNCTIONS_DB_URL) and ctx.env, beside the hook’s own table, operation and user. That is what lets a hook write back.
A hook holding the service-role key writes with it, and service-role writes still fire hooks. A write a hook makes fires hooks again, through the API or through ctx.dbUrl’s change log alike, and only the hook-depth header described below bounds the chain. So a beforeChange on
posts that writes to posts calls itself again: the classic trigger loop, except each hop is a
fresh HTTP request holding a connection and a function slot.
The chain counts itself. Every hook invocation carries its depth, the worker attaches that depth to any
request your handler makes back to your stack, and a write arriving 4 hooks deep is refused with a
508 rather than run without its hooks, because a validation hook that quietly stopped running is a
hole in every check the table declares.
Two consequences worth knowing:
- Fanning out is fine. A
beforeChangeonpostswriting anaudit_logrow whose own hook ships it somewhere is two levels deep and entirely normal. Only a genuine loop reaches the limit. - Writing to your own table needs care. Prefer moving the change into the row you are already
returning from
beforeChange, which costs no extra request at all.
The depth travels as a header the worker attaches for you, so this holds whichever client you write with, and it is never sent anywhere but your own stack.
An after-hook reads the change log, where every write is a new change, so an afterChange that
writes to its own table is called again for its own write. Make such a write stop itself: write only
when the value would actually change, so the second call finds nothing to do.
What a hook costs
A write with a before-hook is two network hops instead of one, plus one edge-function invocation. On Supatype Cloud that invocation counts against your plan’s function limits like any other, and the concurrency limit becomes a limit on concurrent writes to hooked tables: 10 on the Free plan. The default 2s timeout sits well below the Free plan’s 10s function time limit, so a hung hook fails fast instead of occupying a slot.
An after-hook adds nothing to the write’s own latency, since it runs after commit, but each delivery is still a function invocation, and its table writes more WAL (see above).
Hooks are worth this on the tables that need them. hooks on every model is an easy and expensive
mistake.
Checking they will run
npx supatype hooks listnpx supatype doctorBoth list every declared hook and mark one whose handler does not exist. doctor also reports the
other ways a hook can fail to fire: functions switched off in the stack, a manifest that predates
your hooks, or after-hooks on the request path, which miss writes made outside the API. Each of those
failures is otherwise silent: the write simply succeeds unvalidated.
doctor asks the running server, whose listener delivers after-hooks, and warns when its slot holds
more than 1 GiB of WAL: delivery is behind, and Postgres drops a slot that falls too far behind, after
which the changes it had not delivered never are. supatype db check reports the same for any
database you point it at.