数据建模最佳实践火力地堡/ AngularFire地堡、建模、火力、数据

2023-09-14 23:08:18 作者:你比大姨妈还耀眼

我正在开发的火力地堡首次申请,并很好奇,我应该怎么两个对象,一个用户和后。我来自一个多关系数据库背景,很好奇,不仅是如何做到这一点的非关系型的数据块进行,但具体如何设置两个对象之间的关系在火力地堡。

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?]
}

我应该如何在火力地堡结构这两个对象,这样一个帖子属于用户,但所有的帖子,可以被询问不管用户,无一不用户和帖子的对象可以在不破坏其他对象的数据进行编辑和/或关系?

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?

和我应该如何通过火力创造新的关系的对象:

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

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

谢谢!

推荐答案

火力地堡是建立在考虑性能。这就是你要设计不同的数据结构的原因,归在大多数情况下你的敌人。在火力地堡每个对象都可以通过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()quesries 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);

和角/ AngularFire对这种数据结构的大力支持。

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