I'm just starting with firebase and I'm wondering if there are any best practices for storing a tree-like data structure in firebase.
My current thoughts are to flatten all the nodes but store a child and sibling key on each node to create the relationships:
/*
node1 ----- node3
|
node2 ----- node4
|
node5
*/
nodes: {
node1: {
child: 'node2',
sibling: 'node3'
...
},
node2: {
child: null,
sibling: 'node4'
...
},
node3: {
child: null,
sibling: null
...
},
node4: {
child: 'node5',
sibling: null
...
},
node5: {
child: null,
sibling: null
...
}
}
Are there any problems with this approach given users are going to be manipulating the same tree (adding nodes, removing, reordering, etc...) in real-time?
I also thought about storing the reverse relationship ie. parent and previous sibling rather then child and next sibling but not sure if that's any better. It would help me find the root node easier (parent == prevSibling == null) but would make traversing more difficult.
Any insights are appreciated.
Thanks,
Jordan