meticoeus meticoeus - 10 months ago 112
reST (reStructuredText) Question

Paginating Firebase data through the REST API on property without duplicates

Is there an equivalent of

startAt(value, [key])
in the REST API? Specifically the optional key param.


I am paging through a collection on a property (dueDate) using the REST API. Paging through works but, since the values for dueDate are not unique, each successive page may contain several duplicates. In the worst case, there may be more duplicates on a date than the page size, which would break the pagination.

Because of the way Firebase paging through works I'm expecting 1 duplicate, the last item from the previous page.

The JS SDK startAt method supports a second argument to provide the last key when making the next query but this does not appear to exist, or is not documented in the REST API.

Unhelpfully, the REST API docs do not provide a full specification for the query parameters, only the REST-specific params like "auth", and "shallow".


See the section in the guide on ordered data for more information.

limitToFirst, limitToLast, startAt, endAt, equalTo

See the section in the guide on querying data for more information.


The request in NodeJS:

var request = require('request-promise');
var startAt = 0
var now = new Date().getTime(); // calculate once per paginated query
getPage(startAt, now)
// then process page
// then get the next page
// etc

function getPage(startAt, endAt, lastId) {
var params = {
auth: /* ... auth token ... */,
orderBy: '"dueDate"',
startAt: startDate, /* startDate is a Unix timestamp in milliseconds */
endAt: endAt,
limitToFirst: 1000
return request.get({
url: '',
qs: params,
json: true,
timeout: 20000 /* ms */

I have looked at Firebase REST API: How to fetch data by priority like startAt/endAt in JS?. The newer answer refers to the old docs, which as stated above, do not answer this question unless I have missed something. I have read everything in the REST sections in both the old docs and the new docs pages.

Answer Source

After corresponding with Firebase support, they have confirmed that the REST API does not support the startAt with key parameter at this time.

Since our data in this case is a user selected date (so there may be duplicates) we've come up with a workaround to use the date to calculate a firebase push ID at that date-time which we can add as an extra indexed property and paginate over to ensure we do not get more than 1 duplicate per page.

The gist below contains the push ID algorithm with ports to many other languages if anyone else comes across a need to do this.