Part II · Files, Codecs, Web & Zero-FFI SQLite
Chapter 6
Zero-FFI SQLite 3, TLS PostgreSQL & RESP3 Redis
Crash-safe 4096-byte SQLite 3 schemas, B-tree indexes, homoiconic JOINs, TLS PgPool ($1 prepared queries), and RESP3 RedisPool
Recipe 6.1: Crash-Safe Schemas, B-Tree Indexes & Homoiconic JOIN Queries (lib/db/sqlite.vk)
Veko's db/sqlite reads and writes genuine 4096-byte "SQLite format 3\0" B-tree files—complete with a CRC-32-verified 'VKJRNL01' rollback journal, incremental dirty-page os/pwrite commits, multi-page 0x02/0x0a B-tree indexes, and relational INNER/LEFT JOIN—in 100% pure Veko with zero libsqlite3 dependencies:
use [sq = db/sqlite]
Packages = sq/define-table 'packages' (quote [
id i64 .pk
name str .unique
owner_id i64 .index
downloads i64 = 0
])
Authors = sq/define-table 'authors' (quote [
id i64 .pk
handle str .unique
])
run-registry-store = fn [db_path str] block [
mut db = sq/open db_path
_ = sq/migrate mut db Authors
_ = sq/migrate mut db Packages
aid = sq/insert mut db Authors { handle = 'ada' }
_ = sq/insert mut db Packages {
name = 'http_router'
owner_id = aid
downloads = 42
}
_ = sq/save mut db
// Homoiconic LEFT JOIN with qualified projections & indexed ordering
sq/q db (quote [
select [p.name a.handle p.downloads]
from packages p
left-join authors a on [p.owner_id == a.id]
where [p.downloads >= 10]
order-by [p.downloads .desc]
limit 10
])
]Recipe 6.2: Parameterized SQL Queries & Atomic Online Backups
Combine homoiconic sq/q queries with parameterized SQL (? placeholders) and zero-downtime atomic file snapshots via fs/write-atomic:
use [fs, sq = db/sqlite]
increment-downloads = fn [db_path str, pkg_name str] enum [
mut db = sq/open db_path
_ = sq/exec mut db 'UPDATE packages SET downloads = downloads + 1 WHERE name = ?' [pkg_name]
sq/save mut db
]
backup-sqlite-online = fn [db_path str, backup_path str] i64 [
db = sq/open db_path
snapshot = sq/to-bytes db
fs/write-atomic backup_path snapshot
]Recipe 6.3: Crash-Safe Append-Only KV & Secondary Indexes (lib/db/kv.vk + lib/db/query.vk)
For ultra-fast embedded state, pair db/kv with db/query secondary indexes:
use [kv = db/kv, q = db/query]
mut store = kv/open-fast 'sessions.vkkv'
sessions = q/define-table 'sessions' 'id' ['role']
_ = q/put-row mut store sessions { id = 'sess_01', user = 'ada', role = 'superadmin' }
admins = q/where-eq store sessions 'role' 'superadmin'
_ = kv/close mut storeRecipe 6.4: Pooled TLS PostgreSQL $1 Prepared Queries & RESP3 Redis Transactions (lib/db/postgres.vk + lib/db/redis.vk)
Scale stateless backend workers over bounded, health-checked PgPool (TLS 1.3 SSLRequest + SCRAM-SHA-256 + Extended Query $1, $2 binary parameters) and RedisPool (RESP3 HELLO 3 + MULTI/EXEC transactions):
use [pg = db/postgres, rd = db/redis]
fetch-active-tenants = fn [pg_pool pg/PgPool, rd_pool rd/RedisPool, region str] any [
rows = pg/with-conn pg_pool fn [mut conn pg/PgConn] any [
pg/with-tx mut conn fn [mut tx pg/PgConn] any [
pg/query-params mut tx
'SELECT id, slug FROM tenants WHERE region = $1 AND active = $2'
[region true]
]
]
_ = rd/with-conn rd_pool fn [mut rconn rd/RedisConn] any [
rd/with-tx mut rconn fn [mut q block] any [
rd/tx-queue mut q ['INCR' `metrics:queries:{region}`]
rd/tx-queue mut q ['SET' `cache:count:{region}` (mold (len rows))]
.ok
]
]
rows
]