MCPcopy Create free account
hub / github.com/TanStack/db / groupBy

Method groupBy

packages/db/src/query/builder/index.ts:705–720  ·  view source on GitHub ↗

* 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>)

Source from the content-addressed store, hash-verified

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

Calls 4

_getCurrentAliasesMethod · 0.95
createRefProxyFunction · 0.85
toExpressionFunction · 0.85
mapMethod · 0.45

Tested by 3

createDistinctTestsFunction · 0.64
createGroupByTestsFunction · 0.64
createOrderByTestsFunction · 0.64