Hi everyone, I'm looking for some advice on structuring my Firestore database. Use case is that I have a series of pages, which I want to be able to list by tag (a page has only one tag). A page can have child pages, so for a specific tag I could have the following:
Page 1
Page 1a
Page 1b
Page 2
Page 2a
Page 2a.1
Page 2a.2
Page 2b
Page 3
Page 4 etc.
The 'pages' collection is straightforward; each document has the following: id, title, content, parentId, dateCreated and tag (all are strings apart from dateCreated which is a timestamp).
I also have a 'tags' collection; each document has id & name (both strings).
My question is how best to store the structure above. At various points I will need to view the pages for a particular tag; I will only need to show the title and date created for each one (and will need to know the id), but will also have to show the structure.
I'm thinking of adding a 'tree' field to the topic document, which would be something like this for the structure above:
tree: [
0: {
id: page1ID,
name: 'Page 1',
dateCreated: 21st July 2022,
tree: [
0: {
id: page1aID,
name: 'Page 1a',
dateCreated: 21st July 2022,
tree: []
},
1: {
id: page1bID,
name: 'Page 1b',
dateCreated: 21st July 2022,
tree: []
},
]
},
1: {
id: page2ID,
name: 'Page 2',
dateCreated: 21st July 2022,
tree: [
0: {
id: page2aID,
name: 'Page 2a',
dateCreated: 21st July 2022,
tree: [
0: {
id: page2a1ID,
name: 'Page 2a1',
dateCreated: 21st July 2022,
tree: []
},
1: {
id: page2b1ID,
name: 'Page 2b1',
dateCreated: 21st July 2022,
tree: []
}
]
},
1: {
id: page2bID,
name: 'Page 2b',
dateCreated: 21st July 2022,
tree: []
},
]
}
... etc ...
]
However, this is difficult (not impossible) to add pages to, and to reorder etc. Also I'm not sure that Firestore is a big fan of saving arrays - it seems to convert them to objects first - which suggests that using arrays is not a recommended approach.
Is there a standard way of handling this kind of data? Thanks!