-
Notifications
You must be signed in to change notification settings - Fork 24
Description
After changing readStream
and readAll
methods to return the NodeJS stream instead of an array, it's impossible to get an array of events without using the handcrafted helper method or external library (e.g. https://www.npmjs.com/package/iter-tools).
Currently you either have to iterate through it, e.g.:
const resultEvents = client.readStream(streamName, {
fromRevision: START,
direction: FORWARDS,
maxCount: 10,
});
const events = [];
for await (const event of resultEvents) {
events.push(event);
}
return events;
It's impossible to get the array immediately or use methods like map
or other transformations without using, e.g. NodeJS stream transformation.
I think that we should add the helper method toArray
or collect
or other name that will wrap the
const events = [];
for await (const event of resultEvents) {
events.push(event);
}
return events;
(or do it the smarter way).
The other option is to have the take
method with the number of events we'd like to take, but I don't see that being useful. From my experience, 99% of the time, you just want to read the whole stream to rebuild the state. You won't know what's the number of events you have in the stream. Plus, we already removed this need by making maxCount
optional (as in other clients). If we add the limitation, people would still need to write their own helpers to page the event streams.
I understand that we should have the memory effective option to fulfil the needs of people that have longer streams. However, I also believe that we should also enable people who have proper short streams to do it easily. Of course, other packages do it more efficiently. We can mention them in the docs or use them internally. I think that we should not optimise our API for edge cases. The entry point to using EventStoreDB is already high enough. We should lower it.
This feature request is not a breaking change. People can use the NodeJS streams, as they're now in v2. It's just to make the usage more accessible.