Kura is a database layer for Erlang, Ecto-style schemas, changesets, composable queries and migrations in plain Erlang. I posted about 1.x in Kura - Ecto-inspired database layer for Erlang. A lot has changed since then so here is a new thread for 2.x. Current version is 2.31.
Core and backends
In 2.0 kura is split into a core and backends. Core has schemas, changesets, the query builder and the migrator. The backend brings the dialect, driver and pool. Two backends exist today, kura_postgres and kura_sqlite.
{deps, [kura, kura_postgres]}.
[{kura, [
{repos, #{
my_repo => #{
backend => kura_backend_postgres,
host => "localhost",
database => "my_app_dev",
user => "postgres",
password => "postgres",
pool_size => 10
}
}}
]}].
Config is a map of repos, so one app can run several repos, each with its own backend. Kura starts the pools at boot. A backend declares what it supports (window functions, full-text search, row locks, vectors and so on) and kura checks a query against that before it is sent, so you get an error from kura and not a syntax error from the database.
kura_postgres doesn’t use pgo anymore. It runs on minato, a Postgres client I wrote for this. It’s on Hex at 1.2 and works on its own if you just want a client without kura.
Encryption at rest
Wrap a field type in {encrypted, Type} and the value is stored AES-256-GCM encrypted in a BYTEA column. Casting and validation still run on the plaintext.
fields() ->
[
#kura_field{name = id, type = id, primary_key = true},
#kura_field{name = ssn, type = {encrypted, string}, nullable = false}
].
Keys can be rotated, and there is a deterministic mode so equality lookups on an encrypted column still work. Only ciphertext goes into the query, the query cache, telemetry and logs.
pgvector
{vector, N} fields, and kura_query:distance/3 for nearest neighbour search:
D = kura_query:distance(embedding, cosine, QueryVector),
Q = kura_query:from(memory),
Q1 = kura_query:select_expr(Q, [{id, id}, {content, content}, {dist, D}]),
Q2 = kura_query:order_by(Q1, [{dist, asc}]),
Q3 = kura_query:limit(Q2, 10),
{ok, Rows} = kura_repo_worker:all(my_repo, Q3).
Distance also works in where and having, and create_index takes using, opclass and with, so HNSW and IVFFlat indexes come from a normal migration.
Other things since 1.x
Composite primary keys and composite foreign keys, all the way through associations, preload and casting. Window functions via over/2, full-text search with a matches operator, exclusion constraints, read-only repos with replica routing, UUIDv7 primary keys if you want them.
The migrator got fake/1 to baseline an existing database without running the DDL, migrations outside a transaction so CREATE INDEX CONCURRENTLY works, and discovery across several applications.
Hardening
The last couple of months went mostly to this. Every identifier is quoted. Attribute tenancy applies to every query, write and preload, and require_tenant makes a query without a tenant raise instead of reading across tenants. Schemas can list fields in redact/0 and those stay out of logs and telemetry. kura_query:lock/2 takes a lock term instead of raw text.
The new checks are off by default in 2.31 and log one warning per boot when they would apply. In 3.0 they become the default. guides/upgrading.md lists every setting and its default, so you can turn them on one at a time before that.
CI gates new code on mutation score too.
Links
Kura: GitHub - Taure/kura: Ecto-style database layer for Erlang: schemas, changesets, composable queries, migrations and multi-tenancy. PostgreSQL and SQLite backends. · GitHub
HexDocs: kura v2.31.2 — Documentation
kura_postgres: GitHub - Taure/kura_postgres: PostgreSQL backend for kura, the Ecto-style Erlang database layer. Runs on the minato client. · GitHub
kura_sqlite: GitHub - Taure/kura_sqlite: SQLite backend for kura, the Ecto-style Erlang database layer. Runs on the esqlite NIF driver. · GitHub
minato: GitHub - Taure/minato: An independent Erlang Postgres client. Bulk-framed wire protocol, caller-owned sockets, pure codecs. · GitHub
opentelemetry_kura: GitHub - Taure/opentelemetry_kura: OpenTelemetry tracing for kura, the Erlang database layer: a span for every query. · GitHub
rebar3_kura (migrations generated from schema changes): GitHub - Taure/rebar3_kura: rebar3 plugin for kura: generates Erlang database migrations from schema changes and scaffolds new projects. · GitHub