ReviewOS

stacks/bun-query-builder

withSum/withAvg/withMax + per-relation constraint callback on withCount

#1046
Closed glennmichael123 opened this 2 months ago ·
2 months ago

Summary

withCount(...relations: string[]) exists but only accepts relation names — no per-relation constraint callback (Eloquent's withCount(['comments' => fn])) and no aggregate siblings. The subquery builders (buildHasCountSubquery, buildBelongsToManyCountSubquery) are already factored, so adding withSum(relation, col) / withAvg / withMax / withMin and an optional callback arg is incremental.

Effort: M. Location: src/client.ts:~3519. High Eloquent-parity value. Found via internal code audit.

9 days ago

Done in 0d6b1f5. Added withSum/withAvg/withMax/withMin — each aggregates a related column as a correlated subquery (mirroring withCount), aliased <relation>_<fn>_<column> (e.g. posts_sum_views). Works for hasMany/hasOne and belongsToMany (joins through the pivot); column identifiers validated. Regression test added; full suite green.

Note: the per-relation constraint callback on withCount (withCount(['comments' => fn])) is a separate follow-up — left it out of this change to keep it focused on the aggregate siblings.

Sign in to comment on this issue.