Skip to content

Filtering and Pagination

List endpoints in APIs routinely combine pagination, search, date-range bounds, collection membership, and multi-column sorting. SQLSpec provides composable StatementFilter value objects (FilterTypes) in sqlspec.core along with offset (OffsetPagination[T]) and keyset cursor (CursorPagination[T]) pagination helpers.

Built-In Statement Filters

Pass any combination of StatementFilter instances as positional arguments to session.select(...), session.select_with_total(...), or service.paginate(...):

Filter Class Constructor Signature SQL Clause Generated Description
BeforeAfterFilter BeforeAfterFilter(field_name, before=None, after=None) WHERE col < :col_before AND col > :col_after Filter for datetime range queries.
OnBeforeAfterFilter OnBeforeAfterFilter(field_name, on_or_before=None, on_or_after=None) WHERE col <= :col_on_or_before AND col >= :col_on_or_after Filter for inclusive datetime range queries.
InCollectionFilter InCollectionFilter(field_name, values=None) WHERE col IN (:col_in_0, :col_in_1, ...) Filter for IN clause queries.
NotInCollectionFilter NotInCollectionFilter(field_name, values=None) WHERE col NOT IN (:col_notin_0, :col_notin_1, ...) Filter for NOT IN clause queries.
AnyCollectionFilter AnyCollectionFilter(field_name, values=None) WHERE col = ANY(ARRAY[:col_any_0, :col_any_1, ...]) Filter for PostgreSQL-style ANY clause queries.
NotAnyCollectionFilter NotAnyCollectionFilter(field_name, values=None) WHERE NOT col = ANY(ARRAY[:col_not_any_0, ...]) Filter for PostgreSQL-style NOT ANY clause queries.
ChoicesFilter ChoicesFilter(field_name, values=None) WHERE col IN (:col_choices_0, :col_choices_1, ...) Filter for field matching a defined set of choice values.
LimitOffsetFilter LimitOffsetFilter(limit, offset) LIMIT :limit OFFSET :offset Filter for LIMIT and OFFSET clauses.
CursorFilter CursorFilter(keys, limit, cursor=None, secret=None, ttl=None, encrypted=False) WHERE (col, id) > (:cursor_k0, :cursor_k1) ORDER BY col ASC, id ASC LIMIT :cursor_limit Paginate by ordered keys whose last key uniquely identifies each row.
OrderByFilter OrderByFilter(field_name, sort_order='asc', nulls=None) ORDER BY col ASC / DESC [NULLS FIRST / LAST] Filter for ORDER BY clauses.
SearchFilter SearchFilter(field_name, value, ignore_case=False, escape_wildcards=False) WHERE col LIKE :col_search [ESCAPE '/'] (or ILIKE when ignore_case=True) Filter for text search queries.
NotInSearchFilter NotInSearchFilter(field_name, value, ignore_case=False, escape_wildcards=False) WHERE col NOT LIKE :col_not_search [ESCAPE '/'] (or NOT ILIKE when ignore_case=True) Filter for negated text search queries.
NullFilter NullFilter(field_name) WHERE col IS NULL Filter for IS NULL queries.
NotNullFilter NotNullFilter(field_name) WHERE col IS NOT NULL Filter for IS NOT NULL queries.
BooleanFilter BooleanFilter(field_name, value) WHERE col = :col_boolean Filter for boolean comparison queries.
  • Multi-column and escaped LIKE search: SearchFilter and NotInSearchFilter accept set[str] to filter across multiple columns (OR for SearchFilter, AND for NotInSearchFilter). Pass escape_wildcards=True so %, _, and / in user input are automatically escaped with LIKE_ESCAPE_CHAR ("/") and rendered with an ESCAPE '/' clause, or call SearchFilter.escape_like_value(raw) manually.
  • Named parameter binding: Because StatementFilter objects bind named parameters (:limit, :offset, etc.), always bind any additional base-query parameters as named keyword arguments (status="active"), never positional tuples.

Combining Multiple Filters on One Query

Filters compose in any order without mutating the underlying SQL statement:

import datetime as dt

from sqlspec import SQL, SQLSpec
from sqlspec.adapters.sqlite import SqliteConfig
from sqlspec.core import (
    BeforeAfterFilter,
    InCollectionFilter,
    LimitOffsetFilter,
    OrderByFilter,
    SearchFilter,
)

spec = SQLSpec()
config = spec.add_config(
    SqliteConfig(connection_config={"database": ":memory:"})
)

with spec.provide_session(config) as session:
    session.execute(
        "CREATE TABLE articles ("
        "id INTEGER PRIMARY KEY, title TEXT, status TEXT, published_at TIMESTAMP)"
    )
    rows = session.select(
        SQL("SELECT id, title, status FROM articles"),
        InCollectionFilter("status", ["published"]),
        SearchFilter(
            {"title", "status"},
            SearchFilter.escape_like_value("Pool"),
            ignore_case=True,
        ),
        BeforeAfterFilter(
            "published_at",
            after=dt.datetime(2026, 1, 1, tzinfo=dt.timezone.utc),
        ),
        OrderByFilter("published_at", sort_order="desc"),
        LimitOffsetFilter(limit=10, offset=0),
    )

Offset Pagination and Total Counts

Chain .paginate(page=..., page_size=...) on a SQL statement and call session.select_with_total(..., count_with_window=True) to compute COUNT(*) OVER() in a single database round-trip (or omit count_with_window to run a separate scalar COUNT(*) query). Service layers expose SQLSpecSyncService.paginate_limit_offset and SQLSpecAsyncService.paginate_limit_offset to return an OffsetPagination[T] envelope directly:

spec = SQLSpec()
config = spec.add_config(SqliteConfig(connection_config={"database": ":memory:"}))

with spec.provide_session(config) as session:
    session.execute("CREATE TABLE items (id INTEGER PRIMARY KEY, name TEXT)")
    session.execute_many("INSERT INTO items (name) VALUES (?)", [(f"Item {i}",) for i in range(1, 51)])

    page_two = session.select(SQL("SELECT id, name FROM items ORDER BY id").paginate(page=2, page_size=10))
    page_one, total = session.select_with_total(
        SQL("SELECT id, name FROM items ORDER BY id").paginate(page=1, page_size=10), count_with_window=True
    )
    builder_rows = session.execute(
        sql.select("id", "name").from_("items").where_like("name", "Item%").order_by("id").limit(10).offset(0)
    ).all()

Keyset Cursor Pagination

Large tables degrade under deep OFFSET 100000 scans because the engine must read and discard skipped rows. CursorFilter and CursorKey(field_name, sort_order="asc", nulls=None, result_name=None) implement indexed keyset seek pagination with opaque forward (next_cursor) and backward (previous_cursor) tokens:

spec = SQLSpec()
config = spec.add_config(SqliteConfig(connection_config={"database": ":memory:"}))

try:
    with spec.provide_session(config) as session:
        session.execute("CREATE TABLE items (id INTEGER PRIMARY KEY, name TEXT, created_at TEXT)")
        session.execute_many(
            "INSERT INTO items (id, name, created_at) VALUES (?, ?, ?)",
            [(i, f"Item {i}", f"2026-01-{1 + i // 5:02d}") for i in range(1, 26)],
        )
        keys: CursorKeys = [CursorKey("created_at", "desc"), CursorKey("id", "desc")]
        query = "SELECT id, name, created_at FROM items"
        secret = "cursor-signing-key"

        cursor_filter = CursorFilter(keys, limit=10, secret=secret)
        direct_page = cursor_filter.build_page(session.select(query, cursor_filter))

        service = SQLSpecSyncService(session)
        offset_page = service.paginate_limit_offset(query, LimitOffsetFilter(limit=10, offset=0))
        first_page = service.paginate_cursor(query, CursorFilter(keys, limit=10, secret=secret))
        second_page = service.paginate_cursor(
            query, CursorFilter(keys, limit=10, cursor=first_page.next_cursor, secret=secret)
        )
        previous_page = service.paginate(
            query, CursorFilter(keys, limit=10, cursor=second_page.previous_cursor, secret=secret)
        )
finally:
    config.close_pool()
  • Unique tie-breaker key: Always include a unique column (such as CursorKey("id", "desc")) as the final key in CursorKeys so rows with identical timestamps paginate deterministically.
  • Unsigned default & cursor readability: Used directly, without a secret, CursorFilter emits unsigned v1.<payload> base64url JSON tokens that contain the cursor sort values in readable form (plus issued/expiry timestamps when ttl is set). Add a secret for tamper detection (v1.<payload>.<mac>) or set encrypted=True (e1.<nonce+ciphertext>.<mac>) whenever sort-key values should remain opaque to clients.
  • HMAC-SHA256 signing, TTL, and encryption on CursorFilter: Pass secret="your-signing-key" for HMAC-SHA256 signing (v1.), ttl=timedelta(minutes=15) (or positive seconds as int | float) to enforce token expiration (InvalidCursorError on expired tokens or non-expiring tokens checked against a required ttl), and encrypted=True with a non-empty secret to encrypted tokens with stdlib HMAC-SHA256 counter-mode encryption and authenticate them before decryption.
  • Framework plugin & FilterConfig options: On SQLSpecPlugin (Litestar, Starlette, FastAPI) and extension_config (LitestarConfig, StarletteConfig, FastAPIConfig), or per-route FilterConfig, configure pagination_cursor_secret, pagination_cursor_ttl, and pagination_cursor_encrypted. Framework providers resolve configuration in order: FilterConfig override -> SQLSpecPlugin / extension_config -> SQLSPEC_PAGINATION_CURSOR_SECRET -> SECRET_KEY, deriving endpoint-scoped subkeys automatically. Framework providers require a secret: a cursor request without one raises ImproperConfigurationError, and there is no unsigned mode. Tokens are also bound to the request tenant of every tenant-enabled database configuration, so a token issued for one tenant fails verification for another and clients restart from the first page after tenant binding is enabled. Search providers escape %, _, and / in request input by default; set search_escape_wildcards=False on FilterConfig to let them act as wildcards.
  • Sharing secrets across worker processes: Configure an explicit shared secret / pagination_cursor_secret (or SQLSPEC_PAGINATION_CURSOR_SECRET / SECRET_KEY environment variable) across all worker processes and instances so cursors issued by one worker validate and decrypt on another.
  • Direct driver vs service assembly: Call cursor_filter.build_page(rows) when executing via a raw driver session (session.select(query, cursor_filter)), or call service.paginate_cursor(query, cursor_filter) on SQLSpecSyncService / SQLSpecAsyncService.

Declarative HTTP Filter Providers (Litestar)

In Litestar services, create_filter_dependencies generates typed query-parameter dependency providers (id_filter, created_filter, updated_filter, search_filter, order_by_filter, limit_offset_filter) plus an aggregate filters dependency from a single FilterConfig dictionary:

deps = create_filter_dependencies({
    "id_filter": str,
    "id_field": "id",
    "sort_field": ["created_at", "uploaded_collections", "name"],
    "sort_field_aliases": {"lastUpload": "uploaded_collections"},
    "sort_order": "desc",
    "pagination_type": "limit_offset",
    "pagination_size": 20,
    "search": "name,email",
    "search_ignore_case": True,
    "created_at": True,
    "updated_at": True,
})

Wiring Filters in a Litestar Controller

Mount create_filter_dependencies({...}) on Controller.dependencies and pass *filters into await db_session.select_with_total(..., count_with_window=True) (or await service.paginate(statement, *filters, schema_type=Product) when using a SQLSpecAsyncService subclass):

from typing import Annotated

import msgspec
from litestar import Controller, get
from litestar.params import Dependency

from sqlspec import SQL
from sqlspec.adapters.asyncpg import AsyncpgDriver
from sqlspec.core import FilterTypes, LimitOffsetFilter, OffsetPagination
from sqlspec.extensions.litestar.providers import create_filter_dependencies


class Product(msgspec.Struct):
    id: int
    name: str
    price_cents: int


class ProductController(Controller):
    path = "/products"
    dependencies = create_filter_dependencies({
        "sort_field": ["name", "price_cents", "created_at"],
        "sort_order": "asc",
        "pagination_type": "limit_offset",
        "pagination_size": 25,
        "search": "name",
    })

    @get()
    async def list_products(
        self,
        db_session: AsyncpgDriver,
        filters: Annotated[list[FilterTypes], Dependency(skip_validation=True)],
    ) -> OffsetPagination[Product]:
        """Filter, sort, and paginate products."""
        items, total = await db_session.select_with_total(
            SQL("SELECT id, name, price_cents FROM products"),
            *filters,
            schema_type=Product,
            count_with_window=True,
        )
        page = next(f for f in filters if isinstance(f, LimitOffsetFilter))
        return OffsetPagination(
            items=items, limit=page.limit, offset=page.offset, total=total
        )