1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
//! This crate provides the macro [`mongo_db`] to model a mongoDB database.
//To make [`mongo_db`] work reliably a couple of re-exports are needed, these are not relevant for using the macro.
#[doc(hidden)]
pub use {mongodb, mongodb_ext_derive, paste, serde};
/// Defines the default type for the `_id` field.
pub type DefaultId = String;
/// Expands one collection.
///
/// Needed internally, but has no big use on its own.
/// Thus hidden from documentation.
#[macro_export]
#[doc(hidden)]
macro_rules! expand_collection {
// invoked with `_id: none`, thus assume `_id` is added already and finally expand to collection
(
$(#[$additional_coll_attr:meta])*
$coll_name:ident<_id: none> {
$(
$(#[$additional_field_attr:meta])*
$field:ident: $field_type:ty
),*$(,)?
}
) => {
$crate::paste::paste! {
#[doc = "Represents the [`" $coll_name "`] collection in mongodb."]
#[derive($crate::serde::Deserialize, $crate::serde::Serialize, $crate::mongodb_ext_derive::ConstName)]
#[serde(rename_all = "camelCase")]
#[const_name_value(Camel)]
$(#[$additional_coll_attr])*
pub struct $coll_name {
$(
$(#[$additional_field_attr])*
pub [<$field:snake:lower>]: $field_type
),*
}
}
};
// specific type for `_id` given, add it and invoke again with `_id: none` to avoid adding the `_id` field again
(
$(#[$additional_coll_attr:meta])*
$coll_name:ident<_id: $explicit_id_type:ty> {
$(
$(#[$additional_field_attr:meta])*
$field:ident: $field_type:ty
),*$(,)?
}
) => {
$crate::expand_collection! {
$(#[$additional_coll_attr])*
$coll_name<_id: none> {
#[serde(skip_serializing_if = "std::option::Option::is_none")]
#[serde(rename = "_id")]
_id: std::option::Option<$explicit_id_type>,
$(
$(#[$additional_field_attr])*
$field: $field_type
),*
}
}
};
// no specific type for `_id` given, add `DefaultId` and invoke again
(
$(#[$additional_coll_attr:meta])*
$coll_name:ident {
$(
$(#[$additional_field_attr:meta])*
$field:ident: $field_type:ty
),*$(,)?
}
) => {
$crate::expand_collection! {
$(#[$additional_coll_attr])*
$coll_name<_id: $crate::DefaultId> {
$(
$(#[$additional_field_attr])*
$field: $field_type
),*
}
}
};
}
/// Expands the main database client.
///
/// Needed internally, but has no big use on its own.
/// Thus hidden from documentation.
#[macro_export]
#[doc(hidden)]
macro_rules! expand_main_client {
(
$(#[$additional_db_attr:meta])*
$db_name:ident {
$(
$(#[$additional_coll_attr:meta])*
$coll_name:ident<_id: none> {
$(
$(#[$additional_field_attr:meta])*
$field:ident: $field_type:ty
),*$(,)?
}
),+
}
) => {
$crate::paste::paste! {
#[doc = "Client to interact with the [`" $db_name "`] database."]
#[derive($crate::mongodb_ext_derive::ConstName)]
#[const_name_value(Camel)]
#[const_name_key("DB_NAME")]
$(#[$additional_db_attr])*
pub struct $db_name {
pub client: $crate::mongodb::Client,
pub database: $crate::mongodb::Database,
$(pub [<$coll_name:snake:lower _coll>]: $crate::mongodb::Collection<schema::$coll_name>),+
}
impl $db_name {
#[doc = "Initializer funtion of the database."]
pub async fn new(connection_str: &str) -> std::result::Result<Self, std::string::String> {
let client = match $crate::mongodb::Client::with_uri_str(connection_str).await {
$crate::mongodb::error::Result::Ok(client) => client,
$crate::mongodb::error::Result::Err(e) => return Err(format!("Could not initialize mongodb client: {}", e)),
};
let database = client.database(DB_NAME);
$(let [<$coll_name:snake:lower _coll>] = database.collection(schema::[<$coll_name:snake:upper>]);)+
std::result::Result::Ok(Self {
client,
database,
$([<$coll_name:snake:lower _coll>]),+
})
}
#[doc = "Method that sends a ping command to the database."]
#[allow(dead_code)]
pub async fn ping(&self) -> $crate::mongodb::error::Result<$crate::mongodb::bson::document::Document> {
self.database.run_command($crate::mongodb::bson::doc!{"ping": 1}, std::option::Option::None).await
}
}
}
};
}
/// Model a mongodb database.
///
/// This macro creates structs / functions / constants / modules that represent a mongoDB database.
/// Being a macro (which is expanded at compile time) there is no run time performance penalty when using this macro.
///
/// # Structure
///
/// This macro wraps everything in a module called `mongo`.
///
/// The main database handler has the following attributes:
/// - Its name represents the database's name (eg. a database named `MyDatabase` has a struct `mongo::MyDatabase`).
/// - It has an initializer function:
/// `pub async fn new(connection_str: &str) -> Result<Self, String>`
/// - It has a ping function that sends a ping message to the database:
/// `pub async fn ping(&self) -> mongodb::error::Result<mongodb::bson::document::Document>`
/// - It contains handles to all given collections inside the database.
/// These handles have the format `{collection_name}_coll` where `{collection_name}` represents the collection's name in snake_case.
/// - It also contains a [`client`](mongodb::Client) and a [`database`](mongodb::Database) field for you to use.
///
/// All collections are wrapped in an additional public module named `schema`.
///
/// Each collection also has its own struct which stores all specified fields.
/// All collections' structs implement [`Serialize`](serde::Serialize) and [`Deserialize`](serde::Deserialize).
///
/// By default a field `_id` gets added to each collection automatically:
/// `pub _id: Option<DefaultId>` ([`DefaultId`]).
/// This field needs to exist for you to be able to obtain an `_id` field from the database.
/// When serializing, `_id` gets skipped if it is [`None`].
/// All fields except `_id` get renamed to `camelCase` when serializing (converting `_id` to `camelCase` results in `id`).
///
/// Additionally the following constants are specified:
/// - `mongo::DB_NAME` is set to the database's name in `camelCase`.
/// - `mongo::schema::{COLLECTION_NAME}` where `{COLLECTION_NAME}` represents each collection's name in screaming snake case. Set to the collection's name in `camelCase`.
///
/// # Hygiene
///
/// All structs / constants / functions are wrapped in a public module called `mongo`.
/// All structs / constants that refer to a collection are wrapped in an additional public module called `schema`.
/// This is done to maintain more hygiene by exposing less items.
/// A better hygiene creates less interference of the macro and its surrounding items.
///
/// In addition to this measure, all paths referred by the code in the macro are full paths, thus there should be no type interference.
///
/// # Examples
///
/// ## Manipulating / Removing `_id`
///
/// You can specify any type (that implements [`Serialize`](serde::Serialize) and [`Deserialize`](serde::Deserialize)) to be used inside the `_id` [`Option`] by specifying it in `<` / `>` after the collection name:
///
/// ```rust
/// use mongodb_ext::mongo_db;
///
/// mongo_db! {
/// SomeDatabase {
/// SomeCollection<_id: u128> {
/// first_name: String,
/// }
/// }
/// }
///
/// // _id is now u128
/// let some_document = mongo::schema::SomeCollection {
/// _id: Some(255),
/// first_name: String::from("Bob")
/// };
/// ```
///
/// It is also possible to disable the generation of an `_id` field all together by using `<_id: none>`.
///
/// ```rust
/// use mongodb_ext::mongo_db;
///
/// mongo_db! {
/// SomeDatabase {
/// SomeCollection<_id: none> {
/// email_address: String,
/// first_name: String,
/// }
/// }
/// }
///
/// // no _id exists, this example assumes that users are addressed via their email address
/// let some_document = mongo::schema::SomeCollection {
/// email_address: String::from("bob@example.com"),
/// first_name: String::from("Bob")
/// };
/// ```
///
/// These features are unique for each collection:
///
/// ```rust
/// use mongodb_ext::mongo_db;
///
/// mongo_db! {
/// SomeDatabase {
/// SomeCollection<_id: u128> {
/// first_name: String,
/// },
/// Another {
/// some_field: u32,
/// },
/// AndYetAnother<_id: none> {
/// email: String,
/// name: String,
/// }
/// }
/// }
///
/// // `_id` type changed to `u128`
/// let some_document = mongo::schema::SomeCollection {
/// _id: Some(255),
/// first_name: String::from("Bob")
/// };
/// // `_id` type default, eg. `DefaultId`
/// let another_document = mongo::schema::Another {
/// _id: Some(String::from("my_id")),
/// some_field: 1,
/// };
/// // `_id` field omitted
/// let and_yet_another_document = mongo::schema::AndYetAnother {
/// name: String::from("Bob"),
/// email: String::from("bob@example.com")
/// };
/// ```
///
/// ## Serializing from [`json!`](serde_json::json) and [`doc!`](mongodb::bson::doc)
///
/// ```rust
/// use mongodb_ext::mongo_db;
/// use serde_json::{json, Value};
/// use mongodb::{bson::{doc, Document}, bson};
///
/// mongo_db! {
/// #[derive(Debug, Clone)]
/// DatabaseOfItems {
/// #[derive(Debug, Clone, PartialEq)]
/// Items {
/// counter: u16,
/// name: String
/// },
/// }
/// }
///
/// // Note that `_id` is not specified here
/// let my_item: Value = json! ({
/// "counter": 0,
/// "name": "my_special_item"
/// });
///
/// let my_collection_entry: mongo::schema::Items =
/// serde_json::from_value(my_item)
/// .expect("Could not convert json Value to collection document");
///
/// assert_eq!(
/// my_collection_entry,
/// mongo::schema::Items {
/// _id: None,
/// counter: 0,
/// name: String::from("my_special_item")
/// }
/// );
///
/// // Note that `_id` is not specified here
/// let my_item: Document = doc! {
/// "counter": 0,
/// "name": "my_special_item"
/// };
///
/// let my_collection_entry: mongo::schema::Items = bson::de::from_document(my_item)
/// .expect("Could not convert mongodb bson Document to collection document");
///
/// assert_eq!(
/// my_collection_entry,
/// mongo::schema::Items {
/// _id: None,
/// counter: 0,
/// name: String::from("my_special_item")
/// }
/// );
/// ```
///
/// ## General Examples
///
/// ```rust
/// use mongodb_ext::mongo_db;
/// use serde_json::ser;
///
/// mongo_db! {
/// SomeDatabase {
/// #[derive(Debug, Clone)]
/// SomeCollection {
/// first_name: String,
/// }
/// }
/// }
///
/// let mut some_document = mongo::schema::SomeCollection {
/// _id: None,
/// first_name: String::from("alice")
/// };
///
/// // When serializing, `_id` is skipped only if `None`.
/// // Note the key conversion to `camelCase`.
/// assert_eq!(
/// ser::to_string(&some_document).unwrap(),
/// String::from("{\"firstName\":\"alice\"}")
/// );
///
/// // update `_id` field to include in serialization.
/// some_document._id = Some(String::from("my-custom-ID"));
/// assert_eq!(
/// ser::to_string(&some_document).unwrap(),
/// String::from("{\"_id\":\"my-custom-ID\",\"firstName\":\"alice\"}")
/// );
///
/// assert_eq!("someCollection", mongo::schema::SOME_COLLECTION);
/// assert_eq!("someDatabase", mongo::DB_NAME);
/// ```
///
/// ```rust
/// use mongodb_ext::mongo_db;
///
/// mongo_db! {
/// #[derive(Debug, Clone)]
/// MyDatabase {
/// #[derive(Debug, Clone)]
/// MyFirstCollection {
/// first_name: String,
/// last_name: String,
/// age: u8,
/// },
/// #[derive(Debug)]
/// AnotherCollection {
/// some_field: String
/// }
/// }
/// }
///
/// // all constants that were defined
/// assert_eq!("myDatabase", mongo::DB_NAME);
/// assert_eq!("myFirstCollection", mongo::schema::MY_FIRST_COLLECTION);
/// assert_eq!("anotherCollection", mongo::schema::ANOTHER_COLLECTION);
///
/// // initializer function and general usage
/// // note that `tokio_test::block_on` is just a test function to run `async` code
///
/// let mongo = tokio_test::block_on(mongo::MyDatabase::new("mongodb://example.com"))
/// .expect("Could not create mongoDB client");
///
/// let bob = mongo::schema::MyFirstCollection {
/// _id: None,
/// first_name: String::from("Bob"),
/// last_name: String::from("Bob's last name"),
/// age: 255,
/// };
///
/// // This should fail beause there is no actual mongoDB service running at the specified connection.
/// assert!(tokio_test::block_on(
/// mongo.my_first_collection_coll.insert_one(bob, None)
/// ).is_err());
/// ```
#[macro_export]
macro_rules! mongo_db {
// only one match, the real magic happens in `expand_collection` and `expand_main_client`
(
$(#[$additional_db_attr:meta])*
$db_name:ident {
$(
$(#[$additional_coll_attr:meta])*
$coll_name:ident$(<_id: $id_spec:ident>)? {
$(
$(#[$additional_field_attr:meta])*
$field:ident: $field_type:ty
),*$(,)?
}
),+$(,)?
}
) => {
pub mod mongo {
pub mod schema {
$(
$crate::expand_collection! {
$(#[$additional_coll_attr])*
$coll_name$(<_id: $id_spec>)? {
$(
$(#[$additional_field_attr])*
$field: $field_type
),*
}
}
)+
}
$crate::expand_main_client ! {
$(#[$additional_db_attr])*
$db_name {
$(
$(#[$additional_coll_attr])*
$coll_name<_id: none> {
$(
$(#[$additional_field_attr])*
$field: $field_type
),*
}
),+
}
}
}
};
}
#[cfg(test)]
mod test {
use super::mongo_db;
mongo_db! {
#[derive(Debug, Clone)]
DatabaseOfDoom {
#[derive(Debug, Clone, PartialEq)]
Items {
counter: u16,
name: String
},
#[derive(Debug)]
QueuedItems {
something: Option<bool>,
}
}
}
#[test]
pub fn check_json_serialization() {
use serde_json::{from_value, json, Value};
let my_item: Value = json! ({
"counter": 0,
"name": "my_special_item"
});
let my_collection_entry: mongo::schema::Items =
from_value(my_item).expect("Could not convert json Value to collection document");
assert_eq!(
my_collection_entry,
mongo::schema::Items {
_id: None,
counter: 0,
name: String::from("my_special_item")
}
);
}
#[test]
pub fn check_doc_serialization() {
use mongodb::bson::{de::from_document, doc, Document};
let my_item: Document = doc! {
"counter": 0,
"name": "my_special_item"
};
let my_collection_entry: mongo::schema::Items = from_document(my_item)
.expect("Could not convert mongodb bson Document to collection document");
assert_eq!(
my_collection_entry,
mongo::schema::Items {
_id: None,
counter: 0,
name: String::from("my_special_item")
}
);
}
#[test]
pub fn check_json_serialization_with_id() {
use serde_json::{from_value, json, Value};
let my_item: Value = json! ({
"_id": "my_fancy_id",
"counter": 0,
"name": "my_special_item"
});
let my_collection_entry: mongo::schema::Items =
from_value(my_item).expect("Could not convert json Value to collection document");
assert_eq!(
my_collection_entry,
mongo::schema::Items {
_id: Some(String::from("my_fancy_id")),
counter: 0,
name: String::from("my_special_item")
}
);
}
#[test]
pub fn check_doc_serialization_with_id() {
use mongodb::bson::{de::from_document, doc, Document};
let my_item: Document = doc! {
"_id": "my_fancy_id",
"counter": 0,
"name": "my_special_item"
};
let my_collection_entry: mongo::schema::Items = from_document(my_item)
.expect("Could not convert mongodb bson Document to collection document");
assert_eq!(
my_collection_entry,
mongo::schema::Items {
_id: Some(String::from("my_fancy_id")),
counter: 0,
name: String::from("my_special_item")
}
);
}
#[test]
pub fn check_constants() {
assert_eq!("databaseOfDoom", mongo::DB_NAME);
assert_eq!("items", mongo::schema::ITEMS);
assert_eq!("queuedItems", mongo::schema::QUEUED_ITEMS);
}
/// This test is rather useless, but it's currently the best way to test the [`DatabaseOfDoom::new`] function.
#[test]
pub fn check_initializer() {
// try to initialize with an invalid connection string
if let Err(e) =
tokio_test::block_on(mongo::DatabaseOfDoom::new("invalid connection string"))
{
// make sure the correct error message is produced
assert_eq!(
&e,
"Could not initialize mongodb client: An invalid argument was provided: connection string contains no scheme"
);
} else {
// this should really not happen
panic!("Somehow constructed a database client without a proper connection string")
}
// initialize with valid connection string
match tokio_test::block_on(mongo::DatabaseOfDoom::new("mongodb://localhost:27017")) {
Ok(client) => {
// check the collections' names
assert_eq!(client.items_coll.name(), "items");
assert_eq!(client.queued_items_coll.name(), "queuedItems");
}
Err(e) => {
panic!(
"Could not construct mongodb client with proper connection string: {}",
e
)
}
}
}
}