Skip to main content

solidb_client/client/
bulk.rs

1use super::SoliDBClient;
2use crate::protocol::{Command, DriverError, Response};
3use serde_json::Value;
4
5impl SoliDBClient {
6    pub async fn batch(&mut self, commands: Vec<Command>) -> Result<Vec<Response>, DriverError> {
7        let response = self.send_command(Command::Batch { commands }).await?;
8        match response {
9            Response::Batch { responses } => Ok(responses),
10            Response::Error { error } => Err(error),
11            _ => Err(DriverError::ProtocolError(
12                "Expected batch response".to_string(),
13            )),
14        }
15    }
16
17    pub async fn bulk_insert(
18        &mut self,
19        database: &str,
20        collection: &str,
21        documents: Vec<Value>,
22    ) -> Result<usize, DriverError> {
23        let response = self
24            .send_command(Command::BulkInsert {
25                database: database.to_string(),
26                collection: collection.to_string(),
27                documents,
28            })
29            .await?;
30
31        match response {
32            Response::Ok { count, .. } => Ok(count.unwrap_or(0)),
33            Response::Error { error } => Err(error),
34            _ => Err(DriverError::ProtocolError(
35                "Unexpected response".to_string(),
36            )),
37        }
38    }
39}