Metakit / @doeixd/metakit/sql

relatedSubquery

function · packages/metakit/src/sqlRender.ts:188

function relatedSubquery<S>(
  fold: RelatedFold,
  parts: {
    readonly dialect: SqlDialect
    readonly from: S
    readonly matches: S
    readonly where: Option.Option<S>
    readonly value: Option.Option<S>
    readonly aggregate?: S | undefined
  },
  text: (sql: string) => S,
  join: (chunks: ReadonlyArray<S>) => S,
): Result.Result<S, SqlUnsupportedError>

A correlated subquery over the related rows, in a renderer's form of SQL: from is their table, matches says a row points at the row asked of, and an element passes only where where is true. Over none that passes, count and sum are 0, min and max null, any false and every true. A sum is held to safe integers and the safe range as evaluation holds it, and min and max to finite values.