The embed_schema! macro reads your database/schema/ directory at compile time and generates a Rust module containing the SQL for every .surql file it finds. Because the schema is compiled into the binary, there are no external files to deploy and the schema version is always tied to the application version.
Add the macros crate to your dependencies:
Basic usage
Call the macro at the crate root (typically main.rs or lib.rs):
This generates an embedded_schema module. Call sync on it after connecting to apply any outstanding schema changes:
sync behaves identically to surrealkit sync from the CLI: it applies new or changed definitions and prunes any that have been removed, using the __entity metadata table to track state.
What the macro generates
Given a database/schema/ directory with these files:
The macro generates roughly:
You can call run_sync_embedded_with_opts directly if you need to customise sync behaviour:
Compile-time rebuild behaviour
Cargo re-runs the macro whenever a .surql file in database/schema/ changes, because the macro registers each file with include_str!. This means schema changes always produce a fresh build, and there is no risk of shipping stale SQL.
When to use the macro vs runtime loading
| - | embed_schema! | run_sync_embedded |
|---|---|---|
| Schema location | Compiled into binary | Loaded from files at runtime |
| Deployment | No schema files needed | Schema directory must be present |
| Dev iteration | Rebuild required on change | Files can be swapped without rebuild |
| Best for | Production builds, Docker images | Development, mounted volumes |
For most production deployments embed_schema! is the right choice. For local development or environments that mount schema as a volume, run_sync_embedded with files loaded at runtime is more convenient.