Persisting state
All your stores are automatically persisted to disk upon graceful exit, so usually there's no need to manually save them. However, if you want to do so:
ts
import { save, saveAll } from 'tauri-plugin-pinia';
// Save a single store.
await save('my-store');
// Save some stores.
await save(['my-store', 'my-store-2']);
// Save all stores.
await saveAll();
rust
use tauri_plugin_pinia::ManagerExt;
// `manager` represents any type that implements the `Manager` trait.
// This includes `AppHandle`, `Window`, and `WebviewWindow`.
// Save a single store.
manager.pinia().save("my-store");
// Save some stores.
manager.pinia().save_some(&["my-store", "my-store-2"]);
// Save all stores.
manager.pinia().save_all();
Custom directory
By default, the stores are saved in the app's data directory. You can change this by setting the path
option when initializing the plugin.
rust
tauri::Builder::default()
.plugin(
tauri_plugin_pinia::Builder::new()
.path("/path/to/custom/directory")
.build(),
)
.run(tauri::generate_context!())
.expect("error while running tauri application");
Autosave unstable-async
Instead of only saving on app exit, you can also enable autosave
to periodically write them to disk.
rust
use std::time::Duration;
tauri::Builder::default()
.plugin(
tauri_plugin_pinia::Builder::new()
.autosave(Duration::from_secs(300))
.build(),
)
.run(tauri::generate_context!())
.expect("error while running tauri application");
TIP
This is not much different from calling saveAll
inside a JavaScript setInterval
. Use this feature only if you prefer using Rust to handle the autosave.