Skip to main content

Customise with hooks

Hooks are where your own Rust code runs. Declare them in the model, and the generic API calls them around every create, read, update and delete of that entity. The events are beforeCreate, afterCreate, beforeUpdate, afterUpdate, beforeDelete, afterDelete, beforeRead, afterRead, beforeQuery, afterQuery, beforeList, afterList and customValidate.

hooks:
- entity: Account
event: beforeCreate
handler: assignAccountNumber

Two files, on purpose​

FileRegenerated?
backend/src/hooks/mod.rs: the registry, one dispatch function per eventEvery run, so a newly declared hook is always picked up
backend/src/hooks/handlers/<entity>.rs: one function per declared hookWritten once, never overwritten. A hook added later is appended

Your implementations survive every regeneration.

Order inside a write​

  1. customValidate runs first, so validation sees what the caller sent.
  2. beforeCreate / beforeUpdate can change the values.
  3. The write and its audit entry.
  4. after* hooks run after the audit entry and cannot undo the write.

beforeDelete returning Ok(false) refuses the delete, as a guard rather than an error.

Entity names are normalised​

bus_compound, compound, Compound and chemical-inventory style keys all reach the same handlers, so a hook fires whichever route the request took.