Setting up an in-memory SurrealDB database in Rust

SurrealDB Mem

Before getting started on any bigger demo, this example shows how to set up and in-memory Surreal Database in Rust.

Dependencies

examples/surrealdb/in-memory-setup/Cargo.toml

[package]
name = "in-memory-setup"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde = { version = "1.0", features = ["derive"] }
surrealdb = { version = "1.1", features = ["kv-mem"] }
tokio = { version = "1.35", features = ["macros", "rt-multi-thread"] }

The code

examples/surrealdb/in-memory-setup/src/main.rs

use surrealdb::Surreal;
use surrealdb::engine::local::Mem;

#[tokio::main]
async fn main() -> surrealdb::Result<()> {
    let _db = Surreal::new::<Mem>(()).await?;

    Ok(())
}

Conclusion

There is not much in this example.

This will create an in-memory database so anything we add to the database will be gone when the application stops running.

If you prefer a persistent database where the data is stored on the disk, but you would still like to use the simple, embedded version, take a look at Setting up embedded SurrealDB with RocksDB backend.

Related Pages

SurrealDB in-memory with SQL demo in Rust
SurrealDB
Setting up embedded SurrealDB with RocksDB backend in Rust

Author

Gabor Szabo (szabgab)

Gabor Szabo, the author of the Rust Maven web site maintains several Open source projects in Rust and while he still feels he has tons of new things to learn about Rust he already offers training courses in Rust and still teaches Python, Perl, git, GitHub, GitLab, CI, and testing.

Gabor Szabo