Created
February 11, 2015 17:31
-
-
Save DinoChiesa/aee834a4faafac304cc0 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
* ugCollectionForEachPaging | |
* | |
* Iterates through all items in a collection within | |
* Apigee Edge BaaS. Uses the Usergrid client object from the usergrid | |
* module. Notice - this logic calls {has,get}NextPage(). | |
* | |
* @param ugClient - the authenticated client object | |
* @param options - the options for a collection. Pass type and qs. | |
* @param f - function called with each UG entity. Accepts a single argument. | |
* @param doneCb - called in case of error or success. | |
* | |
*********************************************/ | |
function ugCollectionForEachPaging (ugClient, options, f, doneCb) { | |
var results = {count: 0, failCount: 0}; | |
if ( ! options.qs) { | |
doneCb(new Error('missing qs property in the options argument'), null); | |
} | |
if ( ! options.type) { | |
doneCb(new Error('missing type property in the options argument'), null); | |
} | |
ugClient.createCollection(options, function (e, collection) { | |
var e2; | |
function doOnePage(collection, cb) { | |
while(collection.hasNextEntity()) { | |
f(collection.getNextEntity(), results); | |
results.count++; | |
} | |
if (collection.hasNextPage()) { | |
collection.getNextPage(function(e){ | |
if (e) { | |
e2 = new Error('could not get next page of entities'); | |
e2.wrappedError = e; | |
cb(e2, results); | |
} | |
else { | |
doOnePage(collection, cb); | |
} | |
}); | |
} | |
else { | |
cb(null, results); | |
} | |
} | |
if (e) { | |
e2 = new Error('could not make or get collection'); | |
e2.wrappedError = e; | |
doneCb(e2, null); | |
} | |
else { | |
doOnePage(collection, doneCb); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment