feat(sql): improved sqlite load path

pull/2486/head
w33d 5 months ago
parent 02df0421d6
commit f6b9d2b7e4

@ -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<Database> {
const _path = await invoke<string>('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 {

@ -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?;

Loading…
Cancel
Save