Chronicle projection functions for counters and totals
Cratis Chronicle projection functions maintain counters and totals from events, so your application can read a login count, stock quantity, or account balance without calculating it on each query. Choose a function according to what the event contributes: one occurrence, one item, or an amount carried in the event.
Counting events
Section titled “Counting events”Use Count() inside a From mapping to add one for each handled event of that type. In this example, login events and action events update separate counters; neither counter includes unrelated event types:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Projections;
[EventType]public record DecFunctionsUserLoggedIn(string Username);
[EventType]public record DecFunctionsUserPerformedAction(string Username, string ActionType);
public record DecFunctionsUserActivity( string Username, int LoginCount, int ActionCount);
public class DecFunctionsUserActivityProjection : IProjectionFor<DecFunctionsUserActivity>{ public void Define(IProjectionBuilderFor<DecFunctionsUserActivity> builder) => builder .From<DecFunctionsUserLoggedIn>(_ => _ .Count(m => m.LoginCount)) .From<DecFunctionsUserPerformedAction>(_ => _ .Count(m => m.ActionCount));}import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.IProjectionBuilderForimport io.cratis.chronicle.projections.IProjectionFor
@EventTypedata class DecFunctionsUserLoggedIn(val username: String)
@EventTypedata class DecFunctionsUserPerformedAction(val username: String, val actionType: String)
data class DecFunctionsUserActivity( val username: String = "", val loginCount: Int = 0, val actionCount: Int = 0)
class DecFunctionsUserActivityProjection : IProjectionFor<DecFunctionsUserActivity> { override fun define(builder: IProjectionBuilderFor<DecFunctionsUserActivity>) { builder .autoMap() .from(DecFunctionsUserLoggedIn::class) { it.count(DecFunctionsUserActivity::loginCount) } .from(DecFunctionsUserPerformedAction::class) { it.count(DecFunctionsUserActivity::actionCount) } }}import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.IProjectionBuilderFor;import io.cratis.chronicle.projections.IProjectionFor;
@EventTyperecord DecFunctionsUserLoggedIn(String username) {}
@EventTyperecord DecFunctionsUserPerformedAction(String username, String actionType) {}
class DecFunctionsUserActivity { public String username = ""; public int loginCount = 0; public int actionCount = 0;}
class DecFunctionsUserActivityProjection implements IProjectionFor<DecFunctionsUserActivity> { @Override public void define(IProjectionBuilderFor<DecFunctionsUserActivity> builder) { builder .autoMap() .from(DecFunctionsUserLoggedIn.class, fb -> { fb.count("loginCount"); }) .from(DecFunctionsUserPerformedAction.class, fb -> { fb.count("actionCount"); }); }}defmodule MyApp.Events.DecFunctionsUserLoggedIn do use Cratis.Chronicle.Events.EventType, id: "dec-functions-user-logged-in"
defstruct [:username]end
defmodule MyApp.Events.DecFunctionsUserPerformedAction do use Cratis.Chronicle.Events.EventType, id: "dec-functions-user-performed-action"
defstruct [:username, :action_type]end
defmodule MyApp.ReadModels.DecFunctionsUserActivity do use Cratis.Chronicle.ReadModels.ReadModel
defstruct [:username, login_count: 0, action_count: 0]end
defmodule MyApp.Projections.DecFunctionsUserActivityProjection do use Cratis.Chronicle.Projections.Projection, model: MyApp.ReadModels.DecFunctionsUserActivity
alias MyApp.Events.{DecFunctionsUserLoggedIn, DecFunctionsUserPerformedAction}
from DecFunctionsUserLoggedIn, count: :login_count
from DecFunctionsUserPerformedAction, count: :action_countendimport { eventType, IProjectionBuilderFor, IProjectionFor, projection } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class DecFunctionsUserLoggedIn { @field(String) readonly username: string;
constructor(username: string) { this.username = username; }}
@eventType()class DecFunctionsUserPerformedAction { @field(String) readonly username: string; @field(String) readonly actionType: string;
constructor(username: string, actionType: string) { this.username = username; this.actionType = actionType; }}
class DecFunctionsUserActivity { username = ''; loginCount = 0; actionCount = 0;}
@projection()class DecFunctionsUserActivityProjection implements IProjectionFor<DecFunctionsUserActivity> { define(builder: IProjectionBuilderFor<DecFunctionsUserActivity>): void { builder .autoMap() .from(DecFunctionsUserLoggedIn, _ => _ .count(m => m.loginCount)) .from(DecFunctionsUserPerformedAction, _ => _ .count(m => m.actionCount)); }}Increment and decrement
Section titled “Increment and decrement”Use Increment() and Decrement() when an event changes a quantity by one. An item-added event increases inventory; an item-removed event decreases it:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Projections;
[EventType]public record DecFunctionsItemAdded(string Name);
[EventType]public record DecFunctionsItemRemoved(string Name);
public record DecFunctionsInventory(int Quantity);
public class DecFunctionsInventoryProjection : IProjectionFor<DecFunctionsInventory>{ public void Define(IProjectionBuilderFor<DecFunctionsInventory> builder) => builder .From<DecFunctionsItemAdded>(_ => _ .Increment(m => m.Quantity)) .From<DecFunctionsItemRemoved>(_ => _ .Decrement(m => m.Quantity));}import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.IProjectionBuilderForimport io.cratis.chronicle.projections.IProjectionFor
@EventTypedata class DecFunctionsItemAdded(val name: String)
@EventTypedata class DecFunctionsItemRemoved(val name: String)
data class DecFunctionsInventory(val quantity: Int = 0)
class DecFunctionsInventoryProjection : IProjectionFor<DecFunctionsInventory> { override fun define(builder: IProjectionBuilderFor<DecFunctionsInventory>) { builder .autoMap() .from(DecFunctionsItemAdded::class) { it.increment(DecFunctionsInventory::quantity) } .from(DecFunctionsItemRemoved::class) { it.decrement(DecFunctionsInventory::quantity) } }}import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.IProjectionBuilderFor;import io.cratis.chronicle.projections.IProjectionFor;
@EventTyperecord DecFunctionsItemAdded(String name) {}
@EventTyperecord DecFunctionsItemRemoved(String name) {}
class DecFunctionsInventory { public int quantity = 0;}
class DecFunctionsInventoryProjection implements IProjectionFor<DecFunctionsInventory> { @Override public void define(IProjectionBuilderFor<DecFunctionsInventory> builder) { builder .autoMap() .from(DecFunctionsItemAdded.class, fb -> { fb.increment("quantity"); }) .from(DecFunctionsItemRemoved.class, fb -> { fb.decrement("quantity"); }); }}defmodule MyApp.Events.DecFunctionsItemAdded do use Cratis.Chronicle.Events.EventType, id: "dec-functions-item-added"
defstruct [:name]end
defmodule MyApp.Events.DecFunctionsItemRemoved do use Cratis.Chronicle.Events.EventType, id: "dec-functions-item-removed"
defstruct [:name]end
defmodule MyApp.ReadModels.DecFunctionsInventory do use Cratis.Chronicle.ReadModels.ReadModel
defstruct quantity: 0end
defmodule MyApp.Projections.DecFunctionsInventoryProjection do use Cratis.Chronicle.Projections.Projection, model: MyApp.ReadModels.DecFunctionsInventory
alias MyApp.Events.{DecFunctionsItemAdded, DecFunctionsItemRemoved}
from DecFunctionsItemAdded, add: [quantity: 1]
from DecFunctionsItemRemoved, subtract: [quantity: 1]endimport { eventType, IProjectionBuilderFor, IProjectionFor, projection } from '@cratis/chronicle';
@eventType()class DecFunctionsItemAdded { name = '';}
@eventType()class DecFunctionsItemRemoved { name = '';}
class DecFunctionsInventory { quantity = 0;}
@projection()class DecFunctionsInventoryProjection implements IProjectionFor<DecFunctionsInventory> { define(builder: IProjectionBuilderFor<DecFunctionsInventory>): void { builder .autoMap() .from(DecFunctionsItemAdded, _ => _ .increment(m => m.quantity)) .from(DecFunctionsItemRemoved, _ => _ .decrement(m => m.quantity)); }}Each call changes the value by exactly one, regardless of the event’s payload. Count() and Increment() both add one; choose the name that expresses the meaning of the field.
Add and subtract with values
Section titled “Add and subtract with values”Use Add() or Subtract() when the change comes from an event property. In the .NET builder, .With(e => e.Amount) supplies the amount to apply to the current balance:
using Cratis.Chronicle.Events;using Cratis.Chronicle.Projections;
[EventType]public record DecFunctionsAccountOpened(string Number);
[EventType]public record DecFunctionsMoneyDeposited(decimal Amount);
[EventType]public record DecFunctionsMoneyWithdrawn(decimal Amount);
public record DecFunctionsAccount(string Number, decimal Balance);
public class DecFunctionsAccountProjection : IProjectionFor<DecFunctionsAccount>{ public void Define(IProjectionBuilderFor<DecFunctionsAccount> builder) => builder .From<DecFunctionsAccountOpened>(_ => _ .Set(m => m.Balance).ToValue(0m)) .From<DecFunctionsMoneyDeposited>(_ => _ .Add(m => m.Balance).With(e => e.Amount)) .From<DecFunctionsMoneyWithdrawn>(_ => _ .Subtract(m => m.Balance).With(e => e.Amount));}import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.IProjectionBuilderForimport io.cratis.chronicle.projections.IProjectionForimport java.math.BigDecimal
@EventTypedata class DecFunctionsAccountOpened(val number: String)
@EventTypedata class DecFunctionsMoneyDeposited(val amount: BigDecimal)
@EventTypedata class DecFunctionsMoneyWithdrawn(val amount: BigDecimal)
data class DecFunctionsAccount(val number: String = "", val balance: BigDecimal = BigDecimal.ZERO)
class DecFunctionsAccountProjection : IProjectionFor<DecFunctionsAccount> { override fun define(builder: IProjectionBuilderFor<DecFunctionsAccount>) { builder .autoMap() .from(DecFunctionsAccountOpened::class) .from(DecFunctionsMoneyDeposited::class) { it.add(DecFunctionsAccount::balance).with("amount") } .from(DecFunctionsMoneyWithdrawn::class) { it.subtract(DecFunctionsAccount::balance).with("amount") } }}import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.IProjectionBuilderFor;import io.cratis.chronicle.projections.IProjectionFor;
import java.math.BigDecimal;
@EventTyperecord DecFunctionsAccountOpened(String number) {}
@EventTyperecord DecFunctionsMoneyDeposited(BigDecimal amount) {}
@EventTyperecord DecFunctionsMoneyWithdrawn(BigDecimal amount) {}
class DecFunctionsAccount { public String number = ""; public BigDecimal balance = BigDecimal.ZERO;}
class DecFunctionsAccountProjection implements IProjectionFor<DecFunctionsAccount> { @Override public void define(IProjectionBuilderFor<DecFunctionsAccount> builder) { builder .autoMap() .from(DecFunctionsAccountOpened.class) .from(DecFunctionsMoneyDeposited.class, fb -> { fb.add("balance").with("amount"); }) .from(DecFunctionsMoneyWithdrawn.class, fb -> { fb.subtract("balance").with("amount"); }); }}defmodule MyApp.Events.DecFunctionsAccountOpened do use Cratis.Chronicle.Events.EventType, id: "dec-functions-account-opened"
defstruct [:number]end
defmodule MyApp.Events.DecFunctionsMoneyDeposited do use Cratis.Chronicle.Events.EventType, id: "dec-functions-money-deposited"
defstruct [:amount]end
defmodule MyApp.Events.DecFunctionsMoneyWithdrawn do use Cratis.Chronicle.Events.EventType, id: "dec-functions-money-withdrawn"
defstruct [:amount]end
defmodule MyApp.ReadModels.DecFunctionsAccount do use Cratis.Chronicle.ReadModels.ReadModel
defstruct number: "", balance: 0end
defmodule MyApp.Projections.DecFunctionsAccountProjection do use Cratis.Chronicle.Projections.Projection, model: MyApp.ReadModels.DecFunctionsAccount
alias MyApp.Events.{DecFunctionsAccountOpened, DecFunctionsMoneyDeposited, DecFunctionsMoneyWithdrawn}
from DecFunctionsAccountOpened, set: [balance: 0]
from DecFunctionsMoneyDeposited, add: [balance: :amount]
from DecFunctionsMoneyWithdrawn, subtract: [balance: :amount]endimport { eventType, IProjectionBuilderFor, IProjectionFor, projection } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class DecFunctionsAccountOpened { @field(String) readonly number: string;
constructor(number: string) { this.number = number; }}
@eventType()class DecFunctionsMoneyDeposited { @field(Number) readonly amount: number;
constructor(amount: number) { this.amount = amount; }}
@eventType()class DecFunctionsMoneyWithdrawn { @field(Number) readonly amount: number;
constructor(amount: number) { this.amount = amount; }}
class DecFunctionsAccount { number = ''; balance = 0;}
@projection()class DecFunctionsAccountProjection implements IProjectionFor<DecFunctionsAccount> { define(builder: IProjectionBuilderFor<DecFunctionsAccount>): void { builder .autoMap() .from(DecFunctionsAccountOpened, _ => _ .set(m => m.balance).toValue(0)) .from(DecFunctionsMoneyDeposited, _ => _ .add(m => m.balance).with(e => e.amount)) .from(DecFunctionsMoneyWithdrawn, _ => _ .subtract(m => m.balance).with(e => e.amount)); }}Supported types
Section titled “Supported types”The kernel supports numeric targets including int, long, float, double, and decimal. It uses the target property’s schema to select the numeric type and converts values for the operation. Declare a target type suitable for the values you accumulate; client-language type names differ.
How functions work
Section titled “How functions work”Each function reads the property’s current value, applies the change, and stores the result for the next event. A missing numeric property starts at zero. If the read model needs another starting value, declare initial values before accumulating events.
The read model key determines which instance receives the change. A balance keyed by account accumulates that account’s events, not every account’s events. See composite keys when one identifier is not enough to select the instance.
Combining functions
Section titled “Combining functions”A single event can update several properties. Here each transaction increases the transaction count and processed-event count by one, while its amount is added to the total:
[EventType]public record DecFunctionsTransaction(decimal Amount);
public record DecFunctionsTransactionSummary( int TransactionCount, decimal TotalAmount, int ProcessedEvents);
public class DecFunctionsTransactionSummaryProjection : IProjectionFor<DecFunctionsTransactionSummary>{ public void Define(IProjectionBuilderFor<DecFunctionsTransactionSummary> builder) => builder .From<DecFunctionsTransaction>(_ => _ .Count(m => m.TransactionCount) .Add(m => m.TotalAmount).With(e => e.Amount) .Increment(m => m.ProcessedEvents));}import io.cratis.chronicle.events.EventTypeimport io.cratis.chronicle.projections.IProjectionBuilderForimport io.cratis.chronicle.projections.IProjectionForimport java.math.BigDecimal
@EventTypedata class DecFunctionsTransaction(val amount: BigDecimal)
data class DecFunctionsTransactionSummary( val transactionCount: Int = 0, val totalAmount: BigDecimal = BigDecimal.ZERO, val processedEvents: Int = 0)
class DecFunctionsTransactionSummaryProjection : IProjectionFor<DecFunctionsTransactionSummary> { override fun define(builder: IProjectionBuilderFor<DecFunctionsTransactionSummary>) { builder .from(DecFunctionsTransaction::class) { it.count(DecFunctionsTransactionSummary::transactionCount) it.add(DecFunctionsTransactionSummary::totalAmount).with("amount") it.increment(DecFunctionsTransactionSummary::processedEvents) } }}import io.cratis.chronicle.events.EventType;import io.cratis.chronicle.projections.IProjectionBuilderFor;import io.cratis.chronicle.projections.IProjectionFor;
import java.math.BigDecimal;
@EventTyperecord DecFunctionsTransaction(BigDecimal amount) {}
class DecFunctionsTransactionSummary { public int transactionCount = 0; public BigDecimal totalAmount = BigDecimal.ZERO; public int processedEvents = 0;}
class DecFunctionsTransactionSummaryProjection implements IProjectionFor<DecFunctionsTransactionSummary> { @Override public void define(IProjectionBuilderFor<DecFunctionsTransactionSummary> builder) { builder .from(DecFunctionsTransaction.class, fb -> { fb.count("transactionCount"); fb.add("totalAmount").with("amount"); fb.increment("processedEvents"); }); }}defmodule MyApp.Events.DecFunctionsTransaction do use Cratis.Chronicle.Events.EventType, id: "dec-functions-transaction"
defstruct [:amount]end
defmodule MyApp.ReadModels.DecFunctionsTransactionSummary do use Cratis.Chronicle.ReadModels.ReadModel
defstruct transaction_count: 0, total_amount: 0, processed_events: 0end
defmodule MyApp.Projections.DecFunctionsTransactionSummaryProjection do use Cratis.Chronicle.Projections.Projection, model: MyApp.ReadModels.DecFunctionsTransactionSummary
alias MyApp.Events.DecFunctionsTransaction
from DecFunctionsTransaction, count: :transaction_count, add: [total_amount: :amount, processed_events: 1]endimport { eventType, IProjectionBuilderFor, IProjectionFor, projection } from '@cratis/chronicle';import { field } from '@cratis/fundamentals';
@eventType()class DecFunctionsTransaction { @field(Number) readonly amount: number;
constructor(amount: number) { this.amount = amount; }}
class DecFunctionsTransactionSummary { transactionCount = 0; totalAmount = 0; processedEvents = 0;}
@projection()class DecFunctionsTransactionSummaryProjection implements IProjectionFor<DecFunctionsTransactionSummary> { define(builder: IProjectionBuilderFor<DecFunctionsTransactionSummary>): void { builder .from(DecFunctionsTransaction, _ => _ .count(m => m.transactionCount) .add(m => m.totalAmount).with(e => e.amount) .increment(m => m.processedEvents)); }}For a field that should copy a value rather than accumulate it, use Set properties. For a timestamp that should follow every handled event, use FromEvery.