fix
This commit is contained in:
parent
e3c6740b1a
commit
6be8a879f9
3 changed files with 17 additions and 8 deletions
|
@ -2,7 +2,7 @@
|
||||||
mongod is a rust crate for a model based database on top of MongoDB.
|
mongod is a rust crate for a model based database on top of MongoDB.
|
||||||
|
|
||||||
## Usage
|
## 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
|
### 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.
|
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.
|
||||||
|
|
|
@ -20,7 +20,7 @@ macro_rules! get_mongo {
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! col {
|
macro_rules! col {
|
||||||
($db:expr, $col:expr) => {
|
($db:expr, $col:expr) => {
|
||||||
$db.database("owl")
|
$db.database(&std::env::var("DB").unwrap())
|
||||||
.collection::<mongodb::bson::Document>($col)
|
.collection::<mongodb::bson::Document>($col)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,17 +87,20 @@ pub trait Model:
|
||||||
#[must_use]
|
#[must_use]
|
||||||
fn get_partial(
|
fn get_partial(
|
||||||
id: &str,
|
id: &str,
|
||||||
part: &serde_json::Value,
|
mut part: serde_json::Value,
|
||||||
) -> impl std::future::Future<Output = Option<Self::Partial>> {
|
) -> impl std::future::Future<Output = Option<Self::Partial>> {
|
||||||
async move {
|
async move {
|
||||||
let db = get_mongo!();
|
let db = get_mongo!();
|
||||||
let collection = col!(db, Self::collection_name());
|
let collection = col!(db, Self::collection_name());
|
||||||
|
|
||||||
|
part.as_object_mut()?.insert("_id".into(), 1.into());
|
||||||
|
|
||||||
let doc = collection
|
let doc = collection
|
||||||
.find_one(
|
.find_one(
|
||||||
id_of!(id),
|
id_of!(id),
|
||||||
Some(
|
Some(
|
||||||
FindOneOptions::builder()
|
FindOneOptions::builder()
|
||||||
.projection(Some(mongodb::bson::to_document(part).unwrap()))
|
.projection(Some(mongodb::bson::to_document(&part).unwrap()))
|
||||||
.build(),
|
.build(),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
@ -111,17 +114,20 @@ pub trait Model:
|
||||||
#[must_use]
|
#[must_use]
|
||||||
fn find_one_partial(
|
fn find_one_partial(
|
||||||
filter: mongodb::bson::Document,
|
filter: mongodb::bson::Document,
|
||||||
part: &serde_json::Value,
|
mut part: serde_json::Value,
|
||||||
) -> impl std::future::Future<Output = Option<Self::Partial>> {
|
) -> impl std::future::Future<Output = Option<Self::Partial>> {
|
||||||
async move {
|
async move {
|
||||||
let db = get_mongo!();
|
let db = get_mongo!();
|
||||||
let collection = col!(db, Self::collection_name());
|
let collection = col!(db, Self::collection_name());
|
||||||
|
|
||||||
|
part.as_object_mut()?.insert("_id".into(), 1.into());
|
||||||
|
|
||||||
let doc = collection
|
let doc = collection
|
||||||
.find_one(
|
.find_one(
|
||||||
filter,
|
filter,
|
||||||
Some(
|
Some(
|
||||||
FindOneOptions::builder()
|
FindOneOptions::builder()
|
||||||
.projection(Some(mongodb::bson::to_document(part).unwrap()))
|
.projection(Some(mongodb::bson::to_document(&part).unwrap()))
|
||||||
.build(),
|
.build(),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
@ -164,17 +170,20 @@ pub trait Model:
|
||||||
#[must_use]
|
#[must_use]
|
||||||
fn find_partial(
|
fn find_partial(
|
||||||
filter: mongodb::bson::Document,
|
filter: mongodb::bson::Document,
|
||||||
part: &serde_json::Value,
|
mut part: serde_json::Value,
|
||||||
) -> impl std::future::Future<Output = Option<Vec<Self>>> {
|
) -> impl std::future::Future<Output = Option<Vec<Self>>> {
|
||||||
async move {
|
async move {
|
||||||
let db = get_mongo!();
|
let db = get_mongo!();
|
||||||
let collection = col!(db, Self::collection_name());
|
let collection = col!(db, Self::collection_name());
|
||||||
|
|
||||||
|
part.as_object_mut()?.insert("_id".into(), 1.into());
|
||||||
|
|
||||||
let mut results = collection
|
let mut results = collection
|
||||||
.find(
|
.find(
|
||||||
filter,
|
filter,
|
||||||
Some(
|
Some(
|
||||||
FindOptions::builder()
|
FindOptions::builder()
|
||||||
.projection(Some(mongodb::bson::to_document(part).unwrap()))
|
.projection(Some(mongodb::bson::to_document(&part).unwrap()))
|
||||||
.build(),
|
.build(),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue