1个不稳定版本
0.1.0-beta.1 | 2022年1月12日 |
---|
#2716 在 数据库接口
58KB
581 代码行
Nongoose
基于Mongoose和用Rust编写的MongoDB ODM
基本用法
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://127.0.0.1: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);
}
测试
# Sync tests
$ DATABASE_URL=mongodb://127.0.0.1:27017 cargo test --no-default-features --features derive,sync
# Async tests (Tokio runtime)
$ DATABASE_URL=mongodb://127.0.0.1:27017 cargo test
许可证
更多信息请查看COPYING文件。
贡献者
感谢以下这些令人惊叹的人使Nongoose更上一层楼
如果你对Nongoose有所帮助,请随时添加此处。
依赖项
~24–36MB
~672K SLoC