Functions And Triggers
When Handlet uses database-side logic and how to keep it safe.
Database functions and triggers are useful for invariants that must hold even when writes come from different application surfaces.
Good fits
- audit event recording
- immutable state transition guards
- data normalization that must run for every writer
- small helper functions used by RLS policies
Poor fits
- long-running workflow orchestration
- provider API calls
- business decisions that need product context or user-facing error handling
Security
Use SECURITY DEFINER only when the function needs it. Keep the search path explicit and make sure the function cannot be used to read or write another tenant's data.
Change process
Database functions and triggers should be changed through migrations, reviewed with the schema files, and covered by database or service tests when they guard important behavior.