Projection examples?

I want to create a custom projection, but can’t find any good examples or how-to’s to start from, and have no idea where to begin with it.

Basically, I want a variation of the built-in category projection. So, for each event look at some metadata and use that to create the stream id the event should be projected to. Feels like it should be fairly straightforward, but again, just can’t seem to find any examples of this.

Any ideas? Thanks!

How about something like this:

isStoreProductEvent = (e) => { return (e.data && e.data.storeProductId); }

function getStreamName(e) {
    //NOTE: Lack of a dash symbol in stream name to avoid event amplification
    return "StoreProductHistory" + e.data.storeProductId.replace(/-/gi, "");
}

fromAll()
    .when({
        $any: function (s, e) {
            if (isStoreProductEvent(e)) {
                var streamName = getStreamName(e);
                linkTo(streamName, e);
            }
        }
    });

Fabulous, that was exactly what I needed. FWIW this example is different from official documentation, in that linkTo() takes stream, event, and metadata in the docs, and in your example it is just stream name and the event. Maybe that’s a JavaScript thing with optional parameters(?), but it confused the heck outta me.

I am not 100% sure regarding the metadata, so probably takes this with a pinch of salt, but I think e being passed through preserves everything from the original event (since it’s only a linkTo rather than a new event (which you could do with emit()).