Entity / foldkit-entity

Write

namespace · packages/entity/src/write.ts:1046

const Write = { update, insert, delete, expected, ensured, writes, contextValues, contextReads, footprint, reads, bind, boundReads, apply }

Its type

interface Write<
  E extends AnyEntity,
  Fields extends Schema.Struct.Fields,
  Kind extends WriteKind = WriteKind,
> {
  readonly _tag: Kind
  readonly input: EntityInput<E, Fields, any>
  readonly id: WriteSet & {
    readonly key: keyof Fields & string
  }
  readonly expect:
    | (WriteSet & {
        readonly key: keyof Fields & string
      })
    | undefined
  readonly when: Option.Option<AnyQuery>
  readonly from: Option.Option<AnyRefinement>
  readonly becomes: Option.Option<AnyRefinement>
  readonly ensures: Option.Option<Invariant>
  readonly sets: ReadonlyArray<WriteSet>
  readonly increments: ReadonlyArray<WriteIncrement>
  readonly assignments: ReadonlyArray<WriteAssignment>
  readonly links: ReadonlyArray<WriteLink>
  readonly context: ReadonlyArray<ContextSet>
}

One row of an Entity, by id, from an operation's input: updated, inserted with an id the client chose, or deleted. Kind is the one its constructor made, so what it may be refused with can follow it.

In the guides

Members

Write.update

const · packages/entity/src/write.ts:307

update: UpdateWrite

Sets, on the row whose id is the input's id key, every field the input maps a key to. expect names the key holding the revision the row was read at: an interpreter writes only a row still at that revision.

ts
const EditPost = Write.update(EditPostInput, { id: 'id' })

A key mapped to a one relation (Relation.input) points the row at the id it holds. Refused where it is written: an id that is not the Entity's id field, a many relation or a nested key (not written by an update yet), or nothing to set.

when is what the row must hold for the update to land, checked where it commits (({ input }) => Expr.eq(Post.fields.status, 'draft')); a row it does not hold of is refused as unmet. from: Draft (an Entity.refine state) joins that state's predicate to it.

becomes: Published says every row it can start from is in that state once it lands. It is proved where the write is declared, by enumerating those rows over finite fields: one it leaves outside throws, naming it. One enumeration cannot decide (becomes: InStock over an increment) is the write's postcondition, ensures: each writer judges it where the write commits and refuses a row it leaves outside as Invalid.

increment: { stock: 'quantity' } adds an unmapped number input to a field where it commits, and decrement: { balance: 'amount' } takes one away; neither is idempotent, so a retry needs expect or a ledger. A field moved by both, or also set or computed, is refused.

compute sets a field to what an expression comes to over the row before the write and the input, run where it commits ({ label: ({ row }) => concat(row.title, ' draft') }). It is refused for a field another key writes, an expression over the row after the write or another Entity, or one of a kind the field does not store.

Write.insert

function · packages/entity/src/write.ts:1097

insert<E extends AnyEntity, Fields extends Schema.Struct.Fields>(
  input: EntityInput<E, Fields, any>,
  options: {
    readonly id: keyof Fields & string
    readonly becomes?: RefinementOf<E> | undefined
  },
): Write<E, Fields, 'Insert'>

Inserts the row whose id is the input's id key, an id the client chose, with every field and one relation the input maps a key to. An insert expects no revision: there is no row to have read. A row already there under that id (a retry) is left as it is.

What its Entity sets from the write's context (Entity.sets: who asks, when) it writes too, never from the input: a server fills them, a client predicts them. An update writes those set on: 'write'. becomes, as an update's, is proved over what the insert sets, or else ensured.

Write.delete

const · packages/entity/src/write.ts:320

delete: DeleteWrite

Deletes the row whose id is the input's id key, only while it is still at the revision expect names, when it names one. It sets nothing, so a key mapped to a member is refused where it is written. A row already gone (a retry) is gone. when, as an update's, is what the row must hold.

Write.expected

function · packages/entity/src/write.ts:1123

expected<Fields extends Schema.Struct.Fields>(
  write: Write<AnyEntity, Fields>,
  value: Schema.Struct.Type<Fields>,
): Option.Option<{
  readonly field: string
  readonly revision: unknown
}>

The revision this input says the row was read at, encoded, with its field: what an interpreter writes only a row still at. None for a write that expects none.

Write.ensured

function · packages/entity/src/write.ts:1138

ensured(write: AnyWrite): Invariant

The postcondition a write's becomes holds it to where it commits, as the invariant a refusal names (Refusal.isInvalidBy(refusal, Write.ensured(Sell.write))). Throws for a write with none: one with no becomes, or one whose becomes was proved where it was declared.

Write.writes

function · packages/entity/src/write.ts:1146

writes(write: AnyWrite): ReadonlyArray<AnyField | OneRelation>

The members any invocation may write: what a query reading none of them cannot be changed by.

Write.contextValues

function · packages/entity/src/write.ts:1163

contextValues<E, R>(
  write: AnyWrite,
  by: ExprContext,
  read: ContextRead<E, R>,
): Effect.Effect<ContextValues, E, R>

What the context sets come to, encoded as the store holds them: each field's value and each relation's link. A path from the viewer is read through read, one row at a time. unfilled names each member there is no value for: none to read (no reading of the time, a row or field read does not hold), or a null where the member holds none (a visitor, a viewer of another Entity, a path reaching no row). A server refuses such a write; a client predicts nothing for the member.

Write.contextReads

function · packages/entity/src/write.ts:1211

contextReads(write: AnyWrite): ReadonlyArray<string>

The Entities a write's paths from the viewer read, by name: what a server must have a source of.

Write.footprint

function · packages/entity/src/write.ts:1226

footprint(write: AnyWrite): Footprint

What a write reads and writes of its row, by field key (Write.reads, and Write.writes with the revision it expects, which it moves on): what Laws.check judges it by.

Write.reads

function · packages/entity/src/write.ts:1238

reads(write: AnyWrite): ReadonlyArray<AnyField>

The fields a write reads from the row it writes: those its when reads, each it increments, each its assignments read, and the revision it expects. What an interpreter must read, and what a startup check judges it could disclose.

Write.bind

function · packages/entity/src/write.ts:1258

bind<Fields extends Schema.Struct.Fields>(
  write: Write<AnyEntity, Fields>,
  value: Schema.Struct.Type<Fields>,
  keys?: ReadonlyArray<string>,
): BoundWrite

The row this input writes and its new values, encoded as the store holds them: the patch a client shows before the server answers. keys, when given, narrows it to those input keys (what an author changed); the rest of the input is still the whole, validated value.

Write.boundReads

function · packages/entity/src/write.ts:1315

boundReads(
  bound: Pick<BoundWrite, 'increments' | 'assignments'>,
): ReadonlyArray<string>

The fields of the row before it that a bound write's amounts and assignments read: what to read to work out the row it leaves.

Write.apply

function · packages/entity/src/write.ts:1341

apply(
  row: Readonly<Record<string, unknown>>,
  bound: Pick<BoundWrite, 'values' | 'increments' | 'assignments'>,
  options: {
    readonly operators: MemoryOperators
  },
): {
  readonly row: Readonly<Record<string, unknown>>
  readonly unknown: ReadonlyArray<string>
}

The row a bound write leaves over row, the row before it: its values set, each amount added (a null stays null, as in SQL), and each assignment computed by operators over the row before, all by metakit's evaluator. unknown names each field it could not work out: one row lacks, or holds as no number for an amount, or an assignment calling an operator the table lacks. That field keeps what row holds. The one computation of this row: a guard judges it, a writer explains a miss with it, a client predicts it.

A value the evaluator refuses (a sum past the safe integer range, text holding a NUL) is a failure, never a write: it throws a QueryEvaluateError carrying the Refusal, as evaluate does.