This commit is contained in:
JMARyA 2024-07-21 22:01:17 +02:00
parent e3c6740b1a
commit 6be8a879f9
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
3 changed files with 17 additions and 8 deletions

View file

@ -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.

View file

@ -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::<mongodb::bson::Document>($col)
};
}

View file

@ -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<Output = Option<Self::Partial>> {
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<Output = Option<Self::Partial>> {
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<Output = Option<Vec<Self>>> {
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(),
),
)