nexus-graph-sdk 1.14.0

Official Rust SDK for Nexus graph database
Documentation
//! Batch operations for efficient bulk data operations

use crate::client::NexusClient;
use crate::error::{NexusError, Result};
use crate::models::Value;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Batch create nodes request
#[derive(Debug, Clone, Serialize)]
pub struct BatchCreateNodesRequest {
    /// List of nodes to create
    pub nodes: Vec<BatchNode>,
}

/// Batch node definition
#[derive(Debug, Clone, Serialize)]
pub struct BatchNode {
    /// Node labels
    pub labels: Vec<String>,
    /// Node properties
    #[serde(default)]
    pub properties: HashMap<String, Value>,
}

/// Batch create nodes response
#[derive(Debug, Clone, Deserialize)]
pub struct BatchCreateNodesResponse {
    /// List of created node IDs
    pub node_ids: Vec<u64>,
    /// Success message
    pub message: String,
    /// Error message if any
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
}

/// Batch create relationships request
#[derive(Debug, Clone, Serialize)]
pub struct BatchCreateRelationshipsRequest {
    /// List of relationships to create
    pub relationships: Vec<BatchRelationship>,
}

/// Batch relationship definition
#[derive(Debug, Clone, Serialize)]
pub struct BatchRelationship {
    /// Source node ID
    pub source_id: u64,
    /// Target node ID
    pub target_id: u64,
    /// Relationship type
    pub rel_type: String,
    /// Relationship properties
    #[serde(default)]
    pub properties: HashMap<String, Value>,
}

/// Batch create relationships response
#[derive(Debug, Clone, Deserialize)]
pub struct BatchCreateRelationshipsResponse {
    /// List of created relationship IDs
    pub rel_ids: Vec<u64>,
    /// Success message
    pub message: String,
    /// Error message if any
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
}

impl NexusClient {
    /// Batch create multiple nodes
    ///
    /// # Arguments
    ///
    /// * `nodes` - Vector of batch node definitions
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use nexus_sdk::{NexusClient, Value};
    /// # use std::collections::HashMap;
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), nexus_sdk::NexusError> {
    /// # let client = NexusClient::new("http://localhost:15474")?;
    /// let mut nodes = Vec::new();
    /// for i in 0..10 {
    ///     let mut properties = HashMap::new();
    ///     properties.insert("name".to_string(), Value::String(format!("Node{}", i)));
    ///     nodes.push(nexus_sdk::BatchNode {
    ///         labels: vec!["Person".to_string()],
    ///         properties,
    ///     });
    /// }
    /// let response = client.batch_create_nodes(nodes).await?;
    /// tracing::info!("Created {} nodes", response.node_ids.len());
    /// # Ok(())
    /// # }
    /// ```
    pub async fn batch_create_nodes(
        &self,
        nodes: Vec<BatchNode>,
    ) -> Result<BatchCreateNodesResponse> {
        // For now, create nodes sequentially
        // TODO: Implement proper batch endpoint if available
        let mut node_ids = Vec::new();
        let mut errors = Vec::new();

        for node in nodes {
            match self.create_node(node.labels, node.properties).await {
                Ok(response) => node_ids.push(response.node_id),
                Err(e) => errors.push(format!("Failed to create node: {}", e)),
            }
        }

        if !errors.is_empty() {
            return Err(NexusError::Validation(format!(
                "Some nodes failed to create: {}",
                errors.join(", ")
            )));
        }

        let node_count = node_ids.len();
        Ok(BatchCreateNodesResponse {
            node_ids,
            message: format!("Successfully created {} nodes", node_count),
            error: None,
        })
    }

    /// Batch create multiple relationships
    ///
    /// # Arguments
    ///
    /// * `relationships` - Vector of batch relationship definitions
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use nexus_sdk::{NexusClient, Value};
    /// # use std::collections::HashMap;
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), nexus_sdk::NexusError> {
    /// # let client = NexusClient::new("http://localhost:15474")?;
    /// let mut relationships = Vec::new();
    /// for i in 0..5 {
    ///     relationships.push(nexus_sdk::BatchRelationship {
    ///         source_id: i,
    ///         target_id: i + 1,
    ///         rel_type: "KNOWS".to_string(),
    ///         properties: HashMap::new(),
    ///     });
    /// }
    /// let response = client.batch_create_relationships(relationships).await?;
    /// tracing::info!("Created {} relationships", response.rel_ids.len());
    /// # Ok(())
    /// # }
    /// ```
    pub async fn batch_create_relationships(
        &self,
        relationships: Vec<BatchRelationship>,
    ) -> Result<BatchCreateRelationshipsResponse> {
        // For now, create relationships sequentially
        // TODO: Implement proper batch endpoint if available
        let mut rel_ids = Vec::new();
        let mut errors = Vec::new();

        for rel in relationships {
            match self
                .create_relationship(rel.source_id, rel.target_id, rel.rel_type, rel.properties)
                .await
            {
                Ok(response) => rel_ids.push(response.rel_id),
                Err(e) => errors.push(format!("Failed to create relationship: {}", e)),
            }
        }

        if !errors.is_empty() {
            return Err(NexusError::Validation(format!(
                "Some relationships failed to create: {}",
                errors.join(", ")
            )));
        }

        let rel_count = rel_ids.len();
        Ok(BatchCreateRelationshipsResponse {
            rel_ids,
            message: format!("Successfully created {} relationships", rel_count),
            error: None,
        })
    }
}