use super::core::DbOperations;
use crate::schema::SchemaError;
use uuid::Uuid;
impl DbOperations {
pub async fn get_node_id(&self) -> Result<String, SchemaError> {
use crate::storage::traits::TypedStore;
match self.metadata_store().get_item::<String>("node_id").await {
Ok(Some(id)) if !id.is_empty() => {
return Ok(id);
}
Ok(Some(_)) | Ok(None) => {
}
Err(e) => {
log::warn!(
"Failed to deserialize node_id (possibly old format): {}, creating new",
e
);
}
}
let new_id = Uuid::new_v4().to_string();
self.set_node_id(&new_id).await?;
Ok(new_id)
}
pub async fn set_node_id(&self, node_id: &str) -> Result<(), SchemaError> {
use crate::storage::traits::TypedStore;
self.metadata_store()
.put_item("node_id", &node_id.to_string())
.await
.map_err(|e| SchemaError::InvalidData(format!("Failed to set node_id: {}", e)))?;
self.metadata_store()
.inner()
.flush()
.await
.map_err(|e| SchemaError::InvalidData(format!("Failed to flush metadata: {}", e)))?;
Ok(())
}
pub async fn get_schema_permissions(&self, node_id: &str) -> Result<Vec<String>, SchemaError> {
use crate::storage::traits::TypedStore;
self.permissions_store()
.get_item::<Vec<String>>(node_id)
.await
.map_err(|e| SchemaError::InvalidData(format!("Failed to get permissions: {}", e)))
.map(|opt| opt.unwrap_or_default())
}
pub async fn set_schema_permissions(
&self,
node_id: &str,
schemas: &[String],
) -> Result<(), SchemaError> {
use crate::storage::traits::TypedStore;
let schemas_vec: Vec<String> = schemas.to_vec();
self.permissions_store()
.put_item(node_id, &schemas_vec)
.await
.map_err(|e| SchemaError::InvalidData(format!("Failed to set permissions: {}", e)))
}
pub async fn list_nodes_with_permissions(&self) -> Result<Vec<String>, SchemaError> {
use crate::storage::traits::TypedStore;
self.permissions_store()
.list_keys_with_prefix("")
.await
.map_err(|e| SchemaError::InvalidData(format!("Failed to list nodes: {}", e)))
}
pub async fn delete_schema_permissions(&self, node_id: &str) -> Result<bool, SchemaError> {
use crate::storage::traits::TypedStore;
self.permissions_store()
.delete_item(node_id)
.await
.map_err(|e| SchemaError::InvalidData(format!("Failed to delete permissions: {}", e)))
}
pub async fn node_has_permissions(&self, node_id: &str) -> Result<bool, SchemaError> {
use crate::storage::traits::TypedStore;
self.permissions_store()
.exists_item(node_id)
.await
.map_err(|e| SchemaError::InvalidData(format!("Failed to check permissions: {}", e)))
}
}