Backend
Global Backend Hooks
Overview
Section titled “Overview”Rebase runs table hooks at two levels, both of the TableHooks type from
@rebasepro/server/schema:
- Table hooks: the
hookskey of adefineTable. They run only for that table. - Global hooks: exported as
hooksfrombackend/hooks.ts. They fire on every table, on every data path (REST API, WebSocket / realtime, MCP, server-siderebase.dataAsAdmin). The one writer they do not see is the auth system: sign-up, OAuth sign-in and the admin’s user management write user rows directly and run the auth hooks (afterUserCreate, …) instead.
Use global hooks for:
- Row scoping —
beforeQueryon every table, so a tenant’s reads are narrowed in one place instead of per table. SeebeforeQuery. - PII masking — redact sensitive fields for non-admin callers across all tables.
- Unified audit logging — log every create, update, or delete in one place. New accounts from sign-up and OAuth are not among them; log those from
afterUserCreate. - Cross-cutting validation — enforce invariants that span multiple tables.
Each hook receives the descriptor of the table it runs for as table, so
global hooks need declared tables: a backend that introspects the database
(no backend/schema) refuses them at boot.
Configuration
Section titled “Configuration”// backend/hooks.tsimport type { TableHooks } from "@rebasepro/server/schema";
export const hooks: TableHooks = { afterRead({ values }) { // Runs after every row read, across all tables return values; }, beforeSave({ values }) { // Runs before every save return values; }};hooks takes one set. To keep several concerns apart, write each as its own
set and pass composeTableHooks(tenantScope, auditTrail) from
@rebasepro/common: they combine exactly as the tiers above do. See
Combining hook sets.
The same goes for backend.setTableHooks(slug, hooks), which replaces the
hooks a table declared: pass composeTableHooks(declared, yours) to keep them.
TableHooks Type
Section titled “TableHooks Type”type TableHooks = { beforeQuery?(props): QueryNarrowing | void; // Conditions to AND into a read before it is compiled afterRead?(props): Record<string, unknown>; // Transform the row before returning it to the caller beforeSave?(props): Partial<Values>; // Modify values before writing to DB afterSave?(props): void; // After the write, still in the transaction afterSaveError?(props): void; // Side-effects after a failed save beforeDelete?(props): boolean | void; // Return false (403) or throw to block deletion afterDelete?(props): void; // After the delete, still in the transaction};beforeQuery narrows a read before it is compiled; see
beforeQuery.
All hooks may return a Promise (async) or a plain value (sync).
Hook Props
Section titled “Hook Props”Each hook receives a single props object. Common fields:
| Field | Type | Present in |
|---|---|---|
table |
TableDescriptor |
All hooks |
path |
string |
All hooks |
values |
Record<string, unknown> |
All but beforeQuery: the row read (afterRead), being deleted (beforeDelete, afterDelete), or the values written (the save hooks) |
id |
string | number |
beforeSave (optional), afterSave, afterSaveError (optional), beforeDelete, afterDelete |
previousValues |
row (optional) | beforeSave, afterSave, afterSaveError |
status |
"new" | "existing" | "copy" |
beforeSave, afterSave, afterSaveError |
error |
unknown |
afterSaveError |
operation, query |
read kind, the caller’s query | beforeQuery |
context |
RebaseCallContext |
All hooks |
context.user contains the authenticated user (uid, roles, etc.). A request with no token, where the API lets one in, carries the anonymous identity { uid: "anonymous", roles: ["anon"] } rather than undefined.
context.credential names the API key a write was made with: { kind, id, name }, where kind is "personal" or "service". A personal key acts as its owner, so context.user is the owner whether they saved by hand or a CI job wrote with their key; context.credential tells the two apart. It is set on the data API, the realtime socket and /mcp, and absent for a session, the service key, server-context work (rebase.dataAsAdmin, crons) and in the browser. It never carries the secret.
table is always there. A global hook fires for every table, so it is the one
tier that is registered independently of any of them — but it is still never
handed a missing table. A request naming a path the server cannot resolve is
refused with 404 NOT_FOUND before any tier runs, which is the same answer the
read and write paths give such a path anyway. The alternative — skipping the
tier for those paths — would make afterRead a redaction step with a silent
exception, so it is not on offer.
Execution Pipeline
Section titled “Execution Pipeline”[Client Request] │ ▼ [Hono Router] │ [Database Driver] ┌─────┴───────────────────────────────────────────────────────┐ │ 1. Start PostgreSQL Transaction │ │ 2. Set Config: app.user_id = '<uid>', app.user_roles = ... │ │ │ │ 3. Global hook: beforeSave ─┐ │ │ 4. Table hook: beforeSave ─┘ awaited │ │ 5. Drizzle SQL execution & Postgres RLS evaluation │ │ 6. Global hook: afterSave ─┐ │ │ 7. Table hook: afterSave ─┘ awaited │ │ │ │ 8. Commit ← a throw anywhere in 3–7 rolls the write back │ └─────┬───────────────────────────────────────────────────────┘ │ [Realtime notifications flushed — after the commit, never before] │ ▼[Client Response]Blocking vs. Async Semantics
Section titled “Blocking vs. Async Semantics”Every hook in the list below is awaited, and all of them but
afterSaveError run inside the transaction that carries the write. There is
no “fire and forget” tier: the row and everything its hooks did commit
together or not at all.
beforeSave,beforeDelete— if the hook throws, the operation is rejected with an HTTP 400 carrying your message and the codeCALLBACK_REJECTED, and the database write never happens. Throw aRebaseApiErrorfrom@rebasepro/typesto pick the status yourself — see Table hooks. AbeforeDeletethat returnsfalseis the same refusal with no message, and answers 403 with that code.afterRead— the returned row (or transformed row) is what the caller receives, and only the caller: on a write it shapes the response, whileafterSave,beforeDelete,afterDeleteand history get the row as stored — a masked value is never recorded or reverted into the column. Its transaction isREAD ONLY— see below.afterSave,afterDelete— run before the commit, awaited. A throw here rolls the row back and answers the same 400CALLBACK_REJECTED, withdetails.stagenaming the hook. They hold the transaction open while they run, so a slow one is a lock held.afterSaveError— runs when the save failed, on the way out. On a request it runs after the failed write’s transaction has rolled back, with acontextwhose every call is a transaction of its own, so a job it enqueues to report the failure is kept. A throw from it is logged; the caller gets the save’s own error.
When afterSaveError runs
Section titled “When afterSaveError runs”It reports a save that failed at the database or after it — from the INSERT or UPDATE statement on. A save refused before that point never reached the database, so the hook is not told about it.
| Runs | Does not run |
|---|---|
The database refuses the statement: a unique or foreign key violation, a check constraint or trigger, a row-level security policy (WRITE_DENIED) |
A beforeSave refusal — the hook that refused is the one that knows |
afterRead or afterSave throws (the caller gets CALLBACK_REJECTED) |
A request refused before the driver: validation (VALIDATION_*), a field the caller may not write, a missing permission |
| The history entry cannot be recorded | A row the caller cannot address (404), a tenant stamp it may not set |
The commit is refused after the save returned: TRANSACTION_ABORTED (a failed statement a hook caught), or a deferred constraint checked at COMMIT |
|
| A delete — there is no delete counterpart |
In a bulk or _batch write it runs for the row that failed; the rows before it
are rolled back with the batch and are not reported.
Side effects that must not hold the transaction
Section titled “Side effects that must not hold the transaction”Anything slow, or anything that cannot be undone if the transaction rolls back, does not belong in the hook body:
| Want | Do this instead |
|---|---|
| Call a third party, send mail, generate a file | Enqueue a job. A job enqueued in a transaction that rolls back was never enqueued — which is the behaviour you want. |
| Tell other processes something happened | Publish on a realtime channel after the write returns, not from inside the hook. |
| Work in a custom function that the caller need not wait for | waitUntil(c, promise) from @rebasepro/server/functions — it runs after the response, and the host waits for it before shutting down. |
The rule of thumb: if the work should still happen when the write is undone, it is not part of the write, so it does not go in the hook.
afterRead cannot write
Section titled “afterRead cannot write”A request-scoped read opens its transaction READ ONLY, and so does a realtime
subscription’s refetch. afterRead runs inside
it, so no write from that hook can succeed — not a context.data
create, not an update, not one buried in a helper it calls. Postgres refuses the
statement with SQLSTATE 25006, and the caller is answered:
{ "error": { "message": "An `afterRead` hook tried to write. …", "code": "READ_ONLY_TRANSACTION", "details": { "dbCode": "25006" } } }That is a 409, not a 500: it is your code being refused, not the server failing. The read-only mode is deliberate — a read that quietly writes is a read whose cost, locks and RLS surface nobody budgeted for.
So read auditing does not belong in afterRead. Log the read outside the
request instead — from a background job fed by whatever you already emit, or
from a custom function that does the read and the write with two separate
calls:
// ✗ Fails with READ_ONLY_TRANSACTION on every read.export const hooks: TableHooks = { afterRead: async ({ path, values, context }) => { await context.data.read_log.create({ path, uid: context.user?.uid }); return values; }};// backend/functions/read-article.ts — served at /api/functions/read-article/:id// ✓ The read and the audit row are two operations, and only the second writes.import { defineFunction, getUserId, requireAuth, requireDriver } from "@rebasepro/server/functions";
export default defineFunction((app, { rebase }) => { app.get("/:id", requireAuth, async (c) => { const article = await requireDriver(c).fetchOne({ path: "articles", id: c.req.param("id") }); await rebase.dataAsAdmin.read_log.create({ path: "articles", uid: getUserId(c) }); return c.json(article); });});Write-side auditing has no such problem: afterSave and afterDelete run in a
read-write transaction, and the audit row commits with the change it records.
Examples
Section titled “Examples”PII Masking
Section titled “PII Masking”Redact email addresses for non-admin callers across every table:
// backend/hooks.tsimport type { TableHooks } from "@rebasepro/server/schema";
export const hooks: TableHooks = { afterRead({ values, context }) { const isAdmin = context.user?.roles?.includes("admin"); if (!isAdmin && values.email) { return { ...values, email: "********" }; } return values; }};Global Audit Logging
Section titled “Global Audit Logging”Record every deletion, across every table, in an audit_log table. Because
afterDelete runs in the delete’s own transaction, the audit row and the
deletion commit together — there is no window in which one exists without the
other:
// backend/hooks.tsimport type { TableHooks } from "@rebasepro/server/schema";
export const hooks: TableHooks = { async afterDelete({ table, id, values, context }) { if (table.slug === "audit_log") return; // don't audit the audit await context.data.audit_log.create({ action: "delete", table: table.slug, entity_id: String(id), actor: context.user?.uid ?? "anonymous", // The API key, when the delete was made with one. via_key: context.credential?.name ?? null, snapshot: values }); }};Note what this buys and what it costs: if the audit row cannot be written, the delete does not happen either. For an audit trail that is usually what you want. If it is not, catch the error in the hook and say so in a comment.
Table-Specific Logic
Section titled “Table-Specific Logic”Global hooks fire for all tables. To scope logic to a single table, check table.slug or path:
import type { TableHooks } from "@rebasepro/server/schema";
export const hooks: TableHooks = { beforeSave({ table, values }) { if (table.slug === "orders") { const total = values.total; if (typeof total !== "number" || total <= 0) { throw new Error("Order total must be positive"); } } return values; }};For hooks that only apply to a single table, prefer the table’s own hooks instead.