Relatex (relatex v0.2.0)

Copy Markdown View Source

Embed a Relate property graph in an Elixir application.

A supervised process owns the database and its connection. Callers can share the process. Use a directory to retain the graph across application restarts.

iex> {:ok, db} = Relatex.start_link()
iex> {:ok, result} = Relatex.query(db, "RETURN $note AS note", %{"note" => "found it"})
iex> {result.columns, result.rows, result.namespace, result.query_type}
{["note"], [["found it"]], "neo4j", "r"}
iex> Relatex.stop(db)
:ok

Summary

Functions

Returns a specification to start this module under a supervisor.

Runs a single auto-commit statement. Bind parameters by binary key.

Opens a database and starts its linked owner process.

Closes the connection and database and stops the owner process.

Runs an ordered list of {cypher, params} pairs in one transaction.

Types

option()

@type option() ::
  {:database, String.t()}
  | {:statement_timeout, non_neg_integer()}
  | {:max_in_flight, pos_integer() | :infinity}
  | {:name, GenServer.name()}

params()

@type params() :: %{optional(String.t()) => term()}

statement()

@type statement() :: {String.t(), params()}

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

query(db, cypher, params \\ %{})

@spec query(db :: GenServer.server(), cypher :: String.t(), params :: params()) ::
  {:ok, Relatex.Result.t()} | {:error, Relatex.Error.t()}

Runs a single auto-commit statement. Bind parameters by binary key.

Parameters support nil, booleans, signed 64-bit integers, floats, UTF-8 binaries, lists and string-keyed maps. Unknown atoms and overflowing integers return an error. Use Cypher constructors for graph and temporal parameters. An error with stage "decode" can follow a committed write. To require successful result conversion before committing, use transaction/2.

start_link(opts \\ [])

@spec start_link(opts :: [option()]) :: GenServer.on_start()

Opens a database and starts its linked owner process.

Options:

  • :database — a local directory, .rl file or ":memory:" (default). An empty string also opens an in-memory store.
  • :statement_timeout — per-statement engine deadline in milliseconds, default 5,000. Set to zero to clear the deadline. Time spent waiting in the owner's mailbox is outside this deadline.
  • :max_in_flight — maximum number of outstanding queries and transaction batches, including the one running. A positive integer enables admission control. The default is :infinity. A full owner immediately returns a retryable "Relatex.Overloaded" error before accepting the operation.
  • :name — an optional GenServer registration name.

Place {Relatex, database: "./agent-graph", name: MyGraph} in a supervision tree. Its child spec uses :transient restart. After an abnormal exit the replacement owner reopens the durable graph. An in-memory store starts empty for each owner. Engine failures during startup return {:error, %Relatex.Error{}}.

stop(db)

@spec stop(db :: GenServer.server()) :: :ok | {:error, Relatex.Error.t()}

Closes the connection and database and stops the owner process.

transaction(db, statements)

@spec transaction(db :: GenServer.server(), statements :: [statement()]) ::
  {:ok, [Relatex.Result.t()], String.t() | nil} | {:error, Relatex.Error.t()}

Runs an ordered list of {cypher, params} pairs in one transaction.

The owner executes the complete batch before serving another caller. Success returns the statement results and the engine's commit bookmark, which is nil when the transaction committed no writes. A failed statement rolls back the preceding writes and returns its error.