Skip to content

Instantly share code, notes, and snippets.

@chetbox
Created April 11, 2017 08:17
Show Gist options
  • Save chetbox/6817dca0fb8be631cc00b2ac876fa5d4 to your computer and use it in GitHub Desktop.
Save chetbox/6817dca0fb8be631cc00b2ac876fa5d4 to your computer and use it in GitHub Desktop.
Page through large collections of data in a Firebase Cloud Function
const _ = require('lodash');
function forEachPage(ref, pageSize, callback, startAt) {
var query = ref.orderByKey();
if (startAt) query = query.startAt(startAt);
return query.limitToFirst(pageSize + 1) // Ask for one extra
.once('value')
.then((snapshot) => {
if (snapshot.numChildren() <= pageSize) {
// There was no extra value returned, this is the last page
return Promise.resolve(callback(snapshot.val()));
} else {
// We remove the extra value we asked for
const lastKey = Object.keys(snapshot.val() || {}).reduce((a, b) => a>b ? a : b, undefined);
const valuesWithoutExtra = _.omitBy(snapshot.val(), (val, key) => key === lastKey);
return Promise.all([
forEachPage(ref, pageSize, callback, lastKey), // Fetch the next page,
Promise.resolve(callback(valuesWithoutExtra))
]);
}
});
}
module.exports = {
forEachPage: forEachPage
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment