Expand description
Nested set trees for Laravel Rust models.
A nested set stores a tree in one table: each row keeps the lft and rgt bounds of its
subtree, so a whole subtree or every ancestor of a node is one indexed query.
use laravel::model::{Column, model};
use laravel_nestedset::NestedSet;
#[model]
pub struct Category {
pub id: i64,
pub name: String,
#[default]
pub lft: i64,
#[default]
pub rgt: i64,
pub parent_id: Option<i64>,
}
impl NestedSet for Category {
fn lft_column() -> Column<Self, i64> {
Category::lft
}
fn rgt_column() -> Column<Self, i64> {
Category::rgt
}
fn parent_column() -> Column<Self, Option<i64>> {
Category::parent_id
}
}
let mut shop = Category::create_root(Category::create().name("Shop")).await?;
let mut phones = shop.create_child(Category::create().name("Phones")).await?;
let android = phones.create_child(Category::create().name("Android")).await?;
let subtree = shop.descendants().get().await?; // Phones, Android
let breadcrumbs = android.ancestors().get().await?; // Shop, PhonesThe migration adds the columns with
table.nested_set(). From there, NestedSet has:
- creating:
create_root,create_child,create_after,create_treefrom nestedNewNodes, and more; - moving a subtree:
append_to,insert_before,make_root,up, and more; - deleting a subtree:
delete_subtree; - queries:
descendants,ancestors,siblings,children, and more; - conditions for any query:
where_descendant_of,where_ancestor_of, and more; - loading by key:
ancestors_of,subtree_of, and more; - drag-and-drop saves:
arrangefrom nestedArrangements; - repairs:
check_treeandfix_tree, also from the console throughNestedSetServiceProvider.
LoadRelatives loads the ancestors or subtrees of a whole list with one query.
IntoTree::into_tree nests a query’s result into TreeNodes for JSON and templates, and
Scope keeps several trees in one table. Every write locks its tree, so concurrent writers
take turns. See the README for the guide.
Structs§
- Arrangement
- A node’s place in a new arrangement: its key and its children, in order. It deserializes from
the JSON a drag-and-drop tree editor sends, with the key as
keyorid: - Nested
SetService Provider - Registers the
nestedset:checkandnestedset:fixcommands for the models you list. - NewNode
- A node to create with its children, for
NestedSet::create_treeandNestedSet::create_child_tree. - Scope
- The columns that tell trees in one table apart, such as the
menu_idof a menu item. - Tree
Changed - Dispatched after a write to a tree commits, so a listener can drop a cached menu or category list:
- Tree
Node - A node with its children, built by
IntoTree::into_tree. It serializes as the model’s own fields plusdepthandchildren, ready for a JSON API or a template: - Tree
Report - What
NestedSet::check_treefound: the keys of the broken nodes, by kind of damage. - With
Ancestors - A node with its ancestors, built by
LoadRelatives::with_ancestors. It serializes as the model’s own fields plusancestors, from the root down. - With
Count - A node with a count of the related rows in its subtree, built by
LoadRelatives::with_subtree_counts. It serializes as the model’s own fields pluscount.
Enums§
- Nested
SetError - An operation on a tree that can’t be done.
- Tree
Change - What a write did to a tree.
Traits§
- Into
Tree - Builds trees from a list of nodes, such as the result of a query.
- Load
Relatives - Loads the relatives of every node in a list with one query, instead of one per node.
- Nested
Set - A model stored as a nested set: each row keeps the
lftandrgtbounds of its subtree and the key of its parent. - Nested
SetBlueprint - Adds the nested set columns and their index to a table.