* Group rows by one or more columns for aggregation * * @param callback - A function that receives table references and returns the field(s) to group by * @returns A QueryBuilder with grouping applied (enables aggregate functions in SELECT and HAVING) * * @example * ```ts * // G
(callback: GroupByCallback<TContext>)
| 703 | * ``` |
| 704 | */ |
| 705 | groupBy(callback: GroupByCallback<TContext>): QueryBuilder<TContext> { |
| 706 | const aliases = this._getCurrentAliases() |
| 707 | const refProxy = createRefProxy(aliases) as RefsForContext<TContext> |
| 708 | const result = callback(refProxy) |
| 709 | |
| 710 | const newExpressions = Array.isArray(result) |
| 711 | ? result.map((r) => toExpression(r)) |
| 712 | : [toExpression(result)] |
| 713 | |
| 714 | // Extend existing groupBy expressions (multiple groupBy calls should accumulate) |
| 715 | const existingGroupBy = this.query.groupBy || [] |
| 716 | return new BaseQueryBuilder({ |
| 717 | ...this.query, |
| 718 | groupBy: [...existingGroupBy, ...newExpressions], |
| 719 | }) as any |
| 720 | } |
| 721 | |
| 722 | /** |
| 723 | * Limit the number of rows returned by the query |