Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a streaming response from a streaming query in Koa with Mongoose

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?

like image 899
Asksdumbquestions Avatar asked Feb 11 '15 02:02

Asksdumbquestions


1 Answers

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.

like image 192
Bryan Larsen Avatar answered Nov 09 '22 17:11

Bryan Larsen