postgres_es2/aggregates/
postgres_snapshot_store_aggregate_context.rs

1use cqrs_es2::{
2    Aggregate,
3    AggregateContext,
4};
5
6/// Holds context for a pure event store implementation for MemStore
7pub struct PostgresSnapshotStoreAggregateContext<A>
8where
9    A: Aggregate, {
10    /// The aggregate ID of the aggregate instance that has been
11    /// loaded.
12    pub aggregate_id: String,
13    /// The current state of the aggregate instance.
14    aggregate: A,
15    /// The last committed event sequence number for this aggregate
16    /// instance.
17    pub current_sequence: usize,
18}
19
20impl<A> AggregateContext<A>
21    for PostgresSnapshotStoreAggregateContext<A>
22where
23    A: Aggregate,
24{
25    fn aggregate(&self) -> &A {
26        &self.aggregate
27    }
28}
29
30impl<A> PostgresSnapshotStoreAggregateContext<A>
31where
32    A: Aggregate,
33{
34    /// creates a new instance
35    pub fn new(
36        aggregate_id: String,
37        aggregate: A,
38        current_sequence: usize,
39    ) -> PostgresSnapshotStoreAggregateContext<A> {
40        PostgresSnapshotStoreAggregateContext {
41            aggregate_id,
42            aggregate,
43            current_sequence,
44        }
45    }
46
47    pub(crate) fn aggregate_copy(&self) -> A {
48        let ser = serde_json::to_value(&self.aggregate).unwrap();
49        serde_json::from_value(ser).unwrap()
50    }
51}