Entity / Laws
When a rule depends on the past, store the fact
Refinements read one row, invariants read before and after, and document laws read the previous and next snapshot. Instead of querying historical logs for "was ever published", store a boolean field and keep it monotone with a transition invariant:
ts
const ListingFields = Entity.define(
'Listing',
Schema.Struct({
id: Schema.String,
status: Schema.Literals(['draft', 'published']),
everPublished: Schema.Boolean,
}),
)
const Listing = ListingFields.pipe(
Entity.invariant(
'everPublishedStays',
({ before, after }) =>
Expr.or(Expr.not(Expr.eq(before.everPublished, true)), Expr.eq(after.everPublished, true)),
{ message: 'A listing once published stays marked', path: ['everPublished'] },
),
)
const WasPublished = Entity.refine(Listing, 'WasPublished', { everPublished: true })For cross-row rules such as aggregate totals across a list, enforce them in your server handler; see Declared writes.