use crate::db::{
connect_to_db, create_document, create_user, find_documents, find_one_document, get_all_users,
get_document_by_id, get_user_by_name, SaveFileRequest, SearchOptions,
};
use serde_json::Value;
use tauri::command;
#[command(rename_all = "camelCase")]
pub async fn connect(url: String, db_name: Option<String>) -> Result<(), String> {
connect_to_db(url, db_name).await
}
#[command]
pub async fn create(collection: String, document: Value) -> Result<Value, String> {
create_document(collection, document).await
}
#[command]
pub async fn get_by_id(collection: String, id: String) -> Result<Option<Value>, String> {
get_document_by_id(collection, id).await
}
#[command]
pub async fn get_users() -> Result<Vec<Value>, String> {
get_all_users().await
}
#[command]
pub async fn get_user(username: String, db: Option<String>) -> Result<Option<Value>, String> {
get_user_by_name(username, db).await
}
#[command(rename_all = "camelCase")]
pub async fn create_db_user(
username: String,
password: String,
db: String,
roles: Vec<Value>,
custom_data: Option<Value>,
) -> Result<Value, String> {
create_user(username, password, db, roles, custom_data).await
}
#[command]
pub async fn find(
collection: String,
filter: Option<Value>,
options: Option<SearchOptions>,
) -> Result<Vec<Value>, String> {
find_documents(collection, filter, options).await
}
#[command(rename_all = "camelCase")]
pub async fn find_one(
collection: String,
filter: Option<Value>,
options: Option<SearchOptions>,
) -> Result<Option<Value>, String> {
find_one_document(collection, filter, options).await
}
#[command(rename_all = "camelCase")]
pub async fn update_one(
collection: String,
filter: Value,
update: Value,
options: Option<Value>,
) -> Result<Value, String> {
crate::db::update_one_document(collection, filter, update, options).await
}
#[command(rename_all = "camelCase")]
pub async fn save_file(
collection: String,
path: Option<Value>,
data: Option<Vec<u8>>,
filename: Option<String>,
metadata: Option<Value>,
chunk_size_bytes: Option<u32>,
) -> Result<Value, String> {
let payload = SaveFileRequest {
collection,
path,
data,
filename,
metadata,
chunk_size_bytes,
};
crate::db::save_file(payload).await
}