-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpg_query_model_first.rs
More file actions
246 lines (220 loc) · 5.87 KB
/
pg_query_model_first.rs
File metadata and controls
246 lines (220 loc) · 5.87 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use vitrail_pg::{
DeleteMany, InsertInput, InsertResult, QueryResult, QueryVariables, UpdateData, UpdateMany,
VitrailClient, schema, uuid::Uuid,
};
schema! {
name my_schema
model user {
id Int @id @default(autoincrement())
external_id String @unique @db.Uuid
email String @unique
name String
created_at DateTime @default(now())
posts post[]
}
model post {
id Int @id @default(autoincrement())
title String
body String?
published Boolean
author_id Int
created_at DateTime @default(now())
author user @relation(fields: [author_id], references: [id])
}
}
#[allow(dead_code)]
#[derive(QueryResult)]
#[vitrail(
schema = crate::my_schema::Schema,
model = post,
order_by(title = desc),
skip = 0,
limit = 1
)]
struct PostSummary {
id: i64,
title: String,
}
#[allow(dead_code)]
#[derive(InsertInput)]
#[vitrail(schema = crate::my_schema::Schema, model = user)]
struct NewUser {
external_id: Uuid,
email: String,
name: String,
}
#[allow(dead_code)]
#[derive(InsertResult)]
#[vitrail(schema = crate::my_schema::Schema, model = user, input = NewUser)]
struct InsertedUser {
id: i64,
external_id: Uuid,
email: String,
name: String,
}
#[allow(dead_code)]
#[derive(InsertInput)]
#[vitrail(schema = crate::my_schema::Schema, model = post)]
struct NewPost {
title: String,
body: Option<String>,
published: bool,
author_id: i64,
}
#[allow(dead_code)]
#[derive(InsertResult)]
#[vitrail(schema = crate::my_schema::Schema, model = post, input = NewPost)]
struct InsertedPost {
id: i64,
title: String,
published: bool,
author_id: i64,
}
#[derive(QueryVariables)]
struct UserByIdVariables {
user_id: i64,
}
#[derive(QueryVariables)]
struct PostsByAuthorEmailVariables {
author_email: String,
}
#[derive(QueryVariables)]
struct PostByExcludedTitleVariables {
excluded_title: String,
}
#[derive(QueryVariables)]
struct PostByIdsVariables {
post_ids: Vec<i64>,
}
#[allow(dead_code)]
#[derive(UpdateData)]
#[vitrail(schema = crate::my_schema::Schema, model = post)]
struct PublishPostsData {
published: bool,
}
#[allow(dead_code)]
#[derive(UpdateMany)]
#[vitrail(
schema = crate::my_schema::Schema,
model = post,
data = PublishPostsData,
variables = PostsByAuthorEmailVariables,
where(author.email = eq(author_email))
)]
struct PublishPostsByAuthorEmail;
#[allow(dead_code)]
#[derive(DeleteMany)]
#[vitrail(
schema = crate::my_schema::Schema,
model = post,
variables = PostByExcludedTitleVariables,
where(title = not(excluded_title))
)]
struct DeletePostsByExcludedTitle;
#[allow(dead_code)]
#[derive(QueryResult)]
#[vitrail(
schema = crate::my_schema::Schema,
model = user,
variables = UserByIdVariables,
where(id = eq(user_id))
)]
struct UserWithPosts {
id: i64,
external_id: Uuid,
email: String,
name: String,
#[vitrail(include)]
posts: Vec<PostSummary>,
}
#[allow(dead_code)]
#[derive(QueryResult)]
#[vitrail(
schema = crate::my_schema::Schema,
model = post,
variables = PostByIdsVariables,
where(id = in(post_ids)),
order_by(title = desc),
skip = 0,
limit = 1
)]
struct PostByIds {
id: i64,
title: String,
}
#[tokio::main]
async fn main() {
let client = VitrailClient::new("postgres://postgres:postgres@127.0.0.1:5432/vitrail")
.await
.unwrap();
let external_id = Uuid::parse_str("aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa").unwrap();
let user = client
.insert(my_schema::insert::<InsertedUser>(NewUser {
external_id,
email: "alice@example.com".to_owned(),
name: "Alice".to_owned(),
}))
.await
.unwrap();
let hello_post = client
.insert(my_schema::insert::<InsertedPost>(NewPost {
title: "Hello Vitrail".to_owned(),
body: Some("Draft body".to_owned()),
published: false,
author_id: user.id,
}))
.await
.unwrap();
let draft_post = client
.insert(my_schema::insert::<InsertedPost>(NewPost {
title: "Untitled draft".to_owned(),
body: None,
published: false,
author_id: user.id,
}))
.await
.unwrap();
let updated_posts = client
.update_many(my_schema::update_many_with_variables::<
PublishPostsByAuthorEmail,
>(
PostsByAuthorEmailVariables {
author_email: "alice@example.com".to_owned(),
},
PublishPostsData { published: true },
))
.await
.unwrap();
let deleted_posts = client
.delete_many(my_schema::delete_many_with_variables::<
DeletePostsByExcludedTitle,
>(PostByExcludedTitleVariables {
excluded_title: "Hello Vitrail".to_owned(),
}))
.await
.unwrap();
let users = client
.find_many(my_schema::query_with_variables::<UserWithPosts>(
UserByIdVariables { user_id: user.id },
))
.await
.unwrap();
let posts = client
.find_many(my_schema::query_with_variables::<PostByIds>(
PostByIdsVariables {
post_ids: vec![hello_post.id, draft_post.id],
},
))
.await
.unwrap();
println!("inserted user {} ({})", user.email, user.external_id);
println!("updated {} posts", updated_posts);
println!("deleted {} posts", deleted_posts);
println!("fetched {} users", users.len());
println!("latest paginated user post: {}", users[0].posts[0].title);
println!(
"fetched {} paginated posts with an in(...) filter",
posts.len()
);
println!("first paginated ordered post: {}", posts[0].title);
}