首页 > 解决方案 > What is the best practice for a multi-tenant app with Collection Group Queries in Firestore?

问题描述

What is the best practice for multi-tenant app collection group queries? For example query all invoices for a tenant's customers

/tenant/1/customer/2/invoices

If I create a collection group index called invoices, and I want to ensure that I can get all invoices for tenant 1, how do I do this easily?

I tried setting up some security rules to prevent from querying over the tenants but it still threw Access denied errors since it was still querying across tenants. What would the correct firestore rules look like

标签: firebasegoogle-cloud-firestore

解决方案


Firebase 项目不太适合多租户应用。建议您为每个租户创建不同的项目。 这将为您在将来节省很多问题。

如果您绝对必须在单个项目上进行多租户,那么您当前的数据库结构不能很好地支持集合组查询。集合组查询总是查询给定名称的每个集合,没有例外。您不能使用安全规则来过滤结果,因为规则不是过滤器。过滤只能来自客户端,并由安全规则确认。使用您当前的结构,您需要将租户的 ID 存储在您打算使用集合组查询查询的每个文档中,并让客户端将其用作结果的过滤器。


推荐阅读