Firebase数据架构指南(Firebase data schema guidance)

编程入门 行业动态 更新时间:2024-10-11 01:19:13
Firebase数据架构指南(Firebase data schema guidance)

我是Firebase的新手,并致力于使用firebase创建事件。 在这里,我正在邀请我的朋友使用他们的电话号码。(我邀请的人不一定是系统用户的一部分。)

以下是我的架构:

{ "events": [ { "message": "Lunch", "startTime": 1469471400000, "eventCreatorId": 1, "endTime": 1469471400000, "invitees": [ { "phone": "1234567890", "type": "phone" }, { "phone": "345678901", "type": "phone" } ] } ] }

现在的问题是我如何找到特定邀请的所有事件列表? (即在上面的情况下,我想找到电话号码为eqaul到345678901的用户的所有事件列表。)

任何人都可以建议好的架构来处理firebase的上述场景吗?

I am newbie to Firebase and working on to create events using firebase. Here i am inviting my friends using their phone number.(It is not necessary that whom i am inviting will be part of the system user.)

Below is my schema:

{ "events": [ { "message": "Lunch", "startTime": 1469471400000, "eventCreatorId": 1, "endTime": 1469471400000, "invitees": [ { "phone": "1234567890", "type": "phone" }, { "phone": "345678901", "type": "phone" } ] } ] }

Now problem is that how i can find list of all events for specific invites?? (i.e in above case i want to find list of all events for user with phone number eqaul to 345678901.)

Can anyone suggest good schema to handle above scenario with firebase?

最满意答案

欢迎使用NoSQL数据库。 :-)

在NoSQL中,您经常最终将数据建模为不同,以允许您希望应用程序执行的查询。 在这种情况下,您显然希望同时显示每个事件的被邀请者每个被邀请者的事件。 如果是这种情况,您将以两种格式存储数据:

{ "events": { "event1": { "message": "Lunch", "startTime": 1469471400000, "eventCreatorId": 1, "endTime": 1469471400000, "invitees": { "phone_1234567890": true, "phone_345678901": true } } }, "users": { "phone_1234567890": { "phone": "1234567890", "type": "phone", "events": { "event1": true } }, "phone_345678901": { "phone": "345678901", "type": "phone" "events": { "event1": true } } } }

您将看到我将您的数据拆分为两个独立的顶级节点:事件和用户。 它们通过所谓的显式索引相互引用,实质上是一组您在客户端代码中管理(和加入)的外键。

我也用命名键替换了你的数组。 如果您的事件/用户具有自然键(例如,如果您碰巧使用Firebase身份验证时用于识别用户的uid ),则可以使用该键作为密钥。 但除此之外,您可以使用Firebase推送ID。 使用这样的密钥导致更可扩展的数据结构,然后取决于数组索引。

有关数据结构的Firebase文档中涵盖了这两个主题。 我也强烈推荐这篇关于NoSQL数据建模的文章 。

Welcome to using a NoSQL database. :-)

In NoSQL you often end up modeling the data different, to allow the queries that you want your app to execute. In this case, you apparently want to both show the invitees per event and the events per invitee. If that is the case, you'll store the data in both formats:

{ "events": { "event1": { "message": "Lunch", "startTime": 1469471400000, "eventCreatorId": 1, "endTime": 1469471400000, "invitees": { "phone_1234567890": true, "phone_345678901": true } } }, "users": { "phone_1234567890": { "phone": "1234567890", "type": "phone", "events": { "event1": true } }, "phone_345678901": { "phone": "345678901", "type": "phone" "events": { "event1": true } } } }

You'll see that I've split your data into two separate top-level nodes: events and users. They refer to each other with so-called explicit indexes, essentially a set of foreign keys that you manage (and join) in your client-side code.

I've also replaced you arrays with named keys. If your events/users have natural keys (such as uid for identifying the user if you happen to use Firebase Authentication) you'd use that for the key. But otherwise, you can use Firebase push ids. Using such keys leads to a more scalable data structure then depending on array indices.

Both of these topics are covered in the Firebase documentation on data structuring. I also highly recommend this article on NoSQL data modeling.

更多推荐

本文发布于:2023-08-07 17:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465316.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:架构   指南   数据   Firebase   schema

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!