eventually_core/projection.rs
1//! Contain support for [`Projection`], an optimized read model
2//! of an [`Aggregate`] or of a number of `Aggregate`s.
3//!
4//! More information about projections can be found here:
5//! https://eventstore.com/docs/getting-started/projections/index.html
6//!
7//! [`Projection`]: trait.Projection.html
8//! [`Aggregate`]: ../aggregate/trait.Aggregate.html
9
10use futures::future::BoxFuture;
11
12use crate::store::Persisted;
13
14/// A `Projection` is an optimized read model (or materialized view)
15/// of an [`Aggregate`] model(s), that can be assembled by left-folding
16/// its previous state and a number of ordered, consecutive events.
17///
18/// The events passed to a `Projection` have been persisted onto
19/// an [`EventStore`] first.
20///
21/// [`Aggregate`]: ../aggregate/trait.Aggregate.html
22/// [`EventStore`]: ../store/trait.EventStore.html
23pub trait Projection {
24 /// Type of the Source id, typically an [`AggregateId`].
25 ///
26 /// [`AggregateId`]: ../aggregate/type.AggregateId.html
27 type SourceId: Eq;
28
29 /// Event to be stored in the `EventStore`, typically an [`Aggregate::Event`].
30 ///
31 /// [`Aggregate::Event`]: ../aggregate/trait.Aggregate.html#associatedtype.Event
32 type Event;
33
34 /// Type of the possible error that might occur when projecting
35 /// the next state.
36 type Error;
37
38 /// Updates the next value of the `Projection` using the provided event value.
39 fn project<'a>(
40 &'a mut self,
41 event: Persisted<Self::SourceId, Self::Event>,
42 ) -> BoxFuture<'a, Result<(), Self::Error>>;
43}