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
| File | Regenerated? |
|---|---|
backend/src/hooks/mod.rs: the registry, one dispatch function per event | Every run, so a newly declared hook is always picked up |
backend/src/hooks/handlers/<entity>.rs: one function per declared hook | Written once, never overwritten. A hook added later is appended |
Your implementations survive every regeneration.
Order inside a write
customValidateruns first, so validation sees what the caller sent.beforeCreate/beforeUpdatecan change the values.- The write and its audit entry.
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.