Metakit / @doeixd/metakit/sql

toUpdate

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

function toUpdate<
  State,
  Input,
  Emit,
  K extends keyof State & string,
  D extends SqlDialect = 'postgres',
>(
  table: string,
  transition: Transition<State, Input, Emit>,
  input: Input,
  options?: {
    readonly dialect?: D
    readonly column?: ColumnName
    readonly whereId?: {
      readonly field: Path.Selector<NoInfer<State>, K>
      readonly value: NoInfer<State[K]>
    }
    readonly foreign?: ForeignLowerings
  } & ClaimOptions,
): Result.Result<SqlFragment<D>, SqlUnsupportedError | Refused>

A Transition and one input as a guarded, parameterized SQL UPDATE of table: it writes only the rows the guard holds of and whose step does not refuse, each row being its own step (a refused one is left as it was, as Transition.run leaves it; ask Transition.run why). whereId narrows it to the row whose key field holds a value ({ field: state => state.id, value }, a null value the row whose key is null), written as the column column names for it. A write under a struct that may hold null refuses the step where it does, told by a field under it that is never null otherwise; a struct with no such field, or a write inside an Option, is SqlUnsupportedError. An input the transition's input Schema refuses is Refused. A written value that calls a custom operation is refused, since SQL cannot tell where the operation refuses, unless rewritePolicy lets the lowering read the operation's total claim (ClaimOptions). Where transition is a machine's arm, a SqlUnsupportedError names it: the transition "ApiSpend": [metakit/sql] no sqlite lowering of ….