I am studying through2 and sequelize.
My codes:
return Doc.createReadStream({
where: { /*...*/ },
include: [
{
/*...*/
},
],
})
.pipe(through({ objectMode: true }, (doc, enc, cb) => {
Comment.findOne(null, { where: { onId: doc.id } }).then((com) => { /* sequelize: findOne*/
com.destroy(); /* sequelize instance destroy: http://docs.sequelizejs.com/manual/tutorial/instances.html#destroying-deleting-persistent-instances */
cb();
});
}))
.on('finish', () => {
console.log('FINISHED');
})
.on('error', err => console.log('ERR', err));
I am trying to express my question clearly. Doc
and Comment
are sequelize Models. I want to use stream to read Doc instances from database one by one and delete comments on each Doc instance. Comment.findOne
and com.destroy()
will both return promises. I want to the promises resolved for each doc
and then call cb()
. But my above codes cannot work, before com
be destroyed, the codes already finish running.
How to fix it? Thanks
I wrap the above piece of codes in mocha
test, like
it('should be found by readstream', function _testStream(){
/* wrap the first piece of codes here*/
});
But before stream finished reading, the test exist.