Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering and sorting with Firestore

I'm building a task management app in firestore. Tasks can have multiple members and tags. Since I would always sort and display content (based on due-date, priority, etc.) of user(s) and due to the limitations of firestore with lists and composite indexes, I have ended up storing data in the following structure.

projects:

.....   
    101: {
        name: 'task1', 
        members: {201: true, 202: true, ......}, 
        tags:{'tag1':true, 'tag2':true, 'tag3':true,....} 
      },

    102: {
        name: 'task2', 
        members: {201: true, 202: true, ......}, 
        tags:{'tag1':true, 'tag2':true, 'tag3':true,....}
      },

    103: {
        name: 'task3', 
        members: {201: true, 202: true, ......}, 
        tags:{'tag1':true, 'tag2':true, 'tag3':true,....} 
      }

.....

Now, since composite indexes have to be manual, I ended up implementing reverse lookup:

users:
.....

201: {
        name: 'John',
        tasks: 
                501: {taskId: 601, priority: high, ...... },
                502: {taskId: 601, priority: high, ...... },
                503: {taskId: 601, priority: high, ...... },            
     }

202: {
        name: 'Doe',
        tasks: 
                504: {taskId: 601, priority: high, ...... },
                505: {taskId: 601, priority: high, ...... },
                506: {taskId: 601, priority: high, ...... },            
     }

......

At this point, if you have to filter tags too, Under users, I will have to add subcollection for each tag and store tasks under them too. This will create insane amount of documents for each task. For example, if you have one task with 3 members and 3 tags, this setup will create 12 documents for just one task. And any changes I make will involve 12 writes.

What am I missing here? Is it the way I'm storing the data? or is it more to do with the lack of capabilities of firestore itself?

like image 936
ranjjose Avatar asked Jul 27 '18 12:07

ranjjose


1 Answers

  1. Firestore does have limitations on querying

  2. What I see is that you are trying to normalize the data, that is a good approach if you are using RDBMS. In Firestore usually it is recommended to denormalize the data as much possible. But again its a trade-off, your read will be fast & easy to query but write might be slow since you might have to write data at multiple places.

  3. Denormalizing - In simple words, its having flat data structure

Good read - https://angularfirebase.com/lessons/firestore-nosql-data-modeling-by-example/

like image 89
TheWizardJS Avatar answered Oct 12 '22 03:10

TheWizardJS