Remote / Declared writes

What every row holds: an invariant

A precondition checks whether this write applies; an invariant declares what every stored row must satisfy regardless of which write touches it:

ts
const StockFields = Entity.define(
  'Stock',
  Schema.Struct({ id: Schema.String, quantity: Schema.Number, reserved: Schema.Number }),
)
const Stock = StockFields.pipe(
  Entity.invariant(
    'reservedWithinQuantity',
    ({ after }) => Expr.lte(after.reserved, after.quantity),
    { message: 'Reserved stock cannot exceed the quantity', path: ['reserved'] },
  ),
)
  • Travels with the Entity: StockFields and Stock share the invariant across writes, forms, and server bindings (while guard rules stay attached to the Guards.attach descriptor).
  • Evaluated over the post-write row: The SQL writer folds invariants into WHERE using the updated column values (reserved <= 3), allowing broken rows to be repaired while refusing writes that leave a row invalid.
  • Strict null semantics: A null operand fails an invariant unless handled explicitly with Expr.isNull.
  • Independent of authority: Admins cannot bypass invariants, and hidden-field violations omit the rule ID and path from the Invalid refusal.
  • Checked on client and database: Data.mutate rejects definitely-broken writes immediately with Invalid, forms display errors at path on submit, and checkConstraints(binding) generates SQL CHECK constraints for defense in depth.