Skip to content

Chronicle FromEvery mappings for audit fields

Use FromEvery in a Cratis Chronicle projection to update shared fields, such as a last-updated timestamp, whenever the projection handles an event. It saves you from repeating the same mapping in each event declaration. It does not subscribe the projection to unrelated event types; use FromAll for that.

The client APIs use different names: .NET has FromEvery, TypeScript has fromEvery, and Elixir has from_every. The examples show the declaration for each client.

Map the event’s occurrence time when the read model needs to show when it was last touched. Event context is the metadata stored with an event, separate from its payload:

Declarative FromEvery
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record UserCreatedDeclarativeEvery(string Name, string Email);
[EventType]
public record UserEmailChangedDeclarativeEvery(string Email);
public record UserProfileDeclarativeEvery(
string Name,
string Email,
DateTimeOffset LastUpdated);
public class UserProfileDeclarativeEveryProjection : IProjectionFor<UserProfileDeclarativeEvery>
{
public void Define(IProjectionBuilderFor<UserProfileDeclarativeEvery> builder) => builder
.From<UserCreatedDeclarativeEvery>()
.From<UserEmailChangedDeclarativeEvery>()
.FromEvery(_ => _
.Set(m => m.LastUpdated)
.ToEventContextProperty(c => c.Occurred));
}

View C# snippet source on GitHub

Keep several metadata fields together when readers need to trace a change. For example, an occurrence time tells them when it happened, while a correlation ID links events from the same operation:

Map multiple context fields
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record AccountTouchedDeclarativeEvery(string Reason);
public record AccountAuditDeclarativeEvery(
DateTimeOffset LastUpdated,
EventSequenceNumber LastEventSequence,
string LastCorrelationId);
public class AccountAuditDeclarativeEveryProjection : IProjectionFor<AccountAuditDeclarativeEvery>
{
public void Define(IProjectionBuilderFor<AccountAuditDeclarativeEvery> builder) => builder
.From<AccountTouchedDeclarativeEvery>()
.FromEvery(_ => _
.Set(m => m.LastUpdated).ToEventContextProperty(c => c.Occurred)
.Set(m => m.LastEventSequence).ToEventContextProperty(c => c.SequenceNumber)
.Set(m => m.LastCorrelationId).ToEventContextProperty(c => c.CorrelationId));
}

View C# snippet source on GitHub

Map the event source ID when a read model property needs the stream identifier even though events do not carry it in their payload. This sets a property; it does not change the projection’s key:

Map the event source id
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record AccountOpenedDeclarativeEvery(string OwnerName);
public record AccountSummaryDeclarativeEvery(
string AccountId,
string OwnerName);
public class AccountSummaryDeclarativeEveryProjection : IProjectionFor<AccountSummaryDeclarativeEvery>
{
public void Define(IProjectionBuilderFor<AccountSummaryDeclarativeEvery> builder) => builder
.From<AccountOpenedDeclarativeEvery>()
.FromEvery(_ => _
.Set(m => m.AccountId)
.ToEventSourceId());
}

View C# snippet source on GitHub

In .NET, FromEvery and FromAll both support .Set(m => m.Status).ToValue("active") to assign a constant whenever a matching event is processed. Concept values are unwrapped. Enums with an int underlying type use their numeric value; other enums use their name. Use .ToValue<string?>(null) to clear a nullable property.

Pass a value of the read model property’s own type. ToValue infers its type from the supplied value, not the target property, so an incompatible value can compile without being reported when the projection is defined.

In .NET, FromEvery includes events handled only by child projections by default, including events in nested child collections. Those events update the containing parent’s every-event fields using the child’s resolved parent key. They do not create a separate root keyed by the child identifier. A parent’s last-updated timestamp can therefore reflect changes to its children as well as its own events.

Call ExcludeChildProjections() when the field should track only the level where you declared it. That level’s own from and join events still apply the mappings. In the declaration language, every also includes child events by default; add exclude children to opt out.

Exclude child projection events
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record OrderCreatedDeclarativeEveryExclude(string OrderNumber);
public record OrderAuditDeclarativeEveryExclude(
string OrderNumber,
DateTimeOffset LastUpdated);
public class OrderAuditDeclarativeEveryExcludeProjection : IProjectionFor<OrderAuditDeclarativeEveryExclude>
{
public void Define(IProjectionBuilderFor<OrderAuditDeclarativeEveryExclude> builder) => builder
.From<OrderCreatedDeclarativeEveryExclude>()
.FromEvery(_ => _
.Set(m => m.LastUpdated)
.ToEventContextProperty(c => c.Occurred)
.ExcludeChildProjections());
}

View C# snippet source on GitHub

Keep FromEvery when a parent’s audit fields should follow only events the projection already handles. A root FromAll mapping also subscribes to unrelated event types in the sequence and includes child events by default; choose it only when you need that wider subscription.

Include child projection events
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record OrderCreatedDeclarativeEveryChildren(string OrderNumber);
[EventType]
public record ItemAddedDeclarativeEveryChildren(string OrderId, string ProductId, string ProductName, int Quantity);
[EventType]
public record ItemQuantityChangedDeclarativeEveryChildren(string OrderId, string ProductId, int Quantity);
public record OrderDeclarativeEveryChildren(
string OrderNumber,
DateTimeOffset LastModified,
IEnumerable<OrderItemDeclarativeEveryChildren> Items);
public record OrderItemDeclarativeEveryChildren(
string ProductId,
string ProductName,
int Quantity);
public class OrderDeclarativeEveryChildrenProjection : IProjectionFor<OrderDeclarativeEveryChildren>
{
public void Define(IProjectionBuilderFor<OrderDeclarativeEveryChildren> builder) => builder
.From<OrderCreatedDeclarativeEveryChildren>()
.FromEvery(_ => _
.Set(m => m.LastModified)
.ToEventContextProperty(c => c.Occurred))
.Children(m => m.Items, children => children
.IdentifiedBy(m => m.ProductId)
.From<ItemAddedDeclarativeEveryChildren>(_ => _
.UsingKey(e => e.ProductId)
.UsingParentKey(e => e.OrderId))
.From<ItemQuantityChangedDeclarativeEveryChildren>(_ => _
.UsingKey(e => e.ProductId)
.UsingParentKey(e => e.OrderId)));
}

View C# snippet source on GitHub

In .NET, separate FromEvery calls merge their property mappings into one definition. Give each property one mapping rather than declaring the same target in several calls:

Multiple FromEvery declarations
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
[EventType]
public record UserChangedDeclarativeEveryMultiple(string Name);
public record UserAuditDeclarativeEveryMultiple(
string Name,
DateTimeOffset LastUpdated,
string ModifiedBy);
public class UserAuditDeclarativeEveryMultipleProjection : IProjectionFor<UserAuditDeclarativeEveryMultiple>
{
public void Define(IProjectionBuilderFor<UserAuditDeclarativeEveryMultiple> builder) => builder
.From<UserChangedDeclarativeEveryMultiple>()
.FromEvery(_ => _
.Set(m => m.LastUpdated)
.ToEventContextProperty(c => c.Occurred))
.FromEvery(_ => _
.Set(m => m.ModifiedBy)
.ToEventContextProperty(c => c.CausedBy));
}

View C# snippet source on GitHub

Use case Typical source
Audit timestamp Event context occurrence time
Event sequencing Event context sequence number
Correlation tracking Event context correlation id
Event source identification Event source id

If only selected events should update a property, put its mapping in those events’ Set properties declarations instead. For a value that should exist before any mapping assigns it, use Initial values.