Aggregating data to a list

Example Events

A common use-case is collecting some data across all aggregates for a given type. In the example below we want to be able to get a list of all registered Customers from a single query to a Projection.
Let's say two Customers have been registered in our system with the following two events:
1
{
2
"aggregateId":"723ecfce-14e9-4889-98d5-a3d0ad54912f",
3
"events":[
4
{
5
"eventId":"127b80b5-4a05-4774-b870-1c9a2e2a27a3",
6
"eventType":"CustomerRegisteredEvent",
7
"data":{
8
"customer": "Acme Inc"
9
}
10
}
11
]
12
}
Copied!
1
{
2
"aggregateId":"cf26e6df-a883-4fbc-929b-47052f0acdb8",
3
"events":[
4
{
5
"eventId":"982f8747-ba7d-470e-beca-1582c48d3181",
6
"eventType":"CustomerRegisteredEvent",
7
"data":{
8
"customer": "The Trading Company"
9
}
10
}
11
]
12
}
Copied!

Projection definition

We can create an aggregated Projection that appends all Customer names to a field in our Aggregated Projection called registered-customers.
1
{
2
"aggregated": true,
3
"projectionName": "registered-customers",
4
"feedName": "customer",
5
"handlers": [
6
{
7
"eventType": "CustomerRegisteredEvent",
8
"functions": [
9
{
10
"function": "append",
11
"eventSelector": "$.event.customer",
12
"targetSelector": "$.projection.customers"
13
}
14
]
15
}
16
]
17
}
Copied!

Results

When we query the Aggregated Projection we will now get the following result.
/projections/aggregated/registered-customers
1
{
2
"projectionId" : "registered-customers",
3
"createdAt" : 1523518145532,
4
"updatedAt" : 1523518146253,
5
"data": {
6
"customers": ["Acme Inc", "The Trading Company"]
7
}
8
}
Copied!
Last modified 11mo ago