Current section
Files
Jump to
Current section
Files
ecto_ltree
README.md
README.md
# EctoLtreeA library that provides the necessary modules to support the PostgreSQL’s`ltree` data type with Ecto.## Quickstart### 1. Add the package to your list of dependencies in `mix.exs````elixirdef deps do [ ... {:ecto_ltree, "~> 0.1.0"} ]end```### 2. Define a type module with our custom extensions```elixirPostgrex.Types.define( MyApp.PostgresTypes, [EctoLtree.Postgrex.Lquery, EctoLtree.Postgrex.Ltree] ++ Ecto.Adapters.Postgres.extensions())```### 3. Configure the Repo to use the previously defined type module```elixir config :my_app, MyApp.Repo, adapter: Ecto.Adapters.Postgres, username: "postgres", password: "postgres", database: "my_app_dev", hostname: "localhost", poolsize: 10, pool: Ecto.Adapters.SQL.Sandbox, types: MyApp.PostgresTypes```### 4. Add a migration to enable the `ltree` extension```elixirdefmodule MyApp.Repo.Migrations.CreateExtensionLtree do use Ecto.Migration def change do execute("CREATE EXTENSION ltree", "DROP EXTENSION ltree") endend```### 5. Add a migration to create your table```elixirdefmodule MyApp.Repo.Migrations.CreateItems do use Ecto.Migration def change do create table(:items) do add :path, :ltree end create index(:items, [:path], using: :gist) endend```### 6. Define an Ecto Schema```elixirdefmodule MyApp.Item do use Ecto.Schema import Ecto.Changeset alias EctoLtree.LabelTree, as: Ltree schema "items" do field :path, Ltree end def changeset(item, params \\ %{}) do item |> cast(params, [:path]) endend```### 7. Usage```elixiriex(1)> alias MyApp.RepoMyApp.Repoiex(2)> alias MyApp.ItemMyApp.Itemiex(3)> import Ecto.QueryEcto.Queryiex(4)> import EctoLtree.FunctionsEctoLtree.Functionsiex(5)> Item.changeset(%Item{}, %{path: “1.2.3”}) |> Repo.insert!%MyApp.Item{ __meta__: #Ecto.Schema.Metadata<:loaded, “items”>, id: 1, path: %EctoLtree.LabelTree{labels: [“1”, “2”, “3”]}}iex(6)> from(item in Item, select: nlevel(item.path)) |> Repo.one3```The documentation can be found at [hexdocs](https://hexdocs.pm/ecto_ltree).## Copyright and LicenseCopyright (c) 2018 Jose Miguel Rivero BrunoThe source code is licensed under [The MIT License (MIT)](LICENSE.md)