Multitenancy in Firestore

2019-08-05 15:51发布

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 ?

1条回答
迷人小祖宗
2楼-- · 2019-08-05 16:08

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 */});
查看更多
登录 后发表回答