2 releases

new 0.1.1 May 12, 2024
0.1.0 May 1, 2024

#114 in #mongo-db

Download history 117/week @ 2024-04-29 115/week @ 2024-05-06

232 downloads per month
Used in spark-orm

MIT/Apache

13KB
285 lines

Spark-ORM: MongoDB ORM for Rust

Spark-ORM is a high-performance, open-source Object-Relational Mapping (ORM) library designed specifically for MongoDB in Rust. It seamlessly bridges Rust structs with MongoDB collections, effortlessly converting structs into models.

Features

  • Derive Models: Effortlessly convert Rust structs into MongoDB models using the Model trait derivation.

  • Custom Collection Names: Tailor collection names for your models with the #[coll_name] attribute.

  • Memory Efficiency: Built for speed and memory efficiency, Spark-ORM offers a non-heap copy ORM solution for MongoDB.

Getting Started

  1. Define your model by simply applying the Model attribute and setting the collection name with coll_name:

    #[Model(coll_name = "users")]
    #[derive(Serialize, Deserialize, Default, Debug)]
    struct User {
    age: u32,
    name: String,
    email: String,
    }
    
  2. Connect to the database in one of two ways:

    a. Establish a global connection:

    Spark::global_connect("root", "123", "localhost", "6789", "rm_orm_db").await;
    

    b. Or connect locally:

    Spark::connect("root", "123", "localhost", "6789", "rm_orm_db").await;
    

    For the global connection, Spark retains it throughout the program, accessible via: Spark::get_db();

Usage

Instantiate the model:

    let mut user = User::new_model(&db);

Update attributes:

   user.name = "Hossein".to_string();
   user.age = 22;

Save to the database:

   user.save().await.unwrap();

Find a model:

       let mut user = User::new_model(&db);
       let sample = doc! {
            "name" : "Hossein",
            "email" : "spark_orm_test"
       };
       user.find_one(Prototype::Doc(sample)).await.unwrap().unwrap();
       println!("{}" , user.name);

Update and save:

    let mut user = User::new_model(&db);
    user.name = "Hossein".to_string();
    user.email = "spark_orm_test".to_string();

    user.save().await;

    user.name = "Nothing".to_string();

    user.update().await;

Delete a record:

        let mut user = User::new_model(&db);
        user.delete().await;

Attributes

Define index or unique attributes for struct fields:

   #[Model(coll_name = "products")]
   #[derive(Serialize, Deserialize, Default, Debug)]
   struct Product {
       #[index]
       age: u32,
       #[index]
       name: String,
       #[unique]
       email: String,
   }

These indexes are registered during the first initiation of Product.

Note the library is under development and may have lots of changes in the future, event in its basics

Dependencies

~0.7–1.2MB
~26K SLoC