veko doc
Self-Contained API Reference • Gradual Types • Capabilities • Wasm Sandbox

db/query

lib/db/query.vk

lib/db/query.vk Pure-Veko typed table, atomic secondary index, and predicate query layer over db/kv. Stores primary rows under row/<table>/<pk> and secondary index keys under idx/<table>/<field>/<encoded_val>/<pk>, keeping rows and indexes consistent via kv/batch. Requires .fs capability when backed by an open kv/KvStore.

Table struct
signature
Table = struct { name str, pk_field str, indexed_fields block[str] }

Schema descriptor for a secondary-indexed KV table.

signature
define-table = fn [name str, pk_field str, indexed_fields block[str]] Table

Defines a logical Table over a kv/KvStore with primary-key field pk_field and indexed_fields.

Executable Example
veko
use [kv = db/kv, q = db/query]
p = str/join [(os/temp-dir) '/veko_db_query_doctest.vkkv'] ''
_ = os/remove/or p [.ok]
mut db = kv/open-fast p
users = q/define-table 'users' 'id' ['role' 'region']
_ = q/put-row mut db users { id = 'u1', name = 'Ada', role = 'admin', region = 'ca-west' }
_ = q/put-row mut db users { id = 'u2', name = 'Lin', role = 'dev',   region = 'ca-west' }
_ = q/put-row mut db users { id = 'u3', name = 'Kai', role = 'admin', region = 'eu-north' }
admins = q/where-eq db users 'role' 'admin'
_ = kv/close mut db
_ = os/remove/or p [.ok]
if (len admins) != 2 or admins[0].name != 'Ada' or admins[1].name != 'Kai' [ err 'where-eq' ] else [ .ok ]
signature
put-row = fn [mut db kv/KvStore, tbl Table, row map] enum

Inserts or updates row (a map containing tbl.pk_field), atomically updating all secondary indexes.

signature
get-row = fn [db kv/KvStore, tbl Table, pk str] any

Fetches a single row by primary key pk, or returns err if not found (combine with /or).

signature
delete-row = fn [mut db kv/KvStore, tbl Table, pk str] enum

Deletes pk from tbl along with all of its secondary index entries atomically.

signature
scan-all = fn [db kv/KvStore, tbl Table] block

Scans all rows in tbl in deterministic primary-key order.

signature
where-eq = fn [db kv/KvStore, tbl Table, field str, value any] block

Queries all rows where row[field] == value (uses secondary index when field is indexed, or fallback scan).

signature
query-filter = fn [db kv/KvStore, tbl Table, pred any, max_rows i64] block

Filters rows in tbl matching pred (fn [row] bool) with optional /limit max_rows.

signature
count-rows = fn [db kv/KvStore, tbl Table] i64

Counts total live rows in tbl.