Thai Tran
Thai Tran

Reputation: 9935

Looping with Sequelize

I am new to the async programming and currently stuck with this simple task. I have a Mysql table which has 20k records and need to process all the records. The current approach is looping in batch of records.

let limit = 100;
let offset = 0;

sequelize.query(`Select * from abc limit ${limit} offset ${offset}`, {type: sequelize.QueryTypes.SELECT}).then((records) => {
  // process

  return processResult; 
});

Therefore, i need to increase the offset in the while loop.

while (/*....*/) {
  let p = sequelize.query(`Select * from abc limit ${limit} offset ${offset}`, {type: sequelize.QueryTypes.SELECT}).then((records) => {
    // process records

    return processResult; 
  });

  offset += limit; 
}

My question is what is the condition in the while loop in order to stop it ? Since p is always a Promise, thus I cannot use it as the stop condition

Upvotes: 1

Views: 2070

Answers (1)

Adam
Adam

Reputation: 5233

You can use the .each() of bluebird Promise.

var Promise = require('bluebird');

let limit = 100;
let offset = 0;
let queryStrings = [];
let max = 20000;

while (offset < max) {
    queryStrings.push(`Select * from abc limit ${limit} offset ${offset}`);
    offset += limit;
}

let p = Promise.each(queryStrings, (queryString)=> {
    return sequelize.query(queryString, {type: sequelize.QueryTypes.SELECT}).then((records) => {
        // process records

        if(/* need to break */){
            return Promise.reject(new Error('...'));
        }
        return processResult;
    });
});

update

If you don't know the maximum, you can do some recursion:

let _iterate = function (limit, offset) {
    return sequelize.query(`Select * from abc limit ${limit} offset ${offset}`, {type: sequelize.QueryTypes.SELECT}).then((records) => {
        // process records

        if (/* need to break */) {
            return Promise.reject(new Error(''));
        }

        offset += limit;

        return _iterate(limit, offset);
    });
};

let p = _iterate(100, 0);

Upvotes: 1

Related Questions