Collection Hooks
Collection hooks fire when documents in a specific collection change. Attach them per-collection through the Dashboard.Events
pre_save
Fires before a document is created or updated. Runs in the background (does not block the write).
Use it to: enrich data before saving, send a notification that something is about to be written, log an audit event.
Payload:
post_save
Fires after a document is successfully created or updated. Runs in the background.
Use it to: send notifications, update a search index, trigger downstream workflows.
Payload:
pre_delete
Fires before a document is deleted. Runs in the background.
Use it to: archive data elsewhere before it’s gone, clean up related records, send a warning notification.
Payload:
post_delete
Fires after a document has been deleted. Runs in the background.
Use it to: clean up dependent data, update counters, remove from search index.
Payload:
Handling Both pre_save Operations
If one function handles both create and update, check operation:
Rules vs Hooks
Collection hooks are different from collection rules:
Use rules to gate who can write. Use hooks to react to what was written.
Limits & Behaviour
- Hooks run in background threads — they never add latency to your API response.
- If a hook function fails or times out (15 second limit), the error is logged but the document operation is not rolled back.
- You can attach multiple hooks to the same event on the same collection — all of them fire.
- Hooks only fire on operations made through the Cocobase API/SDK, not direct DB writes.
