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");
// This loads the store from disk
let store = app.store_builder("app_data.bin").build()?;
// 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"));
// 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!())
.expect("error while running tauri application");
}
```
### Loading Gracefully
If you call `load` on a `Store` that hasn't yet been written to the disk, 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
use tauri::Wry;
use tauri_plugin_store::StoreExt;
let store = app.store_builder("app_data.bin").build();
store.insert("key", "value");
```
The store created from both Rust side and JavaScript side are stored in the app's resource table and can be accessed by both sides, you can access it by using the same path, with `getStore` and `LazyStore` in the JavaScript side and `get_store` and `store` in the Rust side