Firestore 的复杂数据模型

Raf*_*nez 5 data-modeling firebase google-cloud-firestore

我正在使用 Firestore + angularfire 构建我的第一个应用程序,由于缺乏 Firestore 经验,定义数据模型非常困难。我需要你的帮助。

我有以下实体和依赖项

  1. 餐厅:它们是独立的,可以由应用程序的用户注册。
  2. 创建餐厅的用户成为“所有者”。他/她可以邀请其他用户成为餐厅的“服务员”。
  3. 用户可以从一个或多个餐厅(可能来自不同的所有者)收到“成为我的服务员”邀请。
  4. 我们还有另一个实体“客户”。该实体的条目包含有关订购的菜肴和饮料、价格、小费等的数据...

然后是以下规则

  1. 每个业主可以看到他/她的餐厅内的任何“客户”数据,但看不到其他餐厅的数据。
  2. 每个“服务员”都可以看到他/她服务的顾客(在任何餐厅)的数据,但看不到其他顾客的数据。

正如您所看到的,存在多个依赖项,包括多个 OR 条件,Firestore 中的“where”过滤器不支持这些依赖项。因此,我不确定是否应该将客户集合嵌套到每个餐厅文档中,或者是否应该将所有客户放在根集合中然后过滤它们。

您能否帮我解决这个问题,并提供一些关于您认为如何使用 Firestore 规则进行管理的最佳方法的想法?

小智 5

这是根据您所说的建议:

Restaurants
    [id] (document)
        owner: idUser(Owner)
        waiters (map)
            idUser1: timestamp
            idUser2: timestamp
            idUser3: timestamp
        customersIn (collection)
            [idCustomer1] 
                itens: (map)
                    french fries: 10
                    water: 3
            [idCustomer2]
                itens: (map)
                    hot dog: 6
                    soda: 4
Customers
    [id] (document)
        name
        restaurants (map)
            idRestaurant1: timestamp
            idRestaurant2: timestamp
Users
    [id] (document)
        waiterIn (map)
            idRestaurant1: timestamp
            idRestaurant2: timestamp
Run Code Online (Sandbox Code Playgroud)

当您尝试一起过滤和排序时,此方法在https://firebase.google.com/docs/firestore/solutions/arrays中进行了评论...

在这种情况下,您可以进行任何查询,例如餐厅的顾客、一家餐厅的服务员或服务员所在的餐厅。像这样:

customersRef.where("restaurants." + idRestaurant, '>', 0).orderBy("restaurants." + idRestaurant);
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!