Store
Simple, persistent key-value store.
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
cargo tauri add store
-
Install the store plugin by adding the following to your
Cargo.toml
file:src-tauri/Cargo.toml [dependencies]tauri-plugin-store = "2.0.0-beta"# alternatively with Git:tauri-plugin-store = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v2" } -
Modify
lib.rs
to initialize the plugin:src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]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-store
Usage
import { Store } from '@tauri-apps/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.app_handle().state::<StoreCollection<Wry>>(); let path = PathBuf::from("store.bin");
with_store(app.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 Permissions Overview for more information.
{ "$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" ]}
Permission | Description |
---|---|
store:allow-clear | Enables the clear command without any pre-configured scope. |
store:deny-clear | Denies the clear command without any pre-configured scope. |
store:allow-delete | Enables the delete command without any pre-configured scope. |
store:deny-delete | Denies the delete command without any pre-configured scope. |
store:allow-entries | Enables the entries command without any pre-configured scope. |
store:deny-entries | Denies the entries command without any pre-configured scope. |
store:allow-get | Enables the get command without any pre-configured scope. |
store:deny-get | Denies the get command without any pre-configured scope. |
store:allow-has | Enables the has command without any pre-configured scope. |
store:deny-has | Denies the has command without any pre-configured scope. |
store:allow-keys | Enables the keys command without any pre-configured scope. |
store:deny-keys | Denies the keys command without any pre-configured scope. |
store:allow-length | Enables the length command without any pre-configured scope. |
store:deny-length | Denies the length command without any pre-configured scope. |
store:allow-load | Enables the load command without any pre-configured scope. |
store:deny-load | Denies the load command without any pre-configured scope. |
store:allow-reset | Enables the reset command without any pre-configured scope. |
store:deny-reset | Denies the reset command without any pre-configured scope. |
store:allow-save | Enables the save command without any pre-configured scope. |
store:deny-save | Denies the save command without any pre-configured scope. |
store:allow-set | Enables the set command without any pre-configured scope. |
store:deny-set | Denies the set command without any pre-configured scope. |
store:allow-values | Enables the values command without any pre-configured scope. |
store:deny-values | Denies the values command without any pre-configured scope. |
© 2024 Tauri Contributors. CC-BY / MIT