castore
castore copied to clipboard
Snapshot Discussion
Hey,
We're currently preparing to add snapshots to our projects, it's not implemented yet in Castore.
We are planning on implementing something similar to:
public async getAggregate(id: string): Promise<AGGREGATE_CLASS | undefined> {
const snapshot: { version: number, aggregateState: $AGGREGATE } | undefined = await this.getSnapshot(id);
const version = snapshot ? snapshot.version : 0;
const aggregateState = snapshot ? snapshot.aggregateState : {} as $AGGREGATE;
const { events } = await this.castoreEventStore.getEvents(id, { minVersion: version || 0 });
// cast to any because it's not typed correctly EVENT_DETAIL[] vs $EVENT_DETAIL[]
const aggregate = this.castoreEventStore.buildAggregate(events as any, aggregateState);
if (aggregate === undefined) {
return undefined;
}
// this create an AggregateClass from the state
return this.factory(aggregate);
}
Where snapshots are stored listening to StateCarryingEvents.
But my main concern is about snapshot invalidations, If you have any recommendations for that?
We are also considering adding an AggregateReducerVersion in our class (that we could increment manually) to invalidate snapshots built with another reducer, but it seems tricky to maintain something like that manually.
Do you already have some idea of the future implementation of snapshots in Castore we could take inspiration on? What have been the issues you were facing implementing it in the past?