3.0.0 • Published 10 years ago

find-in-batches v3.0.0

Weekly downloads
162
License
ISC
Repository
github
Last release
10 years ago

find-in-batches

A flow control helper method that invokes a forEach function for each element that was returned in the passed find method. This is a wrapper around async.js and uses simple pagination to do its magic.

API

findInBatches.each([options], findMethod, forEach, callback)
findInBatches.batch([options], findMethod, forBatch, callback)
findInBatches.all([options], findMethod, callback)

findInBatches.each

var request = require('request')
var findInBatches = require('find-in-batches')

var options = {
    offset: 0,            // a default offset
    batchSize: 50,        // this gets passed to the findMethod as limit
    maximum: Infinity,    // maximum count of documents that get loaded
    concurrency: Infinity // how many forEach methods should run simultaneously
}

findInBatches.each(options, findMethod, forEach, function(err){
    if (err) return console.error('Failed to load the entries', err)
    console.log('Loaded all the entries')
});

function findMethod (options, callback) {
    // options = {offset: 0, limit: 50, page: 1} // on first search
    // options = {offset: 50, limit: 50, page: 2} // on second iteration
    var url = 'http://maps.gelbeseiten.de'
    url += '/yp/subscriberlist_ajaxAction.yp?urlSegmentSubjectString=starbucks'
    url += '&recfrom=' + options.offset + '&reccount=' + options.limit
    request.get({url: url, json: true}, function(err, response, body){
        // the callback must be called with either an error
        // or an array as second argument
        if (err) return callback(err)
        if (!(body && body.data && body.data.subscribers)) return callback()
        callback(null, body.data.subscribers.subscribers)
    })
}

// forEach gets invoked for each element that gets returned in the findMethod.
// e.g. if the findMethod would return `callback(null, [{id: 1, name: 'one'}, {id: 2, name: 'two'}])`,
// the entry would equal `{id: 1, name: 'one'}`
function forEach (entry, callback){
    entry.address = entry.address || {}
    entry.address.street = entry.address.street || {}
    console.log(
        "Name: %s, Address: %s, %s",
        entry.name,
        entry.address.street.name + ' ' + entry.address.street.houseno,
        entry.address.location
    )

    callback()
}
3.0.0

10 years ago

2.1.1

10 years ago

2.1.0

10 years ago

2.0.1

10 years ago

2.0.0

10 years ago

1.0.3

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago