In an event sourced system, I have an aggregate root which is of type Order. Let's assume the following events is taking place:
- OrderPlaced (orderId, placedAt, customerId, orderLines) where OrderLine (lineId, productId, price)
- OrderAccepted (orderId)
And let's assume we need two different projections:
An projection holding the total price for all accepted orders grouped by year for each customer. Something like this:
OrdersByCustomer(customerId, summationOnAcceptedOrdersByYear) where SummationOnAcceptedOrdersByYear(year, sum)The issue here is that
OrderAccepteddoesn't containcustomerId. So when the projection receives anOrderAcceptedit has no way of getting the current projection state, as thecustomerIdis thedocumentId. Worth noting is that I'm storing the projections in Scylla - which is only queryable by the partition key - and the projection state is just a json representation. So not queryable by anything other then the documentId/projectionId. Maybe this is not an ideal choice of technology for projections...?
I'm thinking I have two options if going forward with Scylla:
Either pollute
OrderAcceptedwithcustomerId. But I don't feel this is a good approach - then I would need to incorporate that into all events which would be related to an projection where the projectionId / documentId is not the same as the aggregateId.Or have a separate table which contains a mapping between
orderIdandcustomerId, so the projection could query forcustomerId- this table would probably need to be updated in the command handler of the Order aggregate.Alternatively we could have a projection for
CustomerIdByOrderId- but here I think the projections can be in different positions of the event stream - this might be causing issues as well.
An projection which sums up the price of all accepted orders for each product. Something like this
SummationForProducts (productId, orderSummation)So here we have an projection which relies on both OrderPlaced and OrderAccepted. And the thing is that since an OrderPlaced may contain multiple orderLines, thus spanning multiple projections - we would need to update multiple projections when receiving OrderPlaced. Is this normal in Event Sourcing Projections - to update multiple projections pr event?
And the same issue arises here as to OrderAccepted don't include productIds from OrderPlaced event. So here we could probably do with an similar approach to have a table which contains a mapping between orderId and productIds
I wonder how more experienced event sourcerers solve these things...? :) Any input on this is highly appreciated.