/** * What a holder of rows has: the world `Judge` reads an `Expr` over. * * A server's database holds every row, so whatever an expression asks has an * answer there. A client holds what it was sent: some rows, some fields of * them, and none it may not see. `HeldRow` is how a holder says which, so an * interpreter can tell "this field is null" from "I do not have this field", * which a plain record cannot: both read as a missing key. */ import { Option } from 'effect' import { relationsOf } from './descriptor.js' import { Query, type AnyQuery, type OneRelation } from './expr.js' import type { AnyEntity, EntityIdentity, EntityMember } from './index.js' import { own, put } from './record.js' /** A value a holder has, or why it does not have it. */ export type Held = | { readonly _tag: 'Held'; readonly value: A } | { readonly _tag: 'Unheld'; readonly reason: string } /** The rows pointing at one row through a relation, as far as a holder has them. */ export interface Pointing { readonly rows: ReadonlyArray /** * Why a row pointing here may be missing from `rows`; none when it holds * every one whose fields hold the values `pinned` names. */ readonly missing: Option.Option } /** * The values an exists' filter fixes on the rows it counts, by field key (a * link as the id it points at): a row with another value in one of them is * not counted, so a holder holding every pointing row with these values holds * every one that counts (`Judge.pins`). */ export type Pinned = ReadonlyMap /** * One row as a holder has it. Values are **encoded**, as a store keeps them * and as `evaluate` reads a row. */ export interface HeldRow { /** A field's value; unheld when the holder lacks it or may not see it. */ readonly field: (key: string) => Held /** * The row a `one` relation of this row points at: none for a null link, or * a row the reader may not see when the holder knows it holds every row it may. */ readonly follow: (relation: OneRelation) => Held> /** * The rows pointing at this one through `relation`, a `one` relation of * another Entity, for an exists whose filter fixes `pinned`. */ readonly pointing: (relation: OneRelation, pinned: Pinned) => Pointing } /** One Entity's rows, as `Held.rows` takes them. */ export interface HeldTable { readonly entity: AnyEntity /** * Each row's values by key, encoded; a `one` relation's under its own key, * as the id of the row it points at or null. A key a row lacks is unheld. */ readonly rows: ReadonlyArray>> /** * Whether these are every row of the Entity the reader may see, so a row * not among them is known absent. True when not given. */ readonly complete?: boolean | undefined /** Keys the reader may not see on any of these rows. */ readonly hidden?: ReadonlyArray | undefined } /** The rows `Held.rows` holds, by Entity. */ export interface HeldRows { /** The row of `entity` with this id, when it is held. */ readonly row: (entity: AnyEntity, id: unknown) => Option.Option /** Every held row of `entity`. */ readonly rows: (entity: AnyEntity) => ReadonlyArray /** * The row of `entity` with this id as the holder can say: none when it * knows there is no such row it may see, unheld when it cannot tell. */ readonly find: (entity: AnyEntity, id: unknown) => Held> } const held = (value: A): Held => ({ _tag: 'Held', value }) const unheld = (reason: string): Held => ({ _tag: 'Unheld', reason }) /** * Rows held as plain records: each table complete or not, with keys hidden. * What a test, a conformance check or a server that has the rows in hand * judges over. */ const rowsOf = (tables: ReadonlyArray): HeldRows => { interface Table { readonly entity: AnyEntity readonly complete: boolean readonly hidden: ReadonlySet readonly byId: ReadonlyMap readonly views: ReadonlyArray } const byToken = new Map() const tableOf = (identity: EntityIdentity) => Option.fromUndefinedOr(byToken.get(identity.token)) const find = (target: AnyEntity, id: unknown): Held> => Option.match(tableOf(target.identity), { onNone: () => unheld(`no ${target.name} is held`), onSome: reached => Option.match(Option.fromUndefinedOr(reached.byId.get(id)), { onSome: row => held(Option.some(row)), onNone: () => reached.complete ? held(Option.none()) : unheld(`${target.name} ${String(id)} is not held`), }), }) const view = (table: Table, record: Readonly>): HeldRow => { const read = (key: string): Held => table.hidden.has(key) ? unheld(`${table.entity.name}.${key} is hidden from this reader`) : Object.hasOwn(record, key) ? held(record[key]) : unheld(`${table.entity.name}.${key} is not held`) return { field: read, follow: relation => { const link = read(relation.key) if (link._tag === 'Unheld') return link if (link.value === null || link.value === undefined) return held(Option.none()) return find(relation.target(), link.value) }, pointing: relation => Option.match(tableOf(relation.owner), { onNone: () => ({ rows: [], missing: Option.some(`no ${relation.owner.name} is held`), }), onSome: from => { const id = record.id const named = `${relation.owner.name}.${relation.key}` const rows: Array = [] let missing = from.complete ? Option.none() : Option.some(`not every ${relation.owner.name} is held`) for (const each of from.views) { const link = each.field(relation.key) if (link._tag === 'Unheld') { missing = Option.orElse(missing, () => Option.some(link.reason)) continue } if (link.value === id) rows.push(each) } return { rows, missing: Option.map(missing, reason => `${named}: ${reason}`) } }, }), } } for (const given of tables) { const views: Array = [] const byId = new Map() const table: Table = { entity: given.entity, complete: given.complete ?? true, hidden: new Set(given.hidden ?? []), byId, views, } for (const record of given.rows) { const each = view(table, record) views.push(each) byId.set(record.id, each) } byToken.set(given.entity.identity.token, table) } return { row: (entity, id) => Option.flatMap(tableOf(entity.identity), table => Option.fromUndefinedOr(table.byId.get(id))), rows: entity => Option.match(tableOf(entity.identity), { onNone: () => [], onSome: table => table.views }), find, } } const linksRead = new WeakMap>() /** * The links a query reads on its own row: each relation of its Entity it * reads (a body reads only a `one` relation as a value). A relation it reads * existed when it was built, so what it reads is known once. */ const linksOf = (query: AnyQuery): ReadonlyArray => { const known = linksRead.get(query) if (known !== undefined) return known const relations: Readonly> = query.entity.relations const links = Query.dependencies(query) .fields.filter( field => field.owner.token === query.entity.identity.token && Object.hasOwn(relations, field.key), ) .map(field => field.key) linksRead.set(query, links) return links } /** * A row as a Source answers it and a Remote store keeps it, each `one` * relation as its ref key (`'User:ada'`), given as an interpreter reads it: * each link `query` reads as the id it points at. The row itself when the * query reads no link. */ const fromRefs = ( query: AnyQuery, row: Readonly>, ): Readonly> => { const links = linksOf(query) if (links.length === 0) return row const read: Record = { ...row } for (const key of links) { const ref = own(row, key) if (ref !== null && ref !== undefined) put(read, key, idOfRef(ref, `${query.entity.name}.${key}`)) } return read } /** * Whether `key` is a `one` relation of the Entity `owner` names: what a * ref-keyed holder reads as the id it points at on a row it knows only by * identity, as a row pointing through `HeldRow.pointing`'s relation is. */ const isLink = (owner: EntityIdentity, key: string): boolean => { const member = own(relationsOf(owner), key) return member?._tag === 'Relation' && member.cardinality === 'one' } /** The id a ref key (`'User:ada'`) names, refusing what is none, as `where` holds it. */ export const idOfRef = (ref: unknown, where: string): string => { const separator = typeof ref === 'string' ? ref.indexOf(':') : -1 if (separator === -1) { throw new Error( `[foldkit-entity] ${where} holds ${JSON.stringify(ref)}, which is no ref key ("Entity:id")`, ) } return (ref as string).slice(separator + 1) } export const Held = { /** A value the holder has. */ of: held, /** A value the holder does not have, and why. */ unheld, match: ( value: Held, cases: { readonly onHeld: (value: A) => B; readonly onUnheld: (reason: string) => B }, ): B => { switch (value._tag) { case 'Held': return cases.onHeld(value.value) case 'Unheld': return cases.onUnheld(value.reason) } }, /** Rows held as plain records; see `HeldTable`. */ rows: rowsOf, fromRefs, isLink, }