This seems to work
const couchbase = require('couchbase');
const { Readable, PassThrough } = require("stream");
async function main() {
const cluster = await couchbase.connect('couchbase://localhost', {
username: 'Administrator',
password: 'password',
});
const readableStream = new Readable({
read() {},
});
const query = "SELECT RAW ENCODE_JSON(a) FROM `travel-sample` a limit 2";
const queryStream = cluster.query(query);
queryStream.on('row', (row) => {
console.log('row: ',row, typeof(row)); // to show that the row is a string
readableStream.push(row + '\r\n'); // adding \r\n shouldn't be necessary
});
queryStream.on('end', () => {
readableStream.push(null);
});
//setStreamHeaders(res);
//readableStream.pipe(res.raw);
readableStream.pipe(process.stdout)
}
main().catch(e => console.log(e)); // this catch doesn't seem to work as I hoped.
row: {"callsign":"MILE-AIR","country":"United States","iata":"Q5","icao":"MLA","id":10,"name":"40-Mile Air","type":"airline"} string
{"callsign":"MILE-AIR","country":"United States","iata":"Q5","icao":"MLA","id":10,"name":"40-Mile Air","type":"airline"}
row: {"callsign":"TXW","country":"United States","iata":"TQ","icao":"TXW","id":10123,"name":"Texas Wings","type":"airline"} string
{"callsign":"TXW","country":"United States","iata":"TQ","icao":"TXW","id":10123,"name":"Texas Wings","type":"airline"}
Later I found there is somewhat of an answer here that also points to ENCODE_JSON. How convert json object to string?