mod document;
mod documents;
use crate::{Client, Error, execute_wrapper};
use ::std::borrow::Cow;
use serde::{Serialize, de::DeserializeOwned};
use typesense_codegen::{apis::collections_api, models};
pub struct Collection<'c, D = serde_json::Value>
where
D: DeserializeOwned + Serialize,
{
client: &'c Client,
collection_name: Cow<'c, str>,
_phantom: core::marker::PhantomData<D>,
}
impl<'c, D> Collection<'c, D>
where
D: DeserializeOwned + Serialize,
{
#[inline]
pub(super) fn new(client: &'c Client, collection_name: impl Into<Cow<'c, str>>) -> Self {
Self {
client,
collection_name: collection_name.into(),
_phantom: core::marker::PhantomData,
}
}
#[inline]
pub fn documents<'d>(&'d self) -> documents::Documents<'d, D> {
documents::Documents::new(self.client, &self.collection_name)
}
#[inline]
pub fn document<'d>(
&'d self,
document_id: impl Into<Cow<'d, str>>,
) -> document::Document<'d, D> {
document::Document::new(self.client, &self.collection_name, document_id)
}
#[inline]
pub async fn retrieve(
&self,
) -> Result<models::CollectionResponse, Error<collections_api::GetCollectionError>> {
let params = collections_api::GetCollectionParams {
collection_name: self.collection_name.as_ref().into(),
};
execute_wrapper!(self, collections_api::get_collection, params)
}
#[inline]
pub async fn delete(
&self,
) -> Result<models::CollectionResponse, Error<collections_api::DeleteCollectionError>> {
let params = collections_api::DeleteCollectionParams {
collection_name: self.collection_name.as_ref().into(),
};
execute_wrapper!(self, collections_api::delete_collection, params)
}
#[inline]
pub async fn update(
&self,
update_schema: models::CollectionUpdateSchema,
) -> Result<models::CollectionUpdateSchema, Error<collections_api::UpdateCollectionError>> {
let params = collections_api::UpdateCollectionParams {
collection_name: self.collection_name.as_ref().into(),
collection_update_schema: update_schema,
};
execute_wrapper!(self, collections_api::update_collection, params)
}
}