From 1cd26061d7ea01e37ac5f44f423270e0eb597f38 Mon Sep 17 00:00:00 2001 From: Per Stark Date: Thu, 28 Nov 2024 08:26:58 +0100 Subject: [PATCH] refactoring surrealdb fns --- src/retrieval/graph.rs | 32 -------------------------------- src/storage/db.rs | 27 +++++++++++++++++---------- 2 files changed, 17 insertions(+), 42 deletions(-) diff --git a/src/retrieval/graph.rs b/src/retrieval/graph.rs index ed13376..8157ef4 100644 --- a/src/retrieval/graph.rs +++ b/src/retrieval/graph.rs @@ -70,30 +70,6 @@ where Ok(matching_entities) } -// pub async fn find_entities_by_relationship_by_source_ids( -// db_client: &Surreal, -// source_ids: &[String], -// ) -> Result, ProcessingError> { -// let ids = source_ids -// .iter() -// .map(|id| format!("knowledge_entity:`{}`", id)) -// .collect::>() -// .join(", "); - -// debug!("{:?}", ids); - -// let query = format!( -// "SELECT *, <-> relates_to <-> knowledge_entity AS related FROM [{}]", -// ids -// ); - -// debug!("{}", query); - -// let result: Vec = db_client.query(query).await?.take(0)?; - -// Ok(result) -// } - /// Find entities by their relationship to the id pub async fn find_entities_by_relationship_by_id( db_client: &Surreal, @@ -122,11 +98,3 @@ pub async fn get_entity_by_id( Ok(response) } - -pub async fn get_all_stored_items(db_client: &Surreal) -> Result, ProcessingError> -where - T: for<'de> StoredObject, -{ - let response: Vec = db_client.select(T::table_name()).await?; - Ok(response) -} diff --git a/src/storage/db.rs b/src/storage/db.rs index 1757a5b..005c702 100644 --- a/src/storage/db.rs +++ b/src/storage/db.rs @@ -1,5 +1,3 @@ -use crate::error::ProcessingError; - use super::types::StoredObject; use std::ops::Deref; use surrealdb::{ @@ -46,12 +44,11 @@ impl SurrealDbClient { Ok(()) } - pub async fn drop_table(&self) -> Result<(), Error> + pub async fn drop_table(&self) -> Result, Error> where T: StoredObject + Send + Sync + 'static, { - let _deleted: Vec = self.client.delete(T::table_name()).await?; - Ok(()) + self.client.delete(T::table_name()).await } } @@ -71,10 +68,7 @@ impl Deref for SurrealDbClient { /// /// # Returns /// * `Result` - Item or Error -pub async fn store_item( - db_client: &Surreal, - item: T, -) -> Result, ProcessingError> +pub async fn store_item(db_client: &Surreal, item: T) -> Result, Error> where T: StoredObject + Send + Sync + 'static, { @@ -82,5 +76,18 @@ where .create((T::table_name(), item.get_id())) .content(item) .await - .map_err(ProcessingError::from) +} + +/// Operation to retrieve all objects from a certain table, requires the struct to implement StoredObject +/// +/// # Arguments +/// * `db_client` - A initialized database client +/// +/// # Returns +/// * `Result` - Vec or Error +pub async fn get_all_stored_items(db_client: &Surreal) -> Result, Error> +where + T: for<'de> StoredObject, +{ + db_client.select(T::table_name()).await }