Recording a streaming response from a streaming request to Coa using Mongoose

I am trying to send a large result from a Mongo database to a Koa application user (using Mongoose).

I initially had something like:

var res = yield Model.find().limit(500).exec(); this.body = {data: res}; 

However, the size of the sent result set sent caused the application to time out and, as such, I would like to pass 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 am not sure how to write this stream in response, keeping the required format. I want this to happen:

 this.body.write("{data: "}); this.body.write(stream); this.body.write("}"); 

but I know that Koa does not have body.write, and I'm sure I don't use streams either. Can someone point me in the right direction?

+6
source share
1 answer

koa-write can help.

but you may not need it. Coa allows you to:

 this.body = stream; 

In your case, you can create a transform stream, since the mongoose stream is not quite what you want to output.

+4
source

Source: https://habr.com/ru/post/982301/


All Articles