Expand description
§MongODM
A thin ODM layer for MongoDB built upon the official Rust driver.
Main features:
- A stronger API leveraging Rust type system
- Data structure models are defined using the well-known
serde
serialization framework - Index support on top of the
Database::run_command
(index management is currently not implemented in the underlying driver) - Indexes synchronization
- Additional compile-time checks for queries using macros and type associated to mongo operators (eg:
And
instead of “$and”)
§Example
ⓘ
use mongodm::{ToRepository, Model, CollectionConfig, Indexes, Index, IndexOption, sync_indexes};
use mongodm::mongo::{Client, options::ClientOptions, bson::doc};
use serde::{Serialize, Deserialize};
use std::borrow::Cow;
// field! is used to make sure at compile time that some field exists in a given structure
use mongodm::field;
struct UserCollConf;
impl CollectionConfig for UserCollConf {
fn collection_name() -> &'static str {
"user"
}
fn indexes() -> Indexes {
Indexes::new()
.with(Index::new("username").with_option(IndexOption::Unique))
.with(Index::new(field!(last_seen in User))) // field! macro can be used as well
}
}
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct User {
username: String,
last_seen: i64,
}
impl Model for User {
type CollConf = UserCollConf;
}
let client_options = ClientOptions::parse("mongodb://localhost:27017").await?;
let client = Client::with_options(client_options)?;
let db = client.database("mongodm_wayk_demo");
sync_indexes::<UserCollConf>(&db).await?;
// indexes are now synced in backend for user collection
let repository = db.repository::<User>(); // method provided by `ToRepository` trait
let user = User {
username: String::from("David"),
last_seen: 1000,
};
repository.insert_one(&user, None).await?;
// We make sure at compile time that `username` is a field of `User`
let fetched_user = repository.find_one(doc! { field!(username in User): "David" }, None).await?;
assert!(fetched_user.is_some());
assert_eq!(fetched_user.unwrap(), user);
// f! is a shorter version of field!
use mongodm::f;
repository.find_one(doc! { f!(username in User): "David" }, None).await?.unwrap();
// With static operators for queries (prevent invalid queries due to typos)
use mongodm::operator::*;
repository.find_one(
doc! { And: [
{ f!(username in User): "David" },
{ f!(last_seen in User): { GreaterThan: 500 } },
] },
None
).await?.unwrap();
Re-exports§
Modules§
- operator
- Static operators for queries to prevent invalid queries due to typos.
- prelude
- Contains everything you need to use MongODM.
Macros§
- bson
- Construct a bson::BSON value from a literal.
- doc
- Construct a bson::Document value.
- f
- Shorthand for
field!
. - field
- Statically check presence of field in a given struct and stringify it.
- pipeline
- Helper to build aggregation pipelines.
Return a Vec
as expected by the aggregate function.
Structs§
- Bulk
Update - Represents an individual update operation for the
bulk_update
function. - Bulk
Update Result - Result of a
bulk_update
operation. - Bulk
Update Upsert Result - Individual update result of a
bulk_update
operation. Contains the generated id in case of an upsert. - Index
- Specify field to be used for indexing and options.
- Indexes
- Collection of indexes. Provides function to build database commands.
- Repository
- Associate a
mongodb::Collection
and a specificModel
.
Enums§
- Index
Option - Option to be used at index creation.
- Sort
Order - Index sort order (useful for compound indexes).
Traits§
- Collection
Config - Define collection name, configuration and associated indexes.
- Collection
Ext - MongODM-provided utilities functions on
mongodb::Collection<M>
. - Model
- Associate a collection configuration.
- ToRepository
- Utilities methods to get a
Repository
. Implemented formongodb::Database
.
Functions§
- sync_
indexes - Synchronize backend mongo collection for a given
CollectionConfig
.