use arangors::client::reqwest::ReqwestClient;
use arangors::{
collection::{options::CreateOptions, Collection, CollectionType},
ClientError, Database,
};
use serde::{Deserialize, Serialize};
use crate::schema::SchemaDatabaseOperation;
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct CollectionSchema {
pub name: String,
pub is_edge_collection: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub wait_for_sync: Option<bool>,
}
impl CollectionSchema {
pub fn new(name: &str, is_edge_collection: bool, wait_for_sync: Option<bool>) -> Self {
Self {
name: name.to_string(),
is_edge_collection,
wait_for_sync,
}
}
}
#[maybe_async::maybe_async]
impl SchemaDatabaseOperation for CollectionSchema {
type PoolType = Collection<ReqwestClient>;
async fn apply_to_database(
&self,
database: &Database<ReqwestClient>,
silent: bool,
) -> Result<Option<Self::PoolType>, ClientError> {
log::debug!("Creating Collection {}", &self.name);
let creation_settings = CreateOptions::builder()
.name(&self.name)
.collection_type(if self.is_edge_collection {
CollectionType::Edge
} else {
CollectionType::Document
})
.wait_for_sync(self.wait_for_sync.unwrap_or(false))
.build();
let res = database
.create_collection_with_options(creation_settings, Default::default())
.await;
Self::handle_pool_result(res, silent)
}
async fn drop(&self, database: &Database<ReqwestClient>) -> Result<(), ClientError> {
log::debug!("Deleting Collection {}", &self.name);
database.drop_collection(&self.name).await?;
Ok(())
}
async fn get(&self, database: &Database<ReqwestClient>) -> Result<Self::PoolType, ClientError> {
database.collection(&self.name).await
}
}