Changelog - 2026-08-30
Tree utilities and RecursiveTreeSql
New Feature SecurityIn one line. TreeWalker and TreeBuilder handle in-memory trees, and RecursiveTreeSql.walk() builds a WITH RECURSIVE query that always has a depth bound and validated identifiers.
What changed
TreeWalker(in@venizia/ignis-helpers). Static methods over a tree you already have:walk,walkAsync,height,heightWhere,count,collectLeaves.TreeBuilder(in@venizia/ignis-helpers). Static methods that build a tree or read one back:build,leaves,nonLeaves,print.RecursiveTreeSql.walk()(in@venizia/ignis-kernel, also reachable from@venizia/ignis). Walks a parent/child table up (ancestors) or down (descendants) from a root row, on Postgres or SQLite. The dialect comes fromtable, so there is noengineoption.maxDepthis required. A call without it does not compile, andmaxDepth <= 0throws at runtime.- Safe identifiers.
name,idColumn,parentColumnand everycolumnsentry must be letters, digits and underscores (not starting with a digit), or the call throws.rootId,maxDepthandstartDepthare bound parameters.
typescript
import { TreeBuilder, TreeWalker } from '@venizia/ignis-helpers';
import { RecursiveTreeDirections, RecursiveTreeSql } from '@venizia/ignis';
const tree = await TreeBuilder.build({
rootValue: rootCategory,
getChildren: category => fetchChildCategories(category.id),
getKey: category => category.id,
});
TreeWalker.walk({ root: tree, onVisit: (node, depth) => console.log(node.value.name, depth) });
const walk = RecursiveTreeSql.walk({
name: 'category_ancestors',
table: categoriesTable,
rootId: categoryId,
direction: RecursiveTreeDirections.UP,
maxDepth: 20,
});
const rows = await db.execute(sql`${walk} SELECT * FROM category_ancestors`);Other RecursiveTreeSql.walk options: idColumn (default 'id'), parentColumn (default 'parent_id'), columns, recursiveFilter, trackPath (default false), startDepth (default 0).
Who is affected
- New code walking a hierarchy. Use these instead of hand-written traversal or
WITH RECURSIVEqueries. - Everyone else. No action needed - these are new exports.
Details
TreeWalker.walkandwalkAsyncare separate methods. WhenshouldPrune(node, depth)returnstrue, the node is still visited and only its children are skipped.TreeBuilder.buildskips a branch whengetKeyrepeats; it does not throw.leaves({ includePath: true })returns the root-to-leaf chain with each leaf.maxDepthcounts edges, not rows. The root is at depth 0, somaxDepth: Nreturns up to N+1 rows.tableis typedunknownand checked at runtime; a table that is neither Postgres nor SQLite (for example MySQL) throws.maxDepthalone guarantees the walk ends.trackPath: trueaddspathandis_cyclecolumns and stops expanding a row that re-visits an id in its own path.- The
pathandis_cyclecolumns come back in a different shape per engine:
| Column | Postgres | SQLite |
|---|---|---|
path | string[] | string, each id wrapped in char(31) |
is_cycle | boolean | number (1/0) |
- On SQLite, read
is_cycletruthily and splitpathonString.fromCharCode(31).row.is_cycle === trueis alwaysfalsethere. - On SQLite, an id value that contains the
char(31)byte can cause a missed cycle match.RecursiveTreeSqldoes not validate id values for this.