meshuganah

Crates.iomeshuganah
lib.rsmeshuganah
version0.2.2
sourcesrc
created_at2020-10-29 21:08:56.859158
updated_at2020-11-03 15:15:03.115906
descriptionA type safe ODM for MongoDB.
homepagehttps://github.com/adamfriedland/meshuganah
repositoryhttps://github.com/adamfriedland/meshuganah
max_upload_size
id306792
size14,844
Adam Friedland (adamfriedland)

documentation

README

Meshuganah

crates.io version downloads

A strongly typed ODM for the official Mongodb Rust driver, providing an easy to use abstraction.

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);
}

Work still needed to be done

  • Correct error handling

  • Migrations

  • Indexing

  • Extending more of the base client in a type safe way

Commit count: 49

cargo fmt