Skip to content

AsyncBackend

Bases: _BackendContainer

Dependency Injection container for async Event Sourcery components.

Async counterpart of Backend. Registers async counterparts of event store, outbox, subscription and encryption components.

Source code in event_sourcery/_event_store/_async/backend.py
class AsyncBackend(_BackendContainer):
    """
    Dependency Injection container for async Event Sourcery components.

    Async counterpart of `Backend`. Registers async counterparts of event
    store, outbox, subscription and encryption components.
    """

    def __init__(self) -> None:
        super().__init__()
        self[AsyncEncryption] = lambda c: AsyncEncryption(
            registry=c[EventRegistry],
            strategy=c[EncryptionStrategy],
            key_storage=c[AsyncEncryptionKeyStorageStrategy],
        )
        self[AsyncEncryptionKeyStorageStrategy] = (
            lambda c: AsyncNoKeyStorageStrategy().scoped_for_tenant(c[TenantId])
        )
        self[AsyncStorageStrategy] = not_configured(DEFAULT_SAS)
        self[AsyncSubscriptionStrategy] = not_configured(DEFAULT_SAS)
        self[AsyncOutboxStorageStrategy] = lambda _: AsyncNoOutboxStorageStrategy()
        self[AsyncEventStore] = lambda c: AsyncEventStore(
            storage_strategy=c[AsyncStorageStrategy],
            serde=self._serde_for(c),
            context_enrichers=c[ContextEnrichers],
        )
        self[AsyncOutbox] = lambda c: AsyncOutbox(
            strategy=c[AsyncOutboxStorageStrategy],
            serde=self._serde_for(c),
        )
        self[AsyncPositionPhase] = lambda c: AsyncSubscriptionBuilder(
            self._serde_for(c),
            c[AsyncSubscriptionStrategy],
        )

    @staticmethod
    def _serde_for(container: _Container) -> AsyncSerde:
        return AsyncSerde(
            registry=container[EventRegistry],
            encryption=container[AsyncEncryption],
        )

    def with_outbox(self, filterer: OutboxFiltererStrategy = no_filter) -> Self:
        """
        Configure the outbox with a custom filter.
        """
        raise NotImplementedError()

    def with_encryption(
        self,
        strategy: EncryptionStrategy,
        key_storage: AsyncEncryptionKeyStorageStrategy,
    ) -> Self:
        """
        Configures event encryption with the provided strategy and key storage.

        Key storage operations are asynchronous.
        """
        self[EncryptionStrategy] = strategy
        self[AsyncEncryptionKeyStorageStrategy] = (
            lambda c: key_storage.scoped_for_tenant(c[TenantId])
        )
        return self

    @property
    def event_store(self) -> AsyncEventStore:
        """
        Returns the current instance of `AsyncEventStore`.
        """
        return self[AsyncEventStore]

    @property
    def outbox(self) -> AsyncOutbox:
        """
        Returns the current instance of `AsyncOutbox`.
        """
        return self[AsyncOutbox]

    @property
    def subscriber(self) -> AsyncPositionPhase:
        """
        Returns the current instance of `AsyncSubscriptionBuilder`
        (as `AsyncPositionPhase`).
        """
        return self[AsyncPositionPhase]

event_store property

Returns the current instance of AsyncEventStore.

outbox property

Returns the current instance of AsyncOutbox.

subscriber property

Returns the current instance of AsyncSubscriptionBuilder (as AsyncPositionPhase).

with_encryption(strategy, key_storage)

Configures event encryption with the provided strategy and key storage.

Key storage operations are asynchronous.

Source code in event_sourcery/_event_store/_async/backend.py
def with_encryption(
    self,
    strategy: EncryptionStrategy,
    key_storage: AsyncEncryptionKeyStorageStrategy,
) -> Self:
    """
    Configures event encryption with the provided strategy and key storage.

    Key storage operations are asynchronous.
    """
    self[EncryptionStrategy] = strategy
    self[AsyncEncryptionKeyStorageStrategy] = (
        lambda c: key_storage.scoped_for_tenant(c[TenantId])
    )
    return self

with_outbox(filterer=no_filter)

Configure the outbox with a custom filter.

Source code in event_sourcery/_event_store/_async/backend.py
def with_outbox(self, filterer: OutboxFiltererStrategy = no_filter) -> Self:
    """
    Configure the outbox with a custom filter.
    """
    raise NotImplementedError()