I am trying generatea Observablefrom a sequence of (packed) records from an SQL database, I am trying to run all the records in a database. I use ORM on node-js, Sequelizewhich returns records wrapped in a promise.
I defined a function fetchbatch()that fetches the next batch and returns Promise[Array[Record]]and flatMap'result in Observable.
My condition (for completion) is set as global in the promise block, based on the fact that the request did not return any records, but the callback is never called, only promises returns endlessly, and therefore the completion condition is never satisfied. Any suggestions on how this is handled? Here is the gist of the code.
function getAllPaginated(conditions) {
var remaining = true;
var batch_size = 20;
function condition(){ return remaining; }
function selector(promisedBatchOfRecords){
return (promisedBatchOfRecords[1]);
}
function fetchBatch(batchNumberAndBatch) {
var batch_number = batchNumberAndBatch[0];
var offset = (batch_number - 1) * batch_size;
var rs = Records.findAll({where: conditions, offset: offset, limit: batch_size});
return [batch_number + 1,
rs.then(function(batch) {
console.log(batch.length);
if (!(batch.length > 0)){
remaining = false;
};
return batch.map(function(r){r.dataValues});
})];
}
return Observable.generate(fetchBatch([1, []]), condition, fetchBatch).flatMap(Ramda.identity).flatMap(Ramda.identity);
}
var o = getAllPaginated({where: {a: "b"}})
o.subScribeOnNext(console.log)