use crate::bson::Document;
use super::{Collection, Cursor};
use crate::{
error::Result,
options::{
AggregateOptions,
CollectionOptions,
CreateCollectionOptions,
DropDatabaseOptions,
ListCollectionsOptions,
ReadConcern,
SelectionCriteria,
WriteConcern,
},
Database as AsyncDatabase,
RUNTIME,
};
#[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(&self, name: &str) -> Collection {
Collection::new(self.async_database.collection(name))
}
pub fn collection_with_options(&self, name: &str, options: CollectionOptions) -> Collection {
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 list_collections(
&self,
filter: impl Into<Option<Document>>,
options: impl Into<Option<ListCollectionsOptions>>,
) -> Result<Cursor> {
RUNTIME
.block_on(
self.async_database
.list_collections(filter.into(), options.into()),
)
.map(Cursor::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 create_collection(
&self,
name: &str,
options: impl Into<Option<CreateCollectionOptions>>,
) -> Result<()> {
RUNTIME.block_on(self.async_database.create_collection(name, options.into()))
}
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 aggregate(
&self,
pipeline: impl IntoIterator<Item = Document>,
options: impl Into<Option<AggregateOptions>>,
) -> Result<Cursor> {
let pipeline: Vec<Document> = pipeline.into_iter().collect();
RUNTIME
.block_on(self.async_database.aggregate(pipeline, options.into()))
.map(Cursor::new)
}
}