Event Context
Event context provides access to metadata about the event itself, such as when it occurred, its sequence number, and correlation information.
Syntax
Section titled “Syntax”Access event context properties using the $eventContext. prefix:
$eventContext.{property}Available Properties
Section titled “Available Properties”occurred
Section titled “occurred”The timestamp when the event occurred:
from UserRegistered Name = name CreatedAt = $eventContext.occurredsequenceNumber
Section titled “sequenceNumber”The sequence number of the event in its stream:
from EventProcessed LastSequenceNumber = $eventContext.sequenceNumbercorrelationId
Section titled “correlationId”The correlation ID associated with the event:
from OrderPlaced OrderId = orderId CorrelationId = $eventContext.correlationIdeventSourceId
Section titled “eventSourceId”The event source ID (also available as shorthand $eventSourceId):
from UserAssignedToGroup GroupId = $eventContext.eventSourceId # or GroupId = $eventSourceIdEvent Source ID Shorthand
Section titled “Event Source ID Shorthand”The $eventSourceId is a shorthand for $eventContext.eventSourceId:
from UserCreated key $eventSourceId Name = nameCommon Patterns
Section titled “Common Patterns”Audit Fields
Section titled “Audit Fields”Track when things happen:
from RecordCreated Name = name CreatedAt = $eventContext.occurred
from RecordUpdated Name = name UpdatedAt = $eventContext.occurredCorrelation Tracking
Section titled “Correlation Tracking”Link related operations:
from OrderPlaced OrderNumber = orderNumber CorrelationId = $eventContext.correlationId PlacedAt = $eventContext.occurredSequence Tracking
Section titled “Sequence Tracking”Track event order:
from StateChanged CurrentState = state SequenceNumber = $eventContext.sequenceNumber ChangedAt = $eventContext.occurredParent-Child Relationships
Section titled “Parent-Child Relationships”Use event source ID for relationships:
children members id userId from UserAddedToGroup key userId parent $eventContext.eventSourceId Role = role AddedAt = $eventContext.occurredIn Composite Keys
Section titled “In Composite Keys”Event context can be used in composite keys:
from EventProcessed key ProcessingKey { EventId = eventId SequenceNumber = $eventContext.sequenceNumber CorrelationId = $eventContext.correlationId } ProcessedAt = $eventContext.occurredIn Templates
Section titled “In Templates”Event context works in string templates:
from OrderShipped TrackingInfo = `Shipped at ${$eventContext.occurred}` Reference = `${orderNumber}-${$eventContext.sequenceNumber}`Examples
Section titled “Examples”User Activity Log
Section titled “User Activity Log”projection UserActivity => UserActivityReadModel from UserLoggedIn LastLogin = $eventContext.occurred LastLoginSequence = $eventContext.sequenceNumber count LoginCount
from UserAction LastAction = actionType LastActionTime = $eventContext.occurred LastCorrelation = $eventContext.correlationIdVersioned Document
Section titled “Versioned Document”projection Document => DocumentReadModel from DocumentCreated Title = title Content = content Version = $eventContext.sequenceNumber CreatedAt = $eventContext.occurred
from DocumentUpdated Content = content Version = $eventContext.sequenceNumber UpdatedAt = $eventContext.occurredGroup Membership with Audit
Section titled “Group Membership with Audit”projection Group => GroupReadModel from GroupCreated Name = name CreatedAt = $eventContext.occurred CreatedBy = $eventContext.eventSourceId
children members id userId from UserAddedToGroup key userId parent groupId Name = userName Role = role AddedAt = $eventContext.occurred AddedSequence = $eventContext.sequenceNumberOrder Processing
Section titled “Order Processing”projection Order => OrderReadModel from OrderPlaced OrderNumber = orderNumber CustomerId = customerId Total = total PlacedAt = $eventContext.occurred TrackingId = $eventContext.correlationId
from OrderShipped ShippedAt = $eventContext.occurred ShipmentSequence = $eventContext.sequenceNumber Status = "Shipped"Best Practices
Section titled “Best Practices”- Audit Timestamps: Use
occurredfor created/updated timestamps - Correlation: Use
correlationIdto link related operations - Versioning: Use
sequenceNumberfor version tracking - Relationships: Use
eventSourceIdfor parent-child relationships - Debugging: Include correlation IDs and timestamps for troubleshooting
- Immutability: Event context values are immutable and reliable