Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we paginate in Firebase?

While listing or getting data list from Google Firebase, how can we paging the data gathered? As an example,

countries = [ 
  {name: 'Afghanistan', code: 'AF'}, 
  {name: 'Åland Islands', code: 'AX'}, 
  {name: 'Albania', code: 'AL'}, 
... 
] 

I want to list as 10 per page and if i want to get page =0 with size 10 or page=5 with size=5

like image 794
Dr. X Avatar asked Oct 29 '22 01:10

Dr. X


1 Answers

As an example

{
  -KBZIPRqYmrRgNZ3GJt6: { asc: 1, desc: 9, name: "Rusty Kovacek"},
  -KBZIPRvieZbW-k9R9ra: { asc: 2, desc: 8, name: "Lloyd Feil" },
  -KBZIPRvieZbW-k9R9rc: { asc: 3, desc: 7, name: "Jasmin Hilll" },
  -KBZIPRwiXUgOtv3fCAL: { asc: 4,  desc: 6, name: "Ms. Ibrahim Schinner" },
  -KBZIPRwiXUgOtv3fCAN: { asc: 5, desc: 5,  name: "Dorothea Koepp" },
  -KBZIPRxpCAUyo5TJmY3: { asc: 6, desc: 4, name: "Melvin Marquardt" }, 
  -KBZIPRxpCAUyo5TJmY5: { asc: 7,  desc: 3, name: "Celestine Bode"  },
  -KBZIPRy5Uvz9wUOa6Jx: { asc: 8,  desc: 2, name: "Emerald Olson"  }, 
  -KBZIPRy5Uvz9wUOa6Jz: { asc: 9, desc: 1, name: "Miss Joey Jacobi" }, 
  -KBZIPRzRhuguDLLftQR: { asc: 10, desc: 0, name: "Ms. Denis Rutherford" }
}

and

var axios = require('axios');
var Firebase = require('firebase');
var namesRef = new Firebase('https://demos-firebase.firebaseio.com/dataDemo/names');

axios.get(namesRef.toString() + '.json?shallow=true')
  .then(function (res) {
    // This list is not sorted!!!
    // res.data = {
    //   '-KBZIPRqYmrRgNZ3GJt6': true,
    //   '-KBZIPRwiXUgOtv3fCAN': true,
    //   '-KBZIPRy5Uvz9wUOa6Jx': true,
    //   '-KBZIPRzRhuguDLLftQR': true,
    //   '-KBZIPRxpCAUyo5TJmY5': true,
    //   '-KBZIPRxpCAUyo5TJmY3': true,
    //   '-KBZIPRwiXUgOtv3fCAL': true,
    //   '-KBZIPRvieZbW-k9R9ra': true,
    //   '-KBZIPRvieZbW-k9R9rc': true,
    //   '-KBZIPRy5Uvz9wUOa6Jz': true
    // }
    var keys = Object.keys(res.data).sort(); // Notice the .sort()!
    var pageLength = 2;
    var pageCount = keys.length / pageLength;
    var currentPage = 1;
    var promises = [];
    var nextKey;
    var query;

    for (var i = 0; i < pageCount; i++) {
      key = keys[i * pageLength];
      console.log('key', key);
      query = namesRef.orderByKey().limitToFirst(pageLength).startAt(key);
      promises.push(query.once('value'));
    }

    Promise.all(promises)
      .then(function (snaps) {
        var pages = [];
        snaps.forEach(function (snap) {
          pages.push(snap.val());
        });
        console.log('pages', pages);
        process.exit();
        // pages = [{
        //   '-KBZIPRqYmrRgNZ3GJt6': {
        //     asc: 1,
        //     desc: 9,
        //     name: 'Rusty Kovacek'
        //   },
        //   '-KBZIPRvieZbW-k9R9ra': {
        //     asc: 2,
        //     desc: 8,
        //     name: 'Lloyd Feil'
        //   }
        // }, {
        //   '-KBZIPRvieZbW-k9R9rc': {
        //     asc: 3,
        //     desc: 7,
        //     name: 'Jasmin Hilll'
        //   },
        //   '-KBZIPRwiXUgOtv3fCAL': {
        //     asc: 4,
        //     desc: 6,
        //     name: 'Ms. Ibrahim Schinner'
        //   }
        // }, {
        //   '-KBZIPRwiXUgOtv3fCAN': {
        //     asc: 5,
        //     desc: 5,
        //     name: 'Dorothea Koepp'
        //   },
        //   '-KBZIPRxpCAUyo5TJmY3': {
        //     asc: 6,
        //     desc: 4,
        //     name: 'Melvin Marquardt'
        //   }
        // }, {
        //   '-KBZIPRxpCAUyo5TJmY5': {
        //     asc: 7,
        //     desc: 3,
        //     name: 'Celestine Bode'
        //   },
        //   '-KBZIPRy5Uvz9wUOa6Jx': {
        //     asc: 8,
        //     desc: 2,
        //     name: 'Emerald Olson'
        //   }
        // }, {
        //   '-KBZIPRy5Uvz9wUOa6Jz': {
        //     asc: 9,
        //     desc: 1,
        //     name: 'Miss Joey Jacobi'
        //   },
        //   '-KBZIPRzRhuguDLLftQR': {
        //     asc: 10,
        //     desc: 0,
        //     name: 'Ms. Denis Rutherford'
        //   }
        // }]
        
      });
  });
like image 50
Dr. X Avatar answered Nov 09 '22 09:11

Dr. X