Skip to content

Instantly share code, notes, and snippets.

@harish2704
Created July 7, 2017 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harish2704/f253c7ded1d85846f3f2dac6336952f7 to your computer and use it in GitHub Desktop.
Save harish2704/f253c7ded1d85846f3f2dac6336952f7 to your computer and use it in GitHub Desktop.
Do any task using all the items in a Sequlize model. Just write the task alone. ItemMaper will take care about batch execution and limit
class ItemMaper{
constructor( { model, batchSize, limit }, sequelizeOpts ){
this.model = model;
this.batchSize = batchSize || 10;
this.limit = limit || Infinity;
this.sequelizeOpts = sequelizeOpts || {};
}
async forEachBatch( fn ){
let offset = -this.batchSize,
items;
do{
offset += this.batchSize;
items = await this.model.findAll( Object.assign({ offset, limit: this.batchSize }, this.sequelizeOpts ) );
await fn( items );
} while( ( items.length === this.batchSize ) && ( offset < this.limit-this.batchSize ) );
return offset + items.length;
}
forEachItem( fn, opts ){
return this.forEachBatch( items => Promise.map(items, fn, opts ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment