What I'm trying to do, is update this answer to not depend on the JSONStream library, which isn't maintained anymore. There we have:
Comment.find()
.cursor()
.pipe(JSONStream.stringify())
.pipe(res.type('json'))
It's using the Mongoose .cursor(), which returns a Node.js-compatible readable stream, but I'm open to using the native mongo driver.
Now, my first question: is anybody still using Node.js streams or are you nowadays supposed to use JavaScript Iterators and Generators?
If so, I think I'll be able to convert the cursor to an iterator and convert each chunk to JSON separately. (Although library recommendations that do error-handling etc. are welcome, even if off-topic here and not the core of this question).
But how do I make the iterator stream into the express.js result?
I couldn't find any documentation on that (although couldn't find documentation on res being a writable stream either, although it works.) Am I even on the right track with my thinking here?
Edit:
Meanwhile, I've done some more research and found the following libs:
- pull-stream – with no TypeScript typings but with the creator of JSONStream as a contributor
- ts-stream – with no built-in node stream conversion
- scramjet – looks intriguing... maybe a bit overkill, but at least comes with implementations of
mapandfilteretc. for their streams..?