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:
StockFieldsandStockshare the invariant across writes, forms, and server bindings (while guard rules stay attached to theGuards.attachdescriptor). - Evaluated over the post-write row: The SQL writer folds invariants into
WHEREusing the updated column values (reserved <= 3), allowing broken rows to be repaired while refusing writes that leave a row invalid. - Strict null semantics: A
nulloperand fails an invariant unless handled explicitly withExpr.isNull. - Independent of authority: Admins cannot bypass invariants, and hidden-field violations omit the rule ID and
pathfrom theInvalidrefusal. - Checked on client and database:
Data.mutaterejects definitely-broken writes immediately withInvalid, forms display errors atpathon submit, andcheckConstraints(binding)generates SQLCHECKconstraints for defense in depth.