nongoose

Crates.ionongoose
lib.rsnongoose
version0.1.0-beta.1
sourcesrc
created_at2022-01-12 18:08:31.844052
updated_at2022-01-12 18:08:31.844052
descriptionODM for MongoDB based on Mongoose
homepagehttps://nongoose.danielsolarte.com
repositoryhttps://github.com/danielsolartech/nongoose
max_upload_size
id512930
size94,983
Daniel Solarte (iamdacso)

documentation

https://docs.rs/nongoose

README

Nongoose

Crates.io version Crates.io downloads License GitHub repository stars

ODM for MongoDB based on Mongoose and written in Rust

Basic usage

use nongoose::{bson::oid::ObjectId, Client, Schema};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Schema, Serialize)]
struct User {
  #[schema(id)]
  #[serde(rename = "_id")]
  pub id: ObjectId,

  #[schema(unique)]
  pub username: String,
}

#[tokio::main]
async fn main() {
  // Get MongoDB connection.
  let client = match Client::with_uri_str("mongodb://localhost:27017").await {
    Ok(client) => client,
    Err(e) => panic!("Error connecting to the database: {}", e),
  };

  // Nongoose instance.
  let nongoose = nongoose::Nongoose::build(client.database("nextchat"))
    .add_schema::<User>()
    .finish();

  let user = User {
    id: ObjectId::new(),
    username: String::from("nongoose"),
  };

  if let Err(error) = user.save().await {
    panic!("Cannot create the user: {}", error);
  }

  println!("User created in the database: {}", user.id);
}

Tests

# Sync tests
$ DATABASE_URL=mongodb://localhost:27017 cargo test --no-default-features --features derive,sync

# Async tests (Tokio runtime)
$ DATABASE_URL=mongodb://localhost:27017 cargo test

License

Check the COPYING file for more information.

Contributors

Thanks to this amazing people for make Nongoose better:

If you help to Nongoose feel free to add here.

Commit count: 84

cargo fmt