Firebase 3 - 多对多的关系...不能完全得到它 [英] Firebase 3 - Many-to-many relationships...can't quite get it

查看:200
本文介绍了Firebase 3 - 多对多的关系...不能完全得到它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Firebase相对来说比较新,也有新的查询API。

基本上,我有一个数据存储设置用户可以从YouTube上添加他们在个人资料上展示的视频。由于用户可以拥有许多视频,并且视频可以有很多用户,所以我试图根据最佳实践以适当的方式构建我的数据存储,如下所示:

 {
users {
c25zdGFyb3NjaWFrQGdtYWlsLmNvbQ ==
videos {
AFA-rOls8YA:true,
AlLsp4gnyDQ:true,
dX_1B0w7Hzc:true,
ik5qR5bw75E:true,
njos57IJf-0:true
},
videos {
AFA-rOls8YA {
attached_members {
c25zdGFyb3NjaWFrQGdtYWlsLmNvbQ ==:true
}
title:Hello ... it's me
},
AlLsp4gnyDQ:{...},
dX_1B0w7Hzc :{...},
ik5qR5bw75E:{...},
njos57IJf-0:{...},
zn7-fVtT16k
}
}






$ b

我可以怎样在这样的一个查询这些对象在哪里我可以ge t
$ b $ pre code $ c> c25zdGFyb3NjaWFrQGdtYWlsLmNvbQ:{
videos:{
AFA- rOls8YA:{
attached_members:{...}
标题:你好...是我
},
AlLsp4gnyDQ:{...},
dX_1B0w7Hzc:{...},
ik5qR5bw75E:{...},
njos57IJf-0:{...},
zn7-fVtT16k:{...}
}
}

我有很多这样的事情需要发生。我目前正在使用Ionic 2,AngularFire 2,但如果需要的话,可以使用原生的JavaScript SDK。我看到很多关于如何以这种方式构建数据的帖子,但是我不认为这些帖子就像用Firebase 3以最优化的现代方式回答问题一样简单。



任何想法如何做到这一点?

解决方案

所以他们有你desegregate的原因是能够在不加载大量数据的情况下绑定到端点。如果您在加载该节点时将所有数据都放在单个节点下,则会得到整个树。

我们经常做你想做的事情,但它不是一个单一的查询调用。

我看到了两个主要选项:

结果:

public getUser(userKey:string){
/ /假设使用af2
return this.af.database.object('path / to / users /'+'userKey')。map((user)=> {
for(let vidKey in user .videos){
if(user.videos.hasOwnProp(vidKey)){
this.af.database.object('path / to / vids /'+ vidKey).map((video)= > {
user.videos [vidKey] = video;
});
}
}
返回用户;
});
};

这会将您的用户加载为对象,然后加载每个视频并在数据更新时更新对象加载。现在我只使用这个方法,如果你真的需要一个大对象,但我想你只是想显示在用户列表中,然后每个vid的细节:

选项2:较小的功能


pre class =lang-typescript prettyprint-override> //在构造函数
public user = this.getUser('1234');

//在构造函数之后
public getUser(userKey:string){
返回this.af.database.object('path / to / users /'+ userKey);
}
public getUserVids(userKey:string){
return this.af.database.list('path / to / users /'+ userKey +'/ videos');
}
public getVideo(vidKey:string){
return this.af.database.object('path / to / vids /'+ vidKey);

在模板中:

 < div class =user* ngIf =user> 
< h3>名称:{{user.name}}< / h3>
< div class =video* ngFor =let video of geUserVids(user。$ key)| async>
< p>标题:{{video.title}}< / p>
< p>长度:{{video.length}}
< / div>
< / div>

这种方式要小得多,可以在不同的地方使用。这使用AF2的可观察的返回,但可能是你真正想要做的,而不是一个巨大的JSON对象...

I'm relatively new to Firebase, as well as the new Query API that comes along with it.

Basically, I've got a data store setup where users can add videos from YouTube that they showcase on their profiles. Since users can have many videos and videos can have many users, I've attempted to construct my data store in the appropriate fashion, based on best practices, as shown here:

{
    users {
     c25zdGFyb3NjaWFrQGdtYWlsLmNvbQ==
       videos {
         AFA-rOls8YA: true,
         AlLsp4gnyDQ: true,
         dX_1B0w7Hzc: true,
         ik5qR5bw75E: true,
         njos57IJf-0: true
       },
       videos {
         AFA-rOls8YA {
           attached_members {
             c25zdGFyb3NjaWFrQGdtYWlsLmNvbQ==: true
           }
           title: "Hello...it's me"
         },
         AlLsp4gnyDQ: { ... },
         dX_1B0w7Hzc: { ... },
         ik5qR5bw75E: { ... },
         njos57IJf-0: { ... },
         zn7-fVtT16k
        }
      }
    }
}

What can I do to query these objects in such a way where I can get back a single user that looks like this:

c25zdGFyb3NjaWFrQGdtYWlsLmNvbQ: {
  videos: {
     AFA-rOls8YA: {
       attached_members: { ... }
       title: "Hello...it's me"
     },
     AlLsp4gnyDQ: { ... },
     dX_1B0w7Hzc: { ... },
     ik5qR5bw75E: { ... },
     njos57IJf-0: { ... },
     zn7-fVtT16k: { ... }
  }
}

I have many instances where something like this is going to need to happen. I'm currently using Ionic 2, AngularFire 2, but can use the native Javascript SDK if needed. I see many posts about structuring the data in this way, but I don't see that posts as straight forward as answering how to get it in the most optimized modern way, with Firebase 3.

Any thoughts on how to do this?

解决方案

So the reason they have you desegregate is to be able to bind to endpoints without loading a ton of data. If you had all your data under a single node when you load that node you'd get the entire tree.

We do what you are trying to do often, but it's not a single query call.

I see 2 main options,

One: A function to do a big loop over the results:

public getUser(userKey: string) {
  // assumes using af2
  return this.af.database.object('path/to/users/' + 'userKey').map((user) => {
    for ( let vidKey in user.videos) {
      if (user.videos.hasOwnProp(vidKey)) {
        this.af.database.object('path/to/vids/' + vidKey).map((video) => {
          user.videos[vidKey] = video;
        });
      }
    }
    return user;
  });
};

This would load your user as an object, then load each video and update the object when the data is loaded. Now I'd only use this method if you actually need one big object but I think you just want to display it in a list of users, then details on each vid:

Option 2: Smaller functions

TS:

// before constructor
public user = this.getUser('1234');

//after constructor
public getUser(userKey: string) {
  return this.af.database.object('path/to/users/' + userKey);
}
public getUserVids(userKey: string) {
 return this.af.database.list('path/to/users/' + userKey + '/videos');
}
public getVideo(vidKey: string) {
  return this.af.database.object('path/to/vids/' + vidKey);
}

In the template:

<div class="user" *ngIf="user">
    <h3>Name: {{user.name}}</h3>
    <div class="video" *ngFor="let video of geUserVids(user.$key) | async">
        <p>title: {{video.title}}</p>
        <p>length: {{video.length}}
    </div>
</div>

This way is much smaller and the functions could be used in different places. This uses the observables return from AF2 but is probably what you really want to do, not a giant json object...

这篇关于Firebase 3 - 多对多的关系...不能完全得到它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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