Litestone guide / Advanced
Trees
A model related to itself, such as a task with subtasks, is walked with recursive: the whole subtree, the path to the root, or a nested tree, in one read.
model Task {
id Int
title String
parentId Int?
parent Task? (fields: [parentId], references: [id])
children Task[]
}
await db.task.createMany({ data: [
{ id: 5, title: 'root' }, { id: 6, title: 'a', parentId: 5 }, { id: 7, title: 'b', parentId: 6 },
] })
const below = await db.task.findMany({ where: { id: 5 }, recursive: true })
const above = await db.task.findMany({ where: { id: 7 }, recursive: { direction: 'ancestors' } })
if (below.length !== 2 || below.find(t => t.id === 7)._depth !== 2) throw new Error('expected two descendants')
if (above.map(t => t.id).sort().join() !== '5,6') throw new Error('expected the path to the root')
Every row carries _depth, its distance from the anchor, and the anchor itself is never in its own result.
What else it can do
direction: 'descendants'is the default,'ancestors'walks upnested: truereturns each node with achildrenarray, andmaxDepthbounds the walkvianames which self-relation to follow when a model has twoorderBy: { _depth: 'desc' }sorts a tree read by distance- The gate, row policies and soft delete apply at every level, so a hidden node takes its branch with it
select,include,@computedand@frombehave as on any read- Over Junction's API the same option is
$recursive - Only
findManywalks a tree;count,aggregateand the rest refuserecursiveby name - A write that would make a row its own ancestor is refused, naming the field
In depth: Relations § Recursive tree queries, § A tree read is an ordinary read, § Cycles