SurrealKit is available as a Rust crate so you can drive connections, schema sync, and seeding from application code rather than the CLI. This is useful for applications that want to own their own database setup, for example applying schema on startup without a separate migration step.
Adding the dependency
Disabling default features keeps the binary smaller by excluding CLI-only dependencies.
Connecting
DbCfg::from_env reads the same environment variables as the CLI (SURREALDB_HOST, SURREALDB_NAMESPACE, etc.):
DbOverrides lets you override specific fields programmatically while leaving the rest to the environment:
Syncing schema at startup
Compile-time embedding
The embed_schema! macro reads your database/schema/ directory at compile time and bakes the SQL into the binary. No schema files need to ship alongside the executable.
The macro generates an embedded_schema module. Calling sync on it applies any changed definitions and prunes removed ones, the same as surrealkit sync from the CLI.
Runtime sync from files
If you prefer to load schema files at runtime (useful during development or in environments where the schema directory is mounted separately):
Sync with custom options
run_sync_embedded_with_opts gives full control over sync behaviour:
| Option | Default | Description |
|---|---|---|
watch | false | Re-sync on file changes |
debounce_ms | 0 | Debounce delay for watch mode |
dry_run | false | Print changes without applying them |
fail_fast | false | Stop on first error |
prune | true | Remove definitions not present in schema files |
allow_shared_prune | false | Permit pruning on a shared database |
Seeding
seed_from_dir runs all .surql files in a directory against the database, in filename order:
This is the programmatic equivalent of surrealkit seed.