Passive Projections
Mark a projection as passive (not actively observing) using [Passive]:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Keys;using Cratis.Chronicle.Projections.ModelBound;using Cratis.Chronicle.ReadModels;
[EventType]public record MbPassiveSnapshotCreated(string Data);
[Passive]public record MbPassiveHistoricalSnapshot( [Key] Guid Id,
[SetFrom<MbPassiveSnapshotCreated>(nameof(MbPassiveSnapshotCreated.Data))] string Data);import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.FromEventimport io.cratis.chronicle.projections.SetFromimport io.cratis.chronicle.readModels.Passiveimport io.cratis.chronicle.readModels.ReadModel
@EventTypedata class MbPassiveSnapshotCreated(val data: String)
@Passive@ReadModel@FromEvent(MbPassiveSnapshotCreated::class)data class MbPassiveHistoricalSnapshot( @SetFrom("data", MbPassiveSnapshotCreated::class) val data: String = "")import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.FromEvent;import io.cratis.chronicle.projections.SetFrom;import io.cratis.chronicle.readModels.Passive;import io.cratis.chronicle.readModels.ReadModel;
@EventTyperecord MbPassiveSnapshotCreated(String data) {}
@Passive@ReadModel@FromEvent(eventType = MbPassiveSnapshotCreated.class)class MbPassiveHistoricalSnapshot { @SetFrom(propertyPath = "data", eventType = MbPassiveSnapshotCreated.class) public String data = "";}defmodule MyApp.Events.PassiveSnapshotCreated do use Chronicle.Events.EventType, id: "passive-snapshot-created-v1"
defstruct [:data]end
defmodule MyApp.ReadModels.PassiveHistoricalSnapshot do use Chronicle.ReadModels.ReadModel, passive: true
alias MyApp.Events.PassiveSnapshotCreated
defstruct [:id, :data]
from PassiveSnapshotCreated, set: [id: :event_source_id, data: :data]endimport { eventType, Guid, passive, readModel, setFrom } from '@cratis/chronicle';
@eventType()class MbPassiveSnapshotCreated { data = '';}
@readModel()@passiveclass MbPassiveHistoricalSnapshot { id: Guid = Guid.empty;
@setFrom(MbPassiveSnapshotCreated, 'data') data = '';}When to use
Section titled “When to use”- On-demand projections that are only rebuilt when explicitly requested
- Historical snapshots
- Resource-intensive projections that shouldn’t run continuously
Best practices
Section titled “Best practices”- Use for expensive projections that are only needed occasionally
- Consider the trade-off between resource usage and data freshness
- Ensure you have mechanisms to trigger rebuilds when needed