$npx -y skills add 24601/surreal-skills --skill surrealismSurrealDB Surrealism WASM extension development. Write Rust functions, compile to WASM, deploy as database modules. Part of the surreal-skills collection.
| 1 | # Surrealism -- WASM Extensions for SurrealDB |
| 2 | |
| 3 | New in SurrealDB 3. Write custom functions in Rust, compile them to WebAssembly |
| 4 | (WASM), and deploy them as native database modules callable from SurrealQL. |
| 5 | |
| 6 | ## Prerequisites |
| 7 | |
| 8 | - Rust toolchain (stable) with `wasm32-unknown-unknown` target |
| 9 | - SurrealDB CLI v3.1.4+ (`surreal` binary with `surreal module` subcommand) |
| 10 | - Familiarity with SurrealQL `DEFINE MODULE` and `DEFINE BUCKET` |
| 11 | |
| 12 | ## Development Workflow |
| 13 | |
| 14 | ``` |
| 15 | 1. Annotate -- surrealism.toml + #[surrealism] on Rust functions |
| 16 | 2. Compile -- surreal module compile (produces .wasm binary) |
| 17 | 3. Register -- DEFINE BUCKET + DEFINE MODULE in SurrealQL |
| 18 | ``` |
| 19 | |
| 20 | ## Quick Start |
| 21 | |
| 22 | ```bash |
| 23 | # Create a new Surrealism project |
| 24 | cargo new --lib my_extension |
| 25 | cd my_extension |
| 26 | |
| 27 | # Add the WASM target |
| 28 | rustup target add wasm32-unknown-unknown |
| 29 | |
| 30 | # Create surrealism.toml (required manifest) |
| 31 | cat > surrealism.toml << 'TOML' |
| 32 | [package] |
| 33 | name = "my_extension" |
| 34 | version = "0.1.0" |
| 35 | TOML |
| 36 | |
| 37 | # Write your extension (annotate with #[surrealism]) |
| 38 | cat > src/lib.rs << 'RUST' |
| 39 | use surrealism::surrealism; |
| 40 | |
| 41 | #[surrealism] |
| 42 | fn greet(name: String) -> String { |
| 43 | format!("Hello, {}!", name) |
| 44 | } |
| 45 | RUST |
| 46 | |
| 47 | # Compile to WASM using SurrealDB CLI |
| 48 | surreal module compile |
| 49 | |
| 50 | # Register in SurrealDB |
| 51 | surreal sql --endpoint http://localhost:8000 --user root --pass root --ns test --db test |
| 52 | ``` |
| 53 | |
| 54 | ```surql |
| 55 | -- Grant access to the WASM file |
| 56 | DEFINE BUCKET my_bucket; |
| 57 | |
| 58 | -- Register the module functions |
| 59 | DEFINE MODULE my_extension FROM 'my_bucket:my_extension.wasm'; |
| 60 | |
| 61 | -- Use the function in queries |
| 62 | SELECT my_extension::greet('World'); |
| 63 | ``` |
| 64 | |
| 65 | ## Use Cases |
| 66 | |
| 67 | - Custom scalar functions callable from SurrealQL |
| 68 | - Fake/mock data generation for testing |
| 69 | - Domain-specific logic (language processing, quantitative finance, custom encoding) |
| 70 | - Access to niche Rust crate functionality too specific for core SurrealDB |
| 71 | - Custom analyzers for full-text search |
| 72 | |
| 73 | ## Status |
| 74 | |
| 75 | Surrealism is actively in development and not yet stable. The API may change |
| 76 | between SurrealDB 3.x releases. File feedback via GitHub issues/PRs on the |
| 77 | [surrealdb/surrealdb](https://github.com/surrealdb/surrealdb) repository. |
| 78 | |
| 79 | ## Full Documentation |
| 80 | |
| 81 | See the main skill's rule file for complete guidance: |
| 82 | - **[rules/surrealism.md](../../rules/surrealism.md)** -- project setup, Rust function signatures, WASM compilation, DEFINE MODULE/BUCKET syntax, deployment, testing, and best practices |
| 83 | - **[SurrealDB Extensions Docs](https://surrealdb.com/docs/surrealdb/extensions)** -- official documentation |
| 84 | - **[CLI module command](https://surrealdb.com/docs/surrealdb/cli/module)** -- `surreal module` reference |