protean snapshot
The protean snapshot command group creates snapshots for event-sourced
aggregates. A snapshot captures aggregate state at a point in time to make
hydration faster. Instead of replaying the entire event history, Protean loads
the aggregate from the latest snapshot and replays only the events that follow
it.
Protean auto-creates snapshots when the event count exceeds the
snapshot_threshold (default 10). You can also create snapshots by hand for:
- Admin maintenance: Force snapshot creation after data migrations or bulk event imports
- Performance tuning: Pre-warm snapshots for hot aggregates before they are loaded
- Threshold bypass: Create snapshots for aggregates with fewer events than the threshold
All commands accept a --domain option to specify the domain module path
(defaults to the current directory).
Commands
| Command | Description |
|---|---|
protean snapshot create |
Create snapshots for event-sourced aggregates |
protean snapshot create
Creates snapshots for event-sourced aggregates. Supports three modes:
Single aggregate instance
protean snapshot create --domain=my_domain --aggregate=User --identifier=abc-123
Creates a snapshot for one specific aggregate instance. The snapshot is reconstructed from the full event stream regardless of whether a snapshot already exists (forced refresh).
All instances of one aggregate
protean snapshot create --domain=my_domain --aggregate=User
Discovers all instances of the specified aggregate and creates a snapshot for each.
All event-sourced aggregates
protean snapshot create --domain=my_domain
Creates snapshots for every instance of every event-sourced aggregate registered in the domain.
Options
| Option | Description | Default |
|---|---|---|
--domain |
Domain module path | . (current directory) |
--aggregate |
Aggregate class name (e.g. User) |
All ES aggregates |
--identifier |
Specific aggregate identifier (requires --aggregate) |
All instances |
Output
# Single instance:
Snapshot created for User with identifier abc-123.
# All instances of one aggregate:
Created 42 snapshot(s) for User.
# All event-sourced aggregates:
User: 42 snapshot(s)
Order: 15 snapshot(s)
Created 57 snapshot(s) across 2 aggregate(s).
Error Handling
| Condition | Behavior |
|---|---|
--identifier without --aggregate |
Aborts with error message |
| Invalid domain path | Aborts with "Error loading Protean domain" |
| Aggregate not found in registry | Aborts with "not found in domain" |
| Non-event-sourced aggregate | Aborts with "not an event-sourced aggregate" |
| No events for identifier | Aborts with "does not exist" |
| No ES aggregates in domain | Prints informational message |
Domain Discovery
The protean snapshot commands use the same domain discovery mechanism as
protean server. The --domain option accepts:
- A Python module path:
my_package.domain - A file path:
src/my_domain.py - A module with instance name:
my_domain:custom_domain .(default): Searches the current directory
See Domain Discovery for the full resolution logic.
Programmatic API
You can create the same snapshots from scripts or the protean shell with the
Domain methods create_snapshot, create_snapshots, and
create_all_snapshots:
The domain this example uses
from protean import Domain, apply
from protean.fields import Identifier, String
domain = Domain(name="Identity")
@domain.event(part_of="User")
class Registered:
user_id: Identifier(required=True)
email: String(required=True)
@domain.aggregate(event_sourced=True)
class User:
user_id: Identifier(identifier=True)
email: String(required=True)
@classmethod
def register(cls, user_id: str, email: str) -> "User":
user = cls(user_id=user_id, email=email)
user.raise_(Registered(user_id=user_id, email=email))
return user
@apply
def registered(self, event: Registered) -> None:
self.user_id = event.user_id
self.email = event.email
@domain.event(part_of="Order")
class OrderPlaced:
order_id: Identifier(required=True)
user_id: Identifier(required=True)
@domain.aggregate(event_sourced=True)
class Order:
order_id: Identifier(identifier=True)
user_id: Identifier(required=True)
@classmethod
def place(cls, order_id: str, user_id: str) -> "Order":
order = cls(order_id=order_id, user_id=user_id)
order.raise_(OrderPlaced(order_id=order_id, user_id=user_id))
return order
@apply
def placed(self, event: OrderPlaced) -> None:
self.order_id = event.order_id
self.user_id = event.user_id
domain.init(traverse=False)
with domain.domain_context():
domain.repository_for(User).add(User.register("abc-123", "ann@example.com"))
domain.repository_for(User).add(User.register("def-456", "bob@example.com"))
domain.repository_for(Order).add(Order.place("ord-1", user_id="abc-123"))
with domain.domain_context():
# Single aggregate instance
domain.create_snapshot(User, "abc-123")
# All instances of one aggregate
count = domain.create_snapshots(User)
# All event-sourced aggregates
results = domain.create_all_snapshots() # {"User": 2, "Order": 1}