Xitdb — Idiomatic Elixir Client for xitdb

Hex.pm License: MIT

Xitdb is an idiomatic Elixir client and Odin NIF wrapper for xitdb (backed by odin-xitdb), an embeddable, immutable, append-only database with persistent structural sharing.

Rather than re-implementing xitdb's wire format in pure Elixir or requiring query boilerplate for every read, Xitdb exposes database nodes as Clojure/Datomic-style lazy %Xitdb.Cursor{} values that implement Elixir's standard Access, Enumerable, and Inspect protocols.


Key Features


Installation

Add xitdb to your list of dependencies in mix.exs:

def deps do
  [
    {:xitdb, "~> 1.1"}
  ]
end

System Requirements

Building xitdb compiles a native C-ABI Erlang NIF bridge to the embedded Odin database engine:


Quick Start

import Xitdb.Sigils

# 1. Open an in-memory or file-backed database
{:ok, db} = Xitdb.open(backend: :memory)

# 2. Commit a moment using standard Elixir maps, lists, and typed collection wrappers
{:ok, db} =
  Xitdb.transact(
    db,
    %{
      "users" => %{
        "alice" => %{
          "username" => "alice",
          "name" => "Alice Liddell",
          "age" => 30,
          "roles" => ["admin", "architect"],
          "flags" => Xitdb.counted_set(["verified", "staff"])
        }
      },
      "audit_log" => Xitdb.linked_list(["init", "seed"])
    },
    doc: "Seed initial users",
    index_rules: [{:index_on, "users", "username", "username->id"}]
  )

# 3. Navigate lazily with standard Elixir Access brackets & Enumerable
root = Xitdb.db(db)
alice = root["users"]["alice"]       # => #Xitdb.Cursor<hash_map offset: ..., preview: [...]>
alice["name"]                        # => "Alice Liddell"
alice["roles"][1]                    # => "architect"
Enum.member?(alice["flags"], "staff") # => true (O(1) HAMT lookup, no list conversion!)

# 4. Mutate nested paths with put_in/3, update_in/3, and pop_in/2 inside Xitdb.transact/2
snapshot_v0 = Xitdb.as_of(db, 0)

{:ok, db} =
  Xitdb.transact(db, fn r ->
    r
    |> update_in(["users", "alice", "age"], &(&1 + 1))
    |> Xitdb.insert_at(["audit_log"], 1, "promoted")
  end, doc: "Increment Alice age")

# 5. Historical snapshots remain immutable
snapshot_v0["users"]["alice"]["age"] # => 30
db["users"]["alice"]["age"]          # => 31

# 6. Datomic Pull projections & Datalog queries (supports both strings and structured DSLs)
Xitdb.pull!(alice, [:name, :age, roles: 0..1])
# => %{"name" => "Alice Liddell", "age" => 31, "roles" => ["admin"]}

Xitdb.query!(db, ~q"""
find ?id, ?age
where
  $username->id[?uname] = ?id
  starts_with(?uname, "al")
  .users[?id].age = ?age
""")
# => %{vars: ["?id", "?age"], rows: [["alice", 31]]}

Macro DSLs (use Xitdb.DSL)

For operations where Elixir's Access protocol has friction—such as auto-vivifying deep paths without intermediate NIF reads, native list append/insert/slice, O(1) subtree fork, typed collection selectors (#counted, $sorted, [[linked]]), and Datalog queries with pinned runtime variables (^var)—Xitdb provides a compile-time macro DSL:

use Xitdb.DSL

# 1. Atomic transaction block (0 intermediate NIF reads, native collection ops)
{:ok, db} =
  tx db, doc: "promote alice" do
    put users[id].role, "admin"
    append users[id].roles, "staff"
    insert audit_log[[1]], "promoted"
    fork users[id], backups[id]
    update users[id].age, &((&1 || 0) + 1)
  end

# 2. Path selector macro with runtime interpolation & collection tags
role = Xitdb.select!(db, path(users[id].roles[0]))

# 3. Declarative Datalog query macro with ^var pinning
prefix = "al"
min_age = 21

{:ok, %{rows: [["alice", 32]]}} =
  query db do
    find {id, age}

    where [
      sorted("username->id")[uname] == id,
      starts_with(uname, ^prefix),
      users[id].age == age,
      age >= ^min_age
    ]
  end

Ash Framework Integration (Xitdb.Ash.DataLayer & Xitdb.Ash.Resource)

Xitdb supports two complementary integration modes with Ash:

1. Native Lock-Free Ash Storage & BM25 Search (data_layer: Xitdb.Ash.DataLayer)

Use Xitdb.Ash.DataLayer with a supervised Xitdb.Ash.Store for full Ash 3.37.2+ CRUD, atomic updates, relationships, aggregates, calculations, composite B+Tree secondary indexes, same-store FK cascades, time-travel queries, and transactional Odin NIF BM25 + prefix search—with zero SQL migrations:

defmodule MyApp.Accounts.Profile do
  use Ash.Resource,
    domain: MyApp.Accounts,
    data_layer: Xitdb.Ash.DataLayer

  xitdb do
    store MyApp.Store
    collection :profiles

    # Single-field and composite B+Tree (:sorted_map) secondary indexes
    index [:status, :inserted_at]

    # Transactional in-Xitdb BM25 + B+Tree prefix search
    search_field :full_name, boost: 3.0
    search_field :headline, boost: 1.5

    references do
      reference :organization, on_delete: :nilify
    end
  end

  attributes do
    integer_primary_key :id
    attribute :full_name, :string, allow_nil?: false, public?: true
    attribute :headline, :string, public?: true
    attribute :status, :atom, default: :active, public?: true
    timestamps(public?: true)
  end

  relationships do
    belongs_to :organization, MyApp.Accounts.Organization,
      attribute_type: :integer,
      attribute_writable?: true,
      public?: true
  end

  actions do
    defaults [:read, :destroy, create: :*, update: :*]
  end
end

Query BM25 search rankings, pin historical time-travel snapshots, and isolate async: true tests via Xitdb.Ash.TestSandbox:

# 1. Ranked BM25 + prefix search (`query/3` for `{id, score}` hits, or `ids/3` + `sort_by_rank/2` on Ash.Query):
require Ash.Query

hits = Xitdb.Ash.Search.query(MyApp.Accounts.Profile, "alice principal", limit: 25)
ranked_ids = Xitdb.Ash.Search.ids(MyApp.Accounts.Profile, "alice principal", limit: 25)

profiles =
  MyApp.Accounts.Profile
  |> Ash.Query.filter(id in ^ranked_ids)
  |> Xitdb.Ash.Search.sort_by_rank(ranked_ids)
  |> Ash.read!()

# 2. Time-travel any Ash read, aggregate, or BM25 search to an earlier moment:
Xitdb.Ash.Store.with_as_of(MyApp.Store, moment_v0, fn ->
  Ash.read!(MyApp.Accounts.Profile)
end)

2. Hybrid MDM Sync Extension (extensions: [Xitdb.Ash.Resource])

When pairing Xitdb with an external primary store (such as AshPostgres.DataLayer) as an immutable Master Data Management engine or secondary audit graph, add Xitdb.Ash.Resource to extensions: to configure collections, survivorship/lock fields, nested sub-collections (nested_in), and compile-time verified sync projections (VerifySyncProjection):

defmodule MyApp.Accounts.Organization do
  use Ash.Resource,
    domain: MyApp.Accounts,
    data_layer: AshPostgres.DataLayer,
    extensions: [Xitdb.Ash.Resource]

  xitdb do
    store MyApp.MDM.Store
    collection :organizations
    merge_redirect_collection :org_merged_into
    locked_ids_set :locked_org_ids

    editable_fields [:name, :domain]
    passthru_fields [:creatio_id, :parent_organization_id]
    computed_xitdb_fields [:normalized_name]
    field_aliases parent_organization_id: :parent_org_id

    indexes(
      domain: "domain->org_id",
      normalized_name: "norm_name->org_id"
    )
  end

  attributes do
    uuid_primary_key :id
    attribute :name, :string, allow_nil?: false, public?: true
    attribute :domain, :string, public?: true
    attribute :creatio_id, :string, public?: true
    attribute :parent_organization_id, :uuid, public?: true
  end
end

Use tx_for / tx_for! to automatically prepend the resource's configured index_on rules at compile time, and Xitdb.Ash.Info for O(1) precompiled Pull projections:

use Xitdb.DSL
alias MyApp.Accounts.Organization

db =
  tx_for! db, Organization, doc: "upsert organization 101" do
    put organizations["101"].id, "101"
    put organizations["101"].name, "Northleaf Capital"
    put organizations["101"].domain, "northleafcapital.com"
    put organizations["101"].normalized_name, "northleaf capital"
  end

db["domain->org_id"]["northleafcapital.com"] # => "101"

pattern = Xitdb.Ash.Info.pull_pattern_for(Organization)
Xitdb.pull!(db["organizations"]["101"], pattern)

Documentation & Guides

Core Concepts & Protocols

Macro DSLs & Query Languages


Architecture

┌────────────────────────────────────────────────────────────────────────────┐
│ Elixir Client & Ash DataLayer (lib/xitdb*.ex)                              │
│  • %Xitdb{} + %Xitdb.Cursor{db_ref, tag, tag_id, value, full, moment, root,│
│    tx_mode, path_steps, staged_ops, staged_overrides, deleted_paths}       │
│  • Access, Enumerable, Inspect protocols                                   │
│  • Xitdb.DSL + Xitdb.Pull + Xitdb.Sigils (~x, ~q)                          │
│  • Xitdb.Ash.{DataLayer, Store, Search, TestSandbox, Resource, Info, Tx}   │
├────────────────────────────────────────────────────────────────────────────┤
│ C-ABI Erlang NIF Boundary (native/nif_entry.c + native/xitdb_nif.odin)     │
│  • ErlNifResourceType + ErlNifRWLock thread safety                         │
│  • Coarse-grained transaction batching, B+Tree seek & BM25 search pushdown │
├────────────────────────────────────────────────────────────────────────────┤
│ Native Engine Submodule (native/xitdb-odin/xitdb)                          │
│  • 16-way HAMT, Radix Trie, Counted B-Tree, B+Tree, Compactor & Datalog    │
└────────────────────────────────────────────────────────────────────────────┘

Development, Testing & Benchmarks

# 1. Run precommit checks (formatting, --warnings-as-errors, and 4-layer ATDD specs)
mix precommit

# 2. Run the 4-layer Client ATDD executable specification & property suite
mix test --trace

# 3. Run the 1v1 Client Statistical Benchmark Suite (Elixir Wrapper vs Raw Odin)
./benchmarks/compare_1v1.py --runs 5 --warmup 1
./benchmarks/update_readme.sh

See benchmarks/README.md for full statistical latency, throughput, and NIF overhead tables, and usage-rules.md for LLM/coding agent integration rules.