You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Florin 4c1a8e3213
Merge pull request #3 from 50kudos/immediate_child_or_parent
7 months ago
assets add the assets, because I never get the images linked right 9 months ago
config - minor text adjustments 9 months ago
examples Fix examples tests 8 months ago
lib Clean api a bit 7 months ago
test Supprot depth option for memory adapter 7 months ago
.formatter.exs w⦿‿⦿t!! 9 months ago
.gitignore w⦿‿⦿t!! 9 months ago
.iex.exs w⦿‿⦿t!! 9 months ago
.tool-versions w⦿‿⦿t!! 9 months ago
CHANGELOG.md 0.2.1 8 months ago
LICENSE Initial commit 9 months ago
README.md Be able to query immediate parent or child nodes 8 months ago
mix.exs 0.2.1 8 months ago
mix.lock - minor text adjustments 9 months ago

README.md

Hex.pm Hexdocs.pm

Closure Table

while an early preview, this library provides two adapters: an in-memory one, and one for using the closure-table solution with Ecto; for your testing and development convenience.

The Closure Table solution is a simple and elegant way of storing 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.

Throughout the various examples and tests, we will refer to the hierarchies depicted below, where we're modeling a hypothetical forum-like discussion between Rolie, Olie and Polie, and their debate around the usefulness of this implementation :)

Closure Table

Quick start

To start, you can simply use one Adapter from the ones provided, same way you'd use the Ecto's own Repo:

defmodule CTM do
    use CTE,
      otp_app: :ct_empty,
      adapter: CTE.Adapter.Memory,
      nodes: %{
        1 => %{id: 1, author: "Olie", comment: "Is Closure Table better than the Nested Sets?"},
        2 => %{id: 2, author: "Rolie", comment: "It depends. Do you need referential integrity?"},
        3 => %{id: 3, author: "Polie", comment: "Yeah."}
      },
      paths: [[1, 1], [1, 2], [1, 3], [2, 2], [2, 3], [3, 3]]
  end

With the configuration above, the :nodes attribute is a map containing the comments our interlocutors made; these are “nodes”, in CTE's parlance. When using the CTE.Adapter.Ecto implementation, the :nodes attribute will be a Schema (or a table name! 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. The :paths attribute represents the parent-child relationship between the comments.

Add the CTM module to your main supervision tree:

defmodule CTM.Application do
  @moduledoc false

  use Application

  def start(_type, _args) do
    opts = [strategy: :one_for_one, name: CTM.Supervisor]

    Supervisor.start_link([CTM], opts)
  end
end

And then using iex -S mix, for quickly experimenting with the CTE API, let's find the descendants of comment #1:

iex» CTM.descendants(1)
{:ok, [3, 2]}

Please check the docs for more details and return from more updates!

Oh and there is a simple utility for helping you drawing your paths, using graphviz! From Rolie's comments, excerpt:

Maybe useful?! If yes, then we'll let you find this function by yourself ;)

hint: check the tests <3

Installation

If available in Hex, the package can be installed by adding closure_table to your list of dependencies in mix.exs:

def deps do
  [
    {:closure_table, "~> 0.1"}
  ]
end

Contributing

  • Fork this project
  • Create your feature branch (git checkout -b my-new-feature)
  • Setup database (cd examples/ct_ecto && mix test)
  • Test (mix test)
  • Commit your changes (git commit -am 'Add some feature')
  • Push to the branch (git push origin my-new-feature)
  • Create new Pull Request

License

Copyright 2019 Florin T.PATRASCU

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.