Aggregating events

Projections can also be configured to merge events from different aggregates which makes it easy to build things such as reporting features or similar.

Creating your first aggregated projection

To illustrate aggregated projections we will create an aggregated projection that counts the amount of paid orders and their total amount. We use a POST command to create a new projection definition:

cURL
curl -i https://api.serialized.io/projections/definitions \
--header "Content-Type: application/json" \
--header "Serialized-Access-Key: <YOUR_ACCESS_KEY>" \
--header "Serialized-Secret-Access-Key: <YOUR_SECRET_ACCESS_KEY>" \
--data '
{
"projectionName": "order-totals",
"feedName": "order",
"aggregated": true,
"handlers": [
{
"eventType": "OrderPlacedEvent",
"functions": [
{
"function": "add",
"targetSelector": "$.projection.orderAmount",
"eventSelector": "$.event.orderAmount"
},
{
"function": "inc",
"targetSelector": "$.projection.orderCount"
}
]
}
]
}
'

Query an aggregated projection

Since aggregated projections are combinations of multiple aggregates they don’t have any id’s. We query them directly by their name:

cURL
curl -i \
--header "Serialized-Access-Key: <YOUR_ACCESS_KEY>" \
--header "Serialized-Secret-Access-Key: <YOUR_SECRET_ACCESS_KEY>" \
https://api.serialized.io/projections/aggregated/order-totals
Response
HTTP/1.1 200 OK
Content-Type: application/json
Date: Wed, 20 Sep 2017 11:45:27 GMT
ETag: "7b2b05dab4a33088d9aee48b90c4a786"
Last-Modified: Tue, 19 Sep 2017 19:53:08 GMT
Vary: Accept-Encoding
Content-Length: 91
Connection: keep-alive
{
"projectionId": "order-totals",
"updatedAt": 1505850788368,
"data": {
"orderAmount":"1000",
"orderCount":"2"
}
}

Given that we have two OrderPlacedEvent that together sum up to 1000 we should get the response above.