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

Closed
lanwin opened this Issue Sep 28, 2012 · 5 comments

Comments

Projects
None yet
2 participants

lanwin commented Sep 28, 2012

Please add an example to README.

Owner

adrai commented Oct 1, 2012

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 closed this Oct 1, 2012

lanwin commented Oct 9, 2012

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 commented Oct 9, 2012

And All means from all aggregates/streams.

Owner

adrai commented Oct 9, 2012

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 commented Oct 9, 2012

@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