begin implementing database. Down migrate doesnt work yet
This commit is contained in:
1679
package-lock.json
generated
Normal file
1679
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
6112
src-tauri/Cargo.lock
generated
Normal file
6112
src-tauri/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -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"] }
|
||||
|
||||
|
||||
8
src-tauri/migrations/0001_path_node_table.sql
Normal file
8
src-tauri/migrations/0001_path_node_table.sql
Normal file
@@ -0,0 +1,8 @@
|
||||
-- migrate:up
|
||||
CREATE TABLE PathNode (
|
||||
id integer primary key,
|
||||
title text,
|
||||
description text
|
||||
)
|
||||
|
||||
-- migrate:down
|
||||
8
src-tauri/migrations/0002_node_exercise.sql
Normal file
8
src-tauri/migrations/0002_node_exercise.sql
Normal file
@@ -0,0 +1,8 @@
|
||||
-- migrate:up
|
||||
create table NodeExercise (
|
||||
id integer primary key,
|
||||
ex_type text,
|
||||
content text
|
||||
)
|
||||
|
||||
-- migrate:down
|
||||
@@ -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/
|
||||
|
||||
184
src/App.vue
184
src/App.vue
@@ -1,160 +1,38 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from "vue";
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
|
||||
const greetMsg = ref("");
|
||||
const name = ref("");
|
||||
import { ref } from '@vue/reactivity';
|
||||
// @ts-ignore
|
||||
import { invoke } from '@tauri-apps/api/core';
|
||||
|
||||
async function greet() {
|
||||
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
|
||||
greetMsg.value = await invoke("greet", { name: name.value });
|
||||
const dbVersion = ref<string | null>(null);
|
||||
const loading = ref(false);
|
||||
const error = ref<string | null>(null);
|
||||
|
||||
async function fetchDbVersion() {
|
||||
loading.value = true;
|
||||
error.value = null;
|
||||
try {
|
||||
dbVersion.value = await invoke<string>('db_version');
|
||||
} catch (e: any) {
|
||||
error.value = e?.toString() ?? 'Unbekannter Fehler';
|
||||
dbVersion.value = null;
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<main class="container">
|
||||
<h1>Welcome to Tauri + Vue</h1>
|
||||
|
||||
<div class="row">
|
||||
<a href="https://vite.dev" target="_blank">
|
||||
<img src="/vite.svg" class="logo vite" alt="Vite logo" />
|
||||
</a>
|
||||
<a href="https://tauri.app" target="_blank">
|
||||
<img src="/tauri.svg" class="logo tauri" alt="Tauri logo" />
|
||||
</a>
|
||||
<a href="https://vuejs.org/" target="_blank">
|
||||
<img src="./assets/vue.svg" class="logo vue" alt="Vue logo" />
|
||||
</a>
|
||||
</div>
|
||||
<p>Click on the Tauri, Vite, and Vue logos to learn more.</p>
|
||||
|
||||
<form class="row" @submit.prevent="greet">
|
||||
<input id="greet-input" v-model="name" placeholder="Enter a name..." />
|
||||
<button type="submit">Greet</button>
|
||||
</form>
|
||||
<p>{{ greetMsg }}</p>
|
||||
</main>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.logo.vite:hover {
|
||||
filter: drop-shadow(0 0 2em #747bff);
|
||||
}
|
||||
|
||||
.logo.vue:hover {
|
||||
filter: drop-shadow(0 0 2em #249b73);
|
||||
}
|
||||
|
||||
</style>
|
||||
<style>
|
||||
:root {
|
||||
font-family: Inter, Avenir, Helvetica, Arial, sans-serif;
|
||||
font-size: 16px;
|
||||
line-height: 24px;
|
||||
font-weight: 400;
|
||||
|
||||
color: #0f0f0f;
|
||||
background-color: #f6f6f6;
|
||||
|
||||
font-synthesis: none;
|
||||
text-rendering: optimizeLegibility;
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
-webkit-text-size-adjust: 100%;
|
||||
}
|
||||
|
||||
.container {
|
||||
margin: 0;
|
||||
padding-top: 10vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.logo {
|
||||
height: 6em;
|
||||
padding: 1.5em;
|
||||
will-change: filter;
|
||||
transition: 0.75s;
|
||||
}
|
||||
|
||||
.logo.tauri:hover {
|
||||
filter: drop-shadow(0 0 2em #24c8db);
|
||||
}
|
||||
|
||||
.row {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
a {
|
||||
font-weight: 500;
|
||||
color: #646cff;
|
||||
text-decoration: inherit;
|
||||
}
|
||||
|
||||
a:hover {
|
||||
color: #535bf2;
|
||||
}
|
||||
|
||||
h1 {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
input,
|
||||
button {
|
||||
border-radius: 8px;
|
||||
border: 1px solid transparent;
|
||||
padding: 0.6em 1.2em;
|
||||
font-size: 1em;
|
||||
font-weight: 500;
|
||||
font-family: inherit;
|
||||
color: #0f0f0f;
|
||||
background-color: #ffffff;
|
||||
transition: border-color 0.25s;
|
||||
box-shadow: 0 2px 2px rgba(0, 0, 0, 0.2);
|
||||
}
|
||||
|
||||
button {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
border-color: #396cd8;
|
||||
}
|
||||
button:active {
|
||||
border-color: #396cd8;
|
||||
background-color: #e8e8e8;
|
||||
}
|
||||
|
||||
input,
|
||||
button {
|
||||
outline: none;
|
||||
}
|
||||
|
||||
#greet-input {
|
||||
margin-right: 5px;
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:root {
|
||||
color: #f6f6f6;
|
||||
background-color: #2f2f2f;
|
||||
}
|
||||
|
||||
a:hover {
|
||||
color: #24c8db;
|
||||
}
|
||||
|
||||
input,
|
||||
button {
|
||||
color: #ffffff;
|
||||
background-color: #0f0f0f98;
|
||||
}
|
||||
button:active {
|
||||
background-color: #0f0f0f69;
|
||||
}
|
||||
}
|
||||
|
||||
</style>
|
||||
<div style="display: flex; flex-direction: column; align-items: center; gap: 1rem; margin-top: 2rem;">
|
||||
<button @click="fetchDbVersion" :disabled="loading">
|
||||
{{ loading ? 'Lade...' : 'Datenbank-Version abfragen' }}
|
||||
</button>
|
||||
<div v-if="dbVersion">
|
||||
<strong>SQLite-Version:</strong> {{ dbVersion }}
|
||||
</div>
|
||||
<div v-if="error" style="color: red;">
|
||||
Fehler: {{ error }}
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
Reference in New Issue
Block a user