use proc_macro2::{Ident, TokenStream};
use quote::quote;
pub fn derive_find_all(name: &Ident, collection_name: String, _partial_fields: Vec<Ident>) -> TokenStream {
quote! {
async fn find_all(&self, ctx: &(impl mongo_crud_core::MongoContext + Sync), filter: mongodb::bson::Document) -> Vec<Box<Self>> {
use futures_util::stream::TryStreamExt;
let db = mongo_crud_core::MongoContext::get_database(ctx).await;
let mut cursor = match db
.collection::<#name>(#collection_name)
.find(filter, None)
.await
{
Ok(cursor) => cursor,
Err(e) => {
log::error!("Failed to find documents: {:?}", e);
return Vec::new();
}
};
let mut results = Vec::new();
while let Ok(Some(item)) = cursor.try_next().await {
results.push(Box::new(item));
}
results
}
}
}