Skip to main content

Module hooks

Module hooks 

Source
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 valueEffect
{} / null / anything elsecontinue unchanged
{"data": …}replace the payload (before_create/before_update), answer the request without touching the database (before_read/before_list), or replace the response body (after_*)
{"error": {"status": 422, "message": "…"}}abort the request with that status
Err(msg) from the handlerabort with 400 and msg
a panic in the handlerabort 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§

HookRequest
The request-scoped facts every hook sees, independent of the event.

Functions§

announce
Publish the topic a resource declares for event, if it declares one.
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 the user resource declares one.