From 7ffd769240b1973613259bd7b2e5d999db1ce329 Mon Sep 17 00:00:00 2001 From: Tony Date: Thu, 3 Oct 2024 13:34:38 +0800 Subject: [PATCH] Add get-or-create-store --- plugins/store/build.rs | 1 + plugins/store/guest-js/index.ts | 33 +++++++++++++++++-- .../commands/get_or_create_store.toml | 13 ++++++++ .../permissions/autogenerated/reference.md | 26 +++++++++++++++ plugins/store/permissions/default.toml | 2 ++ plugins/store/permissions/schemas/schema.json | 10 ++++++ plugins/store/src/lib.rs | 14 ++++++++ 7 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 plugins/store/permissions/autogenerated/commands/get_or_create_store.toml diff --git a/plugins/store/build.rs b/plugins/store/build.rs index a144df29..5d30417d 100644 --- a/plugins/store/build.rs +++ b/plugins/store/build.rs @@ -5,6 +5,7 @@ const COMMANDS: &[&str] = &[ "create_store", "get_store", + "get_or_create_store", "set", "get", "has", diff --git a/plugins/store/guest-js/index.ts b/plugins/store/guest-js/index.ts index 2c2be476..d21ce45c 100644 --- a/plugins/store/guest-js/index.ts +++ b/plugins/store/guest-js/index.ts @@ -42,6 +42,17 @@ export async function getStore(path: string): Promise { return await Store.getStore(path) } +/** + * @param path: Path to save the store in `app_data_dir` + * @param options: Store configuration options + */ +export async function getOrCreateStore( + path: string, + options?: StoreOptions +): Promise { + return await Store.getOrCreateStore(path, options) +} + /** * A lazy loaded key-value store persisted by the backend layer. * @@ -56,9 +67,7 @@ export class LazyStore implements IStore { private get store(): Promise { if (!this._store) { - this._store = createStore(this.path, this.options).catch( - async () => (await getStore(this.path))! - ) + this._store = getOrCreateStore(this.path, this.options) } return this._store } @@ -174,6 +183,24 @@ export class Store extends Resource implements IStore { return resourceId ? new Store(resourceId, path) : undefined } + /** + * @param path: Path to save the store in `app_data_dir` + * @param options: Store configuration options + */ + static async getOrCreateStore( + path: string, + options?: StoreOptions + ): Promise { + const resourceId = await invoke( + 'plugin:store|get_or_create_store', + { + path, + ...options + } + ) + return new Store(resourceId, path) + } + async set(key: string, value: unknown): Promise { await invoke('plugin:store|set', { rid: this.rid, diff --git a/plugins/store/permissions/autogenerated/commands/get_or_create_store.toml b/plugins/store/permissions/autogenerated/commands/get_or_create_store.toml new file mode 100644 index 00000000..73f12fd7 --- /dev/null +++ b/plugins/store/permissions/autogenerated/commands/get_or_create_store.toml @@ -0,0 +1,13 @@ +# Automatically generated - DO NOT EDIT! + +"$schema" = "../../schemas/schema.json" + +[[permission]] +identifier = "allow-get-or-create-store" +description = "Enables the get_or_create_store command without any pre-configured scope." +commands.allow = ["get_or_create_store"] + +[[permission]] +identifier = "deny-get-or-create-store" +description = "Denies the get_or_create_store command without any pre-configured scope." +commands.deny = ["get_or_create_store"] diff --git a/plugins/store/permissions/autogenerated/reference.md b/plugins/store/permissions/autogenerated/reference.md index ce857346..203bdc7a 100644 --- a/plugins/store/permissions/autogenerated/reference.md +++ b/plugins/store/permissions/autogenerated/reference.md @@ -165,6 +165,32 @@ Denies the get command without any pre-configured scope. +`store:allow-get-or-create-store` + + + + +Enables the get_or_create_store command without any pre-configured scope. + + + + + + + +`store:deny-get-or-create-store` + + + + +Denies the get_or_create_store command without any pre-configured scope. + + + + + + + `store:allow-get-store` diff --git a/plugins/store/permissions/default.toml b/plugins/store/permissions/default.toml index bf888679..225a4b26 100644 --- a/plugins/store/permissions/default.toml +++ b/plugins/store/permissions/default.toml @@ -12,6 +12,8 @@ All operations are enabled by default. """ permissions = [ "allow-create-store", + "allow-get-store", + "allow-get-or-create-store", "allow-clear", "allow-delete", "allow-entries", diff --git a/plugins/store/permissions/schemas/schema.json b/plugins/store/permissions/schemas/schema.json index bfd50f9c..69ef4f64 100644 --- a/plugins/store/permissions/schemas/schema.json +++ b/plugins/store/permissions/schemas/schema.json @@ -344,6 +344,16 @@ "type": "string", "const": "deny-get" }, + { + "description": "Enables the get_or_create_store command without any pre-configured scope.", + "type": "string", + "const": "allow-get-or-create-store" + }, + { + "description": "Denies the get_or_create_store command without any pre-configured scope.", + "type": "string", + "const": "deny-get-or-create-store" + }, { "description": "Enables the get_store command without any pre-configured scope.", "type": "string", diff --git a/plugins/store/src/lib.rs b/plugins/store/src/lib.rs index 532ee1ab..ac444aef 100644 --- a/plugins/store/src/lib.rs +++ b/plugins/store/src/lib.rs @@ -77,6 +77,19 @@ async fn get_store(app: AppHandle, path: PathBuf) -> Option( + app: AppHandle, + path: PathBuf, + auto_save: Option, +) -> Result { + if let Some(rid) = get_store(app.clone(), path.clone()).await { + Ok(rid) + } else { + create_store(app, path, auto_save).await + } +} + #[tauri::command] async fn set( app: AppHandle, @@ -232,6 +245,7 @@ impl Builder { .invoke_handler(tauri::generate_handler![ create_store, get_store, + get_or_create_store, set, get, has,