{"id":21714918,"url":"https://github.com/stephanerob/ancestry-ecto","last_synced_at":"2025-10-04T14:21:49.657Z","repository":{"id":86678533,"uuid":"97202784","full_name":"StephaneRob/ancestry-ecto","owner":"StephaneRob","description":"Organise Elixir Ecto model into a tree structure","archived":false,"fork":false,"pushed_at":"2020-07-16T06:16:17.000Z","size":46,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-25T17:33:21.377Z","etag":null,"topics":["ancestry","ecto","elixir","phoenix","tree"],"latest_commit_sha":null,"homepage":"","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/StephaneRob.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-07-14T06:51:18.000Z","updated_at":"2020-08-07T11:02:38.000Z","dependencies_parsed_at":null,"dependency_job_id":"8a552b60-f122-4257-b626-e7f43eadec82","html_url":"https://github.com/StephaneRob/ancestry-ecto","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StephaneRob%2Fancestry-ecto","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StephaneRob%2Fancestry-ecto/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StephaneRob%2Fancestry-ecto/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/StephaneRob%2Fancestry-ecto/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/StephaneRob","download_url":"https://codeload.github.com/StephaneRob/ancestry-ecto/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244676454,"owners_count":20491828,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ancestry","ecto","elixir","phoenix","tree"],"created_at":"2024-11-26T00:39:27.461Z","updated_at":"2025-10-04T14:21:44.613Z","avatar_url":"https://github.com/StephaneRob.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AncestryEcto\n\n![](https://github.com/StephaneRob/ancestry-ecto/workflows/tests/badge.svg)\n\n## Installation\n\nIf [available in Hex](https://hex.pm/docs/publish), the package can be installed\nby adding `ancestry` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [{:ancestry_ecto, \"~\u003e 0.1.0\"}]\nend\n```\n\nCreate a migration to add an `ancestry` column. Can be configured with `column` option.\n\n```bash\nmix ecto.gen.migration add_ancestry_to_pages\n```\n\nAdd index to migration\n\n```elixir\ndefmodule Migration do\n  use Ecto.Migration\n\n  def change do\n    alter table(:pages) do\n      add :ancestry, :string\n    end\n\n    create index(:pages, [:ancestry])\n  end\nend\n```\n\n```bash\nmix ecto.migrate\n```\n\nStart using ancestry in your schema:\n\n```elixir\ndefmodule MyModel do\n  use Ecto.Schema\n  use AncestryEcto\n\n  import Ecto.Changeset\n\n  schema \"my_models\" do\n    field(:ancestry, :string)\n  end\nend\n\n```\n\n### Usage\n\n- repo : running ecto repo (optional, **default: `YourApp.Repo`**)\n- column : column where the tree will be persisted (optional, **default `:ancestry`**)\n- attribute : column used to reference recors (optional, **default `{:id, :integer}`**)\n- schema : (optional, **default current module**)\n- orphan_strategy : Instruct Ancestry what to do with children of an element that is destroyed\n  - `:destroy`: All children are destroyed as well\n  - `:rootify`: The children of the destroyed node become root nodes (**default**)\n  - `:restrict`: An AncestryException is raised if any children exist\n  - `:adopt`: The orphan subtree is added to the parent of the deleted node, If the deleted node is Root, then rootify the orphan subtree.\n\nFor the following setup\n\n```elixir\n# Implicit options\n# repo: MyApp.Repo\n# column: :ancestry\n# attribute: {:id, :integer}\n# schema: Myapp.Page\n# orphan_strategy: :rootify\n\ndefmodule MyApp.Page do\n  use Ecto.Schema\n  use AncestryEcto\n\n  schema \"pages\" do\n    field(:ancestry)\n    field(:title)\n\n    timestamps()\n  end\nend\n```\n\n#### Available functions\n\n| method             | return value                                                               | usage example                               |\n| ------------------ | -------------------------------------------------------------------------- | ------------------------------------------- |\n| `roots/0`          | all root node                                                              | `MyApp.Page.roots`                          |\n| `is_root?/1`       | true if the record is a root node, false otherwise                         | `MyApp.Page.is_root?(record)`               |\n| `parent/1`         | parent of the record, nil for a root node                                  | `MyApp.Page.parent(record)`                 |\n| `parent_id/1`      | parent id of the record, nil for a root node                               | `MyApp.Page.parent_id(record)`              |\n| `has_parent?/1`    | true if the record has a parent, false otherwise                           | `MyApp.Page.has_parent?(record)`            |\n| `ancestors/1`      | ancestors of the record, starting with the root and ending with the parent | `MyApp.Page.ancestors(record)`              |\n| `ancestor_ids/1`   | ancestor ids of the record                                                 | `MyApp.Page.ancestor_ids(record)`           |\n| `children/1`       | direct children of the record                                              | `MyApp.Page.children(record)`               |\n| `child_ids/1`      | direct children's ids                                                      | `MyApp.Page.child_ids(record)`              |\n| `siblings/1`       | siblings of the record, the record itself is included\\*                    | `MyApp.Page.siblings(record)`               |\n| `sibling_ids/1`    | sibling ids                                                                | `MyApp.Page.sibling_ids(record)`            |\n| `has_siblings?/1`  | true if the record's parent has more than one child                        | `MyApp.Page.has_siblings?(record)`          |\n| `descendants/1`    | direct and indirect children of the record                                 | `MyApp.Page.descendants(record)`            |\n| `descendant_ids/1` | direct and indirect children's ids of the record                           | `MyApp.Page.descendant_ids(record)`         |\n| `subtree/1`        | the model on descendants and itself                                        | `MyApp.Page.subtree(record)`                |\n| `subtree/2`        | the arranged model on descendants and itself                               | `MyApp.Page.subtree(record, arrange: true)` |\n| `subtree_ids/1`    | a list of all ids in the record's subtree                                  | `MyApp.Page.subtree_ids(record)`            |\n\n##### Subtree w/ arrangement\n\n```elixir\n%{\n  %AncestryEcto.Page{\n    ancestry: nil,\n    id: 319,\n  } =\u003e %{\n    %AncestryEcto.Page{\n      ancestry: \"319\",\n      id: 320,\n    } =\u003e %{\n      %AncestryEcto.Page{\n        ancestry: \"319/320\",\n          \"a9b305f0-34e5-4940-9e7d-5b9fc755bae7/9566563f-1281-48d2-8b1a-cdb98ba1f25d\",\n        id: 321,\n      } =\u003e %{\n        %AncestryEcto.Page{\n          ancestry: \"319/320/321\",\n          id: 322,\n        } =\u003e %{}\n      }\n    },\n    %AncestryEcto.Page{\n      ancestry: \"319\",\n      id: 324,\n    } =\u003e %{}\n  }\n}\n\n```\n\n### Changeset usage\n\n`cast_ancestry/2` allows to cast automatically `\"parent_ATTRIBUTE\"` param into ancestry.\n\n```elixir\ndefmodule MyApp.Page do\n  use Ecto.Schema\n  use AncestryEcto\n\n  schema \"pages\" do\n    field(:ancestry)\n    field(:title)\n\n    timestamps()\n  end\n\n  def changeset(page, attrs) do\n    page\n    |\u003e cast(attrs, [:title])\n    |\u003e cast_ancestry(attrs)\n  end\nend\n```\n\nex:\n\n```elixir\n# If page id 10 has ancestry \"5\"\niex\u003e Page.changeset(%{}, %{\"title\" =\u003e \"Hello world!\", \"parent_id\" =\u003e 10})\n%Ecto.Changeset{\n  action: nil,\n  changes: %{title: \"Hello world!\", ancestry: \"5/10\"},\n  ...\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstephanerob%2Fancestry-ecto","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstephanerob%2Fancestry-ecto","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstephanerob%2Fancestry-ecto/lists"}