conundrum_db 0.1.2

Be less dum.
Documentation
use std::{fmt::Debug, sync::Arc};

use arrow_array::RecordBatchIterator;
use conundrum_lang::ecosystem::{
    db::{
        db_client::db_client::DBClient,
        db_traits::db_entity::{DBEntity, DBSchema},
        helpers::open_table::open_table,
    },
    error_handling::db_error::{DatabaseError, DatabaseResult},
};
use serde::Serialize;

pub async fn save_entities<'a, T, IDType>(items: Vec<T>, db: DBClient) -> DatabaseResult<()>
    where T: DBSchema + DBEntity + Clone + Debug + Serialize {
    let schema = <T as DBSchema>::schema().map(Arc::new)?;
    let _db = db.inner_arc().lock_owned().await;
    let table = <T as DBEntity>::table();
    let tbl = open_table(_db, table.clone()).await.inspect_err(|e| {
                                                       log::error!("Table Error: {:?}", e);
                                                   })?;
    let batches = <T as DBSchema>::get_record_batch(items.clone()).inspect_err(|e| {
                                                                      log::error!("get_record_batch Error: {:?}", e);
                                                                  })?;
    let stream = Box::new(RecordBatchIterator::new(vec![batches].into_iter().map(Ok), schema.clone()));
    let primary_key: &[&str] = <T as DBSchema>::merge_keys();
    tbl.merge_insert(primary_key)
       .when_matched_update_all(None)
       .when_not_matched_insert_all()
       .clone()
       .execute(stream)
       .await
       .map_err(|e| {
           log::error!("Database Error: {:?}", e);
           DatabaseError::FailToCreateEntity(T::table().to_model_name())
       })?;
    log::info!("Successfully saved {} `{}` models", items.len(), table.to_model_name());
    Ok(())
}