Mongoose

```rust use asynctrait::asynctrait; use bson::doc; use chrono::{DateTime, Utc}; use mongodb::{options::IndexOptions, Database, IndexModel}; use serde::{Deserialize, Serialize}; use mongoose::Model;

[derive(Debug, Deserialize, Serialize, Clone)]

pub struct User { #[serde(rename = "id")] pub id: String, pub age: u32, pub username: String, #[serde(with = "bson::serdehelpers::chronodatetimeasbsondatetime")] pub createdat: DateTime, #[serde(with = "bson::serdehelpers::chronodatetimeasbsondatetime")] pub updated_at: DateTime, }

[async_trait]

impl Model for User { fn collectionname<'a>() -> &'a str { "users" } async fn createindexes(db: &Database) { let usernameindex = IndexModel::builder() .keys(doc! { "username": 1 }) .options(IndexOptions::builder().unique(true).build()) .build(); let indexes = [usernameindex]; if let Err(err) = db .collection::(Self::collectionname()) .createindexes(indexes, None) .await { tracing::error!( "error creating {:?} indexes: {:?}", Self::collectionname(), err ); } tracing::debug!("indexes created for {:?}", Self::collectionname()); } }

impl Default for User { fn default() -> Self { let now = chrono::Utc::now(); Self { id: Self::generateid(), age: u32::default(), username: String::new(), createdat: now, updated_at: now, } } } ```