From Every
The every directive applies mappings to every event that affects the projection, regardless of event type. This is useful for setting common properties like last update timestamps.
Basic Syntax
Section titled “Basic Syntax”every {mappings}Simple Example
Section titled “Simple Example”projection User => UserReadModel every LastUpdated = $eventContext.occurred UpdatedBy = $eventContext.eventSourceId
from UserRegistered Name = name Email = email
from UserEmailChanged Email = emailBoth UserRegistered and UserEmailChanged events will set LastUpdated and UpdatedBy.
With AutoMap
Section titled “With AutoMap”Apply AutoMap to all events:
projection Product => ProductReadModel every automap LastModified = $eventContext.occurredExclude Children
Section titled “Exclude Children”By default, every applies to events in child collections. To exclude them:
every LastUpdated = $eventContext.occurred exclude childrenThis ensures child events don’t update the parent’s LastUpdated field.
Common Patterns
Section titled “Common Patterns”Audit Fields
Section titled “Audit Fields”Track when the projection was last modified:
every LastModified = $eventContext.occurred LastSequenceNumber = $eventContext.sequenceNumber LastCorrelationId = $eventContext.correlationIdEvent Source Tracking
Section titled “Event Source Tracking”Store the event source that last modified the projection:
every LastModifiedBy = $eventContext.eventSourceId LastModifiedAt = $eventContext.occurredVersion Tracking
Section titled “Version Tracking”Track the latest event sequence:
every Version = $eventContext.sequenceNumber UpdatedAt = $eventContext.occurredExamples
Section titled “Examples”User Profile with Audit
Section titled “User Profile with Audit”projection User => UserReadModel every LastUpdated = $eventContext.occurred LastSequenceNumber = $eventContext.sequenceNumber exclude children
from UserRegistered Name = name Email = email CreatedAt = $eventContext.occurred
from UserEmailChanged Email = email
from UserNameChanged Name = nameProduct with Global AutoMap
Section titled “Product with Global AutoMap”projection Product => ProductReadModel every automap LastModified = $eventContext.occurred
from ProductCreated IsActive = true
from ProductDeactivated IsActive = falseDocument Versioning
Section titled “Document Versioning”projection Document => DocumentReadModel every CurrentVersion = $eventContext.sequenceNumber LastUpdated = $eventContext.occurred LastCorrelation = $eventContext.correlationId exclude children
from DocumentCreated Title = title Content = content
from DocumentUpdated Content = content
from DocumentRenamed Title = titleOrder with Tracking
Section titled “Order with Tracking”projection Order => OrderReadModel every LastEventTime = $eventContext.occurred EventCount = $eventContext.sequenceNumber exclude children
from OrderPlaced CustomerId = customerId Total = total Status = "Placed"
from OrderShipped ShippedAt = $eventContext.occurred Status = "Shipped"
from OrderDelivered DeliveredAt = $eventContext.occurred Status = "Delivered"
children items id itemId from ItemAdded ProductId = productId Quantity = quantityScope and Application
Section titled “Scope and Application”Parent Level Only (with exclude children)
Section titled “Parent Level Only (with exclude children)”every LastUpdated = $eventContext.occurred exclude childrenParent events update LastUpdated, child events do not.
Including Children (default)
Section titled “Including Children (default)”every LastUpdated = $eventContext.occurredBoth parent and child events update LastUpdated.
With Multiple Children
Section titled “With Multiple Children”projection Group => GroupReadModel every LastActivity = $eventContext.occurred exclude children
from GroupCreated Name = name
children members id userId from UserAdded Name = userName
children posts id postId from PostCreated Title = titleGroup-level events update LastActivity, but member and post events do not.
Best Practices
Section titled “Best Practices”- Use for Common Fields: Apply
everyfor timestamps and audit fields that should update with any event - Exclude Children: Use
exclude childrenwhen parent and child lifecycles are independent - Keep It Simple: Limit
everyto truly universal properties - Combine with Specific Rules: Use
fromevents for specific updates,everyfor universal ones - AutoMap Carefully: Global AutoMap can have unintended effects; use selectively
- Version Tracking: Sequence numbers are ideal for optimistic concurrency control
When to Use
Section titled “When to Use”Use every when:
- Setting last modified timestamps
- Tracking latest sequence numbers
- Maintaining version counters
- Storing correlation IDs for all events
- Applying AutoMap universally
Avoid every when:
- Only specific events should update a property
- Different events require different logic
- Children should be treated independently