feat(db): enable db to update items keeping track of the item's id

This commit is contained in:
ryan 2025-01-24 01:54:25 +03:00
parent 3ed12c80a6
commit fc13b0dae6

View file

@ -16,7 +16,7 @@ mod db_impl {
// Create a new database connection // Create a new database connection
pub fn new(db_path: &str) -> Result<Self, Error> { pub fn new(db_path: &str) -> Result<Self, Error> {
let conn = Connection::open(db_path)?; let conn = Connection::open(db_path)?;
logging::log!("Database connection established at: {}", db_path); // Log with Leptos logging::log!("Database connection established at: {}", db_path);
Ok(Database { Ok(Database {
conn: Arc::new(Mutex::new(conn)), conn: Arc::new(Mutex::new(conn)),
}) })
@ -34,7 +34,7 @@ mod db_impl {
custom_properties TEXT custom_properties TEXT
);", );",
)?; )?;
logging::log!("Database schema created or verified"); // Log with Leptos logging::log!("Database schema created or verified");
Ok(()) Ok(())
} }
@ -43,7 +43,13 @@ mod db_impl {
let conn = self.conn.lock().await; let conn = self.conn.lock().await;
let wikidata_id = item.wikidata_id.as_ref().map(|s| s.as_str()).unwrap_or(""); let wikidata_id = item.wikidata_id.as_ref().map(|s| s.as_str()).unwrap_or("");
conn.execute( conn.execute(
"INSERT INTO items (id, name, description, wikidata_id, custom_properties) VALUES (?, ?, ?, ?, ?);", "INSERT INTO items (id, name, description, wikidata_id, custom_properties)
VALUES (?, ?, ?, ?, ?)
ON CONFLICT(id) DO UPDATE SET
name = excluded.name,
description = excluded.description,
wikidata_id = excluded.wikidata_id,
custom_properties = excluded.custom_properties;",
&[ &[
&item.id, &item.id,
&item.name, &item.name,
@ -52,7 +58,7 @@ mod db_impl {
&item.custom_properties, &item.custom_properties,
], ],
)?; )?;
logging::log!("Item inserted: {}", item.id); // Log with Leptos logging::log!("Item inserted: {}", item.id);
Ok(()) Ok(())
} }