Actix-web and MongoDB REST API

Hi everyone! I'm testing to write a REST API using Actix-web and MongoDB.
I'm having difficulty finding the best way to handle the dynamics of BSON Documents into Rust structures, especially with Documents with references to other Documents or projections.

I think that using only the Bson Documents without modeling the data in structures can become very tedious and it is also not RUST way.
Is there an idiomatic way to handle this type of situation?

This is the an example model I'm trying to structure...

To some thing like:

pub struct Person {
    #[serde(rename = "_id")]
    id: Option<ObjectId>,
    email: Option<String>,
    birth_date: Option<String>,
    ...
}

pub struct User {
    #[serde(rename = "_id")]
    pub id: Option<ObjectId>,
    pub fk_people: Option<ObjectId>,
    pub password: Option<String>,
    pub permissions: Option<Vec<Organization>>,
}

pub struct Organization {
    #[serde(rename = "_id")]
    pub id: Option<ObjectId>,
    pub name: Option<String>,
    pub fk_branch: Option<ObjectId>,,  // Child reference
    pub branches: Option<Vec<Branch>>,,  // Child Documents 
}

I have to configure all the fields in Option<T> to be able to use projections and also add an additional field (i.e: branches) to store the referenced sub-document if I want to join thats sub-documents.

Thanks!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.