Skip to main content

sqlx_paginated/paginated_query_as/models/
query_response.rs

1use crate::paginated_query_as::internal::QueryPaginationParams;
2use serde::{Deserialize, Serialize};
3
4/// Represents a paginated response with records and metadata.
5///
6/// This is the standard response structure returned by all paginated queries.
7/// It includes the actual records, pagination information, and optionally
8/// the total count and total pages.
9///
10/// # Type Parameters
11///
12/// * `T` - The type of records being paginated
13///
14/// # Examples
15///
16/// ```rust
17/// use sqlx_paginated::PaginatedResponse;
18/// use serde::Serialize;
19///
20/// #[derive(Serialize)]
21/// struct User {
22///     id: i64,
23///     name: String,
24/// }
25///
26/// // Response will be serialized as:
27/// // {
28/// //   "records": [...],
29/// //   "page": 1,
30/// //   "page_size": 10,
31/// //   "total": 100,
32/// //   "total_pages": 10
33/// // }
34/// ```
35#[derive(Serialize, Deserialize, Clone, Debug)]
36pub struct PaginatedResponse<T> {
37    /// The records for the current page
38    pub records: Vec<T>,
39
40    /// Pagination metadata (page and page_size)
41    #[serde(flatten, skip_serializing_if = "Option::is_none")]
42    pub pagination: Option<QueryPaginationParams>,
43
44    /// Total number of records across all pages
45    #[serde(skip_serializing_if = "Option::is_none")]
46    pub total: Option<i64>,
47
48    /// Total number of pages
49    #[serde(skip_serializing_if = "Option::is_none")]
50    pub total_pages: Option<i64>,
51}