Giter Club home page Giter Club logo

ecto_ltree's Introduction

EctoLtree

Hex Version

A 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

If you are using Elixir >= v1.7 and Ecto ~> 3.2

def deps do
  [
    ...
    {:ecto_ltree, "~> 0.3.0"}
  ]
end

If you are using Ecto ~> 3.0

def deps do
  [
    ...
    {:ecto_ltree, "~> 0.2.0"}
  ]
end

If you are using Elixir v1.6 and Ecto ~> 2.1

def deps do
  [
    ...
    {:ecto_ltree, "~> 0.1.0"}
  ]
end

2. Define a type module with our custom extensions

Postgrex.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

  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

defmodule MyApp.Repo.Migrations.CreateExtensionLtree do
  use Ecto.Migration

  def change do
    execute("CREATE EXTENSION ltree",
            "DROP EXTENSION ltree")
  end
end

5. Add a migration to create your table

defmodule 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)
  end
end

6. Define an Ecto Schema

defmodule 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])
  end
end

7. Usage

iex(1)> alias MyApp.Repo
MyApp.Repo
iex(2)> alias MyApp.Item
MyApp.Item
iex(3)> import Ecto.Query
Ecto.Query
iex(4)> import EctoLtree.Functions
EctoLtree.Functions
iex(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.one
3

The documentation can be found at hexdocs.

Copyright and License

Copyright (c) 2018-2019 Jose Miguel Rivero Bruno

The source code is licensed under The MIT License (MIT)

ecto_ltree's People

Contributors

bismark avatar josemrb avatar warmwaffles avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ecto_ltree's Issues

Evaluating this library

Hello,
I'm evaluating using this library instead of rolling my own.

Is there a reason why this is on version 0.20.1? Is appears development has stopped, and I'm just wondering what You think is missing to get this to version 1.0.0.

Need documentation

Hello,

I didn't find a way to query ltree with the library, so for now I use fragments:

from u in "users",
          where: fragment(" path ~ ?", ^value)

do you provide any function to do that ?

Best regards

Array support

I've appreciated this library a ton.

Seems however that it's missing or now needs to implement array support for lquery and possibly ltree in order to support operations that take arrays as arguments: elixir-ecto/postgrex#528.

I'd be willlilng to help, but I'm totally unfamiliar with how Postgres communicates with Ecto. Is there a model for encoding/decoding arrays of this type I could use to create a PR, if one would be welcome?

update ecto dep

Hi there,

when I try to install the lib I get the following error:

Dependencies have diverged:
* ecto (Hex package)
  the dependency ecto 3.4.2

  > In deps/ecto_ltree/mix.exs:
    {:ecto, "~> 3.0", [env: :prod, hex: "ecto", repo: "hexpm", optional: false]}

  does not match the requirement specified

  > In deps/ecto_sql/mix.exs:
    {:ecto, "~> 3.4.3", [env: :prod, hex: "ecto", repo: "hexpm", optional: false]}

  Ensure they match or specify one of the above in your deps and set "override: true"
** (Mix) Can't continue due to errors on dependencies

adding override: true fixes the issue. maybe it should be added to the description.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.