Remote / foldkit-remote-server

EntityWriter

interface · packages/remote-server/src/index.ts:438

interface EntityWriter<P, R = never> {
  readonly entity: string
  readonly requires?: true
  readonly preconditions?: true
  readonly increments?: true
  readonly assignments?: true
  readonly operators?: MemoryOperators | undefined
  readonly pins?: true
  readonly revision?: string | undefined
  readonly commutes?: (assignment: WriteAssignment) => boolean
  readonly ledger?: true
  readonly invariants?: true
  readonly postconditions?: true
  readonly cascades?: true
  readonly restricts?: true
  readonly clears?: true
  readonly checksTargets?: true
  readonly foreignKey?: (relation: LinkRelation) => boolean
  readonly refuses?: (write: AnyWrite) => ReadonlyArray<string>
  readonly store: StoreCapabilities
  readonly judgesInStatement?: true
  readonly needs?: (write: AnyWrite) => ReadonlyArray<StoreRequirement>
  readonly recorded?: (context: {
    readonly operation: string
    readonly request: string
    readonly id: string
    readonly principal: P
  }) => Effect.Effect<
    Option.Option<Readonly<Record<string, unknown>>>,
    RemoteServerError,
    R
  >
  readonly update: (context: {
    readonly id: string
    readonly operation: Option.Option<string>
    readonly request: string
    readonly values: Readonly<Record<string, unknown>>
    readonly increments: Readonly<Record<string, number>>
    readonly assignments: Readonly<Record<string, Computation>>
    readonly links: Readonly<Record<string, Option.Option<EntityRef>>>
    readonly expect: Option.Option<{
      readonly field: string
      readonly revision: unknown
    }>
    readonly when: Option.Option<WriteWhen>
    readonly requires?: WriteRequirement | undefined
    readonly ensures?: Invariant | undefined
    readonly principal: P
  }) => Effect.Effect<
    Option.Option<Readonly<Record<string, unknown>>>,
    | RemoteServerError
    | RemoteForbidden
    | RemoteUnauthenticated
    | RemoteUnmet
    | RemoteInvalid,
    R
  >
  readonly insert: (context: {
    readonly id: string
    readonly operation: Option.Option<string>
    readonly request: string
    readonly values: Readonly<Record<string, unknown>>
    readonly links: Readonly<Record<string, Option.Option<EntityRef>>>
    readonly ensures?: Invariant | undefined
    readonly principal: P
  }) => Effect.Effect<
    Readonly<Record<string, unknown>>,
    | RemoteServerError
    | RemoteForbidden
    | RemoteUnauthenticated
    | RemoteInvalid
    | RemoteTaken,
    R
  >
  readonly delete: (context: {
    readonly id: string
    readonly expect: Option.Option<{
      readonly field: string
      readonly revision: unknown
    }>
    readonly when: Option.Option<WriteWhen>
    readonly requires?: WriteRequirement | undefined
    readonly cascading?: ReadonlyArray<CascadeRequirement> | undefined
    readonly clearing?: ReadonlyArray<CascadeRequirement> | undefined
    readonly principal: P
  }) => Effect.Effect<
    DeleteOutcome,
    RemoteServerError | RemoteForbidden | RemoteUnauthenticated | RemoteUnmet,
    R
  >
  readonly read?: (context: {
    readonly id: string
    readonly fields: ReadonlyArray<string>
    readonly principal: P
  }) => Effect.Effect<
    Option.Option<Readonly<Record<string, unknown>>>,
    RemoteServerError,
    R
  >
  readonly linked?: (context: {
    readonly id: string
    readonly relations: ReadonlyArray<string>
  }) => Effect.Effect<
    Option.Option<Readonly<Record<string, unknown>>>,
    RemoteServerError,
    R
  >
}

In the guides

Members

EntityWriter.entity

property · packages/remote-server/src/index.ts:439

readonly entity: string

EntityWriter.requires

property · packages/remote-server/src/index.ts:446

readonly requires?: true

Whether update and delete hold the row to their requires in the statement that writes it, and refuse one that does not with Forbidden. A guard hands such a writer the rules it cannot judge over one row, and refuses to wrap a writer without it.

EntityWriter.preconditions

property · packages/remote-server/src/index.ts:452

readonly preconditions?: true

Whether update and delete honour when. RemoteServer.write refuses a write with a precondition over a writer without it, which would write whatever the row held.

EntityWriter.increments

property · packages/remote-server/src/index.ts:458

readonly increments?: true

Whether update adds increments to their fields where it writes. RemoteServer.write refuses a write with an increment over a writer without it, which would leave the fields where they were.

EntityWriter.assignments

property · packages/remote-server/src/index.ts:464

readonly assignments?: true

Whether update computes assignments where it writes, over the row it writes. RemoteServer.write refuses a write computing a field over a writer without it.

EntityWriter.operators

property · packages/remote-server/src/index.ts:470

readonly operators?: MemoryOperators | undefined

The memory table this writer works out the row a write leaves by (Write.apply's), where its own statement computes it: what a wrapping writer (a guard) judges by, so it judges the value the database writes.

EntityWriter.pins

property · packages/remote-server/src/index.ts:477

readonly pins?: true

Whether update writes only the row it read, at the revision it read it, whatever the write expects: what a guard does so its rules judge the row written. Concurrent writes then cannot interleave, so an increment of a real number, or any computed assignment, needs no expect of its own.

EntityWriter.revision

property · packages/remote-server/src/index.ts:486

readonly revision?: string | undefined

The field holding the row's revision, which every update this writer lands moves on by one (the writer itself, or a database trigger such as foldkit-remote-drizzle's revisionTrigger). update answers it with what it wrote, and RemoteServer.write sends it as the answered row's revision, so a client tells a write that landed right after the row it held from one another change came between.

EntityWriter.commutes

property · packages/remote-server/src/index.ts:494

readonly commutes?: (assignment: WriteAssignment) => boolean

Whether assignment, landed without expect, gives one result whatever order concurrent writes land in: its operator op(field, amount), with commutativity and associativity tested for the implementations this writer runs. RemoteServer.write asks of each computed assignment a write lands without a revision.

EntityWriter.ledger

property · packages/remote-server/src/index.ts:501

readonly ledger?: true

Whether update and insert record each write they land under its operation, in the transaction that writes, and answer a retry of it from that record rather than writing again. An increment that expects no revision needs one, and so does a create a client may send again.

EntityWriter.invariants

property · packages/remote-server/src/index.ts:509

readonly invariants?: true

Whether update and insert refuse a row, or a change, that would break one of its Entity's invariants (Entity.invariant), with RemoteInvalid, judged over the rows they write; an insert judges the state invariants alone. RemoteServer.write refuses a write the Entity holds to any through a writer without it.

EntityWriter.postconditions

property · packages/remote-server/src/index.ts:517

readonly postconditions?: true

Whether update and insert refuse a row they would leave that breaks the write's own postcondition (ensures, a becomes enumeration could not decide), with RemoteInvalid, judged as they judge the Entity's invariants. RemoteServer.write refuses a write with one through a writer without it, which would land a row outside the state.

EntityWriter.cascades

property · packages/remote-server/src/index.ts:526

readonly cascades?: true

Whether delete removes, in the transaction that deletes the row, every row declared to go with it (Entity.cascading), and answers them as cascaded; and holds those rows to the cascading requirements it is given, judged before anything is removed, refusing the whole delete where one fails. RemoteServer.write refuses a delete of an Entity that cascades through a writer without it.

EntityWriter.restricts

property · packages/remote-server/src/index.ts:534

readonly restricts?: true

Whether delete refuses, in the statement that deletes, a row that rows point at through a relation declared onDelete: 'restrict' (Entity.restricting), answering Referenced. RemoteServer.write refuses a delete of an Entity something restricts through a writer without it.

EntityWriter.clears

property · packages/remote-server/src/index.ts:543

readonly clears?: true

Whether delete clears, in the transaction that deletes the row, the link of every row pointing at it through a relation declared onDelete: 'setNull' (Entity.clearing), answers the rows the principal may see as cleared, and holds them to the clearing requirements it is given, refusing the whole delete where one fails. RemoteServer.write refuses a delete of an Entity something clears through a writer without it.

EntityWriter.checksTargets

property · packages/remote-server/src/index.ts:553

readonly checksTargets?: true

Whether insert and update refuse, in the same atomic step or transaction that writes, a link through a relation declared onDelete (restrict, cascade or setNull) to a row that is not there, answering Invalid with the relation's key as its path: such a relation promises that no row points at a row that is gone. RemoteServer.write refuses a write setting one through a writer without it, unless the database's own foreign key holds that link (foreignKey).

EntityWriter.foreignKey

property · packages/remote-server/src/index.ts:560

readonly foreignKey?: (relation: LinkRelation) => boolean

Whether the database holds the links of relation by a foreign key it enforces, which locks the row pointed at against its deleters at any isolation. What a delete or a link through relation needs of store is then held by that constraint (@doeixd/metakit/store's constraint).

EntityWriter.refuses

property · packages/remote-server/src/index.ts:566

readonly refuses?: (write: AnyWrite) => ReadonlyArray<string>

Why this writer cannot land write, each a sentence; none when it can. RemoteServer.write asks once, where the write is registered, for what only the store knows: a column's type, say, which the Entity does not say.

EntityWriter.store

property · packages/remote-server/src/index.ts:575

readonly store: StoreCapabilities

How the store this writer writes to runs concurrent writes (@doeixd/metakit/store's vocabulary). Every writer says: RemoteServer.write refuses a writer that names none. A rule that reads other rows (a when or an invariant with a path or an exists) holds only where the writers of those rows run one at a time, so it is refused through a store that cannot.

EntityWriter.judgesInStatement

property · packages/remote-server/src/index.ts:586

readonly judgesInStatement?: true

Whether every rule update, insert and delete are handed (when, requires, invariants, a postcondition, cascading and clearing requirements) is judged inside the statement, or the one batch, that writes, never from a row read before it (an insert's invariants may be judged over its own values, which reads no row). A rule over other rows then holds on a store whose isolation reaches one statement (D1, isolates: 'statement'); RemoteServer.write refuses one there through a writer without it.

EntityWriter.needs

property · packages/remote-server/src/index.ts:592

readonly needs?: (write: AnyWrite) => ReadonlyArray<StoreRequirement>

What this writer's own rules need of store for write, beyond the write's: a guard's rules over other rows. RemoteServer.write holds the store to them with the write's own, and refuses the write where it cannot.

EntityWriter.recorded

property · packages/remote-server/src/index.ts:599

readonly recorded?: (context: {
  readonly operation: string
  readonly request: string
  readonly id: string
  readonly principal: P
}) => Effect.Effect<
  Option.Option<Readonly<Record<string, unknown>>>,
  RemoteServerError,
  R
>

The answer recorded for operation, when this writer has a ledger and holds one: what a wrapping writer (a guard) answers a retry with, without deciding again over a row that has moved since. One recorded for another request fails.

EntityWriter.update

property · packages/remote-server/src/index.ts:611

readonly update: (context: {
  readonly id: string
  readonly operation: Option.Option<string>
  readonly request: string
  readonly values: Readonly<Record<string, unknown>>
  readonly increments: Readonly<Record<string, number>>
  readonly assignments: Readonly<Record<string, Computation>>
  readonly links: Readonly<Record<string, Option.Option<EntityRef>>>
  readonly expect: Option.Option<{
    readonly field: string
    readonly revision: unknown
  }>
  readonly when: Option.Option<WriteWhen>
  readonly requires?: WriteRequirement | undefined
  readonly ensures?: Invariant | undefined
  readonly principal: P
}) => Effect.Effect<
  Option.Option<Readonly<Record<string, unknown>>>,
  | RemoteServerError
  | RemoteForbidden
  | RemoteUnauthenticated
  | RemoteUnmet
  | RemoteInvalid,
  R
>

EntityWriter.insert

property · packages/remote-server/src/index.ts:656

readonly insert: (context: {
  readonly id: string
  readonly operation: Option.Option<string>
  readonly request: string
  readonly values: Readonly<Record<string, unknown>>
  readonly links: Readonly<Record<string, Option.Option<EntityRef>>>
  readonly ensures?: Invariant | undefined
  readonly principal: P
}) => Effect.Effect<
  Readonly<Record<string, unknown>>,
  | RemoteServerError
  | RemoteForbidden
  | RemoteUnauthenticated
  | RemoteInvalid
  | RemoteTaken,
  R
>

Inserts the row and answers it. An id already taken fails with RemoteTaken, whatever the row there holds and whether its asker may see it, so a create never tells what it may not read; unless the writer's ledger records operation as the insert that made it (a retry), answered with the row as it now is.

EntityWriter.delete

property · packages/remote-server/src/index.ts:684

readonly delete: (context: {
  readonly id: string
  readonly expect: Option.Option<{
    readonly field: string
    readonly revision: unknown
  }>
  readonly when: Option.Option<WriteWhen>
  readonly requires?: WriteRequirement | undefined
  readonly cascading?: ReadonlyArray<CascadeRequirement> | undefined
  readonly clearing?: ReadonlyArray<CascadeRequirement> | undefined
  readonly principal: P
}) => Effect.Effect<
  DeleteOutcome,
  RemoteServerError | RemoteForbidden | RemoteUnauthenticated | RemoteUnmet,
  R
>

Deletes the row, while it is still at the revision when one is expected, and, when it cascades, the rows declared to go with it, in the same transaction: a DeleteOutcome.

EntityWriter.read

property · packages/remote-server/src/index.ts:712

readonly read?: (context: {
  readonly id: string
  readonly fields: ReadonlyArray<string>
  readonly principal: P
}) => Effect.Effect<
  Option.Option<Readonly<Record<string, unknown>>>,
  RemoteServerError,
  R
>

The row as principal may see it, under the read rule its Source applies: none when there is no such row, or it is hidden. Given, a write's answer is read back through it, so an answer never shows its writer a row it may not read, and a row the write hid from them answers as deleted.

EntityWriter.linked

property · packages/remote-server/src/index.ts:723

readonly linked?: (context: {
  readonly id: string
  readonly relations: ReadonlyArray<string>
}) => Effect.Effect<
  Option.Option<Readonly<Record<string, unknown>>>,
  RemoteServerError,
  R
>

Each of relations' links as stored, by ref key ('Project:p1') or null, whatever a reader may see of the row it points at; none when there is no such row. A guard reads it to tell a write that unlinks a row hidden from its writer from one that changes nothing.