Skip to content

Commit c631bbf

Browse files
authored
feat: update store plugin API (#2769)
1 parent f0efc8f commit c631bbf

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

src/content/docs/plugin/store.mdx

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,22 @@ Install the store plugin to get started.
7575
<Tabs>
7676
<TabItem label="JavaScript">
7777

78-
```javascript
79-
import { Store } from '@tauri-apps/plugin-store';
78+
```typescript
79+
import { createStore } from '@tauri-apps/plugin-store';
8080
// when using `"withGlobalTauri": true`, you may use
81-
// const { Store } = window.__TAURI__.store;
81+
// const { createStore } = window.__TAURI__.store;
8282

83-
// Store will be loaded automatically when used in JavaScript binding.
84-
const store = new Store('store.bin');
83+
// create a new store or load the existing one
84+
const store = await createStore('store.bin', {
85+
// we can save automatically after each store modification
86+
autoSave: true,
87+
});
8588

8689
// Set a value.
8790
await store.set('some-key', { value: 5 });
8891

8992
// Get a value.
90-
const val = await store.get('some-key');
93+
const val = await store.get<{ value: number }>('some-key');
9194
console.log(val); // { value: 5 }
9295

9396
// You can manually save the store after making changes.
@@ -100,32 +103,28 @@ await store.save();
100103

101104
```rust title="src-tauri/src/lib.rs"
102105
use tauri::Wry;
103-
use tauri_plugin_store::{with_store, StoreCollection};
106+
use tauri_plugin_store::StoreExt;
104107
use serde_json::json;
105108

106109
#[cfg_attr(mobile, tauri::mobile_entry_point)]
107110
pub fn run() {
108111
tauri::Builder::default()
109112
.plugin(tauri_plugin_store::Builder::default().build())
110113
.setup(|app| {
111-
let stores = app.handle().try_state::<StoreCollection<Wry>>().ok_or("Store not found")?;
112-
let path = PathBuf::from("store.bin");
114+
let store = app.handle().store_builder("store.bin").build();
113115

114-
with_store(app.handle().clone(), stores, path, |store| {
115-
// Note that values must be serde_json::Value instances,
116-
// otherwise, they will not be compatible with the JavaScript bindings.
117-
store.insert("some-key".to_string(), json!({ "value": 5 }))?;
116+
// Note that values must be serde_json::Value instances,
117+
// otherwise, they will not be compatible with the JavaScript bindings.
118+
store.insert("some-key", json!({ "value": 5 }))?;
118119

119-
// Get a value from the store.
120-
let value = store.get("some-key").expect("Failed to get value from store");
121-
println!("{}", value); // {"value":5}
120+
// Get a value from the store.
121+
let value = store.get("some-key").expect("Failed to get value from store");
122+
println!("{}", value); // {"value":5}
122123

123-
// You can manually save the store after making changes.
124-
// Otherwise, it will save upon graceful exit as described above.
125-
store.save()?;
124+
// You can manually save the store after making changes.
125+
// Otherwise, it will save upon graceful exit as described above.
126+
store.save()?;
126127

127-
Ok(())
128-
});
129128

130129
Ok(())
131130
})

0 commit comments

Comments
 (0)