How can I create a projection that dynamically sorts events into streams?

We are used event types / names that basically follow this convention:

.<aggregate/entity>.

For example:

Accounting.Account.accountCreated

We store one stream for each aggregate. Because this is new for us we’re also not sure if this is best practice.

I would also like to get a stream per aggregate or domain and trying to create it by doing this for a projection:

var streamName = ‘default’;

fromAll()
.when({
$any: function(state, event){
log(event);
var positon = event.Type.indexOf(’.’)
var streamName = event.Type.substring(0, position);
}
})
.outputTo(streamName)

``

Resulting in: The test projection failed to process an event. Handler: EventStore.Projections.Core.Services.v8.DefaultV8ProjectionStateHandler Event Position: C:329547/P:329547 Message: TypeError: Cannot read property ‘indexOf’ of undefined

I can’t find any documentation about the event object passed there, so I have no clue about it’s properties and methods and I can’t use log or console.log()… So how the heck do I know what I can do with this objcet? :frowning: The documentation seems to lack any information about this.

Hi,
First take a look at the built-in category and event type projections they may be doing what you need already.

Second, I’m not entirely clear on what your approach to stream names is, are you using a stream per aggregate instance or or a stream for all of the instances of an aggregate?

I would recommend using a stream per instance with the format of [aggregate]-[id] (pre-pending the domain is fine just don’t use a hyphen as the separator as that is what the category projection is based on)

Third if I understand your question re objects: The event object in the projection is generated from the json stored in the Event Store, so the fields are based on what you have stored.

So, I think you can get what you need without creating a custom projection.

-Chris

Hi Chris, thanks for your reply.

OK, I’ve switched to the prefix for the stream name, but now I have the problem that I’m getting only events like this from the $category-Account stream:

{ "$v": "2:-1:1:3", "$c": 303378, "$p": 303378, "$causedBy": "296c90a3-3ddd-e248-8c97-b2c8fa515edf" }

I'm trying to use this lib in the case this matters.

[https://github.com/prooph/event-store-http-client](https://github.com/prooph/event-store-http-client)