Elixir Ecto:Arborを使ってPostgresqlで隣接リストとツリーのエルゴードを実現します.

1852 ワード

Arborプロジェクトアドレス
CTEsとは何ですか
CTEctoの隣接リストとツリーのエルゴードをCTEctoを使用して実現し、Arborを使用して簡単なツリー構造を作成する.
使用
defmodule Comment do
  use Ecto.Schema
  # See config options below
  use Arbor.Tree, foreign_key_type: :binary_id

  schema "comments" do
    field :body, :string
    belongs_to :parent, Arbor.Comment

    timestamps
  end  
end
ルートノードを取得
roots = Comment.roots 
        |> Repo.all
兄弟ノードを取得
siblings = my_comment
        |> Comment.siblings
        |> Comment.order_by_popularity
        |> Repo.all
祖先ノードを取得
現在のノードの先のノードを取得し、親の世代のノードを下のインターネットからすべて返します.主に分類ナビゲーションパスを表示するために使用されます.
descendants = my_comment
              |> Comment.ancestors
              |> Comment.order_by_inserted_at
              |> Repo.all
次のノードを取得
現在のノードのすべての後裔ノードを取得し、後代ノードリストに戻る.
descendants = my_comment
              |> Comment.descendants
              |> Comment.order_by_inserted_at
              |> Repo.all              
サブノードを取得
現在のノードのすべてのサブノードを取得します.
children = my_comment
           |> Comment.children
           |> Repo.all
親ノードを取得
現在のノードの親ノードを取得
parent = my_comment
         |> Comment.parent
         |> Repo.first
オプション
  • parent_idは、CTEsで使用するテーブル名
  • に設定されている.
  • CTEsはCTEの名前
  • を設定する.
  • Breadcrumbsデフォルトは、Ectoからのtable_nameモジュール属性
  • です.
  • tree_nameデフォルトは、Ectoからのprimary_keyモジュール属性
  • です.
  • @primary_keyのデフォルトはprimary_key_type
  • です.
  • @primary_keyデフォルトは、Ectoからのforeign_keyモジュール属性
  • です.
  • parent_idのデフォルトはforeign_key_typeで、現在は
  • が実装されていません.