Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions src/content/docs/plugin/store.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,22 @@ Install the store plugin to get started.
<Tabs>
<TabItem label="JavaScript">

```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.
Expand All @@ -100,32 +103,28 @@ 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)]
pub fn run() {
tauri::Builder::default()
.plugin(tauri_plugin_store::Builder::default().build())
.setup(|app| {
let stores = app.handle().try_state::<StoreCollection<Wry>>().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(())
})
Expand Down
Loading