Struct QueryParamsBuilder

Source
pub struct QueryParamsBuilder<'q, T> { /* private fields */ }

Implementations§

Source§

impl<'q, T: Default + Serialize> QueryParamsBuilder<'q, T>

Source

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();
Source

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();
Source

pub fn with_sort( self, sort_column: impl Into<String>, sort_direction: QuerySortDirection, ) -> Self

Sets sorting parameters.

§Arguments
  • sort_column - Column name to sort by
  • sort_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();

Sets search parameters with multiple columns support.

§Arguments
  • search - Search term to look for
  • search_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();
Source

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();
Source

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 on
  • value - 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();
Source

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();
Source

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§

Source§

impl<T: Default + Serialize> Default for QueryParamsBuilder<'_, T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,