Firestore 的复杂数据模型

Complex data model for Firestore

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

我有以下实体和依赖项

  1. 餐厅:它们是独立的,可以由应用程序的用户注册。
  2. 创建餐厅的用户成为 "owner"。 He/she可以邀请其他用户成为餐厅的"waiters/waitress"。
  3. 用户可以收到来自一家或多家餐厅(可能来自不同所有者)的"be my waiter/waitress"邀请。
  4. 我们有 "Customers" 的另一个实体。该实体的条目包含有关订购的菜肴和饮料、价格、小费等的数据...

然后是下面的规则

  1. 每个所有者都可以看到 his/her 家餐厅内的任何 "Customer" 数据,但不能看到其他餐厅内的任何数据。
  2. 每个 "waiter/waitress" 都可以看到服务过的顾客 he/she 的数据(在任何餐厅),但不能看到其他顾客的数据。

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

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

这是根据您所说的建议:

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

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

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

customersRef.where("restaurants." + idRestaurant, '>', 0).orderBy("restaurants." + idRestaurant);

希望对您有所帮助!