Firebase/AngularFire 中的数据建模最佳实践 [英] Data Modeling Best Practices in Firebase/AngularFire

查看:54
本文介绍了Firebase/AngularFire 中的数据建模最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次在 Firebase 中开发一个应用程序,我很好奇我应该如何在两个对象之间建模数据,一个 user 和一个 post.我来自更多的关系数据库背景,不仅很好奇在非关系数据库中如何做到这一点,而且特别好奇如何在 Firebase 中的两个对象之间建立关系.

I'm developing an application in Firebase for the first time and was curious how I should model the data between two objects, a user and a post. I come from more of a relational db background and was curious not only how this would be done in nonrelational DBs but specifically how to set up a relationship between two objects in Firebase.

例如,我的应用程序有很多用户,每个用户都会创建很多帖子.

For example, my application has many Users, and each user creates many Posts.

User {
    firstName: String,
    lastname: String,
    userName: String
}

Post {
    title: String,
    content: String,
    date: Date,
    writtenBy: [User object?]
}

我应该如何在 Firebase 中构建这两个对象,以便帖子属于一个用户,但无论用户如何,都可以查询所有帖子,并且用户和帖子对象都可以在不中断其他对象的数据和/或关系?

How should I structure these two objects in Firebase so that a Post belongs to a User, but all Posts can be queried for regardless of User, and both User and Post objects can be edited without disrupting the other object's data and/or relationship?

我应该如何通过 firebase 创建新的关系"对象:

And how should I create new "relational" objects via firebase:

sync.$set({userA: {
   firstname: "Billy",
   lastName: "Bob",
   userName: "BillyBob",
   Posts: {
       // .....
   }
}
});

谢谢!

推荐答案

Firebase 在构建时考虑到了性能.这就是您必须以不同方式设计数据结构的原因,在大多数情况下,规范化是您的敌人.Firebase 中的每个对象都可以通过 URL 访问,您应该始终牢记这一点.设计数据结构的方法还有很多,这取决于您要执行的查询.如果查询之一是能够显示所有消息(我相信许多最新消息将是最常见的用例),但同时您希望能够显示每个用户的消息而不是可能的数据结构可能如下所示:

Firebase is built with performance in mind. This is the reason you have to design data structures differently, normalization is your enemy in most cases. Every object in Firebase can be accessed by URL, and you should always keep this in mind. There are still many ways of designing the data structures, it depends on what queries do you want to execute. If one of the queries is to be able to display all messages (I believe a number of latest messages would be the most common use case), but at the same time you want to be able to show messages per user than one of the possible data structures could look like this:

User {
    userId(assigned by Firebase automatically) {
        firstName: String,
        lastname: String,
        userName: String
    }
}

Post {
    User {
        userId(matching userId in the User object) {
            postId(assigned by Firebase for every new post automatically) {
                title: String,
                content: String,
                date: Date,
                writtenBy: String, userName or userId (this is not really needed, but may keep it for easier data access)
            }
        }
    }
}

然后您可以更改任何用户数据,而无需触发帖子中的数据更改事件,就像您的示例一样(如果您有大量消息,这将非常繁重).您可以独立于用户获取所有消息:

Then you can change any user data without triggering data change events in Posts, like in your example, (which would be extremely heavy if you have large number of messages). You can get all messages independently of user:

var postListRef = new Firebase(URL);
var lastPostQuery = postListRef.child("Post").limit(500);

您还可以使用 startAt() 和 endAt() 查询 https://www.firebase.com/docs/web/api/query/limit.html作为一个缺点 - 如果您只需要显示消息,您必须解压 for 循环中的每条消息,但我希望您也能显示用户信息,所以应该没问题.

You can also use startAt() and endAt() quesries https://www.firebase.com/docs/web/api/query/limit.html As a drawback - you have to unpack every message in the for loop if you need to show only messages, but I would expect you would show user info as well, so it should be ok.

如果您只想收听一条用户消息,非常简单快捷:

If you want to listen for just one user messages, it's very simple and fast:

var postListRef = new Firebase(URL);
var lastPostQuery = postListRef.child("Post/User").child(userId);

Angular/AngularFire 对这种数据结构有很好的支持.

And Angular/AngularFire has great support for this kind of data structures.

这篇关于Firebase/AngularFire 中的数据建模最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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