1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//! 实体定义模块
//!
//! 提供实体与数据库表的映射关系
use crate::;
use Value;
/// 数据库实体 trait
///
/// 定义实体与数据库表的映射关系
///
/// # Example
///
/// ```ignore
/// struct User {
/// id: i64,
/// name: String,
/// email: String,
/// }
///
/// impl Entity for User {
/// type Id = i64;
///
/// fn table_name() -> &'static str {
/// "users"
/// }
///
/// fn id(&self) -> Self::Id {
/// self.id
/// }
/// }
/// ```
/// 从数据库行解析实体
/// 将实体转换为数据库行
/// 一对多关系 trait
///
/// 定义主实体与从实体的一对多关系
///
/// # Example
///
/// ```ignore
/// struct User {
/// id: i64,
/// posts: Vec<Post>,
/// }
///
/// struct Post {
/// id: i64,
/// user_id: i64,
/// }
///
/// impl HasMany<Post> for User {
/// fn foreign_key() -> &'static str {
/// "user_id"
/// }
///
/// fn local_key() -> &'static str {
/// "id"
/// }
/// }
/// ```
/// 一对多关系的反向 trait
///
/// 定义从实体与主实体的多对一关系
///
/// # Example
///
/// ```ignore
/// struct Post {
/// id: i64,
/// user_id: i64,
/// user: Option<User>,
/// }
///
/// impl BelongsTo<User> for Post {
/// fn foreign_key() -> &'static str {
/// "user_id"
/// }
///
/// fn owner_key() -> &'static str {
/// "id"
/// }
/// }
/// ```
/// 多对多关系 trait
///
/// 定义实体之间的多对多关系
///
/// # Example
///
/// ```ignore
/// struct User {
/// id: i64,
/// roles: Vec<Role>,
/// }
///
/// struct Role {
/// id: i64,
/// users: Vec<User>,
/// }
///
/// impl ManyToMany<Role> for User {
/// fn join_table() -> &'static str {
/// "user_roles"
/// }
///
/// fn local_key() -> &'static str {
/// "user_id"
/// }
///
/// fn foreign_key() -> &'static str {
/// "role_id"
/// }
/// }
/// ```