use crate::db::get_neo4j_graph;
use crate::types::DynError;
use neo4rs::Query;
use serde::de::DeserializeOwned;
#[derive(Debug)]
pub enum OperationOutcome {
Updated,
CreatedOrDeleted,
MissingDependency,
}
pub async fn execute_graph_operation(query: Query) -> Result<OperationOutcome, DynError> {
let mut result;
{
let graph = get_neo4j_graph()?;
let graph = graph.lock().await;
result = graph.execute(query).await?;
}
match result.next().await? {
Some(row) => match row.get("flag")? {
true => Ok(OperationOutcome::Updated),
false => Ok(OperationOutcome::CreatedOrDeleted),
},
None => Ok(OperationOutcome::MissingDependency),
}
}
pub async fn exec_single_row(query: Query) -> Result<(), DynError> {
let graph = get_neo4j_graph()?;
let graph = graph.lock().await;
let mut result = graph.execute(query).await?;
result.next().await?;
Ok(())
}
pub async fn retrieve_from_graph<T>(query: Query, key: &str) -> Result<Option<T>, DynError>
where
T: DeserializeOwned + Send + Sync,
{
let mut result;
{
let graph = get_neo4j_graph()?;
let graph = graph.lock().await;
result = graph.execute(query).await?;
}
if let Some(row) = result.next().await? {
let data: T = row.get(key)?;
return Ok(Some(data));
}
Ok(None)
}