Metakit / @doeixd/metakit/sql

toValue

function · packages/metakit/src/sql.ts:152

function toValue<Env, D extends SqlDialect = 'postgres'>(
  expr: Expr.Expr<Env, unknown>,
  options?: SqlOptions<D>,
): Result.Result<SqlFragment<D>, SqlUnsupportedError>

An Expr as a parameterized SQL value, for a SELECT list: null is NULL, a boolean stays apart from null, and a refusal is NULL or an error the database raises. That NULL is an answer only where nothing uses it as a value: an aggregate, an UPDATE and an operation that handles null itself would, so there the lowering writes the test of refusal beside the value, and refuses a term it cannot write that test for. A SQL value has one type, so a conditional whose arms are different kinds of value (a number and text) is refused, and SQL keeps no order of rows, so a related sum of a value that may hold a fraction, whose total depends on that order, is too.