use super::*;
#[cfg(feature = "rayon")]
use super::results::par_min_records;
fn records_from_dtos<T: crate::record::ToGrpcRecord + Send>(
rows: Vec<T>,
target_class: &str,
) -> Vec<GrpcRecord> {
#[cfg(feature = "rayon")]
if rows.len() >= par_min_records() {
use rayon::prelude::*;
return rows
.into_par_iter()
.map(|row| row.to_grpc_record(target_class))
.collect();
}
rows.into_iter()
.map(|row| row.to_grpc_record(target_class))
.collect()
}
impl ArcadeDbClient {
pub async fn create_record(
&self,
database: &str,
class_name: &str,
props: Vec<(&str, crate::GrpcValue)>,
) -> Result<CreateRecordResponse> {
let req = CreateRecordRequest {
database: database.into(),
credentials: Some(self.auth.creds.clone()),
r#type: class_name.into(),
record: Some(crate::rec(class_name, props)),
transaction: None,
};
Ok(self
.data
.clone()
.create_record(self.add_auth_headers(tonic::Request::new(req)))
.await
.map_err(|e| map_rpc_error("create record", class_name, e))?
.into_inner())
}
pub async fn update_record_partial(
&self,
database: &str,
rid: &crate::Link,
props: impl Into<crate::encode::Params>,
) -> Result<bool> {
let req = UpdateRecordRequest {
database: database.into(),
credentials: Some(self.auth.creds.clone()),
rid: rid.to_rid_string(),
payload: Some(Payload::Partial(PropertiesUpdate {
properties: props.into().0,
})),
transaction: None,
};
let resp: UpdateRecordResponse = self
.data
.clone()
.update_record(self.add_auth_headers(tonic::Request::new(req)))
.await
.map_err(|e| map_rpc_error("update record", &rid.to_string(), e))?
.into_inner();
if !resp.success {
return Err(ArcadeDbError::CommandFailed {
operation: "update record".into(),
message: rid.to_string(),
affected: None,
});
}
Ok(resp.updated)
}
pub async fn delete_record(&self, database: &str, rid: &crate::Link) -> Result<bool> {
let req = DeleteRecordRequest {
database: database.into(),
rid: rid.to_rid_string(),
credentials: Some(self.auth.creds.clone()),
transaction: None,
};
let resp = self
.data
.clone()
.delete_record(self.add_auth_headers(tonic::Request::new(req)))
.await
.map_err(|e| map_rpc_error("delete record", &rid.to_string(), e))?
.into_inner();
if !resp.success {
return Err(ArcadeDbError::CommandFailed {
operation: format!("delete record {rid}"),
message: resp.message,
affected: None,
});
}
Ok(resp.deleted)
}
pub async fn bulk_insert(
&self,
database: &str,
target_class: &str,
key_columns: &[&str],
rows: Vec<GrpcRecord>,
) -> Result<InsertSummary> {
let creds = self.auth.creds.clone();
let req = BulkInsertRequest {
database: database.into(),
credentials: Some(creds.clone()),
options: Some(InsertOptions {
target_class: target_class.into(),
key_columns: key_columns.iter().map(|s| (*s).to_string()).collect(),
conflict_mode: ConflictMode::ConflictIgnore as i32,
database: database.into(),
credentials: Some(creds),
..Default::default()
}),
rows,
transaction: None,
};
let resp = self
.data
.clone()
.bulk_insert(self.add_auth_headers(tonic::Request::new(req)))
.await
.map_err(|e| map_rpc_error("bulk insert", target_class, e))?
.into_inner();
tracing::info!(
class = %target_class,
inserted = resp.inserted,
ignored = resp.ignored,
failed = resp.failed,
time_ms = resp.execution_time_ms,
"bulk insert"
);
Ok(resp)
}
pub async fn bulk_upsert(
&self,
database: &str,
target_class: &str,
key_columns: &[&str],
update_columns_on_conflict: &[&str],
rows: Vec<GrpcRecord>,
) -> Result<InsertSummary> {
let creds = self.auth.creds.clone();
let req = BulkInsertRequest {
database: database.into(),
credentials: Some(creds.clone()),
options: Some(InsertOptions {
target_class: target_class.into(),
key_columns: key_columns.iter().map(|s| (*s).to_string()).collect(),
conflict_mode: ConflictMode::ConflictUpdate as i32,
update_columns_on_conflict: update_columns_on_conflict
.iter()
.map(|s| (*s).to_string())
.collect(),
database: database.into(),
credentials: Some(creds),
..Default::default()
}),
rows,
transaction: None,
};
let resp = self
.data
.clone()
.bulk_insert(self.add_auth_headers(tonic::Request::new(req)))
.await
.map_err(|e| map_rpc_error("bulk upsert", target_class, e))?
.into_inner();
tracing::info!(
class = %target_class,
inserted = resp.inserted,
updated = resp.updated,
ignored = resp.ignored,
failed = resp.failed,
time_ms = resp.execution_time_ms,
"bulk upsert"
);
Ok(resp)
}
pub async fn bulk_upsert_dtos<T>(
&self,
database: &str,
target_class: &str,
rows: Vec<T>,
) -> Result<InsertSummary>
where
T: crate::record::ToGrpcRecord + Send,
{
let keys = T::KEY_COLUMNS;
if keys.is_empty() {
return Err(ArcadeDbError::InvalidInput {
message: format!(
"bulk_upsert_dtos: `{}` declares no #[record(key)] columns — \
the server cannot detect conflicts without a key",
std::any::type_name::<T>(),
),
});
}
let records = records_from_dtos(rows, target_class);
self.bulk_upsert(database, target_class, keys, &[], records)
.await
}
}