wither

Build Status Crates.io docs.rs License

An ODM for MongoDB built upon the mongo rust driver. Please ⭐ on github!

A primary goal of this project is to provide a simple, sane & predictable interface into MongoDB based on data modeling. And if at anypoint this system might get in your way, you have direct access to the underlying driver.

This project makes use of associated constants as of 0.2.0, so you will need to be running rust >= 1.20.

NOTE: progress is being, but there is a lot more to be done! For the time being, there may be backwards incompatible releases made from minor version to minor version until the best patterns for this library are found. It would be best to pin to an exact version in your Cargo.toml. Any such backwords incompatible changes will be declared in the changelog.

Check out the changelog for more details on what has happened from release to release.

example

An example of how you might use this library to define a model for a MongoDB collection.

```rust

[derive(Serialize, Deserialize, Debug, Clone)]

pub struct User { /// The user's unique ID. #[serde(rename = "id", skipserializingif = "Option::isnone")] pub id: Option,

/// The user's unique email.
pub email: String,

}

impl<'a> wither::Model<'a> for User {

const COLLECTION_NAME: &'static str = "users";

fn id(&self) -> Option<bson::oid::ObjectId> {
    return self.id.clone();
}

fn set_id(&mut self, oid: bson::oid::ObjectId) {
    self.id = Some(oid);
}

fn indexes() -> Vec<IndexModel> {
    return vec![
        IndexModel{
            keys: doc!{"email" => 1},
            options: wither::basic_index_options("unique-email", true, Some(true), None, None),
        },
    ];
}

}

fn main() { let client = mongodb::Client::with_uri("mongodb://tests.mongodb:27017/").unwrap(); let db = client.db("usersService");

let mut user = User{id: None, email: "test@test.com".tostring()}; user.save(db.clone(), None).expect("Expected a successful save operation."); let userfromdb = User.findone(db.clone(), Some(doc!{"_id" => (user.id.clone().unwrap())})) .expect("Expected a successful lookup.") // Unwraps the Result. .expect("Expected a populated value from backend."); // Unwraps the optional model instance. } ```