Sorting & Aggregations
Relevant source files
The following files were used as context for generating this wiki page:
Sorting and Aggregations provide mechanisms to organize and summarize data retrieved through GraphQL queries. Sorting allows you to order results based on specific fields and directions, while Aggregations allow you to perform calculations like counts, sums, averages, and grouping across datasets.
Sources: README.md:14-16, examples/auth/e2e/todo-item.resolver.spec.ts:153-200, examples/sequelize/e2e/todo-item.resolver.spec.ts:400-450
Sorting
Sorting organizes query results by one or more fields. You specify a field name and a direction (ASC or DESC). The system supports sorting on primary entity fields and related entity fields.
Sources: packages/core/tests/helpers.spec.ts:1085-1150, packages/query-sequelize/tests/query/filter-query.builder.spec.ts:114-150
Sort Parameters
A sort configuration consists of the following components:
| Parameter | Type | Description |
|---|---|---|
field | string | The name of the field to sort by. |
direction | SortDirection | Direction of the sort: ASC or DESC. |
nulls | SortNulls | (Optional) Specifies where null values should appear: NULLS_FIRST or NULLS_LAST. |
Sources: packages/core/tests/helpers.spec.ts:1100-1120, packages/query-sequelize/tests/query/filter-query.builder.spec.ts:117-125
Sorting Logic Flow
The system transforms DTO-based sort definitions into persistence-specific order clauses.
Aggregations
Aggregations summarize multiple rows of data. The nestjs-query library provides aggregate responses for entity fields, supporting common mathematical operations and grouping.
Sources: examples/auth/e2e/todo-item.resolver.spec.ts:384-440, examples/sequelize/e2e/todo-item.resolver.spec.ts:400-430
Supported Aggregate Functions
| Function | Description |
|---|---|
count | Returns the number of non-null values for a field. |
sum | Calculates the total sum of numeric values. |
avg | Calculates the average value of a numeric field. |
min | Identifies the minimum value in a dataset. |
max | Identifies the maximum value in a dataset. |
Sources: packages/core/tests/helpers.spec.ts:316-335, examples/auth/e2e/todo-item.resolver.spec.ts:168-180
Grouping and Filtering
Aggregations can be combined with groupBy to segment results and filter to restrict the dataset before calculation.
- Grouping: Uses the
groupByfield to split aggregates by specific values (e.g., grouping tasks by completion status). - Temporal Grouping: Allows grouping date fields by intervals such as
DAY,WEEK, orMONTH.
Sources: examples/sequelize/e2e/todo-item.resolver.spec.ts:431-460, examples/typeorm/e2e/tag.resolver.spec.ts:420-470
Aggregate Data Flow
Implementation Helpers
The core package provides helpers to programmatically apply sorting and aggregation logic to in-memory arrays, which is used for testing and internal data transformation.
applySort(data, sortFields): Orders an array of objects based on sort field definitions.transformAggregateResponse(response, fieldMap): Translates persistence-layer field names in an aggregate result back to DTO field names.transformSort(sorting, fieldMap): Translates DTO sort fields to entity-level sort fields based on a provided mapping.
Sources: packages/core/tests/helpers.spec.ts:50-60, packages/core/tests/helpers.spec.ts:354-365, packages/core/tests/helpers.spec.ts:1085-1100
Summary
Sorting and Aggregations are core components of the nestjs-query data retrieval system. Sorting ensures results are presented in a predictable order, while Aggregations provide powerful analytical capabilities directly through the GraphQL API, supporting complex data summarization and grouping across different persistence layers.