BaseAggregate
Base class for aggregate root entities, the primary building block for modeling domain concepts that enforce consistency rules and manage state changes.
See Aggregates guide for practical usage and Aggregates concept for design rationale.
Bases: BaseEntity
Base class for aggregate roots. An aggregate owns a cluster of domain objects, enforces their consistency rules and marks a transaction boundary.
An aggregate has everything an entity has (fields, identity, invariants).
It adds a version for optimistic concurrency, raise_() for domain
events, and from_events() to rebuild an event-sourced aggregate. The
Aggregates guide covers usage.
Meta Options
| Option | Type | Description |
|---|---|---|
event_sourced |
bool |
Enable event-sourcing mode (default: False). |
fact_events |
bool |
Auto-generate fact events on persistence (default: False). |
stream_category |
str |
Override the event stream category name. |
database |
str |
The [databases.*] entry that persists it (default: "default"). |
schema_name |
str |
The storage table/collection name. |
abstract |
bool |
The class cannot be instantiated when True (default: False). |
auto_add_id_field |
bool |
Whether to auto-inject an id field (default: True). |
indexes |
tuple |
Index declarations for the persistence layer (default: ()). |
limit |
int |
Default query result limit (default: 100). |
suppress_checks |
tuple |
Diagnostic codes protean check should not report for this aggregate. |
reserved |
tuple[str, ...] |
Field names that once existed and must never be reused. Removing a field from an event-sourced aggregate is safe only when its name is reserved. During replay, an assignment to a reserved name is dropped, and so is a call to its add_<name>/remove_<name> helper. Child entities those calls would have added are dropped with them. |
Source code in src/protean/core/aggregate.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
raise_
raise_(event: Any) -> None
Raise a domain event on this aggregate.
Enriches the event with metadata (identity, stream, sequence,
checksum) and appends it to self._events.
On an event-sourced aggregate, the event's @apply handler then
runs. If any step raises (an enricher, the handler, or an invariant
check around it), _events, _version and _event_position
go back to their values from before the call and the error
propagates. When an invariant check fails, atomic_change also
undoes the field changes the handler made. When the handler raises
its own error, its field changes stay.
Source code in src/protean/core/aggregate.py
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | |
from_events
classmethod
from_events(events: list[Any]) -> Self
Event-Sourcing: reconstruct an aggregate from a list of events.
Creates a blank aggregate via _create_for_reconstitution() and
applies all events uniformly through _apply(). The first event's
@apply handler must set ALL fields including identity.
| RAISES | DESCRIPTION |
|---|---|
IncorrectUsageError
|
If |
Source code in src/protean/core/aggregate.py
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 | |