0.1.0 |
|
---|
#5 在 #paginate
11KB
127 行
lenght_aware_paginator
lenght_aware_paginator = "0.1.0"
长度感知分页器使您能够分页Diesel查询并获得正在分页的数据长度的信息。它将为您提供项目总数,以及您可以导航到的最后一页,并且仍然可以获取某种类型的数据。
您只需要提供页码和每页参数。
use diesel::pg::PgConnection;
use diesel::Connection;
use diesel::QueryDsl;
use lenght_aware_paginator::{LoadPaginated, Response};
use serde::{Deserialize, Serialize};
// 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,
}
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))
}
#[test]
fn test_orm_query_pagination() {
let connection = get_connection();
// Use `lenght_aware_paginator::LoadPaginated` trait to enable
// using the `load_paginated` method on your query.
// Your query will return `lenght_aware_paginator::Response<T>` struct
let response: Response<User> = schema::users::table
.into_boxed()
.load_paginated(connection, page, per_page)
.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版本,(LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
- MIT许可证 (LICENSE-MIT 或 https://opensource.org/licenses/MIT)
。
贡献
除非您明确表示,否则根据Apache-2.0许可证定义,您提交的任何旨在包含在作品中的有意贡献将根据上述许可进行双重许可,没有任何附加条款或条件。
依赖项
~4MB
~88K SLoC