Creating projections

A projection is a use-case tailored structural representation of multiple events.

Create projection definition

If you performed the previous step you now have a stream of order events. To be able to query the current state of a particular order, without having to load all its events, we could configure a projection.

We use a POST command to create our 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": "orders",
  "feedName": "order",
  "handlers": [
    {
      "eventType": "OrderPlacedEvent",
      "functions": [
        {
          "function": "set",
          "targetSelector": "$.projection.status",
          "rawData": "PLACED"
        },
        {
          "function": "set",
          "targetSelector": "$.projection.orderAmount",
          "eventSelector": "$.event.orderAmount"
        }
      ]
    },
    {
      "eventType": "OrderPaidEvent",
      "functions": [
        {
          "function": "set",
          "targetSelector": "$.projection.status",
          "rawData": "PAID"
        }
      ]
    },
    {
      "eventType": "OrderShippedEvent",
      "functions": [
        {
          "function": "set",
          "targetSelector": "$.projection.status",
          "rawData": "SHIPPED"
        }
      ]
    },
    {
      "eventType": "OrderCancelledEvent",
      "functions": [
        {
          "function": "set",
          "targetSelector": "$.projection.status",
          "rawData": "CANCELLED"
        }
      ]
    }
  ]
}
'

Query a projection

Great! We can now access the projection and check the current state of our order!

curl -i \
  --header "Serialized-Access-Key: <YOUR_ACCESS_KEY>" \
  --header "Serialized-Secret-Access-Key: <YOUR_SECRET_ACCESS_KEY>" \
  https://api.serialized.io/projections/single/orders/723ecfce-14e9-4889-98d5-a3d0ad54912f
HTTP/1.1 200 OK
Content-Type: application/json
ETag: "c604ac5bd1a51a798accc5717bd5109f"
Last-Modified: Mon, 18 Sep 2017 17:01:23 GMT
Vary: Accept-Encoding
Content-Length: 125

{
  "projectionId":"723ecfce-14e9-4889-98d5-a3d0ad54912f",
  "updatedAt":1505754083976,
  "data": {
    "orderAmount":"12345",
    "status":"PAID"
  }
}

You can also create aggregated projections!