Projection examples

How to project one field from all aggregates, for a given type, into a single list

Aggregate type: customer

Event: CustomerRegisteredEvent

  {  
     "aggregateId":"723ecfce-14e9-4889-98d5-a3d0ad54912f",
     "events":[  
        {  
           "eventId":"127b80b5-4a05-4774-b870-1c9a2e2a27a3",
           "eventType":"CustomerRegisteredEvent",
           "data":{  
              "customerId": "673b3926-3667-4daf-9db7-03c08adc5ffd"
           }
        }
     ]
  }

Projection definition: customer-ids

{
  "aggregated": true,
  "projectionName": "customer-ids",
  "feedName": "customer",
  "handlers": [
    {
      "eventType": "CustomerRegisteredEvent",
      "functions": [
        {
          "function": "push",
          "eventSelector": "$.event.customerId",
          "targetSelector": "$.projection.customerIds"
        }
      ]
    }
  ]
}

Projection result

Requesting the URL https://api.serialized.io/projections/aggregated/customer-ids returns:

{
  "projectionId" : "customer-ids",       
  "createdAt" : 1523518145532,                                   
  "updatedAt" : 1523518146253,                                   
  "data": {
    "customerIds": ["673b3926-3667-4daf-9db7-03c08adc5ffd"]
  }
}

How to add and remove from a projected list

Aggregate type: Meeting

Event: InvitationAcceptedEvent

  {  
     "aggregateId":"723ecfce-14e9-4889-98d5-a3d0ad54912f",
     "events":[  
        {  
           "eventId":"127b80b5-4a05-4774-b870-1c9a2e2a27a3",
           "eventType":"InvitationAcceptedEvent",
           "data":{  
              "userId": "673b3926-3667-4daf-9db7-03c08adc5ffd"
           }
        }
     ]
  }

Event: InvitationRejectedEvent

  {  
     "aggregateId":"723ecfce-14e9-4889-98d5-a3d0ad54912f",
     "events":[  
        {  
           "eventId":"127b80b5-4a05-4774-b870-1c9a2e2a27a3",
           "eventType":"InvitationRejectedEvent",
           "data":{  
              "userId": "673b3926-3667-4daf-9db7-03c08adc5ffd"
           }
        }
     ]
  }

Projection definition: meetings

{
  "aggregated": false,
  "projectionName": "meetings",
  "feedName": "meeting",
  "handlers": [
    {
      "eventType": "InvitationAcceptedEvent",
      "functions": [
        {
          "function": "push",
          "targetSelector": "$.projection.participants"
        }
      ]
    },
    {
      "eventType": "InvitationRejectedEvent",
      "functions": [
        {
          "function": "remove",
          "targetSelector": "$.projection.participants[?]",
          "targetFilter": "[?(@.userId == $.event.userId)]"
        }
      ]
    }
  ]
}

Projection result

Requesting the URL /projections/single/meetings/723ecfce-14e9-4889-98d5-a3d0ad54912f returns:

{
  "projectionId" : "723ecfce-14e9-4889-98d5-a3d0ad54912f",       
  "createdAt" : 1523518145532,                                   
  "updatedAt" : 1523518146253,                                   
  "data": {
    "participants": [{
      "userId": "673b3926-3667-4daf-9db7-03c08adc5ffd"
    }]
  }
}

How to mark a projected field as a searchable reference

Aggregate type: shipment

Event: RoutePlannedEvent

  {  
     "aggregateId":"723ecfce-14e9-4889-98d5-a3d0ad54912f",
     "events":[  
        {  
           "eventId":"127b80b5-4a05-4774-b870-1c9a2e2a27a3",
           "eventType":"RoutePlannedEvent",
           "data":{  
              "shipmentCode": "ABC123"
           }
        }
     ]
  }

Projection definition: shipments

{
  "aggregated": false,
  "projectionName": "shipments",
  "feedName": "shipment",
  "handlers": [
    {
      "eventType": "RoutePlannedEvent",
      "functions": [
        {
          "function": "merge"
        },
        {
          "function": "setref",
          "targetSelector": "$.projection.shipmentCode"
        }
      ]
    }
  ]
}

Projection result

Requesting the URL /projections/single/shipments?reference=ABC123 returns:

{                                                                  
  "projections" : [ {                                              
    "projectionId" : "723ecfce-14e9-4889-98d5-a3d0ad54912f",       
    "createdAt" : 1523518145532,                                   
    "updatedAt" : 1523518146253,                                   
    "data" : {                                                     
      "shipmentCode": "ABC123"           
    }
  } ],
  "totalCount" : 1
}