chore: clean & refactor

This commit is contained in:
Per Stark
2024-11-28 11:25:00 +01:00
parent 1cd26061d7
commit 5c2ab58566
4 changed files with 27 additions and 50 deletions

View File

@@ -1,10 +1,7 @@
use surrealdb::{engine::remote::ws::Client, Surreal};
use surrealdb::{engine::remote::ws::Client, Error, Surreal};
use tracing::debug;
use crate::{
error::ProcessingError,
storage::types::{knowledge_entity::KnowledgeEntity, StoredObject},
};
use crate::storage::types::{knowledge_entity::KnowledgeEntity, StoredObject};
/// Retrieves database entries that match a specific source identifier.
///
@@ -26,11 +23,11 @@ use crate::{
///
/// Returns a `Result` containing either:
/// * `Ok(Vec<T>)` - A vector of matching records deserialized into type `T`
/// * `Err(ProcessingError)` - An error if the database query fails
/// * `Err(Error)` - An error if the database query fails
///
/// # Errors
///
/// This function will return a `ProcessingError` if:
/// This function will return a `Error` if:
/// * The database query fails to execute
/// * The results cannot be deserialized into type `T`
///
@@ -54,27 +51,25 @@ pub async fn find_entities_by_source_ids<T>(
source_id: Vec<String>,
table_name: String,
db_client: &Surreal<Client>,
) -> Result<Vec<T>, ProcessingError>
) -> Result<Vec<T>, Error>
where
T: for<'de> serde::Deserialize<'de>,
{
let query = "SELECT * FROM type::table($table) WHERE source_id IN $source_ids";
let matching_entities: Vec<T> = db_client
db_client
.query(query)
.bind(("table", table_name))
.bind(("source_ids", source_id))
.await?
.take(0)?;
Ok(matching_entities)
.take(0)
}
/// Find entities by their relationship to the id
pub async fn find_entities_by_relationship_by_id(
db_client: &Surreal<Client>,
entity_id: String,
) -> Result<Vec<KnowledgeEntity>, ProcessingError> {
) -> Result<Vec<KnowledgeEntity>, Error> {
let query = format!(
"SELECT *, <-> relates_to <-> knowledge_entity AS related FROM knowledge_entity:`{}`",
entity_id
@@ -82,19 +77,15 @@ pub async fn find_entities_by_relationship_by_id(
debug!("{}", query);
let result: Vec<KnowledgeEntity> = db_client.query(query).await?.take(0)?;
Ok(result)
db_client.query(query).await?.take(0)
}
/// Get a specific KnowledgeEntity by its id
pub async fn get_entity_by_id(
db_client: &Surreal<Client>,
entity_id: &str,
) -> Result<Option<KnowledgeEntity>, ProcessingError> {
let response: Option<KnowledgeEntity> = db_client
) -> Result<Option<KnowledgeEntity>, Error> {
db_client
.select((KnowledgeEntity::table_name(), entity_id))
.await?;
Ok(response)
.await
}