I have a Firebase Database. I have Job objects and Item objects. Each Job can contain many Items and one Item must be contained by a single job (Simple One to Many). My present database structure is that both Item and Job are top level lists in firebase and each Item contains the jobKey to which it belongs. This allows me to find all items in a job as long as I know the job key. The firebase code to find the items requires a query which includes and an "orderBy" on the jobKey in the items. So it is easy to retrieve all items for a job. However, I also want to sort and filter the items based in additional data in the items. But because of the firebase restriction of a single "orderBy" I cannot accomplish this second level of filtering using firebase. This is my challenge. The structure of my present data is illustrated below.
+--jobs
|
+-- jobKey1
| |
| +-- <jobdata1> ..
|
+-- jobKey2
|
+-- <jobdata2>..
+--items
|
+-- itemKey1
| |
| +-- jobKey : jobKey2 // this item belongs to job2
| |
| +-- <the rest of item1 data
|
+-- itemKey2
| |
| +-- jobKey : jobKey2 // this item belongs to job2
| |
| +-- <the rest of item2 data
|
+-- itemKey3
| |
| +-- jobKey : jobKey1 // this item belongs to job1
| |
| +-- <the rest of item3 data
|
+-- itemKey4
|
+-- jobKey : jobKey1 // this item belongs to job1
|
+-- <the rest of item4 data
As mentioned earlier, I want to be able to retrieve all items for a job and then order and filter items by various field in the item. Given the structure above, the only way to do this (that I see) is use the firebase query to retrieve the items, then use logic in the component (I'm using angular2) to cache all the items into some sort of collection then sort and filter based on the cached data. This isn't very satisfying, there must be a better way. What are the reasonable alternatives?