Firebase 3 - 多对多关系......不太明白 [英] Firebase 3 - Many-to-many relationships...can't quite get it

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

问题描述

我对 Firebase 以及随之而来的新 Query API 比较陌生.

基本上,我有一个数据存储设置,用户可以在其中添加他们在个人资料中展示的来自 YouTube 的视频.由于用户可以拥有许多视频,而视频可以拥有许多用户,因此我尝试根据最佳实践以适当的方式构建我的数据存储,如下所示:

<代码>{用户{c25zdGFyb3NjaWFrQGdtYWlsLmNvbQ==视频{AFA-rOls8YA:真的,AllLsp4gnyDQ:真,dX_1B0w7Hzc:真,ik5qR5bw75E:是的,njos57IJf-0:真},视频{AFA-rOls8YA {附加成员{c25zdGFyb3NjaWFrQGdtYWlsLmNvbQ==:真}标题:你好……是我"},AllLsp4gnyDQ: { ... },dX_1B0w7Hzc: { ... },ik5qR5bw75E: { ... },njos57IJf-0: { ... },Zn7-fVtT16k}}}}

我该怎么做才能以这样的方式查询这些对象,以便我可以找回看起来像这样的单个用户:

c25zdGFyb3NjaWFrQGdtYWlsLmNvbQ:{视频:{AFA-rOls8YA:{附加成员:{ ... }标题:你好……是我"},AllLsp4gnyDQ: { ... },dX_1B0w7Hzc: { ... },ik5qR5bw75E: { ... },njos57IJf-0: { ... },zn7-fVtT16k: { ... }}}

我有很多实例需要发生这样的事情.我目前正在使用 Ionic 2、AngularFire 2,但如果需要,可以使用本机 Javascript SDK.我看到很多关于以这种方式构建数据的帖子,但我认为这些帖子并不像回答如何使用 Firebase 3 以最优化的现代方式获取数据那样直接.

对如何做到这一点有任何想法吗?

解决方案

所以他们让你取消隔离的原因是能够在不加载大量数据的情况下绑定到端点.如果您在加载该节点时将所有数据放在一个节点下,您将获得整个树.

我们经常做您想做的事情,但这不是一个单一的查询调用.

我看到 2 个主要选项,

一:对结果进行大循环的函数:

public getUser(userKey: string) {//假设使用 af2return this.af.database.object('path/to/users/' + 'userKey').map((user) => {for ( 让 vidKey 在 user.videos 中) {如果(user.videos.hasOwnProp(vidKey)){this.af.database.object('path/to/vids/' + vidKey).map((video) => {user.videos[vidKey] = 视频;});}}返回用户;});};

这会将您的用户作为对象加载,然后加载每个视频并在加载数据时更新该对象.现在,如果您确实需要一个大对象,我只会使用这种方法,但我认为您只想在用户列表中显示它,然后是每个视频的详细信息:

选项 2:更小的函数

TS:

//在构造函数之前公共用户 = this.getUser('1234');//构造函数后公共获取用户(用户密钥:字符串){return this.af.database.object('path/to/users/' + userKey);}公共 getUserVids(userKey: string) {return this.af.database.list('path/to/users/' + userKey + '/videos');}公共获取视频(vidKey:字符串){return this.af.database.object('path/to/vids/' + vidKey);}

在模板中:

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

这种方式要小得多,功能可以在不同的地方使用.这使用了从 AF2 返回的 observables,但可能是您真正想要做的,而不是一个巨大的 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天全站免登陆