Aggregation

Aggregate queries

Each model exposes an paginate method that can be used to create a "page" object, as described in the previous section. This object includes an aggregate property that can be populated by calling the appropriate PaginateBuilder methods:

const result = await Film.paginate()
.count()
.average('rentalCost')
.max('title')
.execute()
console.log(result)

would result in something like this object being printed to your console:

{
aggregate: {
count: 1000,
average: {
rentalCost: 4.79,
},
max: {
title: 'Zoolander',
},
},
}

The available aggregate methods include count, avg, sum, min and max. The PaginateBuilder also exposes methods like where, orderBy, limit and offset to narrow down which records to aggregate.