An elixir database wrapper for dgraph database.
Works with dgraph v0.8.1 (most current release as of 16 AUG 2017)
def deps do
[{:dgraph_ex, "~> 0.1.5"}]
end
defmodule Person do
use DgraphEx.Vertex
vertex :person do
field :name, :string, index: [:exact, :term]
field :address, :string, index: [:exact, :term]
end
end
defmodule Land do
use Vertex
vertex :land do
field :address, :string, index: [:exact, :term], count: true
field :zipcode, :string, index: [:exact], count: true
field :city, :string, index: [:exact], count: true
field :land_size, :int, index: true
field :living_space, :int, index: true
field :zoning, :string, index: [:exact], count: true
field :floors, :int
field :construction_year, :string
field :geo_center, :geo, index: [:geo]
field :geo_border, :geo, index: [:geo]
field :owner, :uid, model: Person, reverse: true
end
end
import DgraphEx
alias DgraphEx.Repo
{:ok, _} = Repo.request mutation(schema: Land)
{:ok, _} = Repo.request mutation(schema: Person)
alias DgraphEx.Changeset
def changeset(%Person{} = model, changes) when is_map(changes) do
model
|> Changeset.cast(changes, _allowed_fields = [:name, :address])
|> Changeset.validate_required(_required_fields = [:name, :address])
|> Changeset.validate_type(_typed_fields = [:name, :address])
|> Changeset.uncast
end
We apply the changeset
function to ensure data consistency:
{:ok, person} =
%Person{}
|> Person.changeset(%{name: "jason", address: "221B Baker St. London, England 55555"})
And person
is:
%Person{
_uid_: nil,
address: "221B Baker St. London, England 55555",
name: "jason",
}
Then we insert into the Repo
:
{:ok, person} = Repo.insert(person)
The resulting person
is:
(notice the _uid_
is populated)
%Person{
_uid_: "0x11c",
address: "221B Baker St. London, England 55555",
name: "jason",
}
Using the person
from above:
{:ok, person} = Person.changeset(person, %{name: "John Lakeman"})
person = Repo.update(person)
Repo.get(Company, "0x11c")
returns nil
or a populated %Company{}
With function syntax:
import DgraphEx
query()
|> func(:spy, eq(:name, "John Lakeman"))
|> select({
:_uid_,
:address,
:name,
})
|> Repo.request
Again with keyword (kwargs) syntax:
import DgraphEx
alias DgraphEx.Repo
query([
get: :spy,
func: eq(:name, "John Lakeman"),
select: {
:_uid_,
:address,
:name,
},
]) |> Repo.request
Both of the above requests for "spy" result in the same response:
{:ok, %{
"spy" => [
%{
"_uid_" => "0x11c",
"address" => "221B Baker St. London, England 55555",
"name" => "John Lakeman",
}
]
}}