Search code examples
typescripteventsevent-handlingprojectioneventstoredb

Eventstore projections not storing in assigned stream


I am running a projection on my eventstoreDB trying to create my project with Event sourcing. This is my projection, running with no errors: (the project is made with node and ts)

 options({
      resultStreamName: "userData",
      $includeLinks: true,
      reorderEvents: false,
      processingLag: 500
    })
    
    fromStream('user')
    .when({
      $init: function() {
        return {
          cash: 0
        }
      },
      trans: function(state, event) {
        state.cash += event.body.cash;
      }
    })
    .transformBy(function(state) {
      state.cash = 10;
    })
    .outputState()

for some reason, it seems like the result won't be uploaded to the resultstreamName as specified.

I see that the projection ( called user), is run:

enter image description here

my projection "test" with cash info:

enter image description here

How can I make the result projection to be stored in the stream userData?


Solution

  • I'm no expert, so if this does not work please comment and I'll delete this answer.

    From information I could find, you do not need the transformBy call here. As you're not transforming the state into an output model, you should be able to perform the calculation directly in your trans event handler. You also need to return the state, after adding the cash amount, as follows:

    options({
        resultStreamName: "userData",
        $includeLinks: true,
        reorderEvents: false,
        processingLag: 500
    })
    
    fromStream('user')
        .when({
            $init: function() {
                return {
                    cash: 0
                }
            },
            trans: function(state, event) {
                state.cash += event.body.cash; // (maybe event.data.cash instead?)
                return state;
            }
        })
        .outputState()