PhoenixKit.Utils.TreeQuery (phoenix_kit v2.41.0)

Copy Markdown View Source

Recursive queries over a self-referencing table — a schema whose rows point at a parent row of the same schema (parent_uuid by default). Each call is one recursive CTE: subtrees for cascades and moves, ancestors for breadcrumbs and cycle checks.

Cycle-safe: the CTEs use UNION, not UNION ALL, so Postgres drops rows it has already seen and a corrupt parent cycle ends the recursion instead of looping.

Uuids come back as strings, in no particular order. An argument that is not a uuid names no row ([]), rather than raising.

TreeQuery.subtree_uuids(Category, [category.uuid])
TreeQuery.ancestor_uuids(Space, space.uuid, parent: :parent_space_uuid)

Options

Summary

Functions

Every row above uuid, up to the top — not uuid itself.

Every row below uuid, not uuid itself. [] for a leaf.

roots and every row below them — the union of their subtrees, each uuid once. [] for no roots.

Functions

ancestor_uuids(schema, uuid, opts \\ [])

@spec ancestor_uuids(module(), String.t(), keyword()) :: [String.t()]

Every row above uuid, up to the top — not uuid itself.

descendant_uuids(schema, uuid, opts \\ [])

@spec descendant_uuids(module(), String.t(), keyword()) :: [String.t()]

Every row below uuid, not uuid itself. [] for a leaf.

subtree_uuids(schema, roots, opts \\ [])

@spec subtree_uuids(module(), [String.t()], keyword()) :: [String.t()]

roots and every row below them — the union of their subtrees, each uuid once. [] for no roots.