Skip to main content
Only fields decorated with @FilterableField will be exposed in aggregate queries.
All examples will be based on the following TodoItem DTO. All fields except age, because it is not decorated with @FilterableField or @IDField, will be exposed in aggregate queries. todo-item/dto/todo-item.dto.ts

Enabling Aggregate Queries

To enable aggregate queries you can set the enableAggregate option in your resolver todo-item/todo-item.module.ts
All aggregate queries use the following naming convention ${objectName}Aggregate. Below is a fragment from the generated schema for TodoItem
nestjs-query will only expose number fields for avg and sum.

Examples

Basic

With GroupBy

To group your aggregate queries you can add a groupBy to specify one or more fields to group on.

With Filter

You can also provide a filter to only aggregate on a subset of data.
When using the count aggregate only non-null fields will be counted. For example assume description is null for all todo items you will get 0 back.

Aggregating Relations

When using the enableAggregate option any defined many relations will also expose a aggregate query {relationName}Aggregate Building on the previous example assume TodoItem has a subTasks connection. The following schema fragment will be created

Examples

Basic

In this example we’ll aggregate on all related subTasks.

With GroupBy

In this example we’ll aggregate on all related subTasks and group by completed.

With Filter

This example will aggregate all related subTasks that are not completed.

Advanced

Enabling Aggregates Only For Root

When using the enableAggregate option it will enable aggregates on the root type as well as all relations. If you only want to expose aggregate functionality on the root type you can specify the aggregate option.

Disable Aggregate for Single Relation

You can also selectively disable aggregates on an individual relation by specifying the enableAggregate option when defining the relation.

Group aggregate on DAY/WEEK/MONTH

This functionality only works for typeorm!
You can change how datetime fields are grouped by passing WEEK, MONTH or YEAR to the date fields by.
Edit this page