Dagex provides tools to create directed, acyclic graphs for your business entities. It relies on the PostgreSQL ltree extension and Ecto.
See the module documentation for Dagex for usage examples.
If available in Hex, the package can be installed
by adding dagex to your list of dependencies in mix.exs:
def deps do
[
{:dagex, "~> 3.0"}
]
enddefmodule MyApp.Repo do
use Ecto.Repo, otp_app: :my_app, adapter: Ecto.Adapters.Postgres
use Dagex.Repo
endRun mix ecto.gen.migration add_dagex_to_project then edit the resulting
migration file:
defmodule MyApp.Repo.Migrations.AddDagexToProject do
use Ecto.Migration
def up do
Dagex.Migrations.up()
end
def down do
Dagex.Migrations.down()
end
endand then migrate your database with mix ecto.migrate.
ecto_ltree is already included as a dependency with the above step, and activating the ltree extension happened in the Dagex migration above.
However there are additional steps that ecto_ltree requires:
-
Define a type module with the Postgrex.Ltree custom extensions e.g. in a new filed called
lib/postgrex_types.ex -
Configure the Repo to use the previously defined type module
Run mix ecto.gen.migration add_organizations then edit the resulting migration
file:
defmodule MyApp.Repo.Migrations.AddOrganizations do
require Dagex.Migrations
use Ecto.Migration
def change do
create table("organizations") do
add :name, :string, null: false
timestamps()
end
# Adds triggers to the "organizations" table to maintain the associated DAG as
# records are added/removed.
Dagex.Migrations.setup_node_type("organizations", "3.0.0")
end
endN.B. You can use any column type that Postgresql can convert to text as the
entity's primary key, however, if your primary key field is a string, you MUST
NOT allow the value "*" as this is reserved internally for the supremum of the
graph. You will see an Ecto.ConstraintError on the dagex_reserved_supremum_id
constraint if you attempt to insert such a record.
defmodule MyApp.Organization do
use Dagex
use Ecto.Schema
schema "organizations" do
field :name, :string
end
endSee the module documentation for Dagex for information on the functionality
that is added to your module when calling use Dagex.