From f6b9d2b7e4ef5cec028bfdf954dd9db77fddfa1f Mon Sep 17 00:00:00 2001 From: w33d Date: Mon, 3 Mar 2025 19:46:01 +0800 Subject: [PATCH] feat(sql): improved sqlite load path --- plugins/sql/guest-js/index.ts | 18 +++++++++++++++--- plugins/sql/src/wrapper.rs | 19 +++++++++++++------ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/plugins/sql/guest-js/index.ts b/plugins/sql/guest-js/index.ts index 11d39e70..f2d6b8fe 100644 --- a/plugins/sql/guest-js/index.ts +++ b/plugins/sql/guest-js/index.ts @@ -38,12 +38,18 @@ export default class Database { * * # Sqlite * - * The path is relative to `tauri::path::BaseDirectory::App` and must start with `sqlite:`. + * The path is relative to `tauri::path::BaseDirectory::App` by default and must start with `sqlite:`. * * @example * ```ts + * // for relative path * const db = await Database.load("sqlite:test.db"); * ``` + * @example + * ```ts + * // for absolute path + * const db = await Database.load("sqlite:/home/ubuntu/test.db"); + * ``` */ static async load(path: string): Promise { const _path = await invoke('plugin:sql|load', { @@ -62,11 +68,17 @@ export default class Database { * * # Sqlite * - * The path is relative to `tauri::path::BaseDirectory::App` and must start with `sqlite:`. + * The path is relative to `tauri::path::BaseDirectory::App` by default and must start with `sqlite:`. * * @example * ```ts - * const db = Database.get("sqlite:test.db"); + * // for relative path + * const db = await Database.load("sqlite:test.db"); + * ``` + * @example + * ```ts + * // for absolute path + * const db = await Database.load("sqlite:/home/ubuntu/test.db"); * ``` */ static get(path: string): Database { diff --git a/plugins/sql/src/wrapper.rs b/plugins/sql/src/wrapper.rs index d47b2d1c..714c66f5 100644 --- a/plugins/sql/src/wrapper.rs +++ b/plugins/sql/src/wrapper.rs @@ -4,6 +4,7 @@ #[cfg(feature = "sqlite")] use std::fs::create_dir_all; +use std::path::Path; use indexmap::IndexMap; use serde_json::Value as JsonValue; @@ -76,14 +77,20 @@ impl DbPool { { #[cfg(feature = "sqlite")] "sqlite" => { - let app_path = _app - .path() - .app_config_dir() - .expect("No App config path was found!"); + let conn_url = if Path::new(conn_url).is_absolute() { + create_dir_all(conn_url).expect("Couldn't create dir"); - create_dir_all(&app_path).expect("Couldn't create app config dir"); + conn_url + } else { + let app_path = _app + .path() + .app_config_dir() + .expect("No App config path was found!"); + + create_dir_all(&app_path).expect("Couldn't create app config dir"); - let conn_url = &path_mapper(app_path, conn_url); + &path_mapper(app_path, conn_url) + }; if !Sqlite::database_exists(conn_url).await.unwrap_or(false) { Sqlite::create_database(conn_url).await?;