From 05a4e24483d15347630c495980cb9563de7acae7 Mon Sep 17 00:00:00 2001 From: itsscb Date: Sat, 3 May 2025 22:27:34 +0200 Subject: [PATCH] feat: add initial db funcs --- src/db.rs | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 2 files changed, 84 insertions(+) create mode 100644 src/db.rs diff --git a/src/db.rs b/src/db.rs new file mode 100644 index 0000000..b8214b8 --- /dev/null +++ b/src/db.rs @@ -0,0 +1,83 @@ +use sqlx::{SqlitePool, migrate::Migrator}; +use uuid::Uuid; + +use crate::script::Script; + +static MIGRATOR: Migrator = sqlx::migrate!("./migrations"); + +// TODO: Add Custom Error Type +#[allow(dead_code)] +pub async fn init_db(pool: &SqlitePool) -> Result<(), String> { + MIGRATOR.run(pool).await.map_err(|e| e.to_string())?; + Ok(()) +} + +// TODO: Write the whole Script into the DB +// TODO: Add Custom Error type +#[allow(dead_code)] +pub async fn save_script(pool: &SqlitePool, script: &Script) -> Result<(), String> { + let id = Uuid::new_v4().to_string(); + let name = script.name(); + let path = script.path(); + sqlx::query!( + r#"INSERT INTO scripts (id, name, path) VALUES (?, ?, ?)"#, + id, + name, + path, + ) + .execute(pool) + .await + .map_err(|e| e.to_string())?; + + Ok(()) +} + +// TODO: Return Vec