mongodb_macro/
lib.rs

1/*!
2MongoDB Macro is a crate with macros for quickly creating structures when working with mongodb.
3In particular, macros are implemented for quick initialization of structures of the "factory" pattern.
4The purpose of this crate is to reduce the amount of boilerplate code when initializing standard structures.
5
6# installation
7
8Install using cargo:
9
10`cargo install mongodb-macro`
11
12Make sure you also add to the project:
13
14> mongodb = "*"
15
16# Usage
17## Macro: Collection
18```no_run
19
20use mongodb::bson::Bson;
21
22// env DB_URL should contain a link to the mongodb url
23// env DB_NAME should contain the database name
24// env COLLECTION_NAME should contain the collection name
25mongodb_macro::collection!(CollectionFactory; CollectionFactoryOpts);
26// or with a specified env
27// mongodb_macro::collection!(CollectionFactory; CollectionFactoryOpts; ("MONGO_DB_URL", "MONGO_DB_NAME", "MONGO_COLLECTION_NAME"));
28
29async fn main() -> std::io::Result<()> {
30
31    // std::env::set_var("MONGODB_HOST", "localhost");
32    // std::env::set_var("DB_URL", "mongodb://root:root@${MONGODB_HOST}:27017");
33
34    let factory = CollectionFactory::parse();
35
36    let collection = factory.create::<Bson>().await.expect("failed to connect");
37    
38    ...
39}
40```
41
42## Macro: Database
43```no_run
44
45use mongodb::bson::Bson;
46
47// env DB_URL should contain a link to the mongodb url
48// env DB_NAME should contain the database name
49mongodb_macro::database!(DbFactory; DbFactoryOpts);
50// or with a specified env
51// mongodb_macro::database!(DbFactory; DbFactoryOpts; ("MONGO_DB_URL", "MONGO_DB_NAME"));
52
53async fn main() -> std::io::Result<()> {
54
55    // std::env::set_var("MONGODB_HOST", "localhost");
56    // std::env::set_var("DB_URL", "mongodb://root:root@${MONGODB_HOST}:27017");
57
58    let factory = DbFactory::parse();
59
60    let db = factory.create().await.expect("failed to connect");
61
62    let collection = db.collection::<Bson>("users");
63    
64    ...
65}
66```
67
68## Macro: Client
69```no_run
70
71use mongodb::bson::Bson;
72
73// env DB_URL should contain a link to the mongodb url
74mongodb_macro::client!(ClientFactory; ClientFactoryOpts);
75// or with a specified env
76// mongodb_macro::client!(ClientFactory; ClientFactoryOpts; "MONGO_DB_URL");
77
78async fn main() -> std::io::Result<()> {
79
80    // std::env::set_var("MONGODB_HOST", "localhost");
81    // std::env::set_var("DB_URL", "mongodb://root:root@${MONGODB_HOST}:27017");
82
83    let factory = ClientFactory::parse();
84
85    let client = factory.create().await.expect("failed to connect");
86
87    let db = client.database("demo");
88    let collection = db.collection::<Bson>("users");
89    
90    ...
91}
92```
93
94## Macro: Config
95```no_run
96
97use mongodb::bson::Bson;
98use mongodb_macro::Parser;
99
100mongodb_macro::config!(Opts);
101// equivalent to
102// mongodb_macro::config!(Opts; "DB_NAME", "COLLECTION_NAME", "DB_URL");
103//
104// or with prefix
105//
106// mongodb_macro::config!(Opts, "MONGO");
107// equivalent to
108// mongodb_macro::config!(Opts; "MONGO_DB_NAME", "MONGO_COLLECTION_NAME", "MONGO_DB_URL");
109
110async fn main() -> std::io::Result<()> {
111
112    // std::env::set_var("MONGODB_HOST", "localhost");
113    // std::env::set_var("DB_URL", "mongodb://root:root@${MONGODB_HOST}:27017");
114
115    let opts = Opts::parse();
116
117    let client = mongodb::Client::with_uri_str(&opts.db_url).await.expect("failed to connect");
118    let db = client.database(&opts.db_name);
119    let collection = db.collection::<Bson>(&opts.collection_name);
120    
121    ...
122}
123```
124*/
125#![warn(missing_docs)]
126
127/// Macros for creating MongoDB configurations from environment variables
128#[doc(hidden)]
129pub mod opts;
130
131/// Macros for creating MongoDB client configurations and factories from environment variables
132#[doc(hidden)]
133pub mod client;
134
135/// Macros for creating MongoDB database configurations and factories from environment variables
136#[doc(hidden)]
137pub mod database;
138
139/// Macros for creating MongoDB collection configurations and factories from environment variables
140#[doc(hidden)]
141pub mod collection;
142
143#[doc(hidden)]
144pub use nested_env_parser::Env;
145
146#[doc(hidden)]
147pub use clap::Parser;