let mut store = StoreBuilder::new(app.handle(), "path/to/store.bin".parse()?).build();
let mut store = StoreBuilder::new("app_data.bin").build(app.handle().clone());
// Attempt to load the store, if it's saved already.
store.load().expect("Failed to load store from disk");
// Note that values must be serde_json::Value instances,
// otherwise, they will not be compatible with the JavaScript bindings.
store.insert("a".to_string(), json!("b"));
store.insert("a".to_string(), json!("b")) // note that values must be serd_json::Value to be compatible with JS
// You can manually save the store after making changes.
// Otherwise, it will save upon graceful exit as described above.
store.save()
})
})
.run(tauri::generate_context!())
.run(tauri::generate_context!())
.expect("error while running tauri application");
.expect("error while running tauri application");
}
}
```
```
As you may have noticed, the Store crated above isn't accessible to the frontend. To interoperate with stores created by JS use the exported `with_store` method:
### Loading Gracefully
If you call `load` on a `Store` that hasn't yet been written to the desk, it will return an error. You must handle this error if you want to gracefully continue and use the default store until you save it to the disk. The example above shows how to do this.
For example, this would cause a panic if the store has not yet been created:
```rust
store.load().unwrap();
```
Rather than silently continuing like you may expect.
You should always handle the error appropriately rather than unwrapping, or you may experience unexpected app crashes:
```rust
store.load().expect("Failed to load store from disk");
```
### Frontend Interoperability
As you may have noticed, the `Store` crated above isn't accessible to the frontend. To interoperate with stores created by JavaScript use the exported `with_store` method:
```rust
```rust
use tauri::Wry;
use tauri::Wry;
use tauri_plugin_store::with_store;
use tauri_plugin_store::with_store;
let stores = app.state::<StoreCollection<Wry>>();
let stores = app.state::<StoreCollection<Wry>>();
let path = PathBuf::from("path/to/the/storefile");