View Source CTE (Closure Table v2.0.5)

The Closure Table for Elixir strategy, CTE for short, is a simple and elegant way of storing and working with hierarchies. It involves storing all paths through a tree, not just those with a direct parent-child relationship. You may want to chose this model, over the Nested Sets model, should you need referential integrity and to assign nodes to multiple trees.

With CTE you can navigate through hierarchies using a simple API, such as: finding the ascendants and descendants of a node, inserting and deleting nodes, moving entire sub-trees or print them as a digraph (.dot) file.

Options available to most of the functions:

  • :limit, to limit the total number of nodes returned, when finding the ancestors or the descendants for nodes
  • :itself, accepting a boolean value. When true, the node used for finding its neighbors are returned as part of the results. Default: true
  • :nodes, accepting a boolean value. When true, the results are containing additional information about the nodes. Default: false

Quick example.

In this example the: :nodes attribute, will be a Schema i.e. Post, TreePath, etc! In our initial implementation, the nodes definitions must have at least the :id, as one of their properties. This caveat will be lifted in a later implementation.

......

todo: Update the docs

Please check the docs, the tests, and the examples folder, for more details.

Summary

Types

@type config() :: Keyword.t()
@type name() :: String.t() | atom()
@type nodes() :: map() | table()
@type paths() :: [list()] | table()
@type repo() :: Ecto.Repo | map()
@type t() :: %CTE{
  adapter: term(),
  name: name() | nil,
  nodes: nodes() | nil,
  options: map() | nil,
  paths: paths() | nil,
  repo: repo() | nil
}
@type table() :: String.t() | atom()