#Embedding SurrealDB with a persistent engine in a Tauri app

4 messages · Page 1 of 1 (latest)

fringe nova
#

Hello,

I'm struggling with embedding SurrealDB with a persistent engine in a Tauri application (specifically Tauri 2 - beta). Everything works correctly when I use the in-memory engine. However, when I try the same thing, with a simple switch to an engine like RocksDB, SpeeDB, or SurrealKV, everything works properly in the dev environment, but after building the project, it stops working. The application builds without errors, but the functions that in dev created and read records in SurrealDB now do nothing. I'm new to Tauri and Rust, but after several days spent on Google, it doesn't seem like I'm doing anything wrong, although I probably am.

Here's what my lib.rs file looks like, where I initiate SurrealDB:

async fn init_db() -> Result<(), Box<dyn std::error::Error>> {
    let db = DB.connect::<SurrealKV>("db/surreal.db").await.expect("Unable to construct Database");
    let _ = DB.use_ns("surreal").use_db("local").await;
    Ok(db)
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
    .setup(|app| {
        let _main_window = app.get_webview_window("main").unwrap();
        tauri::async_runtime::spawn(async {
            init_db().await.expect("Something wrong with initiation of DB");
        });
        Ok(())
    })
        .invoke_handler(tauri::generate_handler![
            ipc::create_user,
            ipc::get_user
        ])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

Could someone help me with this?

#

Embedding SurrealDB with a persistent engine in a Tauri app

shy lagoon
#

but after building the project, it stops working
Looking at your code i assume it's a path issue. You can't use paths relative to the app location beacuse that'll be read-only (and if you try to bundle a preconfigured db it's like the wrong location too, except on windows).

fringe nova
#

@shy lagoon Thanks! I spent some time considering your suggestion and it looks like I think I've solved it through tauri::path::BaseDirectory, thanks a lot!