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
44 45 46 47 | |
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
49 50 51 52 53 54 55 56 | |
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
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
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
402 403 404 405 406 407 408 409 410 411 412 413 414 415 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 441 442 443 444 445 446 | |
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
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 | |
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
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 667 | |
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
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 | |
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
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 | |
stream_head_position
stream_head_position(stream_category: str) -> int
Return the global_position of the newest message in a category stream.
Public wrapper around :meth:_stream_head_position.
| PARAMETER | DESCRIPTION |
|---|---|
stream_category
|
The stream category to check.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
The |
int
|
stream has no messages. |
Source code in src/protean/port/event_store.py
815 816 817 818 819 820 821 822 823 824 825 826 827 | |
CausationNode
Tree node used by build_causation_tree() to represent the causation
hierarchy of messages sharing a correlation_id.