BaseEventStore
Event store interface for event-sourced persistence. All event store adapters (Memory, MessageDB, etc.) implement this contract.
See Event Store Adapters for concrete adapter configuration.
This class outlines the base event store capabilities to be implemented in all supported event store adapters.
It is also a marker interface for registering event store classes with the domain.
Source code in src/protean/port/event_store.py
35 36 37 38 39 40 | |
close
close() -> None
Close the event store and release all connections.
Subclasses that hold external resources (connection pools, sockets, etc.) should override this to perform cleanup. The default implementation is a no-op so that adapters without external resources (e.g. the in-memory store) work without changes.
Source code in src/protean/port/event_store.py
42 43 44 45 46 47 48 49 | |
load_aggregate
load_aggregate(part_of: Type[BaseAggregate], identifier: str, *, at_version: int | None = None, as_of: datetime | None = None) -> Optional[BaseAggregate]
Load an aggregate from underlying events.
By default, reconstitutes the aggregate to its current (latest) state.
When at_version or as_of is provided, reconstitutes a historical
snapshot of the aggregate — a temporal query.
| 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 |
|---|---|
Optional[BaseAggregate]
|
The fully-formed aggregate, or |
Optional[BaseAggregate]
|
(and no temporal param was given that would raise instead). |
Source code in src/protean/port/event_store.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
create_snapshot
create_snapshot(part_of: Type[BaseAggregate], identifier: str) -> bool
Create a snapshot for a specific event-sourced aggregate instance.
Reads the full event stream for the aggregate, reconstructs it via
from_events(), and writes a snapshot to the snapshot stream.
This bypasses the snapshot threshold -- manual triggers always create
a snapshot regardless of event count.
| 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/port/event_store.py
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 | |
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/port/event_store.py
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 | |
trace_causation
trace_causation(message_id: str | Message) -> list[Message]
Walk UP the causation chain from a message to the root.
Returns an ordered list of Messages from the root command (first) to the given message (last). The given message itself is included.
| PARAMETER | DESCRIPTION |
|---|---|
message_id
|
A Protean message ID string (
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Message]
|
List of :class: |
list[Message]
|
target last). |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the message cannot be found in the event store. |
Source code in src/protean/port/event_store.py
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 612 613 614 | |
trace_effects
trace_effects(message_id: str | Message, *, recursive: bool = True) -> list[Message]
Walk DOWN the causation chain to find all effects of a message.
Returns messages that were caused by the given message, ordered by
global_position (chronological order).
| PARAMETER | DESCRIPTION |
|---|---|
message_id
|
A Protean message ID string (
TYPE:
|
recursive
|
If
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Message]
|
List of :class: |
list[Message]
|
in chronological order. The given message itself is NOT included. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the message cannot be found in the event store. |
Source code in src/protean/port/event_store.py
616 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 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 | |
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 |
|---|---|
Root
|
class:
TYPE:
|
CausationNode | None
|
messages found. |
Source code in src/protean/port/event_store.py
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 | |
CausationNode
Tree node used by build_causation_tree() to represent the causation
hierarchy of messages sharing a correlation_id.