begin implementing database. Down migrate doesnt work yet
This commit is contained in:
@@ -1,14 +1,50 @@
|
||||
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
|
||||
use sqlx::sqlite::{self, SqlitePool};
|
||||
|
||||
#[tauri::command]
|
||||
fn greet(name: &str) -> String {
|
||||
format!("Hello, {}! You've been greeted from Rust!", name)
|
||||
}
|
||||
|
||||
// Beispiel: SQLite-Version abfragen
|
||||
#[tauri::command]
|
||||
async fn db_version(app_handle: tauri::AppHandle) -> Result<String, String> {
|
||||
use tauri::Manager;
|
||||
let base_dir = app_handle.path().app_data_dir().map_err(|e| format!("Konnte AppData-Verzeichnis nicht bestimmen: {}", e))?;
|
||||
|
||||
let db_path = base_dir.join("paths.sqlite");
|
||||
println!("{:?}", db_path.to_str());
|
||||
|
||||
if let Some(parent) = db_path.parent() {
|
||||
std::fs::create_dir_all(parent).map_err(|e| format!("Konnte Datenbankverzeichnis nicht erstellen: {}", e))?;
|
||||
}
|
||||
|
||||
let db_opts = sqlite::SqliteConnectOptions::new()
|
||||
.filename(&db_path)
|
||||
.create_if_missing(true);
|
||||
|
||||
let pool = SqlitePool::connect_with(db_opts)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
sqlx::migrate!("./migrations").run(&pool).await.map_err(|e| e.to_string())?;
|
||||
|
||||
let row: (String,) = sqlx::query_as("SELECT sqlite_version()")
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(row.0)
|
||||
}
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
.invoke_handler(tauri::generate_handler![greet])
|
||||
.invoke_handler(tauri::generate_handler![greet, db_version])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
|
||||
// Hinweis: Für produktive Nutzung sollte der Pool als State eingebunden werden,
|
||||
// damit er nicht bei jedem Aufruf neu erstellt wird. Siehe Tauri-Doku:
|
||||
// https://tauri.app/v1/guides/features/state/
|
||||
|
||||
Reference in New Issue
Block a user