pub struct QueryParamsBuilder<'q, T> { /* private fields */ }
Implementations§
Source§impl<'q, T: Default + Serialize> QueryParamsBuilder<'q, T>
impl<'q, T: Default + Serialize> QueryParamsBuilder<'q, T>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new QueryParamsBuilder
with default values.
Default values include:
- Page: 1
- Page size: 10
- Sort column: “created_at”
- Sort direction: Descending
§Examples
use serde::{Serialize};
use sqlx_paginated::{QueryParamsBuilder};
#[derive(Serialize, Default)]
struct UserExample {
name: String
}
let builder = QueryParamsBuilder::<UserExample>::new();
Sourcepub fn with_pagination(self, page: i64, page_size: i64) -> Self
pub fn with_pagination(self, page: i64, page_size: i64) -> Self
Creates a new QueryParamsBuilder
with default values.
Default values include:
- Page: 1
- Page size: 10
- Sort column: “created_at”
- Sort direction: Descending
§Examples
use serde::{Serialize};
use sqlx_paginated::{QueryParamsBuilder};
#[derive(Serialize, Default)]
struct UserExample {
name: String
}
let builder = QueryParamsBuilder::<UserExample>::new();
Sourcepub fn with_sort(
self,
sort_column: impl Into<String>,
sort_direction: QuerySortDirection,
) -> Self
pub fn with_sort( self, sort_column: impl Into<String>, sort_direction: QuerySortDirection, ) -> Self
Sets sorting parameters.
§Arguments
sort_column
- Column name to sort bysort_direction
- Direction of sort (Ascending or Descending)
§Examples
use serde::{Serialize};
use sqlx_paginated::{QueryParamsBuilder, QuerySortDirection};
#[derive(Serialize, Default)]
struct UserExample {
name: String
}
let params = QueryParamsBuilder::<UserExample>::new()
.with_sort("updated_at", QuerySortDirection::Ascending)
.build();
Sourcepub fn with_search(
self,
search: impl Into<String>,
search_columns: Vec<impl Into<String>>,
) -> Self
pub fn with_search( self, search: impl Into<String>, search_columns: Vec<impl Into<String>>, ) -> Self
Sets search parameters with multiple columns support.
§Arguments
search
- Search term to look forsearch_columns
- Vector of column names to search in
§Examples
use serde::{Serialize};
use sqlx_paginated::{QueryParamsBuilder, QuerySortDirection};
#[derive(Serialize, Default)]
struct UserExample {
name: String
}
let params = QueryParamsBuilder::<UserExample>::new()
.with_search("john", vec!["name", "email", "username"])
.build();
Sourcepub fn with_date_range(
self,
date_after: Option<DateTime<Utc>>,
date_before: Option<DateTime<Utc>>,
column_name: Option<impl Into<String>>,
) -> Self
pub fn with_date_range( self, date_after: Option<DateTime<Utc>>, date_before: Option<DateTime<Utc>>, column_name: Option<impl Into<String>>, ) -> Self
Sets date range parameters for filtering by date.
§Arguments
date_after
- Optional start date (inclusive)date_before
- Optional end date (inclusive)column_name
- Optional column name to apply date range filter (defaults to created_at)
§Examples
use chrono::{DateTime, Utc};
use serde::{Serialize};
use sqlx_paginated::{QueryParamsBuilder, QuerySortDirection};
#[derive(Serialize, Default)]
struct UserExample {
name: String,
updated_at: DateTime<Utc>
}
let start = DateTime::parse_from_rfc3339("2024-01-01T00:00:00Z").unwrap().into();
let end = DateTime::parse_from_rfc3339("2024-12-31T23:59:59Z").unwrap().into();
let params = QueryParamsBuilder::<UserExample>::new()
.with_date_range(Some(start), Some(end), Some("updated_at"))
.build();
Sourcepub fn with_filter(
self,
key: impl Into<String>,
value: Option<impl Into<String>>,
) -> Self
pub fn with_filter( self, key: impl Into<String>, value: Option<impl Into<String>>, ) -> Self
Adds a single filter condition.
§Arguments
key
- Column name to filter onvalue
- Optional value to filter by
§Details
Only adds the filter if the column exists in the model struct. Logs a warning if tracing is enabled and the column is invalid.
§Examples
use std::any::Any;
use serde::{Serialize};
use sqlx_paginated::{QueryParamsBuilder};
#[derive(Serialize, Default)]
struct UserExample {
name: String,
status: String,
role: String
}
let params = QueryParamsBuilder::<UserExample>::new()
.with_filter("status", Some("active"))
.with_filter("role", Some("admin"))
.build();
Sourcepub fn with_filters(
self,
filters: HashMap<impl Into<String>, Option<impl Into<String>>>,
) -> Self
pub fn with_filters( self, filters: HashMap<impl Into<String>, Option<impl Into<String>>>, ) -> Self
Adds multiple filter conditions from a HashMap.
§Arguments
filters
- HashMap of column names and their filter values
§Details
Only adds filters for columns that exist in the model struct. Logs a warning if tracing is enabled and a column is invalid.
§Examples
use std::collections::HashMap;
use serde::{Serialize};
use sqlx_paginated::{QueryParamsBuilder};
#[derive(Serialize, Default)]
struct UserExample {
name: String,
status: String,
role: String
}
let mut filters = HashMap::new();
filters.insert("status", Some("active"));
filters.insert("role", Some("admin"));
let params = QueryParamsBuilder::<UserExample>::new()
.with_filters(filters)
.build();
Sourcepub fn build(self) -> QueryParams<'q, T>
pub fn build(self) -> QueryParams<'q, T>
Builds and returns the final QueryParams.
§Returns
Returns the constructed QueryParams<T>
with all the configured parameters.
§Examples
use chrono::{DateTime, Utc};
use sqlx_paginated::{QueryParamsBuilder, QuerySortDirection};
use serde::{Serialize};
#[derive(Serialize, Default)]
struct UserExample {
name: String,
status: String,
email: String,
created_at: DateTime<Utc>
}
let params = QueryParamsBuilder::<UserExample>::new()
.with_pagination(1, 20)
.with_sort("created_at", QuerySortDirection::Descending)
.with_search("john", vec!["name", "email"])
.with_filter("status", Some("active"))
.build();
Trait Implementations§
Auto Trait Implementations§
impl<'q, T> Freeze for QueryParamsBuilder<'q, T>
impl<'q, T> RefUnwindSafe for QueryParamsBuilder<'q, T>where
T: RefUnwindSafe,
impl<'q, T> Send for QueryParamsBuilder<'q, T>where
T: Sync,
impl<'q, T> Sync for QueryParamsBuilder<'q, T>where
T: Sync,
impl<'q, T> Unpin for QueryParamsBuilder<'q, T>
impl<'q, T> UnwindSafe for QueryParamsBuilder<'q, T>where
T: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more