Screenplay counters: count events and unit changes
Counters in a Cratis Screenplay projection change a numeric read model property by one per handled event. Use count to express event occurrences, increment for a unit increase, and decrement for a unit decrease. When one event carries an amount such as a stock quantity or payment value, use arithmetic instead.
A page-view counter answers how often a page was viewed, regardless of the other data on each event. Place count in that event’s mapping block:
count {Property}This mapping excerpt also records when the latest view occurred:
from PageViewed count ViewCount LastViewedAt = $eventContext.occurredEach PageViewed event increases ViewCount by 1.
Increment
Section titled “Increment”Use increment when a one-unit increase best describes the change:
increment {Property}For a login count, it produces the same numeric change as count:
from UserLoggedIn increment LoginCount LastLogin = $eventContext.occurredDecrement
Section titled “Decrement”Use decrement when each event represents exactly one unit leaving the total:
decrement {Property}This excerpt treats each ItemConsumed event as one item consumed:
from ItemConsumed decrement RemainingStock LastConsumed = $eventContext.occurredCount per key
Section titled “Count per key”A counter target can end in a dynamic key, so one dictionary property keeps a count per distinct value:
projection Statistics => StatisticsReadModel from SlotAssigned count assignmentsPerSlot.$eventContent.slot increment assignmentsPerUser.$causedBy.userNameSee Dictionary entries for the keys you can use.
Difference Between Count and Increment
Section titled “Difference Between Count and Increment”count and increment execute the same operation: both add one. Choose the word that makes the model’s intent clearer. Neither sums an event property or counts distinct values; two handled occurrences each add one.
Track one item per stock event
Section titled “Track one item per stock event”This inventory excerpt starts from an explicit stock level. Each later event represents a single item:
projection Product => ProductReadModel from ProductCreated Name = name StockLevel = initialStock
from ItemAdded increment StockLevel
from ItemSold decrement StockLevel count SalesCount
from ItemReturned increment StockLevel decrement SalesCountAn ItemSold event reduces stock by one and increases SalesCount by one. ItemReturned reverses both changes, so SalesCount here means sales less returns. If a sale can contain several items, subtract its quantity with arithmetic instead of decrementing stock once.
Count transactions while summing amounts
Section titled “Count transactions while summing amounts”Counters and arithmetic answer different questions about the same event. This account excerpt counts each deposit or withdrawal once while changing the balance by its amount:
projection Account => AccountReadModel from AccountOpened AccountNumber = accountNumber Balance = initialBalance
from DepositMade increment TransactionCount add Balance by amount
from WithdrawalMade increment TransactionCount subtract Balance by amountA deposit of 100 increases TransactionCount by 1 and Balance by 100. A second deposit of 25 increases the count by another 1 and the balance by 25. Choosing increment Balance would lose the amounts.
Move a task between totals
Section titled “Move a task between totals”Several counters can change for one event when they describe different totals. Completing a task moves it from remaining work into completed work:
projection Project => ProjectReadModel from ProjectCreated Name = name
from TaskAdded increment TotalTasks increment RemainingTasks
from TaskCompleted increment CompletedTasks decrement RemainingTasksTotalTasks stays unchanged when a task completes. Only CompletedTasks and RemainingTasks move, preserving the meaning of the overall total.
Property Requirements
Section titled “Property Requirements”Declare counter targets as numeric properties on the read model, such as Int or a concept based on Int. A counter targeting a non-numeric property fails semantic validation.
Initial Values
Section titled “Initial Values”The reference evaluator starts a missing numeric target at zero, so the first count or increment produces 1. Assign an initial value when the starting state comes from an event, as StockLevel = initialStock does above.
A decrement can take a value below zero; it does not enforce a lower bound. Put a rule such as “stock must not become negative” in the behavior that decides whether the event can occur, rather than relying on a counter to reject it.
See Also
Section titled “See Also”For events carrying quantities, continue with Arithmetic. To replace a value instead of accumulating it, use Property Mapping.