Robert
Robert

Reputation: 336

How do I fix database caching for Tauri - Rust with Diesel and SQLite?

I am trying to build a small app with Tauri and SQLite database with Diesel ORM and every time I start the app with yarn tauri dev it will bring the first data from the migration script of the database. Even if I remove the database it will bring the same data. It feels like the data is cached somehow.

For Cargo.toml I have this dependencies:

[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.2.4", features = ["shell-open"] }
diesel = { version = "2.0.0", features = ["sqlite", "returning_clauses_for_sqlite_3_35"] }
libsqlite3-sys = { version = "0.25.2", features = ["bundled"] }
diesel_migrations = {version = "2.0.0", features = ["sqlite"] }
remove_dir_all = "0.8.2"

The method that I am using to select the data is this:

#[derive(Identifiable, Queryable, Serialize, Deserialize, AsChangeset)]
#[serde(rename_all = "camelCase")]
#[diesel(table_name = pwn_contracts)]
pub struct Contract {
    pub id: i32,
    pub name: String,
}

pub fn contract_list(db_path: String) -> Vec<Contract> {
    let mut connection = establish_connection(&db_path);
    pwn_contracts::dsl::pwn_contracts
        .select(pwn_contracts::all_columns)
        .load::<Contract>(&mut connection)
        .expect("Loading contracts failed")
}

The establish_connection method looks like this:

pub fn establish_connection(path: &str) -> SqliteConnection {
    SqliteConnection::establish(path).unwrap_or_else(|_| panic!("Error connecting to database"))
}

I've tried also with this:

pub fn establish_connection(path: &str) -> SqliteConnection {
    SqliteConnection::establish(&format!("sqlite://{}?cache=none", path)).unwrap_or_else(|_| panic!("Error connecting to database"))
}

But I get some error.

Am I missing something? How can I fix this?

Upvotes: 2

Views: 1012

Answers (1)

Vincent
Vincent

Reputation: 443

If your schema complexity needs do not require SQLite, there is also Native DB which is much easier to maintain and develop. An example with Tauri V2 is available here native_db_tauri_vanilla.

Upvotes: 0

Related Questions