Crates.io | meshuganah |
lib.rs | meshuganah |
version | 0.2.2 |
source | src |
created_at | 2020-10-29 21:08:56.859158 |
updated_at | 2020-11-03 15:15:03.115906 |
description | A type safe ODM for MongoDB. |
homepage | https://github.com/adamfriedland/meshuganah |
repository | https://github.com/adamfriedland/meshuganah |
max_upload_size | |
id | 306792 |
size | 14,844 |
use meshuganah::GenericRepository;
use meshuganah::RepositoryTrait;
use serde::{Deserialize, Serialize};
use mongodb::bson;
use futures::stream::StreamExt;
#[derive(Debug, Deserialize, Serialize)]
pub struct Species {
#[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
pub id: Option<bson::oid::ObjectId>,
pub name: String,
pub category: String,
pub taxonomy: String,
}
unsafe impl Send for Species {}
impl Model for Species {
const COLLECTION_NAME: &'static str = "species";
fn get_id(&self) -> Option<bson::oid::ObjectId> {
self.id.clone()
}
fn set_id(&mut self, oid: bson::oid::ObjectId) {
self.id = Some(oid);
}
}
let me = Species {
id: None,
name: "some_name".to_string(),
category: "some_category".to_string(),
taxonomy: "some_taxonomy".to_string(),
};
let database = mongodb::Client::with_uri_str("mongodb://localhost:27017/")
.await
.unwrap()
.database("database_name");
let repository = GenericRepository::<Species>::new(database).await;
// Inserting a document
repository.add_item(None, me).await;
// Find single document
let found = repository.find_one(None, None).await.unwrap();
// Find multiple documents
let mut cursor = repository.find(None, None).await.unwrap();
while let Some(item) = cursor.next().await {
println!("{:?}", item);
}
Correct error handling
Migrations
Indexing
Extending more of the base client in a type safe way