Xitdb
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?
- Embedded & nested, not flat tables or opaque blobs: Runs in
:memoryor a single.xitfile like SQLite, while natively storing arbitrarily nested maps, lists, sorted B+Tree maps, and sets directly on disk. - Git-like history built into every write: Updating a value never overwrites past commits. Every transaction appends a new immutable moment that shares unchanged subtrees with earlier moments—giving you time-travel, diffs, and field audit history out of the box.
- Lazy cursors, not whole-file loads: Accessing
db["users"]["alice"]returns a lightweight%Xitdb.Cursor{}handle instead of loading the entire database into memory. Only the keys or slices you actually read are fetched, and cursors pass freely across BEAM processes,Tasks, and LiveViews.
Features
- Feels Like Native Elixir (
Access,Enum&Stream): Navigate nested maps and lists with bracket lookups,get_in/2,put_in/3,update_in/3, andpop_in/2, or pipe collections straight intoEnum.count/1,Enum.member?/2,Enum.slice/2, and lazyStreampipelines. - Built-In Time-Travel, Diffs & Audit History: Pin any past commit with
Xitdb.as_of/2, compare any two commits withXitdb.diff_moments/3, trace when and why any field changed withXitdb.history_of/2, or branch new transactions from an older moment. - Instant Zero-Copy Cloning (
fork): Duplicate an entire user, tenant, or document tree inside a transaction without copying its underlying data; subsequent edits diverge cleanly via copy-on-write. - Rich Persistent Collections & Automatic Indexes: Plain Elixir maps, lists, and
MapSets work out of the box, with opt-inXitdb.counted_map/1,Xitdb.sorted_map/1,Xitdb.linked_list/1, andXitdb.sorted_set/1, plus automatic secondary indexes (index_on) that stay in sync across updates and deletes. - Expressive Elixir DSLs (
tx,pull, & Datalog): Stage deep multi-path writes withtx db do ... end, fetch shaped entity slices in one step withXitdb.pull/2, and run joins, aggregations, recursive graphreach, and cross-momentat_momentqueries withquery db do ... end. - Crash-Safe Single-File Storage & Compaction: Append-only checksummed commits require no separate WAL sidecar, recover cleanly after crashes, and reclaim historical space online via
Xitdb.compact/2. - Drop-In Ash Framework DataLayer & Full-Text Search: Persist
Ash.Resourcedomains in.xitfiles (data_layer: Xitdb.Ash.DataLayer) with relationships, secondary indexes, ranked BM25 + prefix search (Xitdb.Ash.Search), foreign-key cascades, time-travel (Xitdb.Ash.Store.with_as_of/3), andasync: trueXitdb.Ash.TestSandboxisolation—with zero SQL migrations.
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
4. Ash Framework & Built-In Full-Text Search
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:
- Odin Compiler:
odinmust be available in yourPATH(see Odin Getting Started). - C Compiler: A standard C compiler (
ccorgcc) for the thin NIF entry shim.
Guides
Core Concepts
- Reading & Updating with Access & Enum — Bracket navigation,
get_in/put_in/update_in/pop_in,Xitdb.at/1, passing cursors across processes, andEnum/Streampipelines. - Collections & Secondary Indexes — Choosing between maps (
hash_map,counted_map,sorted_map), lists (array_list,linked_list), sets, custom tagged scalars, andindex_onsecondary indexes. - Time-Travel, Diffs & Storage — Snapshots (
as_of/2), moment diffs (diff_moments/3), field lineage (history_of/2), branching history, zero-copyfork, backends, and compaction.
DSLs, Queries & Ash
- Transaction DSL — Declarative
txandtx_forblocks, automatic path creation, list edits,fork, metadata, savepoints, and rollbacks. - Path Selectors & Pull Projections — Shape-based
Xitdb.pull/2projections,pull_pattern/1,path/1,~x, and automatic key escaping. - Datalog & Graph Queries —
queryanddatalogblocks,^varpinning, multi-collection joins, aggregations, recursivereach, and cross-momentat_momentqueries. - Ash Framework & Full-Text Search —
Xitdb.Ash.DataLayer,Xitdb.Ash.Store,Xitdb.Ash.Search(BM25 + prefix search),nested_insubtrees,tx_for, andasync: trueXitdb.Ash.TestSandbox.
Under the Hood
- Architecture, Internals & Performance — Storage format, 9-byte
Slotcursors, WAL-free crash safety, NIF boundary cost model, and single-writer supervision.
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