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();
Save on change
If there's a need to save a store whenever its state changes, you can enable the saveOnChange
option when defining the store.
ts
import { ref } from 'vue';
import { defineStore } from 'pinia';
function store() {
const counter = ref(0);
return {
counter,
};
}
export const useStore = defineStore('store', store, {
tauri: {
saveOnChange: true,
// You can also debounce or throttle when saving.
// This is optional. The default behavior is to save immediately.
saveStrategy: 'debounce',
saveInterval: 1000,
},
});
Autosave
You can also enable autosave
to periodically write the stores 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");
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");