Xitdb

CI Hex.pm Elixir Odin Ash Format Benchmarks License: MIT

Xitdb brings xitdb to Elixir—an embedded, single-file database where you store nested maps, lists, sorted maps, and sets directly on disk, and every transaction commits an immutable snapshot in time.

Think SQLite meets Elixir's immutable data structures with Git-like history built in: there is no external database server, no SQL migrations, and no JSON-blob serialization boilerplate. You read and update nested data with standard Elixir Access (db["users"]["alice"]["roles"][0]), Enum, and Stream, or reach for Xitdb's declarative transaction blocks, Pull projections, Datalog queries, and native Ash Framework integration with built-in full-text search.


What is xitdb?


Features


A Tour in Code

1. Read and Update Like Plain Elixir Data

# Open in memory (or use backend: :buffered_file, path: "app.xit" for disk persistence)
{:ok, db} = Xitdb.open(backend: :memory)

# Commit a nested Elixir map as moment 0
{:ok, db} =
  Xitdb.transact(
    db,
    %{
      "users" => %{
        "alice" => %{
          "username" => "alice",
          "name" => "Alice Liddell",
          "age" => 30,
          "roles" => ["admin", "architect"]
        }
      }
    },
    doc: "Seed initial users",
    index_rules: [{:index_on, "users", "username", "username->id"}]
  )

# Navigate lazily with standard Access brackets and Enum
db["users"]["alice"]["name"]      # => "Alice Liddell"
db["users"]["alice"]["roles"][0]  # => "admin"
Enum.to_list(db["users"]["alice"]["roles"])
# => ["admin", "architect"]

# Update nested paths inside a transaction (creates moment 1)
{:ok, db} =
  Xitdb.transact(
    db,
    fn root ->
      update_in(root["users"]["alice"]["age"], &(&1 + 1))
    end,
    doc: "Birthday bump"
  )

2. Time-Travel, Diffs & Field History

# Pin an immutable snapshot of moment 0 without copying data
v0 = Xitdb.as_of(db, 0)

v0["users"]["alice"]["age"] # => 30
db["users"]["alice"]["age"] # => 31

# Diff any two moments
Xitdb.diff_moments(db, 0, 1)
# => {:ok, [%{op: :put, path: ".users.alice.age", old_value: 30, new_value: 31}]}

# Inspect every commit that changed a field
Xitdb.history_of(db, ".users.alice.age")
# => {:ok, [
#   %{moment_index: 0, value: 30, doc: "Seed initial users", instant: 0},
#   %{moment_index: 1, value: 31, doc: "Birthday bump", instant: 0}
# ]}

3. Declarative Transactions, Pull & Datalog

use Xitdb.DSL

id = "alice"

# Atomic transaction block with secondary indexes, list ops & zero-copy fork
{:ok, db} =
  tx db, doc: "promote alice" do
    index_on :users, :username, "username->id"
    put users[id].role, "principal"
    append users[id].roles, "staff"
    fork users[id], backups[id]
    update users[id].age, 0, &(&1 + 1)
  end

# Shape-based Pull projection with bounded list slicing
Xitdb.pull!(db["users"]["alice"], [:name, :role, roles: 0..1])
# => %{"name" => "Alice Liddell", "role" => "principal", "roles" => ["admin", "architect"]}

# Datalog query with pinned runtime variables (^var)
prefix = "al"
min_age = 21

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

    where [
      sorted("username->id")[uname] == uid,
      starts_with(uname, ^prefix),
      users[uid].age == age,
      age >= ^min_age
    ]
  end
defmodule MyApp.Accounts.Profile do
  use Ash.Resource,
    domain: MyApp.Accounts,
    data_layer: Xitdb.Ash.DataLayer

  xitdb do
    store MyApp.Store
    collection :profiles

    index [:status, :inserted_at]

    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

Run ranked BM25 + prefix search and time-travel reads straight through Ash:

require Ash.Query

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!()

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

Installation

Add xitdb to your dependencies in mix.exs:

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

System Requirements

Compiling xitdb builds an embedded Erlang NIF wrapping the Odin database engine:


Guides

Core Concepts

DSLs, Queries & Ash

Under the Hood


Under the Hood & Development

Xitdb embeds odin-xitdb in-process via a C-ABI Erlang NIF (native/nif_entry.c + native/xitdb_nif.odin), giving Elixir applications direct access to xitdb's persistent tries, B+Trees, Datalog engine, and BM25 search without serialization overhead or an external daemon. See Architecture, Internals & Performance for the full technical design and 1v1 Client Benchmarks (benchmarks/README.md) for latency and throughput numbers across 28 workloads.

# Run formatting, --warnings-as-errors compile, and tests
mix precommit

# Run the specification and property test suite with trace
mix test --trace

# Replay ExFuzz regression seeds or run a bounded coverage-guided fuzz campaign
mix test --only fuzz
mix fuzz test/fuzz/parsers_and_codecs_fuzz_test.exs:90 --max-runs 50000