Adding and removing from lists

Example Events

Let's say we're developing support meetings, where participants can be invited and later accept or reject the invitation. In this scenario a reasonable Projection would be to view all participants that currently have accepted their invitation.
An example of a events for two participants that accepted their invitations could look like this:
1
{
2
"aggregateId":"723ecfce-14e9-4889-98d5-a3d0ad54912f",
3
"events":[
4
{
5
"eventId":"127b80b5-4a05-4774-b870-1c9a2e2a27a3",
6
"eventType":"InvitationAcceptedEvent",
7
"data":{
8
"userId": "673b3926-3667-4daf-9db7-03c08adc5ffd"
9
}
10
}
11
]
12
}
Copied!
1
{
2
"aggregateId":"723ecfce-14e9-4889-98d5-a3d0ad54912f",
3
"events":[
4
{
5
"eventId":"1a20dfac-ae44-49ac-9491-bcb6d418c4d0",
6
"eventType":"InvitationAcceptedEvent",
7
"data":{
8
"userId": "fa0d1d7c-9c64-4d65-b000-e1ace3a67bb2"
9
}
10
}
11
]
12
}
Copied!
Later, one of the participants rejects the invitation:
1
{
2
"aggregateId":"723ecfce-14e9-4889-98d5-a3d0ad54912f",
3
"events":[
4
{
5
"eventId":"127b80b5-4a05-4774-b870-1c9a2e2a27a3",
6
"eventType":"InvitationRejectedEvent",
7
"data":{
8
"userId": "673b3926-3667-4daf-9db7-03c08adc5ffd"
9
}
10
}
11
]
12
}
Copied!

Projection definition

1
{
2
"aggregated": false,
3
"projectionName": "meetings",
4
"feedName": "meeting",
5
"handlers": [
6
{
7
"eventType": "InvitationAcceptedEvent",
8
"functions": [
9
{
10
"function": "append",
11
"targetSelector": "$.projection.participants"
12
}
13
]
14
},
15
{
16
"eventType": "InvitationRejectedEvent",
17
"functions": [
18
{
19
"function": "remove",
20
"targetSelector": "$.projection.participants[?]",
21
"targetFilter": "[?(@.userId == $.event.userId)]"
22
}
23
]
24
}
25
]
26
}
Copied!

Results

/projections/single/meetings/723ecfce-14e9-4889-98d5-a3d0ad54912f
1
{
2
"projectionId" : "723ecfce-14e9-4889-98d5-a3d0ad54912f",
3
"createdAt" : 1523518145532,
4
"updatedAt" : 1523518146253,
5
"data": {
6
"participants": [{
7
"userId": "fa0d1d7c-9c64-4d65-b000-e1ace3a67bb2"
8
}]
9
}
10
}
Copied!
Last modified 1yr ago