如何在像 firestore 这样的 nosql 解决方案中创建多租户数据模型?

How would one create a multitenant datamodel in a nosql solution like firestore?

multi-tenant 是什么意思?

  1. 用户可以属于一个组织。
  2. 在没有任何人邀请的情况下注册的用户,被安置在他们自己的组织 (blah123) 中作为其管理员。
  3. 在同一个基于 firestore 的应用程序中,多个此类用户是他们自己的单一组织的创始人。他们可以邀请其他人,被邀请的人加入创始人的组织。
  4. 为该组织创建的任何数据或该组织的用户创建的任何数据都应与另一个组织的用户隔离
  5. 如果需要,我们应该能够配置属于同一组织的用户以查看该组织的其他用户创建的任何数据。

问题

作为专家,您认为为 Cloud Firestore 设计这种数据模型是不可能的吗?可以执行此操作的数据模型会是什么样子?

As an expert, do you think this is an impossible datamodel to design for firestore?

肯定是不可能,其实很简单。

What would a datamodel that can do this, look like?

您的用例可能的数据库模式可能是:

Firestore-root
   |
   --- users (collection)
   |    |
   |    --- uid (document)
   |         |
   |         --- organizations: ["organizationId", "organizationId"] (array)
   |         |
   |         --- //Other user properties
   |
   --- organizations (collection)
         |
         --- organizationId (document)
                |
                --- users: ["uid", "uid"] (array)
                |
                --- //Other organization properties
                |
                --- organizationData (collection)
                      |
                      --- organizationDataId (document)
                             |
                             --- //Organization Data properties

Users can belong to an organization.

如您所见,用户的 ID 添加到 users 数组中,该数组是每个 organizationId 文档中的 属性。作为一个数组,您可以添加属于该特定组织的所有用户的所有用户 ID。

A user who signs up without any invitation from anyone, gets placed in their own organization (blah123) as its admin.

用户注册后,您可以通过生成新的 organizationId 并在 users 数组中添加用户来创建新组织。

Multiple such users are founders of their own single organization in the same firestore based application. They can invite others and those invited join the founder's organization.

上面已经回答了。

Any data created for the organization or any data created by the users of that organization should be segragated from the the users of another organization.

如您所见,我在 organizationId 文档下创建了一个名为 organizationData 的子集合,您可以在其中添加文档组织数据。因为您已经拥有不属于该组织的用户的 uid,所以您可以简单地使用 Firestore security rules 只允许那些用户读取该数据。

If we want, we should be able to configure the users that belong to the same organization to view any data that other users of that organization created.

在这种情况下,当用户加入组织时,您应该首先获取该组织的所有用户对象,然后查询数据库以获取这些用户所属的所有组织,并复制所有这些组织中的 uid .

就是这样:)