surrealkit: Rust library
This document covers SurrealKit as a Rust library. If you are looking for the CLI, see the project README.
The library is useful when you want schema management to happen inside your process at startup, for example with an embedded SurrealDB backend (RocksDB, SpeeDB) or when running SurrealDB in the same binary during tests.
Add to your project
[]
= { = "0.5", = false }
default-features = false skips the CLI dependencies (TLS, file-watching, etc.) and pulls in only the library surface.
Schema sync
embed_schema! (compile-time embedding)
embed_schema! is a proc-macro that walks your .surql files at build time and bakes them into the binary. At runtime the generated embedded_schema::sync function applies any file whose content has changed, using the same hash-tracking logic as the CLI.
// Reads database/schema/**/*.surql relative to your Cargo.toml at compile time.
embed_schema!;
async
A custom path relative to your Cargo.toml can be passed as a string literal:
embed_schema!;
The generated module is always named embedded_schema regardless of the path argument.
run_sync_embedded (runtime slice)
If you want to construct the schema slice yourself (e.g. for tests or when the SQL comes from another source), use run_sync_embedded directly:
use ;
static SCHEMA: & = &;
run_sync_embedded.await?;
run_sync_embedded calls run_setup internally, so you do not need to call it separately.
run_sync_embedded_with_opts (full control)
run_sync_embedded_with_opts accepts a SyncOpts value for fine-grained control:
use ;
run_sync_embedded_with_opts
.await?;
Rollouts
Rollouts can be defined entirely in code. No TOML files or .surql files on disk are required.
Data types
use ;
| Type | Description |
|---|---|
RolloutSpec |
The full rollout definition (id, name, steps) |
RolloutStep |
One step: phase, kind, inline SQL or file list |
RolloutPhase |
Start, Complete, Rollback |
RolloutStepKind |
ApplySchema, RemoveEntities, RunSql, Expect |
EntityKey |
{ kind, scope, name } identifying a DB object |
Full lifecycle example
use ;
// The full desired schema after this rollout completes.
static TARGET: & = &;
let spec = RolloutSpec ;
// Apply the start phase. Blocks if another rollout is already active.
run_start_with_spec.await?;
// ... deploy new application code, wait for traffic drain, etc. ...
// Apply the complete phase, marking the rollout done.
run_complete_with_spec.await?;
Rolling back
Call run_rollback_with_spec instead of run_complete_with_spec to execute the Rollback steps and mark the rollout as rolled back:
use run_rollback_with_spec;
run_rollback_with_spec.await?;
Notes
spec.idis the stable key stored in the database. Use a unique, unchanging string per rollout (e.g. a timestamp prefix or migration name). It must be identical acrossrun_start_with_specandrun_complete_with_speccalls.- Only one rollout can be active at a time.
run_start_with_specreturns an error if a different rollout is already in therunning_startorready_to_completestate. - Inline SQL (
step.sql) and file references (step.files) are mutually exclusive within one step. Use one or the other.
Seeding
seed_from_dir executes .surql files from any directory in lexicographic order:
use seed_from_dir;
seed_from_dir.await?;
Connecting
DbCfg reads connection details from environment variables (with CLI-argument overrides). connect wraps surrealdb::Surreal construction and authentication:
use ;
let cfg = from_env?;
let db = connect.await?;
For in-process SurrealDB (e.g. kv-mem, kv-rocksdb), construct a surrealdb::Surreal directly and pass it to any of the library functions:
use ;
use Capabilities;
let db = connect.await?;
db.use_ns.use_db.await?;
run_sync_embedded.await?;
Metadata tables
SurrealKit creates two internal tables in your configured namespace/database:
| Table | Purpose |
|---|---|
__entity |
Tracks every schema object managed by SurrealKit (hash, file key, namespace) |
__rollout |
Tracks rollout execution state (planned, running_start, ready_to_complete, completed, rolled_back) |
These tables are created automatically on the first call to any library function that needs them.