构建Firebase数据 [英] Structuring Firebase data

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

问题描述

在为Firebase构造数据时遇到一些困难.我有用途和房间.每个用户可以加入多个房间,每个房间可以有多个用户.

I have some difficulties with structuring the data for Firebase. I have uses and rooms. Each user can join multiple rooms and each room can have multiple users.

这是我现在的设置:

我将所有房间ID保存在链接的设备(room_info)上.所有链接的设备都在房间子项的键-> device_info下.我将设备名称保存在那里以便快速查找.

I save all the room id's at the linked device (room_info). All linked devices are under the key -> device_info at the room child. I save the device name there for fast lookup.

我已使用此网页来验证我的想法: https://www.firebase.com/docs/web/guide/structuring-data.html

I have used this webbpage to validate my idea: https://www.firebase.com/docs/web/guide/structuring-data.html

有什么可以做得更好的吗?

Is there something that can be done better?

我想保存设备状态(如果设备在线).我想在房间键下创建一个device_state子级.我想保存所有在线的设备ID.并在离线时将其删除.那是个好方法吗?

And I want to save the device state (if devices are online). I want to create a device_state child under the room key. And I want to save all device id's there that are online. And remove them when offline. Is that a good approach?

推荐答案

您需要知道,构建Firebase数据库没有完美的解决方案.最好的解决方案是适合您的需求并使您的工作更轻松的解决方案.考虑到一切都是为了视图,我建议您要记住一些事情.

You need to know that there is no perfect solution for structuring a Firebase database. The best solution, is the solution that fits your needs and makes your job easier. Having in mind that everything is for the view, i suggest you a few things to remember.

  1. 深度不是从技术角度影响速度的因素.Firebase可以像在第1层一样快速地在第30层上查找节点.快速影响性能的主要因素是您的应用读取/写入的数据大小可用带宽.

去规范化是Firebase中的常见做法.

Denormalization is a common practice within Firebase.

如果谈到可伸缩性,我们还需要记住,firebase中的一个重要规则是使数据尽可能平坦.请参阅以下帖子,为复杂应用正确构建Firebase数据,以便更好地理解.

If we speak about scalability we need also to keep in mind that an important rule in firebase is to have the data as flatten as possible. Please see the following post, Structuring your Firebase Data correctly for a Complex App, for a better understanding.

但是,根据使用情况,通常最好保持数据平坦.因此,如果不需要,不要过度复杂化您的结构.不要只是为了使数据库进行非规范化而使用非规范化.

However, keeping the data flat is usually better depending on your use case. So don't go overcomplicating your structure if you don't need to. Don't use denormalization just for your sake for having the database denormalized.

  1. 另一种想法是使您的数据始终可重复使用.

关于您的实际Firebase数据库结构,如果可以轻松查询它以获取所需的数据,请继续进行操作.如果您想了解更多信息,建议您访问上面的链接.

Regarding your actual Firebase database structure, if you can query it easily to get the desired data, then go ahead with it. If you want more information, i suggest you visit the links above.

如果您有SQL背景,我也建议您参阅本教程

If you have a SQL background, i also suggest you see this tutorial, The Firebase Database For SQL Developers.

关于您的最后一个问题,是的,这是一个非常好的方法.

Regarding your last question, yes, it's a very good approach.

希望有帮助.

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

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