本文介绍了查询Firebase中的多个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为firebase实施适配器,。



我正在寻找一种有效的方法来查询多个记录,以便我可以解析对象之间的关系,例如course.participants

  {
课程:{
'c1':{
参与者: ['p1','p2']
}
},
参与者:{
'p1':{
名称:Jim
} ,
'p2':{
name:Mark
}
}
}

鉴于我有ids'p1'和'p2',查询这两者的有效方法是什么?



我不能使用查询,因为我正在与参与者使用安全规则,即尝试解析course.participants的用户无法访问所有参与者(请记住这是一个人为的例子)。
解决方案

我建议你。这些只是实时的痛苦,分布式数据,并不适用于这样的安全规则和情况。



给定这种结构:

 课程:{
'c1':{
参与者:{
'p1':true,' p2':true
}
}
}

加入这些相当容易。您可以通过使用获得一个正常化的引用,就像Firebase引用一样:

  var ref = new Firebase(...); 
var coll = new Firebase.util.NormalizedCollection(
ref.child('course / c1 / participants'),
ref.child('participant')
).select ( 'participant.name')REF();
$ b coll.on('child_added',function(snap){
console.log('participant'+ snap.key(),snap.val());
});

请注意,这个数据结构(不包含数组)也会使参与者执行读取规则变得更简单数据和类似的,因为它们现在可以匹配一个。


I'm implementing an orbit.js adapter for firebase, orbit-firebase.

I'm looking for an efficient way to query for multiple records so that I can resolve relationships between objects e.g. 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?

I can't use a query because I'm using security rules with the participants i.e. 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. 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:

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.

这篇关于查询Firebase中的多个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 18:08