Read a collection of Cratis Chronicle read models
Read a whole collection in Cratis Chronicle when a small report or diagnostic task needs every instance of a read model. Without an event-count limit, the read returns stored instances for a materialized model and computes passive models from events.
The caller receives the entire result, so use materialized paging for list views or large data sets. A small number of instances can still have a long event history; allow for replay work as well as the size of the returned collection.
Read all instances
Section titled “Read all instances”var accounts = await eventStore.ReadModels.GetInstances<Account>();
foreach (var account in accounts){ Console.WriteLine($"{account.Name}: {account.Balance:C}");}import io.cratis.chronicle.IEventStoreimport io.cratis.chronicle.readModels.ReadModel
@ReadModeldata class GettingCollectionAccount(val name: String = "", val balance: Double = 0.0)
suspend fun printAllAccounts(store: IEventStore) { val accounts = store.readModels.getInstances(GettingCollectionAccount::class) accounts.forEach { println("${it.name}: ${it.balance}") }}import io.cratis.chronicle.EventStore;import io.cratis.chronicle.readModels.ReadModel;
import java.util.List;
import io.cratis.chronicle.java.ReadModelsJavaBridge;
@ReadModelclass GettingCollectionAccount { private String name = ""; private double balance = 0;
public String getName() { return name; } public void setName(String name) { this.name = name; }
public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; }}
class ReadModelsGettingCollectionInstancesBasic { void printAllAccounts(EventStore store) { List<GettingCollectionAccount> accounts = ReadModelsJavaBridge.getInstances(store.getReadModels(), GettingCollectionAccount.class); accounts.forEach(account -> System.out.println(account.getName() + ": " + account.getBalance())); }}alias MyApp.ReadModels.Account
{:ok, accounts} = Cratis.Chronicle.ReadModels.get_instances(Account)
Enum.each(accounts, fn account -> IO.puts("#{account.name}: #{account.balance}")end)const accounts = await store.readModels.getInstances(Account);
for (const account of accounts) { console.log(`${account.name}: ${account.balance}`);}For a materialized model, the kernel pages through the sink, the storage destination updated by its observer, and releases compliance-protected values before returning the stored instances. The result reflects observer progress; reading all instances does not wait for the observer to catch up.
For a passive model, the read processes the events that feed it and returns the computed instances. The work grows with the relevant event history. This is a collection read, not a guard for a later append; use decision-consistent reads when a command depends on state remaining unchanged.
A per-stream read model cannot be read as a whole collection. Read one source and stream instance explicitly instead.
Filter in memory
Section titled “Filter in memory”Filtering after the read does not reduce the events processed or the instances returned to the client. Use language-native filtering when the complete collection is small enough to load first.
var accounts = await eventStore.ReadModels.GetInstances<Account>();
var highValueAccounts = accounts .Where(account => account.Balance > threshold) .OrderByDescending(account => account.Balance) .ToList();
Console.WriteLine($"Found {highValueAccounts.Count} high-value accounts.");import io.cratis.chronicle.IEventStoreimport io.cratis.chronicle.readModels.ReadModel
@ReadModeldata class GettingCollectionFilteringAccount(val name: String = "", val balance: Double = 0.0)
/** * The read returns every instance; apply language-native filtering afterwards. */suspend fun highValueAccounts(store: IEventStore, threshold: Double): List<GettingCollectionFilteringAccount> = store.readModels.getInstances(GettingCollectionFilteringAccount::class) .filter { it.balance > threshold } .sortedByDescending { it.balance }import io.cratis.chronicle.EventStore;import io.cratis.chronicle.readModels.ReadModel;
import java.util.Comparator;import java.util.List;import java.util.stream.Collectors;
import io.cratis.chronicle.java.ReadModelsJavaBridge;
@ReadModelclass GettingCollectionFilteringAccount { private String name = ""; private double balance = 0;
public String getName() { return name; } public void setName(String name) { this.name = name; }
public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; }}
class ReadModelsGettingCollectionInstancesFiltering { // The read returns every instance; apply language-native filtering afterwards. List<GettingCollectionFilteringAccount> highValueAccounts(EventStore store, double threshold) { List<GettingCollectionFilteringAccount> accounts = ReadModelsJavaBridge.getInstances(store.getReadModels(), GettingCollectionFilteringAccount.class); return accounts.stream() .filter(account -> account.getBalance() > threshold) .sorted(Comparator.comparingDouble(GettingCollectionFilteringAccount::getBalance).reversed()) .collect(Collectors.toList()); }}alias MyApp.ReadModels.Account
{:ok, accounts} = Cratis.Chronicle.ReadModels.get_instances(Account)
high_value_accounts = accounts |> Enum.filter(&(&1.balance > threshold)) |> Enum.sort_by(& &1.balance, :desc)
IO.inspect(high_value_accounts, label: "High-value accounts")const accounts = await store.readModels.getInstances(Account);
const highValueAccounts = accounts .filter((account) => account.balance > threshold) .sort((left, right) => right.balance - left.balance);For large result sets, page the materialized read model or use a product-specific query model instead of reading everything and filtering afterward.
Limit the replay
Section titled “Limit the replay”Pass an event-count cap to process a limited number of matching events from the beginning of the history. This requests replay even for a materialized model; it does not limit the number of stored instances returned.
var orders = await eventStore.ReadModels.GetInstances<Order>(eventCount: 1_000);
Console.WriteLine($"Replayed {orders.Count()} orders from the capped history.");import io.cratis.chronicle.IEventStoreimport io.cratis.chronicle.readModels.ReadModel
@ReadModeldata class EventCountOrder(val status: String = "", val total: Double = 0.0)
/** * Caps the replay to the first 1,000 events - faster, but can return incomplete state if the * cap cuts off events that matter. */suspend fun replayCappedOrders(store: IEventStore): List<EventCountOrder> = store.readModels.getInstances(EventCountOrder::class, eventCount = 1_000)import io.cratis.chronicle.IEventStore;import io.cratis.chronicle.java.ReadModelsJavaBridge;import io.cratis.chronicle.readModels.ReadModel;
import java.util.List;
@ReadModelrecord EventCountOrder(String status, double total) {}
class EventCountOrders { /** * Caps the replay to the first 1,000 events - faster, but can return incomplete state if the * cap cuts off events that matter. */ static List<EventCountOrder> replayCappedOrders(IEventStore store) { return ReadModelsJavaBridge.getInstances(store.getReadModels(), EventCountOrder.class, 1_000L); }}alias MyApp.ReadModels.Order
{:ok, orders} = Cratis.Chronicle.ReadModels.get_instances( Order, event_count: 1_000 )
IO.puts("Replayed #{length(orders)} orders from the capped history.")const orders = await store.readModels.getInstances(Order, 1000n);
console.log(`Replayed ${orders.length} orders from the capped history.`);A cap bounds replay work, but later events may change or remove the instances it returns. Use it to inspect a prefix of history for diagnostics or back-testing, not when the reader expects current state. For historical state of one key, continue with Getting Snapshots.
When to use this
Section titled “When to use this”Read the whole collection when:
- The read model population is small.
- You are running an administrative or diagnostic task.
- You are validating data derived from the event log.
Prefer paging or a database-native read when:
- The page is user-facing and called frequently.
- You need server-side filtering, sorting, or paging.
- The read model has many instances.
Related topics
Section titled “Related topics”- Getting a Single Instance - Read one read model instance by key
- Getting Snapshots - Inspect historical state for one instance
- Watching Read Models - React to read model changes
- Materialized Read Models - Read sink-stored read models with paging