pub struct ProjectionBuilder<'a, S, P>{ /* private fields */ }Expand description
Builder used to configure which events should be loaded for a projection.
Implementations§
Source§impl<'a, S, P> ProjectionBuilder<'a, S, P>
impl<'a, S, P> ProjectionBuilder<'a, S, P>
Sourcepub fn event<E>(self) -> Self
pub fn event<E>(self) -> Self
Register a specific event type to load from all aggregates.
§Type Constraints
The store’s metadata type must be convertible to the projection’s
metadata type. Clone is required because event handlers receive
metadata by reference, but Into::into() requires ownership. The
metadata is cloned once per event.
§Example
builder.event::<ProductRestocked>() // All productsSourcepub fn events<E>(self) -> Self
pub fn events<E>(self) -> Self
Register all event kinds supported by a ProjectionEvent sum type
across all aggregates.
This is primarily intended for subscribing to an aggregate’s generated
event enum (A::Event from #[derive(Aggregate)]) as a single
“unit”, rather than registering each DomainEvent type
individually.
§Example
builder.events::<AccountEvent>() // All accounts, all account event variantsSourcepub fn event_for<A, E>(self, aggregate_id: &S::Id) -> Selfwhere
A: Aggregate<Id = S::Id>,
E: DomainEvent,
P: ApplyProjection<E>,
S::Metadata: Clone + Into<P::Metadata>,
pub fn event_for<A, E>(self, aggregate_id: &S::Id) -> Selfwhere
A: Aggregate<Id = S::Id>,
E: DomainEvent,
P: ApplyProjection<E>,
S::Metadata: Clone + Into<P::Metadata>,
Register a specific event type to load from a specific aggregate instance.
Use this when you only care about a single event kind. If you want to
subscribe to an aggregate’s full event enum (A::Event), prefer
ProjectionBuilder::events_for.
§Example
builder.event_for::<Account, FundsDeposited>(&account_id); // One account streamSourcepub fn events_for<A>(self, aggregate_id: &S::Id) -> Selfwhere
A: Aggregate<Id = S::Id>,
A::Event: ProjectionEvent,
P: ApplyProjection<A::Event>,
S::Metadata: Clone + Into<P::Metadata>,
pub fn events_for<A>(self, aggregate_id: &S::Id) -> Selfwhere
A: Aggregate<Id = S::Id>,
A::Event: ProjectionEvent,
P: ApplyProjection<A::Event>,
S::Metadata: Clone + Into<P::Metadata>,
Register all event kinds for a specific aggregate instance.
This subscribes the projection to the aggregate’s event sum type
(A::Event) and loads all events in that stream that correspond to
A::Event::EVENT_KINDS.
§Example
let history = repository
.build_projection::<AccountHistory>()
.events_for::<Account>(&account_id)
.load()?;Sourcepub async fn load(
self,
) -> Result<P, ProjectionError<S::Error, <S::Codec as Codec>::Error>>
pub async fn load( self, ) -> Result<P, ProjectionError<S::Error, <S::Codec as Codec>::Error>>
Replays the configured events and materializes the projection.
Events are dispatched to the projection via the registered handlers,
which deserialize and apply each event through the appropriate
ApplyProjection implementation.
§Errors
Returns ProjectionError when the store fails to load events or when
an event cannot be deserialized.