use crate::{
async_trait::async_trait,
mongodb::{
bson::document::Document, error::Result as MongoResult, Client as DbClient, Database,
},
};
pub trait MongoCollection {
const NAME: &'static str;
const SCHEMA_VERSION: i32;
}
#[async_trait]
pub trait MongoClient
where
Self: Sized,
{
const NAME: &'static str;
async fn new(connection_str: &str) -> MongoResult<Self>;
async fn new_with_client(client: DbClient) -> MongoResult<Self>;
async fn ping(&self) -> MongoResult<Document>;
fn database(&self) -> &Database;
fn client(&self) -> &DbClient;
}
#[cfg(feature = "mongodb-gridfs")]
pub use gridfs::GridFSDb;
#[cfg(feature = "mongodb-gridfs")]
pub mod gridfs {
use {super::MongoClient, mongodb_gridfs::GridFSBucket};
pub trait GridFSDb: MongoClient {
fn create_bucket(&self) -> GridFSBucket {
GridFSBucket::new(self.database().clone(), None)
}
}
impl<T> GridFSDb for T where T: MongoClient {}
}