Apply a series of promise-returning transform functions on an object stream.
const transform = require('streaming-object-transform')([
obj => {
// sort object alphabetically
return require('deep-object-sort')(obj)
},
async => {
// asynchronously populate the user
if (obj.user_id) obj.user = await Users.getById(obj.user_id)
return obj
}
])
app.use((req, res, next) => {
res.setHeader('Content-Type', 'application/json')
db.query({})
.stream()
.on('error', next)
.pipe(transform())
.on('error', next)
.pipe(require('JSONStream').stringify())
.on('error', next)
.pipe(res)
})