castlecraft_engineer.abstractions.event_store
¶
castlecraft_engineer.abstractions.event_store
¶
EventStore
¶
Bases: Generic[TAggregateId]
, ABC
Abstract base class for an event store, responsible for persisting and retrieving streams of domain events.
Source code in src/castlecraft_engineer/abstractions/event_store.py
append_events(aggregate_id, expected_version, events)
abstractmethod
async
¶
Appends a list of events to the stream for a given aggregate.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
aggregate_id
|
TAggregateId
|
The ID of the aggregate to which the events belong. |
required |
expected_version
|
int
|
The version of the aggregate that these events are based on. Used for optimistic concurrency control. If the current version in the store does not match this, an EventStoreConflictError should be raised. |
required |
events
|
List[Event]
|
A list of domain event instances to append. |
required |
Raises:
Type | Description |
---|---|
EventStoreConflictError
|
If the expected_version does not match the current version of the event stream for the aggregate. |
Exception
|
Implementation-specific exceptions related to storage failures. |
Source code in src/castlecraft_engineer/abstractions/event_store.py
get_current_version(aggregate_id)
abstractmethod
async
¶
Retrieves the current version of the event stream for a given aggregate.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
aggregate_id
|
TAggregateId
|
The ID of the aggregate. |
required |
Returns:
Type | Description |
---|---|
Optional[int]
|
The current version (number of events - 1, or sequence of last event) |
Optional[int]
|
or None if the aggregate stream doesn't exist. |
Source code in src/castlecraft_engineer/abstractions/event_store.py
load_events(aggregate_id, from_version=None)
abstractmethod
async
¶
Loads the stream of events for a given aggregate.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
aggregate_id
|
TAggregateId
|
The ID of the aggregate whose events are to be loaded. |
required |
from_version
|
Optional[int]
|
Optionally, the version from which to start loading events. If None, loads all events for the aggregate. |
None
|
Returns:
Type | Description |
---|---|
List[Event]
|
A list of domain event instances, ordered by their sequence. |
List[Event]
|
Returns an empty list if the aggregate has no events or doesn't exist. |
Source code in src/castlecraft_engineer/abstractions/event_store.py
EventStoreConflictError
¶
Bases: RuntimeError
Raised when there's a conflict appending events, e.g., due to version mismatch.