Current section
Files
Jump to
Current section
Files
docs/api/elixir/description.md
# Embedding Relate in Elixir
An application can keep a graph next to the process that consumes it. For example, an agent host can store a tool invocation, its arguments and its observation as connected records. A later session can query the original evidence by session ID. The Elixir process owns the database lifecycle and the engine runs in its BEAM node.
Start an owner for `":memory:"` to get an isolated graph per database, or pass a directory to retain the graph across restarts. A supervisor can start the owner with `{Relatex, database: path, name: MyApp.Graph}`. Other processes send statements to this name. A batch transaction lets related writes commit together.
Many agents can share a store through this owner. For a bursty workload, configure `max_in_flight: 16` to admit up to sixteen outstanding operations, including the executing query or batch. The owner can reject excess work while the connection is busy. The host chooses whether to shed work, wait and retry, or route an independent workspace to a different database. Each connection executes one operation at a time. Multiple connections can serve the same database concurrently.
For readers that need a stable point in time, open a second owner with `read_only: true` on the writer’s directory. Long reports then run on their own connection and carry a fixed freshness cost. Use the writer’s database owner or a live session from `Relatex.connect/2` when the next statement should see newly committed data. Inspect `Relatex.admission_stats/1` when deciding how much work to admit, and the result’s `execute_ms` and `plan` when investigating the query itself.
## What belongs in each layer
| Application need | Owner and rationale |
| --- | --- |
| Open the store, enforce locks and read-only access, commit atomically and recover durable data | Relate supplies these guarantees. Relatex owns the native handles and maps the released C operations to one supervised connection per owner. |
| Bind typed values, get statement diagnostics, checkpoint, or adjust session defaults | Relatex exposes the engine operations and returns BEAM-owned values and structured errors. An ordered batch or a caller-scoped transaction keeps exclusive use of one connection through commit or rollback. |
| Choose a workspace key, share owners, rotate snapshot readers, persist agent evidence or implement an inbox | Relations can combine these primitives with Registry, DynamicSupervisor and application-specific schemas. The application chooses freshness, retry and acknowledgement policies. |
| Stream unbounded rows, follow a writer continuously, wait for a bookmark, or interrupt a running query | Relate v1.5.0's embedded C API exposes materialized results, read-only snapshots, commit bookmarks and statement timeouts. A native pull cursor, follower refresh, bookmark-wait or cancellation operation would need engine support. |
One database owns the default connection and the lifetime of all its sessions. Open a session when a worker needs independent namespace, timeout or admission settings, or needs to overlap work with another worker on the same live graph. The application chooses how many sessions to run. When the database closes, it closes those sessions as well. Use a separate read-only database owner to freeze a directory snapshot. The pinned embedded contract describes the available native operations. [Relate v1.5.0 C API](https://github.com/relatedb/relate/blob/v1.5.0/docs/api/c/spec.md)
Use `mix run docs/api/elixir/examples/observations.exs` for a deterministic example. The repository also includes a live Lemieux example in which two separate agent sessions produce and retrieve tool evidence. [API contract](spec.md) · [Runnable example](https://github.com/houllette/relatex/blob/main/docs/api/elixir/examples/observations.exs) · [Live agent example](https://github.com/houllette/relatex/blob/main/examples/lemieux_memory.exs)