Skip to main content

hexastack-cqrs

Command Query Responsibility Segregation (CQRS) buses, execution pipelines, and middleware for Hexastack.

Python 3.13+


1. Overview & Capabilities

hexastack-cqrs powers the application logic layer of Hexastack applications, enforcing a strict separation between write operations (Commands) and read projections (Queries):

  • Synchronous & Asynchronous Buses: SynchronousCommandBus, SynchronousQueryBus, SynchronousEventBus, and async/distributed buses.
  • Composable Middleware Pipeline: Pluggable middlewares executed sequentially before and after command/query handling:
    • CorrelationMiddleware: Propagates correlation IDs across requests and async tasks.
    • TimingMiddleware: Measures and records execution latency.
    • LoggingMiddleware: Logs execution lifecycles and errors via LoggerPort (dynamically gated via features.cqrs.logging).
    • TenacityRetryMiddleware: Exponential backoff and retry policies powered by tenacity (dynamically gated via features.cqrs.retry).
    • UnitOfWorkMiddleware: Automatic transaction scoping (commit() on success, rollback() on failure).
    • ConditionalFeatureFlagMiddleware: Evaluates dynamic feature flags before dispatching commands/queries.
  • Declarative Decorators & Scanning: @command_handler, @query_handler, @event_handler, and @feature_flag registered via reflective module scanning.
  • Presenter & Response Mapping: Integration with PresenterPort for formatting outputs across CLI and REST adapters.

2. Package Anatomy & Key Components

hexastack_cqrs/
├── domain/          # Command, Query, Event, Handler protocols, CQRS exceptions
├── ports/           # CommandBusPort, QueryBusPort, EventBusPort, MiddlewarePort
├── adapters/        # Buses (Synchronous & Asynchronous with Huey)
└── infra/           # CqrsBootstrapper (order=20), Middleware pipeline, Registries, @feature_flag

Key Exports

Category Exports
Adapters SynchronousCommandBus, SynchronousQueryBus, SynchronousEventBus, HueyCommandBus, HueyEventBus
Bootstrap CqrsBootstrapper (order=20), CqrsConfig
Decorators @command_handler, @query_handler, @event_handler, @feature_flag, @presenter
Domain Command, Query, Event, CommandHandler, QueryHandler, EventHandler
Middlewares CorrelationMiddleware, TimingMiddleware, LoggingMiddleware, TenacityRetryMiddleware, UnitOfWorkMiddleware, ConditionalFeatureFlagMiddleware, ExecutionPipeline
Ports CommandBusPort, QueryBusPort, EventBusPort, MiddlewarePort

3. Monorepo & Sibling Relationships

graph TD
    subgraph DrivingAdapters ["Inbound Driving Adapters"]
        FASTAPI["hexastack-fastapi"]
        GRAPHQL["hexastack-graphql"]
        MCP["hexastack-mcp"]
        GRPC["hexastack-grpc"]
        CLI["hexastack-cli"]
    end

    subgraph CQRS ["hexastack-cqrs"]
        CBUS["CommandBusPort"]
        QBUS["QueryBusPort"]
        EBUS["EventBusPort"]
        PIPE["Middleware Pipeline"]
    end

    subgraph DrivenDependencies ["Driven Subsystems & Kernel"]
        CORE["hexastack-core (DI & Kernel)"]
        DB["hexastack-db (UnitOfWorkPort)"]
        LOG["hexastack-logging (LoggerPort)"]
    end

    FASTAPI -->|dispatches to| CBUS
    FASTAPI -->|dispatches to| QBUS
    GRAPHQL -->|dispatches to| CBUS
    GRAPHQL -->|dispatches to| QBUS
    MCP -->|dispatches to| CBUS
    MCP -->|dispatches to| QBUS
    GRPC -->|dispatches to| CBUS
    GRPC -->|dispatches to| QBUS
    CLI -->|dispatches to| CBUS
    CLI -->|dispatches to| QBUS

    CBUS --> PIPE
    QBUS --> PIPE
    EBUS --> PIPE

    PIPE -->|managed by| CORE
    PIPE -. consumes from DI .-> DB
    PIPE -. consumes from DI .-> LOG

Explicit Dependencies (Direct)

  • hexastack-core: Core kernel, DI container, base exceptions, and ports.
  • tenacity>=9.0.0: Resilient retry policies with exponential backoff.

Implied / Behavioral Relationships (DI-Mediated)

  • UnitOfWork Scoping: UnitOfWorkMiddleware dynamically resolves UnitOfWorkPort from the DI container (injected by hexastack-db) to manage transactions.
  • Telemetry Integration: LoggingMiddleware dynamically resolves LoggerPort from the DI container (injected by hexastack-logging).
  • Driving Adapters: Consumed by hexastack-fastapi and hexastack-cli to route incoming user actions to business handlers.

Optional Integrations (Extras)

  • [huey]: Enables asynchronous, distributed task queue execution using HueyCommandBus and HueyEventBus.

4. Installation

# Standalone installation
pip install hexastack-cqrs

# With asynchronous distributed background worker (Huey)
pip install "hexastack-cqrs[huey]"

# Via umbrella package
pip install hexastack

5. Configuration Reference

[hexastack.cqrs]
enable_correlation = true
enable_timing = true
enable_logging = true
enable_uow = true
retry_attempts = 3
retry_backoff_base = 0.5

6. Quickstart Example

from dataclasses import dataclass
from hexastack_core.infra.bootstrap import bootstrap
from hexastack_cqrs.domain.command import Command
from hexastack_cqrs.infra.decorators import command_handler
from hexastack_cqrs.ports.buses import CommandBusPort


@dataclass(frozen=True)
class CreateOrderCommand(Command):
    order_id: str
    amount: float


@command_handler(CreateOrderCommand)
class CreateOrderHandler:
    def __call__(self, cmd: CreateOrderCommand) -> str:
        return f"Order {cmd.order_id} created for ${cmd.amount:.2f}"


runtime = bootstrap(packages_to_scan=[__name__])
bus = runtime.container.get(CommandBusPort)

result = bus.dispatch(CreateOrderCommand(order_id="ord-99", amount=49.99))
print(result)  # "Order ord-99 created for $49.99"

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hexastack_cqrs-0.0.0.tar.gz (20.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hexastack_cqrs-0.0.0-py3-none-any.whl (38.5 kB view details)

Uploaded Python 3

File details

Details for the file hexastack_cqrs-0.0.0.tar.gz.

File metadata

  • Download URL: hexastack_cqrs-0.0.0.tar.gz
  • Upload date:
  • Size: 20.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for hexastack_cqrs-0.0.0.tar.gz
Algorithm Hash digest
SHA256 de2e37b892a8f8b8e62b0fd54a68206d4fb0eecc1a31a51147fe8454ef74c99f
MD5 6dc3afd5d7c3468fc7626e2f0ed38932
BLAKE2b-256 42c244b25ce6598daad1bc64db471efb92c3fce703a5970eebf1d0ba9b18fdc9

See more details on using hashes here.

File details

Details for the file hexastack_cqrs-0.0.0-py3-none-any.whl.

File metadata

  • Download URL: hexastack_cqrs-0.0.0-py3-none-any.whl
  • Upload date:
  • Size: 38.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for hexastack_cqrs-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ee82728d05e7625d96e250ee4d010701de94ba04a8ee9aa1203330141a99175a
MD5 be5e085b79265719add8dcbcc55682a9
BLAKE2b-256 a5845c2654eced349c9606990f3209fd6bba93efc57558e911551a98f0f5722a

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.0.0 This release

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page