Getting started: regular Rust code
Starting a Surrealism project is best done using the surreal module init command, which is similar to cargo run when starting a project in Rust. The Cargo.toml file will include the following line to instruct cargo to generate a .wasm file instead of the standard .rlib file when compiling Rust.
The command will also ask for the project and organisation name, which will lead to the following surrealism.toml file.
You'll also notice a few sample functions inside lib.rs. To internalise how Surrealism works, let's delete them and begin with three of our own functions that we would like to call using SurrealQL.
One is a simple function that returns a bool.
The second returns a Result.
And the third returns a User struct via a function called random_user() which uses the fake crate to create a user with a random English first name, French middle name, and German last name. This struct is annotated with the SurrealValue trait, allowing its return value to be understood on the SurrealDB side.
Here is all of the code.
Annotating functions
The functions that we want to call from inside SurrealQL can now be exposed by adding the #[surrealism] annotation.
With this done, the Rust code can now be compiled via the module build command in the CLI. This is followed with the --out flag and the output file name/path (e.g. demo.surli). If you are in a separate folder, you can follow this with the path to the Rust code with functions to expose.
For faster local iteration:
The file will now be compiled at either your current location or the one specified like in the command above: /Users/my_name/my_rust_code.
That's the module archive that SurrealDB can point to!
Calling the Surrealism file from SurrealDB
The Surrealism file is ready to be used. All that is left now is to start a database with two environment variables. They are:
SURREAL_CAPS_ALLOW_EXPERIMENTAL=files,surrealism, which allows the files and surrealism features to be used in the first place. Files can be used in SurrealDB once aDEFINE BUCKETstatement has been used to set up the location for the files to be stored.SURREAL_BUCKET_FOLDER_ALLOWLIST="/Users/my_name/my_rust_code/", which tells the database that it's okay to access this folder when using the files feature.
Now it's time to start the database with the surreal start command and these two env vars.
You can then connect through surrealist or the CLI with the surreal sql command:
We're almost there! Only two statements left and we can start accessing these functions.
The first is a DEFINE BUCKET statement to create a bucket called test, linked to the folder that we have allowed the database to access. The "file:/" prefix in this statement tells the DEFINE BUCKET statement that this path is to a file, not storage in memory.
Now that we have a bucket called test, the demo.surli file will be available to us at the f"test:/demo.surli" path. The f there is an instruction to treat the string as a file path instead of just a regular string.
Then we can define a module which will hold all the tests. We'll call it mod::test and use AS to connect it to the file.
And now the magic begins! Let's give the parse_number() function a try, now available at the mod::test::parse_number path.
Next, we can use random_user() to create some random users.
That leaves us with one function left to try out, the can_drive() function.
Two of them can drive, but not Verda Clarisse Schuster who is far too young.