简体   繁体   English

在Firebase中查询多个记录

[英]Query for multiple records in firebase

I'm implementing an orbit.js adapter for firebase, orbit-firebase . 我正在为firebaseorbit- firebase实现一个orbit.js适配器。

I'm looking for an efficient way to query for multiple records so that I can resolve relationships between objects eg course.participants 我正在寻找一种查询多个记录的有效方法,以便我可以解决对象之间的关系,例如course.participants

{
  course: {
    'c1': {
      participants: ['p1', 'p2']
    }
  },
  participant: {
    'p1': {
      name: "Jim"
    },
    'p2': {
      name: "Mark"
    }
  }
}

Given I have the ids 'p1' and 'p2' what's an efficient way to query for both of them? 给定我有id'p1'和'p2',什么是查询它们的有效方法?

I can't use a query because I'm using security rules with the participants ie the user that's trying to resolve course.participants doesn't have access to all of the participants (bear in mind this is a contrived example). 我无法使用查询,因为我正在与参与者一起使用安全规则,即试图解决课程的用户。参与者没有访问所有参与者的权限(请记住,这是一个人为的示例)。

I'd recommend that you move away from arrays in your JSON structures. 我建议您远离 JSON结构中的数组 These are nothing but pain in real-time, distributed data and don't work particularly well with security rules and situations like this. 这些只不过是实时分布式数据上的难题,并且在诸如此类的安全规则和情况下无法很好地发挥作用。

Given this structure: 鉴于此结构:

course: {
  'c1': {
    participants: {
       'p1': true, 'p2': true
    }
  }
}

I could join these fairly easily. 我可以很容易地加入这些。 You can get a normalized ref that behaves just like a Firebase ref by using Firebase.util's NormalizedCollection : 您可以通过使用Firebase.util的NormalizedCollection获得行为类似于Firebase引用的规范化引用:

var ref = new Firebase(...);
var coll = new Firebase.util.NormalizedCollection(
   ref.child('course/c1/participants'),
   ref.child('participant')
).select('participant.name').ref();

coll.on('child_added', function(snap) {
   console.log('participant ' + snap.key(), snap.val());
});

Note that this data structure (sans the array) will also make it simpler to enforce read rules on participant data and the like by allowing you to directly reference the user ids under $courseid/participants/ , since they are now keys that can match a $ variable . 请注意,通过允许您直接引用$courseid/participants/下的用户ID,此数据结构(没有数组)也将使在参与者数据等上执行读取规则更加容易,因为它们现在是可以与$变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM