// Single responsibility: `text()`'s two builders — the text column, which keeps `.sealed()` across // the links that may precede it, and the sealed column that link returns. What a sealed column may // NOT be is refused here, where the chain was written; what an ENTITY may not do with one is // `sealed-declaration.ts`. import { isSealed } from '@ultimat3/core'; import { makeColumn } from './column'; import { refuseColumn } from './refuse'; import { refuseSealed, sealedUniqueOpaque } from './sealed-errors'; import type { Column, ColumnMeta, SealedColumn, SealedOptions, TextColumn } from './types'; type Text = string | null; /** * A sealed column's parser. Inside a driver the value is already the sealed string — longer than * any `max`, and not the author's to validate — so it passes; everywhere else it is the plaintext * and the column's own parser judges it, `max` included. */ const sealedParse = (parse: (value: unknown) => T) => (value: unknown): T => isSealed(value) ? (value as T) : parse(value); export const makeSealedColumn = ( meta: ColumnMeta, parse: (value: unknown) => T, optional: Optional, ): SealedColumn => { const base = makeColumn(meta, parse, optional); const lookup = meta.sealed?.lookup === true; const again = (next: Column): SealedColumn => makeSealedColumn(next.$meta, next.$parse, optional); return { ...base, // The runtime twin of the phantom: one cast, where a boolean becomes the literal it implies. $sealed: (lookup ? 'lookup' : 'opaque') as SealedColumn['$sealed'], nullable: () => again(base.nullable()), column: (name) => again(base.column(name)), unique: () => { // Uniqueness is the database comparing ciphertexts, so it needs equal values to seal equal. if (!lookup) throw sealedUniqueOpaque(); return again(base.unique()); }, default: () => refuseSealed( 'given a default', 'a default is a literal in the DDL, so the database would store it as plaintext', 'text().sealed() with no .default(…) — supply the value in the insert, where it is sealed', ), primaryKey: () => refuseSealed( 'a primary key', 'a key is compared, ordered and referenced by the database, which holds ciphertext', 'uuid().primaryKey() for the key, and text().sealed() for the secret beside it', ), tenant: () => refuseSealed( 'the tenant column', 'every query filters on the tenant, and a sealed value cannot be filtered on', 'uuid().tenant() for the tenant, and text().sealed() for the secret beside it', ), references: () => refuseSealed( 'a foreign key', 'the database joins on the stored value, which is ciphertext', 'uuid().references(() => other.id) for the key, and text().sealed() for the secret', ), searchable: () => refuseSealed( 'searchable', 'the search vector is generated by the database from the stored value, which is ciphertext', 'text().searchable() on a column that is not sealed — a secret is not search text', ), }; }; /** What `.sealed(options)` turns the declared meta into. */ const sealedMeta = ( meta: ColumnMeta, options: SealedOptions, plaintext: (value: unknown) => unknown, ): ColumnMeta => { if (meta.default !== undefined) { refuseSealed( 'given a default', 'a default is a literal in the DDL, so the database would store it as plaintext', 'text().sealed() with no .default(…) — supply the value in the insert, where it is sealed', ); } const lookup = options.lookup === true; if (meta.unique && !lookup) throw sealedUniqueOpaque(); // `check` and `length` go: they are `char_length(col) <= max`, and the column now holds a sealed // string longer than any plaintext bound. The bound still holds where it can — in `$parse`, // against the plaintext, before it is sealed. Any OTHER check has no such home, so it is refused // rather than dropped: the rule would vanish from the DDL with nothing to say it went. if (meta.check !== undefined && meta.length === undefined) { refuseSealed( 'given a CHECK', 'a CHECK runs in the database against the stored value, which is ciphertext', 'text().sealed() with no CHECK — validate the plaintext in the action input schema', ); } const { check: _check, length: _length, ...rest } = meta; return { ...rest, sealed: { lookup, plaintext } }; }; /** * `'C'` is typed, and refused at runtime too: a JavaScript caller or a cast reaches here with any * string, and an unknown collation is a `42704` inside `ROLE=migrate`, far from this line. */ const declaredCollation = (collation: unknown): 'C' => collation === 'C' ? collation : refuseColumn( 'collate', `"${String(collation)}" is not a collation a column may declare: only 'C' (code-point order)`, "text().collate('C') — or drop .collate() for the database's default collation", ); export const makeTextColumn = ( meta: ColumnMeta, parse: (value: unknown) => T, optional: Optional, ): TextColumn => { const base = makeColumn(meta, parse, optional); const again = (next: Column): TextColumn => makeTextColumn(next.$meta, next.$parse, next.$optional); return { ...base, nullable: () => again(base.nullable()), unique: () => again(base.unique()), default: (value) => again(base.default(value)), column: (name) => again(base.column(name)), collate: (collation) => makeTextColumn( { ...meta, collation: declaredCollation(collation) }, parse, optional, ), sealed: (options = {}) => makeSealedColumn(sealedMeta(meta, options, parse), sealedParse(parse), optional), }; };