Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

What is the desired way of replaying all events from store? #4

Closed
lanwin opened this Issue · 5 comments

2 participants

@lanwin

Please add an example to README.

@adrai
Owner

Hi @lanwin!

If you want to replay all events from the store you can do it with the function getEventRange:

for example:

var match = {} // match query in inner event (payload), for example: { id: eventId }
               // if {} all events will return
  , amount = 20; // amount of events to receive per request

var handle = function(err, events) {
  // events is the eventstream
  if (events.length === amount) {
    events.next(handle);
  } else {
    // finished to replay
  }
};

store.getEventRange(match, amount, handle);

If you want to replay all events of a particular aggregate or stream you can do it with the function getEvents:

for example:

var streamId = '1234'
  , revMin = null  // optional, must be a number
  , revMax = null; // optional, must be a number

store.getEvents(streamId, revMin, revMax, function(err, events) {
  // events is the eventstream
});
@adrai adrai closed this
@lanwin

Ok it seems I forgot something.

The first example goes in the direction of what I want. But what I really want is to start replaying all events from beginning to now. Save that cursor. And on a later then start from that cursor position and get all events to now.

@lanwin

And All means from all aggregates/streams.

@adrai
Owner

You can do it this way:

var match = {} // match query in inner event (payload), for example: { id: eventId }
               // if {} all events will return
  , amount = 20; // amount of events to receive per request


var lastEventId; // this is the "cursor"

var handle = function(err, events) {

  for(var i = 0, len = events.length; i < len; i++) {
    var eventStreamEvent = events[i];
    var yourEvent = eventStreamEvent.payload;
  }

  // events is the eventstream
  if (events.length === amount) {
    events.next(handle);
  } else {
    // finished to replay

    var myLastEvent = events[events.length - 1].payload;
    lastEventId = myLastEvent.id;
    // so next time call store.getEventRange with this match:
    match = { id: lastEventId };
  }
};

store.getEventRange(match, amount, handle);
@lanwin

@adrai thanks for the detailed example.

match = { id: lastEventId };

I expected that to work like a WHERE in SQL so it would return only this event and nothing more?!?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.