Version: 1.0 (Stable API) Status: Production ReadyDocumentation Index
Fetch the complete documentation index at: https://docs.snackbase.dev/llms.txt
Use this file to discover all available pages before exploring further.
This page covers the code-level Python hook system for extending SnackBase internals with custom Python code.
To manage hooks via the REST API without writing code (event triggers, cron schedules, and manual triggers), see API-Defined Hooks.
Overview
The SnackBase Hook System is an extensibility framework that allows developers to inject custom logic at specific points in the application lifecycle. It provides a stable, event-driven API for extending SnackBase without modifying core code.Key Features
- Event-Driven Architecture: Subscribe to lifecycle events
- Priority-Based Execution: Control hook execution order
- Tag-Based Filtering: Target specific collections or resources
- Before/After Hooks: Modify data or react to changes
- Built-in Hooks: Core functionality (timestamps, account isolation)
- Abort Capability: Cancel operations from before hooks
- Async Support: Full async/await support
- Stable API: Guaranteed backward compatibility
The Hook System API is stable and follows semantic versioning. Breaking
changes will only occur in major version releases.
Stable API Contract
Guaranteed Stability
Stable (will not change):HookRegistry.register()method signatureHookRegistry.trigger()method signatureHookRegistry.unregister()method signatureHookContextdataclass structureAbortHookExceptionbehavior- Hook event naming convention
- Priority-based execution order
- Tag-based filtering mechanism
- Built-in hook behavior
- New hook events
- New hook categories
- New
HookContextfields (optional) - New built-in hooks
- New utility functions
Hook Categories
Hooks are organized into 8 categories:| Category | Description | Examples |
|---|---|---|
| App Lifecycle | Application startup/shutdown | on_bootstrap, on_serve, on_terminate |
| Model Operations | Internal SQLAlchemy models | on_model_before_create, on_model_after_update |
| Record Operations | Dynamic collection records | on_record_before_create, on_record_after_delete |
| Collection Operations | Schema changes | on_collection_before_create, on_collection_after_update |
| Auth Operations | Authentication events | on_auth_after_login, on_auth_before_register |
| Request Processing | HTTP request lifecycle | on_before_request, on_after_request |
| Realtime | WebSocket/SSE events | on_realtime_connect, on_realtime_message |
| Mailer | Email sending | on_mailer_before_send, on_mailer_after_send |
Hook Events
Naming Convention
All hook events follow a consistent pattern:before_*: Called before operation (can modify data or abort)after_*: Called after successful operation (read-only, side effects)
Complete Event List
Record Operations (Dynamic Collections)
| Event | Timing | Can Modify | Can Abort |
|---|---|---|---|
on_record_before_create | Before | Yes | Yes |
on_record_after_create | After | No | No |
on_record_before_update | Before | Yes | Yes |
on_record_after_update | After | No | No |
on_record_before_delete | Before | No | Yes |
on_record_after_delete | After | No | No |
on_record_before_query | Before | Yes | Yes |
on_record_after_query | After | Yes | No |
Auth Operations
| Event | Timing | Can Modify | Can Abort |
|---|---|---|---|
on_auth_before_login | Before | Yes | Yes |
on_auth_after_login | After | No | No |
on_auth_before_register | Before | Yes | Yes |
on_auth_after_register | After | No | No |
on_auth_before_logout | Before | No | Yes |
on_auth_after_logout | After | No | No |
on_auth_before_password_reset | Before | Yes | Yes |
on_auth_after_password_reset | After | No | No |
Collection Operations
| Event | Timing | Can Modify | Can Abort |
|---|---|---|---|
on_collection_before_create | Before | Yes | Yes |
on_collection_after_create | After | No | No |
on_collection_before_update | Before | Yes | Yes |
on_collection_after_update | After | No | No |
on_collection_before_delete | Before | No | Yes |
on_collection_after_delete | After | No | No |
Usage Guide
Decorator Registration (Recommended)
Available Decorator Methods
Record Operations:on_record_before_create(collection, priority=0)on_record_after_create(collection, priority=0)on_record_before_update(collection, priority=0)on_record_after_update(collection, priority=0)on_record_before_delete(collection, priority=0)on_record_after_delete(collection, priority=0)on_record_before_query(collection, priority=0)on_record_after_query(collection, priority=0)
on_collection_before_create(priority=0)on_collection_after_create(priority=0)on_collection_before_update(priority=0)on_collection_after_update(priority=0)on_collection_before_delete(priority=0)on_collection_after_delete(priority=0)
on_auth_before_login(priority=0)on_auth_after_login(priority=0)on_auth_before_register(priority=0)on_auth_after_register(priority=0)
HookContext Structure
Aborting Operations
UseAbortHookException to cancel an operation from a before_* hook:
Built-in Hooks
SnackBase includes 4 built-in hooks that provide core functionality. These hooks are always active and cannot be disabled.1. Timestamp Hook
Purpose: Automatically setcreated_at and updated_at timestamps.
Events: on_record_before_create, on_record_before_update
Priority: -100 (runs early)
2. Account Isolation Hook
Purpose: Enforce multi-tenancy by settingaccount_id from context.
Events: on_record_before_create
Priority: -200 (runs very early)
3. Created By Hook
Purpose: Track which user created/updated records. Events:on_record_before_create, on_record_before_update
Priority: -150
4. Audit Capture Hook
Purpose: Automatically capture audit log entries for record operations. Events:on_record_after_create, on_record_after_update, on_record_after_delete
Priority: 100 (runs after all user hooks)
This hook respects the
SNACKBASE_AUDIT_LOGGING_ENABLED configuration. When
disabled, no audit entries are captured.