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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#![doc = include_str!("../README.md")]
#![forbid(unsafe_code)]
#![warn(
    clippy::cast_lossless,
    clippy::cast_possible_truncation,
    clippy::cast_possible_wrap,
    clippy::cast_precision_loss,
    clippy::cast_sign_loss,
    clippy::checked_conversions,
    clippy::implicit_saturating_sub,
    clippy::integer_arithmetic,
    clippy::mod_module_files,
    clippy::panic,
    clippy::panic_in_result_fn,
    clippy::unwrap_used,
    missing_docs,
    rust_2018_idioms,
    unused_lifetimes,
    unused_qualifications
)]

//! ### Usage:
//! The usage is a bit different than the node version. See the examples for more details and a working example.
//! ```rust
//! use mongodb::{options::FindOptions, Client};
//! use mongodb_cursor_pagination::{FindResult, Pagination};
//! use bson::doc;
//! use serde::Deserialize;
//!
//! // Note that your data structure must derive Deserialize
//! #[derive(Debug, Deserialize, PartialEq, Clone)]
//! pub struct MyFruit {
//!     name: String,
//!     how_many: i32,
//! }
//! #  impl MyFruit {
//! #     #[must_use]
//! #     pub fn new(name: impl Into<String>, how_many: i32) -> Self {
//! #         Self {
//! #             name: name.into(),
//! #             how_many,
//! #         }
//! #     }
//! # }
//!
//! #[tokio::main]
//! async fn main() {
//!     let client = Client::with_uri_str("mongodb://localhost:27017/")
//!         .await
//!         .expect("Failed to initialize client.");
//!     let db = client.database("mongodb_cursor_pagination");
//!     let fruits = db.collection::<MyFruit>("myfruits");
//!   #  db.collection::<MyFruit>("myfruits")
//!   #      .drop(None)
//!   #      .await
//!   #      .expect("Failed to drop table");
//!
//!     let docs = vec![
//!         doc! { "name": "Apple", "how_many": 5 },
//!         doc! { "name": "Orange", "how_many": 3 },
//!         doc! { "name": "Blueberry", "how_many": 25 },
//!         doc! { "name": "Bananas", "how_many": 8 },
//!         doc! { "name": "Grapes", "how_many": 12 },
//!     ];
//!
//!     db.collection("myfruits")
//!         .insert_many(docs, None)
//!         .await
//!         .expect("Unable to insert data");
//!
//!     // query page 1, 2 at a time
//!     let options = FindOptions::builder()
//!             .limit(2)
//!             .sort(doc! { "name": 1 })
//!             .build();
//!
//!     let mut find_results: FindResult<MyFruit> = fruits
//!         .find_paginated(None, Some(options.clone()), None)
//!         .await
//!         .expect("Unable to find data");
//!   #  assert_eq!(
//!   #     find_results.items,
//!   #     vec![MyFruit::new("Apple", 5), MyFruit::new("Bananas", 8),]
//!   # );
//!     println!("First page: {:?}", find_results);
//!
//!     // get the second page
//!     let mut cursor = find_results.page_info.end_cursor;
//!     find_results = fruits
//!         .find_paginated(None, Some(options), cursor)
//!         .await
//!         .expect("Unable to find data");
//!   #  assert_eq!(
//!   #    find_results.items,
//!   #     vec![MyFruit::new("Blueberry", 25), MyFruit::new("Grapes", 12),]
//!   # );
//!     println!("Second page: {:?}", find_results);
//! }
//! ```
//!
//! ### Response
//! The response `FindResult<T>` contains page info, cursors and edges (cursors for all of the items in the response).
//! ```rust
//! pub struct PageInfo {
//!     pub has_next_page: bool,
//!     pub has_previous_page: bool,
//!     pub start_cursor: Option<String>,
//!     pub next_cursor: Option<String>,
//! }
//!
//! pub struct Edge {
//!     pub cursor: String,
//! }
//!
//! pub struct FindResult<T> {
//!     pub page_info: PageInfo,
//!     pub edges: Vec<Edge>,
//!     pub total_count: i64,
//!     pub items: Vec<T>,
//! }
//! ```
//!
//! ## Features
//! It has support for graphql (using [juniper](https://github.com/graphql-rust/juniper)) if you enable the `graphql` flag. You can use it by just including the `PageInfo` into your code.
//!
//! ```ignore
//! use mongodb_cursor_pagination::{PageInfo, Edge};
//!
//! #[derive(Serialize, Deserialize)]
//! struct MyDataConnection {
//!     page_info: PageInfo,
//!     edges: Vec<Edge>,
//!     data: Vec<MyData>,
//!     total_count: i64,
//! }
//!
//! [juniper::object]
//! impl MyDataConnection {
//!     fn page_info(&self) -> &PageInfo {
//!         self.page_info
//!     }
//!
//!     fn edges(&self) -> &Vec<Edge> {
//!         &self.edges
//!     }
//! }
//! ```

mod error;
mod model;
mod option;

pub use crate::error::*;
pub use crate::model::*;

use crate::option::CursorOptions;
use bson::{doc, Bson, Document};
use error::CursorError;
use futures_util::stream::StreamExt;
use futures_util::TryStreamExt;
use log::warn;
use mongodb::options::CountOptions;
use mongodb::{options::FindOptions, Collection};
use serde::de::DeserializeOwned;

use async_trait::async_trait;

#[async_trait]
/// Used to paginate through a collection.
pub trait Pagination {
    /// Finds the items in the collection matching `filter` based on the `cursor`.
    ///
    /// # Arguments
    /// * `filter`: Optional filter to restrict the result set of the query.
    /// * `options`: Optional find options that you would like to perform any searches with
    /// * `cursor`: An optional existing cursor in base64. This would have come from a previous `FindResult<T>`
    async fn find_paginated<T>(
        &self,
        filter: Option<Document>,
        options: Option<FindOptions>,
        cursor: Option<DirectedCursor>,
    ) -> Result<FindResult<T>, CursorError>
    where
        T: DeserializeOwned + Send;
}

#[async_trait]
impl<I: Send + Sync> Pagination for Collection<I> {
    async fn find_paginated<T>(
        &self,
        filter: Option<Document>,
        options: Option<FindOptions>,
        cursor: Option<DirectedCursor>,
    ) -> Result<FindResult<T>, CursorError>
    where
        T: DeserializeOwned + Send,
    {
        let options = CursorOptions::new(options.unwrap_or_default(), cursor.clone());

        let filter = filter.unwrap_or_default();

        let query = get_query(filter.clone(), &options, cursor.as_ref())?;

        let mut documents = self
            .clone_with_type::<Document>()
            .find(query.clone(), Some(options.clone().into()))
            .await?
            .try_collect::<Vec<Document>>()
            .await?;

        if matches!(cursor, Some(DirectedCursor::Backwards(_))) {
            documents.reverse();
        }

        let items = documents
            .clone()
            .into_iter()
            .map(|doc| bson::from_bson(Bson::Document(doc)))
            .collect::<Result<Vec<T>, _>>()?;

        let edges = documents
            .clone()
            .into_iter()
            .map(|doc| Edge::new(doc, &options))
            .collect::<Vec<Edge>>();

        let end_cursor = edges.last().cloned().map(DirectedCursor::Forward);
        let start_cursor = edges.first().cloned().map(DirectedCursor::Backwards);

        let has_next_page = has_page(
            &self.clone_with_type(),
            filter.clone(),
            options.clone(),
            end_cursor.as_ref(),
        )
        .await?;

        let has_previous_page = has_page(
            &self.clone_with_type(),
            filter.clone(),
            options.clone(),
            start_cursor.as_ref(),
        )
        .await?;

        let page_info = PageInfo {
            has_previous_page,
            has_next_page,
            start_cursor,
            end_cursor,
        };

        Ok(FindResult {
            page_info,
            edges,
            total_count: count_documents(options.clone().into(), self, Some(&filter)).await?,
            items,
        })
    }
}

async fn count_documents<T>(
    mut options: CountOptions,
    collection: &Collection<T>,
    filter: Option<&Document>,
) -> Result<u64, CursorError> {
    options.limit = None;
    options.skip = None;
    let count_query = filter.map_or_else(Document::new, Clone::clone);
    Ok(collection
        .count_documents(count_query, Some(options))
        .await?)
}

/*
$or: [{
    launchDate: { $lt: nextLaunchDate }
}, {
    // If the launchDate is an exact match, we need a tiebreaker, so we use the _id field from the cursor.
    launchDate: nextLaunchDate,
_id: { $lt: nextId }
}]
*/
fn get_query(
    mut filter: Document,
    options: &CursorOptions,
    cursor: Option<&DirectedCursor>,
) -> Result<Document, CursorError> {
    let Some(cursor) = cursor else {
        return Ok(filter);
    };

    let Some(sort) = options.sort.clone() else {
            return Ok(filter);
    };

    // this is the simplest form, it's just a sort by _id
    if sort.len() <= 1 {
        let object_id = cursor
            .inner()
            .get("_id")
            .ok_or(CursorError::InvalidCursor)?
            .clone();
        let direction = if sort.get_i32("_id")? >= 0 {
            "$gt"
        } else {
            "$lt"
        };
        filter.insert("_id", doc! { direction: object_id });
        return Ok(filter);
    }

    let mut queries: Vec<Document> = Vec::new();
    let mut previous_conditions: Vec<(String, Bson)> = Vec::new();

    // Add each sort condition with it's direction and all previous condition with fixed values
    for key in sort.keys() {
        let mut query = filter.clone();
        query.extend(previous_conditions.clone().into_iter()); // Add previous conditions

        let value = cursor.inner().get(key).unwrap_or(&Bson::Null);

        let direction = if sort.get_i32(key)? >= 0 {
            "$gt"
        } else {
            "$lt"
        };

        query.insert(key, doc! { direction: value.clone() });
        previous_conditions.push((key.clone(), value.clone())); // Add self without direction to previous conditions

        queries.push(query);
    }

    filter = if queries.len() > 1 {
        doc! { "$or": queries.iter().as_ref() }
    } else {
        queries.pop().unwrap_or_default()
    };
    Ok(filter)
}

async fn has_page(
    collection: &Collection<Document>,
    filter: Document,
    mut options: CursorOptions,
    cursor: Option<&DirectedCursor>,
) -> Result<bool, CursorError> {
    let Some(cursor) = cursor else {
        return Ok(false);
    };

    options.set_cursor(cursor.clone());
    options.skip = None;
    let filter = get_query(filter, &options, Some(cursor))?;

    Ok(collection
        .find(Some(filter), Some(options.into()))
        .await?
        .next()
        .await
        .transpose()?
        .is_some())
}