laravel_nestedset/lib.rs
1//! Nested set trees for [Laravel Rust](https://crates.io/crates/laravel-rust) models.
2//!
3//! A nested set stores a tree in one table: each row keeps the `lft` and `rgt` bounds of its
4//! subtree, so a whole subtree or every ancestor of a node is one indexed query.
5//!
6//! ```no_run
7//! use laravel::model::{Column, model};
8//! use laravel_nestedset::NestedSet;
9//!
10//! #[model]
11//! pub struct Category {
12//! pub id: i64,
13//! pub name: String,
14//! #[default]
15//! pub lft: i64,
16//! #[default]
17//! pub rgt: i64,
18//! pub parent_id: Option<i64>,
19//! }
20//!
21//! impl NestedSet for Category {
22//! fn lft_column() -> Column<Self, i64> {
23//! Category::lft
24//! }
25//!
26//! fn rgt_column() -> Column<Self, i64> {
27//! Category::rgt
28//! }
29//!
30//! fn parent_column() -> Column<Self, Option<i64>> {
31//! Category::parent_id
32//! }
33//! }
34//!
35//! # async fn example() -> laravel::Result<()> {
36//! let mut shop = Category::create_root(Category::create().name("Shop")).await?;
37//! let mut phones = shop.create_child(Category::create().name("Phones")).await?;
38//! let android = phones.create_child(Category::create().name("Android")).await?;
39//!
40//! let subtree = shop.descendants().get().await?; // Phones, Android
41//! let breadcrumbs = android.ancestors().get().await?; // Shop, Phones
42//! # Ok(())
43//! # }
44//! ```
45//!
46//! The migration adds the columns with
47//! [`table.nested_set()`](NestedSetBlueprint::nested_set). From there, [`NestedSet`] has:
48//!
49//! - creating: [`create_root`](NestedSet::create_root), [`create_child`](NestedSet::create_child),
50//! [`create_after`](NestedSet::create_after), [`create_tree`](NestedSet::create_tree) from
51//! nested [`NewNode`]s, and more;
52//! - moving a subtree: [`append_to`](NestedSet::append_to),
53//! [`insert_before`](NestedSet::insert_before), [`make_root`](NestedSet::make_root),
54//! [`up`](NestedSet::up), and more;
55//! - deleting a subtree: [`delete_subtree`](NestedSet::delete_subtree);
56//! - queries: [`descendants`](NestedSet::descendants), [`ancestors`](NestedSet::ancestors),
57//! [`siblings`](NestedSet::siblings), [`children`](NestedSet::children), and more;
58//! - conditions for any query: [`where_descendant_of`](NestedSet::where_descendant_of),
59//! [`where_ancestor_of`](NestedSet::where_ancestor_of), and more;
60//! - loading by key: [`ancestors_of`](NestedSet::ancestors_of),
61//! [`subtree_of`](NestedSet::subtree_of), and more;
62//! - drag-and-drop saves: [`arrange`](NestedSet::arrange) from nested [`Arrangement`]s;
63//! - repairs: [`check_tree`](NestedSet::check_tree) and [`fix_tree`](NestedSet::fix_tree), also
64//! from the console through [`NestedSetServiceProvider`].
65//!
66//! [`LoadRelatives`] loads the ancestors or subtrees of a whole list with one query.
67//!
68//! [`IntoTree::into_tree`] nests a query's result into [`TreeNode`]s for JSON and templates, and
69//! [`Scope`] keeps several trees in one table. Every write locks its tree, so concurrent writers
70//! take turns. See the [README](https://github.com/axvilab/laravel-nestedset-rs) for the guide.
71
72mod arrange;
73mod chain;
74mod check;
75mod commands;
76mod error;
77mod event;
78mod locks;
79mod node;
80mod ops;
81mod relatives;
82mod schema;
83mod scope;
84mod tree;
85
86pub use crate::arrange::Arrangement;
87pub use crate::check::TreeReport;
88pub use crate::commands::NestedSetServiceProvider;
89pub use crate::error::NestedSetError;
90pub use crate::event::{TreeChange, TreeChanged};
91pub use crate::node::NestedSet;
92pub use crate::relatives::{LoadRelatives, WithAncestors, WithCount};
93pub use crate::schema::NestedSetBlueprint;
94pub use crate::scope::Scope;
95pub use crate::tree::{IntoTree, NewNode, TreeNode};