Cratis Chronicle projections: declare NotRewindable
In Cratis Chronicle, NotRewindable() records IsRewindable = false on a projection definition. Use it to declare that flag, not to prevent observer replay: the flag is separate from observer replayability and does not change event handling.
Defining a non-rewindable projection
Section titled “Defining a non-rewindable projection”Call NotRewindable() on the projection builder:
using Cratis.Chronicle.Projections;
public class DecNotRewindableAuditLogProjection : IProjectionFor<DecNotRewindableAuditLogEntry>{ public void Define(IProjectionBuilderFor<DecNotRewindableAuditLogEntry> builder) => builder .NotRewindable() .FromEvery(_ => _ .Set(m => m.ProcessedAt).ToEventContextProperty(c => c.Occurred)) .From<DecNotRewindableUserAction>(_ => _ .Set(m => m.OccurredAt).ToEventContextProperty(c => c.Occurred));}import io.cratis.chronicle.projections.IProjectionBuilderForimport io.cratis.chronicle.projections.IProjectionFor
class DecNotRewindableAuditLogProjection : IProjectionFor<DecNotRewindableAuditLogEntry> { override fun define(builder: IProjectionBuilderFor<DecNotRewindableAuditLogEntry>) { builder .notRewindable() .fromEvery { it.set(DecNotRewindableAuditLogEntry::processedAt).toEventContextProperty("occurred") } .from(DecNotRewindableUserAction::class) }}import io.cratis.chronicle.projections.IProjectionBuilderFor;import io.cratis.chronicle.projections.IProjectionFor;
class DecNotRewindableAuditLogProjection implements IProjectionFor<DecNotRewindableAuditLogEntry> { @Override public void define(IProjectionBuilderFor<DecNotRewindableAuditLogEntry> builder) { builder .notRewindable() .fromEvery(feb -> { feb.set("processedAt").toEventContextProperty("occurred"); }) .from(DecNotRewindableUserAction.class); }}defmodule MyApp.Events.NotRewindableUserAction do use Cratis.Chronicle.Events.EventType, id: "not-rewindable-user-action"
defstruct [:action]end
defmodule MyApp.ReadModels.NotRewindableAuditLogEntry do use Cratis.Chronicle.ReadModels.ReadModel, not_rewindable: true
defstruct id: nil, action: nil, processed_at: nil, occurred_at: nil
from_every(set: [processed_at: :occurred])
from MyApp.Events.NotRewindableUserAction, set: [id: :event_source_id, action: :action, occurred_at: :occurred]endimport { IProjectionBuilderFor, IProjectionFor, projection } from '@cratis/chronicle';
@projection()class DecNotRewindableAuditLogProjection implements IProjectionFor<DecNotRewindableAuditLogEntry> { define(builder: IProjectionBuilderFor<DecNotRewindableAuditLogEntry>): void { builder .notRewindable() .autoMap() .fromEvery(_ => _ .set(m => m.processedAt).toEventContextProperty('occurred')) .from(DecNotRewindableUserAction, _ => _ .set(m => m.occurredAt).toEventContextProperty('occurred')); }}The client registers this definition with the Kernel with IsRewindable set to false. The From and FromEvery mappings still determine which values the projection writes.
Read model definition
Section titled “Read model definition”The example targets an audit-entry read model with fields for the action and its timestamps:
public record DecNotRewindableAuditLogEntry( string UserId, string Action, string Details, DateTimeOffset OccurredAt, DateTimeOffset ProcessedAt, long SequenceNumber);data class DecNotRewindableAuditLogEntry( val userId: String = "", val action: String = "", val details: String = "", val processedAt: String = "", val sequenceNumber: Long = 0)class DecNotRewindableAuditLogEntry { public String userId = ""; public String action = ""; public String details = ""; public String processedAt = ""; public long sequenceNumber = 0;}defmodule MyApp.ReadModels.DecNotRewindableAuditLogEntry do defstruct [ :user_id, :action, :details, :occurred_at, :processed_at, :sequence_number ]endclass DecNotRewindableAuditLogEntry { userId = ''; action = ''; details = ''; occurredAt = new Date(); processedAt = new Date(); sequenceNumber: bigint = 0n;}Event definitions
Section titled “Event definitions”These events supply the action and system information used in the examples:
using Cratis.Chronicle.Events;
[EventType]public record DecNotRewindableUserAction( string UserId, string ActionType, string Details);
[EventType]public record DecNotRewindableSystemEvent( string ComponentName, string EventType, string Data);import io.cratis.chronicle.events.EventType
@EventTypedata class DecNotRewindableUserAction( val userId: String, val actionType: String, val details: String)
@EventTypedata class DecNotRewindableSystemEvent( val componentName: String, val eventType: String, val data: String)import io.cratis.chronicle.events.EventType;
@EventTyperecord DecNotRewindableUserAction( String userId, String actionType, String details) {}
@EventTyperecord DecNotRewindableSystemEvent( String componentName, String eventType, String data) {}defmodule MyApp.Events.DecNotRewindableUserAction do use Cratis.Chronicle.Events.EventType, id: "dec-not-rewindable-user-action"
defstruct [:user_id, :action_type, :details]end
defmodule MyApp.Events.DecNotRewindableSystemEvent do use Cratis.Chronicle.Events.EventType, id: "dec-not-rewindable-system-event"
defstruct [:component_name, :event_type, :data]endimport { eventType } from '@cratis/chronicle';
@eventType()class DecNotRewindableUserAction { userId = ''; actionType = ''; details = '';}
@eventType()class DecNotRewindableSystemEvent { componentName = ''; eventType = ''; data = '';}How it works
Section titled “How it works”The builder starts with IsRewindable set to true; NotRewindable() changes it to false. The Kernel carries that value on the engine’s projection. Observer replayability is classified separately, so do not use this flag as a substitute for replay policy.
Further examples
Section titled “Further examples”The same declaration works with audit entries, performance metrics, and financial transactions. Each example keeps its own event mappings and read-model shape:
Audit entries
Section titled “Audit entries”using Cratis.Chronicle.Events;using Cratis.Chronicle.Projections;
[EventType]public record DecNotRewindableUserLoginAttempt(string UserId, bool Succeeded);
[EventType]public record DecNotRewindablePermissionChange(string UserId, string Permission);
public record DecNotRewindableSecurityAuditEntry( DateTimeOffset AuditedAt, long SequenceNumber);
public class DecNotRewindableSecurityAuditProjection : IProjectionFor<DecNotRewindableSecurityAuditEntry>{ public void Define(IProjectionBuilderFor<DecNotRewindableSecurityAuditEntry> builder) => builder .NotRewindable() .FromEvery(_ => _ .Set(m => m.AuditedAt).ToEventContextProperty(c => c.Occurred) .Set(m => m.SequenceNumber).ToEventContextProperty(c => c.SequenceNumber)) .From<DecNotRewindableUserLoginAttempt>() .From<DecNotRewindablePermissionChange>();}import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.IProjectionBuilderForimport io.cratis.chronicle.projections.IProjectionFor
@EventTypedata class DecNotRewindableUserLoginAttempt(val userId: String, val succeeded: Boolean)
@EventTypedata class DecNotRewindablePermissionChange(val userId: String, val permission: String)
data class DecNotRewindableSecurityAuditEntry( val auditedAt: String = "", val sequenceNumber: Long = 0)
class DecNotRewindableSecurityAuditProjection : IProjectionFor<DecNotRewindableSecurityAuditEntry> { override fun define(builder: IProjectionBuilderFor<DecNotRewindableSecurityAuditEntry>) { builder .notRewindable() .fromEvery { it.set(DecNotRewindableSecurityAuditEntry::auditedAt).toEventContextProperty("occurred") it.set(DecNotRewindableSecurityAuditEntry::sequenceNumber).toEventContextProperty("sequenceNumber") } .from(DecNotRewindableUserLoginAttempt::class) .from(DecNotRewindablePermissionChange::class) }}import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.IProjectionBuilderFor;import io.cratis.chronicle.projections.IProjectionFor;
@EventTyperecord DecNotRewindableUserLoginAttempt(String userId, boolean succeeded) {}
@EventTyperecord DecNotRewindablePermissionChange(String userId, String permission) {}
class DecNotRewindableSecurityAuditEntry { public String auditedAt = ""; public long sequenceNumber = 0;}
class DecNotRewindableSecurityAuditProjection implements IProjectionFor<DecNotRewindableSecurityAuditEntry> { @Override public void define(IProjectionBuilderFor<DecNotRewindableSecurityAuditEntry> builder) { builder .notRewindable() .fromEvery(feb -> { feb.set("auditedAt").toEventContextProperty("occurred"); feb.set("sequenceNumber").toEventContextProperty("sequenceNumber"); }) .from(DecNotRewindableUserLoginAttempt.class) .from(DecNotRewindablePermissionChange.class); }}defmodule MyApp.Events.DecNotRewindableUserLoginAttempt do use Cratis.Chronicle.Events.EventType, id: "dec-not-rewindable-user-login-attempt"
defstruct user_id: "", succeeded: falseend
defmodule MyApp.Events.DecNotRewindablePermissionChange do use Cratis.Chronicle.Events.EventType, id: "dec-not-rewindable-permission-change"
defstruct user_id: "", permission: ""end
defmodule MyApp.ReadModels.DecNotRewindableSecurityAuditEntry do use Cratis.Chronicle.ReadModels.ReadModel
defstruct audited_at: nil, sequence_number: 0end
defmodule MyApp.Projections.DecNotRewindableSecurityAuditProjection do use Cratis.Chronicle.Projections.Projection, model: MyApp.ReadModels.DecNotRewindableSecurityAuditEntry, not_rewindable: true
alias MyApp.Events.{DecNotRewindablePermissionChange, DecNotRewindableUserLoginAttempt}
from_every set: [audited_at: :occurred, sequence_number: "$eventContext(SequenceNumber)"]
from DecNotRewindableUserLoginAttempt from DecNotRewindablePermissionChangeendimport { eventType, IProjectionBuilderFor, IProjectionFor, projection } from '@cratis/chronicle';
@eventType()class DecNotRewindableUserLoginAttempt { userId = ''; succeeded = false;}
@eventType()class DecNotRewindablePermissionChange { userId = ''; permission = '';}
class DecNotRewindableSecurityAuditEntry { auditedAt = new Date(); sequenceNumber: bigint = 0n;}
@projection()class DecNotRewindableSecurityAuditProjection implements IProjectionFor<DecNotRewindableSecurityAuditEntry> { define(builder: IProjectionBuilderFor<DecNotRewindableSecurityAuditEntry>): void { builder .notRewindable() .autoMap() .fromEvery(_ => _ .set(m => m.auditedAt).toEventContextProperty('occurred') .set(m => m.sequenceNumber).toEventContextProperty('sequenceNumber')) .from(DecNotRewindableUserLoginAttempt) .from(DecNotRewindablePermissionChange); }}Performance metrics
Section titled “Performance metrics”[EventType]public record DecNotRewindableApiRequestCompleted(string Endpoint, int StatusCode, long DurationMilliseconds);
public record DecNotRewindablePerformanceMetric(DateTimeOffset Timestamp);
public class DecNotRewindablePerformanceMetricProjection : IProjectionFor<DecNotRewindablePerformanceMetric>{ public void Define(IProjectionBuilderFor<DecNotRewindablePerformanceMetric> builder) => builder .NotRewindable() .From<DecNotRewindableApiRequestCompleted>(_ => _ .Set(m => m.Timestamp).ToEventContextProperty(c => c.Occurred));}import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.IProjectionBuilderForimport io.cratis.chronicle.projections.IProjectionFor
@EventTypedata class DecNotRewindableApiRequestCompleted(val endpoint: String, val statusCode: Int, val durationMilliseconds: Long)
data class DecNotRewindablePerformanceMetric(val timestamp: String = "")
class DecNotRewindablePerformanceMetricProjection : IProjectionFor<DecNotRewindablePerformanceMetric> { override fun define(builder: IProjectionBuilderFor<DecNotRewindablePerformanceMetric>) { builder .notRewindable() .autoMap() .from(DecNotRewindableApiRequestCompleted::class) { it.set(DecNotRewindablePerformanceMetric::timestamp).toEventContextProperty("occurred") } }}import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.IProjectionBuilderFor;import io.cratis.chronicle.projections.IProjectionFor;
@EventTyperecord DecNotRewindableApiRequestCompleted(String endpoint, int statusCode, long durationMilliseconds) {}
class DecNotRewindablePerformanceMetric { public String timestamp = "";}
class DecNotRewindablePerformanceMetricProjection implements IProjectionFor<DecNotRewindablePerformanceMetric> { @Override public void define(IProjectionBuilderFor<DecNotRewindablePerformanceMetric> builder) { builder .notRewindable() .autoMap() .from(DecNotRewindableApiRequestCompleted.class, fb -> { fb.set("timestamp").toEventContextProperty("occurred"); }); }}defmodule MyApp.Events.DecNotRewindableApiRequestCompleted do use Cratis.Chronicle.Events.EventType, id: "dec-not-rewindable-api-request-completed"
defstruct endpoint: "", status_code: 0, duration_milliseconds: 0end
defmodule MyApp.ReadModels.DecNotRewindablePerformanceMetric do use Cratis.Chronicle.ReadModels.ReadModel
defstruct timestamp: nilend
defmodule MyApp.Projections.DecNotRewindablePerformanceMetricProjection do use Cratis.Chronicle.Projections.Projection, model: MyApp.ReadModels.DecNotRewindablePerformanceMetric, not_rewindable: true
from MyApp.Events.DecNotRewindableApiRequestCompleted, set: [timestamp: :occurred]endimport { eventType, IProjectionBuilderFor, IProjectionFor, projection } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class DecNotRewindableApiRequestCompleted { endpoint = ''; statusCode = 0; @field(Number) durationMilliseconds: bigint = 0n;}
class DecNotRewindablePerformanceMetric { timestamp = new Date();}
@projection()class DecNotRewindablePerformanceMetricProjection implements IProjectionFor<DecNotRewindablePerformanceMetric> { define(builder: IProjectionBuilderFor<DecNotRewindablePerformanceMetric>): void { builder .notRewindable() .autoMap() .from(DecNotRewindableApiRequestCompleted, _ => _ .set(m => m.timestamp).toEventContextProperty('occurred')); }}Financial transactions
Section titled “Financial transactions”[EventType]public record DecNotRewindablePaymentProcessed(string PaymentId, decimal Amount);
public record DecNotRewindableLedgerEntry( DateTimeOffset RecordedAt, string TransactionType);
public class DecNotRewindableTransactionLedgerProjection : IProjectionFor<DecNotRewindableLedgerEntry>{ public void Define(IProjectionBuilderFor<DecNotRewindableLedgerEntry> builder) => builder .NotRewindable() .FromEvery(_ => _ .Set(m => m.RecordedAt).ToEventContextProperty(c => c.Occurred)) .From<DecNotRewindablePaymentProcessed>(_ => _ .Set(m => m.TransactionType).ToValue("PAYMENT"));}import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.IProjectionBuilderForimport io.cratis.chronicle.projections.IProjectionFor
@EventTypedata class DecNotRewindablePaymentProcessed(val paymentId: String, val amount: Double)
data class DecNotRewindableLedgerEntry( val recordedAt: String = "")
class DecNotRewindableTransactionLedgerProjection : IProjectionFor<DecNotRewindableLedgerEntry> { override fun define(builder: IProjectionBuilderFor<DecNotRewindableLedgerEntry>) { builder .notRewindable() .fromEvery { it.set(DecNotRewindableLedgerEntry::recordedAt).toEventContextProperty("occurred") } .from(DecNotRewindablePaymentProcessed::class) }}import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.IProjectionBuilderFor;import io.cratis.chronicle.projections.IProjectionFor;
@EventTyperecord DecNotRewindablePaymentProcessed(String paymentId, double amount) {}
class DecNotRewindableLedgerEntry { public String recordedAt = "";}
class DecNotRewindableTransactionLedgerProjection implements IProjectionFor<DecNotRewindableLedgerEntry> { @Override public void define(IProjectionBuilderFor<DecNotRewindableLedgerEntry> builder) { builder .notRewindable() .fromEvery(feb -> { feb.set("recordedAt").toEventContextProperty("occurred"); }) .from(DecNotRewindablePaymentProcessed.class); }}defmodule MyApp.Events.DecNotRewindablePaymentProcessed do use Cratis.Chronicle.Events.EventType, id: "dec-not-rewindable-payment-processed"
defstruct payment_id: "", amount: 0.0end
defmodule MyApp.ReadModels.DecNotRewindableLedgerEntry do use Cratis.Chronicle.ReadModels.ReadModel
defstruct recorded_at: nil, transaction_type: nilend
defmodule MyApp.Projections.DecNotRewindableTransactionLedgerProjection do use Cratis.Chronicle.Projections.Projection, model: MyApp.ReadModels.DecNotRewindableLedgerEntry, not_rewindable: true
from_every set: [recorded_at: :occurred]
from MyApp.Events.DecNotRewindablePaymentProcessed, set: [transaction_type: "$value(PAYMENT)"]endimport { eventType, IProjectionBuilderFor, IProjectionFor, projection } from '@cratis/chronicle';
@eventType()class DecNotRewindablePaymentProcessed { paymentId = ''; amount = 0;}
class DecNotRewindableLedgerEntry { recordedAt = new Date(); transactionType = '';}
@projection()class DecNotRewindableTransactionLedgerProjection implements IProjectionFor<DecNotRewindableLedgerEntry> { define(builder: IProjectionBuilderFor<DecNotRewindableLedgerEntry>): void { builder .notRewindable() .autoMap() .fromEvery(_ => _ .set(m => m.recordedAt).toEventContextProperty('occurred')) .from(DecNotRewindablePaymentProcessed, _ => _ .set(m => m.transactionType).toValue('PAYMENT')); }}Combining with other features
Section titled “Combining with other features”You can declare the flag alongside an event sequence, passive mode, and property mappings:
using Cratis.Chronicle.Projections;
[EventType]public record DecNotRewindableOrderReceived(string OrderId);
[EventType]public record DecNotRewindableOrderProcessing(string OrderId);
[EventType]public record DecNotRewindableOrderCompleted(string OrderId);
public record DecNotRewindableOrderStatus( string Status, DateTimeOffset LastUpdatedAt);
public class DecNotRewindableRealTimeOrderStatusProjection : IProjectionFor<DecNotRewindableOrderStatus>{ public void Define(IProjectionBuilderFor<DecNotRewindableOrderStatus> builder) => builder .NotRewindable() .FromEventSequence("order-processing") .Passive() .FromEvery(_ => _ .Set(m => m.LastUpdatedAt).ToEventContextProperty(c => c.Occurred)) .From<DecNotRewindableOrderReceived>(_ => _ .Set(m => m.Status).ToValue("RECEIVED")) .From<DecNotRewindableOrderProcessing>(_ => _ .Set(m => m.Status).ToValue("PROCESSING")) .From<DecNotRewindableOrderCompleted>(_ => _ .Set(m => m.Status).ToValue("COMPLETED"));}Kotlin does not support this workflow yet.Java does not support this workflow yet.defmodule MyApp.Events.DecNotRewindableOrderReceived do use Cratis.Chronicle.Events.EventType, id: "dec-not-rewindable-order-received"
defstruct order_id: ""end
defmodule MyApp.Events.DecNotRewindableOrderProcessing do use Cratis.Chronicle.Events.EventType, id: "dec-not-rewindable-order-processing"
defstruct order_id: ""end
defmodule MyApp.Events.DecNotRewindableOrderCompleted do use Cratis.Chronicle.Events.EventType, id: "dec-not-rewindable-order-completed"
defstruct order_id: ""end
defmodule MyApp.ReadModels.DecNotRewindableOrderStatus do use Cratis.Chronicle.ReadModels.ReadModel
defstruct status: nil, last_updated_at: nilend
defmodule MyApp.Projections.DecNotRewindableRealTimeOrderStatusProjection do use Cratis.Chronicle.Projections.Projection, model: MyApp.ReadModels.DecNotRewindableOrderStatus, not_rewindable: true, event_sequence: "order-processing", passive: true
from_every set: [last_updated_at: :occurred]
from MyApp.Events.DecNotRewindableOrderReceived, set: [status: "$value(RECEIVED)"]
from MyApp.Events.DecNotRewindableOrderProcessing, set: [status: "$value(PROCESSING)"]
from MyApp.Events.DecNotRewindableOrderCompleted, set: [status: "$value(COMPLETED)"]endimport { eventType, IProjectionBuilderFor, IProjectionFor, projection } from '@cratis/chronicle';
@eventType()class DecNotRewindableOrderReceived { orderId = '';}
@eventType()class DecNotRewindableOrderProcessing { orderId = '';}
@eventType()class DecNotRewindableOrderCompleted { orderId = '';}
class DecNotRewindableOrderStatus { status = ''; lastUpdatedAt = new Date();}
@projection()class DecNotRewindableRealTimeOrderStatusProjection implements IProjectionFor<DecNotRewindableOrderStatus> { define(builder: IProjectionBuilderFor<DecNotRewindableOrderStatus>): void { builder .notRewindable() .fromEventSequence('order-processing') .passive() .autoMap() .fromEvery(_ => _ .set(m => m.lastUpdatedAt).toEventContextProperty('occurred')) .from(DecNotRewindableOrderReceived, _ => _ .set(m => m.status).toValue('RECEIVED')) .from(DecNotRewindableOrderProcessing, _ => _ .set(m => m.status).toValue('PROCESSING')) .from(DecNotRewindableOrderCompleted, _ => _ .set(m => m.status).toValue('COMPLETED')); }}Here, FromEventSequence selects the events, Passive() selects on-demand computation, and the mappings set status and timestamps. Those choices are independent of NotRewindable(). Continue with passive projections if you want to compute a read model at read time rather than maintain stored state.