I'm trying to create REST service that returns data from database as a response via hapi.js.
I'm using tedious to execute SQL queries and I want to format results as JSON or XML and return them as a response of hapi request. Note that this is not tedious-specific, I can have the same problem with any database. Tedious has a callback that is invoked each time it receives a new row from the result set:
sqlRequest.on('row', function (columns) {
fnOutput(columns[0].value);
});
This is the simplest example with one column output. fnOutput is a custom function that accepts text that should be returned. It can concatenate results and format them as JSON that will be returned to client.
I'm getting row-by-row and I want to concatenate values in rows and return them to the client via hapi. How to continuously send partial results to the response?
- I don't want to concatenate all rows in this callback and then push entire formatted JSON when the query is finished. I don't know how many records I will have and I want to avoid big strings that will be accumulated.
- If I try to call reply(columns[0].value) in fnOutput each time I get new row, it fails when second row is returned with: "reply interface called twice"
Is there some way to continuously append results in this callback to hapi.js output stream each time I get new row in callback function?