use std::sync::Arc;
use serde_json::{json, Value};
use tauri::{Runtime, State, Webview};
use super::keys::KeyRangeArg;
use super::{Caller, OpenRequest, Opened, ReadRequest, ReadResult, StoreSchema, WriteOp};
use crate::error::Result;
use crate::Shared;
fn who<R: Runtime>(shared: &Shared, webview: &Webview<R>) -> Result<Caller> {
let instance = crate::commands::caller(shared, webview)?;
Ok(Caller { label: instance.label.clone(), instance: instance.id, engine: instance.engine })
}
#[tauri::command(rename_all = "snake_case")]
pub async fn db_open<R: Runtime>(
webview: Webview<R>,
state: State<'_, Arc<Shared>>,
app_key: String,
username: String,
version: u32,
schema: Vec<StoreSchema>,
encrypt: Option<bool>,
) -> Result<Opened> {
let caller = who(&state, &webview)?;
let req = OpenRequest { app_key, username, version, schema, encrypt: encrypt.unwrap_or(false) };
state.store.open(&caller, req).await
}
#[tauri::command(rename_all = "snake_case")]
pub fn db_close<R: Runtime>(webview: Webview<R>, state: State<'_, Arc<Shared>>, db: u64) -> Result<()> {
let caller = who(&state, &webview)?;
state.store.close(&caller, db);
Ok(())
}
#[tauri::command(rename_all = "snake_case")]
pub async fn db_delete<R: Runtime>(webview: Webview<R>, state: State<'_, Arc<Shared>>, app_key: String, username: String) -> Result<()> {
crate::commands::require_engine(&webview)?;
state.store.delete(&app_key, &username).await?;
state.hub.broadcast(json!({ "t": "db_deleted", "app_key": app_key, "username": username.to_lowercase() }));
Ok(())
}
#[tauri::command(rename_all = "snake_case")]
pub async fn tx_begin<R: Runtime>(
webview: Webview<R>,
state: State<'_, Arc<Shared>>,
db: u64,
stores: Vec<String>,
mode: String,
durability: Option<String>,
) -> Result<Value> {
let caller = who(&state, &webview)?;
let tx = state.store.begin(&caller, db, stores, &mode, durability.as_deref()).await?;
Ok(json!({ "tx": tx }))
}
#[allow(clippy::too_many_arguments)]
#[tauri::command(rename_all = "snake_case")]
pub async fn tx_read<R: Runtime>(
webview: Webview<R>,
state: State<'_, Arc<Shared>>,
tx: u64,
op: String,
store: String,
key: Option<Value>,
index: Option<String>,
range: Option<KeyRangeArg>,
limit: Option<u64>,
direction: Option<String>,
cursor: Option<String>,
) -> Result<ReadResult> {
let caller = who(&state, &webview)?;
let req = ReadRequest { op, store, key, index, range, limit, direction, cursor };
state.store.read(&caller, tx, req).await
}
#[tauri::command(rename_all = "snake_case")]
pub async fn tx_write<R: Runtime>(webview: Webview<R>, state: State<'_, Arc<Shared>>, tx: u64, ops: Vec<WriteOp>) -> Result<()> {
let caller = who(&state, &webview)?;
state.store.write(&caller, tx, ops).await
}
#[tauri::command(rename_all = "snake_case")]
pub async fn tx_commit<R: Runtime>(webview: Webview<R>, state: State<'_, Arc<Shared>>, tx: u64, ops: Option<Vec<WriteOp>>) -> Result<()> {
let caller = who(&state, &webview)?;
state.store.commit(&caller, tx, ops.unwrap_or_default()).await
}
#[tauri::command(rename_all = "snake_case")]
pub fn tx_abort<R: Runtime>(webview: Webview<R>, state: State<'_, Arc<Shared>>, tx: u64) -> Result<()> {
let caller = who(&state, &webview)?;
state.store.abort(&caller, tx);
Ok(())
}