refactoring: working macro and generics

This commit is contained in:
Per Stark
2024-11-20 22:44:30 +01:00
parent 838aa4c1ec
commit fc20526789
11 changed files with 198 additions and 167 deletions
+27
View File
@@ -0,0 +1,27 @@
use surrealdb::{engine::remote::ws::Client, Surreal};
use crate::error::ProcessingError;
use super::types::StoredObject;
/// Operation to store a object in SurrealDB, requires the struct to implement StoredObject
///
/// # Arguments
/// * `db_client` - A initialized database client
/// * `item` - The item to be stored
///
/// # Returns
/// * `Result` - Item or Error
pub async fn store_item<T>(
db_client: &Surreal<Client>,
item: T,
) -> Result<Option<T>, ProcessingError>
where
T: StoredObject + Send + Sync + 'static,
{
db_client
.create((T::table_name(), item.get_id()))
.content(item)
.await
.map_err(ProcessingError::from)
}