Skip to main content

Crate oximod

Crate oximod 

Source
Expand description

§OxiMod

Schema-aware MongoDB modeling for Rust.

OxiMod is a lightweight modeling layer built on top of the official MongoDB Rust driver. It provides builder-style model construction, validation, defaults, index declarations, and optional lifecycle hooks, while preserving direct access to the underlying driver when needed.

§Features

  • derive-based model definitions
  • builder-style model construction
  • validation and defaults
  • index declarations
  • optional lifecycle hooks
  • global and explicit-client workflows
  • typed and raw MongoDB collection access

§Quick Start

use mongodb::bson::{doc, oid::ObjectId};
use oximod::{Model, OxiClient};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, Model)]
#[db("my_app_db")]
#[collection("users")]
struct User {
    #[serde(skip_serializing_if = "Option::is_none")]
    _id: Option<ObjectId>,

    #[index(unique, name = "email_idx")]
    #[validate(email)]
    email: String,

    #[validate(min_length = 3, max_length = 32)]
    name: String,

    #[validate(non_negative)]
    age: i32,

    #[default(false)]
    active: bool,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    OxiClient::init_global("mongodb://localhost:27017".to_string()).await?;

    User::clear().await?;

    let user = User::new()
        .email("alice@example.com")
        .name("Alice")
        .age(30)
        .active(true);

    let id = user.save().await?;

    if let Some(found) = User::find_by_id(id).await? {
        println!("Found user: {}", found.name);
    }

    let count = User::count(doc! {}).await?;
    println!("Total users: {}", count);

    let collection = User::get_collection()?;

    collection
        .update_one(
            doc! { "_id": id },
            doc! { "$set": { "active": false } },
        )
        .await?;

    Ok(())
}

For more complete examples, see the examples/ directory.

Structs§

OxiClient
MongoDB client wrapper used by OxiMod.
ValidationError
Represents a validation failure for a specific model field. Represents a validation failure for a specific model field.
ValidationErrors
Represents one or more validation failures collected during model validation. Represents all validation errors for all fields on a model

Enums§

OxiModError
Primary error type used by OxiMod.

Traits§

Hooks
Trait for defining lifecycle hooks on OxiMod models.
Model
Core trait implemented by all OxiMod models.

Derive Macros§

Model
Derive macro for defining OxiMod models.