Expand description
Resource lifecycle hooks: running a named function around a CRUD operation.
A resource declares hooks in its [hooks] section, one function name per
HookEvent:
[hooks]
before_create = "validate_post"
after_create = "notify_slack"before_* hooks run after the permission check but before the database is
touched; after_* hooks run once the operation succeeded. Both receive the
operation’s payload as their input (the submitted body, the row, or the
list of rows) plus a context object describing the event,
the request URL, the caller’s auth status and the row(s) in play — reachable
from a function through ctx.hook().
What a hook returns decides what happens next:
| Return value | Effect |
|---|---|
{} / null / anything else | continue unchanged |
{"data": …} | replace the payload (before_create/before_update) or the response body (after_*) |
{"error": {"status": 422, "message": "…"}} | abort the request with that status |
Err(msg) from the handler | abort with 400 and msg |
| a panic in the handler | abort with 500; the detail is logged, not returned |
Hooks are called regardless of a function’s visibility, so a
Private function — invisible over HTTP — is the natural way to write one.
Structs§
- Hook
Request - The request-scoped facts every hook sees, independent of the event.
Functions§
- replacement_
object - A hook’s replacement payload, which must stay a JSON object for the operations that write columns.
- run
- Run the hook bound to
event, if the resource declares one. - run_
auth - Run the auth hook bound to
event, if theuserresource declares one.