import { invoke, InvokeArgs } from "@tauri-apps/api/core"; import { Result } from "./types"; async function exec( command: string, args?: InvokeArgs ): Promise> { try { return { success: true, data: await invoke(`plugin:polodb|${command}`, args), }; } catch (e) { try { return { success: false, error: Object.keys(e as object)[0], context: (e as { [key: string]: any })[ Object.keys(e as object)[0] ], }; } catch (u) { return { success: false, error: "unknown", context: u, }; } } } export async function list_databases(): Promise> { return await exec("list_databases"); } export async function list_collections( database: string ): Promise> { return await exec("list_collections", { database }); } export async function open_database( key: string, path: string ): Promise> { return await exec("open_database", { key, path }); } export async function close_database(key: string): Promise> { return await exec("close_database", { key }); } export async function insert( database: string, collection: string, documents: T[] ): Promise> { return await exec("insert", { database, collection, documents }); } export async function insert_one( database: string, collection: string, document: T ): Promise> { return await exec("insert_one", { database, collection, document }); } export async function find_many< Document extends object = any, Query extends object = any, Sorting extends object = any >( database: string, collection: string, query: Query, sorting?: Sorting | null ): Promise> { return await exec("find", { database, collection, query, sorting: sorting ?? null, }); } export async function find_all< Document extends object = any, Sorting extends object = any >( database: string, collection: string, sorting?: Sorting | null ): Promise> { return await exec("find_all", { database, collection, sorting: sorting ?? null, }); } export async function find_one< Document extends object = any, Query extends object = any >( database: string, collection: string, query: Query ): Promise> { return await exec("find_one", { database, collection, query, }); } export async function delete_many( database: string, collection: string, query: Query ): Promise> { return await exec("delete", { database, collection, query, }); } export async function delete_all( database: string, collection: string ): Promise> { return await exec("delete_all", { database, collection, }); } export async function delete_one( database: string, collection: string, query: Query ): Promise> { return await exec("delete_one", { database, collection, query, }); } export async function update_many< Query extends object = any, Update extends object = any >( database: string, collection: string, query: Query, update: Update, upsert?: boolean ): Promise> { return await exec("update", { database, collection, query, update, upsert: upsert ?? false, }); } export async function update_all( database: string, collection: string, update: Update, upsert?: boolean ): Promise> { return await exec("update_all", { database, collection, update, upsert: upsert ?? false, }); } export async function update_one< Query extends object = any, Update extends object = any >( database: string, collection: string, query: Query, update: Update, upsert?: boolean ): Promise> { return await exec("update_one", { database, collection, query, update, upsert: upsert ?? false, }); }