具有分页功能的Firebase过滤器 [英] Firebase filter with pagination

查看:57
本文介绍了具有分页功能的Firebase过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase + Angular做一个开源Yelp.

I'm doing an opensource Yelp with Firebase + Angular.

我的数据库:

    {
      "reviews" : {
        "-L0f3Bdjk9aVFtVZYteC" : {
          "comment" : "my comment",
          "ownerID" : "Kug2pR1z3LMcZbusqfyNPCqlyHI2",
          "ownerName" : "MyName",
          "rating" : 2,
          "storeID" : "-L0e8Ua03XFG9k0zPmz-"
        },
        "-L0f7eUGqenqAPC1liYj" : {
          "comment" : "me second comment",
          "ownerID" : "Kug2pR1z3LMcZbusqfyNPCqlyHI2",
          "ownerName" : "MyName",
          "rating" : 3,
          "storeID" : "-L0e8Ua03XFG9k0zPmz-"
        },    
      },
      "stores" : {
        "-L0e8Ua03XFG9k0zPmz-" : {      
          "description" : "My good Store",      
          "name" : "GoodStore",
          "ownerID" : "39UApyo0HIXmKPrTOi8D0nWLi6n2",      
          "tags" : [ "good", "health", "cheap" ],
        }
      },
      "users" : {
        "39UApyo0HIXmKPrTOi8D0nWLi6n2" : {
          "name" : "First User"
        },
        "Kug2pR1z3LMcZbusqfyNPCqlyHI2" : {
          "name" : "MyName",
          "reviews" : {
            "-L0f3Bdjk9aVFtVZYteC" : true,
            "-L0f7eUGqenqAPC1liYj" : true
          }
        }
      }
    }

我在下面使用此代码获取所有商店的评论(使用AngularFire2)

I use this code below to get all store's reviews (using AngularFire2)

    getReviews(storeID: string){
        return this.db.list('/reviews', ref => { 
            return ref.orderByChild('storeID').equalTo(storeID);
        });
    }

现在,我想进行服务器端审查分页,但是我认为我无法使用此数据库结构来做到这一点.我对吗?尝试过:

Now, I want to make a server-side review pagination, but I think I cannot do it with this database structure. Am I right? Tried:

    getReviews(storeID: string){
        return this.db.list('/reviews', ref => { 
            return ref.orderByChild('storeID').equalTo(storeID).limitToLast(10) //How to make pagination without retrive all data?
        });
    }

我以为我可以将所有评论放入商店中,但是(i)我不想在有人要商店时立即检索所有评论,并且(ii)我的评论具有用户名,所以我想更改它很容易(这就是为什么我要使用非规范化表的原因)

I thought that I could put all reviews inside stores, but (i) I don't want to retrieve all reviews at once when someone ask for a store and (ii) my review has a username, so I want to make it easy to change it (that why I have a denormalized table)

推荐答案

对于第二页,您需要了解两件事:

For the second page you need to know two things:

  1. 您要过滤的商店ID
  2. 您要开始的评论的关键

您已经具有商店ID,因此很容易.作为开始的密钥,请很好地使用上一页上最​​后一项的密钥,然后再请求一项.最后,您需要使用start()(可能还需要使用endAt():

You already have the store ID, so that's easy. As the key to start at, well use the key of the last item on the previous page, and then just request one item extra. Then finally, you'll need to use start() (and possibly endAt() for this:

return this.db.list('/reviews', ref => { 
    return ref.orderByChild('storeID')
              .startAt(storeID, lastKeyOnPreviousPage)
              .limitToLast(11)
});

这篇关于具有分页功能的Firebase过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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