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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//! # edgedb-composable-query
//!
//! Query arbitrary structs from EdgeDB. Compose queries of arbitrary complexity.
//!
//! Beware: it's very much a work-in-progress. Pre-0.1. It's messy,
//! there're todo!()'s in the code, etc. I'm still figuring out the semantics.
//! If you're interested in this working for your use-cases, please
//! try it and file the issues at: <https://github.com/valyagolev/edgedb-composable-query/issues>.
//! But don't use it seriously yet; it might panic, and *will* change the API.
//!
//! Two major parts of the crate.
//!
//! 1. A set of tools, around the [`EdgedbObject`] derivable trait, that allow you to query
//! arbitrary rust structs from EdgeDB, converting types automatically. See examples below.
//! 2. A set of tools, around the [`composable::EdgedbComposableQuery`] derivable trait, that allow you express
//! complex, composable queries through Rust structs and attributes. See docs and examples in the [composable] submodule.
//!
//! # EdgedbObject Examples
//!
//! If you have this schema:
//!
//! ```edgedb
//! module default {
//! type Inner {
//! required req: str;
//! opt: str;
//! }
//! type Outer {
//! inner: Inner;
//!
//! some_field: str;
//! required other_field: str;
//! }
//! ```
//!
//! Here're some of the ways to use `EdgedbObject`:
//!
//! ```
//! # tokio_test::block_on(async {
//! use edgedb_composable_query::{query, EdgedbObject, EdgedbSetValue, Ref};
//!
//! #[derive(Debug, PartialEq, EdgedbObject)]
//! struct AdHocStruct {
//! a: String,
//! b: Option<String>,
//! }
//!
//! #[derive(Debug, PartialEq, EdgedbObject)]
//! struct Inner {
//! req: String,
//! opt: Option<String>,
//! }
//!
//!
//! // typically you want to use Ref<T> to refer to a type
//! // Ref<T> is basically UUID and an Option<T>
//!
//! #[derive(Debug, PartialEq, EdgedbObject)]
//! struct Outer {
//! inner: Option<Ref<Inner>>,
//! some_field: Option<String>,
//! other_field: String,
//! }
//!
//! let conn = edgedb_tokio::create_client().await?;
//!
//! assert_eq!(query::<i64, _>(&conn, "select 7*8", ()).await?, 56);
//!
//! // use primitive params
//! assert_eq!(
//! query::<Vec<i64>, _>(&conn, "select {1 * <int32>$0, 2 * <int32>$0}", (22,)).await?,
//! vec![22, 44]
//! );
//!
//! // ad-hoc objects:
//! assert_eq!(
//! query::<AdHocStruct, _>(&conn, "select { a := 'aaa', b := <str>{} }", ()).await?,
//! AdHocStruct {
//! a: "aaa".to_string(),
//! b: None
//! }
//! );
//!
//! assert_eq!(
//! query::<AdHocStruct, _>(&conn, "select { a:= 'aaa', b:=<str>{'cc'} }", ()).await?,
//! AdHocStruct {
//! a: "aaa".to_string(),
//! b: Some("cc".to_string())
//! }
//! );
//!
//! // cardinality mismatch:
//! assert!(
//! query::<AdHocStruct, _>(&conn, "select {a := 'aaa',b := <str>{'cc', 'dd'}}", ())
//! .await
//! .is_err()
//! );
//!
//! // look up some objects
//! assert!(
//! dbg!(
//! query::<Vec<Inner>, _>(&conn, "select Inner {req, opt}", ())
//! .await?
//! ).len()
//! > 0
//! );
//!
//! // use ref if you need ids
//! assert!(
//! dbg!(
//! query::<Vec<Ref<Inner>>, _>(&conn, "select Inner {id, req, opt}", ())
//! .await?
//! ).len()
//! > 0
//! );
//!
//!
//! // ref doesn't need the rest of the object
//! assert!(
//! dbg!(
//! query::<Vec<Ref<Inner>>, _>(&conn, "select Inner {id}", ())
//! .await?
//! ).len()
//! > 0
//! );
//!
//! // cardinality mismatch:
//! assert!(
//! query::<Ref<Inner>, _>(&conn, "select Inner {id}", ())
//! .await
//! .is_err()
//! );
//!
//! // you can query things with refs in them:
//!
//! let vs = query::<Vec<Outer>, _>(&conn, "select Outer {inner, some_field, other_field}", ())
//! .await?;
//!
//! assert!(vs.len() > 0);
//!
//!
//! // refs picked up only ids here
//! assert!(
//! vs.iter()
//! .filter_map(|v| v.inner.as_ref())
//! .all(|inner_ref| inner_ref.known_value.is_none())
//! );
//!
//!
//!
//! // if you want the whole object with Ref, don't forget to provide 'id' selector
//! let vs = query::<Vec<Outer>, _>(&conn, "
//! select Outer {
//! inner: { id, req, opt },
//! some_field,
//! other_field
//! }
//! ", ())
//! .await?;
//!
//! assert!(vs.len() > 0);
//!
//! // refs picked up the whole objects
//! assert!(
//! vs.iter()
//! .filter_map(|v| v.inner.as_ref())
//! .all(|inner_ref| inner_ref.known_value.is_some())
//! );
//!
//! # anyhow::Ok(())
//! # }).unwrap();
//! ```
/// currently anyhow::Result. TODO: make crate's own Result type
pub use Result;
pub use itertools as __itertools;
use Client;
pub use EdgedbQueryArgs;
pub use ;
/// Express complex, composable queries through Rust structs and attributes.
extern crate self as edgedb_composable_query;
pub use EdgedbObject;
pub use Ref;
use ;
pub use nonempty;
pub use ;
/// Struct that can be received from EdgeDB as an Object. Derive this trait for your structs.
/// Entry-point for querying `EdgedbObject`s of arbitrary cardinality.
pub async