@haverstack/record-adapter-do-sqlite 0.25.0 → 0.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../../sqlite-shared/src/schema.ts","../../sqlite-shared/src/cursor.ts","../../sqlite-shared/src/fts5.ts","../../sqlite-shared/src/query.ts","../../sqlite-shared/src/mappers.ts","../../sqlite-shared/src/executor.ts","../../sqlite-shared/src/config.ts","../../sqlite-shared/src/record-logic.ts","../../sqlite-shared/src/record-adapter.ts","../src/executor.ts","../src/index.ts"],"names":["StackBadRequestError","cursor","page","statement","contentSortEntry"],"mappings":";;;;AAUO,IAAM,iBAAA,GAAoB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8L1B,IAAM,eAAA,GAAkB;;;;;;;;AAexB,IAAM,sBAAA,GAAyB,CAAA,yBAAA,CAAA;AAwB/B,IAAM,iBAAA,GAAoB,CAAC,IAAA,EAAmB,IAAA,KAAmC;AACtF,EAAA,IAAA,CAAK,KAAK,sBAAsB,CAAA;AAEhC,EAAA,IAAA,CAAK,KAAK,iBAAiB,CAAA;AAC3B,EAAA,IAAA,CAAK,KAAK,eAAe,CAAA;AAC3B,CAAA;AC/NO,IAAM,WAAA,GAAoC,kBAAA;AAiBjD,IAAM,QAAA,GAAW,CAAC,IAAA,KAAwB;AACxC,EAAA,MAAM,KAAA,GAAQ,IAAI,WAAA,EAAW,CAAG,OAAO,IAAI,CAAA;AAC3C,EAAA,IAAI,MAAA,GAAS,EAAA;AACb,EAAA,KAAA,MAAW,IAAA,IAAQ,KAAA;AAAO,IAAA,MAAA,IAAU,MAAA,CAAO,aAAa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C/C;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,KAAA,GAAuB;AAAA,EAAC;AAAA;AAAA,EAG9B,MAAM,KAAA,GAAuB;AAAA,EAAC;AAChC","file":"index.js","sourcesContent":["import type { SqlExecutor } from './executor.js';\n\n/**\n * Schema DDL shared by SQLite-backed record adapters. TOKENS_SCHEMA_SQL\n * is split out from RECORD_SCHEMA_SQL because token storage lives in its\n * own file, never bundled with records — see NativeTokenStore in\n * record-adapter-sqlite and the StackTokenStore portability rationale\n * (docs/spec/wire-format.md § Authentication).\n */\n\nexport const RECORD_SCHEMA_SQL = `\n CREATE TABLE IF NOT EXISTS records (\n id TEXT PRIMARY KEY,\n type_id TEXT NOT NULL,\n created_at INTEGER NOT NULL,\n updated_at INTEGER NOT NULL,\n content TEXT NOT NULL CHECK (json_valid(content)),\n version INTEGER NOT NULL DEFAULT 1,\n parent_id TEXT,\n entity_id TEXT,\n app_id TEXT,\n principal_id TEXT,\n updated_by TEXT,\n updated_via TEXT,\n deleted_at INTEGER,\n unlisted_at INTEGER\n ) STRICT;\n\n -- Every edge a record carries, authority and data alike: a permission\n -- entry has element identity and a delta the same shape as a tag's, so\n -- it earns the same table and the same durability tier. The permissions\n -- and associations fields are projections over it, partitioned by\n -- kind. See docs/spec/access-control.md § Record-level permissions.\n --\n -- A relationship's target is (related_scope, related_id) plus one\n -- qualifier: related_stack for a record in another stack, related_ns for\n -- a foreign namespace. All four are in the primary key, so two targets\n -- differing only by namespace are two associations. A permission's\n -- grantee reuses them — related_scope 'entity' or 'group', related_id the\n -- DID or group id — plus related_role, which only a group grantee sets\n -- and which is in the key because member and admin name two different\n -- sets of people.\n --\n -- attachment_record_id stays out of the primary key: it annotates a\n -- reference rather than naming one, so re-pointing it updates the row an\n -- an amendAssociations() add lands on instead of writing a second reference to the same\n -- file. See docs/spec/attachments.md § Naming the upload a reference came from.\n --\n -- The primary key leads with record_id, so its implicit index already\n -- serves every by-record read and delete; the indexes below exist only\n -- for the filter directions it cannot answer.\n CREATE TABLE IF NOT EXISTS associations (\n record_id TEXT NOT NULL REFERENCES records(id),\n kind TEXT NOT NULL CHECK (kind IN ('tag', 'attachment', 'relationship', 'permission', 'anyone')),\n label TEXT NOT NULL,\n file_id TEXT NOT NULL DEFAULT '',\n related_scope TEXT NOT NULL DEFAULT '' CHECK (related_scope IN ('', 'record', 'entity', 'external', 'group')),\n related_id TEXT NOT NULL DEFAULT '',\n related_ns TEXT NOT NULL DEFAULT '',\n related_stack TEXT NOT NULL DEFAULT '',\n related_role TEXT NOT NULL DEFAULT '' CHECK (related_role IN ('', 'member', 'admin')),\n attachment_record_id TEXT NOT NULL DEFAULT '',\n PRIMARY KEY (record_id, kind, label, file_id, related_scope, related_id, related_ns, related_stack, related_role)\n ) STRICT;\n\n -- Content and the type it is read under. No parent_id, unlisted_at or\n -- associations column: none of those aspects bumps version, so no\n -- version ever snapshots one. See\n -- docs/spec/versioning.md § Version history.\n CREATE TABLE IF NOT EXISTS versions (\n record_id TEXT NOT NULL REFERENCES records(id),\n version INTEGER NOT NULL,\n type_id TEXT NOT NULL,\n content TEXT NOT NULL CHECK (json_valid(content)),\n updated_at INTEGER NOT NULL,\n entity_id TEXT,\n principal_id TEXT,\n updated_by TEXT,\n updated_via TEXT,\n PRIMARY KEY (record_id, version)\n ) STRICT;\n\n -- The durable half of a change event: which aspects moved, who moved\n -- them, and the association deltas nothing else retains. Envelope-level\n -- by design — content lives on a versions row, and copying it here\n -- would make the journal the larger of the two stores.\n -- See docs/spec/journal.md.\n CREATE TABLE IF NOT EXISTS journal (\n record_id TEXT NOT NULL REFERENCES records(id),\n seq INTEGER NOT NULL,\n at INTEGER NOT NULL,\n -- No 'purged': a purge destroys the log rather than appending to it,\n -- so the constraint names exactly the kinds an entry can carry.\n -- See docs/spec/journal.md § A purge destroys the journal.\n kind TEXT NOT NULL CHECK (kind IN ('created', 'changed', 'removed')),\n ops TEXT NOT NULL CHECK (json_valid(ops)),\n version INTEGER NOT NULL,\n type_id TEXT NOT NULL,\n parent_id TEXT,\n -- NULL means the entry names no origin (the record did not move);\n -- '' means it moved off the root. parent_id above needs no such\n -- spelling: it is state, where this is a key that may be absent --\n -- the same input/state split parentId follows everywhere.\n previous_parent_id TEXT,\n actor TEXT CHECK (actor IS NULL OR json_valid(actor)),\n -- One tagged edit per association the write moved, each carrying the\n -- state it displaced. See docs/spec/journal.md § The entry.\n associations TEXT CHECK (associations IS NULL OR json_valid(associations)),\n PRIMARY KEY (record_id, seq)\n ) STRICT;\n\n CREATE TABLE IF NOT EXISTS types (\n id TEXT PRIMARY KEY,\n base_id TEXT NOT NULL,\n version INTEGER NOT NULL,\n name TEXT NOT NULL,\n schema TEXT NOT NULL CHECK (json_valid(schema)),\n schema_hash TEXT NOT NULL,\n migrates_from TEXT,\n created_at INTEGER NOT NULL\n ) STRICT;\n\n -- One row per top-level scalar content field a record holds a value at:\n -- what it orders as, and the file it references. See\n -- docs/spec/adapters.md § Adapter backends and\n -- docs/spec/data-model.md § Sorting by a content field.\n --\n -- value_rank is 0 for a numeric value and 1 for a text one — the same\n -- split as \"num_value IS NULL\", but non-null, so one index can carry\n -- the whole ordering. file_id is set only for a 'file-ref' field.\n CREATE TABLE IF NOT EXISTS content_index (\n record_id TEXT NOT NULL REFERENCES records(id),\n field TEXT NOT NULL,\n value_rank INTEGER NOT NULL CHECK (value_rank IN (0, 1)),\n num_value REAL,\n text_key TEXT,\n text_value TEXT,\n file_id TEXT,\n PRIMARY KEY (record_id, field)\n ) STRICT;\n\n -- Indexes. Each filter column continues into (created_at, id) — the\n -- default ordering and its tiebreak — because these columns are\n -- low-cardinality by design, and a narrow index on one leaves the\n -- planner sorting the whole matching set before it can honor LIMIT.\n --\n -- deleted_at and unlisted_at deliberately have none: nothing searches\n -- for the rows they mark, and an index on a column every query tests\n -- for IS NULL only lures the planner off the one answering ORDER BY.\n CREATE INDEX IF NOT EXISTS idx_records_type_id ON records(type_id, created_at, id);\n CREATE INDEX IF NOT EXISTS idx_records_parent_id ON records(parent_id, created_at, id);\n CREATE INDEX IF NOT EXISTS idx_records_entity_id ON records(entity_id, created_at, id);\n CREATE INDEX IF NOT EXISTS idx_records_app_id ON records(app_id, created_at, id);\n CREATE INDEX IF NOT EXISTS idx_records_principal_id ON records(principal_id, created_at, id);\n CREATE INDEX IF NOT EXISTS idx_records_created_at ON records(created_at, id);\n CREATE INDEX IF NOT EXISTS idx_records_updated_at ON records(updated_at, id);\n CREATE INDEX IF NOT EXISTS idx_records_version ON records(version, id);\n\n -- Each association index ends with record_id so the semi-joins in\n -- query.ts read the matching record ids out of the index alone.\n -- idx_assoc_related carries related_ns ahead of related_id because a\n -- record- or entity-scoped target always stores '' there, which\n -- buildQueryPlan states explicitly to keep the prefix contiguous.\n CREATE INDEX IF NOT EXISTS idx_assoc_kind_label ON associations(kind, label, record_id);\n CREATE INDEX IF NOT EXISTS idx_assoc_kind_file_id ON associations(kind, file_id, record_id);\n CREATE INDEX IF NOT EXISTS idx_assoc_related ON associations(kind, related_scope, related_ns, related_id, record_id);\n CREATE INDEX IF NOT EXISTS idx_types_base_id ON types(base_id, version);\n\n -- The whole content ordering, in index order, for one field at a time:\n -- a content-sorted page is an ordered walk of a slice of this index.\n CREATE INDEX IF NOT EXISTS idx_content_index_sort\n ON content_index(field, value_rank, num_value, text_key, text_value, record_id);\n CREATE INDEX IF NOT EXISTS idx_content_index_file\n ON content_index(file_id, record_id) WHERE file_id IS NOT NULL;\n`;\n\n/**\n * Bearer-token storage backing StackTokenStore. Kept separate from\n * RECORD_SCHEMA_SQL so an adapter can put it in its own file — the\n * portable stack file shouldn't also carry a server's auth material.\n */\nexport const TOKENS_SCHEMA_SQL = `\n CREATE TABLE IF NOT EXISTS tokens (\n id TEXT PRIMARY KEY,\n token_hash TEXT NOT NULL UNIQUE,\n principal_id TEXT NOT NULL,\n subject_id TEXT NOT NULL,\n label TEXT,\n created_at INTEGER NOT NULL,\n expires_at INTEGER\n ) STRICT;\n`;\n\n/**\n * The full-text index behind `StackQuery.filter.search` (see fts5.ts).\n * What `columnsize=0` costs a reader is in docs/spec/adapters.md\n * § Adapter backends. `detail` stays at its default: sanitizeFts5Query\n * passes phrase queries through, and a phrase needs the token positions\n * only that setting records.\n */\nexport const FTS5_SCHEMA_SQL = `\n CREATE VIRTUAL TABLE IF NOT EXISTS records_fts USING fts5(\n content,\n content='records',\n content_rowid='rowid',\n columnsize=0\n );\n`;\n\n/**\n * Enforces the REFERENCES constraints declared above (off by default per\n * SQLite connection), so touching a nonexistent record fails loudly\n * instead of creating an orphan row. Adapters map the constraint\n * violation to StackNotFoundError at the call sites that can trigger it.\n */\nexport const PRAGMA_FOREIGN_KEYS_ON = `PRAGMA foreign_keys = ON;`;\n\n/**\n * WAL journaling: page-level writes, crash-safe without our own\n * temp-file-and-rename dance, and real SQLite file locking. Only\n * meaningful for a real file (a :memory: database silently ignores it).\n */\nexport const PRAGMA_JOURNAL_MODE_WAL = `PRAGMA journal_mode = WAL;`;\n\nexport type RecordSchemaOptions = {\n /**\n * Whether to put the connection in WAL mode. False for an engine that\n * owns its own durability and rejects the pragma outright, as Durable\n * Object storage does.\n */\n wal: boolean;\n};\n\n/**\n * Brings a records database up to the schema every SQLite record adapter\n * expects: foreign keys on, then the tables and the FTS5 index. Every\n * statement is idempotent, so an engine that cannot tell\n * whether a store exists can run this on each attach.\n */\nexport const applyRecordSchema = (exec: SqlExecutor, opts: RecordSchemaOptions): void => {\n exec.exec(PRAGMA_FOREIGN_KEYS_ON);\n if (opts.wal) exec.exec(PRAGMA_JOURNAL_MODE_WAL);\n exec.exec(RECORD_SCHEMA_SQL);\n exec.exec(FTS5_SCHEMA_SQL);\n};\n","/**\n * Cursor encode/decode for keyset pagination. The payload is JSON in\n * base64 rather than delimited fields: a content sort carries the field's\n * stored text, and neither a field name nor a text value is constrained\n * to avoid whatever character a delimiter would claim. Uses btoa/atob\n * with an explicit UTF-8 step rather than Node's Buffer, so the codec\n * stays runtime-agnostic and a non-Latin-1 value survives the round trip.\n */\n\nimport { NATIVE_SORT_FIELDS, StackBadRequestError } from '@haverstack/core';\nimport { contentSortEntry } from '@haverstack/core/adapter';\nimport type { StackRecord, NativeSortField, QuerySort, ScalarFieldKind } from '@haverstack/core';\n\nexport type SortField = NativeSortField;\n\n/**\n * Re-exported under this package's own name rather than re-spelled: the\n * set a cursor's `native` field is narrowed against is the same set core\n * narrows a wire request's sort field to, and a fourth column added there\n * must not need finding here too.\n */\nexport const SORT_FIELDS: readonly SortField[] = NATIVE_SORT_FIELDS;\n\n/**\n * Which partition of the ordering a cursor sits in: a native column or a\n * content field's number, text, or absent value. The three content cases\n * are the boundary keyset pagination has to hold — a page that ended on\n * the last numeric value resumes at the first text one, not back at the\n * top of either.\n */\nexport type CursorKind = 'native' | 'num' | 'text' | 'absent';\n\nexport type DecodedCursor =\n | { kind: 'native'; field: SortField; value: number; id: string }\n | { kind: 'num'; field: string; value: number; id: string }\n | { kind: 'text'; field: string; value: string; id: string }\n | { kind: 'absent'; field: string; id: string };\n\nconst toBase64 = (text: string): string => {\n const bytes = new TextEncoder().encode(text);\n let binary = '';\n for (const byte of bytes) binary += String.fromCharCode(byte);\n return btoa(binary);\n};\n\nconst fromBase64 = (cursor: string): string => {\n const binary = atob(cursor);\n const bytes = new Uint8Array(binary.length);\n for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);\n return new TextDecoder().decode(bytes);\n};\n\nexport const encodeCursor = (decoded: DecodedCursor): string =>\n toBase64(\n JSON.stringify(\n decoded.kind === 'absent'\n ? { k: decoded.kind, f: decoded.field, i: decoded.id }\n : { k: decoded.kind, f: decoded.field, i: decoded.id, v: decoded.value },\n ),\n );\n\n/** Throws StackBadRequestError for any malformed, corrupt, or unrecognized cursor. */\nexport const decodeCursor = (cursor: string): DecodedCursor => {\n const malformed = () => new StackBadRequestError(`Invalid cursor: malformed \"${cursor}\"`);\n let parsed: unknown;\n try {\n parsed = JSON.parse(fromBase64(cursor));\n } catch {\n throw malformed();\n }\n if (!parsed || typeof parsed !== 'object') throw malformed();\n const { k, f, i, v } = parsed as Record<string, unknown>;\n if (typeof f !== 'string' || !f || typeof i !== 'string') throw malformed();\n\n if (k === 'absent') return { kind: 'absent', field: f, id: i };\n if (k === 'text') {\n if (typeof v !== 'string') throw malformed();\n return { kind: 'text', field: f, value: v, id: i };\n }\n if (k === 'num' || k === 'native') {\n if (typeof v !== 'number' || !isFinite(v)) {\n throw new StackBadRequestError(`Invalid cursor: non-numeric sort value`);\n }\n if (k === 'native') {\n if (!SORT_FIELDS.includes(f as SortField)) {\n throw new StackBadRequestError(`Invalid cursor: unknown sort field \"${f}\"`);\n }\n return { kind: 'native', field: f as SortField, value: v, id: i };\n }\n return { kind: 'num', field: f, value: v, id: i };\n }\n throw malformed();\n};\n\nexport const getSortField = (query: { sort?: QuerySort }): SortField =>\n query.sort?.field ?? 'createdAt';\n\nexport const getSortColumn = (field: SortField): string =>\n field === 'createdAt' ? 'created_at' : field === 'updatedAt' ? 'updated_at' : 'version';\n\n/**\n * The cursor naming the last record of a page: where the next page\n * resumes. A content sort re-derives the record's ordered value from its\n * stored content rather than reading it back out of the index, so the\n * cursor and the index can only ever disagree if the write path is\n * broken — in which case pagination is not the thing to paper over.\n */\nexport const makeCursor = (\n record: StackRecord,\n sort: QuerySort | undefined,\n kindOf?: (record: StackRecord, field: string) => ScalarFieldKind | undefined,\n): string => {\n const contentField = sort?.contentField;\n if (contentField === undefined) {\n const field = getSortField({ sort });\n const value =\n field === 'updatedAt'\n ? record.updatedAt.getTime()\n : field === 'version'\n ? record.version\n : record.createdAt.getTime();\n return encodeCursor({ kind: 'native', field, value, id: record.id });\n }\n\n const kind = kindOf?.(record, contentField);\n const entry = kind ? contentSortEntry(kind, record.content[contentField]) : null;\n if (!entry) return encodeCursor({ kind: 'absent', field: contentField, id: record.id });\n return entry.kind === 'num'\n ? encodeCursor({ kind: 'num', field: contentField, value: entry.num, id: record.id })\n : encodeCursor({ kind: 'text', field: contentField, value: entry.text, id: record.id });\n};\n","/**\n * FTS5 query sanitization and indexing — the full-text search half of the\n * shared record logic.\n *\n * FTS5's query language supports operators (AND/OR/NOT, phrases, NEAR,\n * wildcards) that can be expensive or cause parse errors with untrusted\n * input, so a search string is rewritten before it reaches the engine:\n *\n * kept: AND/OR/NOT (with a left operand), phrase queries (\"…\"), implicit AND\n * removed: wildcards (*), NEAR(...) (terms kept, wrapper dropped), every\n * character outside the allow-list, bare NOT (no left operand),\n * operators with no right operand\n * capped: parenthesis nesting depth (default: 2)\n * closed: an odd trailing quote\n *\n * Two FTS5 grammar details drive the less obvious rules:\n *\n * - FTS5's NEAR is a function-call form — `NEAR(a b, 10)`. Deleting the\n * whole span would also delete the terms inside it, which can leave a\n * dangling operator when NEAR(...) sits next to AND/OR (e.g. \"x AND\n * NEAR(a b, 5)\" -> \"x AND\" — a syntax error). So this keeps the terms\n * and drops only the NEAR(...) wrapper and distance.\n * - FTS5 has real column-filter syntax and errors hard on any name but\n * the table's actual columns. It is wider than `colname:term`: `-name`\n * and `{a b}` filter columns with no colon at all, so an ordinary\n * hyphenated word is read as one. Since records_fts has exactly one\n * column and callers have no business targeting columns explicitly,\n * everything outside a phrase is reduced to an allow-list rather than\n * validated — see OUTSIDE_PHRASE_DISALLOWED.\n *\n * The rewrite is best-effort and makes no completeness claim against\n * FTS5's grammar — which is why the caller wraps execution as well, so a\n * case this misses surfaces as a StackBadRequestError rather than a raw engine\n * error. Search text is what a person typed into a box: an unbalanced\n * quote or a trailing \"AND\" is ordinary input, not a malformed request.\n *\n * What this bounds is the grammar, not the cost: a syntactically modest\n * search over a large index can still be expensive, and node:sqlite blocks\n * the calling thread, so there is no timeout to set from inside the call.\n * Bounding execution time belongs to whoever drives the engine under load\n * — see docs/spec/wire-format.md § Bounding query cost for the server-side\n * expectation and the `timeout` wire error that goes with it.\n */\n\nimport type { SqlExecutor } from './executor.js';\n\n/**\n * Outside a phrase, everything but a letter, digit, mark, underscore,\n * whitespace, paren or quote becomes a separator.\n *\n * An allow-list rather than a list of metacharacters to strip, because\n * FTS5's column-filter syntax is wider than it looks and an enumeration\n * kept missing parts of it: `-name` and `{a b}` filter columns with no\n * colon in sight, so `-cats` and even the hyphen inside `cats-dogs` were\n * read as column names and answered \"no such column\". `+` is a bare\n * syntax error. Listing what may pass means the next piece of syntax\n * nobody here anticipated arrives as a separator instead of as an error.\n *\n * Nothing is lost by it: FTS5's default tokenizer already splits terms on\n * these characters, so `cats-dogs` searches for the same two tokens it was\n * indexed as.\n */\nconst OUTSIDE_PHRASE_DISALLOWED = /[^\\p{L}\\p{N}\\p{M}_\\s()\"]/gu;\n\n/**\n * Collapse every whitespace run to a single space.\n *\n * Every rule below that reasons about what sits beside an operator or a\n * paren spells \"beside\" as `\\s*`, and a `\\s*` with a long run to chew on\n * re-tries the whole run once per starting position — quadratic in the\n * run, on text a caller types into a box. Keeping runs collapsed bounds\n * what any of them can backtrack over to a single character. What it does\n * not buy is linearity overall: the rewrite still costs a little more\n * than proportionally as the text grows, which is why bounding a search's\n * length belongs to whoever accepts it.\n *\n * Nothing is lost by it: FTS5's tokenizer splits on whitespace, so a\n * collapsed run tokenizes exactly as the original did — inside a phrase\n * as well as outside, since a phrase matches a sequence of tokens rather\n * than the spacing between them.\n */\nconst collapseWhitespace = (query: string): string => query.replace(/\\s+/g, ' ');\n\nexport const sanitizeFts5Query = (query: string, maxDepth = 2): string => {\n if (!query) return '';\n\n // Drop control characters, inside a phrase as well as outside. A NUL in\n // particular ends the string early for SQLite's C API, so the engine sees\n // an opening quote whose closing one is past the truncation and reports an\n // unterminated string; none of them can match an indexed token anyway.\n let clean = query.replace(/[\\p{Cc}\\p{Cf}]/gu, ' ');\n\n clean = clean.replace(/\\*/g, '');\n\n // Replace NEAR(terms, distance) with just its terms — see note above.\n //\n // Two things here are about what the engine does when the closing paren\n // never arrives, on text a caller types into a box.\n //\n // No `\\s*` after the paren, though the terms it captures may open with\n // whitespace: `[^,()]*` already matches a space, so a second quantifier\n // that also does gives the engine an ambiguous split to backtrack\n // through. The captured space is collapsed with all the rest on the way\n // out.\n //\n // And neither run crosses a `(`, which bounds where a failed match gives\n // up: without it, `NEAR(NEAR(NEAR(` re-scans to the end of the string\n // once per occurrence. A NEAR's terms are words and phrases and its\n // distance a number, so neither holds a paren FTS5 would accept anyway.\n clean = clean.replace(/\\bNEAR\\s*\\(([^,()]*)(?:,[^()]*)?\\)/gi, '$1');\n\n // A NEAR( with no closing paren isn't the function-call form the rule\n // above rewrites, and the paren auto-close below would hand FTS5 a NEAR\n // call it never meant to write. Keep the grouping, drop the keyword.\n clean = clean.replace(/\\bNEAR\\s*\\(/gi, '(');\n\n // Reduce to the characters that carry meaning outside a phrase — see note\n // above. Everything else becomes a separator, which is what FTS5's own\n // tokenizer would do with it anyway.\n clean = clean.replace(/\"[^\"]*\"|[^\"]+/g, (span) =>\n span.startsWith('\"') ? span : span.replace(OUTSIDE_PHRASE_DISALLOWED, ' '),\n );\n\n // The reduction above turns every disallowed character into a space, so\n // this is where the long runs come from.\n clean = collapseWhitespace(clean);\n\n // Strip bare NOT with no left operand — FTS5 requires \"term NOT term\", not \"NOT term\"\n clean = clean.replace(/(?:^|\\(\\s*)NOT\\s+/gi, (m) => m.replace(/NOT\\s+/i, ''));\n\n // Enforce max nesting depth; replace excess ( with a space and discard\n // unmatched ). Parens inside a phrase are literal text to FTS5, so the\n // depth count steps over quoted spans rather than reading them as syntax.\n let currentDepth = 0;\n let inPhrase = false;\n let result = '';\n for (const char of clean) {\n if (char === '\"') {\n inPhrase = !inPhrase;\n result += char;\n } else if (inPhrase) {\n result += char;\n } else if (char === '(') {\n if (currentDepth < maxDepth) {\n currentDepth++;\n result += char;\n } else result += ' ';\n } else if (char === ')') {\n if (currentDepth > 0) {\n currentDepth--;\n result += char;\n } else result += ' ';\n } else {\n result += char;\n }\n }\n\n // Close an odd trailing quote before the parens, so the added ) lands\n // outside the phrase rather than becoming a literal character in it.\n if (inPhrase) result += '\"';\n\n if (currentDepth > 0) result += ')'.repeat(currentDepth);\n\n // Remove empty paren pairs left behind by NEAR/NOT stripping, and drop\n // operators left without an operand. Looped together because each can\n // create work for the other: removing an operator can empty a paren pair\n // (\"(a AND)\" -> \"(a)\" -> ...), and removing a pair can strand the\n // operator that pointed at it (\"x AND ()\" -> \"x AND\").\n let prev: string;\n do {\n prev = result;\n // Empty phrases and empty paren pairs, each of which matches nothing,\n // in one rule so that a run of them collapses to a single space\n // rather than to one space apiece — a run of spaces is what the rules\n // below would have to backtrack over. An empty phrase is also the one\n // construct whose quotes can re-pair differently once a rule inserts\n // text beside it, which is why this repeats to a fixpoint.\n result = result.replace(/(?:\"\"|\\(\\s*\\))+/g, ' ');\n result = dropDanglingOperators(result);\n result = restoreImplicitAnd(result);\n } while (result !== prev);\n\n return collapseWhitespace(result).replace(/\\(\\s+/g, '(').replace(/\\s+\\)/g, ')').trim();\n};\n\n/** An operator FTS5 requires a term on both sides of. */\nconst OPERATOR = String.raw`(?:AND|OR|NOT)`;\n\n/**\n * Stands in for a phrase while operators are rewritten. A private-use\n * character is not a word character, so `\\b` still sees the operator\n * beside it, while `^`, `$` and the paren anchors see an operand — which\n * is what a phrase is. Nothing reaches here holding one: `clean` is\n * rewritten from the caller's string, and a literal U+E000 in it would be\n * a term character FTS5 tokenizes away regardless.\n */\nconst PHRASE_HOLE = '\\u{E000}';\n\n/**\n * Drop operators left without an operand — the shape a person types on\n * their way to a longer query (\"cats AND\") or lands on once the rules\n * above have removed a term. FTS5 rejects each of them outright, so what\n * would otherwise be a syntax error becomes the search for the terms that\n * are actually there.\n *\n * Phrases are masked out first: `\"cats AND dogs\"` is literal text to FTS5,\n * and rewriting inside it would change what the user asked for.\n */\nconst dropDanglingOperators = (query: string): string =>\n withPhrasesMasked(query, (masked) => {\n let out = masked;\n let prev: string;\n do {\n prev = out;\n // A run of operators keeps only the first — the rest have no left operand.\n out = out.replace(\n new RegExp(String.raw`\\b(${OPERATOR})\\b(?:\\s+\\b${OPERATOR}\\b)+`, 'gi'),\n '$1',\n );\n // No left operand: at the start of the query or straight after \"(\".\n out = out.replace(new RegExp(String.raw`(^|\\()\\s*\\b${OPERATOR}\\b\\s*`, 'gi'), '$1');\n // No right operand: at the end of the query or straight before \")\".\n // No leading `\\s*` — whitespace before the operator is left for the\n // collapse on the way out, and a `\\s*` here would re-scan a run of\n // spaces once per position in it before failing to find an operator.\n out = out.replace(new RegExp(String.raw`\\b${OPERATOR}\\b\\s*($|\\))`, 'gi'), '$1');\n } while (out !== prev);\n return out;\n });\n\n/**\n * Run `rewrite` over the query with each phrase swapped for PHRASE_HOLE,\n * then put the phrases back in order. Every rule that reasons about what\n * sits beside an operator or a paren needs this: inside a phrase the text\n * is literal, and from outside one the phrase is a single operand.\n */\nconst withPhrasesMasked = (query: string, rewrite: (masked: string) => string): string => {\n const phrases: string[] = [];\n const masked = query.replace(/\"[^\"]*\"/g, (phrase) => {\n phrases.push(phrase);\n return PHRASE_HOLE;\n });\n let i = 0;\n return rewrite(masked).replace(new RegExp(PHRASE_HOLE, 'g'), () => phrases[i++]);\n};\n\n/**\n * Put back the operator FTS5 requires around a group. Two terms side by\n * side are an implicit AND (`cats dogs`), but a group is not: `cats (dogs)`\n * is a syntax error where `cats AND (dogs)` is fine. Grouping survives a\n * rewrite that strips the operator beside it, so this\n * names the AND that was always meant rather than dropping the parens.\n *\n * Both rules test a *whole* word against the operator list. Matching a\n * prefix instead reads the `D` of a preceding `AND` as a term and inserts\n * a second one beside it.\n *\n * Phrases are masked for the same reason as elsewhere — a phrase is an\n * operand, so `\"a b\" (c)` needs the operator too.\n */\nconst OPERAND_CHAR = String.raw`[\\p{L}\\p{N}_${PHRASE_HOLE}]`;\n\nconst isOperator = (word: string): boolean => /^(?:AND|OR|NOT)$/i.test(word);\n\nconst restoreImplicitAnd = (query: string): string =>\n withPhrasesMasked(query, (masked) =>\n masked\n // \")\" then an operand — \") cats\", \") (\", \") <phrase>\" — unless that\n // operand is an operator, which already joins the two sides.\n .replace(\n new RegExp(String.raw`\\)\\s*(?=${OPERAND_CHAR}|\\()`, 'gu'),\n (match, offset: number, whole: string) => {\n const next = /^\\s*([\\p{L}\\p{N}_]+)/u.exec(whole.slice(offset + match.length));\n return next && isOperator(next[1]) ? match : ') AND ';\n },\n )\n // An operand then \"(\", unless the operand is an operator.\n // The lookbehind pins the match to the start of the operand run.\n // Without it every position inside a run is a start position, and\n // each one re-scans the rest of the run before the lookahead\n // rejects it — quadratic in a long word before a paren.\n .replace(\n new RegExp(String.raw`(?<!${OPERAND_CHAR})(${OPERAND_CHAR}+)\\s*(?=\\()`, 'gu'),\n (match, word: string) => (isOperator(word) ? match : `${word} AND `),\n ),\n );\n\n// -------------------------------------------------------\n// Indexing strategy\n// -------------------------------------------------------\n\n/**\n * FTS5's external-content table needs its special `('delete', rowid,\n * content)` command — with the *old* content — to unindex a row; a plain\n * DELETE leaves stale, still-searchable entries. remove() therefore reads\n * the current rowid/content and must run before the caller's change.\n */\nexport const fts5Strategy = {\n /** Index a record that has no FTS entry yet (used right after an INSERT). */\n insert(exec: SqlExecutor, recordId: string, content: string): void {\n exec.run(`INSERT INTO records_fts(rowid, content) SELECT rowid, ? FROM records WHERE id = ?`, [\n content,\n recordId,\n ]);\n },\n /**\n * Remove a record's FTS entry. MUST be called before the records row's\n * content changes or the row is deleted — see the note above.\n */\n remove(exec: SqlExecutor, recordId: string): void {\n const row = exec.get<{ rowid: number; content: string }>(\n 'SELECT rowid, content FROM records WHERE id = ?',\n [recordId],\n );\n if (!row) return;\n exec.run(`INSERT INTO records_fts(records_fts, rowid, content) VALUES('delete', ?, ?)`, [\n row.rowid,\n row.content,\n ]);\n },\n};\n","/**\n * StackQuery -> SQL. Engine-independent — shared verbatim between\n * SQLite-backed record adapters. The one caveat is full-text search:\n * `f.search` here assumes a `records_fts` virtual table with a MATCH-able\n * `content` column (see fts5.ts).\n *\n * A query compiles to a plan rather than to a single statement, because a\n * content-field sort is two orderings end to end: the records that hold a\n * value at the field, ordered by it, then the records that hold none,\n * ordered by id. Reading them as two statements lets each one walk an\n * index in order and stop at the page boundary; the LEFT JOIN that would\n * express the same result in one statement can only be sorted after the\n * fact, which costs a full pass over the table for every page.\n */\n\nimport { StackBadRequestError, type StackQuery } from '@haverstack/core';\nimport { contentSortKey, parseContentFilterKey } from '@haverstack/core/adapter';\nimport {\n decodeCursor,\n getSortColumn,\n getSortField,\n type DecodedCursor,\n type SortField,\n} from './cursor.js';\nimport { sanitizeFts5Query } from './fts5.js';\n\n/** One statement plus the parameters it binds, in textual order. */\nexport type QueryStatement = { sql: string; params: unknown[] };\n\n/**\n * A page statement with the row budget its trailing `LIMIT ?` binds.\n * buildQueryPlan leaves that parameter off, so the budget is never one of\n * the parameters the query itself bound: the second partition of a\n * content sort asks for what the first left of the page rather than for a\n * page it then discards.\n */\nexport const atBudget = (page: QueryStatement, rows: number): QueryStatement => ({\n sql: page.sql,\n params: [...page.params, rows],\n});\n\n/**\n * Reduce `sort.direction` to a keyword this module may interpolate into\n * SQL. Core's assertValidSort() already rejects anything else at the\n * invariant layer, but this builder writes the value straight into\n * `ORDER BY` and the cursor comparison, so it re-checks rather than trust\n * an upstream that a future refactor could bypass: an unvalidated direction\n * here is a SQL-injection sink. A sort without a direction is refused: core\n * resolves the default before any adapter sees the query.\n */\nconst sqlDirection = (query: StackQuery): 'ASC' | 'DESC' => {\n // No sort at all is the unsorted default; a sort always carries its direction.\n const dir = query.sort ? query.sort.direction : 'desc';\n if (dir !== 'asc' && dir !== 'desc') {\n throw new StackBadRequestError(`Invalid sort direction \"${dir}\": expected \"asc\" or \"desc\".`);\n }\n return dir === 'asc' ? 'ASC' : 'DESC';\n};\n\n/**\n * Normalize a json_each row's value so one array element and one bare\n * value are walked by the same clause: an array stands for its elements,\n * anything else for itself. See docs/spec/data-model.md § Nested content\n * paths.\n */\nconst spread = (alias: string): string =>\n `CASE WHEN ${alias}.type = 'array' THEN ${alias}.value ` +\n `WHEN ${alias}.type = 'object' THEN json_array(json(${alias}.value)) ` +\n `ELSE json_array(${alias}.value) END`;\n\n/**\n * The members of a json_each row that is an object, and none for a row\n * that is not — a path descending through a scalar reaches no value rather\n * than reaching an error.\n */\nconst members = (alias: string): string =>\n `CASE WHEN ${alias}.type = 'object' THEN ${alias}.value ELSE '{}' END`;\n\n/**\n * True when some value reached by `segments` satisfies `leaf`, applied to\n * the alias holding that value. Each segment is a bound parameter matched\n * against json_each's `key`, never interpolated into a path expression, so\n * no key can be read as syntax. See docs/spec/data-model.md § Filter.\n */\nconst contentPathExists = (\n segments: string[],\n leaf: (alias: string) => string,\n params: unknown[],\n nextAlias: () => string,\n): string => {\n // The walk is a flat join list, not a subquery per segment: both SQLite\n // builds cap expression-tree depth far below the segment cap, and\n // nesting spends that budget while a join list spends the join budget\n // the cap is actually sized against.\n let key = nextAlias();\n let value = nextAlias();\n const from = [`json_each(r.content) AS ${key}`, `json_each(${spread(key)}) AS ${value}`];\n const where = [`${key}.key = ?`];\n params.push(segments[0]);\n\n for (const segment of segments.slice(1)) {\n key = nextAlias();\n from.push(`json_each(${members(value)}) AS ${key}`);\n value = nextAlias();\n from.push(`json_each(${spread(key)}) AS ${value}`);\n where.push(`${key}.key = ?`);\n params.push(segment);\n }\n\n where.push(leaf(value));\n return `EXISTS (SELECT 1 FROM ${from.join(', ')} WHERE ${where.join(' AND ')})`;\n};\n\n/**\n * A scalar filter value never matches an object or array stored at the\n * path: json_each exposes those as their JSON text, which would compare\n * equal to a string spelling the same document while every non-SQL adapter\n * compares the value itself.\n */\nconst equalsLeaf = (alias: string): string =>\n `${alias}.type NOT IN ('object', 'array') AND ${alias}.value = ?`;\n\n/** The join alias the content index is read through. */\nconst SORT_ALIAS = 'cs';\n\n/**\n * A cursor names a position in one ordering; carrying it into another\n * would silently resume somewhere arbitrary. The partition matters as\n * much as the field name — a cursor from a numeric page can't be read\n * against text values — so each ordering below accepts only the cursors\n * it can read, and rejecting the rest is what narrows the cursor to them.\n */\nconst cursorMismatch = (cursor: DecodedCursor, field: string): StackBadRequestError =>\n new StackBadRequestError(\n `Cursor sort field \"${cursor.field}\" does not match query sort field \"${field}\"`,\n );\n\nconst nativeCursor = (cursor: DecodedCursor, field: SortField) => {\n if (cursor.kind !== 'native' || cursor.field !== field) throw cursorMismatch(cursor, field);\n return cursor;\n};\n\nconst contentCursor = (cursor: DecodedCursor, field: string) => {\n if (cursor.kind === 'native' || cursor.field !== field) throw cursorMismatch(cursor, field);\n return cursor;\n};\n\n/**\n * Every condition on the records row itself — everything a query asks\n * except where in the ordering to resume, which is per-partition and gets\n * appended by the caller.\n */\nconst recordConditions = (query: StackQuery): { conditions: string[]; params: unknown[] } => {\n const conditions: string[] = [\"r.id != '_config'\"];\n const params: unknown[] = [];\n const f = query.filter ?? {};\n\n /**\n * A filter that accepts one value or a list of them, as the `IN` that\n * covers both. Written even for a single value rather than as `= ?`,\n * since SQLite plans a one-element IN identically and one form means one\n * place for the filter's placeholder count and its bindings to agree.\n */\n const inFilter = (column: string, value: string | readonly string[] | undefined): void => {\n if (value === undefined) return;\n const values = Array.isArray(value) ? value : [value as string];\n conditions.push(`r.${column} IN (${values.map(() => '?').join(',')})`);\n params.push(...values);\n };\n\n /** One side of a date range, as the open-ended comparison it is. */\n const rangeFilter = (\n column: string,\n range: { after?: Date; before?: Date } | undefined,\n ): void => {\n if (range?.after) {\n conditions.push(`r.${column} > ?`);\n params.push(range.after.getTime());\n }\n if (range?.before) {\n conditions.push(`r.${column} < ?`);\n params.push(range.before.getTime());\n }\n };\n\n if (!f.includeDeleted) {\n conditions.push('r.deleted_at IS NULL');\n }\n\n if (!f.includeUnlisted) {\n conditions.push('r.unlisted_at IS NULL');\n }\n\n inFilter('type_id', f.typeId);\n\n // Not an inFilter: parentId takes an explicit null for \"at the root\",\n // which is a different SQL predicate rather than a different value.\n if (f.parentId !== undefined) {\n if (f.parentId === null) {\n conditions.push('r.parent_id IS NULL');\n } else {\n conditions.push('r.parent_id = ?');\n params.push(f.parentId);\n }\n }\n\n inFilter('app_id', f.appId);\n inFilter('entity_id', f.createdBy?.subjectId);\n inFilter('principal_id', f.createdBy?.principalId);\n\n rangeFilter('created_at', f.createdAt);\n rangeFilter('updated_at', f.updatedAt);\n\n // Every association filter below is a semi-join rather than a correlated\n // EXISTS, so the planner drives from the association side — reading the\n // matching record ids straight out of idx_assoc_kind_label /\n // idx_assoc_kind_file_id / idx_assoc_related / idx_content_index_file,\n // each of which ends with record_id and so needs no table read at all —\n // instead of scanning every record and probing for each. The work is\n // proportional to how many records match, not to how many the stack\n // holds.\n\n /**\n * The semi-join itself. `kind` is one of this module's own literals, never\n * caller text, and each clause carries its own placeholder — so the only\n * thing interpolated here is SQL this file wrote.\n */\n const hasAssociation = (kind: 'tag' | 'attachment' | 'relationship', clauses: string[]): string =>\n `r.id IN (SELECT a.record_id FROM associations a WHERE a.kind = '${kind}'` +\n clauses.map((c) => ` AND ${c}`).join('') +\n `)`;\n\n // Conjunctive: a record must carry every tag named.\n if (f.tags?.length) {\n for (const tag of f.tags) {\n conditions.push(hasAssociation('tag', ['a.label = ?']));\n params.push(tag);\n }\n }\n\n // Both halves in one subquery, so they match the same association.\n if (f.attachment) {\n const clauses: string[] = [];\n if (f.attachment.label !== undefined) {\n clauses.push('a.label = ?');\n params.push(f.attachment.label);\n }\n if (f.attachment.fileId !== undefined) {\n clauses.push('a.file_id = ?');\n params.push(f.attachment.fileId);\n }\n conditions.push(hasAssociation('attachment', clauses));\n }\n\n // A reference is an attachment association or a top-level file-ref field.\n if (f.referencesFileId) {\n conditions.push(\n `(${hasAssociation('attachment', ['a.file_id = ?'])}\n OR r.id IN (SELECT ci.record_id FROM content_index ci WHERE ci.file_id = ?))`,\n );\n params.push(f.referencesFileId, f.referencesFileId);\n }\n\n // Relationship filter — each clause is an optional pattern, so a bare\n // label matches every target under it and an external target with no\n // `id` matches its whole namespace (docs/spec/data-model.md § Filter).\n //\n // A record or entity target always stores '' in related_ns (see\n // associationKeyColumns). Saying so turns idx_assoc_related's\n // (related_scope, related_ns, related_id) prefix into a contiguous\n // equality run for those kinds instead of a seek on related_scope alone.\n if (f.relatedTo) {\n const clauses: string[] = [];\n const target = f.relatedTo.target;\n if (f.relatedTo.label !== undefined) {\n clauses.push('a.label = ?');\n params.push(f.relatedTo.label);\n }\n if (target) {\n clauses.push('a.related_scope = ?');\n params.push(target.kind);\n if (target.kind === 'record') {\n clauses.push('a.related_ns = ?', 'a.related_id = ?', 'a.related_stack = ?');\n params.push('', target.recordId, target.stackUrl ?? '');\n } else if (target.kind === 'entity') {\n clauses.push('a.related_ns = ?', 'a.related_id = ?');\n params.push('', target.entityId);\n } else {\n clauses.push('a.related_ns = ?');\n params.push(target.ns);\n if (target.id !== undefined) {\n clauses.push('a.related_id = ?');\n params.push(target.id);\n }\n }\n }\n conditions.push(hasAssociation('relationship', clauses));\n }\n\n // Content field filters — a dot-separated path, matched element-wise\n // through arrays. A `null` value means \"no value at the path, or a value\n // that is null\" (docs/spec/data-model.md § Filter), which is the second\n // arm below: \"some value there is null\" OR \"nothing is there at all\".\n if (f.content) {\n let aliasSeq = 0;\n const nextAlias = () => `cp${aliasSeq++}`;\n for (const [key, value] of Object.entries(f.content)) {\n const segments = parseContentFilterKey(key);\n // undefined is the same absence null names — JSON drops it on the\n // way over the wire, so treating it as a value would make an\n // in-process query and its wire equivalent disagree.\n if (value === null || value === undefined) {\n const isNull = contentPathExists(segments, (a) => `${a}.value IS NULL`, params, nextAlias);\n const anyValue = contentPathExists(\n segments,\n (a) => `${a}.value IS NOT NULL`,\n params,\n nextAlias,\n );\n conditions.push(`(${isNull} OR NOT ${anyValue})`);\n } else {\n const match = contentPathExists(segments, equalsLeaf, params, nextAlias);\n params.push(value);\n conditions.push(match);\n }\n }\n }\n\n // Presence — the question an exact-match value cannot ask. Element-wise\n // like the content filter above: a path holds a value when at least one\n // non-null value is reachable at it. See docs/spec/data-model.md\n // § Filter.\n if (f.contentPresent?.length) {\n let aliasSeq = 0;\n const nextAlias = () => `pp${aliasSeq++}`;\n for (const key of f.contentPresent) {\n conditions.push(\n contentPathExists(\n parseContentFilterKey(key),\n (a) => `${a}.value IS NOT NULL`,\n params,\n nextAlias,\n ),\n );\n }\n }\n\n if (f.search) {\n const sanitized = sanitizeFts5Query(f.search);\n if (sanitized) {\n conditions.push(`r.rowid IN (SELECT rowid FROM records_fts WHERE records_fts MATCH ?)`);\n params.push(sanitized);\n } else {\n // A search that sanitizes to nothing (e.g. \"*\", punctuation-only) is\n // an honest zero-match result, not \"no filter\" — omitting the clause\n // would silently return the full table as the \"search result\".\n conditions.push('0');\n }\n }\n\n return { conditions, params };\n};\n\n/**\n * Everything ordered after the cursor's record within the partition that\n * holds a value: a later value rank, or the same rank past the cursor's\n * value and id. `op` runs the comparison the same way the ORDER BY does,\n * so one form covers both directions — in the direction where the\n * cursor's rank is the last one, the leading disjunct is simply never\n * true.\n */\nconst presentCursorCondition = (\n cursor: Extract<DecodedCursor, { kind: 'num' | 'text' }>,\n op: '>' | '<',\n params: unknown[],\n): string => {\n if (cursor.kind === 'num') {\n params.push(cursor.value, cursor.value, cursor.id);\n return (\n `(${SORT_ALIAS}.value_rank ${op} 0 OR (${SORT_ALIAS}.value_rank = 0 AND ` +\n `(${SORT_ALIAS}.num_value ${op} ? OR (${SORT_ALIAS}.num_value = ? AND ` +\n `${SORT_ALIAS}.record_id ${op} ?))))`\n );\n }\n const key = contentSortKey(cursor.value);\n params.push(key, key, cursor.value, cursor.value, cursor.id);\n return (\n `(${SORT_ALIAS}.value_rank ${op} 1 OR (${SORT_ALIAS}.value_rank = 1 AND ` +\n `(${SORT_ALIAS}.text_key ${op} ? OR (${SORT_ALIAS}.text_key = ? AND ` +\n `(${SORT_ALIAS}.text_value ${op} ? OR (${SORT_ALIAS}.text_value = ? AND ` +\n `${SORT_ALIAS}.record_id ${op} ?))))))`\n );\n};\n\nconst statement = (\n select: string,\n from: string,\n conditions: string[],\n params: unknown[],\n tail = '',\n): QueryStatement => ({\n sql: `SELECT ${select} FROM ${from} WHERE ${conditions.join(' AND ')}${tail}`,\n params,\n});\n\n/**\n * Compile a query into the statements that answer it, read in order and\n * their rows concatenated: the first that fills the page ends the read,\n * so the second is never run for a page that doesn't reach it. Each still\n * needs the row budget its `LIMIT ?` binds — see atBudget.\n */\nexport const buildQueryPlan = (query: StackQuery): QueryStatement[] => {\n const dir = sqlDirection(query);\n const op = dir === 'ASC' ? '>' : '<';\n const decoded = query.cursor ? decodeCursor(query.cursor) : null;\n const contentField = query.sort?.contentField;\n\n if (contentField === undefined) {\n const { conditions, params } = recordConditions(query);\n const col = getSortColumn(getSortField(query));\n if (decoded) {\n const cursor = nativeCursor(decoded, getSortField(query));\n conditions.push(`(r.${col} ${op} ? OR (r.${col} = ? AND r.id ${op} ?))`);\n params.push(cursor.value, cursor.value, cursor.id);\n }\n return [\n statement(\n 'r.*',\n 'records r',\n conditions,\n params,\n ` ORDER BY r.${col} ${dir}, r.id ${dir} LIMIT ?`,\n ),\n ];\n }\n\n const cursor = decoded ? contentCursor(decoded, contentField) : null;\n const pages: QueryStatement[] = [];\n\n // Records holding a value at the field, in that value's order. A cursor\n // sitting in the absent partition has already passed every one of them.\n if (cursor?.kind !== 'absent') {\n const { conditions, params } = recordConditions(query);\n const from = `content_index ${SORT_ALIAS} JOIN records r ON r.id = ${SORT_ALIAS}.record_id`;\n conditions.push(`${SORT_ALIAS}.field = ?`);\n params.push(contentField);\n if (cursor) conditions.push(presentCursorCondition(cursor, op, params));\n // Ordered exactly as idx_content_index_sort stores it, so the page is\n // an ordered walk of the index rather than a sort of the matches.\n const order =\n `${SORT_ALIAS}.value_rank ${dir}, ${SORT_ALIAS}.num_value ${dir}, ` +\n `${SORT_ALIAS}.text_key ${dir}, ${SORT_ALIAS}.text_value ${dir}, ` +\n `${SORT_ALIAS}.record_id ${dir}`;\n pages.push(statement('r.*', from, conditions, params, ` ORDER BY ${order} LIMIT ?`));\n }\n\n // Records holding nothing there, which trail the others whichever way\n // the sort runs (docs/spec/data-model.md § Sorting by a content field).\n {\n const { conditions, params } = recordConditions(query);\n conditions.push(\n `NOT EXISTS (SELECT 1 FROM content_index ${SORT_ALIAS} ` +\n `WHERE ${SORT_ALIAS}.record_id = r.id AND ${SORT_ALIAS}.field = ?)`,\n );\n params.push(contentField);\n if (cursor?.kind === 'absent') {\n conditions.push(`r.id ${op} ?`);\n params.push(cursor.id);\n }\n pages.push(statement('r.*', 'records r', conditions, params, ` ORDER BY r.id ${dir} LIMIT ?`));\n }\n\n return pages;\n};\n","/**\n * Row <-> domain object mappers shared by every SQLite-backed record\n * adapter. Column names and JSON-encoding choices are the storage\n * contract; keeping one copy means the adapters can't drift on them.\n */\n\nimport type {\n Actor,\n AuthorityAssociation,\n DataAssociation,\n GroupRole,\n Grantee,\n RecordJournalEntry,\n StackRecord,\n StackType,\n RecordVersion,\n Association,\n RelationshipTarget,\n} from '@haverstack/core';\n\nexport const toMs = (d: Date): number => d.getTime();\n\n/**\n * An Actor from its two columns. A principal with no subject beside it\n * names nobody to attribute to, so it reads as absent like the subject.\n */\nconst actorFromColumns = (subject: unknown, principal: unknown): Actor | undefined =>\n subject == null\n ? undefined\n : {\n subjectId: subject as string,\n ...(principal != null && { principalId: principal as string }),\n };\nexport const fromMs = (ms: number): Date => new Date(ms);\n\n/**\n * A record's stored edges, split into the two fields it presents.\n * Partitioned by kind rather than stored apart: one table, one delta, one\n * durability tier, and an app editing tags never sees the authority half.\n * See docs/spec/access-control.md § Record-level permissions.\n */\nexport const partitionAssociations = (\n associations: Association[],\n): { associations: DataAssociation[]; permissions: AuthorityAssociation[] } => ({\n associations: associations.filter(\n (a): a is DataAssociation => a.kind !== 'permission' && a.kind !== 'anyone',\n ),\n permissions: associations.filter(\n (a): a is AuthorityAssociation => a.kind === 'permission' || a.kind === 'anyone',\n ),\n});\n\nexport const rowToRecord = (\n row: Record<string, unknown>,\n associations: Association[],\n): StackRecord => {\n const record: StackRecord = {\n id: row.id as string,\n typeId: row.type_id as string,\n createdAt: fromMs(row.created_at as number),\n updatedAt: fromMs(row.updated_at as number),\n content: JSON.parse(row.content as string),\n version: row.version as number,\n };\n // Presence, never truthiness: SQL NULL is the only spelling of an absent\n // field, so the empty string and the epoch are values like any other.\n // The query predicates beside this read `IS NULL`, and a mapper that\n // disagreed would let a record answer one way to get() and another to\n // the filter that should have found it.\n if (row.parent_id != null) record.parentId = row.parent_id as string;\n if (row.app_id != null) record.appId = row.app_id as string;\n const createdBy = actorFromColumns(row.entity_id, row.principal_id);\n if (createdBy) record.createdBy = createdBy;\n const updatedBy = actorFromColumns(row.updated_by, row.updated_via);\n if (updatedBy) record.updatedBy = updatedBy;\n if (row.deleted_at != null) record.deletedAt = fromMs(row.deleted_at as number);\n if (row.unlisted_at != null) record.unlistedAt = fromMs(row.unlisted_at as number);\n const partitioned = partitionAssociations(associations);\n if (partitioned.associations.length) record.associations = partitioned.associations;\n if (partitioned.permissions.length) record.permissions = partitioned.permissions;\n return record;\n};\n\nexport const rowToAssociation = (row: Record<string, unknown>): Association => {\n if (row.kind === 'tag') {\n return { kind: 'tag', label: row.label as string };\n }\n if (row.kind === 'anyone') {\n return { kind: 'anyone', label: 'read' };\n }\n if (row.kind === 'permission') {\n return {\n kind: 'permission',\n label: row.label as 'read' | 'write',\n grantee: rowToGrantee(row),\n };\n }\n if (row.kind === 'attachment') {\n const attachmentRecordId = row.attachment_record_id as string;\n return {\n kind: 'attachment',\n label: row.label as string,\n fileId: row.file_id as string,\n // Stored as '' when absent, like every other optional column here —\n // the field is omitted rather than served empty, so a round trip\n // produces the association the caller wrote.\n ...(attachmentRecordId && { attachmentRecordId }),\n };\n }\n return {\n kind: 'relationship',\n label: row.label as string,\n target: rowToTarget(row),\n };\n};\n\n/**\n * The six columns that identify an association, in the order every\n * INSERT and DELETE below binds them. One helper because the two must\n * agree exactly — a dissociate that bound them differently would delete\n * nothing and report success.\n */\nexport const associationKeyColumns = (\n a: Association,\n): [string, string, string, string, string, string] => {\n if (a.kind === 'attachment') return [a.fileId, '', '', '', '', ''];\n if (a.kind === 'permission') {\n const g = a.grantee;\n return g.kind === 'entity'\n ? ['', 'entity', g.entityId, '', '', '']\n : ['', 'group', g.groupId, '', '', g.role];\n }\n if (a.kind !== 'relationship') return ['', '', '', '', '', ''];\n const t = a.target;\n if (t.kind === 'entity') return ['', 'entity', t.entityId, '', '', ''];\n if (t.kind === 'external') return ['', 'external', t.id, t.ns, '', ''];\n return ['', 'record', t.recordId, '', t.stackUrl ?? '', ''];\n};\n\n/**\n * A permission's grantee, read back off the columns a relationship target\n * otherwise uses. `related_role` is what tells the two group sets apart —\n * `member` is the wider one. See docs/spec/access-control.md\n * § Record-level permissions.\n */\nconst rowToGrantee = (row: Record<string, unknown>): Grantee =>\n row.related_scope === 'group'\n ? {\n kind: 'group',\n groupId: row.related_id as string,\n role: row.related_role as GroupRole,\n }\n : { kind: 'entity', entityId: row.related_id as string };\n\nconst rowToTarget = (row: Record<string, unknown>): RelationshipTarget => {\n const id = row.related_id as string;\n if (row.related_scope === 'entity') return { kind: 'entity', entityId: id };\n if (row.related_scope === 'external') {\n return { kind: 'external', ns: row.related_ns as string, id };\n }\n const stackUrl = row.related_stack as string;\n return { kind: 'record', recordId: id, ...(stackUrl && { stackUrl }) };\n};\n\nexport const rowToType = (row: Record<string, unknown>): StackType => {\n const t: StackType = {\n id: row.id as string,\n baseId: row.base_id as string,\n version: row.version as number,\n name: row.name as string,\n schema: JSON.parse(row.schema as string),\n schemaHash: row.schema_hash as string,\n createdAt: fromMs(row.created_at as number),\n };\n if (row.migrates_from) t.migratesFrom = row.migrates_from as string;\n return t;\n};\n\nexport const rowToVersion = (row: Record<string, unknown>): RecordVersion => {\n const v: RecordVersion = {\n version: row.version as number,\n typeId: row.type_id as string,\n content: JSON.parse(row.content as string),\n updatedAt: fromMs(row.updated_at as number),\n };\n const createdBy = actorFromColumns(row.entity_id, row.principal_id);\n if (createdBy) v.createdBy = createdBy;\n const updatedBy = actorFromColumns(row.updated_by, row.updated_via);\n if (updatedBy) v.updatedBy = updatedBy;\n return v;\n};\n\n/**\n * One journal row. `previous_parent_id` carries the three-way spelling the\n * column comment states: SQL NULL is an entry naming no origin, '' is the\n * root, anything else is a container id.\n */\nexport const rowToJournalEntry = (row: Record<string, unknown>): RecordJournalEntry => {\n const e: RecordJournalEntry = {\n seq: row.seq as number,\n at: fromMs(row.at as number),\n kind: row.kind as RecordJournalEntry['kind'],\n ops: JSON.parse(row.ops as string),\n version: row.version as number,\n typeId: row.type_id as string,\n };\n if (row.parent_id != null) e.parentId = row.parent_id as string;\n if (row.previous_parent_id != null) {\n e.previousParentId = row.previous_parent_id === '' ? null : (row.previous_parent_id as string);\n }\n if (row.actor != null) e.actor = JSON.parse(row.actor as string);\n if (row.associations != null) e.associations = JSON.parse(row.associations as string);\n return e;\n};\n","/**\n * Normalizes a SQLite binding's call convention (array-bind-then-step,\n * spread-args run/get/all, cursor-returning exec) behind one interface,\n * so the actual CRUD/query/version/type/association/token logic can live\n * once in this package instead of being re-typed per engine. Each adapter\n * package provides a thin SqlExecutor implementation over its own\n * database handle.\n *\n * Deliberately synchronous: every binding this targets executes queries\n * in-process without yielding, and an async interface would force the\n * shared logic's transaction sequences to interleave.\n */\nexport interface SqlExecutor {\n /** Run DDL, pragmas, or any statement(s) with no bound params and no results needed. */\n exec(sql: string): void;\n /** Run a single parameterized statement. Returns the number of rows affected (INSERT/UPDATE/DELETE). */\n run(sql: string, params?: readonly unknown[]): number;\n /** Run a parameterized statement and return the first result row, or undefined. */\n get<T = Record<string, unknown>>(sql: string, params?: readonly unknown[]): T | undefined;\n /** Run a parameterized statement and return all result rows. */\n all<T = Record<string, unknown>>(sql: string, params?: readonly unknown[]): T[];\n /**\n * Run `fn` atomically: every statement it issues commits together, or\n * none do if it throws. `fn` must be synchronous and call back into\n * this executor only — not every engine's transaction primitive can\n * straddle a suspended call. A binding with a real multi-statement\n * transaction (BEGIN/COMMIT/ROLLBACK) implements this with one; a\n * binding whose transaction primitive is itself a callback wrapper\n * (e.g. a Durable Object's storage.transactionSync) can pass `fn`\n * straight through to it.\n */\n transaction<T>(fn: () => T): T;\n}\n\n/** SQLite reports FK violations with this exact message; verify it holds when adding an engine. */\nexport const isForeignKeyViolation = (err: unknown): boolean =>\n err instanceof Error && err.message.includes('FOREIGN KEY constraint failed');\n\n/** SQLite reports a PRIMARY KEY/UNIQUE collision with this exact message. */\nexport const isUniqueConstraintViolation = (err: unknown): boolean =>\n err instanceof Error && err.message.includes('UNIQUE constraint failed');\n","import type { SqlExecutor } from './executor.js';\n\n/** timezone is optional passthrough app metadata — no default. */\nexport type StackConfig = { entityId: string; timezone: string | undefined };\n\n/** The one place the singleton record's id is spelled. */\nconst CONFIG_ROW_SQL = `SELECT content FROM records WHERE id = '_config'`;\n\n/**\n * Inserts the singleton _config@1 record and returns what it wrote, so a\n * caller that has just created a database reaches the same StackConfig an\n * existing one yields without a read-back. Only valid on a freshly-created\n * database.\n */\nexport const insertConfigRecord = (\n exec: SqlExecutor,\n entityId: string,\n timezone: string | undefined,\n): StackConfig => {\n const now = Date.now();\n exec.run(\n `INSERT INTO records (id, type_id, created_at, updated_at, content, version)\n VALUES ('_config', '_config@1', ?, ?, ?, 1)`,\n [now, now, JSON.stringify({ entityId, timezone })],\n );\n return { entityId, timezone };\n};\n\n/**\n * Reads the singleton _config@1 record, or null if this database has none.\n * Null is the signal an engine that cannot tell from the outside whether a\n * store exists (a Durable Object) uses to decide between reattaching and creating.\n */\nexport const tryReadStackConfig = (exec: SqlExecutor): StackConfig | null => {\n const row = exec.get<{ content: string }>(CONFIG_ROW_SQL);\n if (!row) return null;\n const content = JSON.parse(row.content) as { entityId: string; timezone?: string };\n return { entityId: content.entityId, timezone: content.timezone };\n};\n\n/** Reads the singleton _config@1 record. Throws if the database is missing it. */\nexport const readStackConfig = (exec: SqlExecutor): StackConfig => {\n const config = tryReadStackConfig(exec);\n if (!config) throw new Error('Stack database is missing its config record.');\n return config;\n};\n","/**\n * The actual StackRecordAdapter logic (minus lifecycle: open/\n * close/flush stay per-engine, since schema setup, pragmas, locking, and\n * durability differ there). Every CRUD/query/version/type/association\n * operation lives here exactly once, parametrized over a SqlExecutor\n * (which normalizes each engine's call conventions). Concrete adapters\n * construct one of these and delegate their StackRecordAdapter methods\n * to it — see record-adapter-sqlite.\n */\n\nimport {\n StackConflictError,\n StackVersionConflictError,\n StackNotFoundError,\n StackBadRequestError,\n applyMergePatch,\n assertOneSurface,\n} from '@haverstack/core';\nimport type {\n RecordJournalEntry,\n JournalQuery,\n VersionsQuery,\n StackRecord,\n StackType,\n TypeId,\n FileId,\n RecordVersion,\n RecordChangeSet,\n StackQuery,\n QueryResult,\n Association,\n AssociationEdit,\n ActorOptions,\n IfVersionOptions,\n ScalarFieldKind,\n} from '@haverstack/core';\nimport type { JournalEntryInput, JournalOptions } from '@haverstack/core/adapter';\nimport { contentSortEntry } from '@haverstack/core/adapter';\nimport type { SqlExecutor } from './executor.js';\nimport { isForeignKeyViolation, isUniqueConstraintViolation } from './executor.js';\nimport { buildQueryPlan, atBudget } from './query.js';\nimport { fts5Strategy } from './fts5.js';\nimport {\n rowToRecord,\n rowToAssociation,\n rowToType,\n rowToVersion,\n rowToJournalEntry,\n toMs,\n associationKeyColumns,\n} from './mappers.js';\nimport { makeCursor } from './cursor.js';\n\n/**\n * Run a query statement, restating an engine parse error as the wire\n * taxonomy's `bad_request` rather than letting it escape as a raw engine\n * error (which a server has no code to map, so it becomes a 500).\n *\n * Scoped to the one filter that carries a query language: `filter.search`\n * is FTS5 source text, and sanitizeFts5Query() repairs the common shapes\n * but claims no completeness against the grammar. Every other clause here\n * is built from parameters, so a parse error in one is this module's bug —\n * rethrown untouched, where it belongs.\n */\nconst asStackBadRequestError = <T>(query: StackQuery, run: () => T): T => {\n try {\n return run();\n } catch (err) {\n if (!query.filter?.search) throw err;\n throw new StackBadRequestError(\n `Search text could not be parsed: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n};\n\n/**\n * One spelling of the optimistic-concurrency failure, so the message a\n * caller sees never depends on which of the three places noticed it.\n */\nconst versionConflict = (id: string, expected: number, actual: number): StackVersionConflictError =>\n new StackVersionConflictError(\n `Record \"${id}\" is at version ${actual}, expected ${expected}`,\n id,\n expected,\n actual,\n );\n\n/**\n * Every `versions` column beyond the (record_id, version) key, and the\n * values for them in the same order — one list, so the INSERT that writes a\n * snapshot and the UPDATE that replaces one can't drift apart on which\n * columns a snapshot is made of.\n */\nconst VERSION_COLUMNS = [\n 'type_id',\n 'content',\n 'updated_at',\n 'entity_id',\n 'principal_id',\n 'updated_by',\n 'updated_via',\n] as const;\n\nconst versionRowValues = (version: RecordVersion): unknown[] => [\n version.typeId,\n JSON.stringify(version.content),\n toMs(version.updatedAt),\n version.createdBy?.subjectId ?? null,\n version.createdBy?.principalId ?? null,\n version.updatedBy?.subjectId ?? null,\n version.updatedBy?.principalId ?? null,\n];\n\nexport type SharedSqlRecordLogicDeps = {\n exec: SqlExecutor;\n};\n\n/**\n * Top-level scalar fields, by name and declared kind — what content_index\n * covers. Arrays and objects are left out: a value inside one has no\n * single position to order its record by, and no file reference the\n * attachment filter can see.\n */\nconst indexedFieldKinds = (\n schema: Record<string, { kind?: string }>,\n): Map<string, ScalarFieldKind> => {\n const kinds = new Map<string, ScalarFieldKind>();\n for (const [field, def] of Object.entries(schema)) {\n if (!def.kind || def.kind === 'array' || def.kind === 'object') continue;\n kinds.set(field, def.kind as ScalarFieldKind);\n }\n return kinds;\n};\n\nexport class SharedSqlRecordLogic {\n /**\n * Per-typeId cache of the fields content_index covers and their declared\n * kinds, so syncContentIndex() doesn't hit the `types` table and\n * re-parse schema JSON on every write. Populated eagerly in saveType()\n * and lazily via getIndexedFields().\n */\n private readonly indexedFieldsByType = new Map<string, Map<string, ScalarFieldKind>>();\n\n constructor(private readonly deps: SharedSqlRecordLogicDeps) {}\n\n private get exec(): SqlExecutor {\n return this.deps.exec;\n }\n\n /**\n * Precondition check for a mutation that has already read the record —\n * either because it can't fold the check into its own statement\n * (purgeRecord: children reference the row, so the DELETE can't\n * carry it), or because it has to settle the precondition *before*\n * fts5Strategy.remove() runs, which needs the records row still holding\n * the old content. In the latter case versionedUpdate() re-checks it\n * inside the statement, and that is the enforcement; this is what lets\n * the work before it be skipped.\n */\n private checkExpectedVersion(record: StackRecord, expectedVersion: number | undefined): void {\n if (expectedVersion === undefined || record.version === expectedVersion) return;\n throw versionConflict(record.id, expectedVersion, record.version);\n }\n\n /**\n * Called after a version-gated statement affected zero rows.\n * Distinguishes \"record doesn't exist\" (StackNotFoundError) from \"it\n * exists but isn't at expectedVersion\" (StackVersionConflictError, with\n * the actual current version for the caller to act on).\n */\n private throwVersionConflict(id: string, expectedVersion: number | undefined): never {\n const row = this.exec.get<{ version: number }>('SELECT version FROM records WHERE id = ?', [\n id,\n ]);\n if (!row) throw new StackNotFoundError(`Record not found: \"${id}\"`);\n throw versionConflict(id, expectedVersion as number, row.version);\n }\n\n /**\n * The one shape every mutating UPDATE in this class has: whatever columns\n * the caller is changing, then the version bump and the actor/timestamp\n * stamp that every mutation owes, gated on the opt-in `ifVersion`\n * precondition and failing loudly when nothing matched.\n *\n * The guard rides in the WHERE clause even where the caller has already\n * checked the version against a record it read: that read is outside this\n * statement, so a writer slipping in between the two is caught here\n * rather than silently overwritten.\n */\n private versionedUpdate(\n id: string,\n sets: string[],\n values: unknown[],\n opts: IfVersionOptions & ActorOptions,\n now = toMs(new Date()),\n ): void {\n const guarded = opts.ifVersion !== undefined;\n const changed = this.exec.run(\n `UPDATE records SET ${[...sets, 'version = version + 1', 'updated_at = ?', 'updated_by = ?', 'updated_via = ?'].join(', ')}` +\n ` WHERE id = ?${guarded ? ' AND version = ?' : ''}`,\n [\n ...values,\n now,\n opts.actor?.subjectId ?? null,\n opts.actor?.principalId ?? null,\n id,\n ...(guarded ? [opts.ifVersion] : []),\n ],\n );\n if (changed === 0) this.throwVersionConflict(id, opts.ifVersion);\n }\n\n /**\n * Read a record back after its own mutation committed, for the caller to\n * return. Its absence is not a \"not found\" for the caller to handle — the\n * mutation just succeeded against it — so it fails as the invariant\n * breach it would be.\n *\n * Synchronous, via readRecord() rather than getRecord(), so the read\n * cannot be separated from the commit by anything: an async caller\n * returning this still hands back a record read in the same tick its\n * transaction committed. See readRecord() for why that matters.\n */\n private reread(id: string, verb: string): StackRecord {\n const updated = this.readRecord(id);\n if (!updated) throw new Error(`Record not found after ${verb}: \"${id}\"`);\n return updated;\n }\n\n // -------------------------------------------------------\n // Records\n // -------------------------------------------------------\n\n /**\n * A duplicate id hits the `records` PK — mapped to StackConflictError\n * instead of surfacing the raw engine exception, mirroring saveVersion's\n * collision mapping below.\n *\n * One transaction, because a record and the four tables that describe it\n * are one fact. A half-applied create is worse than a failed one: the\n * call raises, so the caller believes nothing landed, while the records\n * row survives to fail their retry on the PK — and the row it leaves is\n * invisible to `filter.search` and mis-ordered by `sort.contentField`\n * until something writes it again.\n */\n async createRecord(record: StackRecord, opts: JournalOptions = {}): Promise<StackRecord> {\n this.exec.transaction(() => {\n try {\n this.exec.run(\n `INSERT INTO records\n (id, type_id, created_at, updated_at, content, version,\n parent_id, entity_id, app_id, principal_id, updated_by, updated_via,\n deleted_at, unlisted_at)\n VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,\n [\n record.id,\n record.typeId,\n toMs(record.createdAt),\n toMs(record.updatedAt),\n JSON.stringify(record.content),\n record.version,\n record.parentId ?? null,\n record.createdBy?.subjectId ?? null,\n record.appId ?? null,\n record.createdBy?.principalId ?? null,\n record.updatedBy?.subjectId ?? null,\n record.updatedBy?.principalId ?? null,\n record.deletedAt ? toMs(record.deletedAt) : null,\n record.unlistedAt ? toMs(record.unlistedAt) : null,\n ],\n );\n } catch (err) {\n if (isUniqueConstraintViolation(err)) {\n throw new StackConflictError(`Record already exists: \"${record.id}\"`);\n }\n throw err;\n }\n\n const edges = [...(record.associations ?? []), ...(record.permissions ?? [])];\n if (edges.length) this.insertAssociations(record.id, edges);\n\n fts5Strategy.insert(this.exec, record.id, JSON.stringify(record.content));\n this.syncContentIndex(record.id, record.typeId, record.content);\n this.appendJournal(record.id, opts.journal);\n });\n\n return record;\n }\n\n async getRecord(id: string): Promise<StackRecord | null> {\n return this.readRecord(id);\n }\n\n /**\n * The synchronous read behind getRecord(). Callers inside exec.transaction()\n * use this one: an `await` inside that callback yields the microtask queue\n * mid-transaction, and the next operation to run would try to open one of\n * its own — transaction() requires a synchronous callback for exactly this\n * reason (see SqlExecutor.transaction).\n *\n * The same synchrony is what lets the post-commit reads below report the\n * version their own mutation produced: reread() calls this directly, so\n * nothing interleaves between the commit and the read even though the\n * method returning it is async. A backend that made these reads genuinely\n * asynchronous would open that window, and a concurrent mutation could be\n * reported under this one's verb.\n */\n private readRecord(id: string): StackRecord | null {\n const row = this.exec.get<Record<string, unknown>>('SELECT * FROM records WHERE id = ?', [id]);\n if (!row) return null;\n const associations = this.getAssociationsForRecord(id);\n return rowToRecord(row, associations);\n }\n\n /**\n * Apply a change set in one transaction: one UPDATE over the record row\n * with only the columns the change set names, the association set\n * replaced when it carries one, and the snapshot written alongside — so\n * one call is one version whatever it moved.\n *\n * `opts.bumpsVersion: false` is a change set that touches only\n * `associations`, `parentId` and/or `unlisted` — Stack computes this from\n * which ops the set actually moves. Such a write still lands its columns,\n * but leaves `version`, `updatedAt` and the actor stamps exactly as they\n * stood and takes no snapshot. `ifVersion`, if given, is re-checked\n * synchronously inside the transaction — the CAS guard the bumping path\n * gets from its UPDATE's WHERE clause, restated as a read because the\n * UPDATE here carries no version predicate. See\n * docs/spec/versioning.md § Version history.\n *\n * The ifVersion check for a bumping write is standalone rather than\n * folded into the UPDATE alone, because a content change needs\n * fts.remove() to run *before* the records-table content changes. The\n * guard is still in the UPDATE's WHERE, so a writer that slipped in\n * between the read and the write is caught there rather than overwriting.\n */\n async mutateRecord(\n id: string,\n changes: RecordChangeSet,\n opts: {\n ifVersion?: number;\n snapshot?: RecordVersion;\n bumpsVersion?: boolean;\n } & ActorOptions &\n JournalOptions = {},\n ): Promise<StackRecord> {\n const existing = await this.getRecord(id);\n if (!existing) throw new StackNotFoundError(`Record not found: \"${id}\"`);\n this.checkExpectedVersion(existing, opts.ifVersion);\n\n const now = toMs(new Date());\n\n // Assembled rather than written out per aspect: every aspect is the\n // same column-and-value pair on one row, and a fixed statement per\n // combination would be 2^4 of them. Built before the no-bump branch\n // because both paths write the same columns; only `content` is the\n // bumping path's alone, since a patch always bumps.\n const sets: string[] = [];\n const values: unknown[] = [];\n if (changes.parentId !== undefined) {\n sets.push('parent_id = ?');\n values.push(changes.parentId);\n }\n if (changes.unlisted !== undefined) {\n sets.push('unlisted_at = ?');\n // Stamped fresh because this key is only ever present on a\n // transition — Stack narrows a change set to the aspects that\n // actually moved before it reaches an adapter.\n values.push(changes.unlisted ? now : null);\n }\n\n if (opts.bumpsVersion === false) {\n this.exec.transaction(() => {\n const current = this.readRecord(id);\n if (!current) throw new StackNotFoundError(`Record not found: \"${id}\"`);\n this.checkExpectedVersion(current, opts.ifVersion);\n if (sets.length > 0) {\n this.exec.run(`UPDATE records SET ${sets.join(', ')} WHERE id = ?`, [...values, id]);\n }\n this.replaceAssociationHalves(id, changes);\n this.appendJournal(id, opts.journal);\n });\n return this.reread(id, 'mutateRecord');\n }\n\n const merged = changes.contentPatch\n ? applyMergePatch(existing.content, changes.contentPatch)\n : undefined;\n if (merged !== undefined) {\n sets.unshift('content = ?');\n values.unshift(JSON.stringify(merged));\n }\n\n this.exec.transaction(() => {\n if (opts.snapshot) this.snapshotBeforeMutation(id, opts.snapshot);\n if (merged !== undefined) fts5Strategy.remove(this.exec, id);\n\n this.versionedUpdate(id, sets, values, opts, now);\n\n this.replaceAssociationHalves(id, changes);\n\n if (merged !== undefined) {\n fts5Strategy.insert(this.exec, id, JSON.stringify(merged));\n this.syncContentIndex(id, existing.typeId, merged);\n }\n this.appendJournal(id, opts.journal);\n });\n\n return this.reread(id, 'mutateRecord');\n }\n\n async deleteRecord(\n id: string,\n opts: {\n purge?: boolean;\n ifVersion?: number;\n snapshot?: RecordVersion;\n } & ActorOptions &\n JournalOptions = {},\n ): Promise<StackRecord | null> {\n if (opts.purge) {\n return this.exec.transaction(() => this.purgeRecord(id, opts.ifVersion));\n }\n\n // One timestamp for both columns: a soft delete is a single event, and\n // two `new Date()` calls can straddle a millisecond boundary and leave\n // deleted_at and updated_at disagreeing about when it happened.\n const now = toMs(new Date());\n this.exec.transaction(() => {\n if (opts.snapshot) this.snapshotBeforeMutation(id, opts.snapshot);\n this.versionedUpdate(id, ['deleted_at = ?'], [now], opts, now);\n this.appendJournal(id, opts.journal);\n });\n\n return this.reread(id, 'deleteRecord');\n }\n\n /**\n * Deletes a record's FTS entry, associations, versions, file-refs, sort\n * index rows, and row, returning the record as it stood — the last copy that will ever\n * exist, read here so that nothing can overtake it between the read and\n * the deletes. Null when there was no such record. No write() call —\n * callers batch it. ifVersion is checked first so a lost CAS race\n * leaves nothing touched (children reference the row, so the check can't\n * fold into the final DELETE).\n */\n private purgeRecord(id: string, ifVersion?: number): StackRecord | null {\n const purged = this.readRecord(id);\n if (!purged) {\n // A CAS against a record that isn't there is a failed precondition,\n // not the \"nothing to delete\" that an unconditional purge\n // reports by returning null.\n if (ifVersion !== undefined) throw new StackNotFoundError(`Record not found: \"${id}\"`);\n return null;\n }\n this.checkExpectedVersion(purged, ifVersion);\n fts5Strategy.remove(this.exec, id);\n this.exec.run('DELETE FROM associations WHERE record_id = ?', [id]);\n this.exec.run('DELETE FROM versions WHERE record_id = ?', [id]);\n // A purge takes the journal with it, for the reason it takes the\n // version history: a log naming what a destroyed record once held is\n // exactly the residue this verb exists to leave nothing of.\n this.exec.run('DELETE FROM journal WHERE record_id = ?', [id]);\n this.exec.run('DELETE FROM content_index WHERE record_id = ?', [id]);\n this.exec.run('DELETE FROM records WHERE id = ?', [id]);\n return purged;\n }\n\n async undeleteRecord(\n id: string,\n opts: { ifVersion?: number; snapshot?: RecordVersion } & ActorOptions & JournalOptions = {},\n ): Promise<StackRecord> {\n this.exec.transaction(() => {\n if (opts.snapshot) this.snapshotBeforeMutation(id, opts.snapshot);\n this.versionedUpdate(id, ['deleted_at = NULL'], [], opts);\n this.appendJournal(id, opts.journal);\n });\n\n return this.reread(id, 'undelete');\n }\n\n async restoreVersion(\n id: string,\n version: number,\n opts: {\n ifVersion?: number;\n snapshot?: RecordVersion;\n } & ActorOptions &\n JournalOptions = {},\n ): Promise<StackRecord> {\n const existing = await this.getRecord(id);\n if (!existing) throw new StackNotFoundError(`Record not found: \"${id}\"`);\n this.checkExpectedVersion(existing, opts.ifVersion);\n\n const target = await this.getVersion(id, version);\n if (!target) throw new StackNotFoundError(`Version not found: \"${id}\"@${version}`);\n\n // Only content and its type are restored — a snapshot carries nothing\n // else, so there is nothing here to roll containment, listing or the\n // associations table back to.\n this.exec.transaction(() => {\n if (opts.snapshot) this.snapshotBeforeMutation(id, opts.snapshot);\n this.rewriteContent(id, target.typeId, target.content, opts);\n this.appendJournal(id, opts.journal);\n });\n\n return this.reread(id, 'restoreVersion');\n }\n\n async commitMigration(\n id: string,\n toTypeId: TypeId,\n content: Record<string, unknown>,\n opts: { ifVersion?: number; snapshot?: RecordVersion } & ActorOptions & JournalOptions = {},\n ): Promise<StackRecord> {\n // Checked against a read record first, rather than left to the\n // UPDATE's WHERE clause alone: fts5Strategy.remove() has to run before\n // the content changes, so the precondition has to settle before that\n // work starts. rewriteContent()'s UPDATE still carries the guard, so a\n // writer slipping in between the two is caught rather than overwritten\n // — same shape as mutateRecord() and restoreVersion().\n const existing = await this.getRecord(id);\n if (!existing) throw new StackNotFoundError(`Record not found: \"${id}\"`);\n this.checkExpectedVersion(existing, opts.ifVersion);\n\n this.exec.transaction(() => {\n if (opts.snapshot) this.snapshotBeforeMutation(id, opts.snapshot);\n this.rewriteContent(id, toTypeId, content, opts);\n this.appendJournal(id, opts.journal);\n });\n\n return this.reread(id, 'commitMigration');\n }\n\n /**\n * Replace a record's type and content wholesale — what restoreVersion and\n * commitMigration each do, differing only in where the content came from.\n *\n * The order is the constraint: fts5Strategy.remove() has to read the old\n * content off the records row, so it runs before the UPDATE, and the\n * re-index runs after. Every caller must already sit inside\n * exec.transaction() — a failure between the two halves would otherwise\n * leave the record unindexed.\n */\n private rewriteContent(\n id: string,\n typeId: TypeId,\n content: Record<string, unknown>,\n opts: IfVersionOptions & ActorOptions,\n ): void {\n const json = JSON.stringify(content);\n fts5Strategy.remove(this.exec, id);\n this.versionedUpdate(id, ['type_id = ?', 'content = ?'], [typeId, json], opts);\n fts5Strategy.insert(this.exec, id, json);\n this.syncContentIndex(id, typeId, content);\n }\n\n async queryRecords(query: StackQuery): Promise<QueryResult> {\n const limit = query.limit ?? 50;\n // One extra row, to learn whether a further page follows.\n const fetch = limit + 1;\n\n // A content-field sort answers from two statements, the records that\n // hold a value at the field and then the records that hold none. They\n // read in order, each asking only for the rows the ones before it left\n // of the page, and the second is skipped once the first has filled it —\n // the common case, since a page rarely straddles the boundary.\n const rows: Record<string, unknown>[] = [];\n for (const page of buildQueryPlan(query)) {\n const budget = fetch - rows.length;\n if (budget <= 0) break;\n const statement = atBudget(page, budget);\n rows.push(\n ...asStackBadRequestError(query, () =>\n this.exec.all<Record<string, unknown>>(statement.sql, statement.params),\n ),\n );\n }\n\n const hasMore = rows.length > limit;\n const page = hasMore ? rows.slice(0, limit) : rows;\n\n const records = page.map((row) => {\n const associations = this.getAssociationsForRecord(row.id as string);\n return rowToRecord(row, associations);\n });\n\n const lastRecord = records[records.length - 1];\n const cursor =\n hasMore && lastRecord\n ? makeCursor(lastRecord, query.sort, (record, field) => this.sortFieldKind(record, field))\n : null;\n\n return { records, cursor };\n }\n\n /**\n * Atomically verify fileId is unreferenced, then purge its\n * metadata records across every typeId in `metadataTypeIds` (see\n * deleteUnreferencedAttachmentRecords on the adapter contract). A real\n * SQL transaction of synchronous calls — no `await` between the check\n * and the deletes, so nothing interleaves.\n */\n async deleteUnreferencedAttachmentRecords(\n fileId: FileId,\n metadataTypeIds: TypeId[],\n ): Promise<StackRecord[]> {\n return this.exec.transaction(() => {\n const referenced = this.exec.all<{ found: number }>(\n `SELECT 1 as found FROM associations WHERE kind = 'attachment' AND file_id = ?\n UNION ALL\n SELECT 1 FROM content_index WHERE file_id = ?\n LIMIT 1`,\n [fileId, fileId],\n );\n if (referenced.length) {\n throw new StackConflictError('Attachment is still referenced by one or more records');\n }\n\n // An empty family still owes the caller the reference check above —\n // the conflict is a fact about the file, not about which metadata\n // types happen to be defined.\n if (metadataTypeIds.length === 0) return [];\n\n const placeholders = metadataTypeIds.map(() => '?').join(', ');\n const metaRows = this.exec.all<{ id: string }>(\n `SELECT id FROM records\n WHERE type_id IN (${placeholders}) AND json_extract(content, '$.fileId') = ?`,\n [...metadataTypeIds, fileId],\n );\n const deleted: StackRecord[] = [];\n for (const row of metaRows) {\n const purged = this.purgeRecord(row.id);\n if (purged) deleted.push(purged);\n }\n\n return deleted;\n });\n }\n\n // -------------------------------------------------------\n // Versions\n // -------------------------------------------------------\n\n async getVersions(id: string, query: VersionsQuery = {}): Promise<RecordVersion[]> {\n const conditions = ['record_id = ?'];\n const values: unknown[] = [id];\n if (query.beforeVersion !== undefined) {\n conditions.push('version < ?');\n values.push(query.beforeVersion);\n }\n const rows = this.exec.all<Record<string, unknown>>(\n `SELECT * FROM versions WHERE ${conditions.join(' AND ')} ORDER BY version DESC${\n query.limit !== undefined ? ' LIMIT ?' : ''\n }`,\n query.limit !== undefined ? [...values, query.limit] : values,\n );\n return rows.map(rowToVersion);\n }\n\n async getVersion(id: string, version: number): Promise<RecordVersion | null> {\n const row = this.exec.get<Record<string, unknown>>(\n 'SELECT * FROM versions WHERE record_id = ? AND version = ?',\n [id, version],\n );\n return row ? rowToVersion(row) : null;\n }\n\n /**\n * A (record_id, version) collision is rejected loudly via the UNIQUE\n * constraint, mapped to StackConflictError — never a silently discarded\n * snapshot leaving a hole in rollback history. See\n * docs/spec/versioning.md § Optimistic concurrency (`ifVersion`).\n */\n private insertVersionRow(id: string, version: RecordVersion): void {\n try {\n this.exec.run(\n `INSERT INTO versions\n (record_id, version, ${VERSION_COLUMNS.join(', ')})\n VALUES (?, ?, ${VERSION_COLUMNS.map(() => '?').join(', ')})`,\n [id, version.version, ...versionRowValues(version)],\n );\n } catch (err) {\n if (isUniqueConstraintViolation(err)) {\n throw new StackConflictError(\n `Version ${version.version} already exists for record \"${id}\" — a concurrent ` +\n `writer raced past this version. Use ifVersion to detect this before it happens.`,\n );\n }\n throw err;\n }\n }\n\n /**\n * Replaces every column insertVersionRow writes — structurally, off the\n * same VERSION_COLUMNS list, because a column left out here would keep\n * the replaced row's value and surface later as a restore putting back\n * something the snapshot never said.\n */\n private overwriteVersionRow(id: string, version: RecordVersion): void {\n this.exec.run(\n `UPDATE versions\n SET ${VERSION_COLUMNS.map((c) => `${c} = ?`).join(', ')}\n WHERE record_id = ? AND version = ?`,\n [...versionRowValues(version), id, version.version],\n );\n }\n\n /**\n * Standalone snapshot write for tooling and tests — loud on any\n * collision, since nothing here is about to bump the version. Mutating\n * methods take a `snapshot` option instead; see snapshotBeforeMutation.\n */\n async saveVersion(id: string, version: RecordVersion): Promise<void> {\n this.insertVersionRow(id, version);\n }\n\n /**\n * The snapshot half of a mutating method's atomic snapshot-then-mutate\n * transaction. A collision is a racing-writer conflict, except an orphan\n * row at the record's *current* version — recognized by version number\n * alone, never content — which is overwritten so the interrupted write\n * can complete. See docs/spec/versioning.md § Snapshot atomicity.\n */\n private snapshotBeforeMutation(id: string, version: RecordVersion): void {\n try {\n this.insertVersionRow(id, version);\n } catch (err) {\n if (!(err instanceof StackConflictError)) throw err;\n\n const row = this.exec.get<{ version: number }>('SELECT version FROM records WHERE id = ?', [\n id,\n ]);\n if (!row || row.version !== version.version) throw err;\n\n this.overwriteVersionRow(id, version);\n }\n }\n\n // -------------------------------------------------------\n // Change journal\n // -------------------------------------------------------\n\n /**\n * Append one entry, stamping the record-derived half off the row as it\n * now stands — so every caller must already sit inside the same\n * transaction as the write it describes, after that write has landed.\n *\n * `seq` is allocated here, from the log's own max, rather than computed\n * by the caller. That is what spares this table the collision healing\n * versions needs: nothing outside this statement ever holds a seq it\n * expects to be free. See docs/spec/journal.md § Ordering.\n */\n private appendJournal(recordId: string, entry: JournalEntryInput | undefined): void {\n if (!entry) return;\n // Three columns, not readRecord(): the stamp needs version/typeId/\n // parentId and nothing else, and this runs inside every mutating\n // write — a full row plus its association set, per write, to read\n // three values. `parent_id` is used raw so the NULL/''/id spelling\n // reaches the journal exactly as the records table holds it.\n const row = this.exec.get<{\n version: number;\n type_id: string;\n parent_id: string | null;\n }>('SELECT version, type_id, parent_id FROM records WHERE id = ?', [recordId]);\n if (!row) throw new StackNotFoundError(`Record not found: \"${recordId}\"`);\n\n this.exec.run(\n `INSERT INTO journal\n (record_id, seq, at, kind, ops, version, type_id, parent_id,\n previous_parent_id, actor, associations)\n VALUES (\n ?,\n (SELECT COALESCE(MAX(seq), 0) + 1 FROM journal WHERE record_id = ?),\n ?, ?, ?, ?, ?, ?, ?, ?, ?)`,\n [\n recordId,\n recordId,\n toMs(new Date()),\n entry.kind,\n JSON.stringify(entry.ops),\n row.version,\n row.type_id,\n row.parent_id,\n // '' is the root; NULL is an entry that names no origin at all.\n entry.previousParentId === undefined ? null : (entry.previousParentId ?? ''),\n entry.actor ? JSON.stringify(entry.actor) : null,\n entry.associations?.length ? JSON.stringify(entry.associations) : null,\n ],\n );\n }\n\n async getJournal(id: string, query: JournalQuery = {}): Promise<RecordJournalEntry[]> {\n // An empty log means \"nothing changed\" unconditionally, so a record\n // that isn't there cannot be spelled that way. A purged record is gone,\n // and gets the same refusal. See docs/spec/journal.md § Reading it.\n if (!this.exec.get('SELECT 1 FROM records WHERE id = ?', [id])) {\n throw new StackNotFoundError(`Record not found: \"${id}\"`);\n }\n const conditions = ['record_id = ?'];\n const values: unknown[] = [id];\n if (query.afterSeq !== undefined) {\n conditions.push('seq > ?');\n values.push(query.afterSeq);\n }\n const rows = this.exec.all<Record<string, unknown>>(\n `SELECT * FROM journal WHERE ${conditions.join(' AND ')} ORDER BY seq ASC${\n query.limit !== undefined ? ' LIMIT ?' : ''\n }`,\n query.limit !== undefined ? [...values, query.limit] : values,\n );\n return rows.map(rowToJournalEntry);\n }\n\n // -------------------------------------------------------\n // Types\n // -------------------------------------------------------\n\n async saveType(type: StackType): Promise<void> {\n this.exec.run(\n `INSERT OR REPLACE INTO types\n (id, base_id, version, name, schema, schema_hash, migrates_from, created_at)\n VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,\n [\n type.id,\n type.baseId,\n type.version,\n type.name,\n JSON.stringify(type.schema),\n type.schemaHash,\n type.migratesFrom ?? null,\n toMs(type.createdAt),\n ],\n );\n const schema = type.schema as Record<string, { kind?: string }>;\n this.indexedFieldsByType.set(type.id, indexedFieldKinds(schema));\n }\n\n async getType(id: TypeId): Promise<StackType | null> {\n const row = this.exec.get<Record<string, unknown>>('SELECT * FROM types WHERE id = ?', [id]);\n return row ? rowToType(row) : null;\n }\n\n async listTypes(): Promise<StackType[]> {\n const rows = this.exec.all<Record<string, unknown>>(\n 'SELECT * FROM types ORDER BY base_id, version',\n );\n return rows.map(rowToType);\n }\n\n // -------------------------------------------------------\n // Associations\n // -------------------------------------------------------\n\n /**\n * Never bumps `version`/`updatedAt` and never snapshots — see\n * StackRecordAdapter.amendAssociations(). The records row is untouched\n * entirely; only the associations table changes, in one transaction:\n * removes first, then adds.\n */\n async amendAssociations(\n recordId: string,\n changes: AssociationEdit[],\n opts: JournalOptions = {},\n ): Promise<StackRecord> {\n assertOneSurface(changes);\n this.exec.transaction(() => {\n if (!this.readRecord(recordId))\n throw new StackNotFoundError(`Record not found: \"${recordId}\"`);\n for (const change of changes) {\n if (change.op !== 'remove') continue;\n const { association } = change;\n this.exec.run(\n `DELETE FROM associations\n WHERE record_id = ?\n AND kind = ?\n AND label = ?\n AND file_id = ?\n AND related_scope = ?\n AND related_id = ?\n AND related_ns = ?\n AND related_stack = ?\n AND related_role = ?`,\n [recordId, association.kind, association.label, ...associationKeyColumns(association)],\n );\n }\n const adds = changes.flatMap((c) => (c.op === 'add' ? [c.association] : []));\n if (adds.length) this.insertAssociations(recordId, adds);\n this.appendJournal(recordId, opts.journal);\n });\n\n return this.reread(recordId, 'amendAssociations');\n }\n\n /** The kinds each change-set key replaces, and nothing else. */\n private static readonly DATA_KINDS = ['tag', 'attachment', 'relationship'];\n private static readonly AUTHORITY_KINDS = ['permission', 'anyone'];\n\n /**\n * Apply a change set's two association keys, each replacing only within\n * its own half of the partition. That partition is the whole reason a\n * tag write cannot drop an ACL: the `associations` key deletes data\n * kinds alone, so the authority rows beside them are never in scope.\n * Caller-transactional, like every write below.\n * See docs/spec/access-control.md § Record-level permissions.\n */\n private replaceAssociationHalves(recordId: string, changes: RecordChangeSet): void {\n if (changes.associations !== undefined) {\n this.replaceAssociations(recordId, changes.associations, SharedSqlRecordLogic.DATA_KINDS);\n }\n if (changes.permissions !== undefined) {\n this.replaceAssociations(recordId, changes.permissions, SharedSqlRecordLogic.AUTHORITY_KINDS);\n }\n }\n\n /**\n * Swap one half of a record's association set wholesale — the \"replace,\n * don't merge\" semantics a change set's association list carries.\n */\n private replaceAssociations(\n recordId: string,\n associations: Association[],\n kinds: string[],\n ): void {\n this.exec.run(\n `DELETE FROM associations WHERE record_id = ? AND kind IN (${kinds.map(() => '?').join(', ')})`,\n [recordId, ...kinds],\n );\n if (associations.length) this.insertAssociations(recordId, associations);\n }\n\n /**\n * Upserts on association identity: the primary key decides which row a\n * write lands on, and `attachment_record_id` — outside it — is whatever\n * the incoming association says, so re-pointing one costs no second row.\n *\n * FK enforcement (PRAGMA_FOREIGN_KEYS_ON) means inserting an\n * association against a record that doesn't exist throws — mapped\n * here to StackNotFoundError so amendAssociations() on a nonexistent record\n * fails loudly instead of silently creating an orphan row.\n */\n private insertAssociations(recordId: string, associations: Association[]): void {\n for (const assoc of associations) {\n try {\n this.exec.run(\n `INSERT INTO associations\n (record_id, kind, label, file_id, related_scope, related_id, related_ns,\n related_stack, related_role, attachment_record_id)\n VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\n ON CONFLICT DO UPDATE SET attachment_record_id = excluded.attachment_record_id`,\n [\n recordId,\n assoc.kind,\n assoc.label,\n ...associationKeyColumns(assoc),\n assoc.kind === 'attachment' ? (assoc.attachmentRecordId ?? '') : '',\n ],\n );\n } catch (err) {\n if (isForeignKeyViolation(err)) {\n throw new StackNotFoundError(`Record not found: \"${recordId}\"`);\n }\n throw err;\n }\n }\n }\n\n private getAssociationsForRecord(recordId: string): Association[] {\n const rows = this.exec.all<Record<string, unknown>>(\n 'SELECT * FROM associations WHERE record_id = ?',\n [recordId],\n );\n return rows.map(rowToAssociation);\n }\n\n /**\n * The fields content_index covers for typeId, from the in-memory cache\n * — falling back to a `types` lookup (and populating the cache) only for\n * a typeId this process hasn't seen via saveType() yet, e.g. right after\n * open().\n */\n private getIndexedFields(typeId: string): Map<string, ScalarFieldKind> {\n const cached = this.indexedFieldsByType.get(typeId);\n if (cached) return cached;\n\n const typeRow = this.exec.get<{ schema: string }>('SELECT schema FROM types WHERE id = ?', [\n typeId,\n ]);\n const fields = typeRow\n ? indexedFieldKinds(JSON.parse(typeRow.schema) as Record<string, { kind?: string }>)\n : new Map<string, ScalarFieldKind>();\n this.indexedFieldsByType.set(typeId, fields);\n return fields;\n }\n\n /**\n * Replace a record's content_index rows with what its content currently\n * holds, on every write that can change content or typeId, so neither\n * the ordering nor the file-reference lookup can drift from the record.\n * A field holding no orderable value gets no row at all, which is what\n * puts the record at the end of a sort on that field.\n */\n private syncContentIndex(\n recordId: string,\n typeId: string,\n content: Record<string, unknown>,\n ): void {\n this.exec.run('DELETE FROM content_index WHERE record_id = ?', [recordId]);\n\n for (const [field, kind] of this.getIndexedFields(typeId)) {\n const entry = contentSortEntry(kind, content[field]);\n if (!entry) continue;\n const sql = `INSERT OR IGNORE INTO content_index\n (record_id, field, value_rank, num_value, text_key, text_value, file_id)\n VALUES (?, ?, ?, ?, ?, ?, ?)`;\n if (entry.kind === 'num') {\n this.exec.run(sql, [recordId, field, 0, entry.num, null, null, null]);\n } else {\n // A file-ref field's value is its file id, so the row it already\n // needs for ordering also answers the attachment filter — one\n // field is one row, whatever a query reads it for.\n const fileId = kind === 'file-ref' ? entry.text : null;\n this.exec.run(sql, [recordId, field, 1, null, entry.key, entry.text, fileId]);\n }\n }\n }\n\n /** The declared kind of a record's top-level `field`, if it has one. */\n private sortFieldKind(record: StackRecord, field: string): ScalarFieldKind | undefined {\n return this.getIndexedFields(record.typeId).get(field);\n }\n}\n","/**\n * The engine-independent half of a SQLite-backed StackRecordAdapter: the\n * capability declaration and the delegation of all seventeen\n * StackRecordAdapter methods onto SharedSqlRecordLogic. Every method here\n * is a forward with no engine-specific content, so an adapter that owns a\n * SqlExecutor already knows how to answer all of them.\n *\n * What a concrete adapter is left to supply is exactly what differs\n * between bindings: constructing the database handle, applying the\n * schema, and its own lifecycle (flush/close) — hence those two being the\n * only abstract members.\n *\n * Lives in record.js's barrel rather than index.js's, so an adapter that\n * imports it never pulls in token-logic.ts's `node:crypto` — see the note\n * at the top of record.ts.\n */\n\nimport type {\n JournalQuery,\n VersionsQuery,\n RecordJournalEntry,\n StackType,\n TypeId,\n FileId,\n RecordVersion,\n ActorOptions,\n StackRecord,\n StackQuery,\n QueryResult,\n AssociationEdit,\n RecordChangeSet,\n StackCapabilities,\n} from '@haverstack/core';\nimport type { StackRecordAdapter, JournalOptions } from '@haverstack/core/adapter';\nimport type { SqlExecutor } from './executor.js';\nimport type { StackConfig } from './config.js';\nimport { SharedSqlRecordLogic } from './record-logic.js';\n\n/**\n * What every engine running SharedSqlRecordLogic's query builder supports.\n * It describes the shared SQL, not the binding, so it is the same for each\n * of them — an engine whose SQLite build lacked FTS5 would override\n * `filter.search` rather than restate the whole declaration.\n */\nexport const SQLITE_RECORD_CAPABILITIES: StackCapabilities = {\n filter: {\n content: 'path',\n contentPresent: true,\n search: true,\n },\n sort: {\n fields: ['createdAt', 'updatedAt', 'version'],\n contentField: true,\n },\n limits: {\n attachmentBytes: null,\n contentBytes: null,\n },\n};\n\nexport abstract class SharedSqlRecordAdapter implements StackRecordAdapter {\n readonly capabilities: StackCapabilities = SQLITE_RECORD_CAPABILITIES;\n\n readonly ownerEntityId: string;\n readonly timezone: string | undefined;\n\n private readonly record: SharedSqlRecordLogic;\n\n protected constructor(exec: SqlExecutor, config: StackConfig) {\n this.record = new SharedSqlRecordLogic({ exec });\n this.ownerEntityId = config.entityId;\n this.timezone = config.timezone;\n }\n\n // -------------------------------------------------------\n // Records\n // -------------------------------------------------------\n\n createRecord(record: StackRecord, opts?: JournalOptions): Promise<StackRecord> {\n return this.record.createRecord(record, opts);\n }\n\n getRecord(id: string): Promise<StackRecord | null> {\n return this.record.getRecord(id);\n }\n\n mutateRecord(\n id: string,\n changes: RecordChangeSet,\n opts?: {\n ifVersion?: number;\n snapshot?: RecordVersion;\n bumpsVersion?: boolean;\n } & ActorOptions &\n JournalOptions,\n ): Promise<StackRecord> {\n return this.record.mutateRecord(id, changes, opts);\n }\n\n deleteRecord(\n id: string,\n opts?: { purge?: boolean; ifVersion?: number; snapshot?: RecordVersion } & ActorOptions &\n JournalOptions,\n ): Promise<StackRecord | null> {\n return this.record.deleteRecord(id, opts);\n }\n\n undeleteRecord(\n id: string,\n opts?: { ifVersion?: number; snapshot?: RecordVersion } & ActorOptions & JournalOptions,\n ): Promise<StackRecord> {\n return this.record.undeleteRecord(id, opts);\n }\n\n restoreVersion(\n id: string,\n version: number,\n opts?: { ifVersion?: number; snapshot?: RecordVersion } & ActorOptions & JournalOptions,\n ): Promise<StackRecord> {\n return this.record.restoreVersion(id, version, opts);\n }\n\n commitMigration(\n id: string,\n toTypeId: TypeId,\n content: Record<string, unknown>,\n opts?: { ifVersion?: number; snapshot?: RecordVersion } & ActorOptions & JournalOptions,\n ): Promise<StackRecord> {\n return this.record.commitMigration(id, toTypeId, content, opts);\n }\n\n queryRecords(query: StackQuery): Promise<QueryResult> {\n return this.record.queryRecords(query);\n }\n\n deleteUnreferencedAttachmentRecords(\n fileId: FileId,\n metadataTypeIds: TypeId[],\n ): Promise<StackRecord[]> {\n return this.record.deleteUnreferencedAttachmentRecords(fileId, metadataTypeIds);\n }\n\n // -------------------------------------------------------\n // Versions\n // -------------------------------------------------------\n\n getVersions(id: string, query?: VersionsQuery): Promise<RecordVersion[]> {\n return this.record.getVersions(id, query);\n }\n\n getVersion(id: string, version: number): Promise<RecordVersion | null> {\n return this.record.getVersion(id, version);\n }\n\n saveVersion(id: string, version: RecordVersion): Promise<void> {\n return this.record.saveVersion(id, version);\n }\n\n // -------------------------------------------------------\n // Change journal\n // -------------------------------------------------------\n\n getJournal(id: string, query?: JournalQuery): Promise<RecordJournalEntry[]> {\n return this.record.getJournal(id, query);\n }\n\n // -------------------------------------------------------\n // Types\n // -------------------------------------------------------\n\n saveType(type: StackType): Promise<void> {\n return this.record.saveType(type);\n }\n\n getType(id: TypeId): Promise<StackType | null> {\n return this.record.getType(id);\n }\n\n listTypes(): Promise<StackType[]> {\n return this.record.listTypes();\n }\n\n // -------------------------------------------------------\n // Associations\n // -------------------------------------------------------\n\n amendAssociations(\n recordId: string,\n changes: AssociationEdit[],\n opts?: JournalOptions,\n ): Promise<StackRecord> {\n return this.record.amendAssociations(recordId, changes, opts);\n }\n\n // -------------------------------------------------------\n // Lifecycle\n // -------------------------------------------------------\n\n abstract flush(): Promise<void>;\n abstract close(): Promise<void>;\n}\n","import type { SqlExecutor } from '@haverstack/sqlite-shared/record';\n\n/**\n * Normalizes a Durable Object's ctx.storage.sql to SqlExecutor.\n *\n * Two things don't map onto node:sqlite's shape the way the rest of this\n * interface does:\n *\n * - There is no get/all/run split — sql.exec() always returns a cursor.\n * get()/all() read it via .toArray(); run() reads .rowsWritten for the\n * affected-row count run()'s contract promises.\n * - Raw BEGIN/COMMIT/ROLLBACK are rejected outright by DO SQLite (verified\n * against the real Workers runtime, not assumed) — its storage has no\n * notion of a transaction left open across separate exec() calls, and\n * each statement auto-commits the instant it runs, so no-op'ing those\n * three strings would silently break atomicity, not preserve it. The\n * platform's real primitive is ctx.storage.transactionSync(fn), which\n * SqlExecutor.transaction() reaches directly — see docs/spec/adapters.md\n * § Concurrency & storage ownership.\n */\nexport class DurableObjectSqliteExecutor implements SqlExecutor {\n constructor(private readonly storage: DurableObjectStorage) {}\n\n private get sql(): SqlStorage {\n return this.storage.sql;\n }\n\n exec(sql: string): void {\n this.sql.exec(sql);\n }\n\n run(sql: string, params: readonly unknown[] = []): number {\n const cursor = this.sql.exec(sql, ...(params as SqlStorageValue[]));\n return cursor.rowsWritten;\n }\n\n get<T = Record<string, unknown>>(sql: string, params: readonly unknown[] = []): T | undefined {\n const rows = this.sql.exec(sql, ...(params as SqlStorageValue[])).toArray();\n return rows[0] as T | undefined;\n }\n\n all<T = Record<string, unknown>>(sql: string, params: readonly unknown[] = []): T[] {\n return this.sql.exec(sql, ...(params as SqlStorageValue[])).toArray() as T[];\n }\n\n transaction<T>(fn: () => T): T {\n return this.storage.transactionSync(fn);\n }\n}\n","/**\n * Haverstack — Durable Object SQLite Record Adapter\n * -------------------------------------------------------\n * Implements StackRecordAdapter over a Cloudflare Durable Object's SQLite\n * storage (ctx.storage.sql). Full-text search uses FTS5, same as\n * record-adapter-sqlite — DO's SQLite build ships it.\n *\n * Ownership and durability come from the platform, not from anything this\n * class does: a Durable Object id maps to exactly one running instance,\n * so there is no separate lock file the way record-adapter-sqlite needs\n * one for real files (see docs/spec/adapters.md § Concurrency & storage\n * ownership) — the DO *is* the lock. There is likewise no persist/flush\n * step: every write through ctx.storage.sql is durable by the time the\n * call returns, so flush()/close() are no-ops kept only to satisfy the\n * optional StackRecordAdapter methods.\n *\n * This class itself is a thin binding: the one piece of genuinely\n * engine-specific wiring — SqlExecutor.transaction() reaching\n * ctx.storage.transactionSync() instead of raw SQL BEGIN/COMMIT/ROLLBACK,\n * which DO SQLite rejects outright — lives in executor.ts. Everything\n * above storage comes from @haverstack/sqlite-shared's\n * SharedSqlRecordAdapter, exactly as it does for record-adapter-sqlite.\n */\n\nimport {\n applyRecordSchema,\n insertConfigRecord,\n tryReadStackConfig,\n SharedSqlRecordAdapter,\n type StackConfig,\n} from '@haverstack/sqlite-shared/record';\nimport { OwnerMismatchError } from '@haverstack/core/adapter';\nimport { DurableObjectSqliteExecutor } from './executor.js';\n\n// -------------------------------------------------------\n// Types\n// -------------------------------------------------------\n\nexport type DoSQLiteRecordAdapterOpenOptions = {\n /**\n * Entity ID of the stack owner. A plain string is checked against an\n * existing config record (mismatch throws OwnerMismatchError); a lazy\n * provider is called only when the DO's storage has none yet.\n */\n ownerEntityId: string | (() => string | Promise<string>);\n /** IANA timezone string e.g. \"America/New_York\". Optional passthrough app metadata — no default. */\n timezone?: string;\n};\n\n// -------------------------------------------------------\n// DoSQLiteRecordAdapter\n// -------------------------------------------------------\n\nexport class DoSQLiteRecordAdapter extends SharedSqlRecordAdapter {\n private constructor(exec: DurableObjectSqliteExecutor, config: StackConfig) {\n super(exec, config);\n }\n\n /**\n * Open the adapter for a DO instance, creating its storage on first use.\n * A DO id either already has a config record (reattach) or it doesn't\n * (first call — opts.ownerEntityId/timezone become the config), so there\n * is no `create` option. Schema DDL is `CREATE TABLE IF NOT EXISTS`, so\n * running it every call is idempotent and cheap.\n *\n * DO SQLite manages its own durability and rejects PRAGMA journal_mode\n * outright (\"not authorized\") — verified against the real runtime, not\n * assumed — hence `wal: false`, unlike record-adapter-sqlite.\n */\n static async open(\n storage: DurableObjectStorage,\n opts: DoSQLiteRecordAdapterOpenOptions,\n ): Promise<DoSQLiteRecordAdapter> {\n const exec = new DurableObjectSqliteExecutor(storage);\n applyRecordSchema(exec, { wal: false });\n let config = tryReadStackConfig(exec);\n if (config) {\n if (typeof opts.ownerEntityId === 'string' && opts.ownerEntityId !== config.entityId) {\n throw new OwnerMismatchError(\n opts.ownerEntityId,\n config.entityId,\n 'the Durable Object storage',\n );\n }\n } else {\n const ownerEntityId =\n typeof opts.ownerEntityId === 'function' ? await opts.ownerEntityId() : opts.ownerEntityId;\n config = insertConfigRecord(exec, ownerEntityId, opts.timezone);\n }\n return new DoSQLiteRecordAdapter(exec, config);\n }\n\n // -------------------------------------------------------\n // Lifecycle\n // -------------------------------------------------------\n\n /** No-op: every write through ctx.storage.sql is already durable. */\n async flush(): Promise<void> {}\n\n /** No-op: no lock file, no connection to release — the DO's own lifecycle governs storage. */\n async close(): Promise<void> {}\n}\n\nexport { DurableObjectSqliteExecutor } from './executor.js';\n"]}
1
+ {"version":3,"sources":["../../sqlite-shared/src/schema.ts","../../sqlite-shared/src/fts5.ts","../../sqlite-shared/src/executor.ts","../../sqlite-shared/src/config.ts","../../sqlite-shared/src/cursor.ts","../../sqlite-shared/src/query.ts","../../sqlite-shared/src/indexes.ts","../../sqlite-shared/src/mappers.ts","../../sqlite-shared/src/record-logic.ts","../../sqlite-shared/src/record-adapter.ts","../src/executor.ts","../src/index.ts"],"names":["kind","entry","StackBadRequestError","cursor","constantSortEntry","contentSortEntry","page","statement"],"mappings":";;;;AAUO,IAAM,iBAAA,GAAoB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmL1B,IAAM,eAAA,GAAkB;;;;;;;;AAexB,IAAM,sBAAA,GAAyB,CAAA,yBAAA,CAAA;AAwB/B,IAAM,iBAAA,GAAoB,CAAC,IAAA,EAAmB,IAAA,KAAmC;AACtF,EAAA,IAAA,CAAK,KAAK,sBAAsB,CAAA;AAEhC,EAAA,IAAA,CAAK,KAAK,iBAAiB,CAAA;AAC3B,EAAA,IAAA,CAAK,KAAK,eAAe,CAAA;AAC3B,CAAA;;;AClKA,IAAM,yBAAA,GAA4B,4BAAA;AAOlC,IAAM,qBAAqB,CAAC,KAAA,KAA0B,KAAA,CAAM,OAAA,CAAQ,QAAQ,GAAG,CAAA;AAExE,IAAM,iBAAA,GAAoB,CAAC,KAAA,EAAe,QAAA,GAAW,CAAA,KAAa;A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adapters. TOKENS_SCHEMA_SQL\n * is split out from RECORD_SCHEMA_SQL because token storage lives in its\n * own file, never bundled with records — see NativeTokenStore in\n * record-adapter-sqlite and the StackTokenStore portability rationale\n * (docs/spec/wire-format.md § Authentication).\n */\n\nexport const RECORD_SCHEMA_SQL = `\n CREATE TABLE IF NOT EXISTS records (\n id TEXT PRIMARY KEY,\n type_id TEXT NOT NULL,\n created_at INTEGER NOT NULL,\n updated_at INTEGER NOT NULL,\n content TEXT NOT NULL CHECK (json_valid(content)),\n version INTEGER NOT NULL DEFAULT 1,\n parent_id TEXT,\n entity_id TEXT,\n app_id TEXT,\n principal_id TEXT,\n updated_by TEXT,\n updated_via TEXT,\n deleted_at INTEGER,\n unlisted_at INTEGER\n ) STRICT;\n\n -- Every edge a record carries, authority and data alike: a permission\n -- has element identity and a delta shaped like a tag's, so it earns the\n -- same table. The permissions and associations fields are projections\n -- over it, partitioned by kind.\n -- See docs/spec/access-control.md § Record-level permissions.\n CREATE TABLE IF NOT EXISTS associations (\n record_id TEXT NOT NULL REFERENCES records(id),\n kind TEXT NOT NULL CHECK (kind IN ('tag', 'attachment', 'relationship', 'permission', 'anyone')),\n label TEXT NOT NULL,\n file_id TEXT NOT NULL DEFAULT '',\n -- A relationship target is (related_scope, related_id) qualified by\n -- related_ns (a foreign namespace) or related_stack (another stack); a\n -- permission's grantee reuses scope and id. All are in the key, so\n -- targets differing only by a qualifier are distinct associations.\n related_scope TEXT NOT NULL DEFAULT '' CHECK (related_scope IN ('', 'record', 'entity', 'external', 'group')),\n related_id TEXT NOT NULL DEFAULT '',\n related_ns TEXT NOT NULL DEFAULT '',\n related_stack TEXT NOT NULL DEFAULT '',\n -- Set only by a group grantee; in the key because member and admin name\n -- two different sets of people.\n related_role TEXT NOT NULL DEFAULT '' CHECK (related_role IN ('', 'member', 'admin')),\n -- Out of the key: it annotates a reference rather than naming one, so\n -- re-pointing it updates the existing row. See docs/spec/attachments.md\n -- § Naming the upload a reference came from.\n attachment_record_id TEXT NOT NULL DEFAULT '',\n -- Leads with record_id, so its implicit index serves every by-record\n -- read and delete; the indexes below cover only the filter directions.\n PRIMARY KEY (record_id, kind, label, file_id, related_scope, related_id, related_ns, related_stack, related_role)\n ) STRICT;\n\n -- Content and the type it is read under. No parent_id, unlisted_at or\n -- associations column: none of those aspects bumps version, so no\n -- version ever snapshots one. See\n -- docs/spec/versioning.md § Version history.\n CREATE TABLE IF NOT EXISTS versions (\n record_id TEXT NOT NULL REFERENCES records(id),\n version INTEGER NOT NULL,\n type_id TEXT NOT NULL,\n content TEXT NOT NULL CHECK (json_valid(content)),\n updated_at INTEGER NOT NULL,\n entity_id TEXT,\n principal_id TEXT,\n updated_by TEXT,\n updated_via TEXT,\n PRIMARY KEY (record_id, version)\n ) STRICT;\n\n -- The durable half of a change event: which aspects moved, who moved\n -- them, and the association deltas nothing else retains. Envelope-level\n -- by design — content lives on a versions row, and copying it here\n -- would make the journal the larger of the two stores.\n -- See docs/spec/journal.md.\n CREATE TABLE IF NOT EXISTS journal (\n record_id TEXT NOT NULL REFERENCES records(id),\n seq INTEGER NOT NULL,\n at INTEGER NOT NULL,\n -- No 'purged': a purge destroys the log rather than appending to it,\n -- so the constraint names exactly the kinds an entry can carry.\n -- See docs/spec/journal.md § A purge destroys the journal.\n kind TEXT NOT NULL CHECK (kind IN ('created', 'changed', 'removed')),\n ops TEXT NOT NULL CHECK (json_valid(ops)),\n version INTEGER NOT NULL,\n type_id TEXT NOT NULL,\n parent_id TEXT,\n -- NULL means the entry names no origin (the record did not move);\n -- '' means it moved off the root. parent_id above needs no such\n -- spelling: it is state, where this is a key that may be absent --\n -- the same input/state split parentId follows everywhere.\n previous_parent_id TEXT,\n actor TEXT CHECK (actor IS NULL OR json_valid(actor)),\n -- One tagged edit per association the write moved, each carrying the\n -- state it displaced. See docs/spec/journal.md § The entry.\n associations TEXT CHECK (associations IS NULL OR json_valid(associations)),\n PRIMARY KEY (record_id, seq)\n ) STRICT;\n\n CREATE TABLE IF NOT EXISTS types (\n id TEXT PRIMARY KEY,\n base_id TEXT NOT NULL,\n version INTEGER NOT NULL,\n name TEXT NOT NULL,\n schema TEXT NOT NULL CHECK (json_valid(schema)),\n schema_hash TEXT NOT NULL,\n migrates_from TEXT,\n created_at INTEGER NOT NULL\n ) STRICT;\n\n -- One row per top-level scalar content field a record holds a value at:\n -- what it orders as, and the file it references. See\n -- docs/spec/data-model.md § Sorting by a content field.\n --\n -- value_rank is 0 for a numeric value and 1 for a text one — the same\n -- split as \"num_value IS NULL\", but non-null, so one index can carry\n -- the whole ordering. file_id is set only for a 'file-ref' field.\n CREATE TABLE IF NOT EXISTS content_index (\n record_id TEXT NOT NULL REFERENCES records(id),\n field TEXT NOT NULL,\n value_rank INTEGER NOT NULL CHECK (value_rank IN (0, 1)),\n num_value REAL,\n text_key TEXT,\n text_value TEXT,\n file_id TEXT,\n PRIMARY KEY (record_id, field)\n ) STRICT;\n\n -- Each filter column continues into (created_at, id), the default order\n -- and its tiebreak: these columns are low-cardinality, and a narrow index\n -- leaves the planner sorting every match before it can honor LIMIT.\n -- deleted_at and unlisted_at get none: an index on a column every query\n -- tests IS NULL only lures the planner off the one answering ORDER BY.\n CREATE INDEX IF NOT EXISTS idx_records_type_id ON records(type_id, created_at, id);\n CREATE INDEX IF NOT EXISTS idx_records_parent_id ON records(parent_id, created_at, id);\n CREATE INDEX IF NOT EXISTS idx_records_entity_id ON records(entity_id, created_at, id);\n CREATE INDEX IF NOT EXISTS idx_records_app_id ON records(app_id, created_at, id);\n CREATE INDEX IF NOT EXISTS idx_records_principal_id ON records(principal_id, created_at, id);\n CREATE INDEX IF NOT EXISTS idx_records_created_at ON records(created_at, id);\n CREATE INDEX IF NOT EXISTS idx_records_updated_at ON records(updated_at, id);\n CREATE INDEX IF NOT EXISTS idx_records_version ON records(version, id);\n\n -- Each association index ends with record_id so the semi-joins in\n -- query.ts read the matching record ids out of the index alone.\n -- idx_assoc_related carries related_ns ahead of related_id because a\n -- record- or entity-scoped target always stores '' there, which\n -- buildQueryPlan states explicitly to keep the prefix contiguous.\n CREATE INDEX IF NOT EXISTS idx_assoc_kind_label ON associations(kind, label, record_id);\n CREATE INDEX IF NOT EXISTS idx_assoc_kind_file_id ON associations(kind, file_id, record_id);\n CREATE INDEX IF NOT EXISTS idx_assoc_related ON associations(kind, related_scope, related_ns, related_id, record_id);\n CREATE INDEX IF NOT EXISTS idx_types_base_id ON types(base_id, version);\n\n -- The whole content ordering, in index order, for one field at a time:\n -- a content-sorted page is an ordered walk of a slice of this index.\n CREATE INDEX IF NOT EXISTS idx_content_index_sort\n ON content_index(field, value_rank, num_value, text_key, text_value, record_id);\n CREATE INDEX IF NOT EXISTS idx_content_index_file\n ON content_index(file_id, record_id) WHERE file_id IS NOT NULL;\n`;\n\n/**\n * Bearer-token storage backing StackTokenStore. Kept separate from\n * RECORD_SCHEMA_SQL so an adapter can put it in its own file — the\n * portable stack file shouldn't also carry a server's auth material.\n */\nexport const TOKENS_SCHEMA_SQL = `\n CREATE TABLE IF NOT EXISTS tokens (\n id TEXT PRIMARY KEY,\n token_hash TEXT NOT NULL UNIQUE,\n principal_id TEXT NOT NULL,\n subject_id TEXT NOT NULL,\n label TEXT,\n created_at INTEGER NOT NULL,\n expires_at INTEGER\n ) STRICT;\n`;\n\n/**\n * The full-text index behind `StackQuery.filter.search` (see indexes.ts).\n * What `columnsize=0` costs a reader is in docs/spec/adapters.md\n * § SQLite-backed adapters. `detail` stays at its default: sanitizeFts5Query\n * passes phrase queries through, and a phrase needs the token positions\n * only that setting records.\n */\nexport const FTS5_SCHEMA_SQL = `\n CREATE VIRTUAL TABLE IF NOT EXISTS records_fts USING fts5(\n content,\n content='records',\n content_rowid='rowid',\n columnsize=0\n );\n`;\n\n/**\n * Enforces the REFERENCES constraints declared above (off by default per\n * SQLite connection), so touching a nonexistent record fails loudly\n * instead of creating an orphan row. Adapters map the constraint\n * violation to StackNotFoundError at the call sites that can trigger it.\n */\nexport const PRAGMA_FOREIGN_KEYS_ON = `PRAGMA foreign_keys = ON;`;\n\n/**\n * WAL journaling: page-level writes, crash-safe without our own\n * temp-file-and-rename dance, and real SQLite file locking. Only\n * meaningful for a real file (a :memory: database silently ignores it).\n */\nexport const PRAGMA_JOURNAL_MODE_WAL = `PRAGMA journal_mode = WAL;`;\n\nexport type RecordSchemaOptions = {\n /**\n * Whether to put the connection in WAL mode. False for an engine that\n * owns its own durability and rejects the pragma outright, as Durable\n * Object storage does.\n */\n wal: boolean;\n};\n\n/**\n * Brings a records database up to the schema every SQLite record adapter\n * expects: foreign keys on, then the tables and the FTS5 index. Every\n * statement is idempotent, so an engine that cannot tell\n * whether a store exists can run this on each attach.\n */\nexport const applyRecordSchema = (exec: SqlExecutor, opts: RecordSchemaOptions): void => {\n exec.exec(PRAGMA_FOREIGN_KEYS_ON);\n if (opts.wal) exec.exec(PRAGMA_JOURNAL_MODE_WAL);\n exec.exec(RECORD_SCHEMA_SQL);\n exec.exec(FTS5_SCHEMA_SQL);\n};\n","/**\n * FTS5 query sanitization: turning what a person typed into a search box\n * into a query FTS5 will parse. Indexing lives in indexes.ts.\n *\n * FTS5's query language supports operators (AND/OR/NOT, phrases, NEAR,\n * wildcards) that can be expensive or cause parse errors with untrusted\n * input, so a search string is rewritten before it reaches the engine:\n *\n * kept: AND/OR/NOT (with a left operand), phrase queries (\"…\"), implicit AND\n * removed: wildcards (*), NEAR(...) (terms kept, wrapper dropped), every\n * character outside the allow-list, bare NOT (no left operand),\n * operators with no right operand\n * capped: parenthesis nesting depth (default: 2)\n * closed: an odd trailing quote\n *\n * Two FTS5 grammar details drive the less obvious rules:\n *\n * - FTS5's NEAR is a function-call form — `NEAR(a b, 10)`. Deleting the\n * whole span would also delete the terms inside it, which can leave a\n * dangling operator when NEAR(...) sits next to AND/OR (e.g. \"x AND\n * NEAR(a b, 5)\" -> \"x AND\" — a syntax error). So this keeps the terms\n * and drops only the NEAR(...) wrapper and distance.\n * - FTS5 has real column-filter syntax and errors hard on any name but\n * the table's actual columns. It is wider than `colname:term`: `-name`\n * and `{a b}` filter columns with no colon at all, so an ordinary\n * hyphenated word is read as one. Since records_fts has exactly one\n * column and callers have no business targeting columns explicitly,\n * everything outside a phrase is reduced to an allow-list rather than\n * validated — see OUTSIDE_PHRASE_DISALLOWED.\n *\n * The rewrite is best-effort and makes no completeness claim against\n * FTS5's grammar — which is why the caller wraps execution as well, so a\n * case this misses surfaces as a StackBadRequestError rather than a raw engine\n * error. Search text is what a person typed into a box: an unbalanced\n * quote or a trailing \"AND\" is ordinary input, not a malformed request.\n *\n * What this bounds is the grammar, not the cost: a syntactically modest\n * search over a large index can still be expensive, and node:sqlite blocks\n * the calling thread, so there is no timeout to set from inside the call.\n * Bounding execution time belongs to whoever drives the engine under load\n * — see docs/spec/wire-format.md § Bounding query cost for the server-side\n * expectation and the `timeout` wire error that goes with it.\n *\n * Backtracking. The input is untrusted text, so every regex here is\n * written not to backtrack quadratically on it:\n *\n * - Whitespace runs are collapsed before any rule that spells \"beside an\n * operator or paren\" as `\\s*`; a `\\s*` facing a long run re-tries the\n * whole run per starting position. The rewrite is still a little\n * worse than linear overall, so bounding a search's length belongs to\n * whoever accepts it.\n * - The NEAR rule has no `\\s*` after its paren (its `[^,()]*` already\n * takes a space, and two quantifiers matching the same character give\n * the engine an ambiguous split), and neither of its runs crosses a\n * `(`, so an unclosed `NEAR(NEAR(NEAR(` doesn't re-scan to the end once\n * per occurrence.\n * - The no-right-operand rule has no leading `\\s*`, for the same reason\n * as the collapse.\n * - The operand-before-paren rule's lookbehind pins each match to the\n * start of a word; without it every position inside a long word is a\n * start, each re-scanning the rest of it.\n * - Empty phrases and empty paren pairs are removed by one rule, so a run\n * of them becomes one space rather than one per construct.\n */\n\n/**\n * Outside a phrase, everything but a letter, digit, mark, underscore,\n * whitespace, paren or quote becomes a separator. An allow-list because\n * FTS5's column-filter syntax is wider than any list of metacharacters.\n * See docs/spec/data-model.md § Capability-gated filters.\n */\nconst OUTSIDE_PHRASE_DISALLOWED = /[^\\p{L}\\p{N}\\p{M}_\\s()\"]/gu;\n\n/**\n * Bounds what any `\\s*` can backtrack over (see Backtracking above). Safe\n * inside a phrase too: FTS5 matches a phrase as a sequence of tokens, not\n * the spacing between them.\n */\nconst collapseWhitespace = (query: string): string => query.replace(/\\s+/g, ' ');\n\nexport const sanitizeFts5Query = (query: string, maxDepth = 2): string => {\n if (!query) return '';\n\n // Drop control characters, inside a phrase as well as outside. A NUL in\n // particular ends the string early for SQLite's C API, so the engine sees\n // an opening quote whose closing one is past the truncation and reports an\n // unterminated string; none of them can match an indexed token anyway.\n let clean = query.replace(/[\\p{Cc}\\p{Cf}]/gu, ' ');\n\n clean = clean.replace(/\\*/g, '');\n\n // NEAR(terms, distance) keeps its terms — see the note above. The\n // pattern's shape is about backtracking (see Backtracking); excluding `(`\n // costs nothing, since a NEAR's terms and distance never hold one.\n clean = clean.replace(/\\bNEAR\\s*\\(([^,()]*)(?:,[^()]*)?\\)/gi, '$1');\n\n // A NEAR( with no closing paren isn't the function-call form the rule\n // above rewrites, and the paren auto-close below would hand FTS5 a NEAR\n // call it never meant to write. Keep the grouping, drop the keyword.\n clean = clean.replace(/\\bNEAR\\s*\\(/gi, '(');\n\n // See OUTSIDE_PHRASE_DISALLOWED.\n clean = clean.replace(/\"[^\"]*\"|[^\"]+/g, (span) =>\n span.startsWith('\"') ? span : span.replace(OUTSIDE_PHRASE_DISALLOWED, ' '),\n );\n\n // The reduction above is where long whitespace runs come from.\n clean = collapseWhitespace(clean);\n\n // Strip bare NOT with no left operand — FTS5 requires \"term NOT term\", not\n // \"NOT term\". A phrase is matched first and kept whole, so a NOT inside one\n // stays the word it is; the closing quote is optional because an odd one\n // is only closed further down.\n clean = clean.replace(/\"[^\"]*\"?|(^|\\(\\s*)NOT\\s+/gi, (m, lead?: string) => lead ?? m);\n\n // Enforce max nesting depth; replace excess ( with a space and discard\n // unmatched ). Parens inside a phrase are literal text to FTS5, so the\n // depth count steps over quoted spans rather than reading them as syntax.\n let currentDepth = 0;\n let inPhrase = false;\n let result = '';\n for (const char of clean) {\n if (char === '\"') {\n inPhrase = !inPhrase;\n result += char;\n } else if (inPhrase) {\n result += char;\n } else if (char === '(') {\n if (currentDepth < maxDepth) {\n currentDepth++;\n result += char;\n } else result += ' ';\n } else if (char === ')') {\n if (currentDepth > 0) {\n currentDepth--;\n result += char;\n } else result += ' ';\n } else {\n result += char;\n }\n }\n\n // Close an odd trailing quote before the parens, so the added ) lands\n // outside the phrase rather than becoming a literal character in it.\n if (inPhrase) result += '\"';\n\n if (currentDepth > 0) result += ')'.repeat(currentDepth);\n\n // Looped to a fixpoint because each rule can create work for another:\n // dropping an operator can empty a pair (\"(a AND)\"), dropping a pair can\n // strand an operator (\"x AND ()\"), and an empty phrase's quotes can\n // re-pair once text is inserted beside it.\n let prev: string;\n do {\n prev = result;\n // Empty phrases and empty paren pairs match nothing. One rule for both\n // (see Backtracking).\n result = result.replace(/(?:\"\"|\\(\\s*\\))+/g, ' ');\n result = dropDanglingOperators(result);\n result = restoreImplicitAnd(result);\n } while (result !== prev);\n\n return collapseWhitespace(result).replace(/\\(\\s+/g, '(').replace(/\\s+\\)/g, ')').trim();\n};\n\n/** An operator FTS5 requires a term on both sides of. */\nconst OPERATOR = String.raw`(?:AND|OR|NOT)`;\n\n/**\n * Stands in for a phrase while operators are rewritten. A private-use\n * character is not a word character, so `\\b` still sees an operator beside\n * it, while the anchors see an operand. A literal U+E000 in the input is\n * one FTS5 tokenizes away regardless.\n */\nconst PHRASE_HOLE = '\\u{E000}';\n\n/**\n * Drop operators left without an operand — typed on the way to a longer\n * query (\"cats AND\"), or left behind once a rule above removed a term.\n * FTS5 rejects each outright. Phrases are masked: inside one, an operator\n * is a word the user searched for.\n */\nconst dropDanglingOperators = (query: string): string =>\n withPhrasesMasked(query, (masked) => {\n let out = masked;\n let prev: string;\n do {\n prev = out;\n // A run of operators keeps only the first — the rest have no left operand.\n out = out.replace(\n new RegExp(String.raw`\\b(${OPERATOR})\\b(?:\\s+\\b${OPERATOR}\\b)+`, 'gi'),\n '$1',\n );\n // No left operand: at the start of the query or straight after \"(\".\n out = out.replace(new RegExp(String.raw`(^|\\()\\s*\\b${OPERATOR}\\b\\s*`, 'gi'), '$1');\n // No right operand: at the end of the query or straight before \")\".\n // No leading `\\s*` (see Backtracking); the collapse on the way out\n // takes that whitespace.\n out = out.replace(new RegExp(String.raw`\\b${OPERATOR}\\b\\s*($|\\))`, 'gi'), '$1');\n } while (out !== prev);\n return out;\n });\n\n/**\n * Run `rewrite` over the query with each phrase swapped for PHRASE_HOLE,\n * then put the phrases back in order. Every rule that reasons about what\n * sits beside an operator or a paren needs this: inside a phrase the text\n * is literal, and from outside one the phrase is a single operand.\n */\nconst withPhrasesMasked = (query: string, rewrite: (masked: string) => string): string => {\n const phrases: string[] = [];\n const masked = query.replace(/\"[^\"]*\"/g, (phrase) => {\n phrases.push(phrase);\n return PHRASE_HOLE;\n });\n let i = 0;\n return rewrite(masked).replace(new RegExp(PHRASE_HOLE, 'g'), () => phrases[i++]);\n};\n\nconst OPERAND_CHAR = String.raw`[\\p{L}\\p{N}_${PHRASE_HOLE}]`;\n\n/** A whole word, never a prefix: the `D` of `AND` is not a term. */\nconst isOperator = (word: string): boolean => /^(?:AND|OR|NOT)$/i.test(word);\n\n/**\n * Put back the operator FTS5 requires beside a group: `cats dogs` is an\n * implicit AND, but `cats (dogs)` is a syntax error. A rewrite that strips\n * the operator beside a group keeps the grouping, so this writes back the\n * AND that was meant. A masked phrase is an operand like any other.\n */\nconst restoreImplicitAnd = (query: string): string =>\n withPhrasesMasked(query, (masked) =>\n masked\n // \")\" then an operand — \") cats\", \") (\", \") <phrase>\" — unless that\n // operand is an operator, which already joins the two sides.\n .replace(\n new RegExp(String.raw`\\)\\s*(?=${OPERAND_CHAR}|\\()`, 'gu'),\n (match, offset: number, whole: string) => {\n const next = /^\\s*([\\p{L}\\p{N}_]+)/u.exec(whole.slice(offset + match.length));\n return next && isOperator(next[1]) ? match : ') AND ';\n },\n )\n // An operand then \"(\", unless the operand is an operator. The\n // lookbehind is load-bearing (see Backtracking).\n .replace(\n new RegExp(String.raw`(?<!${OPERAND_CHAR})(${OPERAND_CHAR}+)\\s*(?=\\()`, 'gu'),\n (match, word: string) => (isOperator(word) ? match : `${word} AND `),\n ),\n );\n","/**\n * Normalizes a SQLite binding's call convention (array-bind-then-step,\n * spread-args run/get/all, cursor-returning exec) behind one interface,\n * so the actual CRUD/query/version/type/association/token logic can live\n * once in this package instead of being re-typed per engine. Each adapter\n * package provides a thin SqlExecutor implementation over its own\n * database handle.\n *\n * Deliberately synchronous: every binding this targets executes queries\n * in-process without yielding, and an async interface would force the\n * shared logic's transaction sequences to interleave.\n */\nexport interface SqlExecutor {\n /** Run DDL, pragmas, or any statement(s) with no bound params and no results needed. */\n exec(sql: string): void;\n /** Run a single parameterized statement. Returns the number of rows affected (INSERT/UPDATE/DELETE). */\n run(sql: string, params?: readonly unknown[]): number;\n /** Run a parameterized statement and return the first result row, or undefined. */\n get<T = Record<string, unknown>>(sql: string, params?: readonly unknown[]): T | undefined;\n /** Run a parameterized statement and return all result rows. */\n all<T = Record<string, unknown>>(sql: string, params?: readonly unknown[]): T[];\n /**\n * Run `fn` atomically: every statement it issues commits, or none do if it\n * throws. A callback rather than BEGIN/COMMIT text because some engines\n * only offer one (a Durable Object's storage.transactionSync); for the\n * same reason `fn` must be synchronous and use only this executor.\n */\n transaction<T>(fn: () => T): T;\n}\n\n/** SQLite reports FK violations with this exact message; verify it holds when adding an engine. */\nexport const isForeignKeyViolation = (err: unknown): boolean =>\n err instanceof Error && err.message.includes('FOREIGN KEY constraint failed');\n\n/** SQLite reports a PRIMARY KEY/UNIQUE collision with this exact message. */\nexport const isUniqueConstraintViolation = (err: unknown): boolean =>\n err instanceof Error && err.message.includes('UNIQUE constraint failed');\n","import type { SqlExecutor } from './executor.js';\n\n/** timezone is optional passthrough app metadata — no default. */\nexport type StackConfig = { entityId: string; timezone: string | undefined };\n\n/** The one place the singleton record's id is spelled. */\nconst CONFIG_ROW_SQL = `SELECT content FROM records WHERE id = '_config'`;\n\n/**\n * Inserts the singleton _config@1 record and returns what it wrote, so a\n * caller that has just created a database reaches the same StackConfig an\n * existing one yields without a read-back. Only valid on a freshly-created\n * database.\n */\nexport const insertConfigRecord = (\n exec: SqlExecutor,\n entityId: string,\n timezone: string | undefined,\n): StackConfig => {\n const now = Date.now();\n exec.run(\n `INSERT INTO records (id, type_id, created_at, updated_at, content, version)\n VALUES ('_config', '_config@1', ?, ?, ?, 1)`,\n [now, now, JSON.stringify({ entityId, timezone })],\n );\n return { entityId, timezone };\n};\n\n/**\n * Reads the singleton _config@1 record, or null if this database has none.\n * Null is the signal an engine that cannot tell from the outside whether a\n * store exists (a Durable Object) uses to decide between reattaching and creating.\n */\nexport const tryReadStackConfig = (exec: SqlExecutor): StackConfig | null => {\n const row = exec.get<{ content: string }>(CONFIG_ROW_SQL);\n if (!row) return null;\n const content = JSON.parse(row.content) as { entityId: string; timezone?: string };\n return { entityId: content.entityId, timezone: content.timezone };\n};\n\n/** Reads the singleton _config@1 record. Throws if the database is missing it. */\nexport const readStackConfig = (exec: SqlExecutor): StackConfig => {\n const config = tryReadStackConfig(exec);\n if (!config) throw new Error('Stack database is missing its config record.');\n return config;\n};\n","/**\n * Cursor encode/decode for keyset pagination. The payload is JSON in\n * base64 rather than delimited fields: a content sort carries the field's\n * stored text, and neither a field name nor a text value is constrained\n * to avoid whatever character a delimiter would claim. Uses btoa/atob\n * with an explicit UTF-8 step rather than Node's Buffer, so the codec\n * stays runtime-agnostic and a non-Latin-1 value survives the round trip.\n */\n\nimport { NATIVE_SORT_FIELDS, StackBadRequestError } from '@haverstack/core';\nimport { constantSortEntry, contentSortEntry } from '@haverstack/core/adapter';\nimport type { SortEntry } from '@haverstack/core/adapter';\nimport type {\n ContentSortKey,\n StackRecord,\n NativeSortField,\n QuerySort,\n ScalarFieldKind,\n} from '@haverstack/core';\n\n/**\n * Which partition of the ordering a cursor sits in: a native column or a\n * content field's number, text, or absent value. The three content cases\n * are the boundary keyset pagination has to hold — a page that ended on\n * the last numeric value resumes at the first text one, not back at the\n * top of either.\n */\nexport type CursorKind = 'native' | 'num' | 'text' | 'absent';\n\nexport type DecodedCursor =\n | { kind: 'native'; field: NativeSortField; value: number; id: string }\n | { kind: 'num'; field: string; value: number; id: string }\n | { kind: 'text'; field: string; value: string; id: string }\n | { kind: 'absent'; field: string; id: string };\n\nconst toBase64 = (text: string): string => {\n const bytes = new TextEncoder().encode(text);\n let binary = '';\n for (const byte of bytes) binary += String.fromCharCode(byte);\n return btoa(binary);\n};\n\nconst fromBase64 = (cursor: string): string => {\n const binary = atob(cursor);\n const bytes = new Uint8Array(binary.length);\n for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);\n return new TextDecoder().decode(bytes);\n};\n\nexport const encodeCursor = (decoded: DecodedCursor): string =>\n toBase64(\n JSON.stringify(\n decoded.kind === 'absent'\n ? { k: decoded.kind, f: decoded.field, i: decoded.id }\n : { k: decoded.kind, f: decoded.field, i: decoded.id, v: decoded.value },\n ),\n );\n\n/** Throws StackBadRequestError for any malformed, corrupt, or unrecognized cursor. */\nexport const decodeCursor = (cursor: string): DecodedCursor => {\n const malformed = () => new StackBadRequestError(`Invalid cursor: malformed \"${cursor}\"`);\n let parsed: unknown;\n try {\n parsed = JSON.parse(fromBase64(cursor));\n } catch {\n throw malformed();\n }\n if (!parsed || typeof parsed !== 'object') throw malformed();\n const { k, f, i, v } = parsed as Record<string, unknown>;\n if (typeof f !== 'string' || !f || typeof i !== 'string') throw malformed();\n\n if (k === 'absent') return { kind: 'absent', field: f, id: i };\n if (k === 'text') {\n if (typeof v !== 'string') throw malformed();\n return { kind: 'text', field: f, value: v, id: i };\n }\n if (k === 'num' || k === 'native') {\n if (typeof v !== 'number' || !isFinite(v)) {\n throw new StackBadRequestError(`Invalid cursor: non-numeric sort value`);\n }\n if (k === 'native') {\n if (!NATIVE_SORT_FIELDS.includes(f as NativeSortField)) {\n throw new StackBadRequestError(`Invalid cursor: unknown sort field \"${f}\"`);\n }\n return { kind: 'native', field: f as NativeSortField, value: v, id: i };\n }\n return { kind: 'num', field: f, value: v, id: i };\n }\n throw malformed();\n};\n\n/**\n * The name a cursor carries for a `contentFieldByType` sort: the map\n * itself, keys in order, so a cursor resumes only the map it was minted\n * under.\n */\nexport const byTypeDescriptor = (byType: Record<string, ContentSortKey>): string =>\n JSON.stringify(Object.entries(byType).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)));\n\nexport const getSortField = (query: { sort?: QuerySort }): NativeSortField =>\n query.sort?.field ?? 'createdAt';\n\nexport const getSortColumn = (field: NativeSortField): string =>\n field === 'createdAt' ? 'created_at' : field === 'updatedAt' ? 'updated_at' : 'version';\n\n/**\n * The cursor naming the last record of a page: where the next page\n * resumes. A content sort re-derives the record's ordered value from its\n * stored content rather than reading it back out of the index, so the\n * cursor and the index can only ever disagree if the write path is\n * broken — in which case pagination is not the thing to paper over.\n */\nexport const makeCursor = (\n record: StackRecord,\n sort: QuerySort | undefined,\n kindOf?: (record: StackRecord, field: string) => ScalarFieldKind | undefined,\n): string => {\n const byType = sort?.contentFieldByType;\n if (byType) {\n const field = byTypeDescriptor(byType);\n const key = Object.hasOwn(byType, record.typeId) ? byType[record.typeId] : undefined;\n const kind = key?.field === undefined ? undefined : kindOf?.(record, key.field);\n const entry =\n key?.field !== undefined\n ? kind && contentSortEntry(kind, record.content[key.field])\n : key && constantSortEntry(key.value ?? null);\n return encodeEntry(field, entry || null, record.id);\n }\n const contentField = sort?.contentField;\n if (contentField === undefined) {\n const field = getSortField({ sort });\n const value =\n field === 'updatedAt'\n ? record.updatedAt.getTime()\n : field === 'version'\n ? record.version\n : record.createdAt.getTime();\n return encodeCursor({ kind: 'native', field, value, id: record.id });\n }\n\n const kind = kindOf?.(record, contentField);\n const entry = kind ? contentSortEntry(kind, record.content[contentField]) : null;\n return encodeEntry(contentField, entry, record.id);\n};\n\nconst encodeEntry = (field: string, entry: SortEntry | null, id: string): string => {\n if (!entry) return encodeCursor({ kind: 'absent', field, id });\n return entry.kind === 'num'\n ? encodeCursor({ kind: 'num', field, value: entry.num, id })\n : encodeCursor({ kind: 'text', field, value: entry.text, id });\n};\n","/**\n * StackQuery -> SQL. Engine-independent — shared verbatim between\n * SQLite-backed record adapters. The one caveat is full-text search:\n * `f.search` here assumes a `records_fts` virtual table with a MATCH-able\n * `content` column (see FTS5_SCHEMA_SQL in schema.ts).\n *\n * A query compiles to a plan rather than to a single statement, because a\n * content-field sort is two orderings end to end: the records that hold a\n * value at the field, ordered by it, then the records that hold none,\n * ordered by id. Reading them as two statements lets each one walk an\n * index in order and stop at the page boundary; the LEFT JOIN that would\n * express the same result in one statement can only be sorted after the\n * fact, which costs a full pass over the table for every page.\n */\n\nimport {\n StackBadRequestError,\n type ContentArm,\n type ContentSortKey,\n type NativeSortField,\n type StackQuery,\n} from '@haverstack/core';\nimport { constantSortEntry, contentSortKey, parseContentFilterKey } from '@haverstack/core/adapter';\nimport type { SortEntry } from '@haverstack/core/adapter';\nimport {\n byTypeDescriptor,\n decodeCursor,\n getSortColumn,\n getSortField,\n type DecodedCursor,\n} from './cursor.js';\nimport { sanitizeFts5Query } from './fts5.js';\n\n/** One statement plus the parameters it binds, in textual order. */\nexport type QueryStatement = { sql: string; params: unknown[] };\n\n/**\n * A page statement with the row budget its trailing `LIMIT ?` binds.\n * buildQueryPlan leaves that parameter off, so the budget is never one of\n * the parameters the query itself bound: the second partition of a\n * content sort asks for what the first left of the page rather than for a\n * page it then discards.\n */\nexport const atBudget = (page: QueryStatement, rows: number): QueryStatement => ({\n sql: page.sql,\n params: [...page.params, rows],\n});\n\n/**\n * Reduce `sort.direction` to a keyword safe to interpolate. Core's\n * assertValidSort() already checks it, but this is where it reaches SQL\n * text, so an unchecked value here would be an injection sink. Core\n * resolves the default direction before any adapter sees the query.\n */\nconst sqlDirection = (query: StackQuery): 'ASC' | 'DESC' => {\n // No sort at all is the unsorted default; a sort always carries its direction.\n const dir = query.sort ? query.sort.direction : 'desc';\n if (dir !== 'asc' && dir !== 'desc') {\n throw new StackBadRequestError(`Invalid sort direction \"${dir}\": expected \"asc\" or \"desc\".`);\n }\n return dir === 'asc' ? 'ASC' : 'DESC';\n};\n\n/**\n * Normalize a json_each row's value so one array element and one bare\n * value are walked by the same clause: an array stands for its elements,\n * anything else for itself. See docs/spec/data-model.md § Nested content\n * paths.\n */\nconst spread = (alias: string): string =>\n `CASE WHEN ${alias}.type = 'array' THEN ${alias}.value ` +\n `WHEN ${alias}.type = 'object' THEN json_array(json(${alias}.value)) ` +\n `ELSE json_array(${alias}.value) END`;\n\n/**\n * The members of a json_each row that is an object, and none for a row\n * that is not — a path descending through a scalar reaches no value rather\n * than reaching an error.\n */\nconst members = (alias: string): string =>\n `CASE WHEN ${alias}.type = 'object' THEN ${alias}.value ELSE '{}' END`;\n\n/**\n * True when some value reached by `segments` satisfies `leaf`, applied to\n * the alias holding that value. Each segment is a bound parameter matched\n * against json_each's `key`, never interpolated into a path expression, so\n * no key can be read as syntax. See docs/spec/data-model.md § Filter.\n */\nconst contentPathExists = (\n segments: string[],\n leaf: (alias: string) => string,\n params: unknown[],\n nextAlias: () => string,\n): string => {\n // The walk is a flat join list, not a subquery per segment: both SQLite\n // builds cap expression-tree depth far below the segment cap, and\n // nesting spends that budget while a join list spends the join budget\n // the cap is actually sized against.\n let key = nextAlias();\n let value = nextAlias();\n const from = [`json_each(r.content) AS ${key}`, `json_each(${spread(key)}) AS ${value}`];\n const where = [`${key}.key = ?`];\n params.push(segments[0]);\n\n for (const segment of segments.slice(1)) {\n key = nextAlias();\n from.push(`json_each(${members(value)}) AS ${key}`);\n value = nextAlias();\n from.push(`json_each(${spread(key)}) AS ${value}`);\n where.push(`${key}.key = ?`);\n params.push(segment);\n }\n\n where.push(leaf(value));\n return `EXISTS (SELECT 1 FROM ${from.join(', ')} WHERE ${where.join(' AND ')})`;\n};\n\n/**\n * A scalar filter value never matches an object or array stored at the\n * path: json_each exposes those as their JSON text, which would compare\n * equal to a string spelling the same document while every non-SQL adapter\n * compares the value itself.\n */\nconst equalsLeaf = (alias: string): string =>\n `${alias}.type NOT IN ('object', 'array') AND ${alias}.value = ?`;\n\n/** The join alias the content index is read through. */\nconst SORT_ALIAS = 'cs';\n\n/**\n * A cursor names a position in one ordering; carrying it into another\n * would silently resume somewhere arbitrary. The partition matters as\n * much as the field name — a cursor from a numeric page can't be read\n * against text values — so each ordering below accepts only the cursors\n * it can read, and rejecting the rest is what narrows the cursor to them.\n */\nconst cursorMismatch = (cursor: DecodedCursor, field: string): StackBadRequestError =>\n new StackBadRequestError(\n `Cursor sort field \"${cursor.field}\" does not match query sort field \"${field}\"`,\n );\n\nconst nativeCursor = (cursor: DecodedCursor, field: NativeSortField) => {\n if (cursor.kind !== 'native' || cursor.field !== field) throw cursorMismatch(cursor, field);\n return cursor;\n};\n\nconst contentCursor = (cursor: DecodedCursor, field: string) => {\n if (cursor.kind === 'native' || cursor.field !== field) throw cursorMismatch(cursor, field);\n return cursor;\n};\n\n/**\n * A `content` and `contentPresent` predicate as conditions on `r`, binding\n * in textual order. A dot-separated path is matched element-wise through\n * arrays, and a `null` value means \"no value at the path, or a value that\n * is null\" (docs/spec/data-model.md § Filter): the second arm below, \"some\n * value there is null\" OR \"nothing is there at all\".\n */\nconst contentConditions = (\n predicate: ContentArm,\n params: unknown[],\n nextAlias: () => string,\n): string[] => {\n const conditions: string[] = [];\n for (const [key, value] of Object.entries(predicate.content ?? {})) {\n const segments = parseContentFilterKey(key);\n // undefined is the same absence null names — JSON drops it on the\n // way over the wire, so treating it as a value would make an\n // in-process query and its wire equivalent disagree.\n if (value === null || value === undefined) {\n const isNull = contentPathExists(segments, (a) => `${a}.value IS NULL`, params, nextAlias);\n const anyValue = contentPathExists(\n segments,\n (a) => `${a}.value IS NOT NULL`,\n params,\n nextAlias,\n );\n conditions.push(`(${isNull} OR NOT ${anyValue})`);\n } else {\n const match = contentPathExists(segments, equalsLeaf, params, nextAlias);\n params.push(value);\n conditions.push(match);\n }\n }\n // Presence — the question an exact-match value cannot ask: a path holds\n // a value when at least one non-null value is reachable at it.\n for (const key of predicate.contentPresent ?? []) {\n conditions.push(\n contentPathExists(\n parseContentFilterKey(key),\n (a) => `${a}.value IS NOT NULL`,\n params,\n nextAlias,\n ),\n );\n }\n return conditions;\n};\n\n/**\n * Every condition on the records row itself — everything a query asks\n * except where in the ordering to resume, which is per-partition and gets\n * appended by the caller.\n */\nconst recordConditions = (query: StackQuery): { conditions: string[]; params: unknown[] } => {\n const conditions: string[] = [\"r.id != '_config'\"];\n const params: unknown[] = [];\n const f = query.filter ?? {};\n let aliasSeq = 0;\n const nextContentAlias = () => `cp${aliasSeq++}`;\n\n /**\n * A filter that accepts one value or a list of them, as the `IN` that\n * covers both. Written even for a single value rather than as `= ?`,\n * since SQLite plans a one-element IN identically and one form means one\n * place for the filter's placeholder count and its bindings to agree.\n */\n const inFilter = (column: string, value: string | readonly string[] | undefined): void => {\n if (value === undefined) return;\n const values = Array.isArray(value) ? value : [value as string];\n conditions.push(`r.${column} IN (${values.map(() => '?').join(',')})`);\n params.push(...values);\n };\n\n /** One side of a date range, as the open-ended comparison it is. */\n const rangeFilter = (\n column: string,\n range: { after?: Date; before?: Date } | undefined,\n ): void => {\n if (range?.after) {\n conditions.push(`r.${column} > ?`);\n params.push(range.after.getTime());\n }\n if (range?.before) {\n conditions.push(`r.${column} < ?`);\n params.push(range.before.getTime());\n }\n };\n\n if (!f.includeDeleted) {\n conditions.push('r.deleted_at IS NULL');\n }\n\n if (!f.includeUnlisted) {\n conditions.push('r.unlisted_at IS NULL');\n }\n\n inFilter('type_id', f.typeId);\n\n // Not an inFilter: parentId takes an explicit null for \"at the root\",\n // which is a different SQL predicate rather than a different value.\n if (f.parentId !== undefined) {\n if (f.parentId === null) {\n conditions.push('r.parent_id IS NULL');\n } else {\n conditions.push('r.parent_id = ?');\n params.push(f.parentId);\n }\n }\n\n inFilter('app_id', f.appId);\n inFilter('entity_id', f.createdBy?.subjectId);\n inFilter('principal_id', f.createdBy?.principalId);\n\n rangeFilter('created_at', f.createdAt);\n rangeFilter('updated_at', f.updatedAt);\n\n // Every association filter below is a semi-join rather than a correlated\n // EXISTS, so the planner reads matching record ids straight out of an\n // index ending in record_id instead of probing once per record: the work\n // scales with the matches, not the stack.\n\n /**\n * The semi-join itself. `kind` is one of this module's own literals, never\n * caller text, and each clause carries its own placeholder — so the only\n * thing interpolated here is SQL this file wrote.\n */\n const hasAssociation = (kind: 'tag' | 'attachment' | 'relationship', clauses: string[]): string =>\n `r.id IN (SELECT a.record_id FROM associations a WHERE a.kind = '${kind}'` +\n clauses.map((c) => ` AND ${c}`).join('') +\n `)`;\n\n // Conjunctive: a record must carry every tag named.\n if (f.tags?.length) {\n for (const tag of f.tags) {\n conditions.push(hasAssociation('tag', ['a.label = ?']));\n params.push(tag);\n }\n }\n\n // Both halves in one subquery, so they match the same association.\n if (f.attachment) {\n const clauses: string[] = [];\n if (f.attachment.label !== undefined) {\n clauses.push('a.label = ?');\n params.push(f.attachment.label);\n }\n if (f.attachment.fileId !== undefined) {\n clauses.push('a.file_id = ?');\n params.push(f.attachment.fileId);\n }\n conditions.push(hasAssociation('attachment', clauses));\n }\n\n // A reference is an attachment association or a top-level file-ref field.\n if (f.referencesFileId) {\n conditions.push(\n `(${hasAssociation('attachment', ['a.file_id = ?'])}\n OR r.id IN (SELECT ci.record_id FROM content_index ci WHERE ci.file_id = ?))`,\n );\n params.push(f.referencesFileId, f.referencesFileId);\n }\n\n // Each clause is an optional pattern (docs/spec/data-model.md § Filter).\n // A record or entity target always stores '' in related_ns; saying so\n // keeps idx_assoc_related's prefix a contiguous equality run.\n if (f.relatedTo) {\n const clauses: string[] = [];\n const target = f.relatedTo.target;\n if (f.relatedTo.label !== undefined) {\n clauses.push('a.label = ?');\n params.push(f.relatedTo.label);\n }\n if (target) {\n clauses.push('a.related_scope = ?');\n params.push(target.kind);\n if (target.kind === 'record') {\n clauses.push('a.related_ns = ?', 'a.related_id = ?', 'a.related_stack = ?');\n params.push('', target.recordId, target.stackUrl ?? '');\n } else if (target.kind === 'entity') {\n clauses.push('a.related_ns = ?', 'a.related_id = ?');\n params.push('', target.entityId);\n } else {\n clauses.push('a.related_ns = ?');\n params.push(target.ns);\n if (target.id !== undefined) {\n clauses.push('a.related_id = ?');\n params.push(target.id);\n }\n }\n }\n conditions.push(hasAssociation('relationship', clauses));\n }\n\n if (f.content || f.contentPresent?.length) {\n conditions.push(...contentConditions(f, params, nextContentAlias));\n }\n\n // One arm per Type, each pinned to its type_id; types partition records,\n // so no record meets two arms (docs/spec/data-model.md § Content by type).\n if (f.contentByType) {\n const arms = Object.entries(f.contentByType).map(([typeId, arm]) => {\n params.push(typeId);\n return ['r.type_id = ?', ...contentConditions(arm, params, nextContentAlias)].join(' AND ');\n });\n conditions.push(`(${arms.map((arm) => `(${arm})`).join(' OR ')})`);\n }\n\n if (f.search) {\n const sanitized = sanitizeFts5Query(f.search);\n if (sanitized) {\n conditions.push(`r.rowid IN (SELECT rowid FROM records_fts WHERE records_fts MATCH ?)`);\n params.push(sanitized);\n } else {\n // A search that sanitizes to nothing (e.g. \"*\", punctuation-only) is\n // an honest zero-match result, not \"no filter\" — omitting the clause\n // would silently return the full table as the \"search result\".\n conditions.push('0');\n }\n }\n\n return { conditions, params };\n};\n\n/**\n * Everything after the cursor's record within the partition that holds a\n * value. `op` compares the way the ORDER BY does, so one form covers both\n * directions; where the cursor's rank is the last one, the leading\n * disjunct is simply never true.\n */\nconst presentCursorCondition = (\n cursor: Extract<DecodedCursor, { kind: 'num' | 'text' }>,\n op: '>' | '<',\n params: unknown[],\n): string => {\n if (cursor.kind === 'num') {\n params.push(cursor.value, cursor.value, cursor.id);\n return (\n `(${SORT_ALIAS}.value_rank ${op} 0 OR (${SORT_ALIAS}.value_rank = 0 AND ` +\n `(${SORT_ALIAS}.num_value ${op} ? OR (${SORT_ALIAS}.num_value = ? AND ` +\n `${SORT_ALIAS}.record_id ${op} ?))))`\n );\n }\n const key = contentSortKey(cursor.value);\n params.push(key, key, cursor.value, cursor.value, cursor.id);\n return (\n `(${SORT_ALIAS}.value_rank ${op} 1 OR (${SORT_ALIAS}.value_rank = 1 AND ` +\n `(${SORT_ALIAS}.text_key ${op} ? OR (${SORT_ALIAS}.text_key = ? AND ` +\n `(${SORT_ALIAS}.text_value ${op} ? OR (${SORT_ALIAS}.text_value = ? AND ` +\n `${SORT_ALIAS}.record_id ${op} ?))))))`\n );\n};\n\nconst statement = (\n select: string,\n from: string,\n conditions: string[],\n params: unknown[],\n tail = '',\n): QueryStatement => ({\n sql: `SELECT ${select} FROM ${from} WHERE ${conditions.join(' AND ')}${tail}`,\n params,\n});\n\n/**\n * Compile a query into the statements that answer it, read in order and\n * their rows concatenated: the first that fills the page ends the read,\n * so the second is never run for a page that doesn't reach it. Each still\n * needs the row budget its `LIMIT ?` binds — see atBudget.\n */\nexport const buildQueryPlan = (query: StackQuery): QueryStatement[] => {\n const dir = sqlDirection(query);\n const op = dir === 'ASC' ? '>' : '<';\n const decoded = query.cursor ? decodeCursor(query.cursor) : null;\n if (query.sort?.contentFieldByType) {\n return byTypePlan(query, query.sort.contentFieldByType, dir, op, decoded);\n }\n const contentField = query.sort?.contentField;\n\n if (contentField === undefined) {\n const { conditions, params } = recordConditions(query);\n const field = getSortField(query);\n const col = getSortColumn(field);\n if (decoded) {\n const cursor = nativeCursor(decoded, field);\n conditions.push(`(r.${col} ${op} ? OR (r.${col} = ? AND r.id ${op} ?))`);\n params.push(cursor.value, cursor.value, cursor.id);\n }\n return [\n statement(\n 'r.*',\n 'records r',\n conditions,\n params,\n ` ORDER BY r.${col} ${dir}, r.id ${dir} LIMIT ?`,\n ),\n ];\n }\n\n const cursor = decoded ? contentCursor(decoded, contentField) : null;\n const pages: QueryStatement[] = [];\n\n // Records holding a value at the field, in that value's order. A cursor\n // sitting in the absent partition has already passed every one of them.\n if (cursor?.kind !== 'absent') {\n const { conditions, params } = recordConditions(query);\n const from = `content_index ${SORT_ALIAS} JOIN records r ON r.id = ${SORT_ALIAS}.record_id`;\n conditions.push(`${SORT_ALIAS}.field = ?`);\n params.push(contentField);\n if (cursor) conditions.push(presentCursorCondition(cursor, op, params));\n // Ordered exactly as idx_content_index_sort stores it, so the page is\n // an ordered walk of the index rather than a sort of the matches.\n const order =\n `${SORT_ALIAS}.value_rank ${dir}, ${SORT_ALIAS}.num_value ${dir}, ` +\n `${SORT_ALIAS}.text_key ${dir}, ${SORT_ALIAS}.text_value ${dir}, ` +\n `${SORT_ALIAS}.record_id ${dir}`;\n pages.push(statement('r.*', from, conditions, params, ` ORDER BY ${order} LIMIT ?`));\n }\n\n // Records holding nothing there, which trail the others whichever way\n // the sort runs (docs/spec/data-model.md § Sorting by a content field).\n {\n const { conditions, params } = recordConditions(query);\n conditions.push(\n `NOT EXISTS (SELECT 1 FROM content_index ${SORT_ALIAS} ` +\n `WHERE ${SORT_ALIAS}.record_id = r.id AND ${SORT_ALIAS}.field = ?)`,\n );\n params.push(contentField);\n if (cursor?.kind === 'absent') {\n conditions.push(`r.id ${op} ?`);\n params.push(cursor.id);\n }\n pages.push(statement('r.*', 'records r', conditions, params, ` ORDER BY r.id ${dir} LIMIT ?`));\n }\n\n return pages;\n};\n\n/**\n * A sort naming its value per Type, read through a derived table in place\n * of the index: each Type's own index rows, and its constant in the index's\n * columns, so one cursor condition and one ORDER BY serve both. See\n * docs/spec/data-model.md § Sorting by a content field.\n */\nconst byTypePlan = (\n query: StackQuery,\n byType: Record<string, ContentSortKey>,\n dir: 'ASC' | 'DESC',\n op: '>' | '<',\n decoded: DecodedCursor | null,\n): QueryStatement[] => {\n const cursor = decoded ? contentCursor(decoded, byTypeDescriptor(byType)) : null;\n const fields: [string, string][] = [];\n const constants: [string, SortEntry][] = [];\n for (const [typeId, key] of Object.entries(byType)) {\n if (key.field !== undefined) {\n fields.push([typeId, key.field]);\n continue;\n }\n const entry = constantSortEntry(key.value ?? null);\n if (entry) constants.push([typeId, entry]);\n }\n const pages: QueryStatement[] = [];\n const fieldOf = (alias: string, record: string) =>\n fields.map(() => `(${record}.type_id = ? AND ${alias}.field = ?)`).join(' OR ');\n\n if (cursor?.kind !== 'absent' && fields.length + constants.length > 0) {\n const sources: string[] = [];\n const sourceParams: unknown[] = [];\n if (fields.length > 0) {\n sources.push(\n 'SELECT c.record_id AS record_id, c.value_rank AS value_rank, c.num_value AS num_value, ' +\n 'c.text_key AS text_key, c.text_value AS text_value ' +\n `FROM content_index c JOIN records t ON t.id = c.record_id WHERE ${fieldOf('c', 't')}`,\n );\n for (const [typeId, field] of fields) sourceParams.push(typeId, field);\n }\n for (const [typeId, entry] of constants) {\n sources.push(\n 'SELECT t.id AS record_id, ? AS value_rank, ? AS num_value, ? AS text_key, ' +\n '? AS text_value FROM records t WHERE t.type_id = ?',\n );\n sourceParams.push(\n ...(entry.kind === 'num' ? [0, entry.num, null, null] : [1, null, entry.key, entry.text]),\n typeId,\n );\n }\n const { conditions, params } = recordConditions(query);\n if (cursor) conditions.push(presentCursorCondition(cursor, op, params));\n const from =\n `(${sources.join(' UNION ALL ')}) ${SORT_ALIAS} ` +\n `JOIN records r ON r.id = ${SORT_ALIAS}.record_id`;\n const order =\n `${SORT_ALIAS}.value_rank ${dir}, ${SORT_ALIAS}.num_value ${dir}, ` +\n `${SORT_ALIAS}.text_key ${dir}, ${SORT_ALIAS}.text_value ${dir}, ` +\n `${SORT_ALIAS}.record_id ${dir}`;\n pages.push(\n statement(\n 'r.*',\n from,\n conditions,\n [...sourceParams, ...params],\n ` ORDER BY ${order} LIMIT ?`,\n ),\n );\n }\n\n // Records holding nothing to order by: a Type's field with no value, a\n // `null` constant, or a Type the map does not name.\n {\n const { conditions, params } = recordConditions(query);\n if (fields.length > 0) {\n conditions.push(\n `NOT EXISTS (SELECT 1 FROM content_index ${SORT_ALIAS} ` +\n `WHERE ${SORT_ALIAS}.record_id = r.id AND (${fieldOf(SORT_ALIAS, 'r')}))`,\n );\n for (const [typeId, field] of fields) params.push(typeId, field);\n }\n if (constants.length > 0) {\n conditions.push(`r.type_id NOT IN (${constants.map(() => '?').join(',')})`);\n params.push(...constants.map(([typeId]) => typeId));\n }\n if (cursor?.kind === 'absent') {\n conditions.push(`r.id ${op} ?`);\n params.push(cursor.id);\n }\n pages.push(statement('r.*', 'records r', conditions, params, ` ORDER BY r.id ${dir} LIMIT ?`));\n }\n return pages;\n};\n","/**\n * The two derived indexes a records database keeps beside its rows: FTS5\n * for `filter.search`, and content_index for `sort.contentField` and the\n * file-reference filters. Both can be rebuilt from `records` alone, and\n * both must be rewritten in the same transaction as any write that changes\n * what they cover, or a query answers from a record that no longer exists.\n */\n\nimport type { StackType, ScalarFieldKind } from '@haverstack/core';\nimport { contentSortEntry } from '@haverstack/core/adapter';\nimport type { SqlExecutor } from './executor.js';\n\n/**\n * FTS5's external-content table needs its special `('delete', rowid,\n * content)` command — with the *old* content — to unindex a row; a plain\n * DELETE leaves stale, still-searchable entries. remove() therefore reads\n * the current rowid/content and must run before the caller's change.\n */\nexport class SearchIndex {\n constructor(private readonly exec: SqlExecutor) {}\n\n /** Index a record that has no FTS entry yet (used right after an INSERT). */\n insert(recordId: string, content: string): void {\n this.exec.run(\n `INSERT INTO records_fts(rowid, content) SELECT rowid, ? FROM records WHERE id = ?`,\n [content, recordId],\n );\n }\n\n /**\n * Remove a record's FTS entry. MUST be called before the records row's\n * content changes or the row is deleted — see the note above.\n */\n remove(recordId: string): void {\n const row = this.exec.get<{ rowid: number; content: string }>(\n 'SELECT rowid, content FROM records WHERE id = ?',\n [recordId],\n );\n if (!row) return;\n this.exec.run(`INSERT INTO records_fts(records_fts, rowid, content) VALUES('delete', ?, ?)`, [\n row.rowid,\n row.content,\n ]);\n }\n}\n\n/**\n * Top-level scalar fields, by name and declared kind — what content_index\n * covers. Arrays and objects are left out: a value inside one has no\n * single position to order its record by, and no file reference the\n * attachment filter can see.\n */\nconst indexedFieldKinds = (\n schema: Record<string, { kind?: string }>,\n): Map<string, ScalarFieldKind> => {\n const kinds = new Map<string, ScalarFieldKind>();\n for (const [field, def] of Object.entries(schema)) {\n if (!def.kind || def.kind === 'array' || def.kind === 'object') continue;\n kinds.set(field, def.kind as ScalarFieldKind);\n }\n return kinds;\n};\n\n/**\n * One row per top-level scalar field a record holds a value at. Caches each\n * type's indexed fields so a write doesn't re-read and re-parse the schema.\n */\nexport class ContentIndex {\n private readonly fieldsByType = new Map<string, Map<string, ScalarFieldKind>>();\n\n constructor(private readonly exec: SqlExecutor) {}\n\n /** Prime the cache from a type as it is saved. */\n rememberType(type: StackType): void {\n this.fieldsByType.set(\n type.id,\n indexedFieldKinds(type.schema as Record<string, { kind?: string }>),\n );\n }\n\n /**\n * Replace a record's rows with what its content currently holds, on every\n * write that can change content or typeId, so neither the ordering nor the\n * file-reference lookup can drift from the record. A field holding no\n * orderable value gets no row at all, which is what puts the record at the\n * end of a sort on that field.\n */\n sync(recordId: string, typeId: string, content: Record<string, unknown>): void {\n this.remove(recordId);\n\n for (const [field, kind] of this.fieldsOf(typeId)) {\n const entry = contentSortEntry(kind, content[field]);\n if (!entry) continue;\n const sql = `INSERT OR IGNORE INTO content_index\n (record_id, field, value_rank, num_value, text_key, text_value, file_id)\n VALUES (?, ?, ?, ?, ?, ?, ?)`;\n if (entry.kind === 'num') {\n this.exec.run(sql, [recordId, field, 0, entry.num, null, null, null]);\n } else {\n // A file-ref field's value is its file id, so the row it already\n // needs for ordering also answers the attachment filter — one\n // field is one row, whatever a query reads it for.\n const fileId = kind === 'file-ref' ? entry.text : null;\n this.exec.run(sql, [recordId, field, 1, null, entry.key, entry.text, fileId]);\n }\n }\n }\n\n remove(recordId: string): void {\n this.exec.run('DELETE FROM content_index WHERE record_id = ?', [recordId]);\n }\n\n /** The declared kind of a type's top-level `field`, if it has one. */\n kindOf(typeId: string, field: string): ScalarFieldKind | undefined {\n return this.fieldsOf(typeId).get(field);\n }\n\n /**\n * From the cache, falling back to a `types` lookup only for a typeId this\n * process hasn't seen via rememberType() yet, e.g. right after open().\n */\n private fieldsOf(typeId: string): Map<string, ScalarFieldKind> {\n const cached = this.fieldsByType.get(typeId);\n if (cached) return cached;\n\n const typeRow = this.exec.get<{ schema: string }>('SELECT schema FROM types WHERE id = ?', [\n typeId,\n ]);\n const fields = typeRow\n ? indexedFieldKinds(JSON.parse(typeRow.schema) as Record<string, { kind?: string }>)\n : new Map<string, ScalarFieldKind>();\n this.fieldsByType.set(typeId, fields);\n return fields;\n }\n}\n","/**\n * Row <-> domain object mappers shared by every SQLite-backed record\n * adapter. Column names and JSON-encoding choices are the storage\n * contract; keeping one copy means the adapters can't drift on them.\n */\n\nimport type {\n Actor,\n GroupRole,\n Grantee,\n RecordJournalEntry,\n StackRecord,\n StackType,\n RecordVersion,\n Association,\n RelationshipTarget,\n} from '@haverstack/core';\nimport { partitionAssociations } from '@haverstack/core/adapter';\n\nexport const toMs = (d: Date): number => d.getTime();\n\n/**\n * An Actor from its two columns. A principal with no subject beside it\n * names nobody to attribute to, so it reads as absent like the subject.\n */\nconst actorFromColumns = (subject: unknown, principal: unknown): Actor | undefined =>\n subject == null\n ? undefined\n : {\n subjectId: subject as string,\n ...(principal != null && { principalId: principal as string }),\n };\nexport const fromMs = (ms: number): Date => new Date(ms);\n\nexport const rowToRecord = (\n row: Record<string, unknown>,\n associations: Association[],\n): StackRecord => {\n const record: StackRecord = {\n id: row.id as string,\n typeId: row.type_id as string,\n createdAt: fromMs(row.created_at as number),\n updatedAt: fromMs(row.updated_at as number),\n content: JSON.parse(row.content as string),\n version: row.version as number,\n };\n // Presence, never truthiness: SQL NULL is the only spelling of an absent\n // field, so the empty string and the epoch are values like any other.\n // The query predicates beside this read `IS NULL`, and a mapper that\n // disagreed would let a record answer one way to get() and another to\n // the filter that should have found it.\n if (row.parent_id != null) record.parentId = row.parent_id as string;\n if (row.app_id != null) record.appId = row.app_id as string;\n const createdBy = actorFromColumns(row.entity_id, row.principal_id);\n if (createdBy) record.createdBy = createdBy;\n const updatedBy = actorFromColumns(row.updated_by, row.updated_via);\n if (updatedBy) record.updatedBy = updatedBy;\n if (row.deleted_at != null) record.deletedAt = fromMs(row.deleted_at as number);\n if (row.unlisted_at != null) record.unlistedAt = fromMs(row.unlisted_at as number);\n // One table, partitioned on read. See docs/spec/access-control.md\n // § Record-level permissions.\n const partitioned = partitionAssociations(associations);\n if (partitioned.associations.length) record.associations = partitioned.associations;\n if (partitioned.permissions.length) record.permissions = partitioned.permissions;\n return record;\n};\n\nexport const rowToAssociation = (row: Record<string, unknown>): Association => {\n if (row.kind === 'tag') {\n return { kind: 'tag', label: row.label as string };\n }\n if (row.kind === 'anyone') {\n return { kind: 'anyone', label: 'read' };\n }\n if (row.kind === 'permission') {\n return {\n kind: 'permission',\n label: row.label as 'read' | 'write',\n grantee: rowToGrantee(row),\n };\n }\n if (row.kind === 'attachment') {\n const attachmentRecordId = row.attachment_record_id as string;\n return {\n kind: 'attachment',\n label: row.label as string,\n fileId: row.file_id as string,\n // Stored as '' when absent, like every other optional column here —\n // the field is omitted rather than served empty, so a round trip\n // produces the association the caller wrote.\n ...(attachmentRecordId && { attachmentRecordId }),\n };\n }\n return {\n kind: 'relationship',\n label: row.label as string,\n target: rowToTarget(row),\n };\n};\n\n/**\n * The six columns that identify an association, in the order every\n * INSERT and DELETE below binds them. One helper because the two must\n * agree exactly — a dissociate that bound them differently would delete\n * nothing and report success.\n */\nexport const associationKeyColumns = (\n a: Association,\n): [string, string, string, string, string, string] => {\n if (a.kind === 'attachment') return [a.fileId, '', '', '', '', ''];\n if (a.kind === 'permission') {\n const g = a.grantee;\n return g.kind === 'entity'\n ? ['', 'entity', g.entityId, '', '', '']\n : ['', 'group', g.groupId, '', '', g.role];\n }\n if (a.kind !== 'relationship') return ['', '', '', '', '', ''];\n const t = a.target;\n if (t.kind === 'entity') return ['', 'entity', t.entityId, '', '', ''];\n if (t.kind === 'external') return ['', 'external', t.id, t.ns, '', ''];\n return ['', 'record', t.recordId, '', t.stackUrl ?? '', ''];\n};\n\n/**\n * A permission's grantee, read back off the columns a relationship target\n * otherwise uses. `related_role` is what tells the two group sets apart —\n * `member` is the wider one. See docs/spec/access-control.md\n * § Record-level permissions.\n */\nconst rowToGrantee = (row: Record<string, unknown>): Grantee =>\n row.related_scope === 'group'\n ? {\n kind: 'group',\n groupId: row.related_id as string,\n role: row.related_role as GroupRole,\n }\n : { kind: 'entity', entityId: row.related_id as string };\n\nconst rowToTarget = (row: Record<string, unknown>): RelationshipTarget => {\n const id = row.related_id as string;\n if (row.related_scope === 'entity') return { kind: 'entity', entityId: id };\n if (row.related_scope === 'external') {\n return { kind: 'external', ns: row.related_ns as string, id };\n }\n const stackUrl = row.related_stack as string;\n return { kind: 'record', recordId: id, ...(stackUrl && { stackUrl }) };\n};\n\nexport const rowToType = (row: Record<string, unknown>): StackType => {\n const t: StackType = {\n id: row.id as string,\n baseId: row.base_id as string,\n version: row.version as number,\n name: row.name as string,\n schema: JSON.parse(row.schema as string),\n schemaHash: row.schema_hash as string,\n createdAt: fromMs(row.created_at as number),\n };\n if (row.migrates_from) t.migratesFrom = row.migrates_from as string;\n return t;\n};\n\nexport const rowToVersion = (row: Record<string, unknown>): RecordVersion => {\n const v: RecordVersion = {\n version: row.version as number,\n typeId: row.type_id as string,\n content: JSON.parse(row.content as string),\n updatedAt: fromMs(row.updated_at as number),\n };\n const createdBy = actorFromColumns(row.entity_id, row.principal_id);\n if (createdBy) v.createdBy = createdBy;\n const updatedBy = actorFromColumns(row.updated_by, row.updated_via);\n if (updatedBy) v.updatedBy = updatedBy;\n return v;\n};\n\n/**\n * One journal row. `previous_parent_id` carries the three-way spelling the\n * column comment states: SQL NULL is an entry naming no origin, '' is the\n * root, anything else is a container id.\n */\nexport const rowToJournalEntry = (row: Record<string, unknown>): RecordJournalEntry => {\n const e: RecordJournalEntry = {\n seq: row.seq as number,\n at: fromMs(row.at as number),\n kind: row.kind as RecordJournalEntry['kind'],\n ops: JSON.parse(row.ops as string),\n version: row.version as number,\n typeId: row.type_id as string,\n };\n if (row.parent_id != null) e.parentId = row.parent_id as string;\n if (row.previous_parent_id != null) {\n e.previousParentId = row.previous_parent_id === '' ? null : (row.previous_parent_id as string);\n }\n if (row.actor != null) e.actor = JSON.parse(row.actor as string);\n if (row.associations != null) e.associations = JSON.parse(row.associations as string);\n return e;\n};\n","/**\n * The actual StackRecordAdapter logic (minus lifecycle: open/\n * close/flush stay per-engine, since schema setup, pragmas, locking, and\n * durability differ there). Every CRUD/query/version/type/association\n * operation lives here exactly once, parametrized over a SqlExecutor\n * (which normalizes each engine's call conventions). Concrete adapters\n * construct one of these and delegate their StackRecordAdapter methods\n * to it — see record-adapter-sqlite.\n */\n\nimport {\n StackConflictError,\n StackVersionConflictError,\n StackNotFoundError,\n StackBadRequestError,\n applyMergePatch,\n assertOneSurface,\n} from '@haverstack/core';\nimport type {\n RecordJournalEntry,\n JournalQuery,\n VersionsQuery,\n StackRecord,\n StackType,\n TypeId,\n FileId,\n RecordVersion,\n RecordChangeSet,\n StackQuery,\n QueryResult,\n Association,\n AssociationEdit,\n ActorOptions,\n IfVersionOptions,\n} from '@haverstack/core';\nimport type {\n BumpVersionOptions,\n JournalEntryInput,\n JournalOptions,\n MutateOptions,\n} from '@haverstack/core/adapter';\nimport { AUTHORITY_ASSOCIATION_KINDS } from '@haverstack/core/adapter';\nimport type { SqlExecutor } from './executor.js';\nimport { isForeignKeyViolation, isUniqueConstraintViolation } from './executor.js';\nimport { buildQueryPlan, atBudget } from './query.js';\nimport { ContentIndex, SearchIndex } from './indexes.js';\nimport {\n rowToRecord,\n rowToAssociation,\n rowToType,\n rowToVersion,\n rowToJournalEntry,\n toMs,\n associationKeyColumns,\n} from './mappers.js';\nimport { makeCursor } from './cursor.js';\n\n/**\n * Restate an engine parse error as `bad_request` when the query carries\n * search text, the one filter with a query language the sanitizer can't\n * fully repair. Any other clause is built from parameters, so a parse\n * error there is this module's bug and is rethrown untouched.\n */\nconst asStackBadRequestError = <T>(query: StackQuery, run: () => T): T => {\n try {\n return run();\n } catch (err) {\n if (!query.filter?.search) throw err;\n throw new StackBadRequestError(\n `Search text could not be parsed: ${err instanceof Error ? err.message : String(err)}`,\n );\n }\n};\n\n/**\n * One spelling of the optimistic-concurrency failure, so the message a\n * caller sees never depends on which check noticed it.\n */\nconst versionConflict = (id: string, expected: number, actual: number): StackVersionConflictError =>\n new StackVersionConflictError(\n `Record \"${id}\" is at version ${actual}, expected ${expected}`,\n id,\n expected,\n actual,\n );\n\nconst recordNotFound = (id: string): StackNotFoundError =>\n new StackNotFoundError(`Record not found: \"${id}\"`);\n\n/**\n * Every `versions` column beyond the (record_id, version) key, and the\n * values for them in the same order — one list, so the INSERT that writes a\n * snapshot and the UPDATE that replaces one can't drift apart on which\n * columns a snapshot is made of.\n */\nconst VERSION_COLUMNS = [\n 'type_id',\n 'content',\n 'updated_at',\n 'entity_id',\n 'principal_id',\n 'updated_by',\n 'updated_via',\n] as const;\n\nconst versionRowValues = (version: RecordVersion): unknown[] => [\n version.typeId,\n JSON.stringify(version.content),\n toMs(version.updatedAt),\n version.createdBy?.subjectId ?? null,\n version.createdBy?.principalId ?? null,\n version.updatedBy?.subjectId ?? null,\n version.updatedBy?.principalId ?? null,\n];\n\nexport type SharedSqlRecordLogicDeps = {\n exec: SqlExecutor;\n};\n\nexport class SharedSqlRecordLogic {\n private readonly exec: SqlExecutor;\n private readonly searchIndex: SearchIndex;\n private readonly contentIndex: ContentIndex;\n\n constructor({ exec }: SharedSqlRecordLogicDeps) {\n this.exec = exec;\n this.searchIndex = new SearchIndex(exec);\n this.contentIndex = new ContentIndex(exec);\n }\n\n /** Check `ifVersion` against a version read inside the caller's transaction. */\n private checkExpectedVersion(id: string, actual: number, expected: number | undefined): void {\n if (expected === undefined || actual === expected) return;\n throw versionConflict(id, expected, actual);\n }\n\n /**\n * Called after a version-gated statement affected zero rows.\n * Distinguishes \"record doesn't exist\" (StackNotFoundError) from \"it\n * exists but isn't at expectedVersion\" (StackVersionConflictError, with\n * the actual current version for the caller to act on).\n */\n private throwVersionConflict(id: string, expectedVersion: number | undefined): never {\n const version = this.currentVersion(id);\n if (version === undefined) throw recordNotFound(id);\n throw versionConflict(id, expectedVersion as number, version);\n }\n\n private currentVersion(id: string): number | undefined {\n return this.exec.get<{ version: number }>('SELECT version FROM records WHERE id = ?', [id])\n ?.version;\n }\n\n /**\n * What a mutation builds on, read inside its transaction so nothing can\n * land between the read and the write: a patch applied to an earlier read\n * would overwrite whatever changed since. Throws for a missing record or\n * a failed `ifVersion`.\n */\n private readForWrite(\n id: string,\n ifVersion: number | undefined,\n ): { typeId: TypeId; content: Record<string, unknown> } {\n const row = this.exec.get<{ version: number; type_id: string; content: string }>(\n 'SELECT version, type_id, content FROM records WHERE id = ?',\n [id],\n );\n if (!row) throw recordNotFound(id);\n this.checkExpectedVersion(id, row.version, ifVersion);\n return { typeId: row.type_id, content: JSON.parse(row.content) };\n }\n\n /**\n * Every version-bumping UPDATE. The `ifVersion` guard rides in the WHERE\n * clause, so a write that reads nothing first (a soft delete, an\n * undelete) enforces it with no separate read.\n */\n private versionedUpdate(\n id: string,\n sets: string[],\n values: unknown[],\n opts: IfVersionOptions & ActorOptions,\n now = toMs(new Date()),\n ): void {\n const guarded = opts.ifVersion !== undefined;\n const changed = this.exec.run(\n `UPDATE records SET ${[...sets, 'version = version + 1', 'updated_at = ?', 'updated_by = ?', 'updated_via = ?'].join(', ')}` +\n ` WHERE id = ?${guarded ? ' AND version = ?' : ''}`,\n [\n ...values,\n now,\n opts.actor?.subjectId ?? null,\n opts.actor?.principalId ?? null,\n id,\n ...(guarded ? [opts.ifVersion] : []),\n ],\n );\n if (changed === 0) this.throwVersionConflict(id, opts.ifVersion);\n }\n\n /**\n * Read a record back after its own mutation committed. Absence here is an\n * invariant breach, not a \"not found\". Synchronous, so nothing can run\n * between the commit and the read — see readRecord().\n */\n private reread(id: string, verb: string): StackRecord {\n const updated = this.readRecord(id);\n if (!updated) throw new Error(`Record not found after ${verb}: \"${id}\"`);\n return updated;\n }\n\n // -------------------------------------------------------\n // Records\n // -------------------------------------------------------\n\n /**\n * One transaction, because a record and the tables that describe it are\n * one fact: a half-applied create raises, so the caller believes nothing\n * landed, while the surviving row fails their retry on the PK and is\n * missing from both indexes. A duplicate id maps to StackConflictError.\n */\n async createRecord(record: StackRecord, opts: JournalOptions = {}): Promise<StackRecord> {\n this.exec.transaction(() => {\n try {\n this.exec.run(\n `INSERT INTO records\n (id, type_id, created_at, updated_at, content, version,\n parent_id, entity_id, app_id, principal_id, updated_by, updated_via,\n deleted_at, unlisted_at)\n VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,\n [\n record.id,\n record.typeId,\n toMs(record.createdAt),\n toMs(record.updatedAt),\n JSON.stringify(record.content),\n record.version,\n record.parentId ?? null,\n record.createdBy?.subjectId ?? null,\n record.appId ?? null,\n record.createdBy?.principalId ?? null,\n record.updatedBy?.subjectId ?? null,\n record.updatedBy?.principalId ?? null,\n record.deletedAt ? toMs(record.deletedAt) : null,\n record.unlistedAt ? toMs(record.unlistedAt) : null,\n ],\n );\n } catch (err) {\n if (isUniqueConstraintViolation(err)) {\n throw new StackConflictError(`Record already exists: \"${record.id}\"`);\n }\n throw err;\n }\n\n const edges = [...(record.associations ?? []), ...(record.permissions ?? [])];\n if (edges.length) this.insertAssociations(record.id, edges);\n\n this.searchIndex.insert(record.id, JSON.stringify(record.content));\n this.contentIndex.sync(record.id, record.typeId, record.content);\n this.appendJournal(record.id, opts.journal);\n });\n\n return record;\n }\n\n async getRecord(id: string): Promise<StackRecord | null> {\n return this.readRecord(id);\n }\n\n /**\n * The synchronous read behind getRecord(), for use inside\n * exec.transaction(), whose callback can't await. reread() relies on the\n * same synchrony: nothing interleaves between a commit and its read, so a\n * concurrent mutation is never reported under this one's verb.\n */\n private readRecord(id: string): StackRecord | null {\n const row = this.exec.get<Record<string, unknown>>('SELECT * FROM records WHERE id = ?', [id]);\n return row ? this.hydrate(row) : null;\n }\n\n /** A records row with its association set attached. */\n private hydrate(row: Record<string, unknown>): StackRecord {\n return rowToRecord(row, this.getAssociationsForRecord(row.id as string));\n }\n\n /**\n * One transaction, so one call is one version whatever it moved, and the\n * patch applies to the content as it stands when the write lands.\n */\n async mutateRecord(\n id: string,\n changes: RecordChangeSet,\n opts: MutateOptions & BumpVersionOptions = {},\n ): Promise<StackRecord> {\n const now = toMs(new Date());\n\n // Assembled, since a fixed statement per combination of aspects would\n // be 2^4 of them. Shared by both paths; only `content` is the bumping\n // path's alone, since a patch always bumps.\n const sets: string[] = [];\n const values: unknown[] = [];\n if (changes.parentId !== undefined) {\n sets.push('parent_id = ?');\n values.push(changes.parentId);\n }\n if (changes.unlisted !== undefined) {\n sets.push('unlisted_at = ?');\n // Stamped fresh because this key is only ever present on a\n // transition — Stack narrows a change set to the aspects that\n // actually moved before it reaches an adapter.\n values.push(changes.unlisted ? now : null);\n }\n\n this.exec.transaction(() => {\n const current = this.readForWrite(id, opts.ifVersion);\n\n // No versioned UPDATE to carry `ifVersion`, so readForWrite() is the\n // only check. See docs/spec/versioning.md § Version history.\n if (opts.bumpsVersion === false) {\n if (sets.length > 0) {\n this.exec.run(`UPDATE records SET ${sets.join(', ')} WHERE id = ?`, [...values, id]);\n }\n this.replaceAssociationHalves(id, changes);\n this.appendJournal(id, opts.journal);\n return;\n }\n\n const merged = changes.contentPatch\n ? applyMergePatch(current.content, changes.contentPatch)\n : undefined;\n if (merged !== undefined) {\n sets.unshift('content = ?');\n values.unshift(JSON.stringify(merged));\n }\n\n if (opts.snapshot) this.snapshotBeforeMutation(id, opts.snapshot);\n if (merged !== undefined) this.searchIndex.remove(id);\n\n this.versionedUpdate(id, sets, values, opts, now);\n\n this.replaceAssociationHalves(id, changes);\n\n if (merged !== undefined) {\n this.searchIndex.insert(id, JSON.stringify(merged));\n this.contentIndex.sync(id, current.typeId, merged);\n }\n this.appendJournal(id, opts.journal);\n });\n\n return this.reread(id, 'mutateRecord');\n }\n\n async deleteRecord(\n id: string,\n opts: { purge?: boolean } & MutateOptions = {},\n ): Promise<StackRecord | null> {\n if (opts.purge) {\n return this.exec.transaction(() => this.purgeRecord(id, opts.ifVersion));\n }\n\n // One timestamp for both columns: a soft delete is a single event, and\n // two `new Date()` calls can straddle a millisecond boundary and leave\n // deleted_at and updated_at disagreeing about when it happened.\n const now = toMs(new Date());\n this.exec.transaction(() => {\n if (opts.snapshot) this.snapshotBeforeMutation(id, opts.snapshot);\n this.versionedUpdate(id, ['deleted_at = ?'], [now], opts, now);\n this.appendJournal(id, opts.journal);\n });\n\n return this.reread(id, 'deleteRecord');\n }\n\n /**\n * Delete a record and everything that refers to it, returning the record\n * as it stood (null if there was none). Read here, inside the caller's\n * transaction, so nothing can overtake the last copy that will ever exist.\n */\n private purgeRecord(id: string, ifVersion?: number): StackRecord | null {\n const purged = this.readRecord(id);\n if (!purged) {\n // A CAS against a record that isn't there is a failed precondition,\n // not the \"nothing to delete\" that an unconditional purge\n // reports by returning null.\n if (ifVersion !== undefined) throw recordNotFound(id);\n return null;\n }\n this.checkExpectedVersion(id, purged.version, ifVersion);\n this.searchIndex.remove(id);\n this.exec.run('DELETE FROM associations WHERE record_id = ?', [id]);\n this.exec.run('DELETE FROM versions WHERE record_id = ?', [id]);\n // A purge takes the journal with it, for the reason it takes the\n // version history: a log naming what a destroyed record once held is\n // exactly the residue this verb exists to leave nothing of.\n this.exec.run('DELETE FROM journal WHERE record_id = ?', [id]);\n this.contentIndex.remove(id);\n this.exec.run('DELETE FROM records WHERE id = ?', [id]);\n return purged;\n }\n\n async undeleteRecord(id: string, opts: MutateOptions = {}): Promise<StackRecord> {\n this.exec.transaction(() => {\n if (opts.snapshot) this.snapshotBeforeMutation(id, opts.snapshot);\n this.versionedUpdate(id, ['deleted_at = NULL'], [], opts);\n this.appendJournal(id, opts.journal);\n });\n\n return this.reread(id, 'undelete');\n }\n\n async restoreVersion(\n id: string,\n version: number,\n opts: MutateOptions = {},\n ): Promise<StackRecord> {\n // Only content and its type are restored — a snapshot carries nothing\n // else, so there is nothing here to roll containment, listing or the\n // associations table back to.\n this.exec.transaction(() => {\n this.readForWrite(id, opts.ifVersion);\n const target = this.readVersion(id, version);\n if (!target) throw new StackNotFoundError(`Version not found: \"${id}\"@${version}`);\n if (opts.snapshot) this.snapshotBeforeMutation(id, opts.snapshot);\n this.rewriteContent(id, target.typeId, target.content, opts);\n this.appendJournal(id, opts.journal);\n });\n\n return this.reread(id, 'restoreVersion');\n }\n\n async commitMigration(\n id: string,\n toTypeId: TypeId,\n content: Record<string, unknown>,\n opts: MutateOptions = {},\n ): Promise<StackRecord> {\n this.exec.transaction(() => {\n this.readForWrite(id, opts.ifVersion);\n if (opts.snapshot) this.snapshotBeforeMutation(id, opts.snapshot);\n this.rewriteContent(id, toTypeId, content, opts);\n this.appendJournal(id, opts.journal);\n });\n\n return this.reread(id, 'commitMigration');\n }\n\n /**\n * Replace a record's type and content wholesale. Unindex, write, re-index,\n * in that order (docs/spec/adapters.md § The search index is unindexed\n * before the row moves); callers must be inside exec.transaction().\n */\n private rewriteContent(\n id: string,\n typeId: TypeId,\n content: Record<string, unknown>,\n opts: IfVersionOptions & ActorOptions,\n ): void {\n const json = JSON.stringify(content);\n this.searchIndex.remove(id);\n this.versionedUpdate(id, ['type_id = ?', 'content = ?'], [typeId, json], opts);\n this.searchIndex.insert(id, json);\n this.contentIndex.sync(id, typeId, content);\n }\n\n async queryRecords(query: StackQuery): Promise<QueryResult> {\n const limit = query.limit ?? 50;\n // One extra row, to learn whether a further page follows.\n const fetch = limit + 1;\n\n // A content-field sort is two statements (see buildQueryPlan). Each asks\n // only for what the page still lacks, so the second usually never runs.\n const rows: Record<string, unknown>[] = [];\n for (const page of buildQueryPlan(query)) {\n const budget = fetch - rows.length;\n if (budget <= 0) break;\n const statement = atBudget(page, budget);\n rows.push(\n ...asStackBadRequestError(query, () =>\n this.exec.all<Record<string, unknown>>(statement.sql, statement.params),\n ),\n );\n }\n\n const hasMore = rows.length > limit;\n const page = hasMore ? rows.slice(0, limit) : rows;\n\n const records = page.map((row) => this.hydrate(row));\n\n const lastRecord = records[records.length - 1];\n const cursor =\n hasMore && lastRecord\n ? makeCursor(lastRecord, query.sort, (record, field) =>\n this.contentIndex.kindOf(record.typeId, field),\n )\n : null;\n\n return { records, cursor };\n }\n\n /**\n * The reference check and the purges share one synchronous transaction,\n * so nothing can add a reference between the two.\n */\n async deleteUnreferencedAttachmentRecords(\n fileId: FileId,\n metadataTypeIds: TypeId[],\n ): Promise<StackRecord[]> {\n return this.exec.transaction(() => {\n const referenced = this.exec.all<{ found: number }>(\n `SELECT 1 as found FROM associations WHERE kind = 'attachment' AND file_id = ?\n UNION ALL\n SELECT 1 FROM content_index WHERE file_id = ?\n LIMIT 1`,\n [fileId, fileId],\n );\n if (referenced.length) {\n throw new StackConflictError('Attachment is still referenced by one or more records');\n }\n\n // An empty family still owes the caller the reference check above —\n // the conflict is a fact about the file, not about which metadata\n // types happen to be defined.\n if (metadataTypeIds.length === 0) return [];\n\n const placeholders = metadataTypeIds.map(() => '?').join(', ');\n const metaRows = this.exec.all<{ id: string }>(\n `SELECT id FROM records\n WHERE type_id IN (${placeholders}) AND json_extract(content, '$.fileId') = ?`,\n [...metadataTypeIds, fileId],\n );\n const deleted: StackRecord[] = [];\n for (const row of metaRows) {\n const purged = this.purgeRecord(row.id);\n if (purged) deleted.push(purged);\n }\n\n return deleted;\n });\n }\n\n // -------------------------------------------------------\n // Versions\n // -------------------------------------------------------\n\n async getVersions(id: string, query: VersionsQuery = {}): Promise<RecordVersion[]> {\n const conditions = ['record_id = ?'];\n const values: unknown[] = [id];\n if (query.beforeVersion !== undefined) {\n conditions.push('version < ?');\n values.push(query.beforeVersion);\n }\n const rows = this.exec.all<Record<string, unknown>>(\n `SELECT * FROM versions WHERE ${conditions.join(' AND ')} ORDER BY version DESC${\n query.limit !== undefined ? ' LIMIT ?' : ''\n }`,\n query.limit !== undefined ? [...values, query.limit] : values,\n );\n return rows.map(rowToVersion);\n }\n\n async getVersion(id: string, version: number): Promise<RecordVersion | null> {\n return this.readVersion(id, version);\n }\n\n private readVersion(id: string, version: number): RecordVersion | null {\n const row = this.exec.get<Record<string, unknown>>(\n 'SELECT * FROM versions WHERE record_id = ? AND version = ?',\n [id, version],\n );\n return row ? rowToVersion(row) : null;\n }\n\n /**\n * A (record_id, version) collision is rejected loudly via the UNIQUE\n * constraint, mapped to StackConflictError — never a silently discarded\n * snapshot leaving a hole in rollback history. See\n * docs/spec/versioning.md § Optimistic concurrency (`ifVersion`).\n */\n private insertVersionRow(id: string, version: RecordVersion): void {\n try {\n this.exec.run(\n `INSERT INTO versions\n (record_id, version, ${VERSION_COLUMNS.join(', ')})\n VALUES (?, ?, ${VERSION_COLUMNS.map(() => '?').join(', ')})`,\n [id, version.version, ...versionRowValues(version)],\n );\n } catch (err) {\n if (isUniqueConstraintViolation(err)) {\n throw new StackConflictError(\n `Version ${version.version} already exists for record \"${id}\" — a concurrent ` +\n `writer raced past this version. Use ifVersion to detect this before it happens.`,\n );\n }\n throw err;\n }\n }\n\n /**\n * Replaces every column insertVersionRow writes — structurally, off the\n * same VERSION_COLUMNS list, because a column left out here would keep\n * the replaced row's value and surface later as a restore putting back\n * something the snapshot never said.\n */\n private overwriteVersionRow(id: string, version: RecordVersion): void {\n this.exec.run(\n `UPDATE versions\n SET ${VERSION_COLUMNS.map((c) => `${c} = ?`).join(', ')}\n WHERE record_id = ? AND version = ?`,\n [...versionRowValues(version), id, version.version],\n );\n }\n\n /**\n * Standalone snapshot write for tooling and tests — loud on any\n * collision, since nothing here is about to bump the version. Mutating\n * methods take a `snapshot` option instead; see snapshotBeforeMutation.\n */\n async saveVersion(id: string, version: RecordVersion): Promise<void> {\n this.insertVersionRow(id, version);\n }\n\n /**\n * The snapshot half of a mutating method's atomic snapshot-then-mutate\n * transaction. A collision is a racing-writer conflict, except an orphan\n * row at the record's *current* version — recognized by version number\n * alone, never content — which is overwritten so the interrupted write\n * can complete. See docs/spec/versioning.md § Snapshot atomicity.\n */\n private snapshotBeforeMutation(id: string, version: RecordVersion): void {\n try {\n this.insertVersionRow(id, version);\n } catch (err) {\n if (!(err instanceof StackConflictError)) throw err;\n\n if (this.currentVersion(id) !== version.version) throw err;\n\n this.overwriteVersionRow(id, version);\n }\n }\n\n // -------------------------------------------------------\n // Change journal\n // -------------------------------------------------------\n\n /**\n * Append one entry, stamped from the row as it now stands, so callers run\n * this inside their write's transaction, after the write. `seq` is\n * allocated in the INSERT itself, so no caller ever holds one it expects\n * to be free. See docs/spec/journal.md § Ordering.\n */\n private appendJournal(recordId: string, entry: JournalEntryInput | undefined): void {\n if (!entry) return;\n // Not readRecord(): this runs on every write and needs three columns,\n // not the row plus its associations. parent_id stays raw so its\n // NULL/''/id spelling reaches the journal unchanged.\n const row = this.exec.get<{\n version: number;\n type_id: string;\n parent_id: string | null;\n }>('SELECT version, type_id, parent_id FROM records WHERE id = ?', [recordId]);\n if (!row) throw recordNotFound(recordId);\n\n this.exec.run(\n `INSERT INTO journal\n (record_id, seq, at, kind, ops, version, type_id, parent_id,\n previous_parent_id, actor, associations)\n VALUES (\n ?,\n (SELECT COALESCE(MAX(seq), 0) + 1 FROM journal WHERE record_id = ?),\n ?, ?, ?, ?, ?, ?, ?, ?, ?)`,\n [\n recordId,\n recordId,\n toMs(new Date()),\n entry.kind,\n JSON.stringify(entry.ops),\n row.version,\n row.type_id,\n row.parent_id,\n // '' is the root; NULL is an entry that names no origin at all.\n entry.previousParentId === undefined ? null : (entry.previousParentId ?? ''),\n entry.actor ? JSON.stringify(entry.actor) : null,\n entry.associations?.length ? JSON.stringify(entry.associations) : null,\n ],\n );\n }\n\n async getJournal(id: string, query: JournalQuery = {}): Promise<RecordJournalEntry[]> {\n // An empty log means \"nothing changed\" unconditionally, so a record\n // that isn't there cannot be spelled that way. A purged record is gone,\n // and gets the same refusal. See docs/spec/journal.md § Reading it.\n if (!this.exec.get('SELECT 1 FROM records WHERE id = ?', [id])) {\n throw recordNotFound(id);\n }\n const conditions = ['record_id = ?'];\n const values: unknown[] = [id];\n if (query.afterSeq !== undefined) {\n conditions.push('seq > ?');\n values.push(query.afterSeq);\n }\n const rows = this.exec.all<Record<string, unknown>>(\n `SELECT * FROM journal WHERE ${conditions.join(' AND ')} ORDER BY seq ASC${\n query.limit !== undefined ? ' LIMIT ?' : ''\n }`,\n query.limit !== undefined ? [...values, query.limit] : values,\n );\n return rows.map(rowToJournalEntry);\n }\n\n // -------------------------------------------------------\n // Types\n // -------------------------------------------------------\n\n async saveType(type: StackType): Promise<void> {\n this.exec.run(\n `INSERT OR REPLACE INTO types\n (id, base_id, version, name, schema, schema_hash, migrates_from, created_at)\n VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,\n [\n type.id,\n type.baseId,\n type.version,\n type.name,\n JSON.stringify(type.schema),\n type.schemaHash,\n type.migratesFrom ?? null,\n toMs(type.createdAt),\n ],\n );\n this.contentIndex.rememberType(type);\n }\n\n async getType(id: TypeId): Promise<StackType | null> {\n const row = this.exec.get<Record<string, unknown>>('SELECT * FROM types WHERE id = ?', [id]);\n return row ? rowToType(row) : null;\n }\n\n async listTypes(): Promise<StackType[]> {\n const rows = this.exec.all<Record<string, unknown>>(\n 'SELECT * FROM types ORDER BY base_id, version',\n );\n return rows.map(rowToType);\n }\n\n // -------------------------------------------------------\n // Associations\n // -------------------------------------------------------\n\n /**\n * Never bumps `version`/`updatedAt` and never snapshots — see\n * StackRecordAdapter.amendAssociations(). Removes apply before adds, so\n * removing and re-adding one identity in a call leaves it present.\n */\n async amendAssociations(\n recordId: string,\n changes: AssociationEdit[],\n opts: JournalOptions = {},\n ): Promise<StackRecord> {\n assertOneSurface(changes);\n this.exec.transaction(() => {\n if (this.currentVersion(recordId) === undefined) throw recordNotFound(recordId);\n for (const change of changes) {\n if (change.op !== 'remove') continue;\n const { association } = change;\n this.exec.run(\n `DELETE FROM associations\n WHERE record_id = ?\n AND kind = ?\n AND label = ?\n AND file_id = ?\n AND related_scope = ?\n AND related_id = ?\n AND related_ns = ?\n AND related_stack = ?\n AND related_role = ?`,\n [recordId, association.kind, association.label, ...associationKeyColumns(association)],\n );\n }\n const adds = changes.flatMap((c) => (c.op === 'add' ? [c.association] : []));\n if (adds.length) this.insertAssociations(recordId, adds);\n this.appendJournal(recordId, opts.journal);\n });\n\n return this.reread(recordId, 'amendAssociations');\n }\n\n /**\n * Each key replaces only within its own half of the partition, which is\n * why a tag write cannot drop an ACL. Caller-transactional.\n * See docs/spec/access-control.md § Record-level permissions.\n */\n private replaceAssociationHalves(recordId: string, changes: RecordChangeSet): void {\n if (changes.associations !== undefined) {\n this.replaceAssociations(recordId, changes.associations, 'data');\n }\n if (changes.permissions !== undefined) {\n this.replaceAssociations(recordId, changes.permissions, 'authority');\n }\n }\n\n /**\n * Swap one half of a record's association set wholesale — the \"replace,\n * don't merge\" semantics a change set's association list carries. The data\n * half is \"not an authority kind\", so core's list is the only one.\n */\n private replaceAssociations(\n recordId: string,\n associations: Association[],\n half: 'data' | 'authority',\n ): void {\n this.exec.run(\n `DELETE FROM associations WHERE record_id = ? AND kind ${half === 'data' ? 'NOT IN' : 'IN'} ` +\n `(${AUTHORITY_ASSOCIATION_KINDS.map(() => '?').join(', ')})`,\n [recordId, ...AUTHORITY_ASSOCIATION_KINDS],\n );\n if (associations.length) this.insertAssociations(recordId, associations);\n }\n\n /**\n * Upserts on association identity, so re-pointing `attachment_record_id`\n * (outside the key) costs no second row. A missing record trips the\n * foreign key, reported as StackNotFoundError rather than an orphan row.\n */\n private insertAssociations(recordId: string, associations: Association[]): void {\n for (const assoc of associations) {\n try {\n this.exec.run(\n `INSERT INTO associations\n (record_id, kind, label, file_id, related_scope, related_id, related_ns,\n related_stack, related_role, attachment_record_id)\n VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\n ON CONFLICT DO UPDATE SET attachment_record_id = excluded.attachment_record_id`,\n [\n recordId,\n assoc.kind,\n assoc.label,\n ...associationKeyColumns(assoc),\n assoc.kind === 'attachment' ? (assoc.attachmentRecordId ?? '') : '',\n ],\n );\n } catch (err) {\n if (isForeignKeyViolation(err)) {\n throw recordNotFound(recordId);\n }\n throw err;\n }\n }\n }\n\n private getAssociationsForRecord(recordId: string): Association[] {\n const rows = this.exec.all<Record<string, unknown>>(\n 'SELECT * FROM associations WHERE record_id = ?',\n [recordId],\n );\n return rows.map(rowToAssociation);\n }\n}\n","/**\n * The engine-independent half of a SQLite-backed StackRecordAdapter: the\n * capability declaration and the delegation of all seventeen\n * StackRecordAdapter methods onto SharedSqlRecordLogic. Every method here\n * is a forward with no engine-specific content, so an adapter that owns a\n * SqlExecutor already knows how to answer all of them.\n *\n * What a concrete adapter is left to supply is exactly what differs\n * between bindings: constructing the database handle, applying the\n * schema, and its own lifecycle (flush/close) — hence those two being the\n * only abstract members.\n *\n * Lives in record.js's barrel rather than index.js's, so an adapter that\n * imports it never pulls in token-logic.ts's `node:crypto` — see the note\n * at the top of record.ts.\n */\n\nimport type {\n JournalQuery,\n VersionsQuery,\n RecordJournalEntry,\n StackType,\n TypeId,\n FileId,\n RecordVersion,\n StackRecord,\n StackQuery,\n QueryResult,\n AssociationEdit,\n RecordChangeSet,\n StackCapabilities,\n} from '@haverstack/core';\nimport type {\n StackRecordAdapter,\n BumpVersionOptions,\n JournalOptions,\n MutateOptions,\n} from '@haverstack/core/adapter';\nimport type { SqlExecutor } from './executor.js';\nimport type { StackConfig } from './config.js';\nimport { SharedSqlRecordLogic } from './record-logic.js';\n\n/**\n * What every engine running SharedSqlRecordLogic's query builder supports.\n * It describes the shared SQL, not the binding, so it is the same for each\n * of them — an engine whose SQLite build lacked FTS5 would override\n * `filter.search` rather than restate the whole declaration.\n */\nexport const SQLITE_RECORD_CAPABILITIES: StackCapabilities = {\n filter: {\n content: 'path',\n contentPresent: true,\n // Inside SQLite's limits on expression depth and compound selects; see\n // docs/spec/adapters.md § Adapter capabilities.\n contentByType: 64,\n search: true,\n },\n sort: {\n fields: ['createdAt', 'updatedAt', 'version'],\n contentField: true,\n contentFieldByType: 64,\n },\n limits: {\n attachmentBytes: null,\n contentBytes: null,\n },\n};\n\nexport abstract class SharedSqlRecordAdapter implements StackRecordAdapter {\n readonly capabilities: StackCapabilities = SQLITE_RECORD_CAPABILITIES;\n\n readonly ownerEntityId: string;\n readonly timezone: string | undefined;\n\n private readonly record: SharedSqlRecordLogic;\n\n protected constructor(exec: SqlExecutor, config: StackConfig) {\n this.record = new SharedSqlRecordLogic({ exec });\n this.ownerEntityId = config.entityId;\n this.timezone = config.timezone;\n }\n\n // -------------------------------------------------------\n // Records\n // -------------------------------------------------------\n\n createRecord(record: StackRecord, opts?: JournalOptions): Promise<StackRecord> {\n return this.record.createRecord(record, opts);\n }\n\n getRecord(id: string): Promise<StackRecord | null> {\n return this.record.getRecord(id);\n }\n\n mutateRecord(\n id: string,\n changes: RecordChangeSet,\n opts?: MutateOptions & BumpVersionOptions,\n ): Promise<StackRecord> {\n return this.record.mutateRecord(id, changes, opts);\n }\n\n deleteRecord(\n id: string,\n opts?: { purge?: boolean } & MutateOptions,\n ): Promise<StackRecord | null> {\n return this.record.deleteRecord(id, opts);\n }\n\n undeleteRecord(id: string, opts?: MutateOptions): Promise<StackRecord> {\n return this.record.undeleteRecord(id, opts);\n }\n\n restoreVersion(id: string, version: number, opts?: MutateOptions): Promise<StackRecord> {\n return this.record.restoreVersion(id, version, opts);\n }\n\n commitMigration(\n id: string,\n toTypeId: TypeId,\n content: Record<string, unknown>,\n opts?: MutateOptions,\n ): Promise<StackRecord> {\n return this.record.commitMigration(id, toTypeId, content, opts);\n }\n\n queryRecords(query: StackQuery): Promise<QueryResult> {\n return this.record.queryRecords(query);\n }\n\n deleteUnreferencedAttachmentRecords(\n fileId: FileId,\n metadataTypeIds: TypeId[],\n ): Promise<StackRecord[]> {\n return this.record.deleteUnreferencedAttachmentRecords(fileId, metadataTypeIds);\n }\n\n // -------------------------------------------------------\n // Versions\n // -------------------------------------------------------\n\n getVersions(id: string, query?: VersionsQuery): Promise<RecordVersion[]> {\n return this.record.getVersions(id, query);\n }\n\n getVersion(id: string, version: number): Promise<RecordVersion | null> {\n return this.record.getVersion(id, version);\n }\n\n saveVersion(id: string, version: RecordVersion): Promise<void> {\n return this.record.saveVersion(id, version);\n }\n\n // -------------------------------------------------------\n // Change journal\n // -------------------------------------------------------\n\n getJournal(id: string, query?: JournalQuery): Promise<RecordJournalEntry[]> {\n return this.record.getJournal(id, query);\n }\n\n // -------------------------------------------------------\n // Types\n // -------------------------------------------------------\n\n saveType(type: StackType): Promise<void> {\n return this.record.saveType(type);\n }\n\n getType(id: TypeId): Promise<StackType | null> {\n return this.record.getType(id);\n }\n\n listTypes(): Promise<StackType[]> {\n return this.record.listTypes();\n }\n\n // -------------------------------------------------------\n // Associations\n // -------------------------------------------------------\n\n amendAssociations(\n recordId: string,\n changes: AssociationEdit[],\n opts?: JournalOptions,\n ): Promise<StackRecord> {\n return this.record.amendAssociations(recordId, changes, opts);\n }\n\n // -------------------------------------------------------\n // Lifecycle\n // -------------------------------------------------------\n\n abstract flush(): Promise<void>;\n abstract close(): Promise<void>;\n}\n","import type { SqlExecutor } from '@haverstack/sqlite-shared/record';\n\n/**\n * Normalizes a Durable Object's ctx.storage.sql to SqlExecutor.\n *\n * Two things don't map onto node:sqlite's shape the way the rest of this\n * interface does:\n *\n * - There is no get/all/run split — sql.exec() always returns a cursor.\n * get()/all() read it via .toArray(); run() reads .rowsWritten for the\n * affected-row count run()'s contract promises.\n * - Raw BEGIN/COMMIT/ROLLBACK are rejected outright by DO SQLite (verified\n * against the real Workers runtime, not assumed) — its storage has no\n * notion of a transaction left open across separate exec() calls, and\n * each statement auto-commits the instant it runs, so no-op'ing those\n * three strings would silently break atomicity, not preserve it. The\n * platform's real primitive is ctx.storage.transactionSync(fn), which\n * SqlExecutor.transaction() reaches directly — see docs/spec/adapters.md\n * § Concurrency & storage ownership.\n */\nexport class DurableObjectSqliteExecutor implements SqlExecutor {\n constructor(private readonly storage: DurableObjectStorage) {}\n\n private get sql(): SqlStorage {\n return this.storage.sql;\n }\n\n exec(sql: string): void {\n this.sql.exec(sql);\n }\n\n run(sql: string, params: readonly unknown[] = []): number {\n const cursor = this.sql.exec(sql, ...(params as SqlStorageValue[]));\n return cursor.rowsWritten;\n }\n\n get<T = Record<string, unknown>>(sql: string, params: readonly unknown[] = []): T | undefined {\n const rows = this.sql.exec(sql, ...(params as SqlStorageValue[])).toArray();\n return rows[0] as T | undefined;\n }\n\n all<T = Record<string, unknown>>(sql: string, params: readonly unknown[] = []): T[] {\n return this.sql.exec(sql, ...(params as SqlStorageValue[])).toArray() as T[];\n }\n\n transaction<T>(fn: () => T): T {\n return this.storage.transactionSync(fn);\n }\n}\n","/**\n * Haverstack — Durable Object SQLite Record Adapter\n * -------------------------------------------------------\n * Implements StackRecordAdapter over a Cloudflare Durable Object's SQLite\n * storage (ctx.storage.sql). Full-text search uses FTS5, same as\n * record-adapter-sqlite — DO's SQLite build ships it.\n *\n * Ownership and durability come from the platform, not from anything this\n * class does: a Durable Object id maps to exactly one running instance,\n * so there is no separate lock file the way record-adapter-sqlite needs\n * one for real files (see docs/spec/adapters.md § Concurrency & storage\n * ownership) — the DO *is* the lock. There is likewise no persist/flush\n * step: every write through ctx.storage.sql is durable by the time the\n * call returns, so flush()/close() are no-ops kept only to satisfy the\n * optional StackRecordAdapter methods.\n *\n * This class itself is a thin binding: the one piece of genuinely\n * engine-specific wiring — SqlExecutor.transaction() reaching\n * ctx.storage.transactionSync() instead of raw SQL BEGIN/COMMIT/ROLLBACK,\n * which DO SQLite rejects outright — lives in executor.ts. Everything\n * above storage comes from @haverstack/sqlite-shared's\n * SharedSqlRecordAdapter, exactly as it does for record-adapter-sqlite.\n */\n\nimport {\n applyRecordSchema,\n insertConfigRecord,\n tryReadStackConfig,\n SharedSqlRecordAdapter,\n type StackConfig,\n} from '@haverstack/sqlite-shared/record';\nimport { OwnerMismatchError } from '@haverstack/core/adapter';\nimport { DurableObjectSqliteExecutor } from './executor.js';\n\n// -------------------------------------------------------\n// Types\n// -------------------------------------------------------\n\nexport type DoSQLiteRecordAdapterOpenOptions = {\n /**\n * Entity ID of the stack owner. A plain string is checked against an\n * existing config record (mismatch throws OwnerMismatchError); a lazy\n * provider is called only when the DO's storage has none yet.\n */\n ownerEntityId: string | (() => string | Promise<string>);\n /** IANA timezone string e.g. \"America/New_York\". Optional passthrough app metadata — no default. */\n timezone?: string;\n};\n\n// -------------------------------------------------------\n// DoSQLiteRecordAdapter\n// -------------------------------------------------------\n\nexport class DoSQLiteRecordAdapter extends SharedSqlRecordAdapter {\n private constructor(exec: DurableObjectSqliteExecutor, config: StackConfig) {\n super(exec, config);\n }\n\n /**\n * Open the adapter for a DO instance, creating its storage on first use.\n * A DO id either already has a config record (reattach) or it doesn't\n * (first call — opts.ownerEntityId/timezone become the config), so there\n * is no `create` option. Schema DDL is `CREATE TABLE IF NOT EXISTS`, so\n * running it every call is idempotent and cheap.\n *\n * DO SQLite manages its own durability and rejects PRAGMA journal_mode\n * outright (\"not authorized\") — verified against the real runtime, not\n * assumed — hence `wal: false`, unlike record-adapter-sqlite.\n */\n static async open(\n storage: DurableObjectStorage,\n opts: DoSQLiteRecordAdapterOpenOptions,\n ): Promise<DoSQLiteRecordAdapter> {\n const exec = new DurableObjectSqliteExecutor(storage);\n applyRecordSchema(exec, { wal: false });\n let config = tryReadStackConfig(exec);\n if (config) {\n if (typeof opts.ownerEntityId === 'string' && opts.ownerEntityId !== config.entityId) {\n throw new OwnerMismatchError(\n opts.ownerEntityId,\n config.entityId,\n 'the Durable Object storage',\n );\n }\n } else {\n const ownerEntityId =\n typeof opts.ownerEntityId === 'function' ? await opts.ownerEntityId() : opts.ownerEntityId;\n config = insertConfigRecord(exec, ownerEntityId, opts.timezone);\n }\n return new DoSQLiteRecordAdapter(exec, config);\n }\n\n // -------------------------------------------------------\n // Lifecycle\n // -------------------------------------------------------\n\n /** No-op: every write through ctx.storage.sql is already durable. */\n async flush(): Promise<void> {}\n\n /** No-op: no lock file, no connection to release — the DO's own lifecycle governs storage. */\n async close(): Promise<void> {}\n}\n\nexport { DurableObjectSqliteExecutor } from './executor.js';\n"]}