dlex v0.1.0 Dlex

Dgraph driver for Elixir.

This module handles the connection to Dgraph, providing pooling (via DBConnection), queries, mutations and transactions.

Link to this section Summary

Functions

Supervisor callback

Runs a mutation with delete target and returns the result or raises Dlex.Error if there was an error. See delete/3

Send mutation to dgraph

Runs a mutation and returns the result or raises Dlex.Error if there was an error. See mutate/3

Send mutation to dgraph

Runs a query and returns the result or raises Dlex.Error if there was an error. See query/3

Query schema of dgraph

Start dgraph connection process

Execute serie of queries and mutations in a transactions

Link to this section Types

Link to this section Functions

Link to this function alter!(conn, statement, opts \\ [])

Alter dgraph schema

Link to this function alter(conn, statement, opts \\ [])
alter(conn(), iodata() | map(), Keyword.t()) ::
  {:ok, map()} | {:error, Dlex.Error.t() | term()}
alter(conn(), iodata() | map(), Keyword.t()) :: map()

Alter dgraph schema

Example

iex> Dlex.alter(conn, "name: string @index(term) .")
{:ok, ""}
Link to this function child_spec(opts)
child_spec(Keyword.t()) :: Supervisor.Spec.spec()

Supervisor callback.

For available options, see: start_link/1.

Link to this function delete!(conn, statement, opts \\ [])
delete!(conn(), iodata(), Keyword.t()) :: map()

Runs a mutation with delete target and returns the result or raises Dlex.Error if there was an error. See delete/3.

Link to this function delete(conn, statement, opts \\ [])
delete(conn(), iodata(), Keyword.t()) ::
  {:ok, map()} | {:error, Dlex.Error.t() | term()}

Send mutation to dgraph

Options:

  • return_json - if json with uids should be returned (default: false)

Example of usage

iex> mutation = "
     _:foo <name> "Foo" .
     _:foo <owns> _:bar .
      _:bar <name> "Bar" .
     "
iex> Dlex.delete(conn, mutation)
{:ok, %{"bar" => "0xfe04c", "foo" => "0xfe04b"}}

Using json

iex> json = %{"name" => "Foo",
              "owns" => [%{"name" => "Bar"}]}
     Dlex.delete(conn, json)
{:ok, %{"blank-0" => "0xfe04d", "blank-1" => "0xfe04e"}}

iex> Dlex.delete(conn, json, return_json: true)
{:ok,
 %{
   "name" => "Foo",
   "owns" => [%{"name" => "Bar", "uid" => "0xfe050"}],
   "uid" => "0xfe04f"
 }}
Link to this function mutate!(conn, statement, opts \\ [])
mutate!(conn(), iodata(), Keyword.t()) :: map()

Runs a mutation and returns the result or raises Dlex.Error if there was an error. See mutate/3.

Link to this function mutate(conn, statement, opts \\ [])
mutate(conn(), iodata(), Keyword.t()) ::
  {:ok, map()} | {:error, Dlex.Error.t() | term()}

Send mutation to dgraph

Options:

  • return_json - if json with uids should be returned (default: false)

Example of usage

iex> mutation = "
     _:foo <name> "Foo" .
     _:foo <owns> _:bar .
      _:bar <name> "Bar" .
     "
iex> Dlex.mutate(conn, mutation)
{:ok, %{"bar" => "0xfe04c", "foo" => "0xfe04b"}}

Using json

iex> json = %{"name" => "Foo",
              "owns" => [%{"name" => "Bar"}]}
     Dlex.mutate(conn, json)
{:ok, %{"blank-0" => "0xfe04d", "blank-1" => "0xfe04e"}}
iex> Dlex.mutate(conn, json, return_json: true)
{:ok,
 %{
   "name" => "Foo",
   "owns" => [%{"name" => "Bar", "uid" => "0xfe050"}],
   "uid" => "0xfe04f"
 }}
Link to this function query!(conn, statement, parameters \\ %{}, opts \\ [])
query!(conn(), iodata(), map(), Keyword.t()) :: map()

Runs a query and returns the result or raises Dlex.Error if there was an error. See query/3.

Link to this function query(conn, statement, parameters \\ %{}, opts \\ [])
query(conn(), iodata(), map(), Keyword.t()) ::
  {:ok, map()} | {:error, Dlex.Error.t() | term()}

Send query to dgraph

Example of usage

iex> query = "
     query foo($a: string) {
        foo(func: eq(name, $a)) {
          uid
          expand(_all_)
        }
      }
     "
iex> Dlex.query(conn, query, %{"$a" => "Foo"})
{:ok, %{"foo" => [%{"name" => "Foo", "uid" => "0xfe04d"}]}}
Link to this function query_schema(conn)
query_schema(conn()) :: {:ok, map()} | {:error, Dlex.Error.t() | term()}

Query schema of dgraph

Link to this function start_link(opts \\ [])
start_link(Keyword.t()) :: {:ok, pid()} | {:error, Dlex.Error.t() | term()}

Start dgraph connection process.

Options

  • :hostname - Server hostname (default: DGRAPH_HOST, than localhost)
  • :port - Server port (default: DGRAPH_PORT env var, then 3306)
  • :keepalive - Keepalive option for http client (default: :infinity)

SSL/TLS configuration (automaticly enabled, if required files provided)

  • :cacertfile - Path to your CA certificate. Should be provided for SSL authentication
  • :certfile - Path to client certificate. Should be additionally provided for TSL authentication
  • :keyfile - Path to client key. Should be additionally provided for TSL authentication

DBConnection options

  • :backoff_min - The minimum backoff interval (default: 1_000)
  • :backoff_max - The maximum backoff interval (default: 30_000)
  • :backoff_type - The backoff strategy, :stop for no backoff and to stop, :exp for exponential, :rand for random and :rand_exp for random exponential (default: :rand_exp)
  • :name - A name to register the started process (see the :name option in GenServer.start_link/3)
  • :pool - Chooses the pool to be started
  • :pool_size - Chooses the size of the pool
  • :queue_target in microseconds, defaults to 50
  • :queue_interval in microseconds, defaults to 1000

Example of usage

iex> {:ok, conn} = Dlex.start_link()
{:ok, #PID<0.216.0>}
Link to this function transaction(conn, fun, opts \\ [])
transaction(conn(), (DBConnection.t() -> result :: any()), Keyword.t()) ::
  {:ok, result :: any()} | {:error, any()}

Execute serie of queries and mutations in a transactions