use std::fmt::Debug;
use super::{ChangeStream, ClientSession, Collection, Cursor, SessionChangeStream, SessionCursor};
use crate::{
bson::Document,
change_stream::{event::ChangeStreamEvent, options::ChangeStreamOptions},
error::Result,
options::{
AggregateOptions,
CollectionOptions,
CreateCollectionOptions,
DropDatabaseOptions,
ListCollectionsOptions,
ReadConcern,
SelectionCriteria,
WriteConcern,
},
results::CollectionSpecification,
runtime,
Database as AsyncDatabase,
};
#[derive(Debug, Clone)]
pub struct Database {
async_database: AsyncDatabase,
}
impl Database {
pub(crate) fn new(async_database: AsyncDatabase) -> Self {
Self { async_database }
}
pub fn name(&self) -> &str {
self.async_database.name()
}
pub fn selection_criteria(&self) -> Option<&SelectionCriteria> {
self.async_database.selection_criteria()
}
pub fn read_concern(&self) -> Option<&ReadConcern> {
self.async_database.read_concern()
}
pub fn write_concern(&self) -> Option<&WriteConcern> {
self.async_database.write_concern()
}
pub fn collection<T>(&self, name: &str) -> Collection<T> {
Collection::new(self.async_database.collection(name))
}
pub fn collection_with_options<T>(
&self,
name: &str,
options: CollectionOptions,
) -> Collection<T> {
Collection::new(self.async_database.collection_with_options(name, options))
}
pub fn drop(&self, options: impl Into<Option<DropDatabaseOptions>>) -> Result<()> {
runtime::block_on(self.async_database.drop(options.into()))
}
pub fn drop_with_session(
&self,
options: impl Into<Option<DropDatabaseOptions>>,
session: &mut ClientSession,
) -> Result<()> {
runtime::block_on(
self.async_database
.drop_with_session(options.into(), &mut session.async_client_session),
)
}
pub fn list_collections(
&self,
filter: impl Into<Option<Document>>,
options: impl Into<Option<ListCollectionsOptions>>,
) -> Result<Cursor<CollectionSpecification>> {
runtime::block_on(
self.async_database
.list_collections(filter.into(), options.into()),
)
.map(Cursor::new)
}
pub fn list_collections_with_session(
&self,
filter: impl Into<Option<Document>>,
options: impl Into<Option<ListCollectionsOptions>>,
session: &mut ClientSession,
) -> Result<SessionCursor<CollectionSpecification>> {
runtime::block_on(self.async_database.list_collections_with_session(
filter.into(),
options.into(),
&mut session.async_client_session,
))
.map(SessionCursor::new)
}
pub fn list_collection_names(
&self,
filter: impl Into<Option<Document>>,
) -> Result<Vec<String>> {
runtime::block_on(self.async_database.list_collection_names(filter.into()))
}
pub fn list_collection_names_with_session(
&self,
filter: impl Into<Option<Document>>,
session: &mut ClientSession,
) -> Result<Vec<String>> {
runtime::block_on(
self.async_database.list_collection_names_with_session(
filter.into(),
&mut session.async_client_session,
),
)
}
pub fn create_collection(
&self,
name: impl AsRef<str>,
options: impl Into<Option<CreateCollectionOptions>>,
) -> Result<()> {
runtime::block_on(
self.async_database
.create_collection(name.as_ref(), options.into()),
)
}
pub fn create_collection_with_session(
&self,
name: impl AsRef<str>,
options: impl Into<Option<CreateCollectionOptions>>,
session: &mut ClientSession,
) -> Result<()> {
runtime::block_on(self.async_database.create_collection_with_session(
name.as_ref(),
options.into(),
&mut session.async_client_session,
))
}
pub fn run_command(
&self,
command: Document,
selection_criteria: impl Into<Option<SelectionCriteria>>,
) -> Result<Document> {
runtime::block_on(
self.async_database
.run_command(command, selection_criteria.into()),
)
}
pub fn run_command_with_session(
&self,
command: Document,
selection_criteria: impl Into<Option<SelectionCriteria>>,
session: &mut ClientSession,
) -> Result<Document> {
runtime::block_on(self.async_database.run_command_with_session(
command,
selection_criteria.into(),
&mut session.async_client_session,
))
}
pub fn aggregate(
&self,
pipeline: impl IntoIterator<Item = Document>,
options: impl Into<Option<AggregateOptions>>,
) -> Result<Cursor<Document>> {
let pipeline: Vec<Document> = pipeline.into_iter().collect();
runtime::block_on(self.async_database.aggregate(pipeline, options.into())).map(Cursor::new)
}
pub fn aggregate_with_session(
&self,
pipeline: impl IntoIterator<Item = Document>,
options: impl Into<Option<AggregateOptions>>,
session: &mut ClientSession,
) -> Result<SessionCursor<Document>> {
let pipeline: Vec<Document> = pipeline.into_iter().collect();
runtime::block_on(self.async_database.aggregate_with_session(
pipeline,
options.into(),
&mut session.async_client_session,
))
.map(SessionCursor::new)
}
pub fn watch(
&self,
pipeline: impl IntoIterator<Item = Document>,
options: impl Into<Option<ChangeStreamOptions>>,
) -> Result<ChangeStream<ChangeStreamEvent<Document>>> {
runtime::block_on(self.async_database.watch(pipeline, options)).map(ChangeStream::new)
}
pub fn watch_with_session(
&self,
pipeline: impl IntoIterator<Item = Document>,
options: impl Into<Option<ChangeStreamOptions>>,
session: &mut ClientSession,
) -> Result<SessionChangeStream<ChangeStreamEvent<Document>>> {
runtime::block_on(self.async_database.watch_with_session(
pipeline,
options,
&mut session.async_client_session,
))
.map(SessionChangeStream::new)
}
}