Learn / From declarations to the screen

Declare a write

A declared write names the target Entity, the accepted input schema, which fields it assigns, and an optional when precondition checked on the existing row:

ts
import { Write } from 'foldkit-entity'
import { Mutation } from 'foldkit-remote'

const Publish = Mutation.write(
  'Publish',
  Write.update(
    Entity.input(Product, Schema.Struct({ id: Schema.String, status: Schema.Literal('published') })),
    { id: 'id', when: () => Expr.eq(Product.fields.status, 'draft') },
  ),
)

console.log({
  tag: Publish.write._tag,
  sets: Publish.write.sets.map(set => set.field.key),
  hasWhen: Publish.write.when !== undefined,
})

Running that prints:

{ tag: 'Update', sets: [ 'status' ], hasWhen: true }

Publish updates one Product by id, constrains status to 'published', and checks that the row is currently 'draft'. A server writer executes the SQL statement and enforces authorization guards and invariants (Declared writes).