Skip to content

Commit 607a22e

Browse files
Update store plugin's docs to match the new rework (#2888)
1 parent 74f3d59 commit 607a22e

File tree

1 file changed

+50
-13
lines changed

1 file changed

+50
-13
lines changed

src/content/docs/plugin/store.mdx

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,13 @@ Install the store plugin to get started.
7676
<TabItem label="JavaScript">
7777

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

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-
});
83+
// Create a new store or load the existing one,
84+
// note that the options will be ignored if a `Store` with that path has already been created
85+
const store = await load('store.json', { autoSave: false });
8886

8987
// Set a value.
9088
await store.set('some-key', { value: 5 });
@@ -94,7 +92,9 @@ const val = await store.get<{ value: number }>('some-key');
9492
console.log(val); // { value: 5 }
9593

9694
// You can manually save the store after making changes.
97-
// Otherwise, it will save upon graceful exit as described above.
95+
// Otherwise, it will save upon graceful exit
96+
// And if you set `autoSave` to a number or left empty,
97+
// it will save the changes to disk after a debounce delay, 100ms by default.
9898
await store.save();
9999
```
100100

@@ -111,7 +111,11 @@ pub fn run() {
111111
tauri::Builder::default()
112112
.plugin(tauri_plugin_store::Builder::default().build())
113113
.setup(|app| {
114-
let store = app.handle().store_builder("store.bin").build();
114+
// Create a new store or load the existing one
115+
// this also put the store in the app's resource table
116+
// so your following calls `store` calls (from both rust and js)
117+
// will reuse the same store
118+
let store = app.store("store.json")?;
115119

116120
// Note that values must be serde_json::Value instances,
117121
// otherwise, they will not be compatible with the JavaScript bindings.
@@ -121,10 +125,8 @@ pub fn run() {
121125
let value = store.get("some-key").expect("Failed to get value from store");
122126
println!("{}", value); // {"value":5}
123127

124-
// You can manually save the store after making changes.
125-
// Otherwise, it will save upon graceful exit as described above.
126-
store.save()?;
127-
128+
// Remove the store from the resource table
129+
store.close_resource();
128130

129131
Ok(())
130132
})
@@ -136,6 +138,41 @@ pub fn run() {
136138
</TabItem>
137139
</Tabs>
138140

141+
### LazyStore
142+
143+
There's also a high level JavaScript API `LazyStore` which only loads the store on first access
144+
145+
```typescript
146+
import { LazyStore } from '@tauri-apps/plugin-store';
147+
148+
const store = new LazyStore('settings.json');
149+
```
150+
151+
## Migrating from v1 and v2 beta/rc
152+
153+
<Tabs>
154+
<TabItem label="JavaScript">
155+
156+
```diff
157+
- import { Store } from '@tauri-apps/plugin-store';
158+
+ import { LazyStore } from '@tauri-apps/plugin-store';
159+
```
160+
161+
</TabItem>
162+
<TabItem label="Rust">
163+
164+
```diff
165+
- with_store(app.handle().clone(), stores, path, |store| {
166+
- store.insert("some-key".to_string(), json!({ "value": 5 }))?;
167+
- Ok(())
168+
- });
169+
+ let store = app.store(path)?;
170+
+ store.set("some-key".to_string(), json!({ "value": 5 }));
171+
```
172+
173+
</TabItem>
174+
</Tabs>
175+
139176
## Permissions
140177

141178
By default all potentially dangerous plugin commands and scopes are blocked and cannot be accessed. You must modify the permissions in your `capabilities` configuration to enable these.

0 commit comments

Comments
 (0)