mirror of
https://github.com/perstarkse/minne.git
synced 2026-07-05 12:31:41 +02:00
refactoring surrealdb fns
This commit is contained in:
@@ -70,30 +70,6 @@ where
|
|||||||
Ok(matching_entities)
|
Ok(matching_entities)
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub async fn find_entities_by_relationship_by_source_ids(
|
|
||||||
// db_client: &Surreal<Client>,
|
|
||||||
// source_ids: &[String],
|
|
||||||
// ) -> Result<Vec<KnowledgeEntity>, ProcessingError> {
|
|
||||||
// let ids = source_ids
|
|
||||||
// .iter()
|
|
||||||
// .map(|id| format!("knowledge_entity:`{}`", id))
|
|
||||||
// .collect::<Vec<_>>()
|
|
||||||
// .join(", ");
|
|
||||||
|
|
||||||
// debug!("{:?}", ids);
|
|
||||||
|
|
||||||
// let query = format!(
|
|
||||||
// "SELECT *, <-> relates_to <-> knowledge_entity AS related FROM [{}]",
|
|
||||||
// ids
|
|
||||||
// );
|
|
||||||
|
|
||||||
// debug!("{}", query);
|
|
||||||
|
|
||||||
// let result: Vec<KnowledgeEntity> = db_client.query(query).await?.take(0)?;
|
|
||||||
|
|
||||||
// Ok(result)
|
|
||||||
// }
|
|
||||||
|
|
||||||
/// Find entities by their relationship to the id
|
/// Find entities by their relationship to the id
|
||||||
pub async fn find_entities_by_relationship_by_id(
|
pub async fn find_entities_by_relationship_by_id(
|
||||||
db_client: &Surreal<Client>,
|
db_client: &Surreal<Client>,
|
||||||
@@ -122,11 +98,3 @@ pub async fn get_entity_by_id(
|
|||||||
|
|
||||||
Ok(response)
|
Ok(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_all_stored_items<T>(db_client: &Surreal<Client>) -> Result<Vec<T>, ProcessingError>
|
|
||||||
where
|
|
||||||
T: for<'de> StoredObject,
|
|
||||||
{
|
|
||||||
let response: Vec<T> = db_client.select(T::table_name()).await?;
|
|
||||||
Ok(response)
|
|
||||||
}
|
|
||||||
|
|||||||
+17
-10
@@ -1,5 +1,3 @@
|
|||||||
use crate::error::ProcessingError;
|
|
||||||
|
|
||||||
use super::types::StoredObject;
|
use super::types::StoredObject;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use surrealdb::{
|
use surrealdb::{
|
||||||
@@ -46,12 +44,11 @@ impl SurrealDbClient {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn drop_table<T>(&self) -> Result<(), Error>
|
pub async fn drop_table<T>(&self) -> Result<Vec<T>, Error>
|
||||||
where
|
where
|
||||||
T: StoredObject + Send + Sync + 'static,
|
T: StoredObject + Send + Sync + 'static,
|
||||||
{
|
{
|
||||||
let _deleted: Vec<T> = self.client.delete(T::table_name()).await?;
|
self.client.delete(T::table_name()).await
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,10 +68,7 @@ impl Deref for SurrealDbClient {
|
|||||||
///
|
///
|
||||||
/// # Returns
|
/// # Returns
|
||||||
/// * `Result` - Item or Error
|
/// * `Result` - Item or Error
|
||||||
pub async fn store_item<T>(
|
pub async fn store_item<T>(db_client: &Surreal<Client>, item: T) -> Result<Option<T>, Error>
|
||||||
db_client: &Surreal<Client>,
|
|
||||||
item: T,
|
|
||||||
) -> Result<Option<T>, ProcessingError>
|
|
||||||
where
|
where
|
||||||
T: StoredObject + Send + Sync + 'static,
|
T: StoredObject + Send + Sync + 'static,
|
||||||
{
|
{
|
||||||
@@ -82,5 +76,18 @@ where
|
|||||||
.create((T::table_name(), item.get_id()))
|
.create((T::table_name(), item.get_id()))
|
||||||
.content(item)
|
.content(item)
|
||||||
.await
|
.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<T> or Error
|
||||||
|
pub async fn get_all_stored_items<T>(db_client: &Surreal<Client>) -> Result<Vec<T>, Error>
|
||||||
|
where
|
||||||
|
T: for<'de> StoredObject,
|
||||||
|
{
|
||||||
|
db_client.select(T::table_name()).await
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user