Skip to content

v1.0.35

Choose a tag to compare

@brunolau brunolau released this 16 Oct 18:45
4650d38

v1.0.35

A projected .min(), .max() or .sum() of a collection can now be an orderBy key. Ordering by one threw
buildSql() on CollectionQueryBuilder is only supported for EXISTS and COUNT aggregations, so a grid that shows a
child table's earliest date, latest date or total could not sort by that column. The key now orders by the
projection's output alias, the way a projected sql fragment does. The aggregate the projection already computes is
not evaluated a second time. Queries that do not order by such a key are unchanged.

Fixed: ordering by a projected collection MIN / MAX / SUM

const books = await db.books
  .select(b => ({
    id: b.id,
    firstEdition: b.editions!.min(ed => ed.id),
    lastLabel: b.editions!.where(ed => gt(ed.id, 1)).max(ed => ed.label),
  }))
  .orderBy(r => [[r.lastLabel, 'ASC']])
  .toList();
-- …
ORDER BY "lastLabel" ASC
  • When: the selector returns a projected collection aggregate whose type is MIN, MAX or SUM, at the top
    level or inside a nested object. A nested one orders by its flattened alias, "__nested__<key>__<leaf>".
  • Unchanged: a projected .count() or .exists() is still an expression key that renders its own subquery.
    Collections that are not aggregated (toList(), firstOrDefault()) still pass through as navigation builders.
  • Limits: the same as for a projected sql fragment. The key names an output alias, so an sql expression
    over it is refused (orderBy(): an expression cannot read the projected value …). A later select() that drops
    the alias also drops the value the key names.

Tests

tests/queries/orderby-navigation.test.ts, under orderBy after select(): one case orders books by a projected
min, a filtered max, a sum and a nested min, and checks both the row order and the rendered ORDER BY.
The case fails on 1.0.34 with the buildSql() error above.