问题描述
多租户
是什么意思?
- 用户可以属于组织.
- 在没有任何人邀请的情况下注册的用户,将以自己的组织(blah123)的身份担任管理员.
- 多个此类用户是他们在同一基于Firestore的应用程序中自己的单个组织的创建者.他们可以邀请其他人,被邀请的人也可以加入创始人组织.
- 为该组织创建的任何数据或该组织的用户创建的任何数据都应与其他组织的用户隔离.
- 如果需要,我们应该能够配置属于同一组织的用户以查看该组织的其他用户创建的任何数据.
问题
作为专家,您是否认为这是无法为Cloud Firestore设计的数据模型?可以做到这一点的数据模型是什么样的?
绝对不是不可能,实际上很简单.
您的用例可能的数据库模式可能是:
Firestore-root|---用户(集合)|||--- uid(文档)|||---组织:["organizationId","organizationId"](数组)|||---//其他用户属性|---组织(集合)|--- OrganizationId(文档)|---用户:["uid","uid"](数组)|---//其他组织属性|--- organizationData(集合)|--- organizationDataId(文档)|---//组织数据属性
如您所见,用户ID被添加到 users
数组中,该数组是每个 organizationId
文档中的属性.Beeing a array,您可以添加该特定组织之外的所有用户的所有用户ID.
用户注册后,可以通过生成新的 organizationId
创建新的组织,并将用户的身份添加到 users
数组中.
上面回答了.
如您所见,我在 organizationId
文档下创建了一个名为 organizationData
的子集合,您可以在其中添加组织数据作为文档.由于您已经拥有该组织的用户的uid,因此您只需使用 Firestore安全规则,仅允许那些用户读取该数据.
在这种情况下,当用户加入组织时,您应该首先获取该组织的所有用户对象,然后查询数据库以获取那些用户所属的所有组织,并在所有这些组织中复制uid./p>
就这样:)
What do I mean by multi-tenant
?
- Users can belong to an organization.
- A user who signs up without any invitation from anyone, gets placed in their own organization (blah123) as its admin.
- 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 users of another organization.
- 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.
Question
As an expert, do you think this is an impossible datamodel to design for Cloud Firestore? What would a datamodel that can do this, look like?
Definitely is not impossible, actually is very simple.
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
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.
Once a user signs up, you create a new organization by generating a new organizationId
and add user's in users
array.
Answered above.
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.
In that case, when a user joins an organization you should first get all the user objects of that organizations, then query the database to get all the organizations that those users are apart of and copy the uid in all those organizations.
That's it :)
这篇关于如何在像Firestore这样的nosql解决方案中创建多租户数据模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!