EventStorePort
Event store interface for event-sourced persistence. Every event store adapter
(Memory, MessageDB, etc.) implements EventStorePort, and domain.event_store
is the EventStore service that calls it.
See Event Store Adapters for concrete adapter configuration, and Write an Event-Store Adapter for a worked adapter that implements this Port.
EventStorePort
The typing.Protocol every event store adapter implements. The EventStore
service builds the event store's reads, writes, aggregate loading and
snapshots on these members.
Bases: Protocol
The operations an event-store adapter implements.
Every read includes the row at position. A stream read pages by the
stream position. A category read and an all read page by
global_position (ADR-0024). A category read returns only streams whose
category equals category exactly, and never a snapshot stream
(<category>:snapshot-<id>).
Build the adapter named name for domain from config.
Source code in src/protean/port/event_store.py
96 97 | |
capabilities
abstractmethod
property
capabilities: EventStoreCapabilities
The guarantees this event store makes.
write_message
abstractmethod
write_message(
stream_name: str,
message_type: str,
data: Mapping[str, Any],
metadata: Mapping[str, Any] | None = None,
expected_version: int | None = None,
) -> int
Append a message to stream_name and return its stream position.
| RAISES | DESCRIPTION |
|---|---|
ExpectedVersionError
|
When |
Source code in src/protean/port/event_store.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | |
get_stream_messages
abstractmethod
get_stream_messages(
stream_name: str, position: int = 0, limit: int = 1000
) -> list[RawMessage]
Return up to limit rows of one stream from stream position.
Source code in src/protean/port/event_store.py
120 121 122 123 124 | |
get_category_messages
abstractmethod
get_category_messages(
category: str,
position: int = 0,
limit: int = 1000,
*,
consumer_group_member: int | None = None,
consumer_group_size: int | None = None,
) -> list[RawMessage]
Return up to limit rows of category from global_position.
With consumer_group_member and consumer_group_size, return only
the streams that belong to that member of the group.
| RAISES | DESCRIPTION |
|---|---|
NotSupportedError
|
When a consumer-group argument is given and the
adapter does not declare |
ValueError
|
When an adapter that declares |
Source code in src/protean/port/event_store.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
get_all_messages
abstractmethod
get_all_messages(
position: int = 0, limit: int = 1000
) -> list[RawMessage]
Return up to limit rows of the whole store from global_position.
Source code in src/protean/port/event_store.py
148 149 150 151 152 | |
get_last_stream_message
abstractmethod
get_last_stream_message(
stream_name: str,
) -> RawMessage | None
Return the newest row of one stream, or None when it is empty.
Source code in src/protean/port/event_store.py
154 155 156 | |
get_last_category_message
abstractmethod
get_last_category_message(
category: str,
) -> RawMessage | None
Return the row of category with the highest global_position.
Returns None when the category is empty.
Source code in src/protean/port/event_store.py
158 159 160 161 162 163 | |
get_last_message
abstractmethod
get_last_message() -> RawMessage | None
Return the row with the highest global_position in the store.
Returns None when the store is empty.
Source code in src/protean/port/event_store.py
165 166 167 168 169 170 | |
stream_ids
abstractmethod
stream_ids(category: str) -> list[str]
Return the sorted, unique identifiers of the streams in category.
A stream <category>-<id> gives <id>. Snapshot streams are left
out.
Source code in src/protean/port/event_store.py
172 173 174 175 176 177 178 | |
ping
abstractmethod
ping() -> bool
Return whether the store answers. Never raises.
Source code in src/protean/port/event_store.py
180 181 182 | |
health
abstractmethod
health() -> EventStoreHealth
Return the store's health record.
Source code in src/protean/port/event_store.py
184 185 186 | |
get_connection
abstractmethod
get_connection() -> object
Return the native client, or None for an adapter without one.
Source code in src/protean/port/event_store.py
188 189 190 | |
clear
abstractmethod
clear() -> None
Delete every message. Test-harness use only.
Source code in src/protean/port/event_store.py
192 193 194 | |
close
close() -> None
Release the store's connections. The default does nothing.
Source code in src/protean/port/event_store.py
196 197 198 | |
create_schema
create_schema() -> None
Create the store's tables, behind SCHEMA_MANAGEMENT.
The default does nothing.
Source code in src/protean/port/event_store.py
200 201 202 203 204 205 | |
drop_schema
drop_schema() -> None
Drop the store's tables, behind SCHEMA_MANAGEMENT.
The default does nothing.
Source code in src/protean/port/event_store.py
207 208 209 210 211 212 | |
EventStoreCapabilities
The guarantees an event store declares on its capabilities property.
Bases: Flag
The guarantees an event store makes.
Each event store declares its flags on capabilities. Check a guarantee
with EventStoreCapabilities.DURABLE in store.capabilities.
RawMessage
The row an event store returns from a read.
Bases: TypedDict
One row as an event store returns it.
position is the row's place in its stream, from 0 with no gaps.
global_position is its place in the whole store and may have gaps. A
snapshot row has type == "SNAPSHOT" and metadata of None.
EventStoreHealth
What health() reports: whether the store answered, and its connection pool
counts when the store has a pool.
The health of an event store.
reachable is the result of ping(). pool describes the
adapter's connection pool, or is None for an adapter without one or
whose pool does not exist yet.
EventStore
The service that domain.event_store returns. It holds the domain's adapter
and provides reads, writes, aggregate loading, snapshots, causation tracing and
the integrity check.
The framework's view of the domain's event-store adapter.
domain.event_store returns one. The adapter is built by domain.init();
until then _adapter is None.
| RAISES | DESCRIPTION |
|---|---|
ConfigurationError
|
From every feature method and forwarder that reaches
the adapter, when |
Source code in src/protean/infra/event_store.py
231 232 233 | |
close
close() -> None
Close the event store and release all connections.
Delegates to the adapter's close() without clearing the reference, so the service remains usable (e.g., in test mode where the domain is still accessed after Engine.shutdown()).
Source code in src/protean/infra/event_store.py
235 236 237 238 239 240 241 242 243 244 | |
clear
clear() -> None
Delete every message. Test-harness use only.
Source code in src/protean/infra/event_store.py
286 287 288 | |
ping
ping() -> bool
Return whether the store answers.
Source code in src/protean/infra/event_store.py
290 291 292 | |
health
health() -> EventStoreHealth
Return the store's health record.
Source code in src/protean/infra/event_store.py
294 295 296 | |
get_connection
get_connection() -> object
Return the adapter's native client, or None for an adapter without one.
Source code in src/protean/infra/event_store.py
298 299 300 | |
category
staticmethod
category(stream: str) -> str
Return the category of stream: the part before the first -.
Returns "" for an empty or None stream name.
Source code in src/protean/infra/event_store.py
367 368 369 370 371 372 373 374 375 376 377 | |
read
read(
stream: str, position: int = 0, limit: int = 1000
) -> list[Message]
Return up to limit messages of stream from position.
stream is $all, a category, or a category-id stream. Snapshot
rows count toward limit and are left out of the result.
Source code in src/protean/infra/event_store.py
379 380 381 382 383 384 385 386 387 388 | |
read_all
read_all(
stream: str = "$all", *, page_size: int = 1000
) -> Iterator[Message]
Yield every message in stream, reading the store in pages of page_size rows.
A single read returns at most limit rows, so use this for
a read that must be complete, such as a projection rebuild or a backup.
Memory use stays bounded whatever the size of the stream. Snapshot rows
are skipped, so only events and commands are yielded.
| PARAMETER | DESCRIPTION |
|---|---|
stream
|
The stream to read.
TYPE:
|
page_size
|
Number of rows to read from the store per page.
TYPE:
|
| YIELDS | DESCRIPTION |
|---|---|
Message
|
Every |
Message
|
no duplicates across page boundaries. |
| RAISES | DESCRIPTION |
|---|---|
IncorrectUsageError
|
If |
Source code in src/protean/infra/event_store.py
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 | |
read_last_message
read_last_message(stream: str) -> Message | None
Return the newest event or command of stream, or None when it has none.
Source code in src/protean/infra/event_store.py
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 | |
category_head_position
category_head_position(category: str) -> int
Return the global_position of the newest row in category.
For $all the row can be a snapshot row, because this reads raw rows
and skips nothing.
Subscription lag monitoring uses it to find how far a subscription is behind the head of its category.
| PARAMETER | DESCRIPTION |
|---|---|
category
|
A category, such as
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
The |
int
|
category has no rows. |
Source code in src/protean/infra/event_store.py
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 | |
append
append(message_object: BaseEvent | BaseCommand) -> int
Write an event or command to its stream and return its stream position.
| RAISES | DESCRIPTION |
|---|---|
ExpectedVersionError
|
When the message carries an expected version that is not the stream's current version. Nothing is written. |
Source code in src/protean/infra/event_store.py
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 | |
load_aggregate
load_aggregate(
part_of: type[BaseAggregate],
identifier: str,
*,
at_version: int | None = None,
as_of: datetime | None = None,
) -> BaseAggregate | None
Load an aggregate by replaying its events.
By default, rebuilds the aggregate in its current (latest) state. When
at_version or as_of is given, rebuilds its state at that point
in history.
| PARAMETER | DESCRIPTION |
|---|---|
part_of
|
The EventSourced Aggregate's class.
TYPE:
|
identifier
|
Unique aggregate identifier.
TYPE:
|
at_version
|
Reconstitute to this exact version (0-indexed). Version 0 is the state after the first event.
TYPE:
|
as_of
|
Reconstitute the aggregate as of this timestamp.
Only events written on or before
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
BaseAggregate | None
|
The aggregate, or |
| RAISES | DESCRIPTION |
|---|---|
ObjectNotFoundError
|
If |
Source code in src/protean/infra/event_store.py
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 | |
create_snapshot
create_snapshot(
part_of: type[BaseAggregate], identifier: str
) -> bool
Create a snapshot for a specific event-sourced aggregate instance.
Rebuilds the aggregate from its full event stream and writes its current state to the snapshot stream. The snapshot is written whatever the snapshot threshold.
| PARAMETER | DESCRIPTION |
|---|---|
part_of
|
The EventSourced Aggregate class.
TYPE:
|
identifier
|
Unique aggregate identifier.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if a snapshot was created. |
| RAISES | DESCRIPTION |
|---|---|
IncorrectUsageError
|
If the aggregate is not event-sourced. |
ObjectNotFoundError
|
If no events exist for the given identifier. |
Source code in src/protean/infra/event_store.py
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 | |
create_snapshots
create_snapshots(part_of: type[BaseAggregate]) -> int
Create snapshots for all instances of an event-sourced aggregate.
Discovers all unique aggregate identifiers in the stream category, then creates a snapshot for each.
| PARAMETER | DESCRIPTION |
|---|---|
part_of
|
The EventSourced Aggregate class
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
Number of snapshots created. |
| RAISES | DESCRIPTION |
|---|---|
IncorrectUsageError
|
If the aggregate is not event-sourced. |
Source code in src/protean/infra/event_store.py
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 | |
trace_causation
trace_causation(message_id: str | Message) -> list[Message]
Return the causation chain from the root message down to message_id.
| PARAMETER | DESCRIPTION |
|---|---|
message_id
|
A Protean message ID string (
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Message]
|
|
list[Message]
|
last. Empty when |
list[Message]
|
hold. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
|
Source code in src/protean/infra/event_store.py
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 | |
trace_effects
trace_effects(
message_id: str | Message, *, recursive: bool = True
) -> list[Message]
Return the messages caused by message_id.
| PARAMETER | DESCRIPTION |
|---|---|
message_id
|
A Protean message ID string (
TYPE:
|
recursive
|
If
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Message]
|
|
list[Message]
|
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
|
Source code in src/protean/infra/event_store.py
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 | |
build_causation_tree
build_causation_tree(
correlation_id: str,
) -> CausationNode | None
Build a full causation tree for a correlation ID.
Returns the root node of the tree with children recursively populated.
| PARAMETER | DESCRIPTION |
|---|---|
correlation_id
|
The correlation ID to trace.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
CausationNode | None
|
Root |
CausationNode | None
|
children, or |
Source code in src/protean/infra/event_store.py
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 | |
verify
verify() -> IntegrityReport
Check the store's internal invariants and report every violation.
Reads the whole store and changes nothing. See
protean.infra.integrity.verify for
the invariants it checks.
| RETURNS | DESCRIPTION |
|---|---|
IntegrityReport
|
An |
IntegrityReport
|
the violations found. |
Source code in src/protean/infra/event_store.py
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 | |
CausationNode
Tree node used by build_causation_tree() to represent the causation
hierarchy of messages sharing a correlation_id.
verify
The integrity check that domain.event_store.verify() runs. Pass it an
adapter to check one that is not the domain's.
Check the store's internal invariants and report every violation.
Reads the whole store through $all, then reads the snapshot of each
stream with a - in its name, other than :snapshot- streams,
whose snapshot $all did not return. It changes
nothing. It checks that:
- every row carries
id,stream_nameandposition, - each stream's
positionruns from 0 with no gaps, global_positionstrictly increases across the store,- message ids are unique,
- every row outside a
:snapshot-stream has a body that is an object (datais a dict, not NULL, an array or a scalar), - each
:snapshot-stream holds a well-formed snapshot whose_versionis not past the head of its aggregate stream.
A corrupt row, such as one missing a required field, a message with a
NULL or non-object body or a snapshot with a non-integer _version,
is reported as a violation and never skipped. A NULL or non-object body
is reported as malformed_message and still counts as its stream's
position.
| PARAMETER | DESCRIPTION |
|---|---|
adapter
|
The event store to check.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
IntegrityReport
|
An |
IntegrityReport
|
when there are none. |
Source code in src/protean/infra/integrity.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | |
IntegrityReport
The result of verify(), and the data payload of
protean eventstore verify --json.
The result of verify.
message_count is every message scanned; stream_count is the number of
streams that held at least one positioned message. ok is derived: a
report is ok exactly when it carries no violations, so the two can never
disagree. This shape (with ok included) is the documented, stable contract
behind protean eventstore verify --json.
ok
property
ok: bool
Whether the scan found no violations.
as_dict
as_dict() -> dict[str, Any]
Serialize to the --json data payload, ok included.
dataclasses.asdict omits the ok property, so build the wire
payload explicitly to keep data.ok in the contract.
Source code in src/protean/infra/integrity.py
76 77 78 79 80 81 82 83 84 85 86 87 | |
IntegrityViolation
One broken invariant that verify() found.
A single violation of the event store's internal invariants.
kind names the invariant that broke; stream and position name
where the violation was found (either may be None when it is not
stream- or position-specific); detail is a human-readable explanation.
ViolationKind
The kinds of IntegrityViolation. Each value is the string that
--json writes.
Bases: StrEnum
The kind of an IntegrityViolation.
Each value is a stable machine token in the protean eventstore verify
--json output.