With bookshelf.js, how do you traverse all models in batches?

With bookshelf.js it is easy enough to get all the records for a given model using Model.fetchAll and then loop through them like this:

SomeModel.fetchAll().then(function(results) {
    results.models.forEach(function(model) {
        ...
    });
});

      

But this loads the entire result set at once, which is impractical for very large result sets. Is there an easy way to load the results into smaller batches (e.g. only 1000 at a time, say).

I know it can be done by maintaining an offset counter and using limit () and offset () to flip my own version of this, but I'm actually looking for something that hides nuts and bolts similar to ActiveRecord find_in_batches .

But I cannot find anywhere in the docs or google search if the batch receiver method exists. Is there an easy way to do this?

+3


source to share





All Articles