This commit is contained in:
JMARyA 2024-07-24 16:38:56 +02:00
parent 3b0e8c1866
commit ca364453a7
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
10 changed files with 341 additions and 604 deletions

View file

@ -1,47 +1,5 @@
use crate::item::Item;
/// Collect database results into a `Vec<_>`
#[macro_export]
macro_rules! collect_results {
($res:expr) => {{
use futures::stream::TryStreamExt;
let mut ret = vec![];
while let Some(doc) = $res.try_next().await.unwrap() {
ret.push(doc);
}
ret
}};
}
/// Get a database collection
#[macro_export]
macro_rules! cdb_col {
($db:expr, $col:expr) => {
$db.database("cdb")
.collection::<mongodb::bson::Document>($col)
};
}
/// Get a MongoDB Client from the environment
#[macro_export]
macro_rules! get_mongo {
() => {
mongodb::Client::with_uri_str(std::env::var("DB_URI").unwrap())
.await
.unwrap()
};
}
/// MongoDB filter for the `_id` field.
#[macro_export]
macro_rules! id_of {
($id:expr) => {
doc! { "_id": $id}
};
}
/// Item database
pub struct ItemDB {
index: mdq::Index,
@ -54,11 +12,12 @@ impl ItemDB {
pub async fn new(dir: &str) -> Self {
// scan for markdown item entries
let index = mdq::Index::new(dir, true);
let mongodb = get_mongo!();
for item in &index.documents {
let item = Item::new(item);
item.init_db(&mongodb).await;
log::info!("Adding item {} to DB", item.name);
}
Self { index }