Skip to main content

Crate laravel_nestedset

Crate laravel_nestedset 

Source
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, Phones

The migration adds the columns with table.nested_set(). From there, NestedSet has:

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 key or id:
NestedSetServiceProvider
Registers the nestedset:check and nestedset:fix commands for the models you list.
NewNode
A node to create with its children, for NestedSet::create_tree and NestedSet::create_child_tree.
Scope
The columns that tell trees in one table apart, such as the menu_id of a menu item.
TreeChanged
Dispatched after a write to a tree commits, so a listener can drop a cached menu or category list:
TreeNode
A node with its children, built by IntoTree::into_tree. It serializes as the model’s own fields plus depth and children, ready for a JSON API or a template:
TreeReport
What NestedSet::check_tree found: the keys of the broken nodes, by kind of damage.
WithAncestors
A node with its ancestors, built by LoadRelatives::with_ancestors. It serializes as the model’s own fields plus ancestors, from the root down.
WithCount
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 plus count.

Enums§

NestedSetError
An operation on a tree that can’t be done.
TreeChange
What a write did to a tree.

Traits§

IntoTree
Builds trees from a list of nodes, such as the result of a query.
LoadRelatives
Loads the relatives of every node in a list with one query, instead of one per node.
NestedSet
A model stored as a nested set: each row keeps the lft and rgt bounds of its subtree and the key of its parent.
NestedSetBlueprint
Adds the nested set columns and their index to a table.