Created
July 7, 2017 21:09
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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