pub struct Projection<E: Executor, P: Default + 'static> { /* private fields */ }Expand description
Projection definition: a set of handlers for a primary aggregate type.
A Projection is constructed without an aggregate id. Terminal operations:
Projection::loadreturns aLoadBuilderbound to a specific aggregate id.Projection::subscriptionreturns aProjectionSubscriptionthat auto-updates the projection as new events arrive.
§Example
// Load a single aggregate
let result = Projection::<_, AccountView>::new::<Account>()
.handler(account_opened())
.handler(money_deposited())
.data(app_config)
.load("account-123")
.execute(&executor)
.await?;
// Start a subscription that keeps every aggregate up to date
let subscription = Projection::<_, AccountView>::new::<Account>()
.handler(account_opened())
.handler(money_deposited())
.data(app_config)
.subscription("account-view")
.start(&executor)
.await?;Implementations§
Source§impl<E: Executor, P: Snapshot<E> + Default + 'static> Projection<E, P>
impl<E: Executor, P: Snapshot<E> + Default + 'static> Projection<E, P>
Sourcepub fn new<A: Aggregate>() -> Projection<E, P>
pub fn new<A: Aggregate>() -> Projection<E, P>
Creates a new projection definition for the given primary aggregate type.
Sourcepub fn revision(self, value: u16) -> Self
pub fn revision(self, value: u16) -> Self
Sets the snapshot revision.
Changing the revision invalidates existing snapshots, forcing a full rebuild.
Sourcepub fn strict(self) -> Self
pub fn strict(self) -> Self
Enables safety checks for unhandled events.
When enabled, execution fails if an event is encountered without a handler.
Sourcepub fn tombstone<EV: AggregateEvent + Send + Sync + 'static>(self) -> Self
pub fn tombstone<EV: AggregateEvent + Send + Sync + 'static>(self) -> Self
Declares which event marks an aggregate as deleted (tombstoned).
When set:
LoadBuilder::executeshort-circuits and returnsOk(None)as soon as it sees a committed tombstone event for the requested id, avoiding any snapshot read or event replay.ProjectionSubscriptionroutes tombstone events toSnapshot::drop_snapshotso the user can delete their snapshot row.
Sourcepub fn handler<H: Handler<P> + 'static>(self, h: H) -> Self
pub fn handler<H: Handler<P> + 'static>(self, h: H) -> Self
Registers an event handler with this projection.
§Panics
Panics if a handler for the same event type is already registered.
Sourcepub fn skip<EV: AggregateEvent + Send + Sync + 'static>(self) -> Self
pub fn skip<EV: AggregateEvent + Send + Sync + 'static>(self) -> Self
Registers a skip handler with this projection.
§Panics
Panics if a handler for the same event type is already registered.
Sourcepub fn data<D: Send + Sync + 'static>(self, v: D) -> Self
pub fn data<D: Send + Sync + 'static>(self, v: D) -> Self
Adds shared data to the handler context.
Data added here is accessible in handlers via the context. Data lives
on the projection definition and is reused for both Projection::load
and Projection::subscription.
Sourcepub fn load(self, id: impl Into<String>) -> LoadBuilder<E, P>
pub fn load(self, id: impl Into<String>) -> LoadBuilder<E, P>
Returns a LoadBuilder for loading the aggregate with the given id.
Sourcepub fn load_ids(self, ids: Vec<impl Into<String>>) -> LoadBuilder<E, P>
pub fn load_ids(self, ids: Vec<impl Into<String>>) -> LoadBuilder<E, P>
Returns a LoadBuilder for loading state keyed on multiple aggregate ids.
The ids are hashed via crate::hash_ids to produce a stable snapshot id.
Sourcepub fn subscription(
self,
key: impl Into<String>,
) -> ProjectionSubscription<E, P>
pub fn subscription( self, key: impl Into<String>, ) -> ProjectionSubscription<E, P>
Returns a builder for a subscription that keeps this projection up to date.