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

pul*_*hal 1 database-design datamodel firebase google-cloud-firestore

我是什么意思multi-tenant

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

作为专家,您认为这是为 Cloud Firestore 设计的不可能的数据模型吗?可以做到这一点的数据模型会是什么样子?

Ale*_*amo 15

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

Definitely is not impossible, actually is very simple.

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

A possible database schema for your use-case might be:

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
Run Code Online (Sandbox Code Playgroud)

Users can belong to an organization.

As you can see, the id of the user is added in users array which is a property within each organizationId document. Beeing an array, you can add all user ids of all users that are apart of that particular organization.

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

Once a user signs up, you create a new organization by generating a new organizationId and add user's in users array.

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.

Answered above.

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.

As you can see, I have created a subcollection named organizationData under organizationId document in which you can add as documents organization data. Because you already have the uid's of the user that are apart of this organization, you can simply use Firestore security rules to allow only those users to read that data.

如果需要,我们应该能够配置属于同一组织的用户以查看该组织的其他用户创建的任何数据。

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

就是这样 :)