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
use DeserializeRow;
use SerializeRow;
///
/// Model is a trait that defines the basic structure of a table in the database.
/// It is used to generate the necessary code for the charybdis orm.
/// Macro 'charybdis_model` generates the necessary code for implementation so you don't have
/// to write it manually. The macro is used in the following way:
/// ```rust
/// use charybdis::macros::charybdis_model;
/// use charybdis::types::{Text, Timestamp, Uuid};
///
/// #[charybdis_model(
/// table_name = users,
/// partition_keys = [id],
/// clustering_keys = [],
/// global_secondary_indexes = []
/// )]
/// pub struct User {
/// pub id: Uuid,
/// pub username: Text,
/// pub password: Text,
/// pub hashed_password: Text,
/// pub email: Text,
/// pub first_name: Option<Text>,
/// pub last_name: Option<Text>,
/// pub created_at: Timestamp,
/// pub updated_at: Timestamp,
/// }
///
/// ```
/// These structure is used by smart `migration` tool that automatically migrate the database
/// schema from the code.
/// It detects changes in the model and automatically applies the changes to the database.
///
/// If you have migration package installed, you can run the `migrate` command to automatically
/// migrate the database schema without having to write any CQL queries.
///
///
/// MaterializedView is a trait that defines the basic structure of materialized view.
/// It is used to generate the necessary code for the charybdis orm.
/// Macro 'charybdis_view_model` generates the necessary code for implementation
/// so you don't have to write it manually.
/// ```rust
/// use charybdis::macros::charybdis_view_model;
/// use charybdis::types::{Text, Timestamp, Uuid};
///
/// #[charybdis_view_model(
/// table_name=users_by_username,
/// base_table=users,
/// partition_keys=[username],
/// clustering_keys=[id]
/// )]
/// pub struct UsersByUsername {
/// pub username: Text,
/// pub id: Uuid,
/// pub email: Text,
/// pub created_at: Option<Timestamp>,
/// pub updated_at: Option<Timestamp>,
/// }
/// ```
/// Resulting auto-generated migration query will be:
///
/// ```sql
/// CREATE MATERIALIZED VIEW IF NOT EXISTS users_by_email
/// AS SELECT created_at, updated_at, username, email, id
/// FROM users
/// WHERE email IS NOT NULL AND id IS NOT NULL
/// PRIMARY KEY (email, id)
/// ```
///
/// Declare udt model as a struct within `src/models/udts` dir:
/// ```rust
/// use charybdis::macros::charybdis_udt_model;
/// use charybdis::types::Text;
///
/// #[charybdis_udt_model(type_name = address)]
/// pub struct Address {
/// pub street: Text,
/// pub city: Text,
/// pub state: Text,
/// pub zip: Text,
/// pub country: Text,
/// }
/// ```
/// In extension of partial_model!() in case you need to build native model you can use `as_native` method:
/// ```rust
/// use charybdis::macros::charybdis_model;
/// use charybdis::types::{Text, Timestamp, Uuid};
/// use charybdis::model::AsNative;
///
/// #[charybdis_model(
/// table_name = users,
/// partition_keys = [id],
/// clustering_keys = [],
/// global_secondary_indexes = []
/// )]
/// #[derive(Default)]
/// pub struct User {
/// pub id: Uuid,
/// pub username: Text,
/// pub password: Text,
/// pub hashed_password: Text,
/// pub email: Text,
/// pub first_name: Option<Text>,
/// pub last_name: Option<Text>,
/// pub created_at: Timestamp,
/// pub updated_at: Timestamp,
/// }
///
/// partial_user!(UpdateUsernameUser, id, username);
///
/// impl UpdateUsernameUser {
/// pub fn native(&self) -> User {
/// self.as_native()
/// }
/// }
/// ```