Skip to content

Handlers

Command handlers, event handlers, and query handlers process messages. Command and event handlers are associated with aggregates and use the @handle decorator. Query handlers are associated with projections and use the @read decorator.

Guides: Command Handlers · Event Handlers · Query Handlers


BaseCommandHandler

Bases: Element, HandlerMixin, OptionsMixin

Base class for command handlers. A command handler receives the commands for one aggregate and changes the aggregate's state.

Each method marked @handle(SomeCommand) processes one command class. Subscription options control how the server reads the handler's commands. The Command Handlers guide covers usage.

Meta Options

Option Type Description
part_of type The aggregate this handler is associated with. Required.
stream_category str Read-only. Derived from the associated aggregate's stream category.
sequential_by str Name of a command attribute. Commands with the same value for it are processed one at a time, in order.
subscription_type SubscriptionType or str The subscription type (STREAM or EVENT_STORE), as the enum member or its name.
subscription_profile SubscriptionProfile or str A built-in profile (PRODUCTION, FAST, BATCH, DEBUG, PROJECTION) as the enum member or its name, or a custom profile name from [server.profiles].
subscription_config dict Custom configuration overrides that take precedence over profile defaults.

Configuration Priority (highest to lowest):

1. Handler Meta subscription_config
2. Handler Meta subscription_profile
3. Handler Meta subscription_type
4. Server-level handler-specific config
5. Server-level defaults
6. Profile defaults
7. Hardcoded defaults

Example::

@domain.command_handler(
    part_of=Order,
    subscription_profile=SubscriptionProfile.PRODUCTION,
    subscription_config={"messages_per_tick": 50},
)
class OrderCommandHandler(BaseCommandHandler):
    @handle(PlaceOrder)
    def handle_place_order(self, command):
        pass

BaseEventHandler

Bases: Element, HandlerMixin, OptionsMixin

Base class for event handlers, which process domain events.

Handlers run in the server engine. When event_processing is "sync", they run instead when the Unit of Work commits, before it returns.

Meta Options

Option Type Description
part_of type The aggregate this handler is associated with.
source_stream str Optional filter on the category of the stream that triggered an event. A value without :: gets the domain prefix.
stream_category str The stream category to subscribe to. Defaults to aggregate's category. A value without :: gets the domain prefix (all_orders becomes <domain>::all_orders); $all stays as written.
sequential_by str Partition key field name for per-key sequential processing.
subscription_type SubscriptionType or str The subscription type (STREAM or EVENT_STORE), as the enum member or its name.
subscription_profile SubscriptionProfile or str A built-in profile (PRODUCTION, FAST, BATCH, DEBUG, PROJECTION) as the enum member or its name, or a custom profile name from [server.profiles].
subscription_config dict Custom configuration overrides that take precedence over profile defaults.
retries int Maximum retries when the handler fails with a transient exception. Overrides the domain's server.transient_retry setting; None uses that setting.
backoff str Delay strategy between transient retries: "exponential", "linear" or "fixed". None uses the domain setting.
retry_exceptions tuple Exception types treated as transient, as classes or dotted paths. None uses the domain setting.
suppress_checks tuple Diagnostic codes protean check should not report for this handler.

The subscription options combine with server-level settings in a fixed priority order. See the server configuration reference for that order, and the event handlers guide for a walk-through.

Example::

@domain.event_handler(
    part_of=Order,
    subscription_profile=SubscriptionProfile.PRODUCTION,
    subscription_config={"messages_per_tick": 50},
)
class OrderEventHandler(BaseEventHandler):
    @handle(OrderPlaced)
    def handle_order_placed(self, event):
        pass

BaseQueryHandler

Bases: Element, HandlerMixin, OptionsMixin

Base class for query handlers.

A query handler processes queries synchronously and returns the result. It names its projection with part_of. See the Query Handlers guide for a walk-through.

Meta Options

Option Type Description
part_of type The projection this handler is associated with. Required.
Note

Query handlers are synchronous only. They have no stream category, no subscription configuration, and no UnitOfWork wrapping.

Example::

@domain.query_handler(part_of=OrderSummary)
class OrderSummaryQueryHandler(BaseQueryHandler):
    @read(GetOrdersByCustomer)
    def get_by_customer(self, query):
        view = current_domain.view_for(OrderSummary)
        return view.query.filter(
            customer_id=query.customer_id
        ).all()