如何在像Firestore这样的nosql解决方案中创建多租户数据模型? [英] How would one create a multitenant datamodel in a nosql solution like firestore?

查看:40
本文介绍了如何在像Firestore这样的nosql解决方案中创建多租户数据模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

多租户是什么意思?

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

问题

作为专家,您是否认为这是无法为Cloud Firestore设计的数据模型?可以做到这一点的数据模型是什么样的?

解决方案

作为专家,您认为这是无法为Firestore设计的数据模型吗?

绝对不是不可能,实际上很简单.

可以做到这一点的数据模型是什么样的?

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

  Firestore-root|---用户(集合)|||--- uid(文档)|||---组织:["organizationId","organizationId"](数组)|||---//其他用户属性|---组织(集合)|--- OrganizationId(文档)|---用户:["uid","uid"](数组)|---//其他组织属性|--- organizationData(集合)|--- organizationDataId(文档)|---//组织数据属性 

用户可以属于组织.

如您所见,用户ID被添加到 users 数组中,该数组是每个 organizationId 文档中的属性.Beeing a array,您可以添加该特定组织之外的所有用户的所有用户ID.

在没有任何人邀请的情况下进行注册的用户将被置于其自己的组织(blah123)中作为其管理员.

用户注册后,可以通过生成新的 organizationId 创建新的组织,并将用户的身份添加到 users 数组中.

在基于Firestore的同一应用程序中,多个这样的用户是他们自己的单个组织的创建者.他们可以邀请其他人,被邀请的人也可以加入创始人组织.

上面回答了.

为该组织创建的任何数据或该组织的用户创建的任何数据都应与另一个组织的用户隔离.

如您所见,我在 organizationId 文档下创建了一个名为 organizationData 的子集合,您可以在其中添加组织数据作为文档.由于您已经拥有该组织的用户的uid,因此您只需使用 Firestore安全规则,仅允许那些用户读取该数据.

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

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

就这样:)

What do I mean by multi-tenant?

  1. Users can belong to an organization.
  2. A user who signs up without any invitation from anyone, gets placed in their own organization (blah123) as its admin.
  3. 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.
  4. 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.
  5. 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?

解决方案

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

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.

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.

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解决方案中创建多租户数据模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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