use serde::de::DeserializeOwned;
use serde::Serialize;
use crate::dto::data_modeling::instances::SlimNodeOrEdge;
use crate::models::instances::{
AggregateInstancesRequest, AggregateInstancesResponse, FilterInstancesRequest,
InstancesFilterResponse, NodeAndEdgeCreateCollection, NodeAndEdgeRetrieveRequest,
NodeAndEdgeRetrieveResponse, NodeOrEdge, NodeOrEdgeCreate, NodeOrEdgeSpecification,
QueryInstancesRequest, QueryInstancesResponse, SearchInstancesRequest, SourceReferenceInternal,
};
use crate::models::instances::{FromReadable, WithView};
use crate::models::views::ViewReference;
use crate::Result;
use crate::{DeleteWithResponse, FilterWithRequest, RetrieveWithRequest, UpsertCollection};
use crate::{Resource, WithBasePath};
pub type Instances = Resource<SlimNodeOrEdge>;
impl WithBasePath for Instances {
const BASE_PATH: &'static str = "models/instances";
}
impl<TProperties> FilterWithRequest<FilterInstancesRequest, NodeOrEdge<TProperties>> for Instances where
TProperties: Serialize + DeserializeOwned + Send + Sync
{
}
impl<TProperties>
RetrieveWithRequest<NodeAndEdgeRetrieveRequest, NodeAndEdgeRetrieveResponse<TProperties>>
for Instances
where
TProperties: Serialize + DeserializeOwned + Send + Sync,
{
}
impl<TProperties> UpsertCollection<NodeAndEdgeCreateCollection<TProperties>, SlimNodeOrEdge>
for Instances
{
}
impl DeleteWithResponse<NodeOrEdgeSpecification, NodeOrEdgeSpecification> for Instances {}
impl Instances {
pub async fn filter_with_type_info<TProperties: DeserializeOwned + Send + Sync + 'static>(
&self,
req: FilterInstancesRequest,
) -> Result<InstancesFilterResponse<TProperties>> {
self.api_client
.post(&format!("{}/list", Self::BASE_PATH), &req)
.await
}
pub async fn query<TProperties: DeserializeOwned + Send + Sync + 'static>(
&self,
query: QueryInstancesRequest,
) -> Result<QueryInstancesResponse<TProperties>> {
self.api_client
.post(&format!("{}/query", Self::BASE_PATH), &query)
.await
}
pub async fn sync<TProperties: DeserializeOwned + Send + Sync + 'static>(
&self,
query: QueryInstancesRequest,
) -> Result<QueryInstancesResponse<TProperties>> {
self.api_client
.post(&format!("{}/sync", Self::BASE_PATH), &query)
.await
}
pub async fn aggregate(
&self,
req: AggregateInstancesRequest,
) -> Result<AggregateInstancesResponse> {
self.api_client
.post(&format!("{}/aggregate", Self::BASE_PATH), &req)
.await
}
pub async fn search<TProperties: DeserializeOwned + Send + Sync + 'static>(
&self,
req: SearchInstancesRequest,
) -> Result<NodeAndEdgeRetrieveResponse<TProperties>> {
self.api_client
.post(&format!("{}/search", Self::BASE_PATH), &req)
.await
}
pub async fn fetch<TEntity, TProperties>(
&self,
items: &[NodeOrEdgeSpecification],
view: Option<&ViewReference>,
) -> Result<Vec<TEntity>>
where
TProperties: Serialize + DeserializeOwned + Send + Sync,
TEntity: FromReadable<TProperties> + WithView + Send,
{
let response: NodeAndEdgeRetrieveResponse<TProperties> = self
.retrieve(&NodeAndEdgeRetrieveRequest {
sources: Some(vec![SourceReferenceInternal {
source: view
.unwrap_or(&ViewReference {
space: TEntity::SPACE.to_owned(),
external_id: TEntity::EXTERNAL_ID.to_owned(),
version: TEntity::VERSION.to_owned(),
})
.to_owned()
.into(),
}]),
items: items.to_vec(),
include_typing: None,
})
.await?;
response
.items
.into_iter()
.map(|item| TEntity::try_from(item, view))
.collect()
}
pub async fn apply<TEntity, TProperties>(
&self,
col: &[TEntity],
auto_create_direct_relations: Option<bool>,
auto_create_start_nodes: Option<bool>,
auto_create_end_nodes: Option<bool>,
skip_on_version_conflict: Option<bool>,
replace: bool,
) -> Result<Vec<SlimNodeOrEdge>>
where
TProperties: Serialize + DeserializeOwned + Send + Sync,
TEntity: Clone + Into<NodeOrEdgeCreate<TProperties>> + Send,
{
let collection = col
.iter()
.map(|t| t.to_owned().into())
.collect::<Vec<NodeOrEdgeCreate<_>>>();
let collection = NodeAndEdgeCreateCollection {
items: collection,
auto_create_direct_relations: auto_create_direct_relations.or(Some(true)),
auto_create_start_nodes,
auto_create_end_nodes,
skip_on_version_conflict,
replace: Some(replace),
};
self.upsert(&collection).await
}
}