Writing a streaming response from a streaming query in Koa with Mongoose
Asked Answered
C

1

8

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?

Clein answered 11/2, 2015 at 2:6 Comment(0)
M
6

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.

Micrography answered 11/6, 2015 at 14:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.