DelveroffD
Convex Community17mo ago
9 replies
Delveroff

How to store tree-like data?

I want to implement something like Git, where you have a tree-structure of revisions.
- Every revision has a hash (it's id)
- Every revision has a parent revision (null for the root)
- Every revision may have children revisions (like branching)

Example of such a tree:
A -> B -> C -> D
      \-> C2

I want to be able to fetch a revision as well as it's ancestors up to the root:
await findAncestors('D') // A, B, C, D
await findAncestors('C') // A, B, C
await findAncestors('C2') // A, B, C2

Reads are often. Writes are not, and most of them are new revisions (leafs).

For now, I see several ways of doing this:
type Revision = {
  parentId: DocId<'revisions'> // need recursive query? what about indexing?
}

type Revision = {
  children: Array<DocId<'revisions'>> // what about cascade deletes? also indexing
}

type Revision = {
  path: Array<DocId<'revisions'>> // seems attractive if indexing by the array
}
Was this page helpful?