Firestore中的多租户 [英] Multitenancy in Firestore
问题描述
关于基于子集合值查询数据的实际限制,在Firestore中管理多租户的建议方法是什么?
我希望能够检索并限制访问与用户所属实体/公司有关的数据。
示例数据结构:
/ companies / {companyId} / users /
/ companies / {companyId} / users / {user} / roles
/ companies / {companyId} / docs /
可以 / companies / {companyId} / users /
是一个集合?
我如何才能检索用户在 / companies / {companyId} / users $ c $中拥有角色的公司
$ b $
Firestore路径从收集到文档并再次返回: b
/ collection / document / subcollection / subdocument
所以是的,在这种情况下,您将收集公司
,用户
和 docs
。集合也是隐式的,因为它们是在文档存在于文档中时自动创建的,当文档中不存在文档时会被删除。
目前,子集合查询(例如给定公司中的所有用户)不受支持,因此您必须以其他方式构建查询:在执行查询以查找所有用户时将公司的用户集合作为属性在那家公司里。
ref.collection('users')。where('company','==','ACME ').get()。then((document)=> {/ * Do stuff here * /});
Regarding the actual limitations in querying data based on subcollections values, what is the suggested way to manage multitenancy in Firestore?
I would like to be able to retrieve and limit access to data related to entities/companies the user is part of.
Example data structure :
/companies/{companyId}/users/
/companies/{companyId}/users/{user}/roles
/companies/{companyId}/docs/
Can /companies/{companyId}/users/
be a collection?
How can I only retrieve companies where user own a role in /companies/{companyId}/users
?
Firestore paths alternate from collection to document and back again:
/collection/document/subcollection/subdocument
So yes, in this case, you would have collections of companies
, users
, and docs
. Collections are also implicit in that they are created automatically when documents exist in them, and removed when no documents exist in them.
At present, subcollection queries (e.g. "all users in a given company") aren't supported, so you'll have to structure your query the other way around: having a users collection with company as a property, when performing a query to find all users in that company.
ref.collection('users').where('company', '==', 'ACME').get().then((document) => {/* Do stuff here */});
这篇关于Firestore中的多租户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!