diff --git a/src/content/docs/plugin/store.mdx b/src/content/docs/plugin/store.mdx index 6fc3596472..1552561e14 100644 --- a/src/content/docs/plugin/store.mdx +++ b/src/content/docs/plugin/store.mdx @@ -75,19 +75,22 @@ Install the store plugin to get started. -```javascript -import { Store } from '@tauri-apps/plugin-store'; +```typescript +import { createStore } from '@tauri-apps/plugin-store'; // when using `"withGlobalTauri": true`, you may use -// const { Store } = window.__TAURI__.store; +// const { createStore } = window.__TAURI__.store; -// Store will be loaded automatically when used in JavaScript binding. -const store = new Store('store.bin'); +// create a new store or load the existing one +const store = await createStore('store.bin', { + // we can save automatically after each store modification + autoSave: true, +}); // Set a value. await store.set('some-key', { value: 5 }); // Get a value. -const val = await store.get('some-key'); +const val = await store.get<{ value: number }>('some-key'); console.log(val); // { value: 5 } // You can manually save the store after making changes. @@ -100,7 +103,7 @@ await store.save(); ```rust title="src-tauri/src/lib.rs" use tauri::Wry; -use tauri_plugin_store::{with_store, StoreCollection}; +use tauri_plugin_store::StoreExt; use serde_json::json; #[cfg_attr(mobile, tauri::mobile_entry_point)] @@ -108,24 +111,20 @@ pub fn run() { tauri::Builder::default() .plugin(tauri_plugin_store::Builder::default().build()) .setup(|app| { - let stores = app.handle().try_state::>().ok_or("Store not found")?; - let path = PathBuf::from("store.bin"); + let store = app.handle().store_builder("store.bin").build(); - 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 }))?; + // Note that values must be serde_json::Value instances, + // otherwise, they will not be compatible with the JavaScript bindings. + store.insert("some-key", 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} + // 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()?; + // You can manually save the store after making changes. + // Otherwise, it will save upon graceful exit as described above. + store.save()?; - Ok(()) - }); Ok(()) })