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

length_aware_paginator

为diesel查询构建器添加功能,以返回长度感知的分页响应

3个版本 (1个稳定版本)

1.0.0 2022年4月27日
0.3.0 2021年5月11日
0.1.0 2021年2月13日

#1539 in 数据库接口

Download history 115/week @ 2024-03-10 140/week @ 2024-03-17 68/week @ 2024-03-24 77/week @ 2024-03-31 59/week @ 2024-04-07 2/week @ 2024-04-14 58/week @ 2024-04-21 45/week @ 2024-04-28 70/week @ 2024-05-05 57/week @ 2024-05-12 58/week @ 2024-05-19 29/week @ 2024-05-26 67/week @ 2024-06-02 79/week @ 2024-06-09 59/week @ 2024-06-16 40/week @ 2024-06-23

每月250次下载

MIT 协议

12KB
135

Crates.io

length_aware_paginator

length_aware_paginator = "1.0.0"

长度感知分页器使您能够分页Diesel查询并了解正在分页的数据长度。它将为您提供项目总数以及您可以导航到的最后一页,并仍然获得某种类型的数据。

您只需提供页码和每页数量参数。

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

许可证

在以下任一许可证下授权

任选其一。

贡献

除非您明确声明,否则您提交给工作以供包含的任何贡献,根据Apache-2.0许可证定义,应如上双授权,没有任何额外条款或条件。

依赖

~3.5MB
~75K SLoC