Skip to content

AsyncStorageStrategy

Interface for async event store backends.

Async counterpart of StorageStrategy. Defines the contract for low-level operations on event streams, such as fetching, inserting, and deleting events. All operations that perform I/O are coroutines.

Source code in event_sourcery/_event_store/_async/event_store.py
class AsyncStorageStrategy:
    """
    Interface for async event store backends.

    Async counterpart of `StorageStrategy`. Defines the contract for low-level
    operations on event streams, such as fetching, inserting, and deleting events.
    All operations that perform I/O are coroutines.
    """

    async def fetch_events(
        self,
        stream_id: StreamId,
        start: int | None = None,
        stop: int | None = None,
    ) -> list[RawEvent]:
        """
        Fetches events from a stream in the given range.

        Args:
            stream_id (StreamId): The stream identifier to fetch events from.
            start (int | None): From version (inclusive), or None for the beginning.
            stop (int | None): Stop before version (exclusive), or None for the end.

        Returns:
            list[RawEvent]: List of raw events in the specified range.
        """
        raise NotImplementedError()

    async def insert_events(
        self,
        stream_id: StreamId,
        versioning: Versioning,
        events: list[RawEvent],
    ) -> None:
        """
        Inserts events into a stream with using versioning strategy.

        Args:
            stream_id (StreamId): The stream identifier to insert events into.
            versioning (Versioning): Versioning strategy for optimistic locking.
            events (list[RawEvent]): List of raw events to insert.
        """
        raise NotImplementedError()

    async def save_snapshot(self, snapshot: RawEvent) -> None:
        """
        Saves a snapshot of the stream. Stream will be fetched from newest snapshot.

        Args:
            snapshot (RawEvent): The snapshot event to save.
        """
        raise NotImplementedError()

    async def delete_stream(self, stream_id: StreamId) -> None:
        """
        Deletes a stream and all its events.

        Args:
            stream_id (StreamId): The stream identifier to delete.
        """
        raise NotImplementedError()

    async def current_position(self) -> Position | None:
        """
        Returns the current position (offset) in the event store, if supported.

        Unlike its synchronous counterpart, this is a coroutine, as fetching
        the position performs I/O in real backends.
        """
        raise NotImplementedError()

    def scoped_for_tenant(self, tenant_id: str) -> Self:
        """
        Returns a backend instance scoped for the given tenant.

        Args:
            tenant_id (str): The tenant identifier.

        Returns:
            Self: The backend instance for the tenant.
        """
        raise NotImplementedError()

current_position() async

Returns the current position (offset) in the event store, if supported.

Unlike its synchronous counterpart, this is a coroutine, as fetching the position performs I/O in real backends.

Source code in event_sourcery/_event_store/_async/event_store.py
async def current_position(self) -> Position | None:
    """
    Returns the current position (offset) in the event store, if supported.

    Unlike its synchronous counterpart, this is a coroutine, as fetching
    the position performs I/O in real backends.
    """
    raise NotImplementedError()

delete_stream(stream_id) async

Deletes a stream and all its events.

Parameters:

Name Type Description Default
stream_id StreamId

The stream identifier to delete.

required
Source code in event_sourcery/_event_store/_async/event_store.py
async def delete_stream(self, stream_id: StreamId) -> None:
    """
    Deletes a stream and all its events.

    Args:
        stream_id (StreamId): The stream identifier to delete.
    """
    raise NotImplementedError()

fetch_events(stream_id, start=None, stop=None) async

Fetches events from a stream in the given range.

Parameters:

Name Type Description Default
stream_id StreamId

The stream identifier to fetch events from.

required
start int | None

From version (inclusive), or None for the beginning.

None
stop int | None

Stop before version (exclusive), or None for the end.

None

Returns:

Type Description
list[RawEvent]

list[RawEvent]: List of raw events in the specified range.

Source code in event_sourcery/_event_store/_async/event_store.py
async def fetch_events(
    self,
    stream_id: StreamId,
    start: int | None = None,
    stop: int | None = None,
) -> list[RawEvent]:
    """
    Fetches events from a stream in the given range.

    Args:
        stream_id (StreamId): The stream identifier to fetch events from.
        start (int | None): From version (inclusive), or None for the beginning.
        stop (int | None): Stop before version (exclusive), or None for the end.

    Returns:
        list[RawEvent]: List of raw events in the specified range.
    """
    raise NotImplementedError()

insert_events(stream_id, versioning, events) async

Inserts events into a stream with using versioning strategy.

Parameters:

Name Type Description Default
stream_id StreamId

The stream identifier to insert events into.

required
versioning Versioning

Versioning strategy for optimistic locking.

required
events list[RawEvent]

List of raw events to insert.

required
Source code in event_sourcery/_event_store/_async/event_store.py
async def insert_events(
    self,
    stream_id: StreamId,
    versioning: Versioning,
    events: list[RawEvent],
) -> None:
    """
    Inserts events into a stream with using versioning strategy.

    Args:
        stream_id (StreamId): The stream identifier to insert events into.
        versioning (Versioning): Versioning strategy for optimistic locking.
        events (list[RawEvent]): List of raw events to insert.
    """
    raise NotImplementedError()

save_snapshot(snapshot) async

Saves a snapshot of the stream. Stream will be fetched from newest snapshot.

Parameters:

Name Type Description Default
snapshot RawEvent

The snapshot event to save.

required
Source code in event_sourcery/_event_store/_async/event_store.py
async def save_snapshot(self, snapshot: RawEvent) -> None:
    """
    Saves a snapshot of the stream. Stream will be fetched from newest snapshot.

    Args:
        snapshot (RawEvent): The snapshot event to save.
    """
    raise NotImplementedError()

scoped_for_tenant(tenant_id)

Returns a backend instance scoped for the given tenant.

Parameters:

Name Type Description Default
tenant_id str

The tenant identifier.

required

Returns:

Name Type Description
Self Self

The backend instance for the tenant.

Source code in event_sourcery/_event_store/_async/event_store.py
def scoped_for_tenant(self, tenant_id: str) -> Self:
    """
    Returns a backend instance scoped for the given tenant.

    Args:
        tenant_id (str): The tenant identifier.

    Returns:
        Self: The backend instance for the tenant.
    """
    raise NotImplementedError()