mongodb-macro

Crates.iomongodb-macro
lib.rsmongodb-macro
version1.0.4
sourcesrc
created_at2023-04-13 09:01:26.735333
updated_at2023-05-31 17:19:31.003648
descriptionMongoDB Macro is a crate with macros for quickly creating structures to work with mongodb
homepage
repositoryhttps://github.com/erritis/mongodb-macro
max_upload_size
id838104
size27,480
Zeritiq (zeritiq)

documentation

https://docs.rs/mongodb-macro

README

crates.io documentation MIT License

mongodb-macro

MongoDB Macro is a crate with macros for quickly creating structures when working with mongodb. In particular, macros are implemented for quick initialization of structures of the "factory" pattern. The purpose of this crate is to reduce the amount of boilerplate code when initializing standard structures.

installation

Install using cargo:

cargo install mongodb-macro

Make sure you also add to the project:

mongodb = "*"

Usage

Macro: Collection


use mongodb::bson::Bson;

// env DB_URL should contain a link to the mongodb url
// env DB_NAME should contain the database name
// env COLLECTION_NAME should contain the collection name
mongodb_macro::collection!(CollectionFactory; CollectionFactoryOpts);
// or with a specified env
// mongodb_macro::collection!(CollectionFactory; CollectionFactoryOpts; ("MONGO_DB_URL", "MONGO_DB_NAME", "MONGO_COLLECTION_NAME"));

async fn main() -> std::io::Result<()> {

    // std::env::set_var("MONGODB_HOST", "localhost");
    // std::env::set_var("DB_URL", "mongodb://root:root@${MONGODB_HOST}:27017");

    let factory = CollectionFactory::parse();

    let collection = factory.create::<Bson>().await.expect("failed to connect");

    ...
}

Macro: Database


use mongodb::bson::Bson;

// env DB_URL should contain a link to the mongodb url
// env DB_NAME should contain the database name
mongodb_macro::database!(DbFactory; DbFactoryOpts);
// or with a specified env
// mongodb_macro::database!(DbFactory; DbFactoryOpts; ("MONGO_DB_URL", "MONGO_DB_NAME"));

async fn main() -> std::io::Result<()> {

    // std::env::set_var("MONGODB_HOST", "localhost");
    // std::env::set_var("DB_URL", "mongodb://root:root@${MONGODB_HOST}:27017");

    let factory = DbFactory::parse();

    let db = factory.create().await.expect("failed to connect");

    let collection = db.collection::<Bson>("users");

    ...
}

Macro: Client


use mongodb::bson::Bson;

// env DB_URL should contain a link to the mongodb url
mongodb_macro::client!(ClientFactory; ClientFactoryOpts);
// or with a specified env
// mongodb_macro::client!(ClientFactory; ClientFactoryOpts; "MONGO_DB_URL");

async fn main() -> std::io::Result<()> {

    // std::env::set_var("MONGODB_HOST", "localhost");
    // std::env::set_var("DB_URL", "mongodb://root:root@${MONGODB_HOST}:27017");

    let factory = ClientFactory::parse();

    let client = factory.create().await.expect("failed to connect");

    let db = client.database("demo");
    let collection = db.collection::<Bson>("users");

    ...
}

Macro: Config


use mongodb::bson::Bson;
use mongodb_macro::Parser;

mongodb_macro::config!(Opts);
// equivalent to
// mongodb_macro::config!(Opts; "DB_NAME", "COLLECTION_NAME", "DB_URL");
//
// or with prefix
//
// mongodb_macro::config!(Opts, "MONGO");
// equivalent to
// mongodb_macro::config!(Opts; "MONGO_DB_NAME", "MONGO_COLLECTION_NAME", "MONGO_DB_URL");

async fn main() -> std::io::Result<()> {

    // std::env::set_var("MONGODB_HOST", "localhost");
    // std::env::set_var("DB_URL", "mongodb://root:root@${MONGODB_HOST}:27017");

    let opts = Opts::parse();

    let client = mongodb::Client::with_uri_str(&opts.db_url).await.expect("failed to connect");
    let db = client.database(&opts.db_name);
    let collection = db.collection::<Bson>(&opts.collection_name);

    ...
}

Current version: 1.0.4

License: MIT

Commit count: 14

cargo fmt