~docbibi/pyeventsourcing-contrib

ea7b85ea7caaa5c644df370c1557e40f068e7c65 — Borjan Tchakaloff 1 year, 7 months ago 2f5c711
Introduce the QuickRepo subclass
1 files changed, 38 insertions(+), 0 deletions(-)

A eventsourcing_contrib/repositories.py
A eventsourcing_contrib/repositories.py => eventsourcing_contrib/repositories.py +38 -0
@@ 0,0 1,38 @@
import uuid

import eventsourcing.application


class QuickRepo(eventsourcing.application.Repository):
    """A quicker :class:`~eventsourcing.application.Repository`.

    There is one trade-off to be aware of: the repository should not
    store "deleted" aggregates.

    Soft-deleted aggregates will still appear in the repository, by
    definition. There are regular aggregates with a special event to
    mark them as deleted. Since the aggregates are still stored in the
    repository, the identifiers of such aggregates will pass the
    containment test::

        >>> my_agg_id: uuid.UUID
        >>> app: eventsourcing.application.Application
        >>> ...  # soft delete the aggregate
        >>> my_agg_id in app.repository
        True
    """

    def __contains__(self, item: uuid.UUID) -> bool:
        """Whether an aggregate exists in the repository.

        The current state of the aggregate is not relevant here, only
        that there is at least one creation event about the aggregate
        identifier in the repository.
        """
        if self.cache and self.cache.get(item):
            return True
        return (
            self.event_store.recorder.select_events(
                originator_id=item, gt=1, lte=1, limit=1
            )
        ) != []