use super::Database;
use crate::{
bson::Document,
concern::{ReadConcern, WriteConcern},
error::Result,
options::{ClientOptions, DatabaseOptions, ListDatabasesOptions, SelectionCriteria},
Client as AsyncClient,
RUNTIME,
};
#[derive(Clone, Debug)]
pub struct Client {
async_client: AsyncClient,
}
impl Client {
pub fn with_uri_str(uri: &str) -> Result<Self> {
let async_client = RUNTIME.block_on(AsyncClient::with_uri_str(uri))?;
Ok(Self { async_client })
}
pub fn with_options(options: ClientOptions) -> Result<Self> {
let async_client = AsyncClient::with_options(options)?;
Ok(Self { async_client })
}
pub fn selection_criteria(&self) -> Option<&SelectionCriteria> {
self.async_client.selection_criteria()
}
pub fn read_concern(&self) -> Option<&ReadConcern> {
self.async_client.read_concern()
}
pub fn write_concern(&self) -> Option<&WriteConcern> {
self.async_client.write_concern()
}
pub fn database(&self, name: &str) -> Database {
Database::new(self.async_client.database(name))
}
pub fn database_with_options(&self, name: &str, options: DatabaseOptions) -> Database {
Database::new(self.async_client.database_with_options(name, options))
}
pub fn list_databases(
&self,
filter: impl Into<Option<Document>>,
options: impl Into<Option<ListDatabasesOptions>>,
) -> Result<Vec<Document>> {
RUNTIME.block_on(
self.async_client
.list_databases(filter.into(), options.into()),
)
}
pub fn list_database_names(
&self,
filter: impl Into<Option<Document>>,
options: impl Into<Option<ListDatabasesOptions>>,
) -> Result<Vec<String>> {
RUNTIME.block_on(
self.async_client
.list_database_names(filter.into(), options.into()),
)
}
}