Add metadata from projection

Hi,

given following projection:

options({
    $includeLinks: false,
    reorderEvents: false,
    processingLag: 0
});

fromCategory('my_events')
    .partitionBy(({ data }) => data.Id)
    .when({
        $init: () => {
            return {
                data: {
                    Id: null,
                    Version: 0,
                    Key: null,
                    Title: null
                }
            };
        },
        ItemAdded: (state, { data }) => {
            state.data = {
                Version: state.data.Version + 1,
                ...data
            };
        },
        ItemEdited: (state, { data }) => {
            state.data = {
                Version: state.data.Version + 1,
                Key: state.data.Key,
                ...data
            };
        },
    })
    .transformBy(({ data }) => {
        return { Data: data };
    })
    .outputState()
    .outputTo('item_state', 'item_state-{0}');

How can I write specific metadata to the output stream ‘item_state-{0}’?

Hi,

Seems like outputTo doesn’t support metadata. However looking at your projection it seems it is possible to use emit allowing to pass the metadata. This would require you to call emit from the ItemAdded and ItemEdited handlers causing some repetition in code. However you could also consider using the $all handler and do something like:

options({
    $includeLinks: false,
    reorderEvents: false,
    processingLag: 0
});

fromCategory('my_events')
    .partitionBy(({ data }) => data.Id)
    .when({
        $init: () => {
            return {
                data: {
                    Id: null,
                    Version: 0,
                    Key: null,
                    Title: null
                }
            };
        },
        $all: function(state, event) {
          switch (event.eventType) {
            case "ItemAdded":
                state.data = {
                    Version: state.data.Version + 1
                }
                break;
            case "ItemEdited":
                state.data = {
                    Version: state.data.Version + 1,
                    Key: state.data.Key
                }
                break;
          }
          
          emit("item_state-" + state.data.Key, "item_state", state.data, state.metadata);
        }
    })

Couldn’t really test it, but you get the point :smiley:

HTH!

Cheers,

Peter

outputTo
EventStore/1Prelude.js at 77d18b6b6b660f68e09630f80ca9a7e9ee695b79 · EventStore/EventStore (github.com)

emit
EventStore/1Prelude.js at 77d18b6b6b660f68e09630f80ca9a7e9ee695b79 · EventStore/EventStore (github.com)