AngularFire - 如何查询非规范化数据? [英] AngularFire - How do I query denormalised data?

查看:31
本文介绍了AngularFire - 如何查询非规范化数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我刚开始使用 Firebase.我读过这个:https://www.firebase.com/docs/data-structure.html 我读过这个:https://www.firebase.com/博客/2013-04-12-denormalizing-is-normal.html

Ok Im starting out fresh with Firebase. I've read this: https://www.firebase.com/docs/data-structure.html and I've read this: https://www.firebase.com/blog/2013-04-12-denormalizing-is-normal.html

所以我很困惑,因为一个似乎与另一个矛盾.您可以分层构建数据,但如果您希望它具有可扩展性,那就不要这样做.然而,这并不是真正的问题.

So I'm suitably confused as one seems to contradict the other. You can structure your data hierarchically, but if you want it to be scalable then don't. However that's not the actual problem.

我有以下博客引擎结构(如果有误,请纠正我):

I have the following structure (please correct me if this is wrong) for a blog engine:

"authors" : {
  "-JHvwkE8jHuhevZYrj3O" : {
    "userUid" : "simplelogin:7",
    "email"   : "myemail@domain.com"
  }
},
"posts" : {
  "-JHvwkJ3ZOZAnTenIQFy" : {
    "state" : "draft",
    "body" : "This is my first post",
    "title" : "My first blog",
    "authorId" : "-JHvwkE8jHuhevZYrj3O"
  }
}

作者列表和帖子列表.首先,我想获得 userUid 等于我当前用户的 uid 的作者.然后我想获取 authorId 是提供给查询的帖子.

A list of authors and a list of posts. First of all I want to get the Author where the userUid equals my current user's uid. Then I want to get the posts where the authorId is the one provided to the query.

但我不知道如何做到这一点.任何帮助,将不胜感激!如果这有所作为,我将使用 AngularFire.

But I have no idea how to do this. Any help would be appreciated! I'm using AngularFire if that makes a difference.

推荐答案

Firebase 是一种 NoSQL 数据存储.它是一个 JSON 层次结构,没有传统意义上的 SQL 查询(这些与闪电般快速的实时操作并不真正兼容;它们往往既慢又昂贵).有一些 map reduce 风格的功能(合并视图和工具来协助)的计划,但你目前的主要武器是适当的数据结构.

Firebase is a NoSQL data store. It's a JSON hierarchy and does not have SQL queries in the traditional sense (these aren't really compatible with lightning-fast real-time ops; they tend to be slow and expensive). There are plans for some map reduce style functionality (merged views and tools to assist with this) but your primary weapon at present is proper data structure.

首先,让我们处理树层次结构与非规范化数据.以下是您应该非规范化的一些内容:

First of all, let's tackle the tree hierarchy vs denormalized data. Here's a few things you should denormalize:

  • 您希望能够快速迭代的列表(用户名列表,无需下载用户写过的每条消息或有关用户的所有其他元信息)
  • 您查看部分的大型数据集,例如用户所属的房间/组列表(您应该能够获取给定用户的房间列表,而无需下载系统中的所有组/房间,因此把索引放在一个地方,主房间数据放在其他地方)
  • 任何超过 1,000 条记录的内容(保持精简以提高速度)
  • 包含 1..n(即可能无限)条记录的路径下的子项(例如来自聊天室元数据的聊天消息,这样您就可以获取有关聊天室的信息而无需获取所有消息)

以下是一些非规范化可能没有意义的事情:

Here's a few things it may not make sense to denormalize:

  • data 你总是获取 en toto 并且从不迭代(如果你总是使用 .child(...).on('value', ...) 获取一些记录并显示所有内容在该记录中,从不引用父列表,没有理由优化可迭代性)
  • 列出短于一百条左右的记录,您总是作为一个整体(例如,用户所属的组列表可能总是与该用户一起获取,平均为 5-10 项;可能没有理由将其分开)
  • data you always fetch en toto and never iterate (if you always use .child(...).on('value', ...) to fetch some record and you display everything in that record, never referring to the parent list, there's no reason to optimize for iterability)
  • lists shorter than a hundred or so records that you always as a whole (e.g. the list of groups a user belongs to might always be fetched with that user and would average 5-10 items; probably no reason to keep it split apart)

获取作者就像在 URL 中添加 id 一样简单:

Fetching the author is as simple as just adding the id to the URL:

var userId = 123;
new Firebase('https://INSTANCE.firebaseio.com/users/'+userId);

要获取属于某个用户的帖子列表,请维护该用户帖子的索引:

To fetch a list of posts belonging to a certain user, either maintain an index of that users' posts:

/posts/$post_id/...
/my_posts/$user_id/$post_id/true

var fb = new Firebase('https://INSTANCE.firebaseio.com');
fb.child('/my_posts/'+userId).on('child_added', function(indexSnap) {
   fb.child('posts/'+indexSnap.name()).once('value', function(dataSnap) {
       console.log('fetched post', indexSnap.name(), dataSnap.val());
   });
});

Firebase.util 之类的工具可以帮助规范化已拆分存储的数据直到 Firebase 的视图和高级查询实用程序发布:

A tool like Firebase.util can assist with normalizing data that has been split for storage until Firebase's views and advanced querying utils are released:

/posts/$post_id/...
/my_posts/$user_id/$post_id/true

var fb = new Firebase('https://INSTANCE.firebaseio.com');
var ref = Firebase.util.intersection( fb.child('my_posts/'+userId), fb.child('posts') );
ref.on('child_added', function(snap) {
   console.log('fetched post', snap.name(), snap.val();
});

或者简单地按用户 ID 存储帖子(取决于您稍后如何获取数据的用例):

Or simply store the posts by user id (depending on your use case for how that data is fetched later):

/posts/$user_id/$post_id/...

new Firebase('https://INSTANCE.firebaseio.com/posts/'+userId).on('child_added', function(snap) {
   console.log('fetched post', snap.name(), snap.val());
});

这篇关于AngularFire - 如何查询非规范化数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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