diff --git a/plugins/store/build.rs b/plugins/store/build.rs index a144df296..5d30417d0 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 2c2be4768..d21ce45c3 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 000000000..73f12fd7d --- /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 ce8573460..203bdc7a8 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 bf888679f..225a4b261 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 bfd50f9cb..69ef4f64a 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 532ee1ab7..ac444aef2 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,