Skip to content

Cratis Chronicle ad-hoc projection queries

Use IProjections.Query() in the Cratis Chronicle .NET client to inspect projected state without registering a permanent projection. Send a Projection Declaration Language (PDL) declaration and receive read model entries as JSON strings. You can omit the read model type when exploring a small set of events.

Start with a connected .NET client and registered event types for the events named in your declaration. Inject IProjections into your service, then call Query() with the PDL string:

using Cratis.Chronicle.Projections;
using System.Text.Json;
public class PdlOrderQueryService(IProjections projections)
{
public async Task<IEnumerable<PdlOrderSummary>> GetOrderSummaries()
{
var result = await projections.Query("""
projection OrderSummary
from OrderPlaced
""");
return result.ReadModelEntries
.Select(json => JsonSerializer.Deserialize<PdlOrderSummary>(json)!);
}
}
public record PdlOrderSummary(string OrderId);

View C# snippet source on GitHub

ReadModelEntries is an IReadOnlyList<string>. Each string represents one projected read model instance. The example deserializes those strings into PdlOrderSummary records for the caller; that local result type does not need to be registered as a read model.

Include => ReadModelType when you want to use an existing registered read model schema. The target must be its registered identifier, such as MyApp.ReadModels.PdlOrderReadModel, rather than a display name. Omit the target to let the server infer a schema from the event properties.

// Inferred — schema derived from OrderPlaced and OrderShipped event properties
var inferred = await projections.Query("""
projection Orders
from OrderPlaced
from OrderShipped
""");
// Explicit — assuming PdlOrderReadModel is registered in the MyApp.ReadModels namespace
var explicitResult = await projections.Query("""
projection Orders => MyApp.ReadModels.PdlOrderReadModel
from OrderPlaced
from OrderShipped
""");

View C# snippet source on GitHub

In the inferred declaration, both from blocks contribute properties to the same schema. If those events share a property name, its types must be compatible. A string in one event and an integer in another produces a compiler error at query time:

// This declaration will throw UnableToQueryProjection:
// OrderPlaced.value is string, but OrderShipped.value is int
var result = await projections.Query("""
projection Bad
from OrderPlaced // value: string
from OrderShipped // value: int → incompatible types
""");

View C# snippet source on GitHub

Query() throws UnableToQueryProjection when the declaration contains syntax or type errors. Read the exception message, correct the declaration or conflicting event property types, and submit the query again:

try
{
var result = await projections.Query("""
projection Orders
from OrderPlaced
""");
}
catch (UnableToQueryProjection ex)
{
Console.WriteLine(ex.Message);
}

View C# snippet source on GitHub

By default, Query() reads from the event-log sequence in the client’s event store and namespace. Pass a different sequence identifier as the second argument to inspect another sequence:

var result = await projections.Query(
"""
projection InboxMessages
from MessageReceived
""",
eventSequenceId: "inbox");

View C# snippet source on GitHub

Use an ad-hoc query when you need a one-off view of a known, small event set: inspecting an order during diagnosis, exploring event data while developing a feature, or asserting projected state in a test. Check the number of matching events before relying on the result. A successful call does not establish that the query processed the full history.

Concern Detail
Event volume The server reads at most 1,000 matching events. More matching events mean incomplete results.
No persistence Results are not stored. Every call processes events again from the beginning of the selected sequence.
No registration Query() does not register a projection, with or without an explicit => ReadModelType target.
No change notifications Results are a point-in-time snapshot; this API has no observable or reactive variant.

For a view that stays up to date as events arrive, define a registered projection. For declaration syntax and Chronicle’s naming rules, return to Projection Declaration Language.