Projecting events
To present your event data in multiple ways you can define projections that process your events and generate data structures that can be fetched, listed and filtered in the Serialized API.

Creating your first projection definition

The first part of projecting data is creating a projection definition. A definition defines what events should be processed and how they should be processed into a data structure that is more suitable for the client to query.
It is a good idea to create multiple projection definitions for different use cases. Projections are cheap views and making them tailored for the client makes your data easy to use.
If you performed the first steps explained in Working with aggregates 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
1
curl -i https://api.serialized.io/projections/definitions \
2
--header "Content-Type: application/json" \
3
--header "Serialized-Access-Key: <YOUR_ACCESS_KEY>" \
4
--header "Serialized-Secret-Access-Key: <YOUR_SECRET_ACCESS_KEY>" \
5
--data '
6
{
7
"projectionName": "orders",
8
"feedName": "order",
9
"handlers": [
10
{
11
"eventType": "OrderPlacedEvent",
12
"functions": [
13
{
14
"function": "set",
15
"targetSelector": "$.projection.status",
16
"rawData": "PLACED"
17
},
18
{
19
"function": "set",
20
"targetSelector": "$.projection.orderAmount",
21
"eventSelector": "$.event.orderAmount"
22
}
23
]
24
},
25
{
26
"eventType": "OrderPaidEvent",
27
"functions": [
28
{
29
"function": "set",
30
"targetSelector": "$.projection.status",
31
"rawData": "PAID"
32
}
33
]
34
},
35
{
36
"eventType": "OrderShippedEvent",
37
"functions": [
38
{
39
"function": "set",
40
"targetSelector": "$.projection.status",
41
"rawData": "SHIPPED"
42
}
43
]
44
},
45
{
46
"eventType": "OrderCancelledEvent",
47
"functions": [
48
{
49
"function": "set",
50
"targetSelector": "$.projection.status",
51
"rawData": "CANCELLED"
52
}
53
]
54
}
55
]
56
}
57
'
Copied!

Query our projection

Great! We can now access the projection and check the current state of our order!
cURL
1
curl -i \
2
--header "Serialized-Access-Key: <YOUR_ACCESS_KEY>" \
3
--header "Serialized-Secret-Access-Key: <YOUR_SECRET_ACCESS_KEY>" \
4
https://api.serialized.io/projections/single/orders/723ecfce-14e9-4889-98d5-a3d0ad54912f
Copied!
Response
1
HTTP/1.1 200 OK
2
Content-Type: application/json
3
ETag: "c604ac5bd1a51a798accc5717bd5109f"
4
Last-Modified: Mon, 18 Sep 2017 17:01:23 GMT
5
Vary: Accept-Encoding
6
Content-Length: 125
7
8
{
9
"projectionId":"723ecfce-14e9-4889-98d5-a3d0ad54912f",
10
"updatedAt":1505754083976,
11
"data": {
12
"orderAmount":"12345",
13
"status":"PAID"
14
}
15
}
Copied!