I am building an app using Firebase and am having trouble structuring the data for a hierarchy that my app needs.
Concept
My app consists of items
. Each item can have n
-many child items
. There will be 100s of thousands of items in the database and for any given item. I want to get a count of all child items (i.e.: direct children, grand children, great grandchildren, etc).
Current structure example
items: {
1: {
name: 'neat item 1'
},
2: {
name: 'neat item 2',
parentId: 1
},
3: {
name: 'neat item 3',
parentId: 2
}
}
Question
In Firebase, what is the best way to track the number of children an item has? So in the example above, item #1 has a total 2 children, item #2 has a total of 1 child.
Would it be best to maintain a childCount
on each item and whenever a new item is added, increment that number for all parents? Or would it be better to recursively compute the child count whenever it's needed?
Keeping in mind that there will be 500,000+ items in the database, with some items having a total of 10,000+ children.
Thanks so much for your time!