AncestryEx.Query (ancestry_ex v0.1.0)

Copy Markdown View Source

Query builders used by the functions that use AncestryEx defines in a schema.

Every function receives a queryable as first argument and returns an Ecto.Query, so results can be further composed before being sent to the repo:

Category.children(category)
|> where(active: true)
|> order_by(:name)
|> Repo.all()

You normally call these through the schema (Category.children(node)), which passes the schema config for you. The functions here take the config explicitly and can be used with any queryable over an ancestry schema.

Summary

Functions

Nodes with depth greater than depth.

Ancestors of node, ordered from the root down to the parent.

Nodes with depth equal to depth.

Nodes with depth lower than depth.

Direct children of node.

Dynamic expression with the depth of the node, using the depth cache column when configured and computing it from the ancestry column otherwise.

All nodes below node.

Nodes with depth greater than or equal to depth.

Descendants of node that are not direct children.

Nodes without children.

Orders by the ancestry column, roots first. Parents are always returned before their children, which is what AncestryEx.arrange/1 needs.

The parent of node (no rows for a root).

Ancestors of node plus the node itself, ordered from the root down.

The root of the tree node belongs to (the node itself for a root).

Nodes sharing the parent of node, excluding node. Roots are siblings of each other.

node and all nodes below it.

Nodes with depth lower than or equal to depth.

Functions

after_depth(queryable, config, depth)

Nodes with depth greater than depth.

ancestors(queryable, config, node)

Ancestors of node, ordered from the root down to the parent.

at_depth(queryable, config, depth)

Nodes with depth equal to depth.

before_depth(queryable, config, depth)

Nodes with depth lower than depth.

children(queryable, config, node)

Direct children of node.

depth_expr(config)

@spec depth_expr(AncestryEx.Config.t()) :: Ecto.Query.dynamic_expr()

Dynamic expression with the depth of the node, using the depth cache column when configured and computing it from the ancestry column otherwise.

descendants(queryable, config, node)

All nodes below node.

from_depth(queryable, config, depth)

Nodes with depth greater than or equal to depth.

indirects(queryable, config, node)

Descendants of node that are not direct children.

leaves(queryable, config)

Nodes without children.

ordered_by_ancestry(queryable, config)

Orders by the ancestry column, roots first. Parents are always returned before their children, which is what AncestryEx.arrange/1 needs.

parent(queryable, config, node)

The parent of node (no rows for a root).

path(queryable, config, node)

Ancestors of node plus the node itself, ordered from the root down.

root(queryable, config, node)

The root of the tree node belongs to (the node itself for a root).

roots(queryable, config)

Root nodes.

siblings(queryable, config, node)

Nodes sharing the parent of node, excluding node. Roots are siblings of each other.

subtree(queryable, config, node)

node and all nodes below it.

to_depth(queryable, config, depth)

Nodes with depth lower than or equal to depth.