Firebase Firestore-关系数据设计方法 [英] Firebase Firestore - relational data design approaches

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

问题描述

我对Firebase完全陌生,我正在努力使有关1-1和1-many的关系"数据的最佳数据库模型设计成为现实.

I'm totally new to Firebase, and I'm trying to get my head round the best db model design for 'relational' data, both 1-1 and 1-many.

我们正在使用 Firestore 数据库(不是实时数据库).

We are using the Firestore db (not the realtime db).

假设我们有Projects,其中可以包含许多Users,而User可以位于多个Projects

Say we have Projects which can contain many Users, and a User can be in multiple Projects

用户界面需要在Project中显示Users的列表,该列表显示emailfirstnamelastnamedepartment之类的内容.

The UI needs to show a list of Users in a Project which shows things like email, firstname, lastname and department.

存储关系的最佳方法是什么?

What is the best way to store the relationship?

  • Project文档中的User id数组?
  • Project文档中的ID映射?
  • An array of User ids in the Project document?
  • A map of Ids in the Project document?

我已经读过以上建议的方法,但是对于实时数据库而言? Firestore 支持Sub Collection,听起来更合适...

Ive read the above approaches were recommended, but was that for realtime database? Firestore supports Sub Collections, which sound more appropriate...

  • A sub collection of Users in the Project document?
  • A separate collection mapping Project id to User id?
  • A Reference data type? I've read here https://firebase.google.com/docs/firestore/manage-data/data-types about Reference data type, which sounds like what I want, but I cant find any more on it!

如果它只是一个ID的映射或数组,那么您将如何检索有关用户的其余数据?这是否必须坐在应用程序UI中?

If its just a map or array of Ids, how would you then retrieve the remaining data about the user? Would this have to sit in the application UI?

如果它是用户文档的子集合,是否有任何方法可以维护数据完整性?如果用户更改了名称,那么UI/cloudFunction是否必须更新Su​​b集合中该用户名称的每个条目?

If its a sub collection of Users documents, is there any way to maintain data integrity? If a user changed their name, would the UI / a cloudFunction then have to update every entry of that users name in the Sub collections?

任何帮助/指针都表示赞赏...

any help / pointers appreciated...

推荐答案

在Firestore中建模多对多关系的方法与在Firebase的实时数据库"中的方法几乎相同,我在这里已经回答: Firebase中的多对多关系.确实唯一的区别是,您可以将查找列表存储在每个项目/用户的子集合中.

The approach for modeling many-to-many relationships in Firestore is pretty much the same as it was in Firebase's Realtime Database, which I've answered here: Many to Many relationship in Firebase. The only difference is indeed that you can store the lookup list in a sub-collection of each project/user.

查找链接项也与以前相同,确实需要从客户端分别加载它们.这样的客户端连接速度并没有您最初预期的那么慢,因此请在假定连接速度不够快之前对其进行测试.

Looking up the linked item is also the same as before, it indeed requires loading them individually from the client. Such a client-side join is not nearly as slow as you may initially expect, so test it before assuming it can't possibly be fast enough.

通过执行分批写入或使用交易.这些要么完全成功,要么完全失败.

Ensuring data integrity can be accomplished by performing batched writes or using transactions. These either completely succeed or completely fail.

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

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