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 -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"}]}]}'
Since aggregated projections are combinations of multiple aggregates they don’t have any id’s. We query them directly by their name:
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
ResponseHTTP/1.1 200 OKContent-Type: application/jsonDate: Wed, 20 Sep 2017 11:45:27 GMTETag: "7b2b05dab4a33088d9aee48b90c4a786"Last-Modified: Tue, 19 Sep 2017 19:53:08 GMTVary: Accept-EncodingContent-Length: 91Connection: 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.