Skip to main content

Module tree_build

Module tree_build 

Source
Expand description

Turn a flat, parent-linked row set into a nested tree.

Four places in this crate had grown their own version of this — auto_menu (page-derived menus), navigation (curated menus), the admin page list, and menu-builder.js in the browser. The shape is always the same: bucket rows by parent_id, then walk down from a root, bounded by a depth.

Both functions here are pure and query-free. That is the whole point: the caller does exactly one bounded fetch, and the nesting costs nothing more. Building a tree by asking the database for each node’s children is the N+1 that this module exists to prevent — and on cms_page it would be an unindexed lookup per node, since only path, status, url_path and page_type_id are indexed.

Functions§

build
Materialize the nodes under parent, at most depth levels deep.
index_by_parent
Bucket rows by their parent id.