sqlx-paginated 0.2.2

A flexible, type-safe SQLx query builder for dynamic web APIs, offering seamless pagination, searching, filtering, and sorting.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# Paginated queries for SQLx

[![Rust](https://github.com/alexandrughinea/sqlx-paginated/actions/workflows/rust.yml/badge.svg?branch=main)](https://github.com/alexandrughinea/sqlx-paginated/actions/workflows/rust.yml)
[![Crates.io](https://img.shields.io/crates/v/sqlx-paginate.svg)](https://crates.io/crates/sqlx-paginate)
[![Documentation](https://docs.rs/sqlx-paginate/badge.svg)](https://docs.rs/sqlx-paginate)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

A flexible, type-safe SQLx query builder for dynamic web APIs, offering seamless pagination, searching, filtering, and sorting.

## Table of Contents
- [Paginated queries for SQLx]#paginated-queries-for-sqlx
    - [Features]#features
    - [Core Capabilities]#core-capabilities
    - [Technical Features]#technical-features
    - [Query Features]#query-features
  - [Database Support]#database-support
    - [Current vs Planned Support]#current-vs-planned-support
  - [Market Analysis]#market-analysis
    - [Ecosystem Gaps]#ecosystem-gaps
    - [Unique Selling Points]#unique-selling-points
    - [Target Audience]#target-audience
  - [Installation]#installation
  - [Quick Start]#quick-start
    - [Basic Usage]#basic-usage
    - [Response Example]#response-example
  - [API Reference]#api-reference
    - [Pagination Parameters]#pagination-parameters
    - [Sort Parameters]#sort-parameters
    - [Search Parameters]#search-parameters
    - [Date Range Parameters]#date-range-parameters
    - [Filtering Parameters]#filtering-parameters
  - [Query Examples]#query-examples
    - [Combined search, sort, date range, pagination and filter]#combined-search-sort-date-range-pagination-and-custom-filter
    - [Date Range combined with two other filters]#date-range-filter-combined-with-two-other-custom-filters
  - [Performance Considerations]#performance-considerations
    - [Query Pattern Optimization]#query-pattern-optimization
    - [Recommended Indexes]#recommended-indexes
    - [Pagination Performance]#pagination-performance
  - [Security Features]#security-features
    - [Input Sanitization]#input-sanitization
    - [Protected Patterns]#protected-patterns
  - [Contributing]#contributing
  - [License]#license

## Features

### Core Capabilities
- 🔍 Full-text search with column specification
- 📑 Smart pagination with customizable page size
- 🔄 Dynamic sorting on any column
- 🎯 Flexible filtering system
- 📅 Date range filtering
- 🔒 Type-safe operations
- ⚡ High performance
- 🛡️ SQL injection protection

### Technical Features
- Builder patterns for query parameters and query construction
- Graceful error handling
- Logging with tracing (if enabled)
- Macro and function support

### Query Features
- Case-insensitive search
- Multiple column search
- Complex filtering conditions
- Date-based filtering
- Dynamic sort direction
- Customizable page size
- Result count optimization

## Database Support

### Current vs Planned Support
| 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.`

## Market Analysis

### Ecosystem Gaps
1. **Query builders**
   - Diesel: Full ORM, can be heavyweight
   - SeaQuery: Generic and can be verbose
   - sqlbuilder: Basic SQL building without pagination or security

2. **Missing features in existing solutions**
   - Easy integration with web frameworks
   - Automatic type casting
   - Typesafe search/filter/sort/pagination capabilities

### Unique Selling Points

1. **Quick Web Framework Integration with minimal footprint**

[Actix Web](https://actix.rs/) handler example
```rust
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))
}
```

2. **Type Safety & Ergonomics for parameter configuration**
```rust
let params = QueryParamsBuilder::<User>::new()
    .with_pagination(1, 10)
    .with_sort("created_at", QuerySortDirection::Descending)
    .with_search("john", vec!["name", "email"])
    .build();
```

3. **Advanced Builder Patterns**
- Optional fluent API for query parameters (QueryParams) which allow defining search, search location, date filtering, ordering, and custom filtering.
- Fluent API for the entire supported feature set, more here: [advanced example]src/paginated_query_as/examples/paginated_query_builder_advanced_examples.rs

```rust
    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()
```


### Target Audience
1. **Primary users**
   - Rust web developers
   - Teams needing secure query building
   - Projects requiring pagination APIs
   - SQLx users wanting higher-level abstractions

2. **Use cases**
   - REST APIs with pagination
   - Admin panels
   - Data exploration interfaces

## Installation

Add to `Cargo.toml`:
```toml
[dependencies]
sqlx_paginated = { version = "0.1.0", features = ["postgres"] }
```

## Quick Start

### Basic Usage
```rust
#[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
}
```

### Response Example
```json
{
  "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
}
```

## API Reference

### Pagination Parameters
| 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
```

### Sort Parameters
| 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
```

### Search Parameters
| 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
```

### Date Range Parameters
| 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
```

### Filtering Parameters
| 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
```

## Query Examples

- Given the following `struct`, we can then perform search and filtering
against its own fields. 
- We should also receive a paginated response back with the matching records.

```rust
#[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>>,
}
```

1. ### Combined search, sort, date range, pagination and custom filter

- Notice the `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:
```json
{
  "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"
    }
  ]
}
```

2. ### Date range filter combined with two other custom filters

- Notice the `confirmed=true` and `first_name=Alex` filters.
- For the `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:
```json
{
  "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"
    }
  ]
}
```

## Performance Considerations

### Query Pattern Optimization
| 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 |

### Recommended Indexes
```sql
-- 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);
```

### Pagination Performance
| Page Size | Records | Performance Impact |
|-----------|---------|-------------------|
| 1-10      | Optimal | ✅ Best           |
| 11-50     | Good    | ✅ Good           |
| 51-100    | Caution | ⚠️ Monitor        |
| 100+      | Poor    | ❌ Not Recommended |


## Security Features

### Input Sanitization
- Search terms are cleaned and normalized
- Parameter input values are trimmed and/or clamped against their defaults
- Column names are validated against an allowlist:
  - The struct itself first;
  - Database specific table names second;
- SQL injection patterns are blocked
- System table access is prevented

### Protected Patterns
- System schemas (pg_, information_schema)
- System columns (oid, xmin, etc.)
- SQL injection attempts
- Invalid characters in identifiers

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## License

This project is licensed under the MIT License - see the LICENSE file for details.