Entity / foldkit-entity

Write

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

const Write = { update, insert, delete, expected, writes, bind }

Its type

interface Write<E extends AnyEntity, Fields extends Schema.Struct.Fields> {
  readonly _tag: 'Update' | 'Insert' | 'Delete'
  readonly input: EntityInput<E, Fields, any>
  readonly id: WriteSet & {
    readonly key: keyof Fields & string
  }
  readonly expect:
    | (WriteSet & {
        readonly key: keyof Fields & string
      })
    | undefined
  readonly sets: ReadonlyArray<WriteSet>
  readonly links: ReadonlyArray<WriteLink>
}

One row of an Entity, by id, from an operation's input: updated, inserted with an id the client chose, or deleted.

In the guides

Members

Write.update

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

update<E extends AnyEntity, Fields extends Schema.Struct.Fields>(
  input: EntityInput<E, Fields, any>,
  options: {
    readonly id: keyof Fields & string
    readonly expect?: keyof Fields & string
  },
): Write<E, Fields>

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.

Write.insert

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

insert<E extends AnyEntity, Fields extends Schema.Struct.Fields>(
  input: EntityInput<E, Fields, any>,
  options: {
    readonly id: keyof Fields & string
  },
): Write<E, Fields>

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.

Write.delete

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

delete<E extends AnyEntity, Fields extends Schema.Struct.Fields>(
  input: EntityInput<E, Fields, any>,
  options: {
    readonly id: keyof Fields & string
    readonly expect?: keyof Fields & string
  },
): Write<E, Fields>

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.

Write.expected

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

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.writes

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

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

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

Write.bind

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

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.