Complex data model for Firestore

I'm building my first app with Firestore + angularfire and it's being quite difficult to define the data model due to my lack of experience with Firestore. I need your help.

I have the following entities and dependencies.

  1. Restaurants: they are independent, and can be registered by the users of the app.
  2. The user who creates the restaurant, becomes the "owner". He/she can invite other users to be "waiters/waitress" of the restaurant.
  3. The users can receive a "be my waiter/waitress" invitation from one or multiple restaurants (maybe from different owners).
  4. We have another entity of "Customers". Entries of this entity have data about the dishes and drinks ordered, the price, the tips, etc...

And then the following rules

  1. Each owner can see any "Customer" data within his/her restaurants, but not in other restaurants.
  2. Each "waiter/waitress" can see the data of the customers he/she served (in whatever restaurant) but not other customers.

So as you can see there are multiple dependencies, including several OR conditions, which are not suported by "where" filters in Firestore. Therefore I am not sure if I should nest a customers collection into each restaurant document or if I should have all customers in a root collection and then filter them.

Could you please help me with this and provide some ideas about how you think is the best way to manage it with the Firestore rules??

Upvotes: 5

Views: 1880

Answers (1)

André Lima
André Lima

Reputation: 315

This is a sugestion based on what you said:

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

This aproach is commented in https://firebase.google.com/docs/firestore/solutions/arrays, when you try to filter and sort toghether...

Is this case you can make any queries like the customers in restaurant, the waiters of one restaurant or the restaurants of the waiter. Like this:

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

Hope this helps!

Upvotes: 5

Related Questions