#diesel #query-builder #pagination #page #paginate #per-page

length_aware_paginator

Add functionality to diesel query builder to return length aware paginated response

3 releases (1 stable)

1.0.0 Apr 27, 2022
0.3.0 May 11, 2021
0.1.0 Feb 13, 2021

#1321 in Database interfaces

Download history 199/week @ 2023-12-08 121/week @ 2023-12-15 43/week @ 2023-12-22 101/week @ 2024-01-05 129/week @ 2024-01-12 142/week @ 2024-01-19 61/week @ 2024-01-26 49/week @ 2024-02-02 49/week @ 2024-02-09 183/week @ 2024-02-16 134/week @ 2024-02-23 86/week @ 2024-03-01 103/week @ 2024-03-08 143/week @ 2024-03-15 68/week @ 2024-03-22

417 downloads per month

MIT license

12KB
135 lines

Crates.io

length_aware_paginator

length_aware_paginator = "1.0.0"

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.

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);
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~3MB
~64K SLoC