From b4eb468cd5378bd49fe825dba4d6a5851e2aca49 Mon Sep 17 00:00:00 2001 From: Tony <68118705+Legend-Master@users.noreply.github.com> Date: Mon, 21 Oct 2024 10:10:27 +0800 Subject: [PATCH 1/5] Update store plugin's docs to match the new rework Reference: https://github.com/tauri-apps/plugins-workspace/pull/1860 --- src/content/docs/plugin/store.mdx | 42 ++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/src/content/docs/plugin/store.mdx b/src/content/docs/plugin/store.mdx index 688ffc929a..f6d9cb173e 100644 --- a/src/content/docs/plugin/store.mdx +++ b/src/content/docs/plugin/store.mdx @@ -76,15 +76,12 @@ Install the store plugin to get started. ```typescript -import { createStore } from '@tauri-apps/plugin-store'; +import { load } from '@tauri-apps/plugin-store'; // when using `"withGlobalTauri": true`, you may use -// const { createStore } = window.__TAURI__.store; +// const { load } = window.__TAURI__.store; // 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, -}); +const store = await load('store.bin'); // Set a value. await store.set('some-key', { value: 5 }); @@ -111,7 +108,7 @@ pub fn run() { tauri::Builder::default() .plugin(tauri_plugin_store::Builder::default().build()) .setup(|app| { - let store = app.handle().store_builder("store.bin").build(); + let store = app.store("store.json")?; // Note that values must be serde_json::Value instances, // otherwise, they will not be compatible with the JavaScript bindings. @@ -121,11 +118,6 @@ pub fn run() { 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()?; - - Ok(()) }) .run(tauri::generate_context!()) @@ -136,6 +128,32 @@ pub fn run() { +## Migrating from v1 and v2 beta/rc + + + + + +```diff +- import { Store } from '@tauri-apps/plugin-store'; ++ import { LazyStore } from '@tauri-apps/plugin-store'; +``` + + + + +```diff +- with_store(app.handle().clone(), stores, path, |store| { +- store.insert("some-key".to_string(), json!({ "value": 5 }))?; +- Ok(()) +- }); ++ let store = app.store(path)?; ++ store.set("some-key".to_string(), json!({ "value": 5 })); +``` + + + + ## Permissions 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. From 0d73b208ef258c8647f4c58e6b05f3d58ac71507 Mon Sep 17 00:00:00 2001 From: Tony <68118705+Legend-Master@users.noreply.github.com> Date: Mon, 21 Oct 2024 10:19:46 +0800 Subject: [PATCH 2/5] More docs --- src/content/docs/plugin/store.mdx | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/content/docs/plugin/store.mdx b/src/content/docs/plugin/store.mdx index f6d9cb173e..b36247b0e5 100644 --- a/src/content/docs/plugin/store.mdx +++ b/src/content/docs/plugin/store.mdx @@ -80,8 +80,8 @@ import { load } from '@tauri-apps/plugin-store'; // when using `"withGlobalTauri": true`, you may use // const { load } = window.__TAURI__.store; -// create a new store or load the existing one -const store = await load('store.bin'); +// Create a new store or load the existing one, note that the options will be ignored if a `Store` with that path has already been created +const store = await load('store.bin', { autoSave: false }); // Set a value. await store.set('some-key', { value: 5 }); @@ -91,7 +91,9 @@ const val = await store.get<{ value: number }>('some-key'); console.log(val); // { value: 5 } // You can manually save the store after making changes. -// Otherwise, it will save upon graceful exit as described above. +// Otherwise, it will save upon graceful exit +// And if you set `autoSave` to a number or left empty, +// it will save the changes to disk after a debounce delay, 100ms by default. await store.save(); ``` @@ -108,6 +110,9 @@ pub fn run() { tauri::Builder::default() .plugin(tauri_plugin_store::Builder::default().build()) .setup(|app| { + // Create a new store or load the existing one + // this also put the store in the app's resource table + // so your following calls `store` calls (from both rust and js) will reuse the same store let store = app.store("store.json")?; // Note that values must be serde_json::Value instances, @@ -118,6 +123,9 @@ pub fn run() { let value = store.get("some-key").expect("Failed to get value from store"); println!("{}", value); // {"value":5} + // Remove the store from the resource table + store.close_resource(); + Ok(()) }) .run(tauri::generate_context!()) @@ -128,8 +136,17 @@ pub fn run() { -## Migrating from v1 and v2 beta/rc +### LazyStore + +There's also a high level JavaScript API `LazyStore` which only loads the store on first access + +```typescript +import { LazyStore } from '@tauri-apps/plugin-store' + +const store = new LazyStore('settings.json') +``` +## Migrating from v1 and v2 beta/rc From 198949b7e8c549719179625a124c2f537641865e Mon Sep 17 00:00:00 2001 From: Tony <68118705+Legend-Master@users.noreply.github.com> Date: Mon, 21 Oct 2024 10:23:19 +0800 Subject: [PATCH 3/5] Format --- src/content/docs/plugin/store.mdx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/content/docs/plugin/store.mdx b/src/content/docs/plugin/store.mdx index b36247b0e5..3e73eead1d 100644 --- a/src/content/docs/plugin/store.mdx +++ b/src/content/docs/plugin/store.mdx @@ -80,7 +80,8 @@ import { load } from '@tauri-apps/plugin-store'; // when using `"withGlobalTauri": true`, you may use // const { load } = window.__TAURI__.store; -// Create a new store or load the existing one, note that the options will be ignored if a `Store` with that path has already been created +// Create a new store or load the existing one, +// note that the options will be ignored if a `Store` with that path has already been created const store = await load('store.bin', { autoSave: false }); // Set a value. @@ -112,7 +113,8 @@ pub fn run() { .setup(|app| { // Create a new store or load the existing one // this also put the store in the app's resource table - // so your following calls `store` calls (from both rust and js) will reuse the same store + // so your following calls `store` calls (from both rust and js) + // will reuse the same store let store = app.store("store.json")?; // Note that values must be serde_json::Value instances, From 86fc18b653216d2fa6da529e9554f19b45164b07 Mon Sep 17 00:00:00 2001 From: Tony <68118705+Legend-Master@users.noreply.github.com> Date: Mon, 21 Oct 2024 10:24:04 +0800 Subject: [PATCH 4/5] Use .json --- src/content/docs/plugin/store.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/plugin/store.mdx b/src/content/docs/plugin/store.mdx index 3e73eead1d..65bb6abcc1 100644 --- a/src/content/docs/plugin/store.mdx +++ b/src/content/docs/plugin/store.mdx @@ -82,7 +82,7 @@ import { load } from '@tauri-apps/plugin-store'; // Create a new store or load the existing one, // note that the options will be ignored if a `Store` with that path has already been created -const store = await load('store.bin', { autoSave: false }); +const store = await load('store.json', { autoSave: false }); // Set a value. await store.set('some-key', { value: 5 }); From abb10033e3836cf8c5bc0ee8e3868c82d5581969 Mon Sep 17 00:00:00 2001 From: Tony <68118705+Legend-Master@users.noreply.github.com> Date: Mon, 21 Oct 2024 10:30:27 +0800 Subject: [PATCH 5/5] Missing semi --- src/content/docs/plugin/store.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/content/docs/plugin/store.mdx b/src/content/docs/plugin/store.mdx index 65bb6abcc1..b27311a61a 100644 --- a/src/content/docs/plugin/store.mdx +++ b/src/content/docs/plugin/store.mdx @@ -143,9 +143,9 @@ pub fn run() { There's also a high level JavaScript API `LazyStore` which only loads the store on first access ```typescript -import { LazyStore } from '@tauri-apps/plugin-store' +import { LazyStore } from '@tauri-apps/plugin-store'; -const store = new LazyStore('settings.json') +const store = new LazyStore('settings.json'); ``` ## Migrating from v1 and v2 beta/rc