Store
This plugin provides a persistent key-value store. This is one of many options to handle state in your application. See the state management overview for more information on additional options.
This store will allow you to persist state to a file which can be saved and loaded on demand including between app restarts. Note that this process is asynchronous which will require handling it within your code. It can be used both in the webview or within Rust.
Supported Platforms
- Windows
- Linux
- macOS
- Android
- iOS
Setup
Install the store plugin to get started.
Use your project’s package manager to add the dependency:
npm run tauri add store
yarn run tauri add store
pnpm tauri add store
bun tauri add store
cargo tauri add store
-
Run the following command in the
src-tauri
folder to add the plugin to the project’s dependencies inCargo.toml
:cargo add tauri-plugin-store -
Modify
lib.rs
to initialize the plugin:src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().plugin(tauri_plugin_store::Builder::new().build()).run(tauri::generate_context!()).expect("error while running tauri application");} -
Install the JavaScript Guest bindings using your preferred JavaScript package manager:
npm install @tauri-apps/plugin-storeyarn add @tauri-apps/plugin-storepnpm add @tauri-apps/plugin-storebun add @tauri-apps/plugin-store
Usage
import { Store } from '@tauri-apps/plugin-store';// when using `"withGlobalTauri": true`, you may use// const { Store } = window.__TAURI_PLUGIN_STORE__;
// Store will be loaded automatically when used in JavaScript binding.const store = new Store('store.bin');
// Set a value.await store.set('some-key', { value: 5 });
// Get a value.const val = await store.get('some-key');console.log(val); // { value: 5 }
// You can manually save the store after making changes.// Otherwise, it will save upon graceful exit as described above.await store.save();
use tauri::Wry;use tauri_plugin_store::{with_store, StoreCollection};use serde_json::json;
#[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() { tauri::Builder::default() .plugin(tauri_plugin_store::Builder::default().build()) .setup(|app| { let stores = app.handle().try_state::<StoreCollection<Wry>>().ok_or("Store not found")?; let path = PathBuf::from("store.bin");
with_store(app.handle().clone(), stores, path, |store| { // Note that values must be serde_json::Value instances, // otherwise, they will not be compatible with the JavaScript bindings. store.insert("some-key".to_string(), json!({ "value": 5 }))?;
// Get a value from the store. let value = store.get("some-key").expect("Failed to get value from store"); println!("{}", value); // {"value":5}
// You can manually save the store after making changes. // Otherwise, it will save upon graceful exit as described above. store.save()?;
Ok(()) });
Ok(()) }) .run(tauri::generate_context!()) .expect("error while running tauri application");}
Permissions
By default, all plugin commands are blocked and cannot be accessed. You must define a list of permissions in your capabilities
configuration.
See the Capabilities Overview for more information and the step by step guide to use plugin permissions.
{ "$schema": "../gen/schemas/desktop-schema.json", "identifier": "main-capability", "description": "Capability for the main window", "windows": ["main"], "permissions": [ "store:allow-get", "store:allow-set", "store:allow-save", "store:allow-load" ]}
Default Permission
This permission set configures what kind of operations are available from the store plugin.
Granted Permissions
All operations are enabled by default.
allow-clear
allow-delete
allow-entries
allow-get
allow-has
allow-keys
allow-length
allow-load
allow-reset
allow-save
allow-set
allow-values
Permission Table
Identifier | Description |
---|---|
|
Enables the clear command without any pre-configured scope. |
|
Denies the clear command without any pre-configured scope. |
|
Enables the delete command without any pre-configured scope. |
|
Denies the delete command without any pre-configured scope. |
|
Enables the entries command without any pre-configured scope. |
|
Denies the entries command without any pre-configured scope. |
|
Enables the get command without any pre-configured scope. |
|
Denies the get command without any pre-configured scope. |
|
Enables the has command without any pre-configured scope. |
|
Denies the has command without any pre-configured scope. |
|
Enables the keys command without any pre-configured scope. |
|
Denies the keys command without any pre-configured scope. |
|
Enables the length command without any pre-configured scope. |
|
Denies the length command without any pre-configured scope. |
|
Enables the load command without any pre-configured scope. |
|
Denies the load command without any pre-configured scope. |
|
Enables the reset command without any pre-configured scope. |
|
Denies the reset command without any pre-configured scope. |
|
Enables the save command without any pre-configured scope. |
|
Denies the save command without any pre-configured scope. |
|
Enables the set command without any pre-configured scope. |
|
Denies the set command without any pre-configured scope. |
|
Enables the values command without any pre-configured scope. |
|
Denies the values command without any pre-configured scope. |
© 2024 Tauri Contributors. CC-BY / MIT