通过属性上的REST API对Firebase数据进行分页,无需重复 [英] Paginating Firebase data through the REST API on property without duplicates

查看:156
本文介绍了通过属性上的REST API对Firebase数据进行分页,无需重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在REST API中是否有相当于 startAt(value,[key])的值?特别是可选的关键参数。



https://www.firebase.com/docs/web/api/query/startat.html



详情:

>

我正在使用REST API对属性(dueDate)进行分页。通过工作分页,但由于dueDate的值不是唯一的,每个连续的页面可能包含多个重复项。在最糟糕的情况下,日期可能会比页面大小重复得多,这将会破坏分页。



由于Firebase通过作品进行分页的方式,我

JS SDK startAt方法支持第二个参数,在进行下一个查询时提供最后一个键,但是这不是似乎存在,或没有在REST API中记录。



不幸的是,REST API文档没有提供查询参数的完整规范,只有REST特定的像auth和浅的参数。


orderBy



有关详细信息,请参阅指南中关于订购数据的部分。



limitToFirst,limitToLast,startAt,endAt,equalTo

请参阅指南中有关查询数据的部分以获取更多信息。


文档: https://firebase.google.com/docs/reference/rest/database/#section-query-parameters <在NodeJS中的请求:

  var request = require('request-诺言'); 
var startAt = 0
var now = new Date()。getTime(); //计算每个分页查询一次
getPage(startAt,now)
//然后处理页面
//然后获取下一页
// etc

函数getPage(startAt,endAt,lastId){
var params = {
auth:/ * ... auth token ... * /,
orderBy:'dueDate' ,
startAt:startDate,/ * startDate是以毫秒为单位的Unix时间戳* /
endAt:endAt,
limitToFirst:1000
};
return request.get({
url:'https://my-firebase.firebaseio.com/todos.json',
qs:params,
json:true,
timeout:20000 / * ms * /
});
}

我已经看过 Firebase REST API:如何通过优先级获取数据,如JS中的startAt / endAt ?。较新的答案是指旧文件,如上所述,除非我错过了某些东西,否则不会回答这个问题。在旧文档和新文档页面中,我已经阅读了REST部分中的所有内容。解析方案

在与Firebase支持相对应后,他们已经证实REST API目前不支持带有key参数的startAt。

由于我们的数据在这种情况下是用户选择的日期(所以可能有重复),我们想出了一个解决方法,使用日期来计算在该日期时间的Firebase推送ID,我们可以添加一个额外的索引属性和分页,以确保每页不会超过1个重复。



下面的要点包含推送ID算法,其中包含许多其他语言的端口,如果其他人遇到需要这样做的话。

https://gist.github.com/mikelehen/3596a30bd69384624c11 p>

Is there an equivalent of startAt(value, [key]) in the REST API? Specifically the optional key param.

https://www.firebase.com/docs/web/api/query/startat.html

Details:

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".

orderBy

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.

Docs: https://firebase.google.com/docs/reference/rest/database/#section-query-parameters

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: 'https://my-firebase.firebaseio.com/todos.json',
    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.

解决方案

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.

https://gist.github.com/mikelehen/3596a30bd69384624c11

这篇关于通过属性上的REST API对Firebase数据进行分页,无需重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆