You can easily perform aggregations on any table/collection using the @aggregate directive. The allowed aggregation functions are count, min, max, avg, sum. For more advanced use cases, you can use prepared queries. You can also group, filter, sort data in the aggregations.
Example: Fetch total amount of expenditures for all users:
query {
expenditures(group: ["user_id"]) @postgres {
user_id
amount @aggregate(op: "sum")
}
}const { status, data } = db.get("expenditures")
.aggregateSum("amount")
You can specify the @aggregate directive against multiple fields in a query.
To specify multiple aggregations on the same field, you need to use the field argument in the @aggregate directive.
query {
expenditures(group: ["user_id"]) @postgres {
user_id
total_amount @aggregate(op: "sum" field: "amount")
max_amount @aggregate(op: "max" field: "amount")
min_amount @aggregate(op: "min" field: "amount")
average_amount @aggregate(op: "avg" field: "amount")
}
}// Coming soon!