Add get-or-create-store

pull/1860/head
Tony 8 months ago
parent 17d910b381
commit 7ffd769240
No known key found for this signature in database
GPG Key ID: 34BDD3EA27824956

@ -5,6 +5,7 @@
const COMMANDS: &[&str] = &[ const COMMANDS: &[&str] = &[
"create_store", "create_store",
"get_store", "get_store",
"get_or_create_store",
"set", "set",
"get", "get",
"has", "has",

@ -42,6 +42,17 @@ export async function getStore(path: string): Promise<Store | undefined> {
return await Store.getStore(path) 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<Store> {
return await Store.getOrCreateStore(path, options)
}
/** /**
* A lazy loaded key-value store persisted by the backend layer. * A lazy loaded key-value store persisted by the backend layer.
* *
@ -56,9 +67,7 @@ export class LazyStore implements IStore {
private get store(): Promise<Store> { private get store(): Promise<Store> {
if (!this._store) { if (!this._store) {
this._store = createStore(this.path, this.options).catch( this._store = getOrCreateStore(this.path, this.options)
async () => (await getStore(this.path))!
)
} }
return this._store return this._store
} }
@ -174,6 +183,24 @@ export class Store extends Resource implements IStore {
return resourceId ? new Store(resourceId, path) : undefined 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<Store> {
const resourceId = await invoke<number>(
'plugin:store|get_or_create_store',
{
path,
...options
}
)
return new Store(resourceId, path)
}
async set(key: string, value: unknown): Promise<void> { async set(key: string, value: unknown): Promise<void> {
await invoke('plugin:store|set', { await invoke('plugin:store|set', {
rid: this.rid, rid: this.rid,

@ -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"]

@ -165,6 +165,32 @@ Denies the get command without any pre-configured scope.
<tr> <tr>
<td> <td>
`store:allow-get-or-create-store`
</td>
<td>
Enables the get_or_create_store command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`store:deny-get-or-create-store`
</td>
<td>
Denies the get_or_create_store command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`store:allow-get-store` `store:allow-get-store`
</td> </td>

@ -12,6 +12,8 @@ All operations are enabled by default.
""" """
permissions = [ permissions = [
"allow-create-store", "allow-create-store",
"allow-get-store",
"allow-get-or-create-store",
"allow-clear", "allow-clear",
"allow-delete", "allow-delete",
"allow-entries", "allow-entries",

@ -344,6 +344,16 @@
"type": "string", "type": "string",
"const": "deny-get" "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.", "description": "Enables the get_store command without any pre-configured scope.",
"type": "string", "type": "string",

@ -77,6 +77,19 @@ async fn get_store<R: Runtime>(app: AppHandle<R>, path: PathBuf) -> Option<Resou
stores.get(&path).copied() stores.get(&path).copied()
} }
#[tauri::command]
async fn get_or_create_store<R: Runtime>(
app: AppHandle<R>,
path: PathBuf,
auto_save: Option<AutoSave>,
) -> Result<ResourceId> {
if let Some(rid) = get_store(app.clone(), path.clone()).await {
Ok(rid)
} else {
create_store(app, path, auto_save).await
}
}
#[tauri::command] #[tauri::command]
async fn set<R: Runtime>( async fn set<R: Runtime>(
app: AppHandle<R>, app: AppHandle<R>,
@ -232,6 +245,7 @@ impl<R: Runtime> Builder<R> {
.invoke_handler(tauri::generate_handler![ .invoke_handler(tauri::generate_handler![
create_store, create_store,
get_store, get_store,
get_or_create_store,
set, set,
get, get,
has, has,

Loading…
Cancel
Save