createTPaginatedQuery function creates a sub-router with three separate procedures for paginated tables: getItems, getCount, and getDescription. Each procedure has its own typed input and output.
Basic Usage
Pagination Parameters
ThegetItems function receives pagination parameters as its first argument, ready to use with MongoDB:
Procedures
EachcreateTPaginatedQuery returns three separate tRPC procedures, accessible as nested routes:
getItems
Returns the paginated items. Input includes pagination fields and custom params.getCount
Returns the total count (for calculating total pages on the client). Input includes only custom params.getDescription
Returns the sorting configuration. Takes no input.Options
| Option | Type | Description |
|---|---|---|
params | Schema | Custom input params schema (optional) |
returns | Schema | Output item schema for cleaning (optional) |
getItems | Function | (paginationParams, params, viewer) => items[] - Returns the paginated items |
getCount | Function | (params, viewer) => number - Returns the total count for the query |
allowedSorts | string[] | List of allowed sort fields (optional) |
defaultSortBy | string | Default sort field (optional) |
defaultSortType | ’asc’ | ‘desc’ | Default sort direction (optional) |
defaultLimit | number | Default page size (default: 20) |
maxLimit | number | Maximum allowed page size (default: 200) |
Pagination Fields
The following fields are available in thegetItems input:
| Field | Type | Default | Description |
|---|---|---|---|
page | integer | 1 | Page number (min: 1) |
limit | integer | 20 | Items per page (min: 0, max: 200) |
sortBy | string | - | Sort field (only if allowedSorts is configured) |
sortType | ’asc’ | ‘desc’ | - | Sort direction |
Type Inference
Item types are automatically inferred from thegetItems return type - no manual type annotations needed:
InferRouterOutputs, use the custom InferRouterOutputs type from @orion-js/trpc:
Schema Cleaning
If you provide areturns schema, items will be cleaned/filtered according to that schema: