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?
Firestore does have limitations on querying
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.
Denormalizing - In simple words, its having flat data structure
Good read - https://angularfirebase.com/lessons/firestore-nosql-data-modeling-by-example/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With