Crates.io | sqlx-paginated |
lib.rs | sqlx-paginated |
version | |
source | src |
created_at | 2024-11-11 17:22:11.00222 |
updated_at | 2024-12-12 17:30:00.477506 |
description | A flexible, type-safe SQLx query builder for dynamic web APIs, offering seamless pagination, searching, filtering, and sorting. |
homepage | |
repository | https://github.com/alexandrughinea/sqlx-paginated |
max_upload_size | |
id | 1443999 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
A flexible, type-safe SQLx query builder for dynamic web APIs, offering seamless pagination, searching, filtering, and sorting.
Database | Status | Version | Features | Notes |
---|---|---|---|---|
PostgreSQL | ✅ Supported | 12+ | All features supported | Ready |
SQLite | 🚧 Planned | 3.35+ | Basic features planned | Development starting in Q1 2025 |
MySQL | 🚧 Planned | 8.0+ | Core features planned | On roadmap |
⚠️ Note: This documentation covers PostgreSQL features only, as it's currently the only supported database.
Query builders
Missing features in existing solutions
Actix Web handler example
use sqlx_paginated::{paginated_query_as, FlatQueryParams};
use actix_web::{web, Responder, HttpResponse};
async fn list_users(web::Query(params): web::Query<FlatQueryParams>) -> impl Responder {
let paginated_users = paginated_query_as!(User, "SELECT * FROM users")
.with_params(params)
.fetch_paginated(&pool)
.await
.unwrap();
HttpResponse::Ok().json(json!(paginated_users))
}
let params = QueryParamsBuilder::<User>::new()
.with_pagination(1, 10)
.with_sort("created_at", QuerySortDirection::Descending)
.with_search("john", vec!["name", "email"])
.build();
paginated_query_as!(UserExample, "SELECT * FROM users")
.with_params(initial_params)
.with_query_builder(|params| {
// Can override the default query builder (build_query_with_safe_defaults) with a complete custom one:
QueryBuilder::<UserExample, Postgres>::new()
.with_search(params) // Add or remove search feature from the query;
.with_filters(params) // Add or remove custom filters from the query;
.with_date_range(params) // Add or remove data range;
.with_raw_condition("") // Add raw condition, no checks.
.disable_protection() // This removes all column safety checks.
.with_combined_conditions(|builder| {
// ...
.build()
})
.disable_totals_count() // Disables the calculation of total record count
.fetch_paginated(&pool)
.await
.unwrap()
Primary users
Use cases
Add to Cargo.toml
:
[dependencies]
sqlx_paginated = { version = "0.1.0", features = ["postgres"] }
#[derive(sqlx::FromRow, serde::Serialize)]
struct User {
id: i64,
first_name: String,
last_name: String,
email: String,
confirmed: bool,
created_at: Option<DateTime<Utc>>,
}
/// Macro usage example
async fn get_users(pool: &PgPool) -> Result<PaginatedResponse<User>, sqlx::Error> {
let paginated_response = paginated_query_as!(User, "SELECT * FROM users")
// Alternative function call example (if macros cannot be applied to your use case):
// paginated_query_as::<User>("SELECT * FROM users")
.with_params(params)
.fetch_paginated(&pool)
.await
.unwrap();
paginated_response
}
{
"records": [
{
"id": "409e3900-c190-4dad-882d-ec2d40245329",
"first_name": "John",
"last_name": "Smith",
"email": "john@example.com",
"confirmed": true,
"created_at": "2024-01-01T00:00:00Z"
}
],
"total": 1,
"page": 1,
"page_size": 10,
"total_pages": 1
}
Parameter | Type | Default | Min | Max | Description |
---|---|---|---|---|---|
page | integer | 1 | 1 | n/a | Current page number |
page_size | integer | 10 | 10 | 50 | Number of records per page |
Example:
GET /v1/internal/users?page=2&page_size=20
Parameter | Type | Default | Allowed Values | Description |
---|---|---|---|---|
sort_column | string | created_at | Any valid table column | Column name to sort by |
sort_direction | string | descending | ascending, descending | Sort direction |
Example:
GET /v1/internal/users?sort_column=last_name&sort_direction=ascending
Parameter | Type | Default | Max Length | Description |
---|---|---|---|---|
search | string | null | 100 | Search term to filter results |
search_columns | string | name,description | n/a | Comma-separated list of columns |
Example:
GET /v1/internal/users?search=john&search_columns=first_name,last_name,email
Parameter | Type | Default | Format | Description |
---|---|---|---|---|
date_column | string | created_at | Column name | Column to filter on |
date_after | datetime | null | ISO 8601 | Start of date range |
date_before | datetime | null | ISO 8601 | End of date range |
Example:
GET /v1/internal/users?date_column=created_at&date_after=2024-01-01T00:00:00Z
Parameter | Type | Default | Max Length | Description |
---|---|---|---|---|
* | string,boolean,datetime | null | 100 | Any valid table column for given struct |
Example:
GET /v1/internal/users?confirmed=true
struct
, we can then perform search and filtering
against its own fields.#[derive(Serialize, Deserialize, FromRow, Default)]
pub struct User {
pub id: Option<Uuid>,
pub first_name: String,
pub last_name: String,
pub confirmed: Option<bool>,
pub created_at: Option<DateTime<Utc>>,
pub updated_at: Option<DateTime<Utc>>,
}
confirmed=true
filter.Request:
GET /v1/internal/users
?search=john
&search_columns=first_name,last_name,email
&sort_column=created_at
&sort_direction=descending
&date_before=2024-11-03T12:30:12.081598Z
&date_after=2024-11-02T12:30:12.081598Z
&page=1
&page_size=20
&confirmed=true
Response:
{
"page": 1,
"page_size": 20,
"total": 2,
"total_pages": 1,
"records": [
{
"id": "409e3900-c190-4dad-882d-ec2d40245329",
"first_name": "John",
"last_name": "Smith",
"email": "john.smith@example.com",
"confirmed": true,
"created_at": "2024-11-03T12:30:12.081598Z",
"updated_at": "2024-11-03T12:30:12.081598Z"
},
{
"id": "9167d825-8944-4428-bf91-3c5531728b5e",
"first_name": "Johnny",
"last_name": "Doe",
"email": "johnny.doe@example.com",
"confirmed": true,
"created_at": "2024-10-28T19:14:49.064626Z",
"updated_at": "2024-10-28T19:14:49.064626Z"
}
]
}
confirmed=true
and first_name=Alex
filters.first_name
filter the value will be an exact match (case-sensitive).Request:
GET /v1/internal/users
?date_before=2024-11-03T12:30:12.081598Z
&date_after=2024-11-02T12:30:12.081598Z
&confirmed=true
&first_name=Alex
Response:
{
"page": 1,
"page_size": 20,
"total": 1,
"total_pages": 1,
"records": [
{
"id": "509e3900-c190-4dad-882d-ec2d40245329",
"first_name": "Alex",
"last_name": "Johnson",
"email": "alex.johnson@example.com",
"confirmed": true,
"created_at": "2024-11-02T12:30:12.081598Z"
}
]
}
Query Pattern | Impact | Recommendation |
---|---|---|
SELECT * | ❌ High Impact | Specify needed columns |
Large Text Columns | ❌ High Impact | Use separate detail endpoint |
Computed Columns | ⚠️ Medium Impact | Cache if possible |
JSON Aggregation | ⚠️ Medium Impact | Limit array size |
-- Text search
CREATE INDEX idx_users_name_gin ON users USING gin(to_tsvector('english', name));
-- Composite indexes for common queries
CREATE INDEX idx_users_confirmed_created ON users(confirmed, created_at);
-- JSON indexes
CREATE INDEX idx_users_metadata ON users USING gin(metadata);
Page Size | Records | Performance Impact |
---|---|---|
1-10 | Optimal | ✅ Best |
11-50 | Good | ✅ Good |
51-100 | Caution | ⚠️ Monitor |
100+ | Poor | ❌ Not Recommended |
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.