begin implementing database. Down migrate doesnt work yet

This commit is contained in:
Nico
2025-09-15 22:44:16 +02:00
parent 0fe2aea028
commit 8e876d7206
7 changed files with 7879 additions and 154 deletions

6112
src-tauri/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -23,3 +23,7 @@ tauri-plugin-opener = "2"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
# SQLx und Tokio für asynchrone DB-Zugriffe
sqlx = { version = "0.7", features = ["runtime-tokio-rustls", "sqlite", "macros", "migrate"] }
tokio = { version = "1", features = ["full"] }

View File

@@ -0,0 +1,8 @@
-- migrate:up
CREATE TABLE PathNode (
id integer primary key,
title text,
description text
)
-- migrate:down

View File

@@ -0,0 +1,8 @@
-- migrate:up
create table NodeExercise (
id integer primary key,
ex_type text,
content text
)
-- migrate:down

View File

@@ -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/