Search code examples
wolkenkit

How to make transaction when saving events to events store


I would like to use transaction in wolkenkit-eventstore when saving events to eventstore and be able to rollback those events if something else fail, is it possible ? I saw in source code (in saveEvents method) that you are releasing connection pool:

try {
      const result = await connection.query({ name: `save events ${committedEvents.length}`, text, values });

      for (let i = 0; i < result.rows.length; i++) {
        committedEvents[i].event.metadata.position = Number(result.rows[i].position);
      }
    } catch (ex) {
      if (ex.code === '23505' && ex.detail.startsWith('Key ("aggregateId", revision)')) {
        throw new Error('Aggregate id and revision already exist.');
      }

      throw ex;
    } finally {
      connection.release();
    }

at the finally step, so i can't gain this connection pool in any way. Is there any way i can do transaction based system with wolkenkit-eventstore ?


Solution

  • I'm one of the core developers of wolkenkit, so first of all thanks for bringing up this question 😊

    Right now what you want is actually not possible, but nevertheless it could be a good idea to support this use case.

    In wolkenkit the procedure is that the command handler publishes the events, and only if the command handler succeeds, the events are stored in the event store in an all-or-nothing approach.

    To be able to understand your use case better – you said, you would like:

    to rollback those events if something else fail[s]

    What would this "something else" be?

    Since this could be the start for a longer discussion, I think StackOverflow is probably not the perfect place to do this, so if you would like to talk to us about this feature, could you please open a feature request for this?