Derive Macro reindeer::Entity

source ·
#[derive(Entity)]
{
    // Attributes available to this derive:
    #[entity]
    #[children]
    #[siblings]
}
Expand description

Derive macro for reindeer’s 🦌 Entity trait.

To automatically derive Entity on a struct, you simply have to derive Entity (as Well as serde’s Serialize and Deserialize traits) like so:

#[derive(Serialize,Deserialize,Entity)]
struct User {
    id : (u32,u32),
    email : String,
    username : String,
    last_login : i64,
    password_hash : String,
}

☝😉 This will generate an Entity implementation with store name User, version 0, and id being the id field. To specify other values, use the helper attribute entity like so :

#[derive(Serialize,Deserialize,Entity)]
#[entity(name = "user", version = 1,id = "email")]
struct User {
    email : String,
    username : String,
    last_login : i64,
    password_hash : String,
}

To specify sibling entities and child entities, use the sibling and child helper attributes respectively:

#[derive(Serialize,Deserialize,Entity)]
#[entity(name = "user", version = 1,id = "email")]
#[sibling(("user_data", Cascade))]
#[children(("doc",Cascade),("shared_doc",BreakLink))]
struct User {
    email : String,
    username : String,
    last_login : i64,
    password_hash : String,
}
 
#[derive(Serialize,Deserialize,Entity)]
#[entity(name = "user_data", version = 1,id = "email")]
#[sibling(("user", Error))]
struct UserData {
    email : String,
    username : String,
    last_login : i64,
    password_hash : String,
}

The second part of each relation is a reindeer::DeletionBehaviour enum value : BreakLink,Cascade, or Error.