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
//! Control and customize the query execution and its response.
/// Controls the sorting order when listing the entities from the database
///
/// `ListDirection` enum is used to specify order when listing entities from the database using [`EsRepo`][crate::EsRepo]
/// generated functions like `list_by`, `list_for` and `list_for_filters`. Has two variants: `Ascending` and `Descending`
///
/// # Examples
///
/// ```ignore
/// // List users by ID in ascending order (oldest first)
/// let paginated_users_asc = users.list_by_id(
/// PaginatedQueryArgs { first: 5, after: None },
/// ListDirection::Ascending, // or just Default::default()
/// ).await?
///
/// // List users by name in descending order (Z to A)
/// let paginated_users_desc = users.list_by_name(
/// PaginatedQueryArgs { first: 5, after: None },
/// ListDirection::Descending,
/// ).await?
/// ```
/// Structure to sort entities on a specific field when listing from database
///
/// Sort enum is used to specify the sorting order and the field to sort the entities by when listing them using `list_for_filters`
/// generated by [`EsRepo`][crate::EsRepo]. It is generated automatically for all fields which have `list_by` option. It encapsulates two fields,
/// first is `by` to specify the field name, second is `direction` which takes [`ListDirection`]
///
/// # Example
///
/// ```ignore
/// let result = users.list_for_filters(
/// UserFilters {
/// name: Some("Murphy".to_string()),
/// },
/// Sort {
/// // `UserSortBy::Id` and `UserSortBy::CreatedAt` are created by default,
/// // other columns need `list_by` to sort by
/// by: UserSortBy::Id,
/// direction: ListDirection::Descending,
/// },
/// PaginatedQueryArgs {
/// first: 10,
/// after: Default::default(),
/// },
/// )
/// .await?;
/// ```
/// A cursor-based pagination structure for efficiently paginating through large datasets
///
/// The `PaginatedQueryArgs<T>` encapsulates a `first` field that specifies the count of entities to fetch per query, and an optional `after` field
/// that specifies the cursor to start from for the current query. The `<T>` parameter represents cursor type, which depends on the sorting field.
/// A field's cursor is generated by [`EsRepo`][crate::EsRepo] if it has `list_by` option.
///
/// # Examples
///
/// ```ignore
/// // Initial query - fetch first 10 users after an existing user id
/// let query_args = PaginatedQueryArgs {
/// first: 10,
/// after: Some(user_cursor::UserByIdCursor {
/// id: some_existing_user_id // assume this variable exists
/// }),
/// };
///
/// // Execute query using `query_args` argument of `PaginatedQueryArgs` type
/// let result = users.list_by_id(query_args, ListDirection::Ascending).await?;
///
/// // Continue pagination using the updated `next_query_args` of `PaginatedQueryArgs` type
/// if result.has_next_page {
/// let next_query_args = PaginatedQueryArgs {
/// first: 10,
/// after: result.end_cursor, // Use cursor from previous result to update `after`
/// };
/// let next_result = users.list_by_id(next_query_args, ListDirection::Ascending).await?;
/// }
/// ```
/// Return type for paginated queries containing entities and pagination metadata
///
/// `PaginatedQueryRet` contains the fetched entities and utilities for continuing pagination.
/// Returned by the [`EsRepo`][crate::EsRepo] functions like `list_by`, `list_for` and `list_for_filters`.
/// Used with [`PaginatedQueryArgs`] to perform consistent and efficient pagination
///
/// # Examples
///
/// ```ignore
/// let query_args = PaginatedQueryArgs {
/// first: 10,
/// after: None,
/// };
///
/// // Execute query and get the `result` of type `PaginatedQueryRet`
/// let result = users.list_by_id(query_args, ListDirection::Ascending).await?;
///
/// // Continue pagination using the `next_query_args` argument updated using result
/// // Will continue only if 'has_next_page` returned from `result` is true
/// if result.has_next_page {
/// let next_query_args = PaginatedQueryArgs {
/// first: 10,
/// after: result.end_cursor, // update with 'end_cursor' of previous result
/// };
/// let next_result = users.list_by_id(next_query_args, ListDirection::Ascending).await?;
/// }
///
/// // Or use PaginatedQueryRet::into_next_query() convenience method
/// if let Some(next_query_args) = result.into_next_query() {
/// let next_result = users.list_by_id(next_query_args, ListDirection::Ascending).await?;
/// }
/// ```