Skip to content

Commit

Permalink
Add collection factory creation macro
Browse files Browse the repository at this point in the history
  • Loading branch information
zeritiq committed Apr 15, 2023
1 parent eb1b6e7 commit e827740
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mongodb-macro"
version = "0.3.0"
version = "1.0.0"
edition = "2021"
description = "MongoDB Macro is a crate with macros for quickly creating structures to work with mongodb"
keywords = [ "mongodb", "macro", "config", "clap" ]
Expand Down
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,35 @@ Make sure you also add to the project:
> clap = { version = "*", features = ["derive", "env"] }
## Usage
### Macro: Collection
```rust

use mongodb::bson::Bson;

// env DB_URL should contain a link to the mongodb url
// env DB_NAME should contain the database name
// env COLLECTION_NAME should contain the collection name
mongodb_macro::collection!(CollectionFactory; CollectionFactoryOpts);
// or with a specified env
// mongodb_macro::collection!(CollectionFactory; CollectionFactoryOpts; ("MONGO_DB_URL", "MONGO_DB_NAME", "MONGO_COLLECTION_NAME"));

async fn main() -> std::io::Result<()> {

let factory = CollectionFactory::parse();

let collection = factory.create::<Bson>().await.expect("failed to connect");

...
}
```

### Macro: Database
```rust

use mongodb::bson::Bson;

// env DB_URL should contain a link to the mongodb url
// and DB_NAME should contain the database name
// env DB_NAME should contain the database name
mongodb_macro::database!(DbFactory; DbFactoryOpts);
// or with a specified env
// mongodb_macro::database!(DbFactory; DbFactoryOpts; ("MONGO_DB_URL", "MONGO_DB_NAME"));
Expand Down Expand Up @@ -102,6 +123,6 @@ async fn main() -> std::io::Result<()> {
}
```

Current version: 0.3.0
Current version: 1.0.0

License: MIT
100 changes: 100 additions & 0 deletions src/collection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@


/// Creates a new configuration structure to initialize the MongoDB collection
///
/// Create a new configuration structure to initialize the MongoDB collection with a standard environment variable
///
/// ```
/// mongodb_macro::collection_config!(Opts);
///
/// fn main() {
/// std::env::set_var("DB_URL", "mongodb://root:root@localhost:27017");
/// std::env::set_var("DB_NAME", "test");
/// std::env::set_var("COLLECTION_NAME", "users");
///
/// let opts = Opts::parse();
/// }
/// ```
///
/// Create a new configuration structure to initialize the MongoDB collection with the specified environment variable
///
/// ```
/// mongodb_macro::collection_config!(Opts; ("MONGO_DB_URL", "MONGO_DB_NAME", "MONGO_COLLECTION_NAME"));
///
/// fn main() {
/// std::env::set_var("MONGO_DB_URL", "mongodb://root:root@localhost:27017");
/// std::env::set_var("MONGO_DB_NAME", "test");
/// std::env::set_var("MONGO_COLLECTION_NAME", "users");
///
/// let opts = Opts::parse();
/// }
/// ```
#[macro_export]
macro_rules! collection_config {
($opts:ident) => ($crate::config!{$opts});

($opts:ident; ($db_url:tt, $db_name:tt, $collection_name:tt)) =>
($crate::config!{$opts; $db_url, $db_name, $collection_name});
}

/// Creates a new factory to create a MongoDB collection
///
/// Create mongodb collection factory with standard environment variable for database url, database name and collection name
///
/// ```
/// mongodb_macro::collection!(CollectionFactory; CollectionFactoryOpts);
///
/// fn main() {
/// std::env::set_var("DB_URL", "mongodb://root:root@localhost:27017");
/// std::env::set_var("DB_NAME", "test");
/// std::env::set_var("COLLECTION_NAME", "users");
///
/// let factory = CollectionFactory::parse();
///
/// // let collection = factory.create<Bson>().await.expect("failed to connect");
/// }
/// ```
///
/// Create mongodb collection factory with specified environment variable for database url, database name and collection name
///
/// ```
/// mongodb_macro::collection!(CollectionFactory; CollectionFactoryOpts; ("MONGO_DB_URL", "MONGO_DB_NAME", "MONGO_COLLECTION_NAME"));
///
/// fn main() {
/// std::env::set_var("MONGO_DB_URL", "mongodb://root:root@localhost:27017");
/// std::env::set_var("MONGO_DB_NAME", "test");
/// std::env::set_var("MONGO_COLLECTION_NAME", "users");
///
/// let factory = CollectionFactory::parse();
///
/// // let collection = factory.create<Bson>().await.expect("failed to connect");
/// }
/// ```
#[macro_export]
macro_rules! collection {
($collection_factory:ident; $opts:ident) => ($crate::collection!{$collection_factory; $opts; ("DB_URL", "DB_NAME", "COLLECTION_NAME")});

($collection_factory:ident; $opts:ident; ($db_url:tt, $db_name:tt, $collection_name:tt)) => {

$crate::collection_config!($opts; ($db_url, $db_name, $collection_name));

pub struct $collection_factory($opts);

impl $collection_factory {
fn parse() -> Self {
let opts = $opts::parse();
Self(opts)
}

pub fn config(&self) -> &$opts {
&self.0
}

pub async fn create<T>(&self) -> Result<::mongodb::Collection<T>, ::mongodb::error::Error> {
let client = ::mongodb::Client::with_uri_str(&self.0.db_url).await?;
let db = client.database(&self.0.db_name);
Ok(db.collection::<T>(&self.0.collection_name))
}
}
};
}
29 changes: 27 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,35 @@ Make sure you also add to the project:
> clap = { version = "*", features = ["derive", "env"] }
# Usage
## Macro: Collection
```no_run
use mongodb::bson::Bson;
// env DB_URL should contain a link to the mongodb url
// env DB_NAME should contain the database name
// env COLLECTION_NAME should contain the collection name
mongodb_macro::collection!(CollectionFactory; CollectionFactoryOpts);
// or with a specified env
// mongodb_macro::collection!(CollectionFactory; CollectionFactoryOpts; ("MONGO_DB_URL", "MONGO_DB_NAME", "MONGO_COLLECTION_NAME"));
async fn main() -> std::io::Result<()> {
let factory = CollectionFactory::parse();
let collection = factory.create::<Bson>().await.expect("failed to connect");
...
}
```
## Macro: Database
```no_run
use mongodb::bson::Bson;
// env DB_URL should contain a link to the mongodb url
// and DB_NAME should contain the database name
// env DB_NAME should contain the database name
mongodb_macro::database!(DbFactory; DbFactoryOpts);
// or with a specified env
// mongodb_macro::database!(DbFactory; DbFactoryOpts; ("MONGO_DB_URL", "MONGO_DB_NAME"));
Expand Down Expand Up @@ -102,4 +123,8 @@ pub mod client;

/// Macros for creating MongoDB database configurations and factories from environment variables
#[doc(hidden)]
pub mod database;
pub mod database;

/// Macros for creating MongoDB collection configurations and factories from environment variables
#[doc(hidden)]
pub mod collection;

0 comments on commit e827740

Please sign in to comment.