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
//! Length aware paginator enables you to paginate Diesel queries and have information about the length
//! of data being paginated. It will give you total number of items, and last page that you can navigate to
//! and still get some kind of data.
//!
//! You will only have to provide page and per_page parameters.
//!
//! ```ignore
//! use diesel::pg::PgConnection;
//! use diesel::Connection;
//! use diesel::QueryDsl;
//! use length_aware_paginator::{Paginate, Response};
//! use serde::{Deserialize, Serialize};
//!
//! /// Get the database connection
//! /// *panics* if no DATABASE_URL is defined in the env or if the db is unreachable
//! fn get_connection() -> PgConnection {
//!     let database_url =
//!         dotenv::var("DATABASE_URL").expect("You have to provide DATABASE_URL to run tests");
//!
//!     PgConnection::establish(&database_url)
//!         .unwrap_or_else(|_| panic!("Error connecting to {}", &database_url))
//! }
//!
//! // schema.rs : autogenerated by diesel after running migration
//! table! {
//!     users (id) {
//!         id -> Int4,
//!         email -> Varchar,
//!         first_name -> Varchar,
//!         last_name -> Varchar,
//!         password -> Varchar,
//!     }
//! }
//!
//! // user.rs : your model for the table represented in schema.rs
//! #[derive(Queryable, Deserialize, Serialize)]
//! pub struct User {
//!     id: i32,
//!     email: String,
//!     first_name: String,
//!     last_name: String,
//!     password: String,
//! }
//!
//! #[test]
//! fn test_orm_query_pagination() {
//!     let mut connection = get_connection();
//!
//!     // Use `length_aware_paginator::LoadPaginated` trait to enable
//!     // using the `load_paginated` method on your query.
//!     // Your query will return `length_aware_paginator::Response<T>` struct
//!     let response: Response<User> = schema::users::table
//!         .into_boxed()
//!         .page(Some(1))
//!         .per_page(Some(10))
//!         .load_paginated(&mut connection)
//!         .unwrap();
//!
//!     assert_eq!(response.page, 1);
//!     assert_eq!(response.per_page, 10);
//!     assert_eq!(response.total, 15);
//!     assert_eq!(response.last_page, 2);
//!     assert_eq!(response.data.len(), 10);
//! }
//! ```

#[macro_use]
extern crate diesel;

mod structs;
mod traits;

pub use structs::Response;
pub use traits::Paginate;