mondm

Crates.iomondm
lib.rsmondm
version0.0.1
created_at2025-07-13 02:40:10.255338+00
updated_at2025-07-13 02:40:10.255338+00
descriptiona lightweight, async-friendly, type-safe ODM (Object-Document Mapper) for MongoDB in the Rust ecosystem.
homepagehttps://github.com/rustkit/mondm
repositoryhttps://github.com/rustkit/mondm
max_upload_size
id1749909
size6,934
Billgo (billgo)

documentation

https://docs.rs/mondm

README

mondm

๐Ÿ—ƒ๏ธ mondm is a lightweight, async-friendly, type-safe ODM (Object-Document Mapper) for MongoDB in the Rust ecosystem.

Built on top of the official mongodb driver and inspired by simplicity, mondm helps you map Rust structs to MongoDB documents with minimal boilerplate and maximum control.


โœจ Features

  • โœ… Type-safe #[derive(Model)] style document mapping
  • ๐Ÿ“„ Automatic _id (ObjectId) support
  • ๐Ÿ” Built-in support for insert, find, update, delete
  • ๐Ÿงช Generic CRUD trait for reusable services
  • โš™๏ธ Easy to plug into any async runtime
  • ๐Ÿงฉ Optional query builder + helper macros
  • ๐Ÿ”ง Support for indexes and collection config

๐Ÿš€ Getting Started

1. Add to your Cargo.toml

[dependencies]
mondm = "0.1"
mongodb = { version = "2", default-features = false, features = ["tokio-runtime"] }
serde = { version = "1", features = ["derive"] }

2. Define your model

use mondm::{Model, MongoModel};
use serde::{Deserialize, Serialize};
use bson::oid::ObjectId;

#[derive(Debug, Serialize, Deserialize, Model)]
#[mondm(collection = "users")]
pub struct User {
    #[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
    pub id: Option<ObjectId>,
    pub name: String,
    pub email: String,
}

3. Insert and find

use mondm::MongoRepository;
use mongodb::Client;

#[tokio::main]
async fn main() -> mongodb::error::Result<()> {
    let client = Client::with_uri_str("mongodb://localhost:27017").await?;
    let db = client.database("test");

    let repo = MongoRepository::<User>::new(&db);

    let user = User {
        id: None,
        name: "Alice".into(),
        email: "alice@example.com".into(),
    };

    repo.insert(&user).await?;
    let fetched = repo.find_by_id(user.id.as_ref().unwrap()).await?;

    println!("Fetched user: {:?}", fetched);
    Ok(())
}

๐Ÿงฑ Design Philosophy

  • Minimal abstraction: over the official MongoDB driver
  • Composable: integrate easily into service layers
  • Runtime-agnostic: (Tokio-first, but easy to adapt)

๐Ÿ“„ License

This project is dual-licensed under either:

  • MIT License
Commit count: 0

cargo fmt