Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/imthesquid/goldleaf
A thin wrapper around MongoDB to make it shine!
https://github.com/imthesquid/goldleaf
database mongodb odm orm rust
Last synced: 6 days ago
JSON representation
A thin wrapper around MongoDB to make it shine!
- Host: GitHub
- URL: https://github.com/imthesquid/goldleaf
- Owner: ImTheSquid
- License: apache-2.0
- Created: 2023-10-31T20:59:47.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-11-18T18:37:41.000Z (12 months ago)
- Last Synced: 2024-11-08T20:09:21.788Z (6 days ago)
- Topics: database, mongodb, odm, orm, rust
- Language: Rust
- Homepage:
- Size: 75.2 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# Goldleaf
A thin wrapper over MongoDB to make it shine! Goldleaf uses struct field annotations to manage indexing, while also providing an `auto_collection` function for easy collection access.
Here's a small example of annotations:
```rust
#[derive(Serialize, Deserialize, CollectionIdentity, Debug, Default)]
#[db(name = "user")]
pub struct User {
/// The unique ID of the user
#[db(native_id_field)]
#[serde(rename = "_id", skip_serializing_if = "Option::is_none")]
pub id: Option,
/// The name of the user
pub name: String,
/// The unique username of the user
#[db(indexing(index = 1, unique))]
pub username: String,
/// When the user was last active
#[serde(with = "chrono_datetime_as_bson_datetime")]
pub last_active: DateTime,
/// The user's email
#[db(indexing(index = 1, unique, pfe = r#""email": {"$type": "string"}"#))]
pub email: Option,
/// The user's phone number
#[db(indexing(index = 1, unique))]
pub phone_number: String,
/// The user's sessions
#[db(indexing(
index = 1,
unique,
sub = "token",
pfe = r#""sessions.token": {"$exists": true}"#
))]
pub sessions: Vec,
}
```Don't forget the annotation on your `id` field! Use `native_id_field` to prefix your ID field with an underscore. Otherwise, just use `id_field`.
To use the indices, call this function early in your code:
```rust
User::create_indices(&database).await?;
```
Note: This function will only exist if you create indices on your collection.The API for MongoDB is mostly the same, but collection names are now statically-applied:
```rust
let users = db.auto_collection::();
let username_matches = users
.find_one(doc! {"username": &info.username}, None)
.await?;
```Given an instance of a struct, `save` can also be called:
```rust
user.save(&db).await?;
```