I'm trying to send a large result-set from a Mongo database to the user of a Koa application (using Mongoose).
I originally had something like:
var res = yield Model.find().limit(500).exec();
this.body = {data: res};
However, the size of the result set being sent was causing the application to time out, and as such I'd like to stream the response as it comes from the database.
With Mongoose you can turn the result of a query into a stream by doing something like:
var stream = Model.find().limit(300).stream();
However, I'm not sure how to write this stream into the response while preserving the format needed. I want something like this to happen:
this.body.write("{data: "});
this.body.write(stream);
this.body.write("}");
but I know there is no body.write in Koa and I'm sure I'm not using streams properly either. Can someone point me in the right direction?
koa-write may help.
but you might not need that. Koa allows you to do:
this.body = stream;
In your case you can create a transform stream since the mongoose stream isn't exactly what you want to output.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With