SurrealDB in-memory database in Rust


Using the in-memory database can be very useful, especially in short-lived examples ike the ones we have here, but in other cases as well. It does not need any additional server component.

In this example we only setup the database connection, the namespace and the database without doing anything.

For the in-memory database we don't need authentication as only our process can access it.


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 = "2.0", features = ["kv-mem"] }
tokio = { version = "1.35", features = ["macros", "rt-multi-thread"] }

examples/surrealdb/in-memory-setup/src/main.rs
use surrealdb::engine::local::Mem;
use surrealdb::Surreal;

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

    db.use_ns("namespace").use_db("database").await?;

    Ok(())
}