Reputation: 2265
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.
And then the following rules
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
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