diff --git a/README.md b/README.md index 0e98be6..1a8cbc2 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ mongod is a rust crate for a model based database on top of MongoDB. ## Usage -`mongod` allows you to use structs as `models` with data. it is build upon a MongoDB database. You need to pass a Connection URI for the database to use via the `$DB_URI` environment variable. +`mongod` allows you to use structs as `models` with data. it is build upon a MongoDB database. You need to pass a Connection URI for the database to use via the `$DB_URI` environment variable. The database to use can be configured via `$DB` environment variable. ### Models You can derive the `Model` and `Referencable` traits for your struct. This will provide you with functions like `insert()`, `delete()`, `update()`, `get()`, etc for your struct. Additionally you have to manually implement the `Validate` trait which ensures a consistent valid state of your struct in order to never insert invalid data into the database. diff --git a/src/lib.rs b/src/lib.rs index 3337ba6..66d6136 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,7 +20,7 @@ macro_rules! get_mongo { #[macro_export] macro_rules! col { ($db:expr, $col:expr) => { - $db.database("owl") + $db.database(&std::env::var("DB").unwrap()) .collection::($col) }; } diff --git a/src/model/mod.rs b/src/model/mod.rs index f0b22c7..ed2239f 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -87,17 +87,20 @@ pub trait Model: #[must_use] fn get_partial( id: &str, - part: &serde_json::Value, + mut part: serde_json::Value, ) -> impl std::future::Future> { async move { let db = get_mongo!(); let collection = col!(db, Self::collection_name()); + + part.as_object_mut()?.insert("_id".into(), 1.into()); + let doc = collection .find_one( id_of!(id), Some( FindOneOptions::builder() - .projection(Some(mongodb::bson::to_document(part).unwrap())) + .projection(Some(mongodb::bson::to_document(&part).unwrap())) .build(), ), ) @@ -111,17 +114,20 @@ pub trait Model: #[must_use] fn find_one_partial( filter: mongodb::bson::Document, - part: &serde_json::Value, + mut part: serde_json::Value, ) -> impl std::future::Future> { async move { let db = get_mongo!(); let collection = col!(db, Self::collection_name()); + + part.as_object_mut()?.insert("_id".into(), 1.into()); + let doc = collection .find_one( filter, Some( FindOneOptions::builder() - .projection(Some(mongodb::bson::to_document(part).unwrap())) + .projection(Some(mongodb::bson::to_document(&part).unwrap())) .build(), ), ) @@ -164,17 +170,20 @@ pub trait Model: #[must_use] fn find_partial( filter: mongodb::bson::Document, - part: &serde_json::Value, + mut part: serde_json::Value, ) -> impl std::future::Future>> { async move { let db = get_mongo!(); let collection = col!(db, Self::collection_name()); + + part.as_object_mut()?.insert("_id".into(), 1.into()); + let mut results = collection .find( filter, Some( FindOptions::builder() - .projection(Some(mongodb::bson::to_document(part).unwrap())) + .projection(Some(mongodb::bson::to_document(&part).unwrap())) .build(), ), )