Projection Design Questions

Hi All,

I have a couple of questions about how projections are supposed to work. Let’s say I had an Order that had a bunch of OrderLines. Do I then have a projection for each Order that contains all of its OrderLines? (All OrderLines for Order with id of 1, would be accessed through /projections/OrderLines-1) Or do I have one giant projection and then filter it somehow?

Assuming that I have an OrderLineAdded event, how would I define the above projection?

Finally, the projection handler function has a signature of function(event, state) { … }. What exactly are event and state, and does a value need to be returned?



Hey Brian.

It depends on what kind of projection you are running. If you run a regular projection it will have one state (which is obviously not what you would want for orders :))

if you did a foreach(order) projection you would get one state per order stream. The state would end up in:


where name is the name of your projection and stream is the order stream for the projection. We are adding an Options() that can control these names as well.

You can also easily use the ES by hooking it to whatever you use for projections today. There is a special stream called $all that you can access that represents all the events in the event store.

per state and event that get passed to the function. The function is essentially being left folded over an event stream. The state is the return value of the previous execution of the function ex:

If I had InventoryItemCreated->InventoryItemRenamed->InventoryItemDeactivated and a projection

fromStream(mystream).whenAny(function(s,e) { if(state==null) return 0; return s + 1; })

it would get called as:

myfunction(myfunction(myfunction(null,IventoryItemCreated), InventoryItemRenamed),InventoryItemDeactivated)

passing the state from each execution to the next.

Does this help?