Metakit / API

@doeixd/metakit/aggregates

Cross-record Aggregates (@doeixd/metakit/aggregates). This module evaluates in memory and imports no SQL, so a page can use it; @doeixd/metakit/aggregates/sql compiles the same aggregates to SQL. What each keeps is not declared here: it is read off the fold's row of foldProperties.

  • Totals (Rows.sum, Rows.count): the folds sum and count are commutative, associative and invertible over the integers, so an insert, update or delete moves the value by an O(1) delta (new - old), starting from the fold's identity. The delta is exact only over that domain, so the contribution must be integer-valued (a Schema.Int, Money or Decimal field, or integer arithmetic over them): floating-point addition is not invertible (0.1 + 0.2 + 0.3 - 0.2 is 0.4000000000000001). A difference of two safe totals may leave the safe range itself, so updateDelta refuses it and replaceTotal computes the exact next total instead. The symbolic deltas let Prove.transition prove a rule over any number of rows.
  • Extremes and tests (Rows.min, Rows.max, Rows.every, Rows.any): their folds are not invertible, so an insert is O(1) while deleting or updating the extremum rescans the rows.

Missing values follow SQL: sum, min and max skip a null contribution, count with a predicate and any count an unknown predicate as false, and every counts it as failing (coalesce(p, false)).

evaluate is the kernel's fold of the same body over the rows (Arr.sum, Arr.any and the rest), so an aggregate and that fold in an expression give one answer: any and every settle past a row whose predicate refuses, sum, min and max refuse non-finite where a value is NaN or an infinity, and a value its Schema rejects gets the fold's answer.

Functions

anySemilattice aggregate: true when some row's predicate is true (false when empty). A row whose predicate refuses refuses the whole only where no row holds.
countThe number of rows, or of rows whose predicate is true.
everySemilattice aggregate: true when every row's predicate is true (true when empty). A row whose predicate is unknown counts as failing. A row whose predicate…
maxSemilattice aggregate: the largest non-null value (Option.none() when there is none), refused non-finite where a value is NaN or an infinity.
minSemilattice aggregate: the smallest non-null value (Option.none() when there is none), refused non-finite where a value is NaN or an infinity.
sumThe sum of an integer-valued contribution over the rows (0 when empty; a null contribution adds nothing). The contribution must read only integer-valued…

Interfaces

AnyRows.any: true when some row's predicate is true (false when empty).
CountRows.count: the number of rows, or of rows whose predicate is true.
EveryRows.every: true when no row's predicate is false or unknown (true when empty).
MaxRows.max: the largest non-null value (Option.none() when there is none).
MinRows.min: the smallest non-null value (Option.none() when there is none).
SumRows.sum: the sum of an integer-valued contribution (0 when empty).

Types