Skip to main content
Version: 1.0 (Stable API) Status: Production Ready
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 signature
  • HookRegistry.trigger() method signature
  • HookRegistry.unregister() method signature
  • HookContext dataclass structure
  • AbortHookException behavior
  • Hook event naming convention
  • Priority-based execution order
  • Tag-based filtering mechanism
  • Built-in hook behavior
Additive Changes (non-breaking):
  • New hook events
  • New hook categories
  • New HookContext fields (optional)
  • New built-in hooks
  • New utility functions

Hook Categories

Hooks are organized into 8 categories:

Hook Events

Naming Convention

All hook events follow a consistent pattern:
Timing:
  • 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)

Auth Operations

Collection Operations

Usage Guide

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)
Collection Operations:
  • 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)
Auth Operations:
  • 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

Use AbortHookException 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 set created_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 setting account_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.

Built-in Hook Execution Order

Creating Custom Hooks

Example 1: Data Validation

Example 2: Data Transformation

Example 3: Computed Fields

Example 4: Notifications

Advanced Features

Priority-Based Execution

Hooks execute in priority order (higher priority = earlier execution):

Tag-Based Filtering

Target specific collections or resources:

Error Handling

Hooks can fail without crashing the system:

Best Practices

1. Keep Hooks Focused

2. Use Appropriate Priorities

3. Handle Errors Gracefully

API Reference

HookRegistry

AbortHookException