@@ -76,15 +76,13 @@ Install the store plugin to get started.
76
76
<TabItem label = " JavaScript" >
77
77
78
78
``` typescript
79
- import { createStore } from ' @tauri-apps/plugin-store' ;
79
+ import { load } from ' @tauri-apps/plugin-store' ;
80
80
// when using `"withGlobalTauri": true`, you may use
81
- // const { createStore } = window.__TAURI__.store;
81
+ // const { load } = window.__TAURI__.store;
82
82
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 });
88
86
89
87
// Set a value.
90
88
await store .set (' some-key' , { value: 5 });
@@ -94,7 +92,9 @@ const val = await store.get<{ value: number }>('some-key');
94
92
console .log (val ); // { value: 5 }
95
93
96
94
// 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.
98
98
await store .save ();
99
99
```
100
100
@@ -111,7 +111,11 @@ pub fn run() {
111
111
tauri :: Builder :: default ()
112
112
. plugin (tauri_plugin_store :: Builder :: default (). build ())
113
113
. 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" )? ;
115
119
116
120
// Note that values must be serde_json::Value instances,
117
121
// otherwise, they will not be compatible with the JavaScript bindings.
@@ -121,10 +125,8 @@ pub fn run() {
121
125
let value = store . get (" some-key" ). expect (" Failed to get value from store" );
122
126
println! (" {}" , value ); // {"value":5}
123
127
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 ();
128
130
129
131
Ok (())
130
132
})
@@ -136,6 +138,41 @@ pub fn run() {
136
138
</TabItem >
137
139
</Tabs >
138
140
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
+
139
176
## Permissions
140
177
141
178
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