Last active
August 26, 2018 07:32
-
-
Save deltaepsilon/52ca73fe22b2b7a971ddaf9b8405a7f6 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
| var axios = require('axios'); | |
| var firebase = require('firebase').initializeApp({ | |
| "serviceAccount": "./service-account.json", | |
| "databaseURL": "https://quiver-two.firebaseio.com/" | |
| }); | |
| var ref = firebase.app().database().ref(); | |
| var peopleRef = ref.child('swapi/people'); | |
| // Calling ref.toString() outputs the ref's entire path: https://...firebaseio.com/some/ref/path | |
| var peopleUrl = peopleRef.toString() + '.json?shallow=true'; | |
| var pageLength = 2; | |
| function getPages(accumulator, cursor) { | |
| var pages = accumulator || []; | |
| var query = peopleRef.orderByKey().limitToFirst(pageLength + 1); // limitToFirst starts from the top of the sorted list | |
| if (cursor) { // If no cursor, start at beginning of collection... otherwise, start at the cursor | |
| query = query.startAt(cursor); // Don't forget to overwrite the query variable! | |
| } | |
| return query.once('value') | |
| .then(function (snaps) { | |
| var page = []; | |
| var extraRecord; | |
| snaps.forEach(function (childSnap) { | |
| page.push({ | |
| id: childSnap.ref.getKey(), | |
| name: childSnap.val().name | |
| }); | |
| }); | |
| if (page.length > pageLength) { | |
| extraRecord = page.pop(); | |
| pages.push(page); | |
| console.log(pages, extraRecord.id); | |
| return getPages(pages, extraRecord.id); | |
| } else { | |
| pages.push(page); | |
| return Promise.resolve(pages); | |
| } | |
| }); | |
| }; | |
| getPages() | |
| .then(function (pages) { | |
| console.log('pages', JSON.stringify(pages)); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment