x-cubed / event-store-client

JS client library for connecting to Event Store over TCP/IP
The Unlicense
71 stars 24 forks source link

Get most recent event in stream #23

Closed PaulGrimshaw closed 7 years ago

PaulGrimshaw commented 7 years ago

I may be missing something here, but I can't find a way to get the most recent event in a stream without doing two calls.

I would have thought readStreamEventsBackwards with a max number of 1 would do it, but it requires a "fromEventNumber" which prevents me from getting the last one as I don't know the last event number prior to the query. I'm not sure why "fromEventNumber" is a required field on this query... what am I missing?

x-cubed commented 7 years ago

This client is just exposing the API provided by Event Store, so I can't speak for the design decisions behind the API itself.

Usually, what you would want to do is subscribe to a stream, and you'll receive a callback each time a new event is posted. When you subscribe, you receive in the response the event ID of the most recent event in the stream, so you can use this to decide if you need to load any history from the stream since before the subscription was started.

PaulGrimshaw commented 7 years ago

This is possible using the native event store, by using ReadSingleEvent with StreamPosition.End. See here: http://docs.geteventstore.com/dotnet-api/3.0.0/reading-events/#reading-a-single-event-1

x-cubed commented 7 years ago

StreamPosition.End has the magic value -1, so you can try using that. I can look at defining that as a constant in a future release of the client.

PaulGrimshaw commented 7 years ago

-1 did the trick! Might be worth adding to the docs somewhere, or as a constant as you suggest, must be a failry common use case.

x-cubed commented 7 years ago

Ok, cool.

I'll add the StreamPosition constants into the client.