Packages

Embedded Relate graph databases for Elixir/OTP applications

Current section

Files

Jump to
relatex docs api elixir spec.md
Raw

docs/api/elixir/spec.md

# Relatex API

## Store lifecycle

`Relatex.start_link(opts \\ [])` starts a linked GenServer with one database and one connection in the default session namespace. It accepts these options.

| Option | Meaning |
| --- | --- |
| `:database` | UTF-8 store URI, default `":memory:"`. A directory or a local `.rl` file persists data. |
| `:statement_timeout` | Per-statement engine budget in milliseconds, default 5000. Zero clears the deadline. Time waiting for another caller is outside the budget. |
| `:name` | Optional GenServer registration name, accepted anywhere a server reference can be passed. |

The owner supports `{Relatex, opts}` as an OTP child. A failed engine open returns `{:error, %Relatex.Error{}}`. `Relatex.stop(db)` synchronously disconnects, closes the database and returns `:ok` on success. A normal stop ends a transient child. After an abnormal owner exit, a supervisor restarting the child with the same store URI waits for in-flight native work and can reopen the store. A competing live owner still receives the engine’s lock error. Reopening the same persistent store reads its committed data. A native library runs with the operating-system privileges and crash domain of its BEAM node.

## Statements and errors

`Relatex.query(db, cypher, params \\ %{})` returns `{:ok, %Relatex.Result{}}` or `{:error, %Relatex.Error{}}`. Parameters are string-keyed maps. Bind values with Cypher placeholders such as `$session`. Accepted inputs are nil, booleans, signed int64, finite floats, the special float tuples below, UTF-8 binaries, proper lists, and nested binary-keyed maps. Parameter keys may contain up to 4096 bytes and must be free of NUL. Strings retain embedded NUL. Containers have a 64-level nesting limit. For temporal, spatial and vector parameters, bind their components and construct the value in Cypher.

`columns` holds the ordered column names. Each list in `rows` follows that order. `namespace` is the resolved graph name and `query_type` is the engine classification (`"r"`, `"w"`, `"rw"`, or `"s"`). Result values belong to the BEAM after the query returns. This API materializes the complete result. Use Cypher projection, aggregation or pagination when retrieving many rows.

| Cypher value | BEAM representation |
| --- | --- |
| Null, boolean, integer, string, list, map | `nil`, boolean, integer, UTF-8 binary, list, map with binary keys |
| Float | Elixir float or `{:float, :nan \| :infinity \| :negative_infinity}` |
| Node | `{:node, %{id: ..., element_id: ..., labels: [...], properties: %{...}}}` |
| Relationship | `{:relationship, fields}` with `:id`, `:element_id`, `:type`, `:properties`, `:start_id`, `:end_id`, `:start_element_id`, `:end_element_id` |
| Path | `{:path, %{elements: [...]}}` in traversal order |
| Date, time, datetime | A tagged tuple with `:date`, `:time`, `:local_time`, `:datetime`, or `:local_datetime` and a component map. Date fields are year, month and day. Time fields are hour, minute, second and nanosecond. Zoned variants also include offset_seconds and zone. |
| Duration | `{:duration, %{months: ..., days: ..., seconds: ..., nanoseconds: ...}}` |
| Point | `{:point, %{srid: ..., coordinates: [...]}}` |
| Vector | `{:vector, %{dtype: ..., coordinates: [...]}}` |

Engine errors expose `code`, `gql_status`, `stage`, `retryable`, and `message`. Both statement failures and C ABI call failures use this shape. Binding errors use code `"Relatex.BindingError"`, stage `"binding"`, nil GQL status and `retryable: false`. If the engine succeeds and an Elixir result cannot be decoded, the code is `"Relatex.DecodeError"`, stage `"decode"`, and `retryable` is false. An auto-commit write can already be durable. A batch rolls back on this error. A statement exceeding its native timeout returns an engine error. The connection can serve subsequent queries.

## Transactions

`Relatex.transaction(db, [{cypher, params}, ...])` runs the ordered batch on one connection. Concurrent callers wait until completion. Success returns `{:ok, [result, ...], bookmark_or_nil}`. A write bookmark is an engine-issued string. A transaction that commits no writes can return nil. A failed statement rolls back the batch and returns its error. A failed commit returns its original engine error. The owner rolls back a pending transaction or confirms that the connection can start and roll back a fresh transaction. Once recovery succeeds, callers can use the same owner again. If recovery fails, the owner stops and closes the connection.

## Build compatibility

The compiler selects a checksum-pinned Relate v1.5.0 SDK and verifies C ABI version 2 when the NIF loads. Normal builds require Elixir, OTP headers, make and a C compiler. Release downloads also use curl, tar and sha256sum or shasum. Set `RELATEX_FFI_DIR` for an offline or upstream-checkout build with compatible native artifacts. CI and development toolchain versions live in the repository's `.tool-versions`. [Upstream C contract](https://github.com/relatedb/relate/blob/v1.5.0/docs/api/c/spec.md)