Module clean_base::dtos

source ·
Expand description

Defines default Data Transfer Auxiliary structs

This module contains all the auxiliary structs used to transfer data between layers. These structs are used to transfer data between layers and are not intended to be used as entities.

Examples

If you want to create two related structs with database representations and the relationship between them is established by a foreign key, you can use Parent and Children.

struct Post {
   id: i32,
   title: String,
   comments: Children<Comment, i32>,
}

struct Comment {
    post: Parent<Post, i32>,
    id: i32,
    text: String,
}

Note that the relationship between Post and Comment is established by the foreign key parent in Comment. This foreign key is represented by the Parent struct. The Parent receives two generic parameters, the first one is the type of the record and the second one is the type of the primary key. In this case, the first parameter is Post and the second one is i32.

This, the instance representation of Post with comments as IDs would be:

let post_with_comments_as_ids = Post {
    id: 1,
    title: "Hello World".to_string(),
    comments: Children::Ids(vec![1, 2, 3]),
}

let post_with_comments_as_records = Post {
    id: 1,
    title: "Hello World".to_string(),
    comments: Children::Records(vec![
        Comment {
            post: Parent::Id(1),
            id: 1,
            text: "Hello World from comment 1".to_string(),
        },
        Comment {
            post: Parent::Id(1),
            id: 2,
            text: "Hello World from comment 2".to_string(),
        },
        Comment {
            post: Parent::Id(1),
            id: 3,
            text: "Hello World from comment 3".to_string(),
        },
    ]),
}

Modules

  • This import was keep by compatibility purposes only

Structs

Enums