@orkestrel/database 0.0.9 → 0.0.11

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.
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["#source","#scope","#continue","#next","#return","#throw","#cleaned","#cleanup","#operations","#accepting","#failed","#error","#driver","#name","#version","#error","#emitter","#operations","#status","#transaction","#ready","#schema","#outside","#drain","#connect","#admit","#rollbackError","#transition","#failure","#reconcile","#stamp","#apply","#migration","#keys","#read","#update","#remove","#track","#value","#index","#closed","#queue","#advance","#revise","#delete","#tail","#source","#context","#continue","#next","#return","#throw","#cleaned","#cleanup","#table","#conditions","#orders","#filters","#limit","#offset","#page","#filtered","#ready","#driver","#name","#key","#contract","#guard","#generate","#context","#scope","#emitter","#track","#each","#read","#resolveOne","#collect","#scan","#wait","#put","#updateOne","#delete","#readCursor","#updateCursor","#deleteCursor","#cast","#validate","#prepare","#resolveKey","#driver","#tables","#primary","#generate","#error","#scope","#columns","#build","#key","#tables","#primary","#indexes","#generate","#context","#schema","#columns","#build","#key","#spawn","#settle","#attach","#tables","#metadata","#identities","#schema","#store","#table","#ordered","#stream","#copy","#projectIdentities","#migrate","#require"],"sources":["../../../src/core/constants.ts","../../../src/core/errors.ts","../../../src/core/validators.ts","../../../src/core/cloners.ts","../../../src/core/helpers.ts","../../../src/core/TransactionIterator.ts","../../../src/core/TransactionScope.ts","../../../src/core/DatabaseContext.ts","../../../src/core/Cursor.ts","../../../src/core/DatabaseIterator.ts","../../../src/core/Query.ts","../../../src/core/Table.ts","../../../src/core/DatabaseTransaction.ts","../../../src/core/Database.ts","../../../src/core/drivers/MemoryDriver.ts","../../../src/core/factories.ts"],"sourcesContent":["// Database constants — frozen plain data (AGENTS §5).\n\n/**\n * The primary-key column assumed when {@link PrimaryMap} does not name one.\n *\n * @remarks\n * `id` is the convention IndexedDB (`keyPath: 'id'`) and SQL (`id` / rowid) both\n * lean on, so a table without a `primary` override keys its rows by `id`.\n */\nexport const DEFAULT_PRIMARY = 'id'\n\n/**\n * The longest `LIKE` / `GLOB` pattern the wildcard matcher accepts before rejecting it.\n *\n * @remarks\n * A ReDoS bound (AGENTS §6.5): the SA1–SA4 migration lets a model supply `list`\n * input over the wire, so `matchesLikePattern` / `matchesGlobPattern` run attacker-controlled\n * patterns. The matcher is the LINEAR greedy two-pointer wildcard match — never a\n * backtracking regex (`.*`-segments-separated-by-literals against a long input is the\n * catastrophic shape JS cannot bound without atomic groups), so it is O(value ×\n * pattern). Capping the pattern length bounds that pattern factor, leaving a match\n * linear in the value length whatever the pattern. A longer pattern throws a\n * `VALIDATION` {@link DatabaseError}; the cap is generous for any legitimate search.\n */\nexport const MAX_PATTERN_LENGTH = 1024\n","import type { DatabaseErrorCode } from './types.js'\n\n// AGENTS §12: invalid operations and programmer errors `throw`, always a\n// `DatabaseError` carrying a machine-readable `code` so a `catch` branches on\n// `error.code` instead of parsing the message. Lookups that may simply miss\n// (`get`, `has`, `remove`) return `undefined` / `false` — they never throw.\n\n/**\n * An error thrown by the database layer.\n *\n * @remarks\n * Carries a {@link DatabaseErrorCode} and an optional `context` bag naming the\n * offending table / key. Thrown for: operating on a closed database (`CLOSED`), a\n * `resolve` miss (`NOT_FOUND`), an `add` onto an existing key (`CONFLICT`), a\n * row that fails its table's contract (`VALIDATION`), an aborted operation whose\n * {@link OperationOptions.signal} aborted (`ABORTED`, carrying `signal.reason` in\n * `context`), an inapplicable {@link Migration} plan (`MIGRATION`), a\n * driver that violates a {@link DriverInterface} invariant, thrown by the\n * `conformDriver` helper (`CONFORMANCE`), and an unexpected infrastructure\n * fault surfaced by a driver seam — e.g. a filesystem failure while\n * persisting (`DRIVER`) — as opposed to expected domain conditions, which\n * keep their specific codes.\n */\nexport class DatabaseError extends Error {\n\treadonly code: DatabaseErrorCode\n\treadonly context?: Readonly<Record<string, unknown>>\n\n\tconstructor(\n\t\tcode: DatabaseErrorCode,\n\t\tmessage: string,\n\t\tcontext?: Readonly<Record<string, unknown>>,\n\t) {\n\t\tsuper(message)\n\t\tthis.name = 'DatabaseError'\n\t\tthis.code = code\n\t\tif (context !== undefined) this.context = context\n\t}\n}\n\n/**\n * Narrow an unknown caught value to a {@link DatabaseError}.\n *\n * @param value - The value to test (typically a `catch` binding)\n * @returns `true` when `value` is a {@link DatabaseError}\n *\n * @example\n * ```ts\n * try {\n * \tawait users.add(row)\n * } catch (error) {\n * \tif (isDatabaseError(error) && error.code === 'CONFLICT') await users.set(row)\n * }\n * ```\n */\nexport function isDatabaseError(value: unknown): value is DatabaseError {\n\treturn value instanceof DatabaseError\n}\n","import type {\n\tColumnSchema,\n\tDriverMetadata,\n\tKey,\n\tMigration,\n\tMigrationInput,\n\tMigrationStep,\n\tQueryInput,\n\tTableSchema,\n} from './types.js'\nimport { cloneJSONRecord, cloneJSONValue } from '@orkestrel/contract'\nimport { DatabaseError } from './errors.js'\n\n/**\n * Validate the paging fields of a portable query.\n *\n * @remarks\n * A present `limit` or `offset` must be a finite nonnegative integer; zero is\n * valid. Validation is deterministic (`limit` before `offset`). Non-finite\n * values are rendered as strings in error context so JSON serialization cannot\n * collapse `NaN` or infinity to `null`.\n *\n * @param input - The portable query whose paging fields to validate\n * @throws {@link DatabaseError} `VALIDATION` when a paging field is invalid\n */\nexport function validatePage(input?: QueryInput): void {\n\tconst limit = input?.limit\n\tif (limit !== undefined && (!Number.isInteger(limit) || limit < 0)) {\n\t\tthrow new DatabaseError('VALIDATION', 'Query limit must be a nonnegative integer', {\n\t\t\tfield: 'limit',\n\t\t\tvalue: Number.isFinite(limit) ? limit : String(limit),\n\t\t})\n\t}\n\tconst offset = input?.offset\n\tif (offset !== undefined && (!Number.isInteger(offset) || offset < 0)) {\n\t\tthrow new DatabaseError('VALIDATION', 'Query offset must be a nonnegative integer', {\n\t\t\tfield: 'offset',\n\t\t\tvalue: Number.isFinite(offset) ? offset : String(offset),\n\t\t})\n\t}\n}\n\n/**\n * Test whether a value is a usable database key.\n *\n * @param value - The value to test\n * @returns Whether `value` is a string or finite number\n */\nexport function isKey(value: unknown): value is Key {\n\treturn typeof value === 'string' || (typeof value === 'number' && Number.isFinite(value))\n}\n\n/**\n * Test whether a value is a portable column schema.\n *\n * @param value - The value to test\n * @returns Whether `value` is a complete {@link ColumnSchema}\n */\nexport function isColumnSchema(value: unknown): value is ColumnSchema {\n\ttry {\n\t\tconst column = cloneJSONRecord(value)\n\t\tconst keys = Object.keys(column)\n\t\treturn (\n\t\t\tkeys.length === 4 &&\n\t\t\tkeys.includes('name') &&\n\t\t\tkeys.includes('storage') &&\n\t\t\tkeys.includes('optional') &&\n\t\t\tkeys.includes('nullable') &&\n\t\t\ttypeof column.name === 'string' &&\n\t\t\tcolumn.name.length > 0 &&\n\t\t\t(column.storage === 'text' ||\n\t\t\t\tcolumn.storage === 'integer' ||\n\t\t\t\tcolumn.storage === 'real' ||\n\t\t\t\tcolumn.storage === 'boolean' ||\n\t\t\t\tcolumn.storage === 'json' ||\n\t\t\t\tcolumn.storage === 'blob') &&\n\t\t\ttypeof column.optional === 'boolean' &&\n\t\t\ttypeof column.nullable === 'boolean'\n\t\t)\n\t} catch {\n\t\treturn false\n\t}\n}\n\n/**\n * Test whether a value is a portable table schema.\n *\n * @param value - The value to test\n * @returns Whether `value` is a complete {@link TableSchema}\n */\nexport function isTableSchema(value: unknown): value is TableSchema {\n\ttry {\n\t\tconst table = cloneJSONRecord(value)\n\t\tconst keys = Object.keys(table)\n\t\tif (\n\t\t\tkeys.length !== 4 ||\n\t\t\t!keys.includes('name') ||\n\t\t\t!keys.includes('primary') ||\n\t\t\t!keys.includes('columns') ||\n\t\t\t!keys.includes('indexes') ||\n\t\t\ttypeof table.name !== 'string' ||\n\t\t\ttable.name.length === 0 ||\n\t\t\ttypeof table.primary !== 'string' ||\n\t\t\ttable.primary.length === 0 ||\n\t\t\t!Array.isArray(table.columns) ||\n\t\t\t!Array.isArray(table.indexes) ||\n\t\t\t!table.columns.every(isColumnSchema)\n\t\t) {\n\t\t\treturn false\n\t\t}\n\t\tconst names = table.columns.map((column) => column.name)\n\t\tif (\n\t\t\tnew Set(names).size !== names.length ||\n\t\t\t!names.includes(table.primary) ||\n\t\t\t!table.indexes.every(\n\t\t\t\t(index) =>\n\t\t\t\t\tArray.isArray(index) &&\n\t\t\t\t\tindex.length > 0 &&\n\t\t\t\t\tindex.every((column) => typeof column === 'string' && names.includes(column)),\n\t\t\t)\n\t\t) {\n\t\t\treturn false\n\t\t}\n\t\tconst indexes = table.indexes.map((index) => JSON.stringify(index))\n\t\treturn new Set(indexes).size === indexes.length\n\t} catch {\n\t\treturn false\n\t}\n}\n\n/**\n * Test whether a value is a complete portable driver schema.\n *\n * @param value - The value to test\n * @returns Whether `value` is a table-schema collection with unique table names\n */\nexport function isDriverSchema(value: unknown): value is readonly TableSchema[] {\n\ttry {\n\t\tconst schema = cloneJSONValue(value)\n\t\tif (!Array.isArray(schema) || !schema.every(isTableSchema)) return false\n\t\tconst names = schema.map((table) => table.name)\n\t\treturn new Set(names).size === names.length\n\t} catch {\n\t\treturn false\n\t}\n}\n\n/**\n * Test whether a value is one ordered migration step.\n *\n * @param value - The value to test\n * @returns Whether `value` is a complete {@link MigrationStep}\n */\nexport function isMigrationStep(value: unknown): value is MigrationStep {\n\ttry {\n\t\tconst step = cloneJSONRecord(value)\n\t\tif (typeof step.operation !== 'string') return false\n\t\tconst keys = Object.keys(step)\n\t\tswitch (step.operation) {\n\t\t\tcase 'table.add':\n\t\t\t\treturn (\n\t\t\t\t\tkeys.length === 2 &&\n\t\t\t\t\tkeys.includes('operation') &&\n\t\t\t\t\tkeys.includes('table') &&\n\t\t\t\t\tisTableSchema(step.table)\n\t\t\t\t)\n\t\t\tcase 'table.remove':\n\t\t\t\treturn (\n\t\t\t\t\tkeys.length === 2 &&\n\t\t\t\t\tkeys.includes('operation') &&\n\t\t\t\t\tkeys.includes('table') &&\n\t\t\t\t\ttypeof step.table === 'string' &&\n\t\t\t\t\tstep.table.length > 0\n\t\t\t\t)\n\t\t\tcase 'column.add':\n\t\t\t\treturn (\n\t\t\t\t\tkeys.length === 3 &&\n\t\t\t\t\tkeys.includes('operation') &&\n\t\t\t\t\tkeys.includes('table') &&\n\t\t\t\t\tkeys.includes('column') &&\n\t\t\t\t\ttypeof step.table === 'string' &&\n\t\t\t\t\tstep.table.length > 0 &&\n\t\t\t\t\tisColumnSchema(step.column)\n\t\t\t\t)\n\t\t\tcase 'column.remove':\n\t\t\t\treturn (\n\t\t\t\t\tkeys.length === 3 &&\n\t\t\t\t\tkeys.includes('operation') &&\n\t\t\t\t\tkeys.includes('table') &&\n\t\t\t\t\tkeys.includes('column') &&\n\t\t\t\t\ttypeof step.table === 'string' &&\n\t\t\t\t\tstep.table.length > 0 &&\n\t\t\t\t\ttypeof step.column === 'string' &&\n\t\t\t\t\tstep.column.length > 0\n\t\t\t\t)\n\t\t\tcase 'index.add':\n\t\t\tcase 'index.remove':\n\t\t\t\treturn (\n\t\t\t\t\tkeys.length === 3 &&\n\t\t\t\t\tkeys.includes('operation') &&\n\t\t\t\t\tkeys.includes('table') &&\n\t\t\t\t\tkeys.includes('index') &&\n\t\t\t\t\ttypeof step.table === 'string' &&\n\t\t\t\t\tstep.table.length > 0 &&\n\t\t\t\t\tArray.isArray(step.index) &&\n\t\t\t\t\tstep.index.length > 0 &&\n\t\t\t\t\tstep.index.every((column) => typeof column === 'string' && column.length > 0)\n\t\t\t\t)\n\t\t\tdefault:\n\t\t\t\treturn false\n\t\t}\n\t} catch {\n\t\treturn false\n\t}\n}\n\n/**\n * Test whether a value is an ordered migration plan.\n *\n * @param value - The value to test\n * @returns Whether `value` is a complete {@link Migration}\n */\nexport function isMigration(value: unknown): value is Migration {\n\ttry {\n\t\tconst migration = cloneJSONRecord(value)\n\t\tconst keys = Object.keys(migration)\n\t\treturn (\n\t\t\tkeys.length === 3 &&\n\t\t\tkeys.includes('from') &&\n\t\t\tkeys.includes('to') &&\n\t\t\tkeys.includes('steps') &&\n\t\t\ttypeof migration.from === 'number' &&\n\t\t\tNumber.isFinite(migration.from) &&\n\t\t\ttypeof migration.to === 'number' &&\n\t\t\tNumber.isFinite(migration.to) &&\n\t\t\tArray.isArray(migration.steps) &&\n\t\t\tmigration.steps.every(isMigrationStep)\n\t\t)\n\t} catch {\n\t\treturn false\n\t}\n}\n\n/**\n * Test whether a value is persisted driver metadata.\n *\n * @param value - The value to test\n * @returns Whether `value` is complete {@link DriverMetadata}\n */\nexport function isDriverMetadata(value: unknown): value is DriverMetadata {\n\ttry {\n\t\tconst metadata = cloneJSONRecord(value)\n\t\tconst keys = Object.keys(metadata)\n\t\treturn (\n\t\t\tkeys.length === 2 &&\n\t\t\tkeys.includes('version') &&\n\t\t\tkeys.includes('schema') &&\n\t\t\ttypeof metadata.version === 'number' &&\n\t\t\tNumber.isFinite(metadata.version) &&\n\t\t\tisDriverSchema(metadata.schema)\n\t\t)\n\t} catch {\n\t\treturn false\n\t}\n}\n\n/**\n * Test whether a value is one atomic migration request.\n *\n * @param value - The value to test\n * @returns Whether `value` is a complete {@link MigrationInput}\n */\nexport function isMigrationInput(value: unknown): value is MigrationInput {\n\ttry {\n\t\tconst input = cloneJSONRecord(value)\n\t\tconst keys = Object.keys(input)\n\t\treturn (\n\t\t\t(keys.length === 1 || keys.length === 2) &&\n\t\t\tkeys.includes('plan') &&\n\t\t\t(keys.length === 1 || keys.includes('metadata')) &&\n\t\t\tisMigration(input.plan) &&\n\t\t\t(input.metadata === undefined || isDriverMetadata(input.metadata))\n\t\t)\n\t} catch {\n\t\treturn false\n\t}\n}\n","import type { DriverMetadata, MigrationInput, TableSchema } from './types.js'\nimport { cloneJSONRecord, cloneJSONValue } from '@orkestrel/contract'\nimport { DatabaseError } from './errors.js'\nimport { isDriverMetadata, isDriverSchema, isMigrationInput } from './validators.js'\n\n/**\n * Clone unknown driver metadata into a distinct deeply frozen snapshot.\n *\n * @param value - Unknown metadata\n * @returns Owned driver metadata\n */\nexport function cloneDriverMetadata(value: unknown): DriverMetadata {\n\ttry {\n\t\tconst metadata = cloneJSONRecord(value)\n\t\tif (isDriverMetadata(metadata)) return metadata\n\t\tthrow new DatabaseError('VALIDATION', 'Driver metadata is invalid', {\n\t\t\tpath: 'metadata',\n\t\t})\n\t} catch (error) {\n\t\tif (error instanceof DatabaseError) throw error\n\t\tthrow new DatabaseError('VALIDATION', 'Driver metadata is invalid', {\n\t\t\tpath: 'metadata',\n\t\t\tcause: error,\n\t\t})\n\t}\n}\n\n/**\n * Clone unknown driver schema into a distinct deeply frozen snapshot.\n *\n * @param value - Unknown table schema collection\n * @returns Owned driver schema\n */\nexport function cloneDriverSchema(value: unknown): readonly TableSchema[] {\n\ttry {\n\t\tconst schema = cloneJSONValue(value)\n\t\tif (isDriverSchema(schema)) return schema\n\t\tthrow new DatabaseError('VALIDATION', 'Driver schema is invalid', {\n\t\t\tpath: 'schema',\n\t\t})\n\t} catch (error) {\n\t\tif (error instanceof DatabaseError) throw error\n\t\tthrow new DatabaseError('VALIDATION', 'Driver schema is invalid', {\n\t\t\tpath: 'schema',\n\t\t\tcause: error,\n\t\t})\n\t}\n}\n\n/**\n * Clone unknown migration input into a distinct deeply frozen snapshot.\n *\n * @param value - Unknown migration input\n * @returns Owned migration input\n */\nexport function cloneMigrationInput(value: unknown): MigrationInput {\n\ttry {\n\t\tconst input = cloneJSONRecord(value)\n\t\tif (isMigrationInput(input)) return input\n\t\tthrow new DatabaseError('VALIDATION', 'Migration input is invalid', {\n\t\t\tpath: 'migration',\n\t\t})\n\t} catch (error) {\n\t\tif (error instanceof DatabaseError) throw error\n\t\tthrow new DatabaseError('VALIDATION', 'Migration input is invalid', {\n\t\t\tpath: 'migration',\n\t\t\tcause: error,\n\t\t})\n\t}\n}\n","import type { ContractShape, FieldPath } from '@orkestrel/contract'\nimport type {\n\tAggregateOperation,\n\tColumnSchema,\n\tColumnStorage,\n\tConformanceFinding,\n\tCondition,\n\tQueryInput,\n\tDriverInterface,\n\tKey,\n\tMigration,\n\tMigrationStep,\n\tOrder,\n\tRow,\n\tTableSchema,\n} from './types.js'\nimport {\n\tcompileGuard,\n\tisRecord,\n\tisString,\n\tobjectShape,\n\tparseNumber,\n\tresolveField,\n} from '@orkestrel/contract'\nimport { MAX_PATTERN_LENGTH } from './constants.js'\nimport { cloneDriverSchema, cloneMigrationInput } from './cloners.js'\nimport { DatabaseError, isDatabaseError } from './errors.js'\nimport { isKey, validatePage } from './validators.js'\n\n// The query engine. Every backend's `scan` yields rows; these pure helpers do\n// the filtering, ordering, paging, and aggregation once, so a driver never\n// re-implements WHERE compilation. They are total — like the contracts guards\n// they lean on, hostile input yields a `false` / a skipped value, never a throw.\n\n// === Comparison\n\n/**\n * A total ordering over arbitrary values — the comparator behind sorting and the\n * range operators.\n *\n * @remarks\n * Values of different types order by a fixed type rank (`undefined` < `null` <\n * boolean < number < string < other); same-typed values compare naturally.\n * `NaN` sorts after every other number and equal to itself, so the comparator\n * is total and never returns `NaN`.\n *\n * @param left - The left value\n * @param right - The right value\n * @returns `-1`, `0`, or `1`\n */\nexport function compareValues(left: unknown, right: unknown): number {\n\t// Rank unlike types so a mixed column still sorts deterministically:\n\t// undefined < null < boolean < number < string < other.\n\t// Mapping two inputs always produces two ranks; the defaults only satisfy\n\t// unchecked indexed destructuring and are semantically unreachable.\n\tconst [leftRank = 5, rightRank = 5] = [left, right].map((value) =>\n\t\tvalue === undefined\n\t\t\t? 0\n\t\t\t: value === null\n\t\t\t\t? 1\n\t\t\t\t: typeof value === 'boolean'\n\t\t\t\t\t? 2\n\t\t\t\t\t: typeof value === 'number'\n\t\t\t\t\t\t? 3\n\t\t\t\t\t\t: typeof value === 'string'\n\t\t\t\t\t\t\t? 4\n\t\t\t\t\t\t\t: 5,\n\t)\n\tif (leftRank !== rightRank) return leftRank < rightRank ? -1 : 1\n\tif (typeof left === 'number' && typeof right === 'number') {\n\t\tif (Number.isNaN(left) || Number.isNaN(right)) {\n\t\t\treturn Number.isNaN(left) ? (Number.isNaN(right) ? 0 : 1) : -1\n\t\t}\n\t\treturn left < right ? -1 : left > right ? 1 : 0\n\t}\n\tif (typeof left === 'string' && typeof right === 'string') {\n\t\treturn left < right ? -1 : left > right ? 1 : 0\n\t}\n\tif (typeof left === 'boolean' && typeof right === 'boolean') {\n\t\treturn left === right ? 0 : left ? 1 : -1\n\t}\n\treturn 0\n}\n\n/**\n * Structural equality by SameValueZero leaves — the comparator behind conformance\n * checks and any test/fixture that needs \"same data\", not \"same reference\".\n *\n * @remarks\n * Primitives compare by SameValueZero (`NaN` equals itself; `+0` equals `-0`).\n * Arrays compare by index (same length, every element `equalsValue`). Plain\n * records (via `isRecord`) compare by their OWN enumerable keys: same key\n * COUNT and, for every key in `left`, `right` has that key (`Object.hasOwn`)\n * with a `equalsValue` value — so a key present with value `undefined` is NOT\n * equal to that key being absent (both differ in `Object.keys` membership).\n * Anything else (functions, class instances, mismatched shapes) falls through\n * to `false`. Container pairs are tracked iteratively, so self-referential and\n * mutually cyclic arrays/records terminate without consuming the call stack.\n * Hostile proxy traps and accessors are contained as a non-match.\n *\n * @param left - The left value\n * @param right - The right value\n * @returns Whether `left` and `right` are structurally equal\n *\n * @example\n * ```ts\n * equalsValue(Number.NaN, Number.NaN) // true\n * equalsValue({ a: [1, { b: 2 }] }, { a: [1, { b: 2 }] }) // true\n * equalsValue({ a: undefined }, {}) // false — present-undefined ≠ absent\n * ```\n */\nexport function equalsValue(left: unknown, right: unknown): boolean {\n\tconst pending: Array<readonly [unknown, unknown]> = [[left, right]]\n\tconst compared = new WeakMap<object, WeakSet<object>>()\n\ttry {\n\t\twhile (pending.length > 0) {\n\t\t\tconst pair = pending.pop()\n\t\t\tif (pair === undefined) continue\n\t\t\tconst [currentLeft, currentRight] = pair\n\t\t\tif (typeof currentLeft === 'number' && typeof currentRight === 'number') {\n\t\t\t\tif (\n\t\t\t\t\t(Number.isNaN(currentLeft) && Number.isNaN(currentRight)) ||\n\t\t\t\t\tcurrentLeft === currentRight\n\t\t\t\t) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\treturn false\n\t\t\t}\n\t\t\tif (currentLeft === currentRight) continue\n\n\t\t\tconst leftArray = Array.isArray(currentLeft)\n\t\t\tconst rightArray = Array.isArray(currentRight)\n\t\t\tconst leftRecord = isRecord(currentLeft)\n\t\t\tconst rightRecord = isRecord(currentRight)\n\t\t\tif (leftArray !== rightArray || leftRecord !== rightRecord) return false\n\t\t\tif ((!leftArray && !leftRecord) || (!rightArray && !rightRecord)) return false\n\n\t\t\tconst prior = compared.get(currentLeft)\n\t\t\tif (prior?.has(currentRight)) continue\n\t\t\tif (prior === undefined) {\n\t\t\t\tcompared.set(currentLeft, new WeakSet([currentRight]))\n\t\t\t} else {\n\t\t\t\tprior.add(currentRight)\n\t\t\t}\n\n\t\t\tif (leftArray && rightArray) {\n\t\t\t\tif (currentLeft.length !== currentRight.length) return false\n\t\t\t\tfor (let index = 0; index < currentLeft.length; index += 1) {\n\t\t\t\t\tconst leftOwn = Object.hasOwn(currentLeft, index)\n\t\t\t\t\tconst rightOwn = Object.hasOwn(currentRight, index)\n\t\t\t\t\tif (leftOwn !== rightOwn) return false\n\t\t\t\t\tif (leftOwn) pending.push([currentLeft[index], currentRight[index]])\n\t\t\t\t}\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif (leftRecord && rightRecord) {\n\t\t\t\tconst leftKeys = Object.keys(currentLeft)\n\t\t\t\tconst rightKeys = Object.keys(currentRight)\n\t\t\t\tif (leftKeys.length !== rightKeys.length) return false\n\t\t\t\tfor (const key of leftKeys) {\n\t\t\t\t\tif (!Object.hasOwn(currentRight, key)) return false\n\t\t\t\t\tpending.push([currentLeft[key], currentRight[key]])\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn true\n\t} catch {\n\t\treturn false\n\t}\n}\n\n// === Pattern matching\n\n/**\n * Match a value against a wildcard pattern in LINEAR time — the shared, ReDoS-SAFE\n * engine behind {@link matchesLikePattern} and {@link matchesGlobPattern}.\n *\n * @remarks\n * A backtracking RegExp (`a%b%c` → `^a.*b.*c$`) is CATASTROPHIC on a hostile pattern:\n * `.*` segments separated by literals, matched against a long non-matching input, blow\n * up super-linearly — and JS has no atomic groups / possessive quantifiers to bound it\n * (AGENTS §6.5, now that the authed server runs model-supplied `list` input over the\n * wire). So this builds NO regex. It runs the classic GREEDY TWO-POINTER wildcard match:\n * the `any` wildcard records its position and, on a later mismatch, backtracks ONLY to\n * that last `any` (letting it absorb one more char) — so the work is O(value × pattern),\n * never the exponential / polynomial backtracking a regex would do. The pattern length\n * is capped at {@link MAX_PATTERN_LENGTH} (a `VALIDATION` {@link DatabaseError} over it),\n * bounding the pattern factor so a match stays linear in the value length whatever the\n * pattern.\n *\n * The `any` wildcard matches any run (including empty); `single` matches exactly one\n * char; every other pattern char matches itself LITERALLY (a pattern `.` / `(` / `\\` is\n * a literal — the regex-metacharacter hazard is gone with the regex). `any` is tested\n * BEFORE a literal match, so a value that literally contains the wildcard char never\n * shadows the wildcard. Case folding is applied to BOTH sides when `fold` is set.\n *\n * @param value - The value to test\n * @param pattern - The wildcard pattern\n * @param any - The any-run wildcard char (`%` for `LIKE`, `*` for `GLOB`)\n * @param single - The single-char wildcard char (`_` for `LIKE`, `?` for `GLOB`)\n * @param fold - Whether to match case-INSENSITIVELY (`LIKE` folds; `GLOB` does not)\n * @returns Whether `value` matches `pattern`\n * @throws A `VALIDATION` {@link DatabaseError} when `pattern` exceeds {@link MAX_PATTERN_LENGTH}\n */\nexport function matchesWildcardPattern(\n\tvalue: string,\n\tpattern: string,\n\tany: string,\n\tsingle: string,\n\tfold: boolean,\n): boolean {\n\tif (pattern.length > MAX_PATTERN_LENGTH) {\n\t\tthrow new DatabaseError(\n\t\t\t'VALIDATION',\n\t\t\t`Pattern exceeds the maximum length of ${MAX_PATTERN_LENGTH}`,\n\t\t\t{ length: pattern.length, limit: MAX_PATTERN_LENGTH },\n\t\t)\n\t}\n\tconst haystack = fold ? value.toLowerCase() : value\n\tconst needle = fold ? pattern.toLowerCase() : pattern\n\tlet vi = 0\n\tlet pi = 0\n\t// The greedy backtrack point: the pattern index of the LAST `any` wildcard + the value\n\t// index it was taken at. On a mismatch we resume just past it and let it absorb one more\n\t// char (`mark += 1`) — O(value × pattern), never a regex's exponential backtracking.\n\tlet star = -1\n\tlet mark = 0\n\twhile (vi < haystack.length) {\n\t\tconst pc = pi < needle.length ? needle[pi] : undefined\n\t\tif (pc === any) {\n\t\t\t// Record the wildcard (it absorbs zero chars for now) and advance the pattern.\n\t\t\tstar = pi\n\t\t\tmark = vi\n\t\t\tpi += 1\n\t\t} else if (pc !== undefined && (pc === single || pc === haystack[vi])) {\n\t\t\tvi += 1\n\t\t\tpi += 1\n\t\t} else if (star !== -1) {\n\t\t\t// Mismatch under an open `any`: let it swallow one more value char and retry.\n\t\t\tpi = star + 1\n\t\t\tmark += 1\n\t\t\tvi = mark\n\t\t} else {\n\t\t\treturn false\n\t\t}\n\t}\n\t// The value is consumed — the leftover pattern matches iff it is all `any` wildcards.\n\twhile (pi < needle.length && needle[pi] === any) pi += 1\n\treturn pi === needle.length\n}\n\n// SQL `LIKE` → case-INSENSITIVE wildcard match (`%` any run, `_` any char).\nexport function matchesLikePattern(value: string, pattern: string): boolean {\n\treturn matchesWildcardPattern(value, pattern, '%', '_', true)\n}\n\n// `GLOB` → case-SENSITIVE wildcard match (`*` any run, `?` any char).\nexport function matchesGlobPattern(value: string, pattern: string): boolean {\n\treturn matchesWildcardPattern(value, pattern, '*', '?', false)\n}\n\n// === Condition matching\n\n/**\n * Evaluate one {@link Condition} against a row — the per-operator predicate.\n *\n * @remarks\n * Reads the condition's column — a `FieldPath`, resolved with `resolveField` (a\n * string is one column; an array descends a nested value) — and applies the\n * operator. Range operators (`above` / `below` / `from` / `to` / `between`) use\n * {@link compareValues}, the total order; the equality family (`equals` / `not`\n * / `any` / `none`) uses {@link equalsValue} — STRUCTURAL equality, not the total\n * order's rank-5-collapses-all-objects behavior, so `equals` on an object/array\n * operand only matches a structurally-equal value, never every row holding any\n * object. This is a semantics change from ranking: `equalsValue` is SameValueZero\n * on leaves, so `NaN` now equals `NaN` under `equals` / `any` (it never matched\n * anything under the old rank-based comparison). `like` / `glob` / `starts` /\n * `ends` match only strings; `absent` / `present` test nullishness. Total — a\n * type mismatch is simply a non-match.\n *\n * @param row - The row to test\n * @param condition - The condition to apply\n * @returns Whether the row satisfies the condition\n */\nexport function matchesCondition(row: Row, condition: Condition): boolean {\n\tconst value = resolveField(row, condition.column)\n\tconst first = condition.values[0]\n\tconst second = condition.values[1]\n\tswitch (condition.operator) {\n\t\tcase 'equals':\n\t\t\treturn equalsValue(value, first)\n\t\tcase 'not':\n\t\t\treturn !equalsValue(value, first)\n\t\tcase 'above':\n\t\t\treturn compareValues(value, first) > 0\n\t\tcase 'below':\n\t\t\treturn compareValues(value, first) < 0\n\t\tcase 'from':\n\t\t\treturn compareValues(value, first) >= 0\n\t\tcase 'to':\n\t\t\treturn compareValues(value, first) <= 0\n\t\tcase 'between':\n\t\t\treturn compareValues(value, first) >= 0 && compareValues(value, second) <= 0\n\t\tcase 'like':\n\t\t\treturn isString(value) && isString(first) && matchesLikePattern(value, first)\n\t\tcase 'glob':\n\t\t\treturn isString(value) && isString(first) && matchesGlobPattern(value, first)\n\t\tcase 'starts':\n\t\t\treturn isString(value) && isString(first) && value.startsWith(first)\n\t\tcase 'ends':\n\t\t\treturn isString(value) && isString(first) && value.endsWith(first)\n\t\tcase 'any':\n\t\t\treturn condition.values.some((candidate) => equalsValue(value, candidate))\n\t\tcase 'none':\n\t\t\treturn !condition.values.some((candidate) => equalsValue(value, candidate))\n\t\tcase 'absent':\n\t\t\treturn value === undefined || value === null\n\t\tcase 'present':\n\t\t\treturn value !== undefined && value !== null\n\t}\n}\n\n/**\n * Fold a row through a list of conditions, joining each by its connector.\n *\n * @remarks\n * Evaluated left-to-right: the first condition seeds the result, and each later\n * condition combines with `&&` (`and`) or `||` (`or`). An empty list matches\n * every row. There is no operator precedence — conditions combine in the order\n * the query builder recorded them.\n *\n * @param row - The row to test\n * @param conditions - The conditions to fold\n * @returns Whether the row satisfies the combined conditions\n */\nexport function matchesQuery(row: Row, conditions: readonly Condition[]): boolean {\n\tlet result = true\n\tlet seeded = false\n\tfor (const condition of conditions) {\n\t\tconst match = matchesCondition(row, condition)\n\t\tif (!seeded) {\n\t\t\tresult = match\n\t\t\tseeded = true\n\t\t} else {\n\t\t\tresult = condition.connector === 'or' ? result || match : result && match\n\t\t}\n\t}\n\treturn result\n}\n\n/**\n * Filter rows by a list of conditions — the shared basis for a table's count\n * and aggregate paths (no sort/page, unlike {@link applyQuery}).\n *\n * @remarks\n * An empty condition list matches every row (returned as-is, no copy). Folds\n * each row through {@link matchesQuery}.\n *\n * @param rows - The rows to filter\n * @param conditions - The conditions to apply (empty matches everything)\n * @returns The matching rows\n *\n * @example\n * ```ts\n * filterRows(\n * \t[{ age: 30 }, { age: 12 }],\n * \t[{ column: 'age', operator: 'above', values: [18], connector: 'and' }],\n * ) // => [{ age: 30 }]\n * ```\n */\nexport function filterRows(rows: readonly Row[], conditions: readonly Condition[]): readonly Row[] {\n\tif (conditions.length === 0) return rows\n\treturn rows.filter((row) => matchesQuery(row, conditions))\n}\n\n// === Ordering & paging\n\n/**\n * Sort rows by an ordering specification, leaving the input untouched.\n *\n * @remarks\n * Applies the terms in priority order — the first term that distinguishes two\n * rows decides — using {@link compareValues}, reversing for `descending`.\n *\n * @param rows - The rows to sort\n * @param order - The ordering terms in priority order\n * @returns A new, sorted array\n */\nexport function sortRows(rows: readonly Row[], order: readonly Order[]): readonly Row[] {\n\tconst sorted = [...rows]\n\tsorted.sort((left, right) => {\n\t\tfor (const term of order) {\n\t\t\tconst comparison = compareValues(\n\t\t\t\tresolveField(left, term.column),\n\t\t\t\tresolveField(right, term.column),\n\t\t\t)\n\t\t\tif (comparison !== 0) return term.direction === 'descending' ? -comparison : comparison\n\t\t}\n\t\treturn 0\n\t})\n\treturn sorted\n}\n\n/**\n * Apply a {@link QueryInput} to rows — filter, then sort, then page.\n *\n * @remarks\n * The whole portable read pipeline in one place: conditions filter, `order`\n * sorts, and `offset` / `limit` window the result. Each step is skipped when its\n * part of the input is absent. The reference {@link DriverInterface} backends\n * lean on this rather than each re-deriving it.\n *\n * @param rows - The rows to process (typically a table's full `scan`)\n * @param input - The read specification, or `undefined` for all rows as-is\n * @returns The filtered, sorted, paged rows\n */\nexport function applyQuery(rows: readonly Row[], input?: QueryInput): readonly Row[] {\n\tvalidatePage(input)\n\tlet result = rows\n\tconst conditions = input?.conditions\n\tif (conditions !== undefined && conditions.length > 0) {\n\t\tresult = result.filter((row) => matchesQuery(row, conditions))\n\t}\n\tconst order = input?.order\n\tif (order !== undefined && order.length > 0) {\n\t\tresult = sortRows(result, order)\n\t}\n\tconst offset = input?.offset ?? 0\n\tconst limit = input?.limit\n\tif (offset > 0 || limit !== undefined) {\n\t\tresult = result.slice(offset, limit !== undefined ? offset + limit : undefined)\n\t}\n\treturn result\n}\n\n// === Aggregation\n\n/**\n * Compute an aggregate over a column across rows.\n *\n * @remarks\n * `count` returns the row count. The numeric aggregates coerce each cell with\n * the contracts `parseNumber` (so `'42'` counts) and ignore non-numeric cells;\n * over zero numeric values they return `undefined` — the SQL `NULL` of an empty\n * aggregate.\n *\n * @param rows - The rows to aggregate (non-record entries are ignored)\n * @param operation - The aggregate to compute\n * @param column - The column to aggregate\n * @returns The aggregate value, or `undefined` when undefined for the inputs\n */\nexport function computeAggregate(\n\trows: readonly unknown[],\n\toperation: AggregateOperation,\n\tcolumn: FieldPath,\n): number | undefined {\n\tif (operation === 'count') return rows.length\n\tconst numbers: number[] = []\n\tfor (const row of rows) {\n\t\tif (!isRecord(row)) continue\n\t\tconst value = parseNumber(resolveField(row, column))\n\t\tif (value !== undefined) numbers.push(value)\n\t}\n\tif (numbers.length === 0) return undefined\n\tif (operation === 'sum' || operation === 'average') {\n\t\tconst total = numbers.reduce((sum, value) => sum + value, 0)\n\t\treturn operation === 'average' ? total / numbers.length : total\n\t}\n\treturn operation === 'minimum' ? Math.min(...numbers) : Math.max(...numbers)\n}\n\n// === Keys\n\n/**\n * Read a row's primary key from a column, when it is a usable {@link Key}.\n *\n * @param row - The row to read\n * @param column - The primary-key column name\n * @returns The key (a string or finite number), or `undefined`\n */\nexport function extractKey(row: Row, column: string): Key | undefined {\n\tconst value = row[column]\n\treturn isKey(value) ? value : undefined\n}\n\n/**\n * Return a fresh row whose primary column is authoritatively bound to its storage key.\n *\n * @param row - The caller row\n * @param primary - The primary column\n * @param key - The authoritative storage key\n * @returns A fresh row with the bound primary\n */\nexport function bindRowKey(row: Row, primary: string, key: Key): Row {\n\treturn { ...row, [primary]: key }\n}\n\n// === Schema\n\n/**\n * Map a column's {@link ContractShape} to its portable {@link ColumnStorage} — the\n * value a `TableSchema` carries so a native backend can declare a real column.\n *\n * @remarks\n * `string` → `text`; `number` → `integer` when the shape is integer-only, else\n * `real`; `boolean` → `boolean`. A `literal` takes the type of its values\n * (all-boolean → `boolean`, all-integer → `integer`, mixed/fractional numbers →\n * `real`, anything else → `text`). `optional` / `nullable` unwrap to their inner\n * type (nullability is tracked separately). `null` / `object` / `array` / `union` /\n * `json` / `raw` → `json`: a backend stores them as JSON text and can `json_extract`\n * for nested `FieldPath` queries. A scan-only backend ignores the result.\n *\n * @param shape - The column's contract shape\n * @returns The portable column type\n *\n * @example\n * ```ts\n * shapeToColumnStorage(stringShape()) // 'text'\n * shapeToColumnStorage(integerShape()) // 'integer'\n * shapeToColumnStorage(optionalShape(integerShape())) // 'integer'\n * shapeToColumnStorage(objectShape({ a: stringShape() })) // 'json'\n * ```\n */\nexport function shapeToColumnStorage(shape: ContractShape): ColumnStorage {\n\tswitch (shape.type) {\n\t\tcase 'string':\n\t\t\treturn 'text'\n\t\tcase 'number':\n\t\t\treturn shape.integer === true ? 'integer' : 'real'\n\t\tcase 'boolean':\n\t\t\treturn 'boolean'\n\t\tcase 'literal': {\n\t\t\tif (shape.values.every((value) => typeof value === 'boolean')) return 'boolean'\n\t\t\tif (shape.values.every((value) => typeof value === 'number')) {\n\t\t\t\treturn shape.values.every((value) => Number.isInteger(value)) ? 'integer' : 'real'\n\t\t\t}\n\t\t\treturn 'text'\n\t\t}\n\t\tcase 'optional':\n\t\tcase 'nullable':\n\t\t\treturn shapeToColumnStorage(shape.inner)\n\t\tcase 'null':\n\t\tcase 'object':\n\t\tcase 'array':\n\t\tcase 'union':\n\t\tcase 'json':\n\t\tcase 'raw':\n\t\t\treturn 'json'\n\t}\n}\n\n/**\n * Project one contract shape into a portable column schema.\n *\n * @param name - The column name\n * @param shape - The column contract shape\n * @returns The portable storage and independent absence/null acceptance\n */\nexport function shapeToColumnSchema(name: string, shape: ContractShape): ColumnSchema {\n\tconst isColumn = compileGuard(objectShape({ value: shape }))\n\treturn {\n\t\tname,\n\t\tstorage: shapeToColumnStorage(shape),\n\t\toptional: isColumn({}),\n\t\tnullable: isColumn({ value: null }),\n\t}\n}\n\n// === Abort\n\n/**\n * Throw when an {@link OperationOptions.signal | AbortSignal} has fired — the shared\n * abort gate checked at operation boundaries and between streamed rows.\n *\n * @remarks\n * A no-op for `undefined` or a live signal, so callers thread `options?.signal`\n * straight through. When the signal has aborted, throws an `ABORTED`\n * {@link DatabaseError} carrying the signal's `reason` in its context — callers\n * mint signals with native APIs such as `AbortSignal.timeout(ms)` or\n * `new AbortController()`.\n *\n * @param signal - The signal to check, if any\n * @returns Nothing — returns normally while the signal is live\n * @throws An `ABORTED` {@link DatabaseError} when the signal has aborted\n *\n * @example\n * ```ts\n * import { checkAbort } from '@orkestrel/database'\n *\n * const controller = new AbortController()\n * checkAbort(controller.signal) // returns\n * controller.abort('too slow')\n * checkAbort(controller.signal) // throws DatabaseError('ABORTED', …)\n * ```\n */\nexport function checkAbort(signal: AbortSignal | undefined): void {\n\tif (signal?.aborted) {\n\t\tthrow new DatabaseError('ABORTED', 'Operation aborted', { reason: signal.reason })\n\t}\n}\n\n// === Migrations\n\n/**\n * Structurally diff a deployed and a declared table set into a {@link Migration}\n * plan.\n *\n * @remarks\n * Tables present in `declared` but not `deployed` become `table.add` steps\n * (carrying the full declared {@link TableSchema}); tables present in\n * `deployed` but not `declared` become `table.remove` steps. Tables present in\n * both are diffed column-by-column (by name) and index-group-by-index-group\n * (by deep equality of the column-name array), each producing `column.add` /\n * `column.remove` / `index.add` / `index.remove` steps. Step order is\n * deterministic: every `table.remove`, then every `table.add`, then each\n * shared table's column/index changes in `declared` order. `from` / `to` are\n * plan labels only; versioning drivers persist and reconcile them through\n * {@link DriverMetadata}.\n *\n * A column present in BOTH schemas under the same name but with a different\n * `storage`, `optional`, or `nullable` value throws a `MIGRATION`\n * {@link DatabaseError} naming the\n * table, the column, and the from→to difference — a name-only diff would\n * otherwise silently produce NO step for the drift, and versioned\n * reconciliation would stamp over it. There is no automatic in-place\n * type-change step: the manual path is to add a new column, copy/convert the\n * data at the application layer, then remove the old column — two separate\n * plans, never a single implicit \"alter\" step.\n *\n * @param deployed - The table schemas currently applied\n * @param declared - The table schemas the caller wants applied\n * @param from - The plan's source version label (defaults to `0`)\n * @param to - The plan's target version label (defaults to `1`)\n * @returns The migration plan moving `deployed` toward `declared`\n * @throws A `MIGRATION` {@link DatabaseError} when a shared table's primary or\n * a shared column's `storage`, `optional`, or `nullable` differs, or when a\n * required non-null column would be added to an existing table without a\n * portable backfill\n *\n * @example\n * ```ts\n * const plan = planMigration(\n * \t[{ name: 'users', primary: 'id', columns: [{ name: 'id', storage: 'text', optional: false, nullable: false }], indexes: [] }],\n * \t[{ name: 'users', primary: 'id', columns: [{ name: 'id', storage: 'text', optional: false, nullable: false }, { name: 'age', storage: 'integer', optional: true, nullable: false }], indexes: [] }],\n * )\n * // plan.steps === [{ operation: 'column.add', table: 'users', column: { name: 'age', ... } }]\n * ```\n */\nexport function planMigration(\n\tdeployed: readonly TableSchema[],\n\tdeclared: readonly TableSchema[],\n\tfrom = 0,\n\tto = 1,\n): Migration {\n\tif (!Number.isFinite(from) || !Number.isFinite(to)) {\n\t\tthrow new DatabaseError('MIGRATION', 'Migration versions must be finite', { from, to })\n\t}\n\tlet beforeSchema: readonly TableSchema[]\n\tlet targetSchema: readonly TableSchema[]\n\ttry {\n\t\tbeforeSchema = normalizeDriverSchema(deployed)\n\t\ttargetSchema = normalizeDriverSchema(declared)\n\t} catch (error) {\n\t\tthrow new DatabaseError('MIGRATION', 'Migration schema is invalid', { cause: error })\n\t}\n\tconst deployedByName = new Map(beforeSchema.map((table) => [table.name, table]))\n\tconst declaredByName = new Map(targetSchema.map((table) => [table.name, table]))\n\n\tconst steps: MigrationStep[] = []\n\n\tfor (const table of beforeSchema) {\n\t\tif (!declaredByName.has(table.name))\n\t\t\tsteps.push({ operation: 'table.remove', table: table.name })\n\t}\n\tfor (const table of targetSchema) {\n\t\tif (!deployedByName.has(table.name)) steps.push({ operation: 'table.add', table })\n\t}\n\n\tfor (const table of targetSchema) {\n\t\tconst before = deployedByName.get(table.name)\n\t\tif (before === undefined) continue\n\t\tif (before.primary !== table.primary) {\n\t\t\tthrow new DatabaseError(\n\t\t\t\t'MIGRATION',\n\t\t\t\t`planMigration: primary column on table '${table.name}' changed from '${before.primary}' to '${table.primary}'`,\n\t\t\t\t{ table: table.name, from: before.primary, to: table.primary },\n\t\t\t)\n\t\t}\n\n\t\tconst beforeColumnMap = new Map(before.columns.map((column) => [column.name, column]))\n\t\tconst afterColumnMap = new Map(table.columns.map((column) => [column.name, column]))\n\n\t\tfor (const index of before.indexes) {\n\t\t\tif (!table.indexes.some((candidate) => equalsValue(candidate, index))) {\n\t\t\t\tsteps.push({ operation: 'index.remove', table: table.name, index })\n\t\t\t}\n\t\t}\n\t\tfor (const column of before.columns) {\n\t\t\tif (!afterColumnMap.has(column.name)) {\n\t\t\t\tsteps.push({ operation: 'column.remove', table: table.name, column: column.name })\n\t\t\t}\n\t\t}\n\t\tfor (const column of table.columns) {\n\t\t\tconst previous = beforeColumnMap.get(column.name)\n\t\t\tif (previous === undefined) {\n\t\t\t\tsteps.push({ operation: 'column.add', table: table.name, column })\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif (\n\t\t\t\tprevious.storage !== column.storage ||\n\t\t\t\tprevious.optional !== column.optional ||\n\t\t\t\tprevious.nullable !== column.nullable\n\t\t\t) {\n\t\t\t\tthrow new DatabaseError(\n\t\t\t\t\t'MIGRATION',\n\t\t\t\t\t`planMigration: column '${column.name}' on table '${table.name}' changed shape ` +\n\t\t\t\t\t\t`(storage ${previous.storage}→${column.storage}, optional ${previous.optional}→${column.optional}, nullable ${previous.nullable}→${column.nullable}) — ` +\n\t\t\t\t\t\t`in-place storage/optionality/nullability changes are not auto-migrated; add a new column, copy/convert ` +\n\t\t\t\t\t\t`the data, then remove the old column`,\n\t\t\t\t\t{\n\t\t\t\t\t\ttable: table.name,\n\t\t\t\t\t\tcolumn: column.name,\n\t\t\t\t\t\tfrom: {\n\t\t\t\t\t\t\tstorage: previous.storage,\n\t\t\t\t\t\t\toptional: previous.optional,\n\t\t\t\t\t\t\tnullable: previous.nullable,\n\t\t\t\t\t\t},\n\t\t\t\t\t\tto: {\n\t\t\t\t\t\t\tstorage: column.storage,\n\t\t\t\t\t\t\toptional: column.optional,\n\t\t\t\t\t\t\tnullable: column.nullable,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t)\n\t\t\t}\n\t\t}\n\n\t\tfor (const index of table.indexes) {\n\t\t\tif (!before.indexes.some((candidate) => equalsValue(candidate, index))) {\n\t\t\t\tsteps.push({ operation: 'index.add', table: table.name, index })\n\t\t\t}\n\t\t}\n\t}\n\n\tconst projected = projectMigrationSchema(beforeSchema, steps)\n\tif (!equalsValue(projected, targetSchema)) {\n\t\tthrow new DatabaseError('MIGRATION', 'Migration plan does not project to the declared schema', {\n\t\t\tprojected,\n\t\t\tdeclared: targetSchema,\n\t\t})\n\t}\n\treturn cloneMigrationInput({ plan: { from, to, steps } }).plan\n}\n\n/**\n * Sequentially project migration steps over a canonical validated owned schema.\n * Adding a required non-null column to an existing table rejects with\n * `MIGRATION`; optional-only and nullable-only additions remain portable.\n *\n * @param schema - The initial deployed schema\n * @param steps - The ordered migration steps\n * @returns A fresh owned final schema\n */\nexport function projectMigrationSchema(\n\tschema: readonly TableSchema[],\n\tsteps: readonly MigrationStep[],\n): readonly TableSchema[] {\n\tlet owned: readonly TableSchema[]\n\tlet projectedSteps: readonly MigrationStep[]\n\ttry {\n\t\towned = normalizeDriverSchema(schema)\n\t\tprojectedSteps = cloneMigrationInput({ plan: { from: 0, to: 1, steps } }).plan.steps\n\t} catch (error) {\n\t\tthrow new DatabaseError('MIGRATION', 'Migration input is invalid', { cause: error })\n\t}\n\tconst tables = new Map(owned.map((table) => [table.name, table]))\n\tfor (const step of projectedSteps) {\n\t\tif (step.operation === 'table.add') {\n\t\t\tif (tables.has(step.table.name)) {\n\t\t\t\tthrow new DatabaseError('MIGRATION', `migrate: table '${step.table.name}' already exists`, {\n\t\t\t\t\ttable: step.table.name,\n\t\t\t\t})\n\t\t\t}\n\t\t\ttables.set(step.table.name, step.table)\n\t\t\tcontinue\n\t\t}\n\t\tconst table = tables.get(step.table)\n\t\tif (table === undefined) {\n\t\t\tthrow new DatabaseError('MIGRATION', `migrate: table '${step.table}' does not exist`, {\n\t\t\t\ttable: step.table,\n\t\t\t})\n\t\t}\n\t\tif (step.operation === 'table.remove') {\n\t\t\ttables.delete(step.table)\n\t\t\tcontinue\n\t\t}\n\t\tif (step.operation === 'column.add') {\n\t\t\tif (table.columns.some((column) => column.name === step.column.name)) {\n\t\t\t\tthrow new DatabaseError(\n\t\t\t\t\t'MIGRATION',\n\t\t\t\t\t`migrate: column '${step.column.name}' already exists`,\n\t\t\t\t\t{ table: step.table, column: step.column.name },\n\t\t\t\t)\n\t\t\t}\n\t\t\tif (!step.column.optional && !step.column.nullable) {\n\t\t\t\tthrow new DatabaseError(\n\t\t\t\t\t'MIGRATION',\n\t\t\t\t\t`migrate: required non-null column '${step.column.name}' cannot be added automatically to existing table '${step.table}'`,\n\t\t\t\t\t{ table: step.table, column: step.column.name },\n\t\t\t\t)\n\t\t\t}\n\t\t\ttables.set(step.table, { ...table, columns: [...table.columns, step.column] })\n\t\t\tcontinue\n\t\t}\n\t\tif (step.operation === 'column.remove') {\n\t\t\tif (!table.columns.some((column) => column.name === step.column)) {\n\t\t\t\tthrow new DatabaseError('MIGRATION', `migrate: column '${step.column}' does not exist`, {\n\t\t\t\t\ttable: step.table,\n\t\t\t\t\tcolumn: step.column,\n\t\t\t\t})\n\t\t\t}\n\t\t\tif (table.primary === step.column) {\n\t\t\t\tthrow new DatabaseError('MIGRATION', 'migrate: cannot remove the primary column', {\n\t\t\t\t\ttable: step.table,\n\t\t\t\t\tcolumn: step.column,\n\t\t\t\t})\n\t\t\t}\n\t\t\tif (table.indexes.some((index) => index.includes(step.column))) {\n\t\t\t\tthrow new DatabaseError('MIGRATION', 'migrate: cannot remove an indexed column', {\n\t\t\t\t\ttable: step.table,\n\t\t\t\t\tcolumn: step.column,\n\t\t\t\t})\n\t\t\t}\n\t\t\ttables.set(step.table, {\n\t\t\t\t...table,\n\t\t\t\tcolumns: table.columns.filter((column) => column.name !== step.column),\n\t\t\t})\n\t\t\tcontinue\n\t\t}\n\t\tif (step.operation === 'index.add') {\n\t\t\tif (\n\t\t\t\tstep.index.length === 0 ||\n\t\t\t\tstep.index.some((name) => !table.columns.some((column) => column.name === name))\n\t\t\t) {\n\t\t\t\tthrow new DatabaseError('MIGRATION', 'migrate: index references a missing column', {\n\t\t\t\t\ttable: step.table,\n\t\t\t\t\tindex: step.index,\n\t\t\t\t})\n\t\t\t}\n\t\t\tif (table.indexes.some((index) => equalsValue(index, step.index))) {\n\t\t\t\tthrow new DatabaseError('MIGRATION', 'migrate: index already exists', {\n\t\t\t\t\ttable: step.table,\n\t\t\t\t\tindex: step.index,\n\t\t\t\t})\n\t\t\t}\n\t\t\ttables.set(step.table, { ...table, indexes: [...table.indexes, step.index] })\n\t\t\tcontinue\n\t\t}\n\t\tif (!table.indexes.some((index) => equalsValue(index, step.index))) {\n\t\t\tthrow new DatabaseError('MIGRATION', 'migrate: index does not exist', {\n\t\t\t\ttable: step.table,\n\t\t\t\tindex: step.index,\n\t\t\t})\n\t\t}\n\t\ttables.set(step.table, {\n\t\t\t...table,\n\t\t\tindexes: table.indexes.filter((index) => !equalsValue(index, step.index)),\n\t\t})\n\t}\n\ttry {\n\t\treturn normalizeDriverSchema([...tables.values()])\n\t} catch (error) {\n\t\tthrow new DatabaseError('MIGRATION', 'Projected migration schema is invalid', { cause: error })\n\t}\n}\n\n/**\n * Canonicalize an unknown driver schema into a distinct deeply frozen snapshot.\n *\n * @remarks\n * Table and column lists are sorted by name. The index list is sorted by the\n * complete serialized tuple while column order inside each compound index is\n * preserved because it carries index semantics. Validation and ownership flow\n * through {@link cloneDriverSchema} before and after projection.\n *\n * @param value - Unknown driver schema\n * @returns A validated, owned canonical schema\n */\nexport function normalizeDriverSchema(value: unknown): readonly TableSchema[] {\n\tconst owned = cloneDriverSchema(value)\n\tconst tables = owned.map((table) => ({\n\t\tname: table.name,\n\t\tprimary: table.primary,\n\t\tcolumns: [...table.columns].sort((left, right) => compareValues(left.name, right.name)),\n\t\tindexes: [...table.indexes].sort((left, right) =>\n\t\t\tcompareValues(JSON.stringify(left), JSON.stringify(right)),\n\t\t),\n\t}))\n\ttables.sort((left, right) => compareValues(left.name, right.name))\n\treturn cloneDriverSchema(tables)\n}\n\n/**\n * Apply one table's {@link MigrationStep}s to its rows — a pure row transform.\n *\n * @remarks\n * `column.remove` drops that field from every row (a fresh copy — inputs are\n * never mutated, AGENTS §11); `column.add` leaves rows as-is (an absent field\n * reads as `undefined`, backfill is application policy). `table.add` /\n * `table.remove` / `index.add` / `index.remove` are no-ops here (they operate\n * on storage shape, not row shape). Steps for tables other than the one\n * `rows` belongs to are ignored — pass only the steps relevant to this table.\n *\n * @param rows - The table's current rows\n * @param steps - The migration steps to apply (typically one table's slice of a {@link Migration})\n * @returns A new array of transformed rows; `rows` is never mutated\n *\n * @example\n * ```ts\n * const rows = [{ id: 'a', name: 'Ada', legacy: true }]\n * migrateRows(rows, [{ operation: 'column.remove', table: 'users', column: 'legacy' }])\n * // => [{ id: 'a', name: 'Ada' }]\n * ```\n */\nexport function migrateRows(rows: readonly Row[], steps: readonly MigrationStep[]): readonly Row[] {\n\tconst removed = steps\n\t\t.filter(\n\t\t\t(step): step is Extract<MigrationStep, { operation: 'column.remove' }> =>\n\t\t\t\tstep.operation === 'column.remove',\n\t\t)\n\t\t.map((step) => step.column)\n\n\tif (removed.length === 0) return rows.map((row) => ({ ...row }))\n\n\treturn rows.map((row) => {\n\t\tconst next: Row = {}\n\t\tfor (const key of Object.keys(row)) {\n\t\t\tif (!removed.includes(key)) next[key] = row[key]\n\t\t}\n\t\treturn next\n\t})\n}\n\n// === Conformance\n\n/**\n * Run the driver-conformance battery against a fresh {@link DriverInterface}\n * per phase, yielding one {@link ConformanceFinding} per violated invariant —\n * the shared invariant suite every backend (in-memory, SQLite, IndexedDB)\n * must uphold to be a drop-in {@link DriverInterface}.\n *\n * @remarks\n * Framework-agnostic: no test-runner or Node imports, only sibling core\n * modules — so it runs equally from a unit test, a smoke script, or a new\n * driver's own README. Opens a fixed two-table schema (`users` keyed by the\n * default `id`, `posts` keyed by a non-id `slug`) and, calling `factory()`\n * fresh for each phase so failures stay isolated, verifies: `open`/`close`;\n * `read` of a missing key returns `undefined`; `write`/`read` round-trip with\n * DEEP copy-in/copy-out isolation (mutating the caller's row — including a\n * NESTED field — after `write`, or a row `read` returns, never perturbs\n * stored state) and upsert-overwrite; simultaneous same-key `insert` calls\n * produce exactly one commit and one `CONFLICT`; pre-aborted `write`,\n * `insert`, and `delete` calls leave storage unchanged; `delete` returns\n * `true` then `false`;\n * `keys`/`scan` yield in ascending key order; `clear` empties only its target\n * table; `snapshot`'s rollback thunk restores pre-snapshot state, including a\n * NESTED field mutated in place on a read-back row between capture and\n * restore; a scoped `snapshot(['users'])` rolls back only the named table,\n * leaving a concurrent mutation to another table intact; a\n * non-`id` primary key (`posts.slug`) round-trips; a nested-object row\n * round-trips structurally (via {@link equalsValue}). The optional surface is\n * presence-gated: when `migrate` exists, a `column.remove` plan strips the\n * column from stored rows and a plan referencing an unknown table throws\n * `DatabaseError` `MIGRATION`; when `stream` exists, it yields only\n * condition-matching rows and honors `offset`/`limit`; when `transaction`\n * exists, `commit` persists and `rollback` restores; when both `metadata` and\n * `stamp` exist, a fresh store's `metadata()` is `undefined`, and after\n * `stamp({ version, schema })`, `metadata()` returns the exact stamped value.\n *\n * Each phase runs within a `try`/`catch`: an EXPECTED mismatch yields a\n * finding built from the assertion, while an UNEXPECTED throw (a driver\n * crash mid-phase) is caught and yielded as a finding too, naming the phase\n * as `check` and carrying the caught error in `context.error` — a broken\n * driver can never escape the battery as an unhandled rejection. Within a\n * phase, the FIRST violated assertion yields and the phase stops (matching\n * the historical fail-fast shape at phase granularity); the generator then\n * moves on to the next phase regardless. Because this is a **generator**,\n * consuming only the first yielded value reproduces true fail-fast (later\n * phases never run) — that is exactly what {@link conformDriver} does.\n *\n * @param factory - Mints a fresh, unopened driver instance (called once per phase)\n * @yields One {@link ConformanceFinding} per violated invariant, in phase order\n *\n * @example\n * ```ts\n * import { createMemoryDriver, driverFindings } from '@orkestrel/database'\n *\n * for await (const finding of driverFindings(() => createMemoryDriver())) {\n * \tconsole.log(finding.check, finding.message)\n * }\n * ```\n */\nexport async function* driverFindings(\n\tfactory: () => DriverInterface,\n): AsyncIterable<ConformanceFinding> {\n\t// Fixed two-table schema every phase opens: `users` keyed by `id` (the\n\t// default primary), `posts` keyed by a non-id `slug` — exercising both\n\t// primary-key shapes in one battery.\n\tconst CONFORMANCE_USERS_SCHEMA: TableSchema = {\n\t\tname: 'users',\n\t\tprimary: 'id',\n\t\tcolumns: [\n\t\t\t{ name: 'id', storage: 'text', optional: false, nullable: false },\n\t\t\t{ name: 'name', storage: 'text', optional: false, nullable: false },\n\t\t\t{ name: 'age', storage: 'integer', optional: true, nullable: false },\n\t\t\t// Declared so the nested-roundtrip phase is fair to typed-column\n\t\t\t// backends (a SQL driver persists only declared columns; schemaless\n\t\t\t// backends ignore declarations entirely).\n\t\t\t{ name: 'meta', storage: 'json', optional: true, nullable: false },\n\t\t],\n\t\tindexes: [],\n\t}\n\tconst CONFORMANCE_POSTS_SCHEMA: TableSchema = {\n\t\tname: 'posts',\n\t\tprimary: 'slug',\n\t\tcolumns: [\n\t\t\t{ name: 'slug', storage: 'text', optional: false, nullable: false },\n\t\t\t{ name: 'title', storage: 'text', optional: false, nullable: false },\n\t\t],\n\t\tindexes: [],\n\t}\n\tconst CONFORMANCE_SCHEMA: readonly TableSchema[] = [\n\t\tCONFORMANCE_USERS_SCHEMA,\n\t\tCONFORMANCE_POSTS_SCHEMA,\n\t]\n\t// a. open with the inline two-table schema, then close cleanly.\n\ttry {\n\t\tconst driver = factory()\n\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\tawait driver.close()\n\t} catch (error) {\n\t\tyield {\n\t\t\tcheck: 'open-close',\n\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\tcontext: { error },\n\t\t}\n\t}\n\n\t// b. read of a missing key -> undefined.\n\ttry {\n\t\tconst driver = factory()\n\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\tconst missing = await driver.read('users', 'nope')\n\t\tawait driver.close()\n\t\tif (missing !== undefined) {\n\t\t\tyield {\n\t\t\t\tcheck: 'read-missing',\n\t\t\t\tmessage: 'read of a missing key must return undefined',\n\t\t\t\tcontext: { table: 'users', expected: undefined, actual: missing },\n\t\t\t}\n\t\t}\n\t} catch (error) {\n\t\tyield {\n\t\t\tcheck: 'read-missing',\n\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\tcontext: { error },\n\t\t}\n\t}\n\n\t// c. write/read round-trip, copy-in/copy-out isolation (including NESTED\n\t// fields, not just top-level ones), upsert-overwrite.\n\twriteRead: {\n\t\ttry {\n\t\t\tconst driver = factory()\n\t\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\t\tconst input: Row = { id: 'caller', name: 'Ada', age: 30, meta: { tags: ['a'] } }\n\t\t\tawait driver.write('users', 'u1', input)\n\t\t\tinput.name = 'Mutated after write'\n\t\t\tif (isRecord(input.meta) && Array.isArray(input.meta.tags)) input.meta.tags.push('mutated')\n\t\t\tconst stored = await driver.read('users', 'u1')\n\t\t\tconst original = { id: 'u1', name: 'Ada', age: 30, meta: { tags: ['a'] } }\n\t\t\tif (stored === undefined || !equalsValue(stored, original)) {\n\t\t\t\tawait driver.close()\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'copy-in',\n\t\t\t\t\tmessage:\n\t\t\t\t\t\t'write must deep-copy the input row (including nested fields) rather than store it by reference',\n\t\t\t\t\tcontext: { table: 'users', expected: original, actual: stored },\n\t\t\t\t}\n\t\t\t\tbreak writeRead\n\t\t\t}\n\t\t\tstored.name = 'Mutated after read'\n\t\t\tif (isRecord(stored.meta) && Array.isArray(stored.meta.tags)) stored.meta.tags.push('mutated')\n\t\t\tconst reread = await driver.read('users', 'u1')\n\t\t\tif (reread === undefined || !equalsValue(reread, original)) {\n\t\t\t\tawait driver.close()\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'copy-out',\n\t\t\t\t\tmessage:\n\t\t\t\t\t\t'read must deep-copy the stored row (including nested fields) rather than return it by reference',\n\t\t\t\t\tcontext: { table: 'users', expected: original, actual: reread },\n\t\t\t\t}\n\t\t\t\tbreak writeRead\n\t\t\t}\n\t\t\tconst overwrite = { id: 'caller', name: 'Ada Overwritten', age: 31 }\n\t\t\tawait driver.write('users', 'u1', overwrite)\n\t\t\tconst overwritten = await driver.read('users', 'u1')\n\t\t\tawait driver.close()\n\t\t\tconst expectedOverwrite = { ...overwrite, id: 'u1' }\n\t\t\tif (overwritten === undefined || !equalsValue(overwritten, expectedOverwrite)) {\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'upsert',\n\t\t\t\t\tmessage: 'write must upsert-overwrite an existing key',\n\t\t\t\t\tcontext: { table: 'users', expected: expectedOverwrite, actual: overwritten },\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tyield {\n\t\t\t\tcheck: 'write-read',\n\t\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\t\tcontext: { error },\n\t\t\t}\n\t\t}\n\t}\n\n\t// c2. insert is atomic: concurrent duplicates produce one commit and one CONFLICT.\n\t{\n\t\ttry {\n\t\t\tconst driver = factory()\n\t\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\t\tconst outcomes = await Promise.allSettled([\n\t\t\t\tdriver.insert('users', 'u1', { id: 'u1', name: 'Ada', age: 30 }),\n\t\t\t\tdriver.insert('users', 'u1', { id: 'u1', name: 'Grace', age: 40 }),\n\t\t\t])\n\t\t\tlet fulfilled = 0\n\t\t\tlet conflicted = 0\n\t\t\tfor (const outcome of outcomes) {\n\t\t\t\tif (outcome.status === 'fulfilled') fulfilled += 1\n\t\t\t\telse if (isDatabaseError(outcome.reason) && outcome.reason.code === 'CONFLICT') {\n\t\t\t\t\tconflicted += 1\n\t\t\t\t}\n\t\t\t}\n\t\t\tconst keys = await driver.keys('users')\n\t\t\tawait driver.close()\n\t\t\tif (fulfilled !== 1 || conflicted !== 1 || !equalsValue(keys, ['u1'])) {\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'insert-atomic',\n\t\t\t\t\tmessage: 'concurrent same-key inserts must produce one commit and one CONFLICT',\n\t\t\t\t\tcontext: {\n\t\t\t\t\t\texpected: { fulfilled: 1, conflicted: 1, keys: ['u1'] },\n\t\t\t\t\t\tactual: { fulfilled, conflicted, keys },\n\t\t\t\t\t},\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tyield {\n\t\t\t\tcheck: 'insert-atomic',\n\t\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\t\tcontext: { error },\n\t\t\t}\n\t\t}\n\t}\n\n\t// d. delete -> true then false.\n\tdeletePhase: {\n\t\ttry {\n\t\t\tconst driver = factory()\n\t\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\t\tawait driver.write('users', 'u1', { id: 'u1', name: 'Ada', age: 30 })\n\t\t\tconst first = await driver.delete('users', 'u1')\n\t\t\tif (first !== true) {\n\t\t\t\tawait driver.close()\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'delete-true',\n\t\t\t\t\tmessage: 'delete of an existing key must return true',\n\t\t\t\t\tcontext: { table: 'users', expected: true, actual: first },\n\t\t\t\t}\n\t\t\t\tbreak deletePhase\n\t\t\t}\n\t\t\tconst second = await driver.delete('users', 'u1')\n\t\t\tawait driver.close()\n\t\t\tif (second !== false) {\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'delete-false',\n\t\t\t\t\tmessage: 'delete of an already-removed key must return false',\n\t\t\t\t\tcontext: { table: 'users', expected: false, actual: second },\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tyield {\n\t\t\t\tcheck: 'delete',\n\t\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\t\tcontext: { error },\n\t\t\t}\n\t\t}\n\t}\n\n\t// d2. pre-aborted point mutations reject as ABORTED without changing rows.\n\ttry {\n\t\tconst driver = factory()\n\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\tawait driver.write('users', 'u1', { id: 'u1', name: 'Ada', age: 30 })\n\t\tconst controller = new AbortController()\n\t\tcontroller.abort('conformance abort')\n\t\tlet writeError: unknown\n\t\tlet insertError: unknown\n\t\tlet deleteError: unknown\n\t\ttry {\n\t\t\tawait driver.write(\n\t\t\t\t'users',\n\t\t\t\t'u2',\n\t\t\t\t{ id: 'u2', name: 'Grace', age: 40 },\n\t\t\t\t{ signal: controller.signal },\n\t\t\t)\n\t\t} catch (error) {\n\t\t\twriteError = error\n\t\t}\n\t\ttry {\n\t\t\tawait driver.insert(\n\t\t\t\t'users',\n\t\t\t\t'u2',\n\t\t\t\t{ id: 'u2', name: 'Grace', age: 40 },\n\t\t\t\t{ signal: controller.signal },\n\t\t\t)\n\t\t} catch (error) {\n\t\t\tinsertError = error\n\t\t}\n\t\ttry {\n\t\t\tawait driver.delete('users', 'u1', { signal: controller.signal })\n\t\t} catch (error) {\n\t\t\tdeleteError = error\n\t\t}\n\t\tconst keys = await driver.keys('users')\n\t\tawait driver.close()\n\t\tif (\n\t\t\t!isDatabaseError(writeError) ||\n\t\t\twriteError.code !== 'ABORTED' ||\n\t\t\t!isDatabaseError(insertError) ||\n\t\t\tinsertError.code !== 'ABORTED' ||\n\t\t\t!isDatabaseError(deleteError) ||\n\t\t\tdeleteError.code !== 'ABORTED' ||\n\t\t\t!equalsValue(keys, ['u1'])\n\t\t) {\n\t\t\tyield {\n\t\t\t\tcheck: 'mutation-abort',\n\t\t\t\tmessage: 'pre-aborted write/insert/delete must reject ABORTED without changing rows',\n\t\t\t\tcontext: {\n\t\t\t\t\texpected: { write: 'ABORTED', insert: 'ABORTED', delete: 'ABORTED', keys: ['u1'] },\n\t\t\t\t\tactual: {\n\t\t\t\t\t\twrite: isDatabaseError(writeError) ? writeError.code : writeError,\n\t\t\t\t\t\tinsert: isDatabaseError(insertError) ? insertError.code : insertError,\n\t\t\t\t\t\tdelete: isDatabaseError(deleteError) ? deleteError.code : deleteError,\n\t\t\t\t\t\tkeys,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t}\n\t\t}\n\t} catch (error) {\n\t\tyield {\n\t\t\tcheck: 'mutation-abort',\n\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\tcontext: { error },\n\t\t}\n\t}\n\n\t// e. keys and scan in ascending key order.\n\torderPhase: {\n\t\ttry {\n\t\t\tconst driver = factory()\n\t\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\t\tconst rows = [\n\t\t\t\t{ id: 'c', name: 'C', age: 3 },\n\t\t\t\t{ id: 'a', name: 'A', age: 1 },\n\t\t\t\t{ id: 'b', name: 'B', age: 2 },\n\t\t\t]\n\t\t\tfor (const row of rows) await driver.write('users', row.id, row)\n\t\t\tconst expected = ['a', 'b', 'c']\n\t\t\tconst keys = [...(await driver.keys('users'))]\n\t\t\tif (!equalsValue(keys, expected)) {\n\t\t\t\tawait driver.close()\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'keys-order',\n\t\t\t\t\tmessage: 'keys must be returned in ascending key order',\n\t\t\t\t\tcontext: { table: 'users', expected, actual: keys },\n\t\t\t\t}\n\t\t\t\tbreak orderPhase\n\t\t\t}\n\t\t\tconst scanned: Row[] = []\n\t\t\tfor await (const row of driver.scan('users')) scanned.push(row)\n\t\t\tconst scannedIds = scanned.map((row) => row.id)\n\t\t\tawait driver.close()\n\t\t\tif (!equalsValue(scannedIds, expected)) {\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'scan-order',\n\t\t\t\t\tmessage: 'scan must yield rows in ascending key order',\n\t\t\t\t\tcontext: { table: 'users', expected, actual: scannedIds },\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tyield {\n\t\t\t\tcheck: 'order',\n\t\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\t\tcontext: { error },\n\t\t\t}\n\t\t}\n\t}\n\n\t// f. clear empties only the targeted table.\n\tclearPhase: {\n\t\ttry {\n\t\t\tconst driver = factory()\n\t\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\t\tawait driver.write('users', 'u1', { id: 'u1', name: 'Ada', age: 30 })\n\t\t\tawait driver.write('posts', 'p1', { slug: 'p1', title: 'Post' })\n\t\t\tawait driver.clear('users')\n\t\t\tconst usersKeys = await driver.keys('users')\n\t\t\tconst postsKeys = await driver.keys('posts')\n\t\t\tawait driver.close()\n\t\t\tif (usersKeys.length !== 0) {\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'clear-target',\n\t\t\t\t\tmessage: 'clear must empty the targeted table',\n\t\t\t\t\tcontext: { table: 'users', expected: [], actual: usersKeys },\n\t\t\t\t}\n\t\t\t\tbreak clearPhase\n\t\t\t}\n\t\t\tif (postsKeys.length !== 1) {\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'clear-other',\n\t\t\t\t\tmessage: 'clear must not affect other tables',\n\t\t\t\t\tcontext: { table: 'posts', expected: 1, actual: postsKeys.length },\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tyield {\n\t\t\t\tcheck: 'clear',\n\t\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\t\tcontext: { error },\n\t\t\t}\n\t\t}\n\t}\n\n\t// g. snapshot rollback restores pre-snapshot state.\n\tsnapshotPhase: {\n\t\ttry {\n\t\t\tconst driver = factory()\n\t\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\t\tconst original = { id: 'u1', name: 'Ada', age: 30 }\n\t\t\tawait driver.write('users', 'u1', original)\n\t\t\tconst rollback = await driver.snapshot()\n\t\t\tawait driver.write('users', 'u2', { id: 'u2', name: 'Grace', age: 40 })\n\t\t\tawait driver.delete('users', 'u1')\n\t\t\tawait rollback()\n\t\t\tconst keys = [...(await driver.keys('users'))]\n\t\t\tif (!equalsValue(keys, ['u1'])) {\n\t\t\t\tawait driver.close()\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'snapshot-rollback',\n\t\t\t\t\tmessage: 'snapshot rollback must restore the pre-snapshot key set',\n\t\t\t\t\tcontext: { table: 'users', expected: ['u1'], actual: keys },\n\t\t\t\t}\n\t\t\t\tbreak snapshotPhase\n\t\t\t}\n\t\t\tconst restored = await driver.read('users', 'u1')\n\t\t\tawait driver.close()\n\t\t\tif (restored === undefined || !equalsValue(restored, original)) {\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'snapshot-rollback-value',\n\t\t\t\t\tmessage: 'snapshot rollback must restore pre-snapshot row values',\n\t\t\t\t\tcontext: { table: 'users', expected: original, actual: restored },\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tyield {\n\t\t\t\tcheck: 'snapshot',\n\t\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\t\tcontext: { error },\n\t\t\t}\n\t\t}\n\t}\n\n\t// g2. snapshot rollback survives a nested field mutated between capture and restore.\n\ttry {\n\t\tconst driver = factory()\n\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\tconst original = { id: 'u3', name: 'Nested', age: 20, meta: { tags: ['a'] } }\n\t\tawait driver.write('users', 'u3', original)\n\t\tconst rollback = await driver.snapshot()\n\t\tconst before = await driver.read('users', 'u3')\n\t\tif (isRecord(before) && isRecord(before.meta) && Array.isArray(before.meta.tags)) {\n\t\t\tbefore.meta.tags.push('mutated-before-restore')\n\t\t}\n\t\tawait driver.write('users', 'u3', {\n\t\t\tid: 'u3',\n\t\t\tname: 'Nested',\n\t\t\tage: 20,\n\t\t\tmeta: { tags: ['a', 'mutated-after-write'] },\n\t\t})\n\t\tawait rollback()\n\t\tconst restored = await driver.read('users', 'u3')\n\t\tawait driver.close()\n\t\tif (restored === undefined || !equalsValue(restored, original)) {\n\t\t\tyield {\n\t\t\t\tcheck: 'snapshot-nested',\n\t\t\t\tmessage:\n\t\t\t\t\t'snapshot rollback must restore pre-snapshot nested field values, unaffected by a later in-place mutation of a read-back row',\n\t\t\t\tcontext: { table: 'users', expected: original, actual: restored },\n\t\t\t}\n\t\t}\n\t} catch (error) {\n\t\tyield {\n\t\t\tcheck: 'snapshot-nested',\n\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\tcontext: { error },\n\t\t}\n\t}\n\n\t// h. non-id primary extraction (posts keyed by slug).\n\ttry {\n\t\tconst driver = factory()\n\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\tawait driver.write('posts', 'hello-world', { slug: 'caller', title: 'Hello' })\n\t\tconst post = await driver.read('posts', 'hello-world')\n\t\tconst key = post === undefined ? undefined : extractKey(post, 'slug')\n\t\tawait driver.close()\n\t\tif (key !== 'hello-world') {\n\t\t\tyield {\n\t\t\t\tcheck: 'non-id-primary',\n\t\t\t\tmessage: 'a non-id primary key column must round-trip through the store',\n\t\t\t\tcontext: { table: 'posts', expected: 'hello-world', actual: key },\n\t\t\t}\n\t\t}\n\t} catch (error) {\n\t\tyield {\n\t\t\tcheck: 'non-id-primary',\n\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\tcontext: { error },\n\t\t}\n\t}\n\n\t// i. nested-object row round-trip (structural, via equalsValue).\n\ttry {\n\t\tconst driver = factory()\n\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\tconst nested = {\n\t\t\tid: 'u3',\n\t\t\tname: 'Nested',\n\t\t\tage: 20,\n\t\t\tmeta: { tags: ['a', 'b'], deep: { flag: true } },\n\t\t}\n\t\tawait driver.write('users', 'u3', nested)\n\t\tconst readBack = await driver.read('users', 'u3')\n\t\tawait driver.close()\n\t\tif (readBack === undefined || !equalsValue(readBack, nested)) {\n\t\t\tyield {\n\t\t\t\tcheck: 'nested-roundtrip',\n\t\t\t\tmessage: 'a nested-object row must round-trip structurally',\n\t\t\t\tcontext: { table: 'users', expected: nested, actual: readBack },\n\t\t\t}\n\t\t}\n\t} catch (error) {\n\t\tyield {\n\t\t\tcheck: 'nested-roundtrip',\n\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\tcontext: { error },\n\t\t}\n\t}\n\n\t// j. migrate (presence-gated): column.remove strips rows; unknown tables fail.\n\tmigratePhase: {\n\t\ttry {\n\t\t\tconst driver = factory()\n\t\t\tif (driver.migrate === undefined) break migratePhase\n\t\t\tconst deployedUsers: TableSchema = {\n\t\t\t\t...CONFORMANCE_USERS_SCHEMA,\n\t\t\t\tcolumns: [\n\t\t\t\t\t...CONFORMANCE_USERS_SCHEMA.columns,\n\t\t\t\t\t{ name: 'legacy', storage: 'boolean', optional: true, nullable: false },\n\t\t\t\t],\n\t\t\t}\n\t\t\tawait driver.open([deployedUsers, CONFORMANCE_POSTS_SCHEMA])\n\t\t\tawait driver.write('users', 'u1', { id: 'u1', name: 'Ada', age: 30, legacy: true })\n\t\t\tconst removePlan = planMigration([deployedUsers], [CONFORMANCE_USERS_SCHEMA])\n\t\t\tawait driver.migrate({ plan: removePlan })\n\t\t\tconst migrated = await driver.read('users', 'u1')\n\t\t\tif (migrated === undefined || 'legacy' in migrated) {\n\t\t\t\tawait driver.close()\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'migrate-column-remove',\n\t\t\t\t\tmessage: 'a column.remove migration must strip the column from stored rows',\n\t\t\t\t\tcontext: {\n\t\t\t\t\t\ttable: 'users',\n\t\t\t\t\t\texpected: undefined,\n\t\t\t\t\t\tactual: migrated === undefined ? undefined : migrated.legacy,\n\t\t\t\t\t},\n\t\t\t\t}\n\t\t\t\tbreak migratePhase\n\t\t\t}\n\t\t\tlet caught: unknown\n\t\t\ttry {\n\t\t\t\tawait driver.migrate({\n\t\t\t\t\tplan: {\n\t\t\t\t\t\tfrom: 0,\n\t\t\t\t\t\tto: 1,\n\t\t\t\t\t\tsteps: [{ operation: 'table.remove', table: 'ghost' }],\n\t\t\t\t\t},\n\t\t\t\t})\n\t\t\t} catch (error) {\n\t\t\t\tcaught = error\n\t\t\t}\n\t\t\tawait driver.close()\n\t\t\tif (!isDatabaseError(caught) || caught.code !== 'MIGRATION') {\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'migrate-unknown-table',\n\t\t\t\t\tmessage:\n\t\t\t\t\t\t'a migration step referencing an unknown table must throw a MIGRATION DatabaseError',\n\t\t\t\t\tcontext: {\n\t\t\t\t\t\ttable: 'ghost',\n\t\t\t\t\t\texpected: 'MIGRATION',\n\t\t\t\t\t\tactual: isDatabaseError(caught) ? caught.code : caught,\n\t\t\t\t\t},\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tyield {\n\t\t\t\tcheck: 'migrate',\n\t\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\t\tcontext: { error },\n\t\t\t}\n\t\t}\n\t}\n\n\t// k. stream (presence-gated): condition matching and paging.\n\tstreamPhase: {\n\t\ttry {\n\t\t\tconst driver = factory()\n\t\t\tif (driver.stream === undefined) break streamPhase\n\t\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\t\tconst rows = [\n\t\t\t\t{ id: 'a', name: 'A', age: 10 },\n\t\t\t\t{ id: 'b', name: 'B', age: 20 },\n\t\t\t\t{ id: 'c', name: 'C', age: 30 },\n\t\t\t]\n\t\t\tfor (const row of rows) await driver.write('users', row.id, row)\n\t\t\tconst input: QueryInput = {\n\t\t\t\tconditions: [{ column: 'age', operator: 'above', values: [10], connector: 'and' }],\n\t\t\t}\n\t\t\tconst matched: Row[] = []\n\t\t\tfor await (const row of driver.stream('users', input)) matched.push(row)\n\t\t\tconst matchedIds = matched.map((row) => row.id).sort()\n\t\t\tif (!equalsValue(matchedIds, ['b', 'c'])) {\n\t\t\t\tawait driver.close()\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'stream-match',\n\t\t\t\t\tmessage: 'stream must yield only condition-matching rows',\n\t\t\t\t\tcontext: { table: 'users', expected: ['b', 'c'], actual: matchedIds },\n\t\t\t\t}\n\t\t\t\tbreak streamPhase\n\t\t\t}\n\t\t\tconst paged: Row[] = []\n\t\t\tfor await (const row of driver.stream('users', { offset: 1, limit: 1 })) paged.push(row)\n\t\t\tawait driver.close()\n\t\t\tif (paged.length !== 1) {\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'stream-page',\n\t\t\t\t\tmessage: 'stream must honor offset and limit',\n\t\t\t\t\tcontext: { table: 'users', expected: 1, actual: paged.length },\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tyield {\n\t\t\t\tcheck: 'stream',\n\t\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\t\tcontext: { error },\n\t\t\t}\n\t\t}\n\t}\n\n\t// l. transaction (presence-gated): commit persists, rollback restores.\n\ttransactionPhase: {\n\t\ttry {\n\t\t\tconst driver = factory()\n\t\t\tif (driver.transaction === undefined) break transactionPhase\n\t\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\t\tawait driver.write('users', 'u1', { id: 'u1', name: 'Ada', age: 30 })\n\t\t\tawait driver.transaction(async (transaction) => {\n\t\t\t\tawait transaction.write('users', 'u2', { id: 'u2', name: 'Grace', age: 40 })\n\t\t\t})\n\t\t\tconst afterCommit = [...(await driver.keys('users'))].sort()\n\t\t\tif (!equalsValue(afterCommit, ['u1', 'u2'])) {\n\t\t\t\tawait driver.close()\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'transaction-commit',\n\t\t\t\t\tmessage: 'transaction commit must persist writes made during the scope',\n\t\t\t\t\tcontext: { table: 'users', expected: ['u1', 'u2'], actual: afterCommit },\n\t\t\t\t}\n\t\t\t\tbreak transactionPhase\n\t\t\t}\n\t\t\tconst reason = {}\n\t\t\ttry {\n\t\t\t\tawait driver.transaction(async (transaction) => {\n\t\t\t\t\tawait transaction.write('users', 'u3', { id: 'u3', name: 'Marie', age: 50 })\n\t\t\t\t\tthrow reason\n\t\t\t\t})\n\t\t\t} catch (error) {\n\t\t\t\tif (error !== reason) throw error\n\t\t\t}\n\t\t\tconst afterRollback = [...(await driver.keys('users'))].sort()\n\t\t\tawait driver.close()\n\t\t\tif (!equalsValue(afterRollback, ['u1', 'u2'])) {\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'transaction-rollback',\n\t\t\t\t\tmessage: 'transaction rollback must restore pre-transaction state',\n\t\t\t\t\tcontext: { table: 'users', expected: ['u1', 'u2'], actual: afterRollback },\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tyield {\n\t\t\t\tcheck: 'transaction',\n\t\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\t\tcontext: { error },\n\t\t\t}\n\t\t}\n\t}\n\n\t// m. metadata/stamp (presence-gated): fresh is undefined; stamp round-trips.\n\tmetadataPhase: {\n\t\ttry {\n\t\t\tconst driver = factory()\n\t\t\tif (driver.metadata === undefined || driver.stamp === undefined) break metadataPhase\n\t\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\t\tconst fresh = await driver.metadata()\n\t\t\tif (fresh !== undefined) {\n\t\t\t\tawait driver.close()\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'metadata-fresh',\n\t\t\t\t\tmessage: 'a fresh store must report undefined metadata',\n\t\t\t\t\tcontext: { expected: undefined, actual: fresh },\n\t\t\t\t}\n\t\t\t\tbreak metadataPhase\n\t\t\t}\n\t\t\tconst stamped = { version: 1, schema: CONFORMANCE_SCHEMA }\n\t\t\tawait driver.stamp(stamped)\n\t\t\tconst read = await driver.metadata()\n\t\t\tawait driver.close()\n\t\t\tif (read === undefined || !equalsValue(read, stamped)) {\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'metadata-stamp',\n\t\t\t\t\tmessage: 'metadata() must return exactly the last-stamped value',\n\t\t\t\t\tcontext: { expected: stamped, actual: read },\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tyield {\n\t\t\t\tcheck: 'metadata-stamp',\n\t\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\t\tcontext: { error },\n\t\t\t}\n\t\t}\n\t}\n\n\t// n. scoped snapshot rolls back only the named table.\n\tscopedPhase: {\n\t\ttry {\n\t\t\tconst driver = factory()\n\t\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\t\tconst original = { id: 'u1', name: 'Ada', age: 30 }\n\t\t\tawait driver.write('users', 'u1', original)\n\t\t\tawait driver.write('posts', 'p1', { slug: 'p1', title: 'Post' })\n\t\t\tconst rollback = await driver.snapshot(['users'])\n\t\t\tawait driver.write('users', 'u2', { id: 'u2', name: 'Grace', age: 40 })\n\t\t\tawait driver.write('posts', 'p2', { slug: 'p2', title: 'Another post' })\n\t\t\tawait rollback()\n\t\t\tconst usersKeys = [...(await driver.keys('users'))]\n\t\t\tif (!equalsValue(usersKeys, ['u1'])) {\n\t\t\t\tawait driver.close()\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'snapshot-scoped-users',\n\t\t\t\t\tmessage: 'a scoped snapshot must roll back only the named table',\n\t\t\t\t\tcontext: { table: 'users', expected: ['u1'], actual: usersKeys },\n\t\t\t\t}\n\t\t\t\tbreak scopedPhase\n\t\t\t}\n\t\t\tconst postsKeys = [...(await driver.keys('posts'))].sort()\n\t\t\tawait driver.close()\n\t\t\tif (!equalsValue(postsKeys, ['p1', 'p2'])) {\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'snapshot-scoped-posts',\n\t\t\t\t\tmessage: \"a scoped snapshot must leave an unnamed table's mutations intact\",\n\t\t\t\t\tcontext: { table: 'posts', expected: ['p1', 'p2'], actual: postsKeys },\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tyield {\n\t\t\t\tcheck: 'snapshot-scoped',\n\t\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\t\tcontext: { error },\n\t\t\t}\n\t\t}\n\t}\n}\n\n/**\n * Run the driver-conformance battery, throwing on the first violated\n * invariant — the fail-fast entry point most callers (test setup, CI smoke\n * checks) want.\n *\n * @remarks\n * A thin driver over {@link driverFindings}: because that generator is\n * lazy, consuming only its first yielded value means every LATER phase\n * never runs — true fail-fast, not merely \"report only the first\". The\n * thrown error is byte-compatible with the historical shape: a\n * `CONFORMANCE` {@link DatabaseError} whose `message` is the finding's\n * `message` and whose `context` is `{ check, ...finding.context }`.\n *\n * @param factory - Mints a fresh, unopened driver instance (called once per phase)\n * @returns Nothing — resolves once every phase has passed\n * @throws A `CONFORMANCE` {@link DatabaseError} on the first violated invariant\n *\n * @example\n * ```ts\n * import { conformDriver, createMemoryDriver } from '@orkestrel/database'\n *\n * await conformDriver(() => createMemoryDriver()) // resolves when every invariant holds\n * ```\n */\nexport async function conformDriver(factory: () => DriverInterface): Promise<void> {\n\tfor await (const finding of driverFindings(factory)) {\n\t\tthrow new DatabaseError('CONFORMANCE', finding.message, {\n\t\t\tcheck: finding.check,\n\t\t\t...finding.context,\n\t\t})\n\t}\n}\n\n/**\n * Run the FULL driver-conformance battery and collect every violation — the\n * audit entry point for a driver author who wants a complete report rather\n * than a single fail-fast throw.\n *\n * @remarks\n * Drains {@link driverFindings} to completion: every phase runs regardless\n * of earlier violations, so a driver breaking two independent invariants\n * reports both. An empty array means the driver is fully conformant.\n *\n * @param factory - Mints a fresh, unopened driver instance (called once per phase)\n * @returns Every violated invariant found, in phase order (empty when fully conformant)\n *\n * @example\n * ```ts\n * import { auditDriver, createMemoryDriver } from '@orkestrel/database'\n *\n * const findings = await auditDriver(() => createMemoryDriver())\n * for (const finding of findings) console.log(`${finding.check}: ${finding.message}`)\n * ```\n */\nexport async function auditDriver(\n\tfactory: () => DriverInterface,\n): Promise<readonly ConformanceFinding[]> {\n\tconst findings: ConformanceFinding[] = []\n\tfor await (const finding of driverFindings(factory)) findings.push(finding)\n\treturn findings\n}\n","import type { TransactionScope } from './TransactionScope.js'\n\n/**\n * The internal continuation boundary for one transaction-scoped async iterable.\n *\n * @remarks\n * Each active continuation enters the owning transaction ledger independently,\n * so an idle iterator never pins settlement. A continuation requested after\n * admission closes rejects while still attempting source cleanup exactly once.\n */\nexport class TransactionIterator<T> implements AsyncIterableIterator<T> {\n\treadonly #source: AsyncIterator<T>\n\treadonly #scope: TransactionScope\n\t#cleaned = false\n\n\tconstructor(source: AsyncIterable<T>, scope: TransactionScope) {\n\t\tthis.#source = source[Symbol.asyncIterator]()\n\t\tthis.#scope = scope\n\t}\n\n\t[Symbol.asyncIterator](): AsyncIterableIterator<T> {\n\t\treturn this\n\t}\n\n\tnext(): Promise<IteratorResult<T>> {\n\t\treturn this.#continue(() => this.#next())\n\t}\n\n\treturn(): Promise<IteratorResult<T>> {\n\t\treturn this.#continue(() => this.#return())\n\t}\n\n\tthrow(error?: unknown): Promise<IteratorResult<T>> {\n\t\treturn this.#continue(() => this.#throw(error))\n\t}\n\n\tasync #next(): Promise<IteratorResult<T>> {\n\t\tif (this.#cleaned) return { done: true, value: undefined }\n\t\tconst result = await this.#source.next()\n\t\tif (result.done === true) this.#cleaned = true\n\t\treturn result\n\t}\n\n\tasync #return(): Promise<IteratorResult<T>> {\n\t\tif (this.#cleaned || this.#source.return === undefined) {\n\t\t\tthis.#cleaned = true\n\t\t\treturn { done: true, value: undefined }\n\t\t}\n\t\tthis.#cleaned = true\n\t\treturn this.#source.return()\n\t}\n\n\tasync #throw(error: unknown): Promise<IteratorResult<T>> {\n\t\tif (this.#source.throw !== undefined) {\n\t\t\tconst result = await this.#source.throw(error)\n\t\t\tif (result.done === true) this.#cleaned = true\n\t\t\treturn result\n\t\t}\n\t\ttry {\n\t\t\tawait this.#return()\n\t\t} catch {}\n\t\tthrow error\n\t}\n\n\t#continue<R>(operation: () => Promise<R>): Promise<R> {\n\t\tif (!this.#scope.accepting) this.#cleanup()\n\t\treturn this.#scope.track(operation)\n\t}\n\n\t#cleanup(): void {\n\t\tif (this.#cleaned) return\n\t\tthis.#cleaned = true\n\t\ttry {\n\t\t\tconst cleanup = this.#source.return?.()\n\t\t\tcleanup?.catch(() => {})\n\t\t} catch {}\n\t}\n}\n","import { DatabaseError } from './errors.js'\nimport { TransactionIterator } from './TransactionIterator.js'\n\n/**\n * The internal lifetime boundary for one database transaction callback.\n *\n * @remarks\n * Promise operations enter synchronously through {@link track}. Closing stops new\n * work while {@link drain} contains every operation already accepted, including\n * work the callback started without awaiting. {@link stream} applies the same\n * boundary to each iterator continuation without retaining an idle iterator.\n */\nexport class TransactionScope {\n\treadonly #operations = new Set<Promise<unknown>>()\n\t#accepting = true\n\t#failed = false\n\t#error: unknown\n\n\tget accepting(): boolean {\n\t\treturn this.#accepting\n\t}\n\n\tcheck(): void {\n\t\tif (!this.#accepting) {\n\t\t\tthrow new DatabaseError('CONFLICT', 'Transaction scope has settled')\n\t\t}\n\t}\n\n\ttrack<R>(operation: () => Promise<R>): Promise<R> {\n\t\ttry {\n\t\t\tthis.check()\n\t\t} catch (error) {\n\t\t\treturn Promise.reject(error)\n\t\t}\n\t\tlet promise: Promise<R>\n\t\ttry {\n\t\t\tpromise = operation()\n\t\t} catch (error) {\n\t\t\tpromise = Promise.reject(error)\n\t\t}\n\t\tthis.#operations.add(promise)\n\t\tpromise.then(\n\t\t\t() => {\n\t\t\t\tthis.#operations.delete(promise)\n\t\t\t},\n\t\t\t(error: unknown) => {\n\t\t\t\tthis.#operations.delete(promise)\n\t\t\t\tif (!this.#failed) {\n\t\t\t\t\tthis.#failed = true\n\t\t\t\t\tthis.#error = error\n\t\t\t\t}\n\t\t\t},\n\t\t)\n\t\treturn promise\n\t}\n\n\tstream<T>(source: AsyncIterable<T>): AsyncIterable<T> {\n\t\treturn new TransactionIterator(source, this)\n\t}\n\n\tstop(): void {\n\t\tthis.#accepting = false\n\t}\n\n\tasync drain(): Promise<void> {\n\t\twhile (this.#operations.size > 0) {\n\t\t\tawait Promise.allSettled(this.#operations)\n\t\t}\n\t\tif (this.#failed) throw this.#error\n\t}\n}\n","import type { Result } from '@orkestrel/contract'\nimport type { EmitterErrorHandler, EmitterInterface } from '@orkestrel/emitter'\nimport type {\n\tDatabaseEventMap,\n\tDatabaseOptions,\n\tDatabaseStatus,\n\tDriverInterface,\n\tDriverMetadata,\n\tMigration,\n\tMigrationInput,\n\tOperationOptions,\n\tStorageInterface,\n\tTableSchema,\n} from './types.js'\nimport { Emitter } from '@orkestrel/emitter'\nimport { DatabaseError } from './errors.js'\nimport { checkAbort, equalsValue, normalizeDriverSchema, planMigration } from './helpers.js'\nimport { TransactionScope } from './TransactionScope.js'\n\n/**\n * The internal shared owner behind every typed view of one database.\n *\n * @remarks\n * A context owns the driver, merged physical schema, lifecycle, observation,\n * migration, and single transaction admission. It is deliberately omitted from\n * the public barrel; {@link Database} is the consumer-facing typed view.\n */\nexport class DatabaseContext {\n\treadonly #driver: DriverInterface\n\treadonly #name: string\n\treadonly #version: number | undefined\n\treadonly #error: EmitterErrorHandler | undefined\n\treadonly #emitter: Emitter<DatabaseEventMap>\n\treadonly #operations = new Set<Promise<unknown>>()\n\t#schema: readonly TableSchema[] = []\n\t#transaction: object | undefined\n\t#status: DatabaseStatus = 'idle'\n\t#ready: Promise<void> | undefined\n\t#failure: { readonly error: unknown } | undefined\n\n\tconstructor(options: DatabaseOptions) {\n\t\tthis.#driver = options.driver\n\t\tthis.#name = options.name ?? 'database'\n\t\tthis.#version = options.version\n\t\tthis.#error = options.error\n\t\tthis.#emitter = new Emitter<DatabaseEventMap>({\n\t\t\t...(options.on === undefined ? {} : { on: options.on }),\n\t\t\t...(options.error === undefined ? {} : { error: options.error }),\n\t\t})\n\t}\n\n\tget driver(): DriverInterface {\n\t\treturn this.#driver\n\t}\n\n\tget emitter(): EmitterInterface<DatabaseEventMap> {\n\t\treturn this.#emitter\n\t}\n\n\tget error(): EmitterErrorHandler | undefined {\n\t\treturn this.#error\n\t}\n\n\tget name(): string {\n\t\treturn this.#name\n\t}\n\n\tget accepting(): boolean {\n\t\treturn this.#status !== 'closed' && this.#transaction === undefined\n\t}\n\n\tget status(): DatabaseStatus {\n\t\treturn this.#status\n\t}\n\n\tget version(): number | undefined {\n\t\treturn this.#version\n\t}\n\n\tregister(schema: readonly TableSchema[]): void {\n\t\tif (this.#status === 'closed') {\n\t\t\tthrow new DatabaseError('CLOSED', `Database '${this.#name}' is closed`, {\n\t\t\t\tname: this.#name,\n\t\t\t})\n\t\t}\n\t\tif (this.#ready !== undefined || this.#transaction !== undefined) {\n\t\t\tthrow new DatabaseError(\n\t\t\t\t'CONFLICT',\n\t\t\t\t`Database '${this.#name}' cannot import tables after opening has started`,\n\t\t\t\t{ name: this.#name, status: this.#status },\n\t\t\t)\n\t\t}\n\t\tconst registered = normalizeDriverSchema(schema)\n\t\tconst merged = [...this.#schema]\n\t\tfor (const table of registered) {\n\t\t\tconst existing = merged.find((candidate) => candidate.name === table.name)\n\t\t\tif (existing === undefined) {\n\t\t\t\tmerged.push(table)\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif (!equalsValue(existing, table)) {\n\t\t\t\tthrow new DatabaseError(\n\t\t\t\t\t'VALIDATION',\n\t\t\t\t\t`Table '${table.name}' conflicts with its registered schema`,\n\t\t\t\t\t{ table: table.name },\n\t\t\t\t)\n\t\t\t}\n\t\t}\n\t\tthis.#schema = normalizeDriverSchema(merged)\n\t}\n\n\tasync open(): Promise<void> {\n\t\tthis.#outside()\n\t\tawait this.connect()\n\t}\n\n\tasync close(): Promise<void> {\n\t\tthis.#outside()\n\t\tif (this.#status === 'closed') return\n\t\tthis.#status = 'closed'\n\t\tawait this.#drain()\n\t\tconst ready = this.#ready\n\t\tif (ready !== undefined) await ready.catch(() => {})\n\t\tthis.#ready = undefined\n\t\tawait this.#driver.close()\n\t\tthis.#emitter.emit('close')\n\t}\n\n\tconnect(): Promise<void> {\n\t\treturn this.#connect()\n\t}\n\n\ttrack<R>(operation: () => Promise<R>): Promise<R> {\n\t\ttry {\n\t\t\tthis.#admit()\n\t\t} catch (error) {\n\t\t\treturn Promise.reject(error)\n\t\t}\n\t\tlet promise: Promise<R>\n\t\ttry {\n\t\t\tpromise = operation()\n\t\t} catch (error) {\n\t\t\tpromise = Promise.reject(error)\n\t\t}\n\t\tthis.#operations.add(promise)\n\t\tpromise.then(\n\t\t\t() => {\n\t\t\t\tthis.#operations.delete(promise)\n\t\t\t},\n\t\t\t() => {\n\t\t\t\tthis.#operations.delete(promise)\n\t\t\t},\n\t\t)\n\t\treturn promise\n\t}\n\n\tasync transaction<R>(\n\t\tscope: (storage: StorageInterface, lifetime: TransactionScope) => Promise<Result<R, unknown>>,\n\t\toptions?: OperationOptions,\n\t): Promise<R> {\n\t\tcheckAbort(options?.signal)\n\t\tthis.#admit()\n\t\tconst token = {}\n\t\tthis.#transaction = token\n\t\ttry {\n\t\t\tawait this.#drain()\n\t\t\tawait this.#connect()\n\t\t\tif (this.#driver.transaction !== undefined) {\n\t\t\t\tconst rejection: { rejected: boolean; error: unknown; marker: object } = {\n\t\t\t\t\trejected: false,\n\t\t\t\t\terror: undefined,\n\t\t\t\t\tmarker: {},\n\t\t\t\t}\n\t\t\t\ttry {\n\t\t\t\t\tconst value = await this.#driver.transaction(async (storage) => {\n\t\t\t\t\t\tthis.#emitter.emit('transaction')\n\t\t\t\t\t\tconst outcome = await scope(storage, new TransactionScope())\n\t\t\t\t\t\tif (!outcome.success) {\n\t\t\t\t\t\t\trejection.rejected = true\n\t\t\t\t\t\t\trejection.error = outcome.error\n\t\t\t\t\t\t\tthrow rejection.marker\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn outcome.value\n\t\t\t\t\t})\n\t\t\t\t\tthis.#emitter.emit('commit')\n\t\t\t\t\treturn value\n\t\t\t\t} catch (error) {\n\t\t\t\t\tif (rejection.rejected) {\n\t\t\t\t\t\tif (Object.is(error, rejection.marker)) {\n\t\t\t\t\t\t\tthis.#emitter.emit('rollback', rejection.error)\n\t\t\t\t\t\t\tthrow rejection.error\n\t\t\t\t\t\t}\n\t\t\t\t\t\tthrow this.#rollbackError(rejection.error, error)\n\t\t\t\t\t}\n\t\t\t\t\tthrow error\n\t\t\t\t}\n\t\t\t}\n\t\t\tconst rollback = await this.#driver.snapshot()\n\t\t\tthis.#emitter.emit('transaction')\n\t\t\tconst outcome = await scope(this.#driver, new TransactionScope())\n\t\t\tif (outcome.success) {\n\t\t\t\tthis.#emitter.emit('commit')\n\t\t\t\treturn outcome.value\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tawait rollback()\n\t\t\t} catch (cause) {\n\t\t\t\tthrow this.#rollbackError(outcome.error, cause)\n\t\t\t}\n\t\t\tthis.#emitter.emit('rollback', outcome.error)\n\t\t\tthrow outcome.error\n\t\t} finally {\n\t\t\tif (this.#transaction === token) this.#transaction = undefined\n\t\t}\n\t}\n\n\tasync migrate(deployed: readonly TableSchema[], options?: OperationOptions): Promise<Migration> {\n\t\tcheckAbort(options?.signal)\n\t\tthis.#outside()\n\t\tif (this.#ready !== undefined || this.#status !== 'idle') {\n\t\t\tthrow new DatabaseError(\n\t\t\t\t'CONFLICT',\n\t\t\t\t`Database '${this.#name}' cannot apply an explicit deployed schema after opening`,\n\t\t\t\t{ name: this.#name, status: this.#status },\n\t\t\t)\n\t\t}\n\t\tif (this.#driver.migrate === undefined) {\n\t\t\tthrow new DatabaseError(\n\t\t\t\t'MIGRATION',\n\t\t\t\t`Database '${this.#name}' driver does not support migration`,\n\t\t\t\t{ name: this.#name },\n\t\t\t)\n\t\t}\n\t\tconst plan = planMigration(deployed, this.#schema)\n\t\tconst readiness = this.#transition(deployed, plan).catch((error: unknown) => {\n\t\t\tif (this.#ready === readiness) this.#ready = undefined\n\t\t\tthis.#failure = { error }\n\t\t\tthrow error\n\t\t})\n\t\tthis.#failure = undefined\n\t\tthis.#ready = readiness\n\t\tawait readiness\n\t\treturn plan\n\t}\n\n\t#admit(): void {\n\t\tif (this.#status === 'closed') {\n\t\t\tthrow new DatabaseError('CLOSED', `Database '${this.#name}' is closed`, {\n\t\t\t\tname: this.#name,\n\t\t\t})\n\t\t}\n\t\tif (this.#transaction !== undefined) {\n\t\t\tthrow new DatabaseError('CONFLICT', `Database '${this.#name}' has an active transaction`, {\n\t\t\t\tname: this.#name,\n\t\t\t})\n\t\t}\n\t\tif (this.#failure !== undefined) throw this.#failure.error\n\t}\n\n\t#outside(): void {\n\t\tif (this.#transaction !== undefined) {\n\t\t\tthrow new DatabaseError('CONFLICT', `Database '${this.#name}' has an active transaction`, {\n\t\t\t\tname: this.#name,\n\t\t\t})\n\t\t}\n\t}\n\n\t#connect(): Promise<void> {\n\t\tif (this.#ready !== undefined) return this.#ready\n\t\tif (this.#failure !== undefined) throw this.#failure.error\n\t\tif (this.#status === 'closed') {\n\t\t\tthrow new DatabaseError('CLOSED', `Database '${this.#name}' is closed`, {\n\t\t\t\tname: this.#name,\n\t\t\t})\n\t\t}\n\t\tconst readiness = this.#driver\n\t\t\t.open(this.#schema)\n\t\t\t.then(async () => {\n\t\t\t\tif (this.#status === 'idle') {\n\t\t\t\t\tthis.#status = 'open'\n\t\t\t\t\tthis.#emitter.emit('open')\n\t\t\t\t}\n\t\t\t\tawait this.#reconcile()\n\t\t\t})\n\t\t\t.catch((error: unknown) => {\n\t\t\t\tif (this.#ready === readiness) this.#ready = undefined\n\t\t\t\tthrow error\n\t\t\t})\n\t\tthis.#ready = readiness\n\t\treturn readiness\n\t}\n\n\tasync #drain(): Promise<void> {\n\t\tif (this.#operations.size === 0) return\n\t\tawait Promise.allSettled(this.#operations)\n\t}\n\n\tasync #reconcile(): Promise<void> {\n\t\tif (\n\t\t\tthis.#version === undefined ||\n\t\t\tthis.#driver.metadata === undefined ||\n\t\t\tthis.#driver.stamp === undefined\n\t\t) {\n\t\t\treturn\n\t\t}\n\t\tconst metadata = await this.#driver.metadata()\n\t\tif (metadata === undefined) {\n\t\t\tawait this.#stamp()\n\t\t\treturn\n\t\t}\n\t\tif (metadata.version > this.#version) {\n\t\t\tthrow new DatabaseError(\n\t\t\t\t'MIGRATION',\n\t\t\t\t`Database '${this.#name}' store version ${metadata.version} is newer than declared version ${this.#version}`,\n\t\t\t\t{ name: this.#name, stored: metadata.version, declared: this.#version },\n\t\t\t)\n\t\t}\n\t\tif (metadata.version === this.#version) {\n\t\t\tif (!equalsValue(normalizeDriverSchema(metadata.schema), this.#schema)) {\n\t\t\t\tthrow new DatabaseError(\n\t\t\t\t\t'MIGRATION',\n\t\t\t\t\t`Database '${this.#name}' stored schema differs at version ${this.#version}`,\n\t\t\t\t\t{ name: this.#name, version: this.#version },\n\t\t\t\t)\n\t\t\t}\n\t\t\treturn\n\t\t}\n\t\tconst plan = planMigration(metadata.schema, this.#schema, metadata.version, this.#version)\n\t\tif (plan.steps.length > 0 && this.#driver.migrate === undefined) {\n\t\t\tthrow new DatabaseError(\n\t\t\t\t'MIGRATION',\n\t\t\t\t`Database '${this.#name}' driver does not support migration`,\n\t\t\t\t{ name: this.#name, stored: metadata.version, declared: this.#version },\n\t\t\t)\n\t\t}\n\t\tawait this.#apply(plan)\n\t}\n\n\tasync #apply(plan: Migration): Promise<void> {\n\t\tif (this.#driver.transaction !== undefined) {\n\t\t\tawait this.#driver.transaction(async (storage) => {\n\t\t\t\tif (plan.steps.length > 0 && storage.migrate === undefined) {\n\t\t\t\t\tthrow new DatabaseError(\n\t\t\t\t\t\t'MIGRATION',\n\t\t\t\t\t\t`Database '${this.#name}' transaction does not support migration`,\n\t\t\t\t\t\t{ name: this.#name },\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\tif (storage.migrate === undefined) await this.#stamp(storage)\n\t\t\t\telse await storage.migrate(this.#migration(plan))\n\t\t\t})\n\t\t\tthis.#emitter.emit('migrate', plan)\n\t\t\treturn\n\t\t}\n\t\tif (this.#driver.migrate === undefined) await this.#stamp()\n\t\telse await this.#driver.migrate(this.#migration(plan))\n\t\tthis.#emitter.emit('migrate', plan)\n\t}\n\n\tasync #transition(deployed: readonly TableSchema[], plan: Migration): Promise<void> {\n\t\tawait this.#driver.open(deployed)\n\t\tawait this.#apply(plan)\n\t\tif (this.#status === 'idle') {\n\t\t\tthis.#status = 'open'\n\t\t\tthis.#emitter.emit('open')\n\t\t}\n\t}\n\n\tasync #stamp(storage?: StorageInterface): Promise<void> {\n\t\tconst target = storage ?? this.#driver\n\t\tif (this.#version === undefined || target.stamp === undefined) return\n\t\tconst metadata: DriverMetadata = {\n\t\t\tversion: this.#version,\n\t\t\tschema: this.#schema,\n\t\t}\n\t\tawait target.stamp(metadata)\n\t}\n\n\t#migration(plan: Migration): MigrationInput {\n\t\tif (this.#version === undefined) return { plan }\n\t\treturn {\n\t\t\tplan,\n\t\t\tmetadata: { version: this.#version, schema: this.#schema },\n\t\t}\n\t}\n\n\t#rollbackError(transaction: unknown, cause: unknown): DatabaseError {\n\t\treturn new DatabaseError('DRIVER', `Database '${this.#name}' rollback failed`, {\n\t\t\tcause,\n\t\t\ttransaction,\n\t\t})\n\t}\n}\n","import type { CursorInterface, Key } from './types.js'\n\n/**\n * A forward row cursor for bulk in-place mutation.\n *\n * @remarks\n * Iterates a snapshot of the table's keys captured when the cursor was opened,\n * reading each row lazily through the owning table — so a mutation made during\n * iteration cannot corrupt the walk, and a key removed mid-iteration is simply\n * skipped. `update` and `remove` act on the row at the current position.\n */\nexport class Cursor<T = Record<string, unknown>> implements CursorInterface<T> {\n\treadonly #keys: readonly Key[]\n\treadonly #read: (key: Key) => Promise<T | undefined>\n\treadonly #update: (key: Key, changes: Partial<T>) => Promise<boolean>\n\treadonly #remove: (key: Key) => Promise<boolean>\n\treadonly #track: <R>(operation: () => Promise<R>) => Promise<R>\n\t#tail = Promise.resolve()\n\t#index = -1\n\t#value: T | undefined\n\t#closed = false\n\n\tconstructor(\n\t\tkeys: readonly Key[],\n\t\tread: (key: Key) => Promise<T | undefined>,\n\t\tupdate: (key: Key, changes: Partial<T>) => Promise<boolean>,\n\t\tremove: (key: Key) => Promise<boolean>,\n\t\ttrack: <R>(operation: () => Promise<R>) => Promise<R>,\n\t) {\n\t\tthis.#keys = keys\n\t\tthis.#read = read\n\t\tthis.#update = update\n\t\tthis.#remove = remove\n\t\tthis.#track = track\n\t}\n\n\tget value(): T | undefined {\n\t\treturn this.#value\n\t}\n\n\tget index(): number {\n\t\treturn this.#index\n\t}\n\n\tget done(): boolean {\n\t\treturn this.#closed || this.#index >= this.#keys.length\n\t}\n\n\tnext(): Promise<void> {\n\t\treturn this.#track(() => this.#queue(() => this.#advance()))\n\t}\n\n\tupdate(changes: Partial<T>): Promise<void> {\n\t\treturn this.#track(() => this.#queue(() => this.#revise(changes)))\n\t}\n\n\tremove(): Promise<void> {\n\t\treturn this.#track(() => this.#queue(() => this.#delete()))\n\t}\n\n\tclose(): void {\n\t\tthis.#closed = true\n\t\tthis.#value = undefined\n\t}\n\n\tasync #advance(): Promise<void> {\n\t\tif (this.#closed) return\n\t\tthis.#index += 1\n\t\twhile (this.#index < this.#keys.length) {\n\t\t\tif (this.#closed) return\n\t\t\tconst key = this.#keys[this.#index]\n\t\t\tif (key === undefined) {\n\t\t\t\tthis.#index += 1\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tconst row = await this.#read(key)\n\t\t\tif (this.#closed) return\n\t\t\tif (row !== undefined) {\n\t\t\t\tthis.#value = row\n\t\t\t\treturn\n\t\t\t}\n\t\t\tthis.#index += 1\n\t\t}\n\t\tthis.#value = undefined\n\t}\n\n\tasync #revise(changes: Partial<T>): Promise<void> {\n\t\tif (this.#closed || this.#value === undefined) return\n\t\tconst key = this.#keys[this.#index]\n\t\tif (key === undefined) return\n\t\tawait this.#update(key, changes)\n\t\tif (this.#closed) return\n\t\tconst row = await this.#read(key)\n\t\tif (this.#closed) return\n\t\tthis.#value = row\n\t}\n\n\tasync #delete(): Promise<void> {\n\t\tif (this.#closed || this.#value === undefined) return\n\t\tconst key = this.#keys[this.#index]\n\t\tif (key === undefined) return\n\t\tawait this.#remove(key)\n\t\tif (this.#closed) return\n\t\tthis.#value = undefined\n\t}\n\n\t#queue(operation: () => Promise<void>): Promise<void> {\n\t\tconst result = this.#tail.then(operation)\n\t\tthis.#tail = result.then(\n\t\t\t() => undefined,\n\t\t\t() => undefined,\n\t\t)\n\t\treturn result\n\t}\n}\n","import type { DatabaseContext } from './DatabaseContext.js'\n\n/**\n * The internal continuation admission boundary for a root database stream.\n *\n * @remarks\n * Each continuation enters the shared root operation ledger independently, so\n * an idle iterator never delays a transaction or close. A continuation rejected\n * after transaction or close admission closes attempts source cleanup exactly\n * once and leaves the iterator terminal.\n */\nexport class DatabaseIterator<T> implements AsyncIterableIterator<T> {\n\treadonly #source: AsyncIterator<T>\n\treadonly #context: DatabaseContext\n\t#cleaned = false\n\n\tconstructor(source: AsyncIterable<T>, context: DatabaseContext) {\n\t\tthis.#source = source[Symbol.asyncIterator]()\n\t\tthis.#context = context\n\t}\n\n\t[Symbol.asyncIterator](): AsyncIterableIterator<T> {\n\t\treturn this\n\t}\n\n\tnext(): Promise<IteratorResult<T>> {\n\t\treturn this.#continue(() => this.#next())\n\t}\n\n\treturn(): Promise<IteratorResult<T>> {\n\t\treturn this.#continue(() => this.#return())\n\t}\n\n\tthrow(error?: unknown): Promise<IteratorResult<T>> {\n\t\treturn this.#continue(() => this.#throw(error))\n\t}\n\n\tasync #next(): Promise<IteratorResult<T>> {\n\t\tif (this.#cleaned) return { done: true, value: undefined }\n\t\tawait this.#context.connect()\n\t\tconst result = await this.#source.next()\n\t\tif (result.done === true) this.#cleaned = true\n\t\treturn result\n\t}\n\n\tasync #return(): Promise<IteratorResult<T>> {\n\t\tif (this.#cleaned || this.#source.return === undefined) {\n\t\t\tthis.#cleaned = true\n\t\t\treturn { done: true, value: undefined }\n\t\t}\n\t\tthis.#cleaned = true\n\t\treturn this.#source.return()\n\t}\n\n\tasync #throw(error: unknown): Promise<IteratorResult<T>> {\n\t\tif (this.#source.throw !== undefined) {\n\t\t\tconst result = await this.#source.throw(error)\n\t\t\tif (result.done === true) this.#cleaned = true\n\t\t\treturn result\n\t\t}\n\t\ttry {\n\t\t\tawait this.#return()\n\t\t} catch {}\n\t\tthrow error\n\t}\n\n\t#continue<R>(operation: () => Promise<R>): Promise<R> {\n\t\tif (!this.#context.accepting) this.#cleanup()\n\t\treturn this.#context.track(operation)\n\t}\n\n\t#cleanup(): void {\n\t\tif (this.#cleaned) return\n\t\tthis.#cleaned = true\n\t\ttry {\n\t\t\tconst cleanup = this.#source.return?.()\n\t\t\tcleanup?.catch(() => {})\n\t\t} catch {}\n\t}\n}\n","import type { FieldPath } from '@orkestrel/contract'\nimport type {\n\tAggregateOperation,\n\tCondition,\n\tOperationOptions,\n\tOrder,\n\tQueryInterface,\n\tTableInterface,\n} from './types.js'\nimport { computeAggregate } from './helpers.js'\nimport { validatePage } from './validators.js'\n\n/**\n * A fluent query builder bound to one table.\n *\n * @remarks\n * Accumulates typed conditions, ordering, JS filters, and a page. Each builder\n * method mutates and returns the same instance. Portable inputs flow to the\n * table, while predicates remain an in-memory refinement.\n */\nexport class Query<T = Record<string, unknown>> implements QueryInterface<T> {\n\treadonly #table: TableInterface<T>\n\treadonly #conditions: Condition[] = []\n\treadonly #orders: Order[] = []\n\treadonly #filters: Array<(row: T) => boolean> = []\n\t#limit: number | undefined\n\t#offset: number | undefined\n\n\tconstructor(table: TableInterface<T>) {\n\t\tthis.#table = table\n\t}\n\n\tcondition(input: Condition): QueryInterface<T> {\n\t\tthis.#conditions.push(input)\n\t\treturn this\n\t}\n\n\torder(input: Order): QueryInterface<T> {\n\t\tthis.#orders.push(input)\n\t\treturn this\n\t}\n\n\tfilter(predicate: (row: T) => boolean): QueryInterface<T> {\n\t\tthis.#filters.push(predicate)\n\t\treturn this\n\t}\n\n\tlimit(count: number): QueryInterface<T> {\n\t\tvalidatePage({ limit: count })\n\t\tthis.#limit = count\n\t\treturn this\n\t}\n\n\toffset(count: number): QueryInterface<T> {\n\t\tvalidatePage({ offset: count })\n\t\tthis.#offset = count\n\t\treturn this\n\t}\n\n\tasync collect(): Promise<readonly T[]> {\n\t\tif (this.#filters.length === 0) {\n\t\t\treturn this.#table.records({\n\t\t\t\tconditions: this.#conditions,\n\t\t\t\torder: this.#orders,\n\t\t\t\t...(this.#limit !== undefined ? { limit: this.#limit } : {}),\n\t\t\t\t...(this.#offset !== undefined ? { offset: this.#offset } : {}),\n\t\t\t})\n\t\t}\n\t\tconst fetched = await this.#table.records({\n\t\t\tconditions: this.#conditions,\n\t\t\torder: this.#orders,\n\t\t})\n\t\treturn this.#page(this.#filtered(fetched))\n\t}\n\n\tasync find(): Promise<T | undefined> {\n\t\tconst rows = await this.collect()\n\t\treturn rows[0]\n\t}\n\n\tasync count(): Promise<number> {\n\t\tif (this.#filters.length === 0) {\n\t\t\treturn this.#table.count({ conditions: this.#conditions })\n\t\t}\n\t\tconst fetched = await this.#table.records({ conditions: this.#conditions })\n\t\treturn this.#filtered(fetched).length\n\t}\n\n\t/**\n\t * Lazily evaluate conditions, filters, offset, and limit.\n\t *\n\t * @param options - Optional abort options\n\t * @returns Matching rows in storage order\n\t */\n\tasync *stream(options?: OperationOptions): AsyncIterable<T> {\n\t\tif (this.#filters.length === 0) {\n\t\t\tyield* this.#table.scan(\n\t\t\t\t{\n\t\t\t\t\tconditions: this.#conditions,\n\t\t\t\t\t...(this.#limit !== undefined ? { limit: this.#limit } : {}),\n\t\t\t\t\t...(this.#offset !== undefined ? { offset: this.#offset } : {}),\n\t\t\t\t},\n\t\t\t\toptions,\n\t\t\t)\n\t\t\treturn\n\t\t}\n\t\tconst offset = this.#offset ?? 0\n\t\tlet matched = 0\n\t\tlet yielded = 0\n\t\tfor await (const row of this.#table.scan({ conditions: this.#conditions }, options)) {\n\t\t\tif (this.#limit !== undefined && yielded >= this.#limit) break\n\t\t\tlet matches = true\n\t\t\tfor (const predicate of this.#filters) {\n\t\t\t\tif (!predicate(row)) {\n\t\t\t\t\tmatches = false\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (!matches) continue\n\t\t\tif (matched < offset) {\n\t\t\t\tmatched += 1\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tmatched += 1\n\t\t\tyielded += 1\n\t\t\tyield row\n\t\t}\n\t}\n\n\taggregate(operation: AggregateOperation, column: FieldPath): Promise<number | undefined> {\n\t\tif (this.#filters.length === 0) {\n\t\t\treturn this.#table.aggregate(operation, column, {\n\t\t\t\tconditions: this.#conditions,\n\t\t\t})\n\t\t}\n\t\treturn this.#table\n\t\t\t.records({ conditions: this.#conditions })\n\t\t\t.then((fetched) => computeAggregate(this.#filtered(fetched), operation, column))\n\t}\n\n\t#filtered(rows: readonly T[]): readonly T[] {\n\t\tlet result = rows\n\t\tfor (const predicate of this.#filters) result = result.filter(predicate)\n\t\treturn result\n\t}\n\n\t#page(rows: readonly T[]): readonly T[] {\n\t\tconst offset = this.#offset ?? 0\n\t\tif (offset === 0 && this.#limit === undefined) return rows\n\t\treturn rows.slice(offset, this.#limit === undefined ? undefined : offset + this.#limit)\n\t}\n}\n","import type { ContractInterface, FieldPath, Guard } from '@orkestrel/contract'\nimport type { EmitterErrorHandler, EmitterInterface } from '@orkestrel/emitter'\nimport type { DatabaseContext } from './DatabaseContext.js'\nimport type {\n\tAggregateOperation,\n\tQueryInput,\n\tCursorInterface,\n\tKey,\n\tKeyFunction,\n\tQueryInterface,\n\tOperationOptions,\n\tRow,\n\tTableEventMap,\n\tTableInterface,\n\tStorageInterface,\n} from './types.js'\nimport { isArray, isRecord } from '@orkestrel/contract'\nimport { Emitter } from '@orkestrel/emitter'\nimport { DatabaseError } from './errors.js'\nimport {\n\tapplyQuery,\n\tcheckAbort,\n\tcomputeAggregate,\n\tequalsValue,\n\textractKey,\n\tfilterRows,\n\tmatchesQuery,\n} from './helpers.js'\nimport { Cursor } from './Cursor.js'\nimport { DatabaseIterator } from './DatabaseIterator.js'\nimport { Query } from './Query.js'\nimport type { TransactionScope } from './TransactionScope.js'\nimport { validatePage } from './validators.js'\n\n/**\n * A table — typed keyed CRUD plus fluent query and cursor access over a driver.\n *\n * @remarks\n * The table's contract is the load-bearing piece: writes go through `parse`\n * (coercing inputs and rejecting rows that don't fit with a `VALIDATION` throw),\n * reads come back through the contract guard (narrowing a stored {@link Row} to\n * the table's type — no assertion, AGENTS §1), and `contract` is exposed for\n * introspection and seeding. The driver only stores and scans; all querying is\n * the shared core engine in `helpers.ts`.\n *\n * @remarks\n * - **Observable (§13).** The owned {@link emitter} ({@link TableEventMap}) carries the\n * per-row mutation moments — `write` (set / add / update), `remove`, `clear` — for\n * fire-and-forget observers (cache invalidation, sync, an audit log), ALONGSIDE the\n * database-level lifecycle. Events carry the affected KEY only (no value payload, to\n * keep fan-out lean); reads / queries / counts are not emitted. Every event is emitted\n * directly, strictly AFTER the driver write / delete / clear completes; the emitter\n * isolates a listener throw and routes it to its `error` handler (the `error` option),\n * so a buggy observer can never corrupt a write or perturb a transaction.\n */\nexport class Table<T = Row> implements TableInterface<T> {\n\treadonly #ready: () => Promise<void>\n\treadonly #driver: StorageInterface\n\treadonly #name: string\n\treadonly #key: string\n\treadonly #contract: ContractInterface<T>\n\treadonly #guard: Guard<T>\n\treadonly #generate: KeyFunction | undefined\n\treadonly #context: DatabaseContext | undefined\n\treadonly #scope: TransactionScope | undefined\n\t// The PUSH observation surface (§13) — owned, never inherited. The emitter isolates a\n\t// listener throw (routing it to the `error` handler), so it can never escape into a write\n\t// or a transaction.\n\treadonly #emitter: Emitter<TableEventMap>\n\n\tconstructor(\n\t\tready: () => Promise<void>,\n\t\tdriver: StorageInterface,\n\t\tname: string,\n\t\tkey: string,\n\t\tcontract: ContractInterface<T>,\n\t\tgenerate?: KeyFunction,\n\t\terror?: EmitterErrorHandler,\n\t\tcontext?: DatabaseContext,\n\t\tscope?: TransactionScope,\n\t) {\n\t\tthis.#ready = ready\n\t\tthis.#driver = driver\n\t\tthis.#name = name\n\t\tthis.#key = key\n\t\tthis.#contract = contract\n\t\tthis.#guard = contract.is\n\t\tthis.#generate = generate\n\t\tthis.#context = context\n\t\tthis.#scope = scope\n\t\tthis.#emitter = new Emitter<TableEventMap>({\n\t\t\t...(error !== undefined ? { error } : {}),\n\t\t})\n\t}\n\n\tget emitter(): EmitterInterface<TableEventMap> {\n\t\treturn this.#emitter\n\t}\n\n\tget name(): string {\n\t\treturn this.#name\n\t}\n\n\tget primary(): string {\n\t\treturn this.#key\n\t}\n\n\tget contract(): ContractInterface<T> {\n\t\treturn this.#contract\n\t}\n\n\tget(key: Key): Promise<T | undefined>\n\tget(keys: readonly Key[]): Promise<ReadonlyArray<T | undefined>>\n\tget(keys: Key | readonly Key[]): Promise<(T | undefined) | ReadonlyArray<T | undefined>> {\n\t\treturn this.#track(async () => {\n\t\t\tawait this.#ready()\n\t\t\tif (isArray(keys)) return this.#each(keys, (key) => this.#read(key))\n\t\t\treturn this.#read(keys)\n\t\t})\n\t}\n\n\tresolve(key: Key): Promise<T>\n\tresolve(keys: readonly Key[]): Promise<readonly T[]>\n\tresolve(keys: Key | readonly Key[]): Promise<T | readonly T[]> {\n\t\treturn this.#track(async () => {\n\t\t\tawait this.#ready()\n\t\t\tif (isArray(keys)) return this.#each(keys, (key) => this.#resolveOne(key))\n\t\t\treturn this.#resolveOne(keys)\n\t\t})\n\t}\n\n\thas(key: Key): Promise<boolean>\n\thas(keys: readonly Key[]): Promise<readonly boolean[]>\n\thas(keys: Key | readonly Key[]): Promise<boolean | readonly boolean[]> {\n\t\treturn this.#track(async () => {\n\t\t\tawait this.#ready()\n\t\t\tif (isArray(keys)) {\n\t\t\t\treturn this.#each(keys, async (key) => (await this.#read(key)) !== undefined)\n\t\t\t}\n\t\t\treturn (await this.#read(keys)) !== undefined\n\t\t})\n\t}\n\n\tkeys(): Promise<readonly Key[]> {\n\t\treturn this.#track(async () => {\n\t\t\tawait this.#ready()\n\t\t\treturn this.#driver.keys(this.#name)\n\t\t})\n\t}\n\n\tasync records(input?: QueryInput, options?: OperationOptions): Promise<readonly T[]> {\n\t\tvalidatePage(input)\n\t\treturn this.#track(async () => {\n\t\t\tcheckAbort(options?.signal)\n\t\t\tawait this.#ready()\n\t\t\tconst candidate: QueryInput = {\n\t\t\t\t...(input?.conditions === undefined ? {} : { conditions: input.conditions }),\n\t\t\t\t...(input?.order === undefined ? {} : { order: input.order }),\n\t\t\t}\n\t\t\tconst native = await this.#driver.records?.(this.#name, candidate)\n\t\t\tconst source = native ?? applyQuery(await this.#collect(), candidate)\n\t\t\tconst rows: T[] = []\n\t\t\tfor (const row of source) {\n\t\t\t\tif (this.#guard(row)) rows.push(row)\n\t\t\t}\n\t\t\tconst offset = input?.offset ?? 0\n\t\t\tconst limit = input?.limit\n\t\t\treturn rows.slice(offset, limit === undefined ? undefined : offset + limit)\n\t\t})\n\t}\n\n\t/**\n\t * Count contract-valid rows matching `input`'s conditions.\n\t *\n\t * @remarks\n\t * Paging is ignored. Candidate rows use the driver's native `records` hook\n\t * when present, then the table contract guard determines the count so legacy\n\t * invalid rows cannot make `count()` disagree with `records()`.\n\t *\n\t * @param input - Optional conditions to filter by (paging is ignored)\n\t * @param options - `{ signal }` to abort\n\t * @returns The count of matching contract-valid rows\n\t */\n\tasync count(input?: QueryInput, options?: OperationOptions): Promise<number> {\n\t\tvalidatePage(input)\n\t\treturn this.#track(async () => {\n\t\t\tcheckAbort(options?.signal)\n\t\t\tawait this.#ready()\n\t\t\tconst conditions = input?.conditions\n\t\t\tconst candidate: QueryInput = conditions === undefined ? {} : { conditions }\n\t\t\tconst native = await this.#driver.records?.(this.#name, candidate)\n\t\t\tconst rows = native ?? filterRows(await this.#collect(), conditions ?? [])\n\t\t\tlet count = 0\n\t\t\tfor (const row of rows) {\n\t\t\t\tif (this.#guard(row)) count += 1\n\t\t\t}\n\t\t\treturn count\n\t\t})\n\t}\n\n\t/**\n\t * Compute an aggregate over `column` across rows matching `input`'s\n\t * conditions.\n\t *\n\t * @remarks\n\t * Like {@link count}, `aggregate` operates on STORED rows WITHOUT the\n\t * contract guard {@link records} / {@link scan} apply — a non-conforming\n\t * stored row still contributes to the computed aggregate when it matches\n\t * the conditions, even though it would never appear in `records()`'s\n\t * output.\n\t *\n\t * @param operation - The aggregate to compute\n\t * @param column - The column to aggregate\n\t * @param input - Optional conditions to filter by (paging is ignored)\n\t * @param options - `{ signal }` to abort\n\t * @returns The aggregate value, or `undefined` when undefined for the inputs\n\t */\n\tasync aggregate(\n\t\toperation: AggregateOperation,\n\t\tcolumn: FieldPath,\n\t\tinput?: QueryInput,\n\t\toptions?: OperationOptions,\n\t): Promise<number | undefined> {\n\t\tvalidatePage(input)\n\t\treturn this.#track(async () => {\n\t\t\tcheckAbort(options?.signal)\n\t\t\tawait this.#ready()\n\t\t\t// Aggregate over filtered (not paged) rows — native hook, native records, or scan.\n\t\t\tconst conditions = input?.conditions\n\t\t\tconst filter: QueryInput = conditions ? { conditions } : {}\n\t\t\t// `?.()` is `undefined` only when the driver lacks the method; a present\n\t\t\t// hook returns a Promise (whose resolved value may itself be `undefined`).\n\t\t\tconst native = this.#driver.aggregate?.(this.#name, operation, column, filter)\n\t\t\tif (native !== undefined) return native\n\t\t\tconst rows = await this.#driver.records?.(this.#name, filter)\n\t\t\tconst matched = rows ?? filterRows(await this.#collect(), input?.conditions ?? [])\n\t\t\treturn computeAggregate(matched, operation, column)\n\t\t})\n\t}\n\n\t/**\n\t * Stream the table's rows matching `input`, applying offset/limit paging.\n\t *\n\t * @remarks\n\t * `input.limit` counts rows that pass both the input conditions and the\n\t * table's contract guard. Native streams receive only the conditions; this\n\t * table rechecks them, narrows each candidate, and applies offset/limit last,\n\t * matching {@link records} when storage contains legacy invalid rows.\n\t *\n\t * @param input - Optional conditions plus offset/limit paging\n\t * @param options - `{ signal }` to abort mid-stream\n\t * @returns An async iterable of matching, guard-conforming rows\n\t */\n\tscan(input?: QueryInput, options?: OperationOptions): AsyncIterable<T> {\n\t\tvalidatePage(input)\n\t\tconst source = this.#scan(input, options)\n\t\tif (this.#context !== undefined) return new DatabaseIterator(source, this.#context)\n\t\treturn this.#scope === undefined ? source : this.#scope.stream(source)\n\t}\n\n\tset(row: T, options?: OperationOptions): Promise<Key>\n\tset(rows: readonly T[], options?: OperationOptions): Promise<readonly Key[]>\n\tset(rows: T | readonly T[], options?: OperationOptions): Promise<Key | readonly Key[]> {\n\t\treturn this.#track(async () => {\n\t\t\tawait this.#wait(options?.signal)\n\t\t\tif (isArray(rows)) {\n\t\t\t\treturn this.#each(rows, (row) => this.#put(row, false, options), options?.signal)\n\t\t\t}\n\t\t\treturn this.#put(rows, false, options)\n\t\t})\n\t}\n\n\tadd(row: T, options?: OperationOptions): Promise<Key>\n\tadd(rows: readonly T[], options?: OperationOptions): Promise<readonly Key[]>\n\tadd(rows: T | readonly T[], options?: OperationOptions): Promise<Key | readonly Key[]> {\n\t\treturn this.#track(async () => {\n\t\t\tawait this.#wait(options?.signal)\n\t\t\tif (isArray(rows)) {\n\t\t\t\treturn this.#each(rows, (row) => this.#put(row, true, options), options?.signal)\n\t\t\t}\n\t\t\treturn this.#put(rows, true, options)\n\t\t})\n\t}\n\n\tupdate(key: Key, changes: Partial<T>, options?: OperationOptions): Promise<boolean>\n\tupdate(\n\t\tkeys: readonly Key[],\n\t\tchanges: Partial<T>,\n\t\toptions?: OperationOptions,\n\t): Promise<readonly boolean[]>\n\tupdate(\n\t\tkeys: Key | readonly Key[],\n\t\tchanges: Partial<T>,\n\t\toptions?: OperationOptions,\n\t): Promise<boolean | readonly boolean[]> {\n\t\treturn this.#track(async () => {\n\t\t\tawait this.#wait(options?.signal)\n\t\t\tif (isArray(keys)) {\n\t\t\t\treturn this.#each(keys, (key) => this.#updateOne(key, changes, options), options?.signal)\n\t\t\t}\n\t\t\treturn this.#updateOne(keys, changes, options)\n\t\t})\n\t}\n\n\tremove(key: Key, options?: OperationOptions): Promise<boolean>\n\tremove(keys: readonly Key[], options?: OperationOptions): Promise<readonly boolean[]>\n\tremove(\n\t\tkeys: Key | readonly Key[],\n\t\toptions?: OperationOptions,\n\t): Promise<boolean | readonly boolean[]> {\n\t\treturn this.#track(async () => {\n\t\t\tawait this.#wait(options?.signal)\n\t\t\tif (isArray(keys)) {\n\t\t\t\treturn this.#each(keys, (key) => this.#delete(key, options), options?.signal)\n\t\t\t}\n\t\t\treturn this.#delete(keys, options)\n\t\t})\n\t}\n\n\tclear(): Promise<void> {\n\t\treturn this.#track(async () => {\n\t\t\tawait this.#ready()\n\t\t\tawait this.#driver.clear(this.#name)\n\t\t\t// Observe the cleared table — AFTER the driver emptied it, so a swallowed listener\n\t\t\t// throw can never alter the clear (no value payload — `clear` is a pure signal).\n\t\t\tthis.#emitter.emit('clear')\n\t\t})\n\t}\n\n\tquery(): QueryInterface<T> {\n\t\treturn new Query<T>(this)\n\t}\n\n\tcursor(): Promise<CursorInterface<T>> {\n\t\treturn this.#track(async () => {\n\t\t\tawait this.#ready()\n\t\t\tlet initializing = true\n\t\t\tconst cursor = new Cursor<T>(\n\t\t\t\tawait this.#driver.keys(this.#name),\n\t\t\t\t(key) => this.#readCursor(key),\n\t\t\t\t(key, changes) => this.#updateCursor(key, changes),\n\t\t\t\t(key) => this.#deleteCursor(key),\n\t\t\t\t(operation) => (initializing ? operation() : this.#track(operation)),\n\t\t\t)\n\t\t\tawait cursor.next()\n\t\t\tinitializing = false\n\t\t\treturn cursor\n\t\t})\n\t}\n\n\tasync *#scan(input?: QueryInput, options?: OperationOptions): AsyncIterable<T> {\n\t\tcheckAbort(options?.signal)\n\t\tawait this.#ready()\n\t\tconst conditions = input?.conditions\n\t\tconst offset = input?.offset ?? 0\n\t\tconst limit = input?.limit\n\t\tlet matched = 0\n\t\tlet yielded = 0\n\t\tconst source =\n\t\t\tthis.#driver.stream === undefined\n\t\t\t\t? this.#driver.scan(this.#name)\n\t\t\t\t: this.#driver.stream(this.#name, conditions === undefined ? {} : { conditions })\n\t\tconst iterator = source[Symbol.asyncIterator]()\n\t\ttry {\n\t\t\twhile (true) {\n\t\t\t\tawait this.#ready()\n\t\t\t\tcheckAbort(options?.signal)\n\t\t\t\tif (limit !== undefined && yielded >= limit) return\n\t\t\t\tconst step = await iterator.next()\n\t\t\t\tawait this.#ready()\n\t\t\t\tcheckAbort(options?.signal)\n\t\t\t\tif (step.done === true) return\n\t\t\t\tconst row = step.value\n\t\t\t\tif (conditions !== undefined && conditions.length > 0 && !matchesQuery(row, conditions)) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tconst narrowed = this.#cast(row)\n\t\t\t\tif (narrowed === undefined) continue\n\t\t\t\tif (matched < offset) {\n\t\t\t\t\tmatched += 1\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tmatched += 1\n\t\t\t\tyielded += 1\n\t\t\t\tyield narrowed\n\t\t\t}\n\t\t} finally {\n\t\t\tawait iterator.return?.()\n\t\t}\n\t}\n\n\t// Run a single-item operation across each item in order — the batch overloads\n\t// loop one item at a time (sequential, so writes never race) rather than\n\t// pushing batch logic into the thin driver. `signal` (write batches only) is\n\t// checked before EVERY item, so an abort mid-batch stops before the next\n\t// item runs — already-applied items stay applied (no rollback).\n\tasync #each<I, R>(\n\t\telements: readonly I[],\n\t\toperation: (element: I) => Promise<R>,\n\t\tsignal?: AbortSignal,\n\t): Promise<readonly R[]> {\n\t\tconst results: R[] = []\n\t\tfor (const element of elements) {\n\t\t\tcheckAbort(signal)\n\t\t\tresults.push(await operation(element))\n\t\t}\n\t\treturn results\n\t}\n\n\t// Read and narrow one row (assumes the driver is connected).\n\tasync #read(key: Key): Promise<T | undefined> {\n\t\treturn this.#cast(await this.#driver.read(this.#name, key))\n\t}\n\n\tasync #readCursor(key: Key): Promise<T | undefined> {\n\t\tawait this.#ready()\n\t\treturn this.#read(key)\n\t}\n\n\t// Read one row or throw NOT_FOUND.\n\tasync #resolveOne(key: Key): Promise<T> {\n\t\tconst row = await this.#read(key)\n\t\tif (row === undefined) {\n\t\t\tthrow new DatabaseError('NOT_FOUND', `No row '${key}' in table '${this.#name}'`, {\n\t\t\t\ttable: this.#name,\n\t\t\t\tkey,\n\t\t\t})\n\t\t}\n\t\treturn row\n\t}\n\n\t// Coerce/validate and write one row; `insert` selects the atomic insert primitive.\n\tasync #put(row: T, insert: boolean, options?: OperationOptions): Promise<Key> {\n\t\tconst validated = this.#validate(this.#prepare(row))\n\t\tconst key = this.#resolveKey(validated)\n\t\tif (insert) await this.#driver.insert(this.#name, key, validated, options)\n\t\telse await this.#driver.write(this.#name, key, validated, options)\n\t\t// Observe the written row — AFTER the driver write succeeded; carries the KEY only\n\t\t// (set / add / update all emit one `write`, the consumer re-reads if it needs the\n\t\t// value). A swallowed listener throw can't perturb the write (or its transaction).\n\t\tthis.#emitter.emit('write', key)\n\t\treturn key\n\t}\n\n\t// Merge changes into one existing row and re-validate; `false` when it is absent.\n\tasync #updateOne(key: Key, changes: Partial<T>, options?: OperationOptions): Promise<boolean> {\n\t\tconst existing = await this.#driver.read(this.#name, key)\n\t\tif (existing === undefined) {\n\t\t\tcheckAbort(options?.signal)\n\t\t\treturn false\n\t\t}\n\t\tconst input: unknown = changes\n\t\tif (isRecord(input) && Object.hasOwn(input, this.#key) && !equalsValue(input[this.#key], key)) {\n\t\t\tthrow new DatabaseError(\n\t\t\t\t'VALIDATION',\n\t\t\t\t`Update cannot change primary column '${this.#key}' on table '${this.#name}'`,\n\t\t\t\t{ table: this.#name, column: this.#key, key },\n\t\t\t)\n\t\t}\n\t\tawait this.#driver.write(\n\t\t\tthis.#name,\n\t\t\tkey,\n\t\t\tthis.#validate(Object.assign({}, existing, changes)),\n\t\t\toptions,\n\t\t)\n\t\t// Observe the updated row — AFTER the driver write, and only on the path that wrote\n\t\t// (an absent key returned `false` above, emitting nothing).\n\t\tthis.#emitter.emit('write', key)\n\t\treturn true\n\t}\n\n\tasync #updateCursor(key: Key, changes: Partial<T>): Promise<boolean> {\n\t\tawait this.#wait(undefined)\n\t\treturn this.#updateOne(key, changes)\n\t}\n\n\t// Delete one row, emitting `remove` only when a row was actually removed (a delete of\n\t// an absent key returns `false` and emits nothing) — AFTER the driver delete completes.\n\tasync #delete(key: Key, options?: OperationOptions): Promise<boolean> {\n\t\tconst removed = await this.#driver.delete(this.#name, key, options)\n\t\tif (removed) this.#emitter.emit('remove', key)\n\t\treturn removed\n\t}\n\n\tasync #deleteCursor(key: Key): Promise<boolean> {\n\t\tawait this.#wait(undefined)\n\t\treturn this.#delete(key)\n\t}\n\n\t// Await the shared lazy-open promise without tying its lifetime to this\n\t// mutation. An abort rejects this waiter promptly and consumes the open\n\t// promise's later settlement; the driver still checks the same signal at its\n\t// commit point, so a readiness completion after abort can never dispatch a\n\t// row mutation.\n\tasync #wait(signal: AbortSignal | undefined): Promise<void> {\n\t\tcheckAbort(signal)\n\t\tconst ready = this.#ready()\n\t\tif (signal === undefined) {\n\t\t\tawait ready\n\t\t\treturn\n\t\t}\n\t\tconst cleanup = new AbortController()\n\t\ttry {\n\t\t\tawait new Promise<void>((resolve, reject) => {\n\t\t\t\tsignal.addEventListener(\n\t\t\t\t\t'abort',\n\t\t\t\t\t() => {\n\t\t\t\t\t\tready.catch(() => {})\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tcheckAbort(signal)\n\t\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\t\treject(error)\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t\t{ once: true, signal: cleanup.signal },\n\t\t\t\t)\n\t\t\t\tready.then(resolve, reject)\n\t\t\t\tif (signal.aborted) {\n\t\t\t\t\tready.catch(() => {})\n\t\t\t\t\ttry {\n\t\t\t\t\t\tcheckAbort(signal)\n\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\treject(error)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t})\n\t\t} finally {\n\t\t\tcleanup.abort()\n\t\t}\n\t\tcheckAbort(signal)\n\t}\n\n\t// Gather the table's full contents from the driver's ordered scan.\n\tasync #collect(): Promise<readonly Row[]> {\n\t\tconst rows: Row[] = []\n\t\tfor await (const row of this.#driver.scan(this.#name)) rows.push(row)\n\t\treturn rows\n\t}\n\n\t// Copy the input and assign a generated key when the key column is empty.\n\t#prepare(row: T): Row {\n\t\tif (!isRecord(row)) {\n\t\t\tthrow new DatabaseError('VALIDATION', `Row for table '${this.#name}' is not a record`, {\n\t\t\t\ttable: this.#name,\n\t\t\t})\n\t\t}\n\t\tconst prepared: Row = { ...row }\n\t\tif (prepared[this.#key] === undefined) {\n\t\t\tif (this.#generate !== undefined) {\n\t\t\t\ttry {\n\t\t\t\t\tprepared[this.#key] = this.#generate()\n\t\t\t\t} catch (cause) {\n\t\t\t\t\tthrow new DatabaseError(\n\t\t\t\t\t\t'VALIDATION',\n\t\t\t\t\t\t`Failed to generate primary column '${this.#key}' for table '${this.#name}'`,\n\t\t\t\t\t\t{ table: this.#name, column: this.#key, cause },\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\ttry {\n\t\t\t\t\tprepared[this.#key] = crypto.randomUUID()\n\t\t\t\t} catch (cause) {\n\t\t\t\t\tthrow new DatabaseError(\n\t\t\t\t\t\t'DRIVER',\n\t\t\t\t\t\t`Host failed to generate primary column '${this.#key}' for table '${this.#name}'`,\n\t\t\t\t\t\t{ table: this.#name, column: this.#key, cause },\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn prepared\n\t}\n\n\t// Coerce *and* validate through the contract in one step: the contract's `parse`\n\t// now coerces types (`'36'` → `36`) AND enforces every leaf refinement (`min` /\n\t// `max` / `pattern`), so a non-`undefined` result already satisfies the guard\n\t// (AGENTS §14 parse↔guard soundness) — no separate `is` re-check is needed.\n\t// `isRecord` is kept solely to narrow the parsed `T` back to a storable `Row`\n\t// without an assertion (AGENTS §1); a table contract is always an object shape,\n\t// so it never rejects a genuinely-parsed row.\n\t#validate(row: Row): Row {\n\t\tconst parsed = this.#contract.parse(row)\n\t\tif (parsed === undefined || !isRecord(parsed)) {\n\t\t\tconst [fault] = this.#contract.explain(row)\n\t\t\tthrow new DatabaseError('VALIDATION', `Row failed the '${this.#name}' contract`, {\n\t\t\t\ttable: this.#name,\n\t\t\t\t...(fault === undefined ? {} : { field: fault.path, reason: fault.reason }),\n\t\t\t})\n\t\t}\n\t\treturn parsed\n\t}\n\n\t#resolveKey(row: Row): Key {\n\t\tconst key = extractKey(row, this.#key)\n\t\tif (key === undefined) {\n\t\t\tthrow new DatabaseError('VALIDATION', `Row has no usable key in column '${this.#key}'`, {\n\t\t\t\ttable: this.#name,\n\t\t\t\tcolumn: this.#key,\n\t\t\t})\n\t\t}\n\t\treturn key\n\t}\n\n\t// Narrow a stored row to the table's type through the contract guard.\n\t#cast(row: Row | undefined): T | undefined {\n\t\treturn row !== undefined && this.#guard(row) ? row : undefined\n\t}\n\n\t#track<R>(operation: () => Promise<R>): Promise<R> {\n\t\tif (this.#context !== undefined) return this.#context.track(operation)\n\t\treturn this.#scope === undefined ? operation() : this.#scope.track(operation)\n\t}\n}\n","import type { ContractInterface } from '@orkestrel/contract'\nimport type { EmitterErrorHandler } from '@orkestrel/emitter'\nimport type {\n\tColumnMap,\n\tDatabaseStorageInterface,\n\tKeyFunction,\n\tRowOf,\n\tTableInterface,\n\tPrimaryMap,\n\tTableMap,\n\tStorageInterface,\n} from './types.js'\nimport { createContract, objectShape } from '@orkestrel/contract'\nimport { DEFAULT_PRIMARY } from './constants.js'\nimport { DatabaseError } from './errors.js'\nimport { Table } from './Table.js'\nimport type { TransactionScope } from './TransactionScope.js'\n\n/**\n * A table-only database view bound to one driver transaction scope.\n *\n * @typeParam T - The declared table shape map\n *\n * @remarks\n * The view gives transaction work the same typed tables as its owning database\n * while enforcing a materially narrower contract and lifetime. `table` and\n * every table operation call the owning scope check, so a captured capability\n * cannot escape its transaction.\n */\nexport class DatabaseTransaction<\n\tT extends TableMap = TableMap,\n> implements DatabaseStorageInterface<T> {\n\treadonly #driver: StorageInterface\n\treadonly #tables: T\n\treadonly #primary: PrimaryMap\n\treadonly #generate: KeyFunction | undefined\n\treadonly #error: EmitterErrorHandler | undefined\n\treadonly #scope: TransactionScope\n\n\tconstructor(\n\t\tdriver: StorageInterface,\n\t\ttables: T,\n\t\tprimary: PrimaryMap,\n\t\tgenerate: KeyFunction | undefined,\n\t\terror: EmitterErrorHandler | undefined,\n\t\tscope: TransactionScope,\n\t) {\n\t\tthis.#driver = driver\n\t\tthis.#tables = tables\n\t\tthis.#primary = primary\n\t\tthis.#generate = generate\n\t\tthis.#error = error\n\t\tthis.#scope = scope\n\t}\n\n\ttable<K extends keyof T & string>(name: K): TableInterface<RowOf<T[K]>> {\n\t\tthis.#scope.check()\n\t\tconst columns = this.#columns(name)\n\t\treturn this.#build(name, this.#key(name), createContract(objectShape(columns)))\n\t}\n\n\t#build<R>(name: string, key: string, contract: ContractInterface<R>): TableInterface<R> {\n\t\treturn new Table(\n\t\t\t() => Promise.resolve(),\n\t\t\tthis.#driver,\n\t\t\tname,\n\t\t\tkey,\n\t\t\tcontract,\n\t\t\tthis.#generate,\n\t\t\tthis.#error,\n\t\t\tundefined,\n\t\t\tthis.#scope,\n\t\t)\n\t}\n\n\t#key(name: string): string {\n\t\treturn this.#primary[name] ?? DEFAULT_PRIMARY\n\t}\n\n\t#columns<K extends keyof T & string>(name: K): T[K]\n\t#columns(name: string): ColumnMap\n\t#columns(name: string): ColumnMap {\n\t\tconst columns = this.#tables[name]\n\t\tif (columns === undefined) {\n\t\t\tthrow new DatabaseError('NOT_FOUND', `Table '${name}' is not declared`, { table: name })\n\t\t}\n\t\treturn columns\n\t}\n}\n","import type { ContractInterface, Result } from '@orkestrel/contract'\nimport type { EmitterInterface } from '@orkestrel/emitter'\nimport type {\n\tColumnMap,\n\tDatabaseEventMap,\n\tDatabaseInterface,\n\tDatabaseOptions,\n\tDatabaseStatus,\n\tDatabaseStorageInterface,\n\tIndexMap,\n\tKeyFunction,\n\tMigration,\n\tOperationOptions,\n\tPrimaryMap,\n\tRowOf,\n\tTableDefinition,\n\tTableInterface,\n\tTableMap,\n\tTableSchema,\n} from './types.js'\nimport { compileSchema, createContract, objectShape } from '@orkestrel/contract'\nimport { DEFAULT_PRIMARY } from './constants.js'\nimport { DatabaseError } from './errors.js'\nimport { shapeToColumnSchema } from './helpers.js'\nimport { DatabaseContext } from './DatabaseContext.js'\nimport { DatabaseTransaction } from './DatabaseTransaction.js'\nimport { Table } from './Table.js'\nimport type { TransactionScope } from './TransactionScope.js'\n\n/**\n * A typed database view over one shared internal lifecycle and storage context.\n *\n * @remarks\n * Each view owns only its table contracts, primary columns, indexes, and key\n * generator. Imported views register their physical schemas with the same\n * internal context before opening begins, so every view observes one driver,\n * merged schema, emitter, status, transaction boundary, and terminal close.\n */\nexport class Database<T extends TableMap = TableMap> implements DatabaseInterface<T> {\n\t#context: DatabaseContext\n\treadonly #tables: T\n\treadonly #primary: PrimaryMap\n\treadonly #indexes: IndexMap\n\treadonly #generate: KeyFunction | undefined\n\n\tconstructor(options: DatabaseOptions<T>) {\n\t\tthis.#tables = options.tables\n\t\tthis.#primary = options.primary ?? {}\n\t\tthis.#indexes = options.indexes ?? {}\n\t\tthis.#generate = options.generator\n\t\tthis.#context = new DatabaseContext(options)\n\t\tthis.#context.register(this.#schema())\n\t}\n\n\tget emitter(): EmitterInterface<DatabaseEventMap> {\n\t\treturn this.#context.emitter\n\t}\n\n\tget name(): string {\n\t\treturn this.#context.name\n\t}\n\n\tget status(): DatabaseStatus {\n\t\treturn this.#context.status\n\t}\n\n\ttable<K extends keyof T & string>(name: K): TableInterface<RowOf<T[K]>> {\n\t\tif (this.#context.status === 'closed') {\n\t\t\tthrow new DatabaseError('CLOSED', `Database '${this.#context.name}' is closed`, {\n\t\t\t\tname: this.#context.name,\n\t\t\t})\n\t\t}\n\t\tconst columns = this.#columns(name)\n\t\treturn this.#build(name, this.#key(name), createContract(objectShape(columns)))\n\t}\n\n\timport<U extends TableMap>(tables: U, primary?: PrimaryMap): DatabaseInterface<U> {\n\t\treturn this.#spawn(tables, { ...this.#primary, ...primary })\n\t}\n\n\texport(): Readonly<Record<string, TableDefinition>> {\n\t\tconst result: Record<string, TableDefinition> = {}\n\t\tfor (const name of Object.keys(this.#tables)) {\n\t\t\tconst columns = this.#columns(name)\n\t\t\tresult[name] = {\n\t\t\t\tprimary: this.#key(name),\n\t\t\t\tcolumns,\n\t\t\t\tschema: compileSchema(objectShape(columns)),\n\t\t\t}\n\t\t}\n\t\treturn result\n\t}\n\n\topen(): Promise<void> {\n\t\treturn this.#context.open()\n\t}\n\n\tclose(): Promise<void> {\n\t\treturn this.#context.close()\n\t}\n\n\ttransaction<R>(\n\t\tscope: (transaction: DatabaseStorageInterface<T>) => Promise<R>,\n\t\toptions?: OperationOptions,\n\t): Promise<R> {\n\t\treturn this.#context.transaction(async (storage, lifetime) => {\n\t\t\tconst transaction = new DatabaseTransaction(\n\t\t\t\tstorage,\n\t\t\t\tthis.#tables,\n\t\t\t\tthis.#primary,\n\t\t\t\tthis.#generate,\n\t\t\t\tthis.#context.error,\n\t\t\t\tlifetime,\n\t\t\t)\n\t\t\treturn this.#settle(scope, transaction, lifetime)\n\t\t}, options)\n\t}\n\n\tmigrate(deployed: readonly TableSchema[], options?: OperationOptions): Promise<Migration> {\n\t\treturn this.#context.migrate(deployed, options)\n\t}\n\n\t#build<R>(name: string, key: string, contract: ContractInterface<R>): TableInterface<R> {\n\t\treturn new Table(\n\t\t\t() => this.#context.connect(),\n\t\t\tthis.#context.driver,\n\t\t\tname,\n\t\t\tkey,\n\t\t\tcontract,\n\t\t\tthis.#generate,\n\t\t\tthis.#context.error,\n\t\t\tthis.#context,\n\t\t)\n\t}\n\n\t#spawn<X extends TableMap>(tables: X, primary: PrimaryMap): DatabaseInterface<X> {\n\t\treturn Database.#attach(\n\t\t\t{\n\t\t\t\tdriver: this.#context.driver,\n\t\t\t\ttables,\n\t\t\t\tprimary,\n\t\t\t\tname: this.#context.name,\n\t\t\t\t...(this.#context.error === undefined ? {} : { error: this.#context.error }),\n\t\t\t\t...(this.#generate === undefined ? {} : { generator: this.#generate }),\n\t\t\t\t...(this.#context.version === undefined ? {} : { version: this.#context.version }),\n\t\t\t},\n\t\t\tthis.#context,\n\t\t)\n\t}\n\n\t#key(name: string): string {\n\t\treturn this.#primary[name] ?? DEFAULT_PRIMARY\n\t}\n\n\t#columns<K extends keyof T & string>(name: K): T[K]\n\t#columns(name: string): ColumnMap\n\t#columns(name: string): ColumnMap {\n\t\tconst columns = this.#tables[name]\n\t\tif (columns === undefined) {\n\t\t\tthrow new DatabaseError('NOT_FOUND', `Table '${name}' is not declared`, { table: name })\n\t\t}\n\t\treturn columns\n\t}\n\n\t#schema(): readonly TableSchema[] {\n\t\treturn Object.keys(this.#tables).map((name) => {\n\t\t\tconst columns = this.#columns(name)\n\t\t\treturn {\n\t\t\t\tname,\n\t\t\t\tprimary: this.#key(name),\n\t\t\t\tcolumns: Object.entries(columns).map(([column, shape]) =>\n\t\t\t\t\tshapeToColumnSchema(column, shape),\n\t\t\t\t),\n\t\t\t\tindexes: this.#indexes[name] ?? [],\n\t\t\t}\n\t\t})\n\t}\n\n\tasync #settle<R>(\n\t\tscope: (transaction: DatabaseStorageInterface<T>) => Promise<R>,\n\t\ttransaction: DatabaseStorageInterface<T>,\n\t\tlifetime: TransactionScope,\n\t): Promise<Result<R, unknown>> {\n\t\tconst outcome: Result<R, unknown> = await Promise.resolve()\n\t\t\t.then(() => scope(transaction))\n\t\t\t.then(\n\t\t\t\t(value) => ({ success: true, value }),\n\t\t\t\t(error: unknown) => ({ success: false, error }),\n\t\t\t)\n\t\tlifetime.stop()\n\t\tconst drained: Result<void, unknown> = await lifetime.drain().then(\n\t\t\t() => ({ success: true, value: undefined }),\n\t\t\t(error: unknown) => ({ success: false, error }),\n\t\t)\n\t\tif (!outcome.success) return outcome\n\t\tif (!drained.success) return drained\n\t\treturn outcome\n\t}\n\n\tstatic #attach<X extends TableMap>(\n\t\toptions: DatabaseOptions<X>,\n\t\tcontext: DatabaseContext,\n\t): Database<X> {\n\t\tconst database = new Database(options)\n\t\tdatabase.#context = context\n\t\tcontext.register(database.#schema())\n\t\treturn database\n\t}\n}\n","import type {\n\tQueryInput,\n\tDriverInterface,\n\tDriverMetadata,\n\tKey,\n\tMigrationInput,\n\tMigrationStep,\n\tOperationOptions,\n\tRow,\n\tTableSchema,\n} from '../types.js'\nimport { cloneDriverMetadata, cloneMigrationInput } from '../cloners.js'\nimport { DatabaseError } from '../errors.js'\nimport {\n\tbindRowKey,\n\tcheckAbort,\n\tcompareValues,\n\tequalsValue,\n\tmatchesQuery,\n\tmigrateRows,\n\tnormalizeDriverSchema,\n\tplanMigration,\n\tprojectMigrationSchema,\n} from '../helpers.js'\nimport { isKey, validatePage } from '../validators.js'\n\n/**\n * The reference {@link DriverInterface} — nested maps, no I/O.\n *\n * @remarks\n * The in-between made concrete: it runs identically in a browser or on a server,\n * so it is the storage behind tests, ephemeral caches, and any code that wants\n * the database API without a persistent backend. Rows are DEEP-copied (via\n * `structuredClone`) in and out — at `write`, `read`, `scan`, `stream`, and both\n * snapshot capture and restore — so a caller mutating a nested field of an input\n * row, a returned row, or a row mutated in place between snapshot and rollback\n * can never perturb stored state (AGENTS §11); a shallow `{ ...row }` spread\n * would still share nested object/array references. Metadata instead routes\n * through `cloneDriverMetadata`: `stamp` and migration snapshot exact JSON at\n * ingress, and `metadata` returns a distinct deeply frozen owned copy. `snapshot`\n * clones every table to give transactions an exact rollback point. `scan` and\n * `keys` yield in key order — sorted by the core {@link compareValues} total\n * order, the same contract the SQLite (`ORDER BY`) and IndexedDB (key-ordered\n * reads) backends honor, so an unordered read agrees across every backend rather\n * than leaking Map insertion order. A persistent backend (IndexedDB, SQLite)\n * implements the same required methods over real storage.\n */\nexport class MemoryDriver implements DriverInterface {\n\treadonly #tables = new Map<string, Map<Key, Row>>()\n\t#identities = new Map<string, object>()\n\t#schema: readonly TableSchema[] = []\n\t#metadata: DriverMetadata | undefined\n\n\tasync open(schema: readonly TableSchema[]): Promise<void> {\n\t\tconst owned = normalizeDriverSchema(schema)\n\t\tconst deployed = normalizeDriverSchema(this.#metadata?.schema ?? owned)\n\t\tconst names = new Set(deployed.map((table) => table.name))\n\t\tfor (const name of this.#identities.keys()) {\n\t\t\tif (!names.has(name)) this.#identities.delete(name)\n\t\t}\n\t\tfor (const table of deployed) {\n\t\t\tif (!this.#tables.has(table.name)) this.#tables.set(table.name, new Map())\n\t\t\tif (!this.#identities.has(table.name)) this.#identities.set(table.name, {})\n\t\t}\n\t\tthis.#schema = deployed\n\t}\n\n\tasync close(): Promise<void> {}\n\n\tasync read(table: string, key: Key): Promise<Row | undefined> {\n\t\tconst row = this.#store(table).get(key)\n\t\treturn row === undefined ? undefined : structuredClone(row)\n\t}\n\n\tasync write(table: string, key: Key, row: Row, options?: OperationOptions): Promise<void> {\n\t\tcheckAbort(options?.signal)\n\t\tconst primary = this.#table(table).primary\n\t\tthis.#store(table).set(key, structuredClone(bindRowKey(row, primary, key)))\n\t}\n\n\tasync insert(table: string, key: Key, row: Row, options?: OperationOptions): Promise<void> {\n\t\tcheckAbort(options?.signal)\n\t\tconst store = this.#store(table)\n\t\tif (store.has(key)) {\n\t\t\tthrow new DatabaseError('CONFLICT', `Row '${key}' already exists in table '${table}'`, {\n\t\t\t\ttable,\n\t\t\t\tkey,\n\t\t\t})\n\t\t}\n\t\tconst primary = this.#table(table).primary\n\t\tstore.set(key, structuredClone(bindRowKey(row, primary, key)))\n\t}\n\n\tasync delete(table: string, key: Key, options?: OperationOptions): Promise<boolean> {\n\t\tcheckAbort(options?.signal)\n\t\treturn this.#store(table).delete(key)\n\t}\n\n\tasync keys(table: string): Promise<readonly Key[]> {\n\t\treturn this.#ordered(table)\n\t}\n\n\tasync *scan(table: string): AsyncIterable<Row> {\n\t\tconst store = this.#store(table)\n\t\tfor (const key of this.#ordered(table)) {\n\t\t\tconst row = store.get(key)\n\t\t\tif (row !== undefined) yield structuredClone(row)\n\t\t}\n\t}\n\n\t/**\n\t * Natively filtered lazy iteration — the {@link DriverInterface.stream} hook.\n\t *\n\t * @remarks\n\t * Iterates the table's keys in the same key order `scan` and `keys` yield\n\t * (sorted by {@link compareValues}), testing each row against\n\t * `input.conditions` (via {@link matchesQuery}) before counting it\n\t * toward `offset` / `limit`. Both are applied lazily as matches are found —\n\t * `offset` matches are skipped without being yielded, and iteration stops the\n\t * instant `limit` yields have been produced, so a large table is never fully\n\t * walked for a small page. `input.order` is IGNORED (the same contract as\n\t * `TableInterface.scan` and `QueryInterface.stream`): streaming yields key\n\t * order, sorted output is `records()`'s job. Rows yield copy-out (AGENTS\n\t * §11), and an unknown table mirrors `scan`'s empty-yield behavior.\n\t *\n\t * @param table - The table to stream\n\t * @param input - The filter / offset / limit to apply lazily\n\t *\n\t * @example\n\t * ```ts\n\t * for await (const row of driver.stream('users', { conditions, limit: 10 })) {\n\t * // one matched row at a time, in key order\n\t * }\n\t * ```\n\t */\n\tstream(table: string, input: QueryInput): AsyncIterable<Row> {\n\t\tvalidatePage(input)\n\t\treturn this.#stream(table, input)\n\t}\n\n\tasync *#stream(table: string, input: QueryInput): AsyncIterable<Row> {\n\t\tconst store = this.#store(table)\n\t\tconst conditions = input.conditions\n\t\tconst offset = input.offset ?? 0\n\t\tconst limit = input.limit\n\t\tlet skipped = 0\n\t\tlet yielded = 0\n\t\tfor (const key of this.#ordered(table)) {\n\t\t\tif (limit !== undefined && yielded >= limit) return\n\t\t\tconst row = store.get(key)\n\t\t\tif (row === undefined) continue\n\t\t\tif (conditions !== undefined && conditions.length > 0 && !matchesQuery(row, conditions)) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif (skipped < offset) {\n\t\t\t\tskipped += 1\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tyield structuredClone(row)\n\t\t\tyielded += 1\n\t\t}\n\t}\n\n\tasync clear(table: string): Promise<void> {\n\t\tthis.#store(table).clear()\n\t}\n\n\t/**\n\t * Capture the current state and return a thunk that rolls back to it.\n\t *\n\t * @remarks\n\t * Capture owns rows, schema, and one session-local table identity. Replay\n\t * adapts rows to each surviving same-identity table's current schema before\n\t * changing storage. Removed or replaced tables are skipped; uncaptured and\n\t * later-added tables retain their current rows. Schema and metadata are never\n\t * restored.\n\t *\n\t * @param tables - The table names to scope the snapshot to; omitted captures every table\n\t * @returns A thunk that restores the captured tables\n\t */\n\tasync snapshot(tables?: readonly string[]): Promise<() => Promise<void>> {\n\t\tconst names =\n\t\t\ttables === undefined\n\t\t\t\t? this.#schema.map((table) => table.name)\n\t\t\t\t: [...new Set(tables)].filter((name) => this.#schema.some((table) => table.name === name))\n\t\tconst captured = new Map<\n\t\t\tstring,\n\t\t\t{\n\t\t\t\treadonly identity: object\n\t\t\t\treadonly rows: ReadonlyMap<Key, Row>\n\t\t\t\treadonly schema: TableSchema\n\t\t\t}\n\t\t>()\n\t\tfor (const name of names) {\n\t\t\tconst schema = this.#schema.find((table) => table.name === name)\n\t\t\tconst store = this.#tables.get(name)\n\t\t\tconst identity = this.#identities.get(name)\n\t\t\tif (schema === undefined || store === undefined || identity === undefined) continue\n\t\t\tconst rows = new Map<Key, Row>()\n\t\t\tfor (const [key, row] of store) rows.set(key, structuredClone(row))\n\t\t\tcaptured.set(name, { identity, rows, schema })\n\t\t}\n\t\treturn async () => {\n\t\t\tconst replacements = new Map<Map<Key, Row>, ReadonlyMap<Key, Row>>()\n\t\t\tfor (const [name, capture] of captured) {\n\t\t\t\tconst schema = this.#schema.find((table) => table.name === name)\n\t\t\t\tconst store = this.#tables.get(name)\n\t\t\t\tif (\n\t\t\t\t\tschema === undefined ||\n\t\t\t\t\tstore === undefined ||\n\t\t\t\t\tthis.#identities.get(name) !== capture.identity\n\t\t\t\t) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tconst plan = planMigration([capture.schema], [schema])\n\t\t\t\tconst entries = [...capture.rows.entries()]\n\t\t\t\tconst migrated = migrateRows(\n\t\t\t\t\tentries.map(([, row]) => row),\n\t\t\t\t\tplan.steps,\n\t\t\t\t)\n\t\t\t\tif (migrated.length !== entries.length) {\n\t\t\t\t\tthrow new DatabaseError('MIGRATION', 'Snapshot row count changed during migration', {\n\t\t\t\t\t\ttable: name,\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t\tconst rows = new Map<Key, Row>()\n\t\t\t\tfor (const [index, [key]] of entries.entries()) {\n\t\t\t\t\tconst row = migrated[index]\n\t\t\t\t\tif (!isKey(key) || row === undefined) {\n\t\t\t\t\t\tthrow new DatabaseError('MIGRATION', 'Snapshot row has no usable primary key', {\n\t\t\t\t\t\t\ttable: name,\n\t\t\t\t\t\t\tcolumn: schema.primary,\n\t\t\t\t\t\t\tindex,\n\t\t\t\t\t\t})\n\t\t\t\t\t}\n\t\t\t\t\trows.set(key, structuredClone(bindRowKey(row, schema.primary, key)))\n\t\t\t\t}\n\t\t\t\treplacements.set(store, rows)\n\t\t\t}\n\t\t\tfor (const [store, rows] of replacements) {\n\t\t\t\tstore.clear()\n\t\t\t\tfor (const [key, row] of rows) store.set(key, row)\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Return the persisted {@link DriverMetadata}, or `undefined` when the store has\n\t * never been stamped.\n\t *\n\t * @remarks\n\t * In-process only — the metadata lives in this instance's memory, exactly\n\t * like the rest of this driver's storage. The returned value is a distinct\n\t * deeply frozen owned snapshot. A driver-conformance-valid implementation of\n\t * the optional `metadata` / `stamp` pair.\n\t *\n\t * @returns The last-stamped {@link DriverMetadata}, or `undefined`\n\t */\n\tasync metadata(): Promise<DriverMetadata | undefined> {\n\t\treturn this.#metadata === undefined ? undefined : cloneDriverMetadata(this.#metadata)\n\t}\n\n\t/**\n\t * Persist an owned snapshot for a later `metadata()` to return.\n\t *\n\t * @param metadata - The {@link DriverMetadata} to persist\n\t */\n\tasync stamp(metadata: DriverMetadata): Promise<void> {\n\t\tthis.#metadata = cloneDriverMetadata(metadata)\n\t}\n\n\t/**\n\t * Apply a {@link Migration} plan's steps against the in-memory store.\n\t *\n\t * @remarks\n\t * Steps apply against an isolated candidate. Rows, schema changes, and\n\t * optional metadata publish together only after the whole request succeeds.\n\t *\n\t * @param input - The migration plan and optional metadata to settle atomically\n\t */\n\tasync migrate(input: MigrationInput): Promise<void> {\n\t\tconst owned = cloneMigrationInput(input)\n\t\tconst schema = projectMigrationSchema(this.#schema, owned.plan.steps)\n\t\tif (\n\t\t\towned.metadata !== undefined &&\n\t\t\t!equalsValue(normalizeDriverSchema(owned.metadata.schema), schema)\n\t\t) {\n\t\t\tthrow new DatabaseError('MIGRATION', 'Migration metadata schema does not match the plan', {\n\t\t\t\tprojected: schema,\n\t\t\t\tmetadata: owned.metadata.schema,\n\t\t\t})\n\t\t}\n\t\tconst candidate = this.#copy(this.#tables)\n\t\tconst identities = this.#projectIdentities(this.#identities, owned.plan.steps)\n\t\tfor (const step of owned.plan.steps) this.#migrate(candidate, step)\n\t\tthis.#tables.clear()\n\t\tfor (const [name, store] of candidate) this.#tables.set(name, store)\n\t\tthis.#identities = identities\n\t\tthis.#schema = schema\n\t\tif (owned.metadata !== undefined) this.#metadata = owned.metadata\n\t}\n\n\t// A table's keys in key order — the contract `scan` and `keys` yield in.\n\t// `compareValues` is the core total order (number < string, natural within),\n\t// matching the SQLite `ORDER BY` and IndexedDB key-range orderings.\n\t#ordered(table: string): readonly Key[] {\n\t\treturn [...this.#store(table).keys()].sort(compareValues)\n\t}\n\n\t// A migration step's table must already exist — unlike `#store`, a missing\n\t// table here is a MIGRATION error rather than an as-yet-untouched table.\n\t#require(tables: Map<string, Map<Key, Row>>, table: string): Map<Key, Row> {\n\t\tconst store = tables.get(table)\n\t\tif (store === undefined) {\n\t\t\tthrow new DatabaseError('MIGRATION', `migrate: unknown table '${table}'`, { table })\n\t\t}\n\t\treturn store\n\t}\n\n\t#copy(tables: Map<string, Map<Key, Row>>): Map<string, Map<Key, Row>> {\n\t\tconst copy = new Map<string, Map<Key, Row>>()\n\t\tfor (const [name, store] of tables) {\n\t\t\tconst cloned = new Map<Key, Row>()\n\t\t\tfor (const [key, row] of store) cloned.set(key, structuredClone(row))\n\t\t\tcopy.set(name, cloned)\n\t\t}\n\t\treturn copy\n\t}\n\n\t#projectIdentities(\n\t\tidentities: ReadonlyMap<string, object>,\n\t\tsteps: readonly MigrationStep[],\n\t): Map<string, object> {\n\t\tconst projected = new Map(identities)\n\t\tfor (const step of steps) {\n\t\t\tif (step.operation === 'table.add') projected.set(step.table.name, {})\n\t\t\tif (step.operation === 'table.remove') projected.delete(step.table)\n\t\t}\n\t\treturn projected\n\t}\n\n\t#table(name: string): TableSchema {\n\t\tconst schema = this.#schema.find((table) => table.name === name)\n\t\tif (schema === undefined) {\n\t\t\tthrow new DatabaseError('NOT_FOUND', `Unknown table '${name}'`, { table: name })\n\t\t}\n\t\treturn schema\n\t}\n\n\t#migrate(tables: Map<string, Map<Key, Row>>, step: MigrationStep): void {\n\t\tswitch (step.operation) {\n\t\t\tcase 'table.add':\n\t\t\t\tif (!tables.has(step.table.name)) tables.set(step.table.name, new Map())\n\t\t\t\tbreak\n\t\t\tcase 'table.remove':\n\t\t\t\tthis.#require(tables, step.table)\n\t\t\t\ttables.delete(step.table)\n\t\t\t\tbreak\n\t\t\tcase 'column.add':\n\t\t\tcase 'column.remove': {\n\t\t\t\tconst store = this.#require(tables, step.table)\n\t\t\t\tconst rows = [...store.entries()]\n\t\t\t\tconst migrated = migrateRows(\n\t\t\t\t\trows.map(([, row]) => row),\n\t\t\t\t\t[step],\n\t\t\t\t)\n\t\t\t\tfor (const [index, [key]] of rows.entries()) {\n\t\t\t\t\tconst row = migrated[index]\n\t\t\t\t\tif (row === undefined) {\n\t\t\t\t\t\tthrow new DatabaseError('MIGRATION', 'migrate: transformed row is missing', {\n\t\t\t\t\t\t\ttable: step.table,\n\t\t\t\t\t\t\tindex,\n\t\t\t\t\t\t})\n\t\t\t\t\t}\n\t\t\t\t\tstore.set(key, row)\n\t\t\t\t}\n\t\t\t\tbreak\n\t\t\t}\n\t\t\tcase 'index.add':\n\t\t\tcase 'index.remove':\n\t\t\t\tthis.#require(tables, step.table)\n\t\t\t\tbreak\n\t\t}\n\t}\n\n\t// Resolve only a currently declared table. `open` creates every backing map,\n\t// so a missing map is a lookup failure rather than an implicit declaration.\n\t#store(table: string): Map<Key, Row> {\n\t\tthis.#table(table)\n\t\tconst store = this.#tables.get(table)\n\t\tif (store === undefined) {\n\t\t\tthrow new DatabaseError('NOT_FOUND', `Table '${table}' has no backing store`, { table })\n\t\t}\n\t\treturn store\n\t}\n}\n","import type { DatabaseInterface, DatabaseOptions, DriverInterface, TableMap } from './types.js'\nimport { Database } from './Database.js'\nimport { MemoryDriver } from './drivers/MemoryDriver.js'\n\n/**\n * Create a database over a driver and a declared `tables` schema.\n *\n * @remarks\n * `tables` maps each name to its columns (a `column → shape` map); the database\n * wraps each in an `objectShape`, so you never write `objectShape` at the table\n * level. The `const` type parameter captures the literal names and columns, so\n * `db.table('users')` is checked against the schema and typed by `Infer` of its\n * columns — no annotations. Name a non-`id` primary-key column per table via the\n * optional `primary` and `indexes` maps.\n *\n * @param options - The driver, `tables`, and optional `primary`, `indexes`,\n * `name`, `generator`, `version`, and emitter hooks\n * @returns A typed {@link DatabaseInterface}\n *\n * @example\n * ```ts\n * import { createDatabase, createMemoryDriver } from '@orkestrel/database'\n * import { integerShape, stringShape } from '@orkestrel/contract'\n *\n * const db = createDatabase({\n * \tdriver: createMemoryDriver(),\n * \ttables: {\n * \t\tusers: { id: stringShape(), age: integerShape() },\n * \t\tposts: { slug: stringShape(), title: stringShape() },\n * \t},\n * \tprimary: { posts: 'slug' },\n * })\n * await db.table('users').set({ id: 'u1', age: 36 }) // typed; coerced + validated\n * ```\n */\nexport function createDatabase<const T extends TableMap>(\n\toptions: DatabaseOptions<T>,\n): DatabaseInterface<T> {\n\treturn new Database(options)\n}\n\n/**\n * Create the in-memory reference {@link DriverInterface}.\n *\n * @remarks\n * Backed by nested maps with no I/O — the same driver runs in a browser or on a\n * server, making it the natural choice for tests and ephemeral storage.\n *\n * @returns A fresh in-memory driver\n */\nexport function createMemoryDriver(): DriverInterface {\n\treturn new MemoryDriver()\n}\n"],"mappings":";;;;;;;;;;AASA,IAAa,kBAAkB;;;;;;;;;;;;;;AAe/B,IAAa,qBAAqB;;;;;;;;;;;;;;;;;;;ACDlC,IAAa,gBAAb,cAAmC,MAAM;CACxC;CACA;CAEA,YACC,MACA,SACA,SACC;EACD,MAAM,OAAO;EACb,KAAK,OAAO;EACZ,KAAK,OAAO;EACZ,IAAI,YAAY,KAAA,GAAW,KAAK,UAAU;CAC3C;AACD;;;;;;;;;;;;;;;;AAiBA,SAAgB,gBAAgB,OAAwC;CACvE,OAAO,iBAAiB;AACzB;;;;;;;;;;;;;;;AC/BA,SAAgB,aAAa,OAA0B;CACtD,MAAM,QAAQ,OAAO;CACrB,IAAI,UAAU,KAAA,MAAc,CAAC,OAAO,UAAU,KAAK,KAAK,QAAQ,IAC/D,MAAM,IAAI,cAAc,cAAc,6CAA6C;EAClF,OAAO;EACP,OAAO,OAAO,SAAS,KAAK,IAAI,QAAQ,OAAO,KAAK;CACrD,CAAC;CAEF,MAAM,SAAS,OAAO;CACtB,IAAI,WAAW,KAAA,MAAc,CAAC,OAAO,UAAU,MAAM,KAAK,SAAS,IAClE,MAAM,IAAI,cAAc,cAAc,8CAA8C;EACnF,OAAO;EACP,OAAO,OAAO,SAAS,MAAM,IAAI,SAAS,OAAO,MAAM;CACxD,CAAC;AAEH;;;;;;;AAQA,SAAgB,MAAM,OAA8B;CACnD,OAAO,OAA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M,IAAI,GAAG,OAAO,IAAI,KAAK,MAAM,sBAAM,IAAI,IAAI,CAAC;IACvE;GACD,KAAK;IACJ,KAAKQ,SAAS,QAAQ,KAAK,KAAK;IAChC,OAAO,OAAO,KAAK,KAAK;IACxB;GACD,KAAK;GACL,KAAK,iBAAiB;IACrB,MAAM,QAAQ,KAAKA,SAAS,QAAQ,KAAK,KAAK;IAC9C,MAAM,OAAO,CAAC,GAAG,MAAM,QAAQ,CAAC;IAChC,MAAM,WAAW,YAChB,KAAK,KAAK,GAAG,SAAS,GAAG,GACzB,CAAC,IAAI,CACN;IACA,KAAK,MAAM,CAAC,OAAO,CAAC,SAAS,KAAK,QAAQ,GAAG;KAC5C,MAAM,MAAM,SAAS;KACrB,IAAI,QAAQ,KAAA,GACX,MAAM,IAAI,cAAc,aAAa,uCAAuC;MAC3E,OAAO,KAAK;MACZ;KACD,CAAC;KAEF,MAAM,IAAI,KAAK,GAAG;IACnB;IACA;GACD;GACA,KAAK;GACL,KAAK,gBACJ,KAAKA,SAAS,QAAQ,KAAK,KAAK;EAElC;CACD;CAIA,OAAO,OAA8B;EACpC,KAAKN,OAAO,KAAK;EACjB,MAAM,QAAQ,KAAKL,QAAQ,IAAI,KAAK;EACpC,IAAI,UAAU,KAAA,GACb,MAAM,IAAI,cAAc,aAAa,UAAU,MAAM,yBAAyB,EAAE,MAAM,CAAC;EAExF,OAAO;CACR;AACD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;ACxWA,SAAgB,eACf,SACuB;CACvB,OAAO,IAAI,SAAS,OAAO;AAC5B;;;;;;;;;;AAWA,SAAgB,qBAAsC;CACrD,OAAO,IAAI,aAAa;AACzB"}
1
+ {"version":3,"file":"index.js","names":["#source","#scope","#continue","#next","#return","#throw","#cleaned","#cleanup","#operations","#accepting","#failed","#error","#driver","#name","#version","#error","#emitter","#operations","#status","#transaction","#ready","#schema","#outside","#drain","#connect","#admit","#rollbackError","#transition","#failure","#reconcile","#stamp","#apply","#migration","#keys","#read","#update","#remove","#track","#value","#index","#closed","#queue","#advance","#revise","#delete","#tail","#source","#context","#continue","#next","#return","#throw","#cleaned","#cleanup","#table","#conditions","#orders","#filters","#limit","#offset","#page","#filtered","#ready","#driver","#name","#key","#contract","#guard","#generate","#context","#scope","#emitter","#track","#each","#read","#resolveOne","#collect","#scan","#wait","#put","#updateOne","#delete","#readCursor","#updateCursor","#deleteCursor","#cast","#validate","#prepare","#resolveKey","#driver","#tables","#primary","#generate","#error","#scope","#columns","#build","#key","#tables","#primary","#indexes","#generate","#context","#schema","#columns","#build","#key","#spawn","#settle","#attach","#tables","#metadata","#identities","#schema","#store","#table","#ordered","#stream","#copy","#projectIdentities","#migrate","#require"],"sources":["../../../src/core/constants.ts","../../../src/core/errors.ts","../../../src/core/validators.ts","../../../src/core/cloners.ts","../../../src/core/helpers.ts","../../../src/core/TransactionIterator.ts","../../../src/core/TransactionScope.ts","../../../src/core/DatabaseContext.ts","../../../src/core/Cursor.ts","../../../src/core/DatabaseIterator.ts","../../../src/core/Query.ts","../../../src/core/Table.ts","../../../src/core/DatabaseTransaction.ts","../../../src/core/Database.ts","../../../src/core/drivers/MemoryDriver.ts","../../../src/core/factories.ts"],"sourcesContent":["// Database constants — frozen plain data (AGENTS §5).\n\n/**\n * The primary-key column assumed when {@link PrimaryMap} does not name one.\n *\n * @remarks\n * `id` is the convention IndexedDB (`keyPath: 'id'`) and SQL (`id` / rowid) both\n * lean on, so a table without a `primary` override keys its rows by `id`.\n */\nexport const DEFAULT_PRIMARY = 'id'\n\n/**\n * The longest `LIKE` / `GLOB` pattern the wildcard matcher accepts before rejecting it.\n *\n * @remarks\n * A ReDoS bound (AGENTS §6.5): the SA1–SA4 migration lets a model supply `list`\n * input over the wire, so `matchesLikePattern` / `matchesGlobPattern` run attacker-controlled\n * patterns. The matcher is the LINEAR greedy two-pointer wildcard match — never a\n * backtracking regex (`.*`-segments-separated-by-literals against a long input is the\n * catastrophic shape JS cannot bound without atomic groups), so it is O(value ×\n * pattern). Capping the pattern length bounds that pattern factor, leaving a match\n * linear in the value length whatever the pattern. A longer pattern throws a\n * `VALIDATION` {@link DatabaseError}; the cap is generous for any legitimate search.\n */\nexport const MAX_PATTERN_LENGTH = 1024\n","import type { DatabaseErrorCode } from './types.js'\n\n// AGENTS §12: invalid operations and programmer errors `throw`, always a\n// `DatabaseError` carrying a machine-readable `code` so a `catch` branches on\n// `error.code` instead of parsing the message. Lookups that may simply miss\n// (`get`, `has`, `remove`) return `undefined` / `false` — they never throw.\n\n/**\n * An error thrown by the database layer.\n *\n * @remarks\n * Carries a {@link DatabaseErrorCode} and an optional `context` bag naming the\n * offending table / key. Thrown for: operating on a closed database (`CLOSED`), a\n * `resolve` miss (`NOT_FOUND`), an `add` onto an existing key (`CONFLICT`), a\n * row that fails its table's contract (`VALIDATION`), an aborted operation whose\n * {@link OperationOptions.signal} aborted (`ABORTED`, carrying `signal.reason` in\n * `context`), an inapplicable {@link Migration} plan (`MIGRATION`), a\n * driver that violates a {@link DriverInterface} invariant, thrown by the\n * `conformDriver` helper (`CONFORMANCE`), and an unexpected infrastructure\n * fault surfaced by a driver seam — e.g. a filesystem failure while\n * persisting (`DRIVER`) — as opposed to expected domain conditions, which\n * keep their specific codes.\n */\nexport class DatabaseError extends Error {\n\treadonly code: DatabaseErrorCode\n\treadonly context?: Readonly<Record<string, unknown>>\n\n\tconstructor(\n\t\tcode: DatabaseErrorCode,\n\t\tmessage: string,\n\t\tcontext?: Readonly<Record<string, unknown>>,\n\t) {\n\t\tsuper(message)\n\t\tthis.name = 'DatabaseError'\n\t\tthis.code = code\n\t\tif (context !== undefined) this.context = context\n\t}\n}\n\n/**\n * Narrow an unknown caught value to a {@link DatabaseError}.\n *\n * @param value - The value to test (typically a `catch` binding)\n * @returns `true` when `value` is a {@link DatabaseError}\n *\n * @example\n * ```ts\n * try {\n * \tawait users.add(row)\n * } catch (error) {\n * \tif (isDatabaseError(error) && error.code === 'CONFLICT') await users.set(row)\n * }\n * ```\n */\nexport function isDatabaseError(value: unknown): value is DatabaseError {\n\treturn value instanceof DatabaseError\n}\n","import type {\n\tColumnSchema,\n\tDriverMetadata,\n\tKey,\n\tMigration,\n\tMigrationInput,\n\tMigrationStep,\n\tQueryInput,\n\tTableSchema,\n} from './types.js'\nimport { cloneJSONRecord, cloneJSONValue } from '@orkestrel/contract'\nimport { DatabaseError } from './errors.js'\n\n/**\n * Validate the paging fields of a portable query.\n *\n * @remarks\n * A present `limit` or `offset` must be a finite nonnegative integer; zero is\n * valid. Validation is deterministic (`limit` before `offset`). Non-finite\n * values are rendered as strings in error context so JSON serialization cannot\n * collapse `NaN` or infinity to `null`.\n *\n * @param input - The portable query whose paging fields to validate\n * @throws {@link DatabaseError} `VALIDATION` when a paging field is invalid\n */\nexport function validatePage(input?: QueryInput): void {\n\tconst limit = input?.limit\n\tif (limit !== undefined && (!Number.isInteger(limit) || limit < 0)) {\n\t\tthrow new DatabaseError('VALIDATION', 'Query limit must be a nonnegative integer', {\n\t\t\tfield: 'limit',\n\t\t\tvalue: Number.isFinite(limit) ? limit : String(limit),\n\t\t})\n\t}\n\tconst offset = input?.offset\n\tif (offset !== undefined && (!Number.isInteger(offset) || offset < 0)) {\n\t\tthrow new DatabaseError('VALIDATION', 'Query offset must be a nonnegative integer', {\n\t\t\tfield: 'offset',\n\t\t\tvalue: Number.isFinite(offset) ? offset : String(offset),\n\t\t})\n\t}\n}\n\n/**\n * Test whether a value is a usable database key.\n *\n * @param value - The value to test\n * @returns Whether `value` is a string or finite number\n */\nexport function isKey(value: unknown): value is Key {\n\treturn typeof value === 'string' || (typeof value === 'number' && Number.isFinite(value))\n}\n\n/**\n * Test whether a value is a portable column schema.\n *\n * @param value - The value to test\n * @returns Whether `value` is a complete {@link ColumnSchema}\n */\nexport function isColumnSchema(value: unknown): value is ColumnSchema {\n\ttry {\n\t\tconst column = cloneJSONRecord(value)\n\t\tconst keys = Object.keys(column)\n\t\treturn (\n\t\t\tkeys.length === 4 &&\n\t\t\tkeys.includes('name') &&\n\t\t\tkeys.includes('storage') &&\n\t\t\tkeys.includes('optional') &&\n\t\t\tkeys.includes('nullable') &&\n\t\t\ttypeof column.name === 'string' &&\n\t\t\tcolumn.name.length > 0 &&\n\t\t\t(column.storage === 'text' ||\n\t\t\t\tcolumn.storage === 'integer' ||\n\t\t\t\tcolumn.storage === 'real' ||\n\t\t\t\tcolumn.storage === 'boolean' ||\n\t\t\t\tcolumn.storage === 'json' ||\n\t\t\t\tcolumn.storage === 'blob') &&\n\t\t\ttypeof column.optional === 'boolean' &&\n\t\t\ttypeof column.nullable === 'boolean'\n\t\t)\n\t} catch {\n\t\treturn false\n\t}\n}\n\n/**\n * Test whether a value is a portable table schema.\n *\n * @param value - The value to test\n * @returns Whether `value` is a complete {@link TableSchema}\n */\nexport function isTableSchema(value: unknown): value is TableSchema {\n\ttry {\n\t\tconst table = cloneJSONRecord(value)\n\t\tconst keys = Object.keys(table)\n\t\tif (\n\t\t\tkeys.length !== 4 ||\n\t\t\t!keys.includes('name') ||\n\t\t\t!keys.includes('primary') ||\n\t\t\t!keys.includes('columns') ||\n\t\t\t!keys.includes('indexes') ||\n\t\t\ttypeof table.name !== 'string' ||\n\t\t\ttable.name.length === 0 ||\n\t\t\ttypeof table.primary !== 'string' ||\n\t\t\ttable.primary.length === 0 ||\n\t\t\t!Array.isArray(table.columns) ||\n\t\t\t!Array.isArray(table.indexes) ||\n\t\t\t!table.columns.every(isColumnSchema)\n\t\t) {\n\t\t\treturn false\n\t\t}\n\t\tconst names = table.columns.map((column) => column.name)\n\t\tif (\n\t\t\tnew Set(names).size !== names.length ||\n\t\t\t!names.includes(table.primary) ||\n\t\t\t!table.indexes.every(\n\t\t\t\t(index) =>\n\t\t\t\t\tArray.isArray(index) &&\n\t\t\t\t\tindex.length > 0 &&\n\t\t\t\t\tindex.every((column) => typeof column === 'string' && names.includes(column)),\n\t\t\t)\n\t\t) {\n\t\t\treturn false\n\t\t}\n\t\tconst indexes = table.indexes.map((index) => JSON.stringify(index))\n\t\treturn new Set(indexes).size === indexes.length\n\t} catch {\n\t\treturn false\n\t}\n}\n\n/**\n * Test whether a value is a complete portable driver schema.\n *\n * @param value - The value to test\n * @returns Whether `value` is a table-schema collection with unique table names\n */\nexport function isDriverSchema(value: unknown): value is readonly TableSchema[] {\n\ttry {\n\t\tconst schema = cloneJSONValue(value)\n\t\tif (!Array.isArray(schema) || !schema.every(isTableSchema)) return false\n\t\tconst names = schema.map((table) => table.name)\n\t\treturn new Set(names).size === names.length\n\t} catch {\n\t\treturn false\n\t}\n}\n\n/**\n * Test whether a value is one ordered migration step.\n *\n * @param value - The value to test\n * @returns Whether `value` is a complete {@link MigrationStep}\n */\nexport function isMigrationStep(value: unknown): value is MigrationStep {\n\ttry {\n\t\tconst step = cloneJSONRecord(value)\n\t\tif (typeof step.operation !== 'string') return false\n\t\tconst keys = Object.keys(step)\n\t\tswitch (step.operation) {\n\t\t\tcase 'table.add':\n\t\t\t\treturn (\n\t\t\t\t\tkeys.length === 2 &&\n\t\t\t\t\tkeys.includes('operation') &&\n\t\t\t\t\tkeys.includes('table') &&\n\t\t\t\t\tisTableSchema(step.table)\n\t\t\t\t)\n\t\t\tcase 'table.remove':\n\t\t\t\treturn (\n\t\t\t\t\tkeys.length === 2 &&\n\t\t\t\t\tkeys.includes('operation') &&\n\t\t\t\t\tkeys.includes('table') &&\n\t\t\t\t\ttypeof step.table === 'string' &&\n\t\t\t\t\tstep.table.length > 0\n\t\t\t\t)\n\t\t\tcase 'column.add':\n\t\t\t\treturn (\n\t\t\t\t\tkeys.length === 3 &&\n\t\t\t\t\tkeys.includes('operation') &&\n\t\t\t\t\tkeys.includes('table') &&\n\t\t\t\t\tkeys.includes('column') &&\n\t\t\t\t\ttypeof step.table === 'string' &&\n\t\t\t\t\tstep.table.length > 0 &&\n\t\t\t\t\tisColumnSchema(step.column)\n\t\t\t\t)\n\t\t\tcase 'column.remove':\n\t\t\t\treturn (\n\t\t\t\t\tkeys.length === 3 &&\n\t\t\t\t\tkeys.includes('operation') &&\n\t\t\t\t\tkeys.includes('table') &&\n\t\t\t\t\tkeys.includes('column') &&\n\t\t\t\t\ttypeof step.table === 'string' &&\n\t\t\t\t\tstep.table.length > 0 &&\n\t\t\t\t\ttypeof step.column === 'string' &&\n\t\t\t\t\tstep.column.length > 0\n\t\t\t\t)\n\t\t\tcase 'index.add':\n\t\t\tcase 'index.remove':\n\t\t\t\treturn (\n\t\t\t\t\tkeys.length === 3 &&\n\t\t\t\t\tkeys.includes('operation') &&\n\t\t\t\t\tkeys.includes('table') &&\n\t\t\t\t\tkeys.includes('index') &&\n\t\t\t\t\ttypeof step.table === 'string' &&\n\t\t\t\t\tstep.table.length > 0 &&\n\t\t\t\t\tArray.isArray(step.index) &&\n\t\t\t\t\tstep.index.length > 0 &&\n\t\t\t\t\tstep.index.every((column) => typeof column === 'string' && column.length > 0)\n\t\t\t\t)\n\t\t\tdefault:\n\t\t\t\treturn false\n\t\t}\n\t} catch {\n\t\treturn false\n\t}\n}\n\n/**\n * Test whether a value is an ordered migration plan.\n *\n * @param value - The value to test\n * @returns Whether `value` is a complete {@link Migration}\n */\nexport function isMigration(value: unknown): value is Migration {\n\ttry {\n\t\tconst migration = cloneJSONRecord(value)\n\t\tconst keys = Object.keys(migration)\n\t\treturn (\n\t\t\tkeys.length === 3 &&\n\t\t\tkeys.includes('from') &&\n\t\t\tkeys.includes('to') &&\n\t\t\tkeys.includes('steps') &&\n\t\t\ttypeof migration.from === 'number' &&\n\t\t\tNumber.isFinite(migration.from) &&\n\t\t\ttypeof migration.to === 'number' &&\n\t\t\tNumber.isFinite(migration.to) &&\n\t\t\tArray.isArray(migration.steps) &&\n\t\t\tmigration.steps.every(isMigrationStep)\n\t\t)\n\t} catch {\n\t\treturn false\n\t}\n}\n\n/**\n * Test whether a value is persisted driver metadata.\n *\n * @param value - The value to test\n * @returns Whether `value` is complete {@link DriverMetadata}\n */\nexport function isDriverMetadata(value: unknown): value is DriverMetadata {\n\ttry {\n\t\tconst metadata = cloneJSONRecord(value)\n\t\tconst keys = Object.keys(metadata)\n\t\treturn (\n\t\t\tkeys.length === 2 &&\n\t\t\tkeys.includes('version') &&\n\t\t\tkeys.includes('schema') &&\n\t\t\ttypeof metadata.version === 'number' &&\n\t\t\tNumber.isFinite(metadata.version) &&\n\t\t\tisDriverSchema(metadata.schema)\n\t\t)\n\t} catch {\n\t\treturn false\n\t}\n}\n\n/**\n * Test whether a value is one atomic migration request.\n *\n * @param value - The value to test\n * @returns Whether `value` is a complete {@link MigrationInput}\n */\nexport function isMigrationInput(value: unknown): value is MigrationInput {\n\ttry {\n\t\tconst input = cloneJSONRecord(value)\n\t\tconst keys = Object.keys(input)\n\t\treturn (\n\t\t\t(keys.length === 1 || keys.length === 2) &&\n\t\t\tkeys.includes('plan') &&\n\t\t\t(keys.length === 1 || keys.includes('metadata')) &&\n\t\t\tisMigration(input.plan) &&\n\t\t\t(input.metadata === undefined || isDriverMetadata(input.metadata))\n\t\t)\n\t} catch {\n\t\treturn false\n\t}\n}\n","import type { DriverMetadata, MigrationInput, TableSchema } from './types.js'\nimport { cloneJSONRecord, cloneJSONValue } from '@orkestrel/contract'\nimport { DatabaseError } from './errors.js'\nimport { isDriverMetadata, isDriverSchema, isMigrationInput } from './validators.js'\n\n/**\n * Clone unknown driver metadata into a distinct deeply frozen snapshot.\n *\n * @param value - Unknown metadata\n * @returns Owned driver metadata\n */\nexport function cloneDriverMetadata(value: unknown): DriverMetadata {\n\ttry {\n\t\tconst metadata = cloneJSONRecord(value)\n\t\tif (isDriverMetadata(metadata)) return metadata\n\t\tthrow new DatabaseError('VALIDATION', 'Driver metadata is invalid', {\n\t\t\tpath: 'metadata',\n\t\t})\n\t} catch (error) {\n\t\tif (error instanceof DatabaseError) throw error\n\t\tthrow new DatabaseError('VALIDATION', 'Driver metadata is invalid', {\n\t\t\tpath: 'metadata',\n\t\t\tcause: error,\n\t\t})\n\t}\n}\n\n/**\n * Clone unknown driver schema into a distinct deeply frozen snapshot.\n *\n * @param value - Unknown table schema collection\n * @returns Owned driver schema\n */\nexport function cloneDriverSchema(value: unknown): readonly TableSchema[] {\n\ttry {\n\t\tconst schema = cloneJSONValue(value)\n\t\tif (isDriverSchema(schema)) return schema\n\t\tthrow new DatabaseError('VALIDATION', 'Driver schema is invalid', {\n\t\t\tpath: 'schema',\n\t\t})\n\t} catch (error) {\n\t\tif (error instanceof DatabaseError) throw error\n\t\tthrow new DatabaseError('VALIDATION', 'Driver schema is invalid', {\n\t\t\tpath: 'schema',\n\t\t\tcause: error,\n\t\t})\n\t}\n}\n\n/**\n * Clone unknown migration input into a distinct deeply frozen snapshot.\n *\n * @param value - Unknown migration input\n * @returns Owned migration input\n */\nexport function cloneMigrationInput(value: unknown): MigrationInput {\n\ttry {\n\t\tconst input = cloneJSONRecord(value)\n\t\tif (isMigrationInput(input)) return input\n\t\tthrow new DatabaseError('VALIDATION', 'Migration input is invalid', {\n\t\t\tpath: 'migration',\n\t\t})\n\t} catch (error) {\n\t\tif (error instanceof DatabaseError) throw error\n\t\tthrow new DatabaseError('VALIDATION', 'Migration input is invalid', {\n\t\t\tpath: 'migration',\n\t\t\tcause: error,\n\t\t})\n\t}\n}\n","import type { ContractShape, FieldPath } from '@orkestrel/contract'\nimport type {\n\tAggregateOperation,\n\tColumnSchema,\n\tColumnStorage,\n\tConformanceFinding,\n\tCondition,\n\tQueryInput,\n\tDriverInterface,\n\tKey,\n\tMigration,\n\tMigrationStep,\n\tOrder,\n\tRow,\n\tTableSchema,\n} from './types.js'\nimport {\n\tcompileGuard,\n\tisRecord,\n\tisString,\n\tobjectShape,\n\tparseNumber,\n\tresolveField,\n} from '@orkestrel/contract'\nimport { MAX_PATTERN_LENGTH } from './constants.js'\nimport { cloneDriverSchema, cloneMigrationInput } from './cloners.js'\nimport { DatabaseError, isDatabaseError } from './errors.js'\nimport { isKey, validatePage } from './validators.js'\n\n// The query engine. Every backend's `scan` yields rows; these pure helpers do\n// the filtering, ordering, paging, and aggregation once, so a driver never\n// re-implements WHERE compilation. They are total — like the contracts guards\n// they lean on, hostile input yields a `false` / a skipped value, never a throw.\n\n// === Comparison\n\n/**\n * A total ordering over arbitrary values — the comparator behind sorting and the\n * range operators.\n *\n * @remarks\n * Values of different types order by a fixed type rank (`undefined` < `null` <\n * boolean < number < string < other); same-typed values compare naturally.\n * `NaN` sorts after every other number and equal to itself, so the comparator\n * is total and never returns `NaN`.\n *\n * @param left - The left value\n * @param right - The right value\n * @returns `-1`, `0`, or `1`\n */\nexport function compareValues(left: unknown, right: unknown): number {\n\t// Rank unlike types so a mixed column still sorts deterministically:\n\t// undefined < null < boolean < number < string < other.\n\t// Mapping two inputs always produces two ranks; the defaults only satisfy\n\t// unchecked indexed destructuring and are semantically unreachable.\n\tconst [leftRank = 5, rightRank = 5] = [left, right].map((value) =>\n\t\tvalue === undefined\n\t\t\t? 0\n\t\t\t: value === null\n\t\t\t\t? 1\n\t\t\t\t: typeof value === 'boolean'\n\t\t\t\t\t? 2\n\t\t\t\t\t: typeof value === 'number'\n\t\t\t\t\t\t? 3\n\t\t\t\t\t\t: typeof value === 'string'\n\t\t\t\t\t\t\t? 4\n\t\t\t\t\t\t\t: 5,\n\t)\n\tif (leftRank !== rightRank) return leftRank < rightRank ? -1 : 1\n\tif (typeof left === 'number' && typeof right === 'number') {\n\t\tif (Number.isNaN(left) || Number.isNaN(right)) {\n\t\t\treturn Number.isNaN(left) ? (Number.isNaN(right) ? 0 : 1) : -1\n\t\t}\n\t\treturn left < right ? -1 : left > right ? 1 : 0\n\t}\n\tif (typeof left === 'string' && typeof right === 'string') {\n\t\treturn left < right ? -1 : left > right ? 1 : 0\n\t}\n\tif (typeof left === 'boolean' && typeof right === 'boolean') {\n\t\treturn left === right ? 0 : left ? 1 : -1\n\t}\n\treturn 0\n}\n\n/**\n * Structural equality by SameValueZero leaves — the comparator behind conformance\n * checks and any test/fixture that needs \"same data\", not \"same reference\".\n *\n * @remarks\n * Primitives compare by SameValueZero (`NaN` equals itself; `+0` equals `-0`).\n * Arrays compare by index (same length, every element `equalsValue`). Plain\n * records (via `isRecord`) compare by their OWN enumerable keys: same key\n * COUNT and, for every key in `left`, `right` has that key (`Object.hasOwn`)\n * with a `equalsValue` value — so a key present with value `undefined` is NOT\n * equal to that key being absent (both differ in `Object.keys` membership).\n * Anything else (functions, class instances, mismatched shapes) falls through\n * to `false`. Container pairs are tracked iteratively, so self-referential and\n * mutually cyclic arrays/records terminate without consuming the call stack.\n * Hostile proxy traps and accessors are contained as a non-match.\n *\n * @param left - The left value\n * @param right - The right value\n * @returns Whether `left` and `right` are structurally equal\n *\n * @example\n * ```ts\n * equalsValue(Number.NaN, Number.NaN) // true\n * equalsValue({ a: [1, { b: 2 }] }, { a: [1, { b: 2 }] }) // true\n * equalsValue({ a: undefined }, {}) // false — present-undefined ≠ absent\n * ```\n */\nexport function equalsValue(left: unknown, right: unknown): boolean {\n\tconst pending: Array<readonly [unknown, unknown]> = [[left, right]]\n\tconst compared = new WeakMap<object, WeakSet<object>>()\n\ttry {\n\t\twhile (pending.length > 0) {\n\t\t\tconst pair = pending.pop()\n\t\t\tif (pair === undefined) continue\n\t\t\tconst [currentLeft, currentRight] = pair\n\t\t\tif (typeof currentLeft === 'number' && typeof currentRight === 'number') {\n\t\t\t\tif (\n\t\t\t\t\t(Number.isNaN(currentLeft) && Number.isNaN(currentRight)) ||\n\t\t\t\t\tcurrentLeft === currentRight\n\t\t\t\t) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\treturn false\n\t\t\t}\n\t\t\tif (currentLeft === currentRight) continue\n\n\t\t\tconst leftArray = Array.isArray(currentLeft)\n\t\t\tconst rightArray = Array.isArray(currentRight)\n\t\t\tconst leftRecord = isRecord(currentLeft)\n\t\t\tconst rightRecord = isRecord(currentRight)\n\t\t\tif (leftArray !== rightArray || leftRecord !== rightRecord) return false\n\t\t\tif ((!leftArray && !leftRecord) || (!rightArray && !rightRecord)) return false\n\n\t\t\tconst prior = compared.get(currentLeft)\n\t\t\tif (prior?.has(currentRight)) continue\n\t\t\tif (prior === undefined) {\n\t\t\t\tcompared.set(currentLeft, new WeakSet([currentRight]))\n\t\t\t} else {\n\t\t\t\tprior.add(currentRight)\n\t\t\t}\n\n\t\t\tif (leftArray && rightArray) {\n\t\t\t\tif (currentLeft.length !== currentRight.length) return false\n\t\t\t\tfor (let index = 0; index < currentLeft.length; index += 1) {\n\t\t\t\t\tconst leftOwn = Object.hasOwn(currentLeft, index)\n\t\t\t\t\tconst rightOwn = Object.hasOwn(currentRight, index)\n\t\t\t\t\tif (leftOwn !== rightOwn) return false\n\t\t\t\t\tif (leftOwn) pending.push([currentLeft[index], currentRight[index]])\n\t\t\t\t}\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif (leftRecord && rightRecord) {\n\t\t\t\tconst leftKeys = Object.keys(currentLeft)\n\t\t\t\tconst rightKeys = Object.keys(currentRight)\n\t\t\t\tif (leftKeys.length !== rightKeys.length) return false\n\t\t\t\tfor (const key of leftKeys) {\n\t\t\t\t\tif (!Object.hasOwn(currentRight, key)) return false\n\t\t\t\t\tpending.push([currentLeft[key], currentRight[key]])\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn true\n\t} catch {\n\t\treturn false\n\t}\n}\n\n// === Pattern matching\n\n/**\n * Match a query against a value as a case-insensitive ordered subsequence.\n *\n * @remarks\n * Every query character must appear in order in the value, but the characters\n * do not need to be contiguous. Query characters are literal, including\n * whitespace. Matching applies JavaScript `toLowerCase()` to both inputs\n * without locale-specific folding or Unicode normalization. An empty query\n * matches every value.\n *\n * @param value - The text searched for the query's characters\n * @param query - The characters that must all appear in order\n * @returns Whether the case-folded query is a subsequence of the case-folded value\n *\n * @example\n * ```ts\n * matchesFuzzy('Database', 'dbe') // true\n * matchesFuzzy('Database', 'abd') // false\n * ```\n */\nexport function matchesFuzzy(value: string, query: string): boolean {\n\tconst folded = value.toLowerCase()\n\tconst wanted = query.toLowerCase()\n\tlet cursor = 0\n\tfor (const char of wanted) {\n\t\tconst found = folded.indexOf(char, cursor)\n\t\tif (found === -1) return false\n\t\tcursor = found + 1\n\t}\n\treturn true\n}\n\n/**\n * Match a value against a wildcard pattern in LINEAR time — the shared, ReDoS-SAFE\n * engine behind {@link matchesLikePattern} and {@link matchesGlobPattern}.\n *\n * @remarks\n * A backtracking RegExp (`a%b%c` → `^a.*b.*c$`) is CATASTROPHIC on a hostile pattern:\n * `.*` segments separated by literals, matched against a long non-matching input, blow\n * up super-linearly — and JS has no atomic groups / possessive quantifiers to bound it\n * (AGENTS §6.5, now that the authed server runs model-supplied `list` input over the\n * wire). So this builds NO regex. It runs the classic GREEDY TWO-POINTER wildcard match:\n * the `any` wildcard records its position and, on a later mismatch, backtracks ONLY to\n * that last `any` (letting it absorb one more char) — so the work is O(value × pattern),\n * never the exponential / polynomial backtracking a regex would do. The pattern length\n * is capped at {@link MAX_PATTERN_LENGTH} (a `VALIDATION` {@link DatabaseError} over it),\n * bounding the pattern factor so a match stays linear in the value length whatever the\n * pattern.\n *\n * The `any` wildcard matches any run (including empty); `single` matches exactly one\n * char; every other pattern char matches itself LITERALLY (a pattern `.` / `(` / `\\` is\n * a literal — the regex-metacharacter hazard is gone with the regex). `any` is tested\n * BEFORE a literal match, so a value that literally contains the wildcard char never\n * shadows the wildcard. Case folding is applied to BOTH sides when `fold` is set.\n *\n * @param value - The value to test\n * @param pattern - The wildcard pattern\n * @param any - The any-run wildcard char (`%` for `LIKE`, `*` for `GLOB`)\n * @param single - The single-char wildcard char (`_` for `LIKE`, `?` for `GLOB`)\n * @param fold - Whether to match case-INSENSITIVELY (`LIKE` folds; `GLOB` does not)\n * @returns Whether `value` matches `pattern`\n * @throws A `VALIDATION` {@link DatabaseError} when `pattern` exceeds {@link MAX_PATTERN_LENGTH}\n */\nexport function matchesWildcardPattern(\n\tvalue: string,\n\tpattern: string,\n\tany: string,\n\tsingle: string,\n\tfold: boolean,\n): boolean {\n\tif (pattern.length > MAX_PATTERN_LENGTH) {\n\t\tthrow new DatabaseError(\n\t\t\t'VALIDATION',\n\t\t\t`Pattern exceeds the maximum length of ${MAX_PATTERN_LENGTH}`,\n\t\t\t{ length: pattern.length, limit: MAX_PATTERN_LENGTH },\n\t\t)\n\t}\n\tconst haystack = fold ? value.toLowerCase() : value\n\tconst needle = fold ? pattern.toLowerCase() : pattern\n\tlet vi = 0\n\tlet pi = 0\n\t// The greedy backtrack point: the pattern index of the LAST `any` wildcard + the value\n\t// index it was taken at. On a mismatch we resume just past it and let it absorb one more\n\t// char (`mark += 1`) — O(value × pattern), never a regex's exponential backtracking.\n\tlet star = -1\n\tlet mark = 0\n\twhile (vi < haystack.length) {\n\t\tconst pc = pi < needle.length ? needle[pi] : undefined\n\t\tif (pc === any) {\n\t\t\t// Record the wildcard (it absorbs zero chars for now) and advance the pattern.\n\t\t\tstar = pi\n\t\t\tmark = vi\n\t\t\tpi += 1\n\t\t} else if (pc !== undefined && (pc === single || pc === haystack[vi])) {\n\t\t\tvi += 1\n\t\t\tpi += 1\n\t\t} else if (star !== -1) {\n\t\t\t// Mismatch under an open `any`: let it swallow one more value char and retry.\n\t\t\tpi = star + 1\n\t\t\tmark += 1\n\t\t\tvi = mark\n\t\t} else {\n\t\t\treturn false\n\t\t}\n\t}\n\t// The value is consumed — the leftover pattern matches iff it is all `any` wildcards.\n\twhile (pi < needle.length && needle[pi] === any) pi += 1\n\treturn pi === needle.length\n}\n\n// SQL `LIKE` → case-INSENSITIVE wildcard match (`%` any run, `_` any char).\nexport function matchesLikePattern(value: string, pattern: string): boolean {\n\treturn matchesWildcardPattern(value, pattern, '%', '_', true)\n}\n\n// `GLOB` → case-SENSITIVE wildcard match (`*` any run, `?` any char).\nexport function matchesGlobPattern(value: string, pattern: string): boolean {\n\treturn matchesWildcardPattern(value, pattern, '*', '?', false)\n}\n\n// === Condition matching\n\n/**\n * Evaluate one {@link Condition} against a row — the per-operator predicate.\n *\n * @remarks\n * Reads the condition's column — a `FieldPath`, resolved with `resolveField` (a\n * string is one column; an array descends a nested value) — and applies the\n * operator. Range operators (`above` / `below` / `from` / `to` / `between`) use\n * {@link compareValues}, the total order; the equality family (`equals` / `not`\n * / `any` / `none`) uses {@link equalsValue} — STRUCTURAL equality, not the total\n * order's rank-5-collapses-all-objects behavior, so `equals` on an object/array\n * operand only matches a structurally-equal value, never every row holding any\n * object. This is a semantics change from ranking: `equalsValue` is SameValueZero\n * on leaves, so `NaN` now equals `NaN` under `equals` / `any` (it never matched\n * anything under the old rank-based comparison). `like` / `glob` / `starts` /\n * `ends` match only strings; `absent` / `present` test nullishness. Total — a\n * type mismatch is simply a non-match.\n *\n * @param row - The row to test\n * @param condition - The condition to apply\n * @returns Whether the row satisfies the condition\n */\nexport function matchesCondition(row: Row, condition: Condition): boolean {\n\tconst value = resolveField(row, condition.column)\n\tconst first = condition.values[0]\n\tconst second = condition.values[1]\n\tswitch (condition.operator) {\n\t\tcase 'equals':\n\t\t\treturn equalsValue(value, first)\n\t\tcase 'not':\n\t\t\treturn !equalsValue(value, first)\n\t\tcase 'above':\n\t\t\treturn compareValues(value, first) > 0\n\t\tcase 'below':\n\t\t\treturn compareValues(value, first) < 0\n\t\tcase 'from':\n\t\t\treturn compareValues(value, first) >= 0\n\t\tcase 'to':\n\t\t\treturn compareValues(value, first) <= 0\n\t\tcase 'between':\n\t\t\treturn compareValues(value, first) >= 0 && compareValues(value, second) <= 0\n\t\tcase 'like':\n\t\t\treturn isString(value) && isString(first) && matchesLikePattern(value, first)\n\t\tcase 'glob':\n\t\t\treturn isString(value) && isString(first) && matchesGlobPattern(value, first)\n\t\tcase 'starts':\n\t\t\treturn isString(value) && isString(first) && value.startsWith(first)\n\t\tcase 'ends':\n\t\t\treturn isString(value) && isString(first) && value.endsWith(first)\n\t\tcase 'any':\n\t\t\treturn condition.values.some((candidate) => equalsValue(value, candidate))\n\t\tcase 'none':\n\t\t\treturn !condition.values.some((candidate) => equalsValue(value, candidate))\n\t\tcase 'absent':\n\t\t\treturn value === undefined || value === null\n\t\tcase 'present':\n\t\t\treturn value !== undefined && value !== null\n\t}\n}\n\n/**\n * Fold a row through a list of conditions, joining each by its connector.\n *\n * @remarks\n * Evaluated left-to-right: the first condition seeds the result, and each later\n * condition combines with `&&` (`and`) or `||` (`or`). An empty list matches\n * every row. There is no operator precedence — conditions combine in the order\n * the query builder recorded them.\n *\n * @param row - The row to test\n * @param conditions - The conditions to fold\n * @returns Whether the row satisfies the combined conditions\n */\nexport function matchesQuery(row: Row, conditions: readonly Condition[]): boolean {\n\tlet result = true\n\tlet seeded = false\n\tfor (const condition of conditions) {\n\t\tconst match = matchesCondition(row, condition)\n\t\tif (!seeded) {\n\t\t\tresult = match\n\t\t\tseeded = true\n\t\t} else {\n\t\t\tresult = condition.connector === 'or' ? result || match : result && match\n\t\t}\n\t}\n\treturn result\n}\n\n/**\n * Filter rows by a list of conditions — the shared basis for a table's count\n * and aggregate paths (no sort/page, unlike {@link applyQuery}).\n *\n * @remarks\n * An empty condition list matches every row (returned as-is, no copy). Folds\n * each row through {@link matchesQuery}.\n *\n * @param rows - The rows to filter\n * @param conditions - The conditions to apply (empty matches everything)\n * @returns The matching rows\n *\n * @example\n * ```ts\n * filterRows(\n * \t[{ age: 30 }, { age: 12 }],\n * \t[{ column: 'age', operator: 'above', values: [18], connector: 'and' }],\n * ) // => [{ age: 30 }]\n * ```\n */\nexport function filterRows(rows: readonly Row[], conditions: readonly Condition[]): readonly Row[] {\n\tif (conditions.length === 0) return rows\n\treturn rows.filter((row) => matchesQuery(row, conditions))\n}\n\n// === Ordering & paging\n\n/**\n * Sort rows by an ordering specification, leaving the input untouched.\n *\n * @remarks\n * Applies the terms in priority order — the first term that distinguishes two\n * rows decides — using {@link compareValues}, reversing for `descending`.\n *\n * @param rows - The rows to sort\n * @param order - The ordering terms in priority order\n * @returns A new, sorted array\n */\nexport function sortRows(rows: readonly Row[], order: readonly Order[]): readonly Row[] {\n\tconst sorted = [...rows]\n\tsorted.sort((left, right) => {\n\t\tfor (const term of order) {\n\t\t\tconst comparison = compareValues(\n\t\t\t\tresolveField(left, term.column),\n\t\t\t\tresolveField(right, term.column),\n\t\t\t)\n\t\t\tif (comparison !== 0) return term.direction === 'descending' ? -comparison : comparison\n\t\t}\n\t\treturn 0\n\t})\n\treturn sorted\n}\n\n/**\n * Apply a {@link QueryInput} to rows — filter, then sort, then page.\n *\n * @remarks\n * The whole portable read pipeline in one place: conditions filter, `order`\n * sorts, and `offset` / `limit` window the result. Each step is skipped when its\n * part of the input is absent. The reference {@link DriverInterface} backends\n * lean on this rather than each re-deriving it.\n *\n * @param rows - The rows to process (typically a table's full `scan`)\n * @param input - The read specification, or `undefined` for all rows as-is\n * @returns The filtered, sorted, paged rows\n */\nexport function applyQuery(rows: readonly Row[], input?: QueryInput): readonly Row[] {\n\tvalidatePage(input)\n\tlet result = rows\n\tconst conditions = input?.conditions\n\tif (conditions !== undefined && conditions.length > 0) {\n\t\tresult = result.filter((row) => matchesQuery(row, conditions))\n\t}\n\tconst order = input?.order\n\tif (order !== undefined && order.length > 0) {\n\t\tresult = sortRows(result, order)\n\t}\n\tconst offset = input?.offset ?? 0\n\tconst limit = input?.limit\n\tif (offset > 0 || limit !== undefined) {\n\t\tresult = result.slice(offset, limit !== undefined ? offset + limit : undefined)\n\t}\n\treturn result\n}\n\n// === Aggregation\n\n/**\n * Compute an aggregate over a column across rows.\n *\n * @remarks\n * `count` returns the row count. The numeric aggregates coerce each cell with\n * the contracts `parseNumber` (so `'42'` counts) and ignore non-numeric cells;\n * over zero numeric values they return `undefined` — the SQL `NULL` of an empty\n * aggregate.\n *\n * @param rows - The rows to aggregate (non-record entries are ignored)\n * @param operation - The aggregate to compute\n * @param column - The column to aggregate\n * @returns The aggregate value, or `undefined` when undefined for the inputs\n */\nexport function computeAggregate(\n\trows: readonly unknown[],\n\toperation: AggregateOperation,\n\tcolumn: FieldPath,\n): number | undefined {\n\tif (operation === 'count') return rows.length\n\tconst numbers: number[] = []\n\tfor (const row of rows) {\n\t\tif (!isRecord(row)) continue\n\t\tconst value = parseNumber(resolveField(row, column))\n\t\tif (value !== undefined) numbers.push(value)\n\t}\n\tif (numbers.length === 0) return undefined\n\tif (operation === 'sum' || operation === 'average') {\n\t\tconst total = numbers.reduce((sum, value) => sum + value, 0)\n\t\treturn operation === 'average' ? total / numbers.length : total\n\t}\n\treturn operation === 'minimum' ? Math.min(...numbers) : Math.max(...numbers)\n}\n\n// === Keys\n\n/**\n * Read a row's primary key from a column, when it is a usable {@link Key}.\n *\n * @param row - The row to read\n * @param column - The primary-key column name\n * @returns The key (a string or finite number), or `undefined`\n */\nexport function extractKey(row: Row, column: string): Key | undefined {\n\tconst value = row[column]\n\treturn isKey(value) ? value : undefined\n}\n\n/**\n * Return a fresh row whose primary column is authoritatively bound to its storage key.\n *\n * @param row - The caller row\n * @param primary - The primary column\n * @param key - The authoritative storage key\n * @returns A fresh row with the bound primary\n */\nexport function bindRowKey(row: Row, primary: string, key: Key): Row {\n\treturn { ...row, [primary]: key }\n}\n\n// === Schema\n\n/**\n * Map a column's {@link ContractShape} to its portable {@link ColumnStorage} — the\n * value a `TableSchema` carries so a native backend can declare a real column.\n *\n * @remarks\n * `string` → `text`; `number` → `integer` when the shape is integer-only, else\n * `real`; `boolean` → `boolean`. A `literal` takes the type of its values\n * (all-boolean → `boolean`, all-integer → `integer`, mixed/fractional numbers →\n * `real`, anything else → `text`). `optional` / `nullable` unwrap to their inner\n * type (nullability is tracked separately). `null` / `object` / `array` / `union` /\n * `json` / `raw` → `json`: a backend stores them as JSON text and can `json_extract`\n * for nested `FieldPath` queries. A scan-only backend ignores the result.\n *\n * @param shape - The column's contract shape\n * @returns The portable column type\n *\n * @example\n * ```ts\n * shapeToColumnStorage(stringShape()) // 'text'\n * shapeToColumnStorage(integerShape()) // 'integer'\n * shapeToColumnStorage(optionalShape(integerShape())) // 'integer'\n * shapeToColumnStorage(objectShape({ a: stringShape() })) // 'json'\n * ```\n */\nexport function shapeToColumnStorage(shape: ContractShape): ColumnStorage {\n\tswitch (shape.type) {\n\t\tcase 'string':\n\t\t\treturn 'text'\n\t\tcase 'number':\n\t\t\treturn shape.integer === true ? 'integer' : 'real'\n\t\tcase 'boolean':\n\t\t\treturn 'boolean'\n\t\tcase 'literal': {\n\t\t\tif (shape.values.every((value) => typeof value === 'boolean')) return 'boolean'\n\t\t\tif (shape.values.every((value) => typeof value === 'number')) {\n\t\t\t\treturn shape.values.every((value) => Number.isInteger(value)) ? 'integer' : 'real'\n\t\t\t}\n\t\t\treturn 'text'\n\t\t}\n\t\tcase 'optional':\n\t\tcase 'nullable':\n\t\t\treturn shapeToColumnStorage(shape.inner)\n\t\tcase 'null':\n\t\tcase 'object':\n\t\tcase 'array':\n\t\tcase 'union':\n\t\tcase 'json':\n\t\tcase 'raw':\n\t\t\treturn 'json'\n\t}\n}\n\n/**\n * Project one contract shape into a portable column schema.\n *\n * @param name - The column name\n * @param shape - The column contract shape\n * @returns The portable storage and independent absence/null acceptance\n */\nexport function shapeToColumnSchema(name: string, shape: ContractShape): ColumnSchema {\n\tconst isColumn = compileGuard(objectShape({ value: shape }))\n\treturn {\n\t\tname,\n\t\tstorage: shapeToColumnStorage(shape),\n\t\toptional: isColumn({}),\n\t\tnullable: isColumn({ value: null }),\n\t}\n}\n\n// === Abort\n\n/**\n * Throw when an {@link OperationOptions.signal | AbortSignal} has fired — the shared\n * abort gate checked at operation boundaries and between streamed rows.\n *\n * @remarks\n * A no-op for `undefined` or a live signal, so callers thread `options?.signal`\n * straight through. When the signal has aborted, throws an `ABORTED`\n * {@link DatabaseError} carrying the signal's `reason` in its context — callers\n * mint signals with native APIs such as `AbortSignal.timeout(ms)` or\n * `new AbortController()`.\n *\n * @param signal - The signal to check, if any\n * @returns Nothing — returns normally while the signal is live\n * @throws An `ABORTED` {@link DatabaseError} when the signal has aborted\n *\n * @example\n * ```ts\n * import { checkAbort } from '@orkestrel/database'\n *\n * const controller = new AbortController()\n * checkAbort(controller.signal) // returns\n * controller.abort('too slow')\n * checkAbort(controller.signal) // throws DatabaseError('ABORTED', …)\n * ```\n */\nexport function checkAbort(signal: AbortSignal | undefined): void {\n\tif (signal?.aborted) {\n\t\tthrow new DatabaseError('ABORTED', 'Operation aborted', { reason: signal.reason })\n\t}\n}\n\n// === Migrations\n\n/**\n * Structurally diff a deployed and a declared table set into a {@link Migration}\n * plan.\n *\n * @remarks\n * Tables present in `declared` but not `deployed` become `table.add` steps\n * (carrying the full declared {@link TableSchema}); tables present in\n * `deployed` but not `declared` become `table.remove` steps. Tables present in\n * both are diffed column-by-column (by name) and index-group-by-index-group\n * (by deep equality of the column-name array), each producing `column.add` /\n * `column.remove` / `index.add` / `index.remove` steps. Step order is\n * deterministic: every `table.remove`, then every `table.add`, then each\n * shared table's column/index changes in `declared` order. `from` / `to` are\n * plan labels only; versioning drivers persist and reconcile them through\n * {@link DriverMetadata}.\n *\n * A column present in BOTH schemas under the same name but with a different\n * `storage`, `optional`, or `nullable` value throws a `MIGRATION`\n * {@link DatabaseError} naming the\n * table, the column, and the from→to difference — a name-only diff would\n * otherwise silently produce NO step for the drift, and versioned\n * reconciliation would stamp over it. There is no automatic in-place\n * type-change step: the manual path is to add a new column, copy/convert the\n * data at the application layer, then remove the old column — two separate\n * plans, never a single implicit \"alter\" step.\n *\n * @param deployed - The table schemas currently applied\n * @param declared - The table schemas the caller wants applied\n * @param from - The plan's source version label (defaults to `0`)\n * @param to - The plan's target version label (defaults to `1`)\n * @returns The migration plan moving `deployed` toward `declared`\n * @throws A `MIGRATION` {@link DatabaseError} when a shared table's primary or\n * a shared column's `storage`, `optional`, or `nullable` differs, or when a\n * required non-null column would be added to an existing table without a\n * portable backfill\n *\n * @example\n * ```ts\n * const plan = planMigration(\n * \t[{ name: 'users', primary: 'id', columns: [{ name: 'id', storage: 'text', optional: false, nullable: false }], indexes: [] }],\n * \t[{ name: 'users', primary: 'id', columns: [{ name: 'id', storage: 'text', optional: false, nullable: false }, { name: 'age', storage: 'integer', optional: true, nullable: false }], indexes: [] }],\n * )\n * // plan.steps === [{ operation: 'column.add', table: 'users', column: { name: 'age', ... } }]\n * ```\n */\nexport function planMigration(\n\tdeployed: readonly TableSchema[],\n\tdeclared: readonly TableSchema[],\n\tfrom = 0,\n\tto = 1,\n): Migration {\n\tif (!Number.isFinite(from) || !Number.isFinite(to)) {\n\t\tthrow new DatabaseError('MIGRATION', 'Migration versions must be finite', { from, to })\n\t}\n\tlet beforeSchema: readonly TableSchema[]\n\tlet targetSchema: readonly TableSchema[]\n\ttry {\n\t\tbeforeSchema = normalizeDriverSchema(deployed)\n\t\ttargetSchema = normalizeDriverSchema(declared)\n\t} catch (error) {\n\t\tthrow new DatabaseError('MIGRATION', 'Migration schema is invalid', { cause: error })\n\t}\n\tconst deployedByName = new Map(beforeSchema.map((table) => [table.name, table]))\n\tconst declaredByName = new Map(targetSchema.map((table) => [table.name, table]))\n\n\tconst steps: MigrationStep[] = []\n\n\tfor (const table of beforeSchema) {\n\t\tif (!declaredByName.has(table.name))\n\t\t\tsteps.push({ operation: 'table.remove', table: table.name })\n\t}\n\tfor (const table of targetSchema) {\n\t\tif (!deployedByName.has(table.name)) steps.push({ operation: 'table.add', table })\n\t}\n\n\tfor (const table of targetSchema) {\n\t\tconst before = deployedByName.get(table.name)\n\t\tif (before === undefined) continue\n\t\tif (before.primary !== table.primary) {\n\t\t\tthrow new DatabaseError(\n\t\t\t\t'MIGRATION',\n\t\t\t\t`planMigration: primary column on table '${table.name}' changed from '${before.primary}' to '${table.primary}'`,\n\t\t\t\t{ table: table.name, from: before.primary, to: table.primary },\n\t\t\t)\n\t\t}\n\n\t\tconst beforeColumnMap = new Map(before.columns.map((column) => [column.name, column]))\n\t\tconst afterColumnMap = new Map(table.columns.map((column) => [column.name, column]))\n\n\t\tfor (const index of before.indexes) {\n\t\t\tif (!table.indexes.some((candidate) => equalsValue(candidate, index))) {\n\t\t\t\tsteps.push({ operation: 'index.remove', table: table.name, index })\n\t\t\t}\n\t\t}\n\t\tfor (const column of before.columns) {\n\t\t\tif (!afterColumnMap.has(column.name)) {\n\t\t\t\tsteps.push({ operation: 'column.remove', table: table.name, column: column.name })\n\t\t\t}\n\t\t}\n\t\tfor (const column of table.columns) {\n\t\t\tconst previous = beforeColumnMap.get(column.name)\n\t\t\tif (previous === undefined) {\n\t\t\t\tsteps.push({ operation: 'column.add', table: table.name, column })\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif (\n\t\t\t\tprevious.storage !== column.storage ||\n\t\t\t\tprevious.optional !== column.optional ||\n\t\t\t\tprevious.nullable !== column.nullable\n\t\t\t) {\n\t\t\t\tthrow new DatabaseError(\n\t\t\t\t\t'MIGRATION',\n\t\t\t\t\t`planMigration: column '${column.name}' on table '${table.name}' changed shape ` +\n\t\t\t\t\t\t`(storage ${previous.storage}→${column.storage}, optional ${previous.optional}→${column.optional}, nullable ${previous.nullable}→${column.nullable}) — ` +\n\t\t\t\t\t\t`in-place storage/optionality/nullability changes are not auto-migrated; add a new column, copy/convert ` +\n\t\t\t\t\t\t`the data, then remove the old column`,\n\t\t\t\t\t{\n\t\t\t\t\t\ttable: table.name,\n\t\t\t\t\t\tcolumn: column.name,\n\t\t\t\t\t\tfrom: {\n\t\t\t\t\t\t\tstorage: previous.storage,\n\t\t\t\t\t\t\toptional: previous.optional,\n\t\t\t\t\t\t\tnullable: previous.nullable,\n\t\t\t\t\t\t},\n\t\t\t\t\t\tto: {\n\t\t\t\t\t\t\tstorage: column.storage,\n\t\t\t\t\t\t\toptional: column.optional,\n\t\t\t\t\t\t\tnullable: column.nullable,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t)\n\t\t\t}\n\t\t}\n\n\t\tfor (const index of table.indexes) {\n\t\t\tif (!before.indexes.some((candidate) => equalsValue(candidate, index))) {\n\t\t\t\tsteps.push({ operation: 'index.add', table: table.name, index })\n\t\t\t}\n\t\t}\n\t}\n\n\tconst projected = projectMigrationSchema(beforeSchema, steps)\n\tif (!equalsValue(projected, targetSchema)) {\n\t\tthrow new DatabaseError('MIGRATION', 'Migration plan does not project to the declared schema', {\n\t\t\tprojected,\n\t\t\tdeclared: targetSchema,\n\t\t})\n\t}\n\treturn cloneMigrationInput({ plan: { from, to, steps } }).plan\n}\n\n/**\n * Sequentially project migration steps over a canonical validated owned schema.\n * Adding a required non-null column to an existing table rejects with\n * `MIGRATION`; optional-only and nullable-only additions remain portable.\n *\n * @param schema - The initial deployed schema\n * @param steps - The ordered migration steps\n * @returns A fresh owned final schema\n */\nexport function projectMigrationSchema(\n\tschema: readonly TableSchema[],\n\tsteps: readonly MigrationStep[],\n): readonly TableSchema[] {\n\tlet owned: readonly TableSchema[]\n\tlet projectedSteps: readonly MigrationStep[]\n\ttry {\n\t\towned = normalizeDriverSchema(schema)\n\t\tprojectedSteps = cloneMigrationInput({ plan: { from: 0, to: 1, steps } }).plan.steps\n\t} catch (error) {\n\t\tthrow new DatabaseError('MIGRATION', 'Migration input is invalid', { cause: error })\n\t}\n\tconst tables = new Map(owned.map((table) => [table.name, table]))\n\tfor (const step of projectedSteps) {\n\t\tif (step.operation === 'table.add') {\n\t\t\tif (tables.has(step.table.name)) {\n\t\t\t\tthrow new DatabaseError('MIGRATION', `migrate: table '${step.table.name}' already exists`, {\n\t\t\t\t\ttable: step.table.name,\n\t\t\t\t})\n\t\t\t}\n\t\t\ttables.set(step.table.name, step.table)\n\t\t\tcontinue\n\t\t}\n\t\tconst table = tables.get(step.table)\n\t\tif (table === undefined) {\n\t\t\tthrow new DatabaseError('MIGRATION', `migrate: table '${step.table}' does not exist`, {\n\t\t\t\ttable: step.table,\n\t\t\t})\n\t\t}\n\t\tif (step.operation === 'table.remove') {\n\t\t\ttables.delete(step.table)\n\t\t\tcontinue\n\t\t}\n\t\tif (step.operation === 'column.add') {\n\t\t\tif (table.columns.some((column) => column.name === step.column.name)) {\n\t\t\t\tthrow new DatabaseError(\n\t\t\t\t\t'MIGRATION',\n\t\t\t\t\t`migrate: column '${step.column.name}' already exists`,\n\t\t\t\t\t{ table: step.table, column: step.column.name },\n\t\t\t\t)\n\t\t\t}\n\t\t\tif (!step.column.optional && !step.column.nullable) {\n\t\t\t\tthrow new DatabaseError(\n\t\t\t\t\t'MIGRATION',\n\t\t\t\t\t`migrate: required non-null column '${step.column.name}' cannot be added automatically to existing table '${step.table}'`,\n\t\t\t\t\t{ table: step.table, column: step.column.name },\n\t\t\t\t)\n\t\t\t}\n\t\t\ttables.set(step.table, { ...table, columns: [...table.columns, step.column] })\n\t\t\tcontinue\n\t\t}\n\t\tif (step.operation === 'column.remove') {\n\t\t\tif (!table.columns.some((column) => column.name === step.column)) {\n\t\t\t\tthrow new DatabaseError('MIGRATION', `migrate: column '${step.column}' does not exist`, {\n\t\t\t\t\ttable: step.table,\n\t\t\t\t\tcolumn: step.column,\n\t\t\t\t})\n\t\t\t}\n\t\t\tif (table.primary === step.column) {\n\t\t\t\tthrow new DatabaseError('MIGRATION', 'migrate: cannot remove the primary column', {\n\t\t\t\t\ttable: step.table,\n\t\t\t\t\tcolumn: step.column,\n\t\t\t\t})\n\t\t\t}\n\t\t\tif (table.indexes.some((index) => index.includes(step.column))) {\n\t\t\t\tthrow new DatabaseError('MIGRATION', 'migrate: cannot remove an indexed column', {\n\t\t\t\t\ttable: step.table,\n\t\t\t\t\tcolumn: step.column,\n\t\t\t\t})\n\t\t\t}\n\t\t\ttables.set(step.table, {\n\t\t\t\t...table,\n\t\t\t\tcolumns: table.columns.filter((column) => column.name !== step.column),\n\t\t\t})\n\t\t\tcontinue\n\t\t}\n\t\tif (step.operation === 'index.add') {\n\t\t\tif (\n\t\t\t\tstep.index.length === 0 ||\n\t\t\t\tstep.index.some((name) => !table.columns.some((column) => column.name === name))\n\t\t\t) {\n\t\t\t\tthrow new DatabaseError('MIGRATION', 'migrate: index references a missing column', {\n\t\t\t\t\ttable: step.table,\n\t\t\t\t\tindex: step.index,\n\t\t\t\t})\n\t\t\t}\n\t\t\tif (table.indexes.some((index) => equalsValue(index, step.index))) {\n\t\t\t\tthrow new DatabaseError('MIGRATION', 'migrate: index already exists', {\n\t\t\t\t\ttable: step.table,\n\t\t\t\t\tindex: step.index,\n\t\t\t\t})\n\t\t\t}\n\t\t\ttables.set(step.table, { ...table, indexes: [...table.indexes, step.index] })\n\t\t\tcontinue\n\t\t}\n\t\tif (!table.indexes.some((index) => equalsValue(index, step.index))) {\n\t\t\tthrow new DatabaseError('MIGRATION', 'migrate: index does not exist', {\n\t\t\t\ttable: step.table,\n\t\t\t\tindex: step.index,\n\t\t\t})\n\t\t}\n\t\ttables.set(step.table, {\n\t\t\t...table,\n\t\t\tindexes: table.indexes.filter((index) => !equalsValue(index, step.index)),\n\t\t})\n\t}\n\ttry {\n\t\treturn normalizeDriverSchema([...tables.values()])\n\t} catch (error) {\n\t\tthrow new DatabaseError('MIGRATION', 'Projected migration schema is invalid', { cause: error })\n\t}\n}\n\n/**\n * Canonicalize an unknown driver schema into a distinct deeply frozen snapshot.\n *\n * @remarks\n * Table and column lists are sorted by name. The index list is sorted by the\n * complete serialized tuple while column order inside each compound index is\n * preserved because it carries index semantics. Validation and ownership flow\n * through {@link cloneDriverSchema} before and after projection.\n *\n * @param value - Unknown driver schema\n * @returns A validated, owned canonical schema\n */\nexport function normalizeDriverSchema(value: unknown): readonly TableSchema[] {\n\tconst owned = cloneDriverSchema(value)\n\tconst tables = owned.map((table) => ({\n\t\tname: table.name,\n\t\tprimary: table.primary,\n\t\tcolumns: [...table.columns].sort((left, right) => compareValues(left.name, right.name)),\n\t\tindexes: [...table.indexes].sort((left, right) =>\n\t\t\tcompareValues(JSON.stringify(left), JSON.stringify(right)),\n\t\t),\n\t}))\n\ttables.sort((left, right) => compareValues(left.name, right.name))\n\treturn cloneDriverSchema(tables)\n}\n\n/**\n * Apply one table's {@link MigrationStep}s to its rows — a pure row transform.\n *\n * @remarks\n * `column.remove` drops that field from every row (a fresh copy — inputs are\n * never mutated, AGENTS §11); `column.add` leaves rows as-is (an absent field\n * reads as `undefined`, backfill is application policy). `table.add` /\n * `table.remove` / `index.add` / `index.remove` are no-ops here (they operate\n * on storage shape, not row shape). Steps for tables other than the one\n * `rows` belongs to are ignored — pass only the steps relevant to this table.\n *\n * @param rows - The table's current rows\n * @param steps - The migration steps to apply (typically one table's slice of a {@link Migration})\n * @returns A new array of transformed rows; `rows` is never mutated\n *\n * @example\n * ```ts\n * const rows = [{ id: 'a', name: 'Ada', legacy: true }]\n * migrateRows(rows, [{ operation: 'column.remove', table: 'users', column: 'legacy' }])\n * // => [{ id: 'a', name: 'Ada' }]\n * ```\n */\nexport function migrateRows(rows: readonly Row[], steps: readonly MigrationStep[]): readonly Row[] {\n\tconst removed = steps\n\t\t.filter(\n\t\t\t(step): step is Extract<MigrationStep, { operation: 'column.remove' }> =>\n\t\t\t\tstep.operation === 'column.remove',\n\t\t)\n\t\t.map((step) => step.column)\n\n\tif (removed.length === 0) return rows.map((row) => ({ ...row }))\n\n\treturn rows.map((row) => {\n\t\tconst next: Row = {}\n\t\tfor (const key of Object.keys(row)) {\n\t\t\tif (!removed.includes(key)) next[key] = row[key]\n\t\t}\n\t\treturn next\n\t})\n}\n\n// === Conformance\n\n/**\n * Run the driver-conformance battery against a fresh {@link DriverInterface}\n * per phase, yielding one {@link ConformanceFinding} per violated invariant —\n * the shared invariant suite every backend (in-memory, SQLite, IndexedDB)\n * must uphold to be a drop-in {@link DriverInterface}.\n *\n * @remarks\n * Framework-agnostic: no test-runner or Node imports, only sibling core\n * modules — so it runs equally from a unit test, a smoke script, or a new\n * driver's own README. Opens a fixed two-table schema (`users` keyed by the\n * default `id`, `posts` keyed by a non-id `slug`) and, calling `factory()`\n * fresh for each phase so failures stay isolated, verifies: `open`/`close`;\n * `read` of a missing key returns `undefined`; `write`/`read` round-trip with\n * DEEP copy-in/copy-out isolation (mutating the caller's row — including a\n * NESTED field — after `write`, or a row `read` returns, never perturbs\n * stored state) and upsert-overwrite; simultaneous same-key `insert` calls\n * produce exactly one commit and one `CONFLICT`; pre-aborted `write`,\n * `insert`, and `delete` calls leave storage unchanged; `delete` returns\n * `true` then `false`;\n * `keys`/`scan` yield in ascending key order; `clear` empties only its target\n * table; `snapshot`'s rollback thunk restores pre-snapshot state, including a\n * NESTED field mutated in place on a read-back row between capture and\n * restore; a scoped `snapshot(['users'])` rolls back only the named table,\n * leaving a concurrent mutation to another table intact; a\n * non-`id` primary key (`posts.slug`) round-trips; a nested-object row\n * round-trips structurally (via {@link equalsValue}). The optional surface is\n * presence-gated: when `migrate` exists, a `column.remove` plan strips the\n * column from stored rows and a plan referencing an unknown table throws\n * `DatabaseError` `MIGRATION`; when `stream` exists, it yields only\n * condition-matching rows and honors `offset`/`limit`; when `transaction`\n * exists, `commit` persists and `rollback` restores; when both `metadata` and\n * `stamp` exist, a fresh store's `metadata()` is `undefined`, and after\n * `stamp({ version, schema })`, `metadata()` returns the exact stamped value.\n *\n * Each phase runs within a `try`/`catch`: an EXPECTED mismatch yields a\n * finding built from the assertion, while an UNEXPECTED throw (a driver\n * crash mid-phase) is caught and yielded as a finding too, naming the phase\n * as `check` and carrying the caught error in `context.error` — a broken\n * driver can never escape the battery as an unhandled rejection. Within a\n * phase, the FIRST violated assertion yields and the phase stops (matching\n * the historical fail-fast shape at phase granularity); the generator then\n * moves on to the next phase regardless. Because this is a **generator**,\n * consuming only the first yielded value reproduces true fail-fast (later\n * phases never run) — that is exactly what {@link conformDriver} does.\n *\n * @param factory - Mints a fresh, unopened driver instance (called once per phase)\n * @yields One {@link ConformanceFinding} per violated invariant, in phase order\n *\n * @example\n * ```ts\n * import { createMemoryDriver, driverFindings } from '@orkestrel/database'\n *\n * for await (const finding of driverFindings(() => createMemoryDriver())) {\n * \tconsole.log(finding.check, finding.message)\n * }\n * ```\n */\nexport async function* driverFindings(\n\tfactory: () => DriverInterface,\n): AsyncIterable<ConformanceFinding> {\n\t// Fixed two-table schema every phase opens: `users` keyed by `id` (the\n\t// default primary), `posts` keyed by a non-id `slug` — exercising both\n\t// primary-key shapes in one battery.\n\tconst CONFORMANCE_USERS_SCHEMA: TableSchema = {\n\t\tname: 'users',\n\t\tprimary: 'id',\n\t\tcolumns: [\n\t\t\t{ name: 'id', storage: 'text', optional: false, nullable: false },\n\t\t\t{ name: 'name', storage: 'text', optional: false, nullable: false },\n\t\t\t{ name: 'age', storage: 'integer', optional: true, nullable: false },\n\t\t\t// Declared so the nested-roundtrip phase is fair to typed-column\n\t\t\t// backends (a SQL driver persists only declared columns; schemaless\n\t\t\t// backends ignore declarations entirely).\n\t\t\t{ name: 'meta', storage: 'json', optional: true, nullable: false },\n\t\t],\n\t\tindexes: [],\n\t}\n\tconst CONFORMANCE_POSTS_SCHEMA: TableSchema = {\n\t\tname: 'posts',\n\t\tprimary: 'slug',\n\t\tcolumns: [\n\t\t\t{ name: 'slug', storage: 'text', optional: false, nullable: false },\n\t\t\t{ name: 'title', storage: 'text', optional: false, nullable: false },\n\t\t],\n\t\tindexes: [],\n\t}\n\tconst CONFORMANCE_SCHEMA: readonly TableSchema[] = [\n\t\tCONFORMANCE_USERS_SCHEMA,\n\t\tCONFORMANCE_POSTS_SCHEMA,\n\t]\n\t// a. open with the inline two-table schema, then close cleanly.\n\ttry {\n\t\tconst driver = factory()\n\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\tawait driver.close()\n\t} catch (error) {\n\t\tyield {\n\t\t\tcheck: 'open-close',\n\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\tcontext: { error },\n\t\t}\n\t}\n\n\t// b. read of a missing key -> undefined.\n\ttry {\n\t\tconst driver = factory()\n\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\tconst missing = await driver.read('users', 'nope')\n\t\tawait driver.close()\n\t\tif (missing !== undefined) {\n\t\t\tyield {\n\t\t\t\tcheck: 'read-missing',\n\t\t\t\tmessage: 'read of a missing key must return undefined',\n\t\t\t\tcontext: { table: 'users', expected: undefined, actual: missing },\n\t\t\t}\n\t\t}\n\t} catch (error) {\n\t\tyield {\n\t\t\tcheck: 'read-missing',\n\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\tcontext: { error },\n\t\t}\n\t}\n\n\t// c. write/read round-trip, copy-in/copy-out isolation (including NESTED\n\t// fields, not just top-level ones), upsert-overwrite.\n\twriteRead: {\n\t\ttry {\n\t\t\tconst driver = factory()\n\t\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\t\tconst input: Row = { id: 'caller', name: 'Ada', age: 30, meta: { tags: ['a'] } }\n\t\t\tawait driver.write('users', 'u1', input)\n\t\t\tinput.name = 'Mutated after write'\n\t\t\tif (isRecord(input.meta) && Array.isArray(input.meta.tags)) input.meta.tags.push('mutated')\n\t\t\tconst stored = await driver.read('users', 'u1')\n\t\t\tconst original = { id: 'u1', name: 'Ada', age: 30, meta: { tags: ['a'] } }\n\t\t\tif (stored === undefined || !equalsValue(stored, original)) {\n\t\t\t\tawait driver.close()\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'copy-in',\n\t\t\t\t\tmessage:\n\t\t\t\t\t\t'write must deep-copy the input row (including nested fields) rather than store it by reference',\n\t\t\t\t\tcontext: { table: 'users', expected: original, actual: stored },\n\t\t\t\t}\n\t\t\t\tbreak writeRead\n\t\t\t}\n\t\t\tstored.name = 'Mutated after read'\n\t\t\tif (isRecord(stored.meta) && Array.isArray(stored.meta.tags)) stored.meta.tags.push('mutated')\n\t\t\tconst reread = await driver.read('users', 'u1')\n\t\t\tif (reread === undefined || !equalsValue(reread, original)) {\n\t\t\t\tawait driver.close()\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'copy-out',\n\t\t\t\t\tmessage:\n\t\t\t\t\t\t'read must deep-copy the stored row (including nested fields) rather than return it by reference',\n\t\t\t\t\tcontext: { table: 'users', expected: original, actual: reread },\n\t\t\t\t}\n\t\t\t\tbreak writeRead\n\t\t\t}\n\t\t\tconst overwrite = { id: 'caller', name: 'Ada Overwritten', age: 31 }\n\t\t\tawait driver.write('users', 'u1', overwrite)\n\t\t\tconst overwritten = await driver.read('users', 'u1')\n\t\t\tawait driver.close()\n\t\t\tconst expectedOverwrite = { ...overwrite, id: 'u1' }\n\t\t\tif (overwritten === undefined || !equalsValue(overwritten, expectedOverwrite)) {\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'upsert',\n\t\t\t\t\tmessage: 'write must upsert-overwrite an existing key',\n\t\t\t\t\tcontext: { table: 'users', expected: expectedOverwrite, actual: overwritten },\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tyield {\n\t\t\t\tcheck: 'write-read',\n\t\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\t\tcontext: { error },\n\t\t\t}\n\t\t}\n\t}\n\n\t// c2. insert is atomic: concurrent duplicates produce one commit and one CONFLICT.\n\t{\n\t\ttry {\n\t\t\tconst driver = factory()\n\t\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\t\tconst outcomes = await Promise.allSettled([\n\t\t\t\tdriver.insert('users', 'u1', { id: 'u1', name: 'Ada', age: 30 }),\n\t\t\t\tdriver.insert('users', 'u1', { id: 'u1', name: 'Grace', age: 40 }),\n\t\t\t])\n\t\t\tlet fulfilled = 0\n\t\t\tlet conflicted = 0\n\t\t\tfor (const outcome of outcomes) {\n\t\t\t\tif (outcome.status === 'fulfilled') fulfilled += 1\n\t\t\t\telse if (isDatabaseError(outcome.reason) && outcome.reason.code === 'CONFLICT') {\n\t\t\t\t\tconflicted += 1\n\t\t\t\t}\n\t\t\t}\n\t\t\tconst keys = await driver.keys('users')\n\t\t\tawait driver.close()\n\t\t\tif (fulfilled !== 1 || conflicted !== 1 || !equalsValue(keys, ['u1'])) {\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'insert-atomic',\n\t\t\t\t\tmessage: 'concurrent same-key inserts must produce one commit and one CONFLICT',\n\t\t\t\t\tcontext: {\n\t\t\t\t\t\texpected: { fulfilled: 1, conflicted: 1, keys: ['u1'] },\n\t\t\t\t\t\tactual: { fulfilled, conflicted, keys },\n\t\t\t\t\t},\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tyield {\n\t\t\t\tcheck: 'insert-atomic',\n\t\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\t\tcontext: { error },\n\t\t\t}\n\t\t}\n\t}\n\n\t// d. delete -> true then false.\n\tdeletePhase: {\n\t\ttry {\n\t\t\tconst driver = factory()\n\t\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\t\tawait driver.write('users', 'u1', { id: 'u1', name: 'Ada', age: 30 })\n\t\t\tconst first = await driver.delete('users', 'u1')\n\t\t\tif (first !== true) {\n\t\t\t\tawait driver.close()\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'delete-true',\n\t\t\t\t\tmessage: 'delete of an existing key must return true',\n\t\t\t\t\tcontext: { table: 'users', expected: true, actual: first },\n\t\t\t\t}\n\t\t\t\tbreak deletePhase\n\t\t\t}\n\t\t\tconst second = await driver.delete('users', 'u1')\n\t\t\tawait driver.close()\n\t\t\tif (second !== false) {\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'delete-false',\n\t\t\t\t\tmessage: 'delete of an already-removed key must return false',\n\t\t\t\t\tcontext: { table: 'users', expected: false, actual: second },\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tyield {\n\t\t\t\tcheck: 'delete',\n\t\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\t\tcontext: { error },\n\t\t\t}\n\t\t}\n\t}\n\n\t// d2. pre-aborted point mutations reject as ABORTED without changing rows.\n\ttry {\n\t\tconst driver = factory()\n\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\tawait driver.write('users', 'u1', { id: 'u1', name: 'Ada', age: 30 })\n\t\tconst controller = new AbortController()\n\t\tcontroller.abort('conformance abort')\n\t\tlet writeError: unknown\n\t\tlet insertError: unknown\n\t\tlet deleteError: unknown\n\t\ttry {\n\t\t\tawait driver.write(\n\t\t\t\t'users',\n\t\t\t\t'u2',\n\t\t\t\t{ id: 'u2', name: 'Grace', age: 40 },\n\t\t\t\t{ signal: controller.signal },\n\t\t\t)\n\t\t} catch (error) {\n\t\t\twriteError = error\n\t\t}\n\t\ttry {\n\t\t\tawait driver.insert(\n\t\t\t\t'users',\n\t\t\t\t'u2',\n\t\t\t\t{ id: 'u2', name: 'Grace', age: 40 },\n\t\t\t\t{ signal: controller.signal },\n\t\t\t)\n\t\t} catch (error) {\n\t\t\tinsertError = error\n\t\t}\n\t\ttry {\n\t\t\tawait driver.delete('users', 'u1', { signal: controller.signal })\n\t\t} catch (error) {\n\t\t\tdeleteError = error\n\t\t}\n\t\tconst keys = await driver.keys('users')\n\t\tawait driver.close()\n\t\tif (\n\t\t\t!isDatabaseError(writeError) ||\n\t\t\twriteError.code !== 'ABORTED' ||\n\t\t\t!isDatabaseError(insertError) ||\n\t\t\tinsertError.code !== 'ABORTED' ||\n\t\t\t!isDatabaseError(deleteError) ||\n\t\t\tdeleteError.code !== 'ABORTED' ||\n\t\t\t!equalsValue(keys, ['u1'])\n\t\t) {\n\t\t\tyield {\n\t\t\t\tcheck: 'mutation-abort',\n\t\t\t\tmessage: 'pre-aborted write/insert/delete must reject ABORTED without changing rows',\n\t\t\t\tcontext: {\n\t\t\t\t\texpected: { write: 'ABORTED', insert: 'ABORTED', delete: 'ABORTED', keys: ['u1'] },\n\t\t\t\t\tactual: {\n\t\t\t\t\t\twrite: isDatabaseError(writeError) ? writeError.code : writeError,\n\t\t\t\t\t\tinsert: isDatabaseError(insertError) ? insertError.code : insertError,\n\t\t\t\t\t\tdelete: isDatabaseError(deleteError) ? deleteError.code : deleteError,\n\t\t\t\t\t\tkeys,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t}\n\t\t}\n\t} catch (error) {\n\t\tyield {\n\t\t\tcheck: 'mutation-abort',\n\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\tcontext: { error },\n\t\t}\n\t}\n\n\t// e. keys and scan in ascending key order.\n\torderPhase: {\n\t\ttry {\n\t\t\tconst driver = factory()\n\t\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\t\tconst rows = [\n\t\t\t\t{ id: 'c', name: 'C', age: 3 },\n\t\t\t\t{ id: 'a', name: 'A', age: 1 },\n\t\t\t\t{ id: 'b', name: 'B', age: 2 },\n\t\t\t]\n\t\t\tfor (const row of rows) await driver.write('users', row.id, row)\n\t\t\tconst expected = ['a', 'b', 'c']\n\t\t\tconst keys = [...(await driver.keys('users'))]\n\t\t\tif (!equalsValue(keys, expected)) {\n\t\t\t\tawait driver.close()\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'keys-order',\n\t\t\t\t\tmessage: 'keys must be returned in ascending key order',\n\t\t\t\t\tcontext: { table: 'users', expected, actual: keys },\n\t\t\t\t}\n\t\t\t\tbreak orderPhase\n\t\t\t}\n\t\t\tconst scanned: Row[] = []\n\t\t\tfor await (const row of driver.scan('users')) scanned.push(row)\n\t\t\tconst scannedIds = scanned.map((row) => row.id)\n\t\t\tawait driver.close()\n\t\t\tif (!equalsValue(scannedIds, expected)) {\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'scan-order',\n\t\t\t\t\tmessage: 'scan must yield rows in ascending key order',\n\t\t\t\t\tcontext: { table: 'users', expected, actual: scannedIds },\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tyield {\n\t\t\t\tcheck: 'order',\n\t\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\t\tcontext: { error },\n\t\t\t}\n\t\t}\n\t}\n\n\t// f. clear empties only the targeted table.\n\tclearPhase: {\n\t\ttry {\n\t\t\tconst driver = factory()\n\t\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\t\tawait driver.write('users', 'u1', { id: 'u1', name: 'Ada', age: 30 })\n\t\t\tawait driver.write('posts', 'p1', { slug: 'p1', title: 'Post' })\n\t\t\tawait driver.clear('users')\n\t\t\tconst usersKeys = await driver.keys('users')\n\t\t\tconst postsKeys = await driver.keys('posts')\n\t\t\tawait driver.close()\n\t\t\tif (usersKeys.length !== 0) {\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'clear-target',\n\t\t\t\t\tmessage: 'clear must empty the targeted table',\n\t\t\t\t\tcontext: { table: 'users', expected: [], actual: usersKeys },\n\t\t\t\t}\n\t\t\t\tbreak clearPhase\n\t\t\t}\n\t\t\tif (postsKeys.length !== 1) {\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'clear-other',\n\t\t\t\t\tmessage: 'clear must not affect other tables',\n\t\t\t\t\tcontext: { table: 'posts', expected: 1, actual: postsKeys.length },\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tyield {\n\t\t\t\tcheck: 'clear',\n\t\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\t\tcontext: { error },\n\t\t\t}\n\t\t}\n\t}\n\n\t// g. snapshot rollback restores pre-snapshot state.\n\tsnapshotPhase: {\n\t\ttry {\n\t\t\tconst driver = factory()\n\t\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\t\tconst original = { id: 'u1', name: 'Ada', age: 30 }\n\t\t\tawait driver.write('users', 'u1', original)\n\t\t\tconst rollback = await driver.snapshot()\n\t\t\tawait driver.write('users', 'u2', { id: 'u2', name: 'Grace', age: 40 })\n\t\t\tawait driver.delete('users', 'u1')\n\t\t\tawait rollback()\n\t\t\tconst keys = [...(await driver.keys('users'))]\n\t\t\tif (!equalsValue(keys, ['u1'])) {\n\t\t\t\tawait driver.close()\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'snapshot-rollback',\n\t\t\t\t\tmessage: 'snapshot rollback must restore the pre-snapshot key set',\n\t\t\t\t\tcontext: { table: 'users', expected: ['u1'], actual: keys },\n\t\t\t\t}\n\t\t\t\tbreak snapshotPhase\n\t\t\t}\n\t\t\tconst restored = await driver.read('users', 'u1')\n\t\t\tawait driver.close()\n\t\t\tif (restored === undefined || !equalsValue(restored, original)) {\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'snapshot-rollback-value',\n\t\t\t\t\tmessage: 'snapshot rollback must restore pre-snapshot row values',\n\t\t\t\t\tcontext: { table: 'users', expected: original, actual: restored },\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tyield {\n\t\t\t\tcheck: 'snapshot',\n\t\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\t\tcontext: { error },\n\t\t\t}\n\t\t}\n\t}\n\n\t// g2. snapshot rollback survives a nested field mutated between capture and restore.\n\ttry {\n\t\tconst driver = factory()\n\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\tconst original = { id: 'u3', name: 'Nested', age: 20, meta: { tags: ['a'] } }\n\t\tawait driver.write('users', 'u3', original)\n\t\tconst rollback = await driver.snapshot()\n\t\tconst before = await driver.read('users', 'u3')\n\t\tif (isRecord(before) && isRecord(before.meta) && Array.isArray(before.meta.tags)) {\n\t\t\tbefore.meta.tags.push('mutated-before-restore')\n\t\t}\n\t\tawait driver.write('users', 'u3', {\n\t\t\tid: 'u3',\n\t\t\tname: 'Nested',\n\t\t\tage: 20,\n\t\t\tmeta: { tags: ['a', 'mutated-after-write'] },\n\t\t})\n\t\tawait rollback()\n\t\tconst restored = await driver.read('users', 'u3')\n\t\tawait driver.close()\n\t\tif (restored === undefined || !equalsValue(restored, original)) {\n\t\t\tyield {\n\t\t\t\tcheck: 'snapshot-nested',\n\t\t\t\tmessage:\n\t\t\t\t\t'snapshot rollback must restore pre-snapshot nested field values, unaffected by a later in-place mutation of a read-back row',\n\t\t\t\tcontext: { table: 'users', expected: original, actual: restored },\n\t\t\t}\n\t\t}\n\t} catch (error) {\n\t\tyield {\n\t\t\tcheck: 'snapshot-nested',\n\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\tcontext: { error },\n\t\t}\n\t}\n\n\t// h. non-id primary extraction (posts keyed by slug).\n\ttry {\n\t\tconst driver = factory()\n\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\tawait driver.write('posts', 'hello-world', { slug: 'caller', title: 'Hello' })\n\t\tconst post = await driver.read('posts', 'hello-world')\n\t\tconst key = post === undefined ? undefined : extractKey(post, 'slug')\n\t\tawait driver.close()\n\t\tif (key !== 'hello-world') {\n\t\t\tyield {\n\t\t\t\tcheck: 'non-id-primary',\n\t\t\t\tmessage: 'a non-id primary key column must round-trip through the store',\n\t\t\t\tcontext: { table: 'posts', expected: 'hello-world', actual: key },\n\t\t\t}\n\t\t}\n\t} catch (error) {\n\t\tyield {\n\t\t\tcheck: 'non-id-primary',\n\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\tcontext: { error },\n\t\t}\n\t}\n\n\t// i. nested-object row round-trip (structural, via equalsValue).\n\ttry {\n\t\tconst driver = factory()\n\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\tconst nested = {\n\t\t\tid: 'u3',\n\t\t\tname: 'Nested',\n\t\t\tage: 20,\n\t\t\tmeta: { tags: ['a', 'b'], deep: { flag: true } },\n\t\t}\n\t\tawait driver.write('users', 'u3', nested)\n\t\tconst readBack = await driver.read('users', 'u3')\n\t\tawait driver.close()\n\t\tif (readBack === undefined || !equalsValue(readBack, nested)) {\n\t\t\tyield {\n\t\t\t\tcheck: 'nested-roundtrip',\n\t\t\t\tmessage: 'a nested-object row must round-trip structurally',\n\t\t\t\tcontext: { table: 'users', expected: nested, actual: readBack },\n\t\t\t}\n\t\t}\n\t} catch (error) {\n\t\tyield {\n\t\t\tcheck: 'nested-roundtrip',\n\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\tcontext: { error },\n\t\t}\n\t}\n\n\t// j. migrate (presence-gated): column.remove strips rows; unknown tables fail.\n\tmigratePhase: {\n\t\ttry {\n\t\t\tconst driver = factory()\n\t\t\tif (driver.migrate === undefined) break migratePhase\n\t\t\tconst deployedUsers: TableSchema = {\n\t\t\t\t...CONFORMANCE_USERS_SCHEMA,\n\t\t\t\tcolumns: [\n\t\t\t\t\t...CONFORMANCE_USERS_SCHEMA.columns,\n\t\t\t\t\t{ name: 'legacy', storage: 'boolean', optional: true, nullable: false },\n\t\t\t\t],\n\t\t\t}\n\t\t\tawait driver.open([deployedUsers, CONFORMANCE_POSTS_SCHEMA])\n\t\t\tawait driver.write('users', 'u1', { id: 'u1', name: 'Ada', age: 30, legacy: true })\n\t\t\tconst removePlan = planMigration([deployedUsers], [CONFORMANCE_USERS_SCHEMA])\n\t\t\tawait driver.migrate({ plan: removePlan })\n\t\t\tconst migrated = await driver.read('users', 'u1')\n\t\t\tif (migrated === undefined || 'legacy' in migrated) {\n\t\t\t\tawait driver.close()\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'migrate-column-remove',\n\t\t\t\t\tmessage: 'a column.remove migration must strip the column from stored rows',\n\t\t\t\t\tcontext: {\n\t\t\t\t\t\ttable: 'users',\n\t\t\t\t\t\texpected: undefined,\n\t\t\t\t\t\tactual: migrated === undefined ? undefined : migrated.legacy,\n\t\t\t\t\t},\n\t\t\t\t}\n\t\t\t\tbreak migratePhase\n\t\t\t}\n\t\t\tlet caught: unknown\n\t\t\ttry {\n\t\t\t\tawait driver.migrate({\n\t\t\t\t\tplan: {\n\t\t\t\t\t\tfrom: 0,\n\t\t\t\t\t\tto: 1,\n\t\t\t\t\t\tsteps: [{ operation: 'table.remove', table: 'ghost' }],\n\t\t\t\t\t},\n\t\t\t\t})\n\t\t\t} catch (error) {\n\t\t\t\tcaught = error\n\t\t\t}\n\t\t\tawait driver.close()\n\t\t\tif (!isDatabaseError(caught) || caught.code !== 'MIGRATION') {\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'migrate-unknown-table',\n\t\t\t\t\tmessage:\n\t\t\t\t\t\t'a migration step referencing an unknown table must throw a MIGRATION DatabaseError',\n\t\t\t\t\tcontext: {\n\t\t\t\t\t\ttable: 'ghost',\n\t\t\t\t\t\texpected: 'MIGRATION',\n\t\t\t\t\t\tactual: isDatabaseError(caught) ? caught.code : caught,\n\t\t\t\t\t},\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tyield {\n\t\t\t\tcheck: 'migrate',\n\t\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\t\tcontext: { error },\n\t\t\t}\n\t\t}\n\t}\n\n\t// k. stream (presence-gated): condition matching and paging.\n\tstreamPhase: {\n\t\ttry {\n\t\t\tconst driver = factory()\n\t\t\tif (driver.stream === undefined) break streamPhase\n\t\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\t\tconst rows = [\n\t\t\t\t{ id: 'a', name: 'A', age: 10 },\n\t\t\t\t{ id: 'b', name: 'B', age: 20 },\n\t\t\t\t{ id: 'c', name: 'C', age: 30 },\n\t\t\t]\n\t\t\tfor (const row of rows) await driver.write('users', row.id, row)\n\t\t\tconst input: QueryInput = {\n\t\t\t\tconditions: [{ column: 'age', operator: 'above', values: [10], connector: 'and' }],\n\t\t\t}\n\t\t\tconst matched: Row[] = []\n\t\t\tfor await (const row of driver.stream('users', input)) matched.push(row)\n\t\t\tconst matchedIds = matched.map((row) => row.id).sort()\n\t\t\tif (!equalsValue(matchedIds, ['b', 'c'])) {\n\t\t\t\tawait driver.close()\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'stream-match',\n\t\t\t\t\tmessage: 'stream must yield only condition-matching rows',\n\t\t\t\t\tcontext: { table: 'users', expected: ['b', 'c'], actual: matchedIds },\n\t\t\t\t}\n\t\t\t\tbreak streamPhase\n\t\t\t}\n\t\t\tconst paged: Row[] = []\n\t\t\tfor await (const row of driver.stream('users', { offset: 1, limit: 1 })) paged.push(row)\n\t\t\tawait driver.close()\n\t\t\tif (paged.length !== 1) {\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'stream-page',\n\t\t\t\t\tmessage: 'stream must honor offset and limit',\n\t\t\t\t\tcontext: { table: 'users', expected: 1, actual: paged.length },\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tyield {\n\t\t\t\tcheck: 'stream',\n\t\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\t\tcontext: { error },\n\t\t\t}\n\t\t}\n\t}\n\n\t// l. transaction (presence-gated): commit persists, rollback restores.\n\ttransactionPhase: {\n\t\ttry {\n\t\t\tconst driver = factory()\n\t\t\tif (driver.transaction === undefined) break transactionPhase\n\t\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\t\tawait driver.write('users', 'u1', { id: 'u1', name: 'Ada', age: 30 })\n\t\t\tawait driver.transaction(async (transaction) => {\n\t\t\t\tawait transaction.write('users', 'u2', { id: 'u2', name: 'Grace', age: 40 })\n\t\t\t})\n\t\t\tconst afterCommit = [...(await driver.keys('users'))].sort()\n\t\t\tif (!equalsValue(afterCommit, ['u1', 'u2'])) {\n\t\t\t\tawait driver.close()\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'transaction-commit',\n\t\t\t\t\tmessage: 'transaction commit must persist writes made during the scope',\n\t\t\t\t\tcontext: { table: 'users', expected: ['u1', 'u2'], actual: afterCommit },\n\t\t\t\t}\n\t\t\t\tbreak transactionPhase\n\t\t\t}\n\t\t\tconst reason = {}\n\t\t\ttry {\n\t\t\t\tawait driver.transaction(async (transaction) => {\n\t\t\t\t\tawait transaction.write('users', 'u3', { id: 'u3', name: 'Marie', age: 50 })\n\t\t\t\t\tthrow reason\n\t\t\t\t})\n\t\t\t} catch (error) {\n\t\t\t\tif (error !== reason) throw error\n\t\t\t}\n\t\t\tconst afterRollback = [...(await driver.keys('users'))].sort()\n\t\t\tawait driver.close()\n\t\t\tif (!equalsValue(afterRollback, ['u1', 'u2'])) {\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'transaction-rollback',\n\t\t\t\t\tmessage: 'transaction rollback must restore pre-transaction state',\n\t\t\t\t\tcontext: { table: 'users', expected: ['u1', 'u2'], actual: afterRollback },\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tyield {\n\t\t\t\tcheck: 'transaction',\n\t\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\t\tcontext: { error },\n\t\t\t}\n\t\t}\n\t}\n\n\t// m. metadata/stamp (presence-gated): fresh is undefined; stamp round-trips.\n\tmetadataPhase: {\n\t\ttry {\n\t\t\tconst driver = factory()\n\t\t\tif (driver.metadata === undefined || driver.stamp === undefined) break metadataPhase\n\t\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\t\tconst fresh = await driver.metadata()\n\t\t\tif (fresh !== undefined) {\n\t\t\t\tawait driver.close()\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'metadata-fresh',\n\t\t\t\t\tmessage: 'a fresh store must report undefined metadata',\n\t\t\t\t\tcontext: { expected: undefined, actual: fresh },\n\t\t\t\t}\n\t\t\t\tbreak metadataPhase\n\t\t\t}\n\t\t\tconst stamped = { version: 1, schema: CONFORMANCE_SCHEMA }\n\t\t\tawait driver.stamp(stamped)\n\t\t\tconst read = await driver.metadata()\n\t\t\tawait driver.close()\n\t\t\tif (read === undefined || !equalsValue(read, stamped)) {\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'metadata-stamp',\n\t\t\t\t\tmessage: 'metadata() must return exactly the last-stamped value',\n\t\t\t\t\tcontext: { expected: stamped, actual: read },\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tyield {\n\t\t\t\tcheck: 'metadata-stamp',\n\t\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\t\tcontext: { error },\n\t\t\t}\n\t\t}\n\t}\n\n\t// n. scoped snapshot rolls back only the named table.\n\tscopedPhase: {\n\t\ttry {\n\t\t\tconst driver = factory()\n\t\t\tawait driver.open(CONFORMANCE_SCHEMA)\n\t\t\tconst original = { id: 'u1', name: 'Ada', age: 30 }\n\t\t\tawait driver.write('users', 'u1', original)\n\t\t\tawait driver.write('posts', 'p1', { slug: 'p1', title: 'Post' })\n\t\t\tconst rollback = await driver.snapshot(['users'])\n\t\t\tawait driver.write('users', 'u2', { id: 'u2', name: 'Grace', age: 40 })\n\t\t\tawait driver.write('posts', 'p2', { slug: 'p2', title: 'Another post' })\n\t\t\tawait rollback()\n\t\t\tconst usersKeys = [...(await driver.keys('users'))]\n\t\t\tif (!equalsValue(usersKeys, ['u1'])) {\n\t\t\t\tawait driver.close()\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'snapshot-scoped-users',\n\t\t\t\t\tmessage: 'a scoped snapshot must roll back only the named table',\n\t\t\t\t\tcontext: { table: 'users', expected: ['u1'], actual: usersKeys },\n\t\t\t\t}\n\t\t\t\tbreak scopedPhase\n\t\t\t}\n\t\t\tconst postsKeys = [...(await driver.keys('posts'))].sort()\n\t\t\tawait driver.close()\n\t\t\tif (!equalsValue(postsKeys, ['p1', 'p2'])) {\n\t\t\t\tyield {\n\t\t\t\t\tcheck: 'snapshot-scoped-posts',\n\t\t\t\t\tmessage: \"a scoped snapshot must leave an unnamed table's mutations intact\",\n\t\t\t\t\tcontext: { table: 'posts', expected: ['p1', 'p2'], actual: postsKeys },\n\t\t\t\t}\n\t\t\t}\n\t\t} catch (error) {\n\t\t\tyield {\n\t\t\t\tcheck: 'snapshot-scoped',\n\t\t\t\tmessage: error instanceof Error ? error.message : String(error),\n\t\t\t\tcontext: { error },\n\t\t\t}\n\t\t}\n\t}\n}\n\n/**\n * Run the driver-conformance battery, throwing on the first violated\n * invariant — the fail-fast entry point most callers (test setup, CI smoke\n * checks) want.\n *\n * @remarks\n * A thin driver over {@link driverFindings}: because that generator is\n * lazy, consuming only its first yielded value means every LATER phase\n * never runs — true fail-fast, not merely \"report only the first\". The\n * thrown error is byte-compatible with the historical shape: a\n * `CONFORMANCE` {@link DatabaseError} whose `message` is the finding's\n * `message` and whose `context` is `{ check, ...finding.context }`.\n *\n * @param factory - Mints a fresh, unopened driver instance (called once per phase)\n * @returns Nothing — resolves once every phase has passed\n * @throws A `CONFORMANCE` {@link DatabaseError} on the first violated invariant\n *\n * @example\n * ```ts\n * import { conformDriver, createMemoryDriver } from '@orkestrel/database'\n *\n * await conformDriver(() => createMemoryDriver()) // resolves when every invariant holds\n * ```\n */\nexport async function conformDriver(factory: () => DriverInterface): Promise<void> {\n\tfor await (const finding of driverFindings(factory)) {\n\t\tthrow new DatabaseError('CONFORMANCE', finding.message, {\n\t\t\tcheck: finding.check,\n\t\t\t...finding.context,\n\t\t})\n\t}\n}\n\n/**\n * Run the FULL driver-conformance battery and collect every violation — the\n * audit entry point for a driver author who wants a complete report rather\n * than a single fail-fast throw.\n *\n * @remarks\n * Drains {@link driverFindings} to completion: every phase runs regardless\n * of earlier violations, so a driver breaking two independent invariants\n * reports both. An empty array means the driver is fully conformant.\n *\n * @param factory - Mints a fresh, unopened driver instance (called once per phase)\n * @returns Every violated invariant found, in phase order (empty when fully conformant)\n *\n * @example\n * ```ts\n * import { auditDriver, createMemoryDriver } from '@orkestrel/database'\n *\n * const findings = await auditDriver(() => createMemoryDriver())\n * for (const finding of findings) console.log(`${finding.check}: ${finding.message}`)\n * ```\n */\nexport async function auditDriver(\n\tfactory: () => DriverInterface,\n): Promise<readonly ConformanceFinding[]> {\n\tconst findings: ConformanceFinding[] = []\n\tfor await (const finding of driverFindings(factory)) findings.push(finding)\n\treturn findings\n}\n","import type { TransactionScope } from './TransactionScope.js'\n\n/**\n * The internal continuation boundary for one transaction-scoped async iterable.\n *\n * @remarks\n * Each active continuation enters the owning transaction ledger independently,\n * so an idle iterator never pins settlement. A continuation requested after\n * admission closes rejects while still attempting source cleanup exactly once.\n */\nexport class TransactionIterator<T> implements AsyncIterableIterator<T> {\n\treadonly #source: AsyncIterator<T>\n\treadonly #scope: TransactionScope\n\t#cleaned = false\n\n\tconstructor(source: AsyncIterable<T>, scope: TransactionScope) {\n\t\tthis.#source = source[Symbol.asyncIterator]()\n\t\tthis.#scope = scope\n\t}\n\n\t[Symbol.asyncIterator](): AsyncIterableIterator<T> {\n\t\treturn this\n\t}\n\n\tnext(): Promise<IteratorResult<T>> {\n\t\treturn this.#continue(() => this.#next())\n\t}\n\n\treturn(): Promise<IteratorResult<T>> {\n\t\treturn this.#continue(() => this.#return())\n\t}\n\n\tthrow(error?: unknown): Promise<IteratorResult<T>> {\n\t\treturn this.#continue(() => this.#throw(error))\n\t}\n\n\tasync #next(): Promise<IteratorResult<T>> {\n\t\tif (this.#cleaned) return { done: true, value: undefined }\n\t\tconst result = await this.#source.next()\n\t\tif (result.done === true) this.#cleaned = true\n\t\treturn result\n\t}\n\n\tasync #return(): Promise<IteratorResult<T>> {\n\t\tif (this.#cleaned || this.#source.return === undefined) {\n\t\t\tthis.#cleaned = true\n\t\t\treturn { done: true, value: undefined }\n\t\t}\n\t\tthis.#cleaned = true\n\t\treturn this.#source.return()\n\t}\n\n\tasync #throw(error: unknown): Promise<IteratorResult<T>> {\n\t\tif (this.#source.throw !== undefined) {\n\t\t\tconst result = await this.#source.throw(error)\n\t\t\tif (result.done === true) this.#cleaned = true\n\t\t\treturn result\n\t\t}\n\t\ttry {\n\t\t\tawait this.#return()\n\t\t} catch {}\n\t\tthrow error\n\t}\n\n\t#continue<R>(operation: () => Promise<R>): Promise<R> {\n\t\tif (!this.#scope.accepting) this.#cleanup()\n\t\treturn this.#scope.track(operation)\n\t}\n\n\t#cleanup(): void {\n\t\tif (this.#cleaned) return\n\t\tthis.#cleaned = true\n\t\ttry {\n\t\t\tconst cleanup = this.#source.return?.()\n\t\t\tcleanup?.catch(() => {})\n\t\t} catch {}\n\t}\n}\n","import { DatabaseError } from './errors.js'\nimport { TransactionIterator } from './TransactionIterator.js'\n\n/**\n * The internal lifetime boundary for one database transaction callback.\n *\n * @remarks\n * Promise operations enter synchronously through {@link track}. Closing stops new\n * work while {@link drain} contains every operation already accepted, including\n * work the callback started without awaiting. {@link stream} applies the same\n * boundary to each iterator continuation without retaining an idle iterator.\n */\nexport class TransactionScope {\n\treadonly #operations = new Set<Promise<unknown>>()\n\t#accepting = true\n\t#failed = false\n\t#error: unknown\n\n\tget accepting(): boolean {\n\t\treturn this.#accepting\n\t}\n\n\tcheck(): void {\n\t\tif (!this.#accepting) {\n\t\t\tthrow new DatabaseError('CONFLICT', 'Transaction scope has settled')\n\t\t}\n\t}\n\n\ttrack<R>(operation: () => Promise<R>): Promise<R> {\n\t\ttry {\n\t\t\tthis.check()\n\t\t} catch (error) {\n\t\t\treturn Promise.reject(error)\n\t\t}\n\t\tlet promise: Promise<R>\n\t\ttry {\n\t\t\tpromise = operation()\n\t\t} catch (error) {\n\t\t\tpromise = Promise.reject(error)\n\t\t}\n\t\tthis.#operations.add(promise)\n\t\tpromise.then(\n\t\t\t() => {\n\t\t\t\tthis.#operations.delete(promise)\n\t\t\t},\n\t\t\t(error: unknown) => {\n\t\t\t\tthis.#operations.delete(promise)\n\t\t\t\tif (!this.#failed) {\n\t\t\t\t\tthis.#failed = true\n\t\t\t\t\tthis.#error = error\n\t\t\t\t}\n\t\t\t},\n\t\t)\n\t\treturn promise\n\t}\n\n\tstream<T>(source: AsyncIterable<T>): AsyncIterable<T> {\n\t\treturn new TransactionIterator(source, this)\n\t}\n\n\tstop(): void {\n\t\tthis.#accepting = false\n\t}\n\n\tasync drain(): Promise<void> {\n\t\twhile (this.#operations.size > 0) {\n\t\t\tawait Promise.allSettled(this.#operations)\n\t\t}\n\t\tif (this.#failed) throw this.#error\n\t}\n}\n","import type { Result } from '@orkestrel/contract'\nimport type { EmitterErrorHandler, EmitterInterface } from '@orkestrel/emitter'\nimport type {\n\tDatabaseEventMap,\n\tDatabaseOptions,\n\tDatabaseStatus,\n\tDriverInterface,\n\tDriverMetadata,\n\tMigration,\n\tMigrationInput,\n\tOperationOptions,\n\tStorageInterface,\n\tTableSchema,\n} from './types.js'\nimport { Emitter } from '@orkestrel/emitter'\nimport { DatabaseError } from './errors.js'\nimport { checkAbort, equalsValue, normalizeDriverSchema, planMigration } from './helpers.js'\nimport { TransactionScope } from './TransactionScope.js'\n\n/**\n * The internal shared owner behind every typed view of one database.\n *\n * @remarks\n * A context owns the driver, merged physical schema, lifecycle, observation,\n * migration, and single transaction admission. It is deliberately omitted from\n * the public barrel; {@link Database} is the consumer-facing typed view.\n */\nexport class DatabaseContext {\n\treadonly #driver: DriverInterface\n\treadonly #name: string\n\treadonly #version: number | undefined\n\treadonly #error: EmitterErrorHandler | undefined\n\treadonly #emitter: Emitter<DatabaseEventMap>\n\treadonly #operations = new Set<Promise<unknown>>()\n\t#schema: readonly TableSchema[] = []\n\t#transaction: object | undefined\n\t#status: DatabaseStatus = 'idle'\n\t#ready: Promise<void> | undefined\n\t#failure: { readonly error: unknown } | undefined\n\n\tconstructor(options: DatabaseOptions) {\n\t\tthis.#driver = options.driver\n\t\tthis.#name = options.name ?? 'database'\n\t\tthis.#version = options.version\n\t\tthis.#error = options.error\n\t\tthis.#emitter = new Emitter<DatabaseEventMap>({\n\t\t\t...(options.on === undefined ? {} : { on: options.on }),\n\t\t\t...(options.error === undefined ? {} : { error: options.error }),\n\t\t})\n\t}\n\n\tget driver(): DriverInterface {\n\t\treturn this.#driver\n\t}\n\n\tget emitter(): EmitterInterface<DatabaseEventMap> {\n\t\treturn this.#emitter\n\t}\n\n\tget error(): EmitterErrorHandler | undefined {\n\t\treturn this.#error\n\t}\n\n\tget name(): string {\n\t\treturn this.#name\n\t}\n\n\tget accepting(): boolean {\n\t\treturn this.#status !== 'closed' && this.#transaction === undefined\n\t}\n\n\tget status(): DatabaseStatus {\n\t\treturn this.#status\n\t}\n\n\tget version(): number | undefined {\n\t\treturn this.#version\n\t}\n\n\tregister(schema: readonly TableSchema[]): void {\n\t\tif (this.#status === 'closed') {\n\t\t\tthrow new DatabaseError('CLOSED', `Database '${this.#name}' is closed`, {\n\t\t\t\tname: this.#name,\n\t\t\t})\n\t\t}\n\t\tif (this.#ready !== undefined || this.#transaction !== undefined) {\n\t\t\tthrow new DatabaseError(\n\t\t\t\t'CONFLICT',\n\t\t\t\t`Database '${this.#name}' cannot import tables after opening has started`,\n\t\t\t\t{ name: this.#name, status: this.#status },\n\t\t\t)\n\t\t}\n\t\tconst registered = normalizeDriverSchema(schema)\n\t\tconst merged = [...this.#schema]\n\t\tfor (const table of registered) {\n\t\t\tconst existing = merged.find((candidate) => candidate.name === table.name)\n\t\t\tif (existing === undefined) {\n\t\t\t\tmerged.push(table)\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif (!equalsValue(existing, table)) {\n\t\t\t\tthrow new DatabaseError(\n\t\t\t\t\t'VALIDATION',\n\t\t\t\t\t`Table '${table.name}' conflicts with its registered schema`,\n\t\t\t\t\t{ table: table.name },\n\t\t\t\t)\n\t\t\t}\n\t\t}\n\t\tthis.#schema = normalizeDriverSchema(merged)\n\t}\n\n\tasync open(): Promise<void> {\n\t\tthis.#outside()\n\t\tawait this.connect()\n\t}\n\n\tasync close(): Promise<void> {\n\t\tthis.#outside()\n\t\tif (this.#status === 'closed') return\n\t\tthis.#status = 'closed'\n\t\tawait this.#drain()\n\t\tconst ready = this.#ready\n\t\tif (ready !== undefined) await ready.catch(() => {})\n\t\tthis.#ready = undefined\n\t\tawait this.#driver.close()\n\t\tthis.#emitter.emit('close')\n\t}\n\n\tconnect(): Promise<void> {\n\t\treturn this.#connect()\n\t}\n\n\ttrack<R>(operation: () => Promise<R>): Promise<R> {\n\t\ttry {\n\t\t\tthis.#admit()\n\t\t} catch (error) {\n\t\t\treturn Promise.reject(error)\n\t\t}\n\t\tlet promise: Promise<R>\n\t\ttry {\n\t\t\tpromise = operation()\n\t\t} catch (error) {\n\t\t\tpromise = Promise.reject(error)\n\t\t}\n\t\tthis.#operations.add(promise)\n\t\tpromise.then(\n\t\t\t() => {\n\t\t\t\tthis.#operations.delete(promise)\n\t\t\t},\n\t\t\t() => {\n\t\t\t\tthis.#operations.delete(promise)\n\t\t\t},\n\t\t)\n\t\treturn promise\n\t}\n\n\tasync transaction<R>(\n\t\tscope: (storage: StorageInterface, lifetime: TransactionScope) => Promise<Result<R, unknown>>,\n\t\toptions?: OperationOptions,\n\t): Promise<R> {\n\t\tcheckAbort(options?.signal)\n\t\tthis.#admit()\n\t\tconst token = {}\n\t\tthis.#transaction = token\n\t\ttry {\n\t\t\tawait this.#drain()\n\t\t\tawait this.#connect()\n\t\t\tif (this.#driver.transaction !== undefined) {\n\t\t\t\tconst rejection: { rejected: boolean; error: unknown; marker: object } = {\n\t\t\t\t\trejected: false,\n\t\t\t\t\terror: undefined,\n\t\t\t\t\tmarker: {},\n\t\t\t\t}\n\t\t\t\ttry {\n\t\t\t\t\tconst value = await this.#driver.transaction(async (storage) => {\n\t\t\t\t\t\tthis.#emitter.emit('transaction')\n\t\t\t\t\t\tconst outcome = await scope(storage, new TransactionScope())\n\t\t\t\t\t\tif (!outcome.success) {\n\t\t\t\t\t\t\trejection.rejected = true\n\t\t\t\t\t\t\trejection.error = outcome.error\n\t\t\t\t\t\t\tthrow rejection.marker\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn outcome.value\n\t\t\t\t\t})\n\t\t\t\t\tthis.#emitter.emit('commit')\n\t\t\t\t\treturn value\n\t\t\t\t} catch (error) {\n\t\t\t\t\tif (rejection.rejected) {\n\t\t\t\t\t\tif (Object.is(error, rejection.marker)) {\n\t\t\t\t\t\t\tthis.#emitter.emit('rollback', rejection.error)\n\t\t\t\t\t\t\tthrow rejection.error\n\t\t\t\t\t\t}\n\t\t\t\t\t\tthrow this.#rollbackError(rejection.error, error)\n\t\t\t\t\t}\n\t\t\t\t\tthrow error\n\t\t\t\t}\n\t\t\t}\n\t\t\tconst rollback = await this.#driver.snapshot()\n\t\t\tthis.#emitter.emit('transaction')\n\t\t\tconst outcome = await scope(this.#driver, new TransactionScope())\n\t\t\tif (outcome.success) {\n\t\t\t\tthis.#emitter.emit('commit')\n\t\t\t\treturn outcome.value\n\t\t\t}\n\t\t\ttry {\n\t\t\t\tawait rollback()\n\t\t\t} catch (cause) {\n\t\t\t\tthrow this.#rollbackError(outcome.error, cause)\n\t\t\t}\n\t\t\tthis.#emitter.emit('rollback', outcome.error)\n\t\t\tthrow outcome.error\n\t\t} finally {\n\t\t\tif (this.#transaction === token) this.#transaction = undefined\n\t\t}\n\t}\n\n\tasync migrate(deployed: readonly TableSchema[], options?: OperationOptions): Promise<Migration> {\n\t\tcheckAbort(options?.signal)\n\t\tthis.#outside()\n\t\tif (this.#ready !== undefined || this.#status !== 'idle') {\n\t\t\tthrow new DatabaseError(\n\t\t\t\t'CONFLICT',\n\t\t\t\t`Database '${this.#name}' cannot apply an explicit deployed schema after opening`,\n\t\t\t\t{ name: this.#name, status: this.#status },\n\t\t\t)\n\t\t}\n\t\tif (this.#driver.migrate === undefined) {\n\t\t\tthrow new DatabaseError(\n\t\t\t\t'MIGRATION',\n\t\t\t\t`Database '${this.#name}' driver does not support migration`,\n\t\t\t\t{ name: this.#name },\n\t\t\t)\n\t\t}\n\t\tconst plan = planMigration(deployed, this.#schema)\n\t\tconst readiness = this.#transition(deployed, plan).catch((error: unknown) => {\n\t\t\tif (this.#ready === readiness) this.#ready = undefined\n\t\t\tthis.#failure = { error }\n\t\t\tthrow error\n\t\t})\n\t\tthis.#failure = undefined\n\t\tthis.#ready = readiness\n\t\tawait readiness\n\t\treturn plan\n\t}\n\n\t#admit(): void {\n\t\tif (this.#status === 'closed') {\n\t\t\tthrow new DatabaseError('CLOSED', `Database '${this.#name}' is closed`, {\n\t\t\t\tname: this.#name,\n\t\t\t})\n\t\t}\n\t\tif (this.#transaction !== undefined) {\n\t\t\tthrow new DatabaseError('CONFLICT', `Database '${this.#name}' has an active transaction`, {\n\t\t\t\tname: this.#name,\n\t\t\t})\n\t\t}\n\t\tif (this.#failure !== undefined) throw this.#failure.error\n\t}\n\n\t#outside(): void {\n\t\tif (this.#transaction !== undefined) {\n\t\t\tthrow new DatabaseError('CONFLICT', `Database '${this.#name}' has an active transaction`, {\n\t\t\t\tname: this.#name,\n\t\t\t})\n\t\t}\n\t}\n\n\t#connect(): Promise<void> {\n\t\tif (this.#ready !== undefined) return this.#ready\n\t\tif (this.#failure !== undefined) throw this.#failure.error\n\t\tif (this.#status === 'closed') {\n\t\t\tthrow new DatabaseError('CLOSED', `Database '${this.#name}' is closed`, {\n\t\t\t\tname: this.#name,\n\t\t\t})\n\t\t}\n\t\tconst readiness = this.#driver\n\t\t\t.open(this.#schema)\n\t\t\t.then(async () => {\n\t\t\t\tif (this.#status === 'idle') {\n\t\t\t\t\tthis.#status = 'open'\n\t\t\t\t\tthis.#emitter.emit('open')\n\t\t\t\t}\n\t\t\t\tawait this.#reconcile()\n\t\t\t})\n\t\t\t.catch((error: unknown) => {\n\t\t\t\tif (this.#ready === readiness) this.#ready = undefined\n\t\t\t\tthrow error\n\t\t\t})\n\t\tthis.#ready = readiness\n\t\treturn readiness\n\t}\n\n\tasync #drain(): Promise<void> {\n\t\tif (this.#operations.size === 0) return\n\t\tawait Promise.allSettled(this.#operations)\n\t}\n\n\tasync #reconcile(): Promise<void> {\n\t\tif (\n\t\t\tthis.#version === undefined ||\n\t\t\tthis.#driver.metadata === undefined ||\n\t\t\tthis.#driver.stamp === undefined\n\t\t) {\n\t\t\treturn\n\t\t}\n\t\tconst metadata = await this.#driver.metadata()\n\t\tif (metadata === undefined) {\n\t\t\tawait this.#stamp()\n\t\t\treturn\n\t\t}\n\t\tif (metadata.version > this.#version) {\n\t\t\tthrow new DatabaseError(\n\t\t\t\t'MIGRATION',\n\t\t\t\t`Database '${this.#name}' store version ${metadata.version} is newer than declared version ${this.#version}`,\n\t\t\t\t{ name: this.#name, stored: metadata.version, declared: this.#version },\n\t\t\t)\n\t\t}\n\t\tif (metadata.version === this.#version) {\n\t\t\tif (!equalsValue(normalizeDriverSchema(metadata.schema), this.#schema)) {\n\t\t\t\tthrow new DatabaseError(\n\t\t\t\t\t'MIGRATION',\n\t\t\t\t\t`Database '${this.#name}' stored schema differs at version ${this.#version}`,\n\t\t\t\t\t{ name: this.#name, version: this.#version },\n\t\t\t\t)\n\t\t\t}\n\t\t\treturn\n\t\t}\n\t\tconst plan = planMigration(metadata.schema, this.#schema, metadata.version, this.#version)\n\t\tif (plan.steps.length > 0 && this.#driver.migrate === undefined) {\n\t\t\tthrow new DatabaseError(\n\t\t\t\t'MIGRATION',\n\t\t\t\t`Database '${this.#name}' driver does not support migration`,\n\t\t\t\t{ name: this.#name, stored: metadata.version, declared: this.#version },\n\t\t\t)\n\t\t}\n\t\tawait this.#apply(plan)\n\t}\n\n\tasync #apply(plan: Migration): Promise<void> {\n\t\tif (this.#driver.transaction !== undefined) {\n\t\t\tawait this.#driver.transaction(async (storage) => {\n\t\t\t\tif (plan.steps.length > 0 && storage.migrate === undefined) {\n\t\t\t\t\tthrow new DatabaseError(\n\t\t\t\t\t\t'MIGRATION',\n\t\t\t\t\t\t`Database '${this.#name}' transaction does not support migration`,\n\t\t\t\t\t\t{ name: this.#name },\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t\tif (storage.migrate === undefined) await this.#stamp(storage)\n\t\t\t\telse await storage.migrate(this.#migration(plan))\n\t\t\t})\n\t\t\tthis.#emitter.emit('migrate', plan)\n\t\t\treturn\n\t\t}\n\t\tif (this.#driver.migrate === undefined) await this.#stamp()\n\t\telse await this.#driver.migrate(this.#migration(plan))\n\t\tthis.#emitter.emit('migrate', plan)\n\t}\n\n\tasync #transition(deployed: readonly TableSchema[], plan: Migration): Promise<void> {\n\t\tawait this.#driver.open(deployed)\n\t\tawait this.#apply(plan)\n\t\tif (this.#status === 'idle') {\n\t\t\tthis.#status = 'open'\n\t\t\tthis.#emitter.emit('open')\n\t\t}\n\t}\n\n\tasync #stamp(storage?: StorageInterface): Promise<void> {\n\t\tconst target = storage ?? this.#driver\n\t\tif (this.#version === undefined || target.stamp === undefined) return\n\t\tconst metadata: DriverMetadata = {\n\t\t\tversion: this.#version,\n\t\t\tschema: this.#schema,\n\t\t}\n\t\tawait target.stamp(metadata)\n\t}\n\n\t#migration(plan: Migration): MigrationInput {\n\t\tif (this.#version === undefined) return { plan }\n\t\treturn {\n\t\t\tplan,\n\t\t\tmetadata: { version: this.#version, schema: this.#schema },\n\t\t}\n\t}\n\n\t#rollbackError(transaction: unknown, cause: unknown): DatabaseError {\n\t\treturn new DatabaseError('DRIVER', `Database '${this.#name}' rollback failed`, {\n\t\t\tcause,\n\t\t\ttransaction,\n\t\t})\n\t}\n}\n","import type { CursorInterface, Key } from './types.js'\n\n/**\n * A forward row cursor for bulk in-place mutation.\n *\n * @remarks\n * Iterates a snapshot of the table's keys captured when the cursor was opened,\n * reading each row lazily through the owning table — so a mutation made during\n * iteration cannot corrupt the walk, and a key removed mid-iteration is simply\n * skipped. `update` and `remove` act on the row at the current position.\n */\nexport class Cursor<T = Record<string, unknown>> implements CursorInterface<T> {\n\treadonly #keys: readonly Key[]\n\treadonly #read: (key: Key) => Promise<T | undefined>\n\treadonly #update: (key: Key, changes: Partial<T>) => Promise<boolean>\n\treadonly #remove: (key: Key) => Promise<boolean>\n\treadonly #track: <R>(operation: () => Promise<R>) => Promise<R>\n\t#tail = Promise.resolve()\n\t#index = -1\n\t#value: T | undefined\n\t#closed = false\n\n\tconstructor(\n\t\tkeys: readonly Key[],\n\t\tread: (key: Key) => Promise<T | undefined>,\n\t\tupdate: (key: Key, changes: Partial<T>) => Promise<boolean>,\n\t\tremove: (key: Key) => Promise<boolean>,\n\t\ttrack: <R>(operation: () => Promise<R>) => Promise<R>,\n\t) {\n\t\tthis.#keys = keys\n\t\tthis.#read = read\n\t\tthis.#update = update\n\t\tthis.#remove = remove\n\t\tthis.#track = track\n\t}\n\n\tget value(): T | undefined {\n\t\treturn this.#value\n\t}\n\n\tget index(): number {\n\t\treturn this.#index\n\t}\n\n\tget done(): boolean {\n\t\treturn this.#closed || this.#index >= this.#keys.length\n\t}\n\n\tnext(): Promise<void> {\n\t\treturn this.#track(() => this.#queue(() => this.#advance()))\n\t}\n\n\tupdate(changes: Partial<T>): Promise<void> {\n\t\treturn this.#track(() => this.#queue(() => this.#revise(changes)))\n\t}\n\n\tremove(): Promise<void> {\n\t\treturn this.#track(() => this.#queue(() => this.#delete()))\n\t}\n\n\tclose(): void {\n\t\tthis.#closed = true\n\t\tthis.#value = undefined\n\t}\n\n\tasync #advance(): Promise<void> {\n\t\tif (this.#closed) return\n\t\tthis.#index += 1\n\t\twhile (this.#index < this.#keys.length) {\n\t\t\tif (this.#closed) return\n\t\t\tconst key = this.#keys[this.#index]\n\t\t\tif (key === undefined) {\n\t\t\t\tthis.#index += 1\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tconst row = await this.#read(key)\n\t\t\tif (this.#closed) return\n\t\t\tif (row !== undefined) {\n\t\t\t\tthis.#value = row\n\t\t\t\treturn\n\t\t\t}\n\t\t\tthis.#index += 1\n\t\t}\n\t\tthis.#value = undefined\n\t}\n\n\tasync #revise(changes: Partial<T>): Promise<void> {\n\t\tif (this.#closed || this.#value === undefined) return\n\t\tconst key = this.#keys[this.#index]\n\t\tif (key === undefined) return\n\t\tawait this.#update(key, changes)\n\t\tif (this.#closed) return\n\t\tconst row = await this.#read(key)\n\t\tif (this.#closed) return\n\t\tthis.#value = row\n\t}\n\n\tasync #delete(): Promise<void> {\n\t\tif (this.#closed || this.#value === undefined) return\n\t\tconst key = this.#keys[this.#index]\n\t\tif (key === undefined) return\n\t\tawait this.#remove(key)\n\t\tif (this.#closed) return\n\t\tthis.#value = undefined\n\t}\n\n\t#queue(operation: () => Promise<void>): Promise<void> {\n\t\tconst result = this.#tail.then(operation)\n\t\tthis.#tail = result.then(\n\t\t\t() => undefined,\n\t\t\t() => undefined,\n\t\t)\n\t\treturn result\n\t}\n}\n","import type { DatabaseContext } from './DatabaseContext.js'\n\n/**\n * The internal continuation admission boundary for a root database stream.\n *\n * @remarks\n * Each continuation enters the shared root operation ledger independently, so\n * an idle iterator never delays a transaction or close. A continuation rejected\n * after transaction or close admission closes attempts source cleanup exactly\n * once and leaves the iterator terminal.\n */\nexport class DatabaseIterator<T> implements AsyncIterableIterator<T> {\n\treadonly #source: AsyncIterator<T>\n\treadonly #context: DatabaseContext\n\t#cleaned = false\n\n\tconstructor(source: AsyncIterable<T>, context: DatabaseContext) {\n\t\tthis.#source = source[Symbol.asyncIterator]()\n\t\tthis.#context = context\n\t}\n\n\t[Symbol.asyncIterator](): AsyncIterableIterator<T> {\n\t\treturn this\n\t}\n\n\tnext(): Promise<IteratorResult<T>> {\n\t\treturn this.#continue(() => this.#next())\n\t}\n\n\treturn(): Promise<IteratorResult<T>> {\n\t\treturn this.#continue(() => this.#return())\n\t}\n\n\tthrow(error?: unknown): Promise<IteratorResult<T>> {\n\t\treturn this.#continue(() => this.#throw(error))\n\t}\n\n\tasync #next(): Promise<IteratorResult<T>> {\n\t\tif (this.#cleaned) return { done: true, value: undefined }\n\t\tawait this.#context.connect()\n\t\tconst result = await this.#source.next()\n\t\tif (result.done === true) this.#cleaned = true\n\t\treturn result\n\t}\n\n\tasync #return(): Promise<IteratorResult<T>> {\n\t\tif (this.#cleaned || this.#source.return === undefined) {\n\t\t\tthis.#cleaned = true\n\t\t\treturn { done: true, value: undefined }\n\t\t}\n\t\tthis.#cleaned = true\n\t\treturn this.#source.return()\n\t}\n\n\tasync #throw(error: unknown): Promise<IteratorResult<T>> {\n\t\tif (this.#source.throw !== undefined) {\n\t\t\tconst result = await this.#source.throw(error)\n\t\t\tif (result.done === true) this.#cleaned = true\n\t\t\treturn result\n\t\t}\n\t\ttry {\n\t\t\tawait this.#return()\n\t\t} catch {}\n\t\tthrow error\n\t}\n\n\t#continue<R>(operation: () => Promise<R>): Promise<R> {\n\t\tif (!this.#context.accepting) this.#cleanup()\n\t\treturn this.#context.track(operation)\n\t}\n\n\t#cleanup(): void {\n\t\tif (this.#cleaned) return\n\t\tthis.#cleaned = true\n\t\ttry {\n\t\t\tconst cleanup = this.#source.return?.()\n\t\t\tcleanup?.catch(() => {})\n\t\t} catch {}\n\t}\n}\n","import type { FieldPath } from '@orkestrel/contract'\nimport type {\n\tAggregateOperation,\n\tCondition,\n\tOperationOptions,\n\tOrder,\n\tQueryInterface,\n\tTableInterface,\n} from './types.js'\nimport { computeAggregate } from './helpers.js'\nimport { validatePage } from './validators.js'\n\n/**\n * A fluent query builder bound to one table.\n *\n * @remarks\n * Accumulates typed conditions, ordering, JS filters, and a page. Each builder\n * method mutates and returns the same instance. Portable inputs flow to the\n * table, while predicates remain an in-memory refinement.\n */\nexport class Query<T = Record<string, unknown>> implements QueryInterface<T> {\n\treadonly #table: TableInterface<T>\n\treadonly #conditions: Condition[] = []\n\treadonly #orders: Order[] = []\n\treadonly #filters: Array<(row: T) => boolean> = []\n\t#limit: number | undefined\n\t#offset: number | undefined\n\n\tconstructor(table: TableInterface<T>) {\n\t\tthis.#table = table\n\t}\n\n\tcondition(input: Condition): QueryInterface<T> {\n\t\tthis.#conditions.push(input)\n\t\treturn this\n\t}\n\n\torder(input: Order): QueryInterface<T> {\n\t\tthis.#orders.push(input)\n\t\treturn this\n\t}\n\n\tfilter(predicate: (row: T) => boolean): QueryInterface<T> {\n\t\tthis.#filters.push(predicate)\n\t\treturn this\n\t}\n\n\tlimit(count: number): QueryInterface<T> {\n\t\tvalidatePage({ limit: count })\n\t\tthis.#limit = count\n\t\treturn this\n\t}\n\n\toffset(count: number): QueryInterface<T> {\n\t\tvalidatePage({ offset: count })\n\t\tthis.#offset = count\n\t\treturn this\n\t}\n\n\tasync collect(): Promise<readonly T[]> {\n\t\tif (this.#filters.length === 0) {\n\t\t\treturn this.#table.records({\n\t\t\t\tconditions: this.#conditions,\n\t\t\t\torder: this.#orders,\n\t\t\t\t...(this.#limit !== undefined ? { limit: this.#limit } : {}),\n\t\t\t\t...(this.#offset !== undefined ? { offset: this.#offset } : {}),\n\t\t\t})\n\t\t}\n\t\tconst fetched = await this.#table.records({\n\t\t\tconditions: this.#conditions,\n\t\t\torder: this.#orders,\n\t\t})\n\t\treturn this.#page(this.#filtered(fetched))\n\t}\n\n\tasync find(): Promise<T | undefined> {\n\t\tconst rows = await this.collect()\n\t\treturn rows[0]\n\t}\n\n\tasync count(): Promise<number> {\n\t\tif (this.#filters.length === 0) {\n\t\t\treturn this.#table.count({ conditions: this.#conditions })\n\t\t}\n\t\tconst fetched = await this.#table.records({ conditions: this.#conditions })\n\t\treturn this.#filtered(fetched).length\n\t}\n\n\t/**\n\t * Lazily evaluate conditions, filters, offset, and limit.\n\t *\n\t * @param options - Optional abort options\n\t * @returns Matching rows in storage order\n\t */\n\tasync *stream(options?: OperationOptions): AsyncIterable<T> {\n\t\tif (this.#filters.length === 0) {\n\t\t\tyield* this.#table.scan(\n\t\t\t\t{\n\t\t\t\t\tconditions: this.#conditions,\n\t\t\t\t\t...(this.#limit !== undefined ? { limit: this.#limit } : {}),\n\t\t\t\t\t...(this.#offset !== undefined ? { offset: this.#offset } : {}),\n\t\t\t\t},\n\t\t\t\toptions,\n\t\t\t)\n\t\t\treturn\n\t\t}\n\t\tconst offset = this.#offset ?? 0\n\t\tlet matched = 0\n\t\tlet yielded = 0\n\t\tfor await (const row of this.#table.scan({ conditions: this.#conditions }, options)) {\n\t\t\tif (this.#limit !== undefined && yielded >= this.#limit) break\n\t\t\tlet matches = true\n\t\t\tfor (const predicate of this.#filters) {\n\t\t\t\tif (!predicate(row)) {\n\t\t\t\t\tmatches = false\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (!matches) continue\n\t\t\tif (matched < offset) {\n\t\t\t\tmatched += 1\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tmatched += 1\n\t\t\tyielded += 1\n\t\t\tyield row\n\t\t}\n\t}\n\n\taggregate(operation: AggregateOperation, column: FieldPath): Promise<number | undefined> {\n\t\tif (this.#filters.length === 0) {\n\t\t\treturn this.#table.aggregate(operation, column, {\n\t\t\t\tconditions: this.#conditions,\n\t\t\t})\n\t\t}\n\t\treturn this.#table\n\t\t\t.records({ conditions: this.#conditions })\n\t\t\t.then((fetched) => computeAggregate(this.#filtered(fetched), operation, column))\n\t}\n\n\t#filtered(rows: readonly T[]): readonly T[] {\n\t\tlet result = rows\n\t\tfor (const predicate of this.#filters) result = result.filter(predicate)\n\t\treturn result\n\t}\n\n\t#page(rows: readonly T[]): readonly T[] {\n\t\tconst offset = this.#offset ?? 0\n\t\tif (offset === 0 && this.#limit === undefined) return rows\n\t\treturn rows.slice(offset, this.#limit === undefined ? undefined : offset + this.#limit)\n\t}\n}\n","import type { ContractInterface, FieldPath, Guard } from '@orkestrel/contract'\nimport type { EmitterErrorHandler, EmitterInterface } from '@orkestrel/emitter'\nimport type { DatabaseContext } from './DatabaseContext.js'\nimport type {\n\tAggregateOperation,\n\tQueryInput,\n\tCursorInterface,\n\tKey,\n\tKeyFunction,\n\tQueryInterface,\n\tOperationOptions,\n\tRow,\n\tTableEventMap,\n\tTableInterface,\n\tStorageInterface,\n} from './types.js'\nimport { isArray, isRecord } from '@orkestrel/contract'\nimport { Emitter } from '@orkestrel/emitter'\nimport { DatabaseError } from './errors.js'\nimport {\n\tapplyQuery,\n\tcheckAbort,\n\tcomputeAggregate,\n\tequalsValue,\n\textractKey,\n\tfilterRows,\n\tmatchesQuery,\n} from './helpers.js'\nimport { Cursor } from './Cursor.js'\nimport { DatabaseIterator } from './DatabaseIterator.js'\nimport { Query } from './Query.js'\nimport type { TransactionScope } from './TransactionScope.js'\nimport { validatePage } from './validators.js'\n\n/**\n * A table — typed keyed CRUD plus fluent query and cursor access over a driver.\n *\n * @remarks\n * The table's contract is the load-bearing piece: writes go through `parse`\n * (coercing inputs and rejecting rows that don't fit with a `VALIDATION` throw),\n * reads come back through the contract guard (narrowing a stored {@link Row} to\n * the table's type — no assertion, AGENTS §1), and `contract` is exposed for\n * introspection and seeding. The driver only stores and scans; all querying is\n * the shared core engine in `helpers.ts`.\n *\n * @remarks\n * - **Observable (§13).** The owned {@link emitter} ({@link TableEventMap}) carries the\n * per-row mutation moments — `write` (set / add / update), `remove`, `clear` — for\n * fire-and-forget observers (cache invalidation, sync, an audit log), ALONGSIDE the\n * database-level lifecycle. Events carry the affected KEY only (no value payload, to\n * keep fan-out lean); reads / queries / counts are not emitted. Every event is emitted\n * directly, strictly AFTER the driver write / delete / clear completes; the emitter\n * isolates a listener throw and routes it to its `error` handler (the `error` option),\n * so a buggy observer can never corrupt a write or perturb a transaction.\n */\nexport class Table<T = Row> implements TableInterface<T> {\n\treadonly #ready: () => Promise<void>\n\treadonly #driver: StorageInterface\n\treadonly #name: string\n\treadonly #key: string\n\treadonly #contract: ContractInterface<T>\n\treadonly #guard: Guard<T>\n\treadonly #generate: KeyFunction | undefined\n\treadonly #context: DatabaseContext | undefined\n\treadonly #scope: TransactionScope | undefined\n\t// The PUSH observation surface (§13) — owned, never inherited. The emitter isolates a\n\t// listener throw (routing it to the `error` handler), so it can never escape into a write\n\t// or a transaction.\n\treadonly #emitter: Emitter<TableEventMap>\n\n\tconstructor(\n\t\tready: () => Promise<void>,\n\t\tdriver: StorageInterface,\n\t\tname: string,\n\t\tkey: string,\n\t\tcontract: ContractInterface<T>,\n\t\tgenerate?: KeyFunction,\n\t\terror?: EmitterErrorHandler,\n\t\tcontext?: DatabaseContext,\n\t\tscope?: TransactionScope,\n\t) {\n\t\tthis.#ready = ready\n\t\tthis.#driver = driver\n\t\tthis.#name = name\n\t\tthis.#key = key\n\t\tthis.#contract = contract\n\t\tthis.#guard = contract.is\n\t\tthis.#generate = generate\n\t\tthis.#context = context\n\t\tthis.#scope = scope\n\t\tthis.#emitter = new Emitter<TableEventMap>({\n\t\t\t...(error !== undefined ? { error } : {}),\n\t\t})\n\t}\n\n\tget emitter(): EmitterInterface<TableEventMap> {\n\t\treturn this.#emitter\n\t}\n\n\tget name(): string {\n\t\treturn this.#name\n\t}\n\n\tget primary(): string {\n\t\treturn this.#key\n\t}\n\n\tget contract(): ContractInterface<T> {\n\t\treturn this.#contract\n\t}\n\n\tget(key: Key): Promise<T | undefined>\n\tget(keys: readonly Key[]): Promise<ReadonlyArray<T | undefined>>\n\tget(keys: Key | readonly Key[]): Promise<(T | undefined) | ReadonlyArray<T | undefined>> {\n\t\treturn this.#track(async () => {\n\t\t\tawait this.#ready()\n\t\t\tif (isArray(keys)) return this.#each(keys, (key) => this.#read(key))\n\t\t\treturn this.#read(keys)\n\t\t})\n\t}\n\n\tresolve(key: Key): Promise<T>\n\tresolve(keys: readonly Key[]): Promise<readonly T[]>\n\tresolve(keys: Key | readonly Key[]): Promise<T | readonly T[]> {\n\t\treturn this.#track(async () => {\n\t\t\tawait this.#ready()\n\t\t\tif (isArray(keys)) return this.#each(keys, (key) => this.#resolveOne(key))\n\t\t\treturn this.#resolveOne(keys)\n\t\t})\n\t}\n\n\thas(key: Key): Promise<boolean>\n\thas(keys: readonly Key[]): Promise<readonly boolean[]>\n\thas(keys: Key | readonly Key[]): Promise<boolean | readonly boolean[]> {\n\t\treturn this.#track(async () => {\n\t\t\tawait this.#ready()\n\t\t\tif (isArray(keys)) {\n\t\t\t\treturn this.#each(keys, async (key) => (await this.#read(key)) !== undefined)\n\t\t\t}\n\t\t\treturn (await this.#read(keys)) !== undefined\n\t\t})\n\t}\n\n\tkeys(): Promise<readonly Key[]> {\n\t\treturn this.#track(async () => {\n\t\t\tawait this.#ready()\n\t\t\treturn this.#driver.keys(this.#name)\n\t\t})\n\t}\n\n\tasync records(input?: QueryInput, options?: OperationOptions): Promise<readonly T[]> {\n\t\tvalidatePage(input)\n\t\treturn this.#track(async () => {\n\t\t\tcheckAbort(options?.signal)\n\t\t\tawait this.#ready()\n\t\t\tconst candidate: QueryInput = {\n\t\t\t\t...(input?.conditions === undefined ? {} : { conditions: input.conditions }),\n\t\t\t\t...(input?.order === undefined ? {} : { order: input.order }),\n\t\t\t}\n\t\t\tconst native = await this.#driver.records?.(this.#name, candidate)\n\t\t\tconst source = native ?? applyQuery(await this.#collect(), candidate)\n\t\t\tconst rows: T[] = []\n\t\t\tfor (const row of source) {\n\t\t\t\tif (this.#guard(row)) rows.push(row)\n\t\t\t}\n\t\t\tconst offset = input?.offset ?? 0\n\t\t\tconst limit = input?.limit\n\t\t\treturn rows.slice(offset, limit === undefined ? undefined : offset + limit)\n\t\t})\n\t}\n\n\t/**\n\t * Count contract-valid rows matching `input`'s conditions.\n\t *\n\t * @remarks\n\t * Paging is ignored. Candidate rows use the driver's native `records` hook\n\t * when present, then the table contract guard determines the count so legacy\n\t * invalid rows cannot make `count()` disagree with `records()`.\n\t *\n\t * @param input - Optional conditions to filter by (paging is ignored)\n\t * @param options - `{ signal }` to abort\n\t * @returns The count of matching contract-valid rows\n\t */\n\tasync count(input?: QueryInput, options?: OperationOptions): Promise<number> {\n\t\tvalidatePage(input)\n\t\treturn this.#track(async () => {\n\t\t\tcheckAbort(options?.signal)\n\t\t\tawait this.#ready()\n\t\t\tconst conditions = input?.conditions\n\t\t\tconst candidate: QueryInput = conditions === undefined ? {} : { conditions }\n\t\t\tconst native = await this.#driver.records?.(this.#name, candidate)\n\t\t\tconst rows = native ?? filterRows(await this.#collect(), conditions ?? [])\n\t\t\tlet count = 0\n\t\t\tfor (const row of rows) {\n\t\t\t\tif (this.#guard(row)) count += 1\n\t\t\t}\n\t\t\treturn count\n\t\t})\n\t}\n\n\t/**\n\t * Compute an aggregate over `column` across rows matching `input`'s\n\t * conditions.\n\t *\n\t * @remarks\n\t * Like {@link count}, `aggregate` operates on STORED rows WITHOUT the\n\t * contract guard {@link records} / {@link scan} apply — a non-conforming\n\t * stored row still contributes to the computed aggregate when it matches\n\t * the conditions, even though it would never appear in `records()`'s\n\t * output.\n\t *\n\t * @param operation - The aggregate to compute\n\t * @param column - The column to aggregate\n\t * @param input - Optional conditions to filter by (paging is ignored)\n\t * @param options - `{ signal }` to abort\n\t * @returns The aggregate value, or `undefined` when undefined for the inputs\n\t */\n\tasync aggregate(\n\t\toperation: AggregateOperation,\n\t\tcolumn: FieldPath,\n\t\tinput?: QueryInput,\n\t\toptions?: OperationOptions,\n\t): Promise<number | undefined> {\n\t\tvalidatePage(input)\n\t\treturn this.#track(async () => {\n\t\t\tcheckAbort(options?.signal)\n\t\t\tawait this.#ready()\n\t\t\t// Aggregate over filtered (not paged) rows — native hook, native records, or scan.\n\t\t\tconst conditions = input?.conditions\n\t\t\tconst filter: QueryInput = conditions ? { conditions } : {}\n\t\t\t// `?.()` is `undefined` only when the driver lacks the method; a present\n\t\t\t// hook returns a Promise (whose resolved value may itself be `undefined`).\n\t\t\tconst native = this.#driver.aggregate?.(this.#name, operation, column, filter)\n\t\t\tif (native !== undefined) return native\n\t\t\tconst rows = await this.#driver.records?.(this.#name, filter)\n\t\t\tconst matched = rows ?? filterRows(await this.#collect(), input?.conditions ?? [])\n\t\t\treturn computeAggregate(matched, operation, column)\n\t\t})\n\t}\n\n\t/**\n\t * Stream the table's rows matching `input`, applying offset/limit paging.\n\t *\n\t * @remarks\n\t * `input.limit` counts rows that pass both the input conditions and the\n\t * table's contract guard. Native streams receive only the conditions; this\n\t * table rechecks them, narrows each candidate, and applies offset/limit last,\n\t * matching {@link records} when storage contains legacy invalid rows.\n\t *\n\t * @param input - Optional conditions plus offset/limit paging\n\t * @param options - `{ signal }` to abort mid-stream\n\t * @returns An async iterable of matching, guard-conforming rows\n\t */\n\tscan(input?: QueryInput, options?: OperationOptions): AsyncIterable<T> {\n\t\tvalidatePage(input)\n\t\tconst source = this.#scan(input, options)\n\t\tif (this.#context !== undefined) return new DatabaseIterator(source, this.#context)\n\t\treturn this.#scope === undefined ? source : this.#scope.stream(source)\n\t}\n\n\tset(row: T, options?: OperationOptions): Promise<Key>\n\tset(rows: readonly T[], options?: OperationOptions): Promise<readonly Key[]>\n\tset(rows: T | readonly T[], options?: OperationOptions): Promise<Key | readonly Key[]> {\n\t\treturn this.#track(async () => {\n\t\t\tawait this.#wait(options?.signal)\n\t\t\tif (isArray(rows)) {\n\t\t\t\treturn this.#each(rows, (row) => this.#put(row, false, options), options?.signal)\n\t\t\t}\n\t\t\treturn this.#put(rows, false, options)\n\t\t})\n\t}\n\n\tadd(row: T, options?: OperationOptions): Promise<Key>\n\tadd(rows: readonly T[], options?: OperationOptions): Promise<readonly Key[]>\n\tadd(rows: T | readonly T[], options?: OperationOptions): Promise<Key | readonly Key[]> {\n\t\treturn this.#track(async () => {\n\t\t\tawait this.#wait(options?.signal)\n\t\t\tif (isArray(rows)) {\n\t\t\t\treturn this.#each(rows, (row) => this.#put(row, true, options), options?.signal)\n\t\t\t}\n\t\t\treturn this.#put(rows, true, options)\n\t\t})\n\t}\n\n\tupdate(key: Key, changes: Partial<T>, options?: OperationOptions): Promise<boolean>\n\tupdate(\n\t\tkeys: readonly Key[],\n\t\tchanges: Partial<T>,\n\t\toptions?: OperationOptions,\n\t): Promise<readonly boolean[]>\n\tupdate(\n\t\tkeys: Key | readonly Key[],\n\t\tchanges: Partial<T>,\n\t\toptions?: OperationOptions,\n\t): Promise<boolean | readonly boolean[]> {\n\t\treturn this.#track(async () => {\n\t\t\tawait this.#wait(options?.signal)\n\t\t\tif (isArray(keys)) {\n\t\t\t\treturn this.#each(keys, (key) => this.#updateOne(key, changes, options), options?.signal)\n\t\t\t}\n\t\t\treturn this.#updateOne(keys, changes, options)\n\t\t})\n\t}\n\n\tremove(key: Key, options?: OperationOptions): Promise<boolean>\n\tremove(keys: readonly Key[], options?: OperationOptions): Promise<readonly boolean[]>\n\tremove(\n\t\tkeys: Key | readonly Key[],\n\t\toptions?: OperationOptions,\n\t): Promise<boolean | readonly boolean[]> {\n\t\treturn this.#track(async () => {\n\t\t\tawait this.#wait(options?.signal)\n\t\t\tif (isArray(keys)) {\n\t\t\t\treturn this.#each(keys, (key) => this.#delete(key, options), options?.signal)\n\t\t\t}\n\t\t\treturn this.#delete(keys, options)\n\t\t})\n\t}\n\n\tclear(): Promise<void> {\n\t\treturn this.#track(async () => {\n\t\t\tawait this.#ready()\n\t\t\tawait this.#driver.clear(this.#name)\n\t\t\t// Observe the cleared table — AFTER the driver emptied it, so a swallowed listener\n\t\t\t// throw can never alter the clear (no value payload — `clear` is a pure signal).\n\t\t\tthis.#emitter.emit('clear')\n\t\t})\n\t}\n\n\tquery(): QueryInterface<T> {\n\t\treturn new Query<T>(this)\n\t}\n\n\tcursor(): Promise<CursorInterface<T>> {\n\t\treturn this.#track(async () => {\n\t\t\tawait this.#ready()\n\t\t\tlet initializing = true\n\t\t\tconst cursor = new Cursor<T>(\n\t\t\t\tawait this.#driver.keys(this.#name),\n\t\t\t\t(key) => this.#readCursor(key),\n\t\t\t\t(key, changes) => this.#updateCursor(key, changes),\n\t\t\t\t(key) => this.#deleteCursor(key),\n\t\t\t\t(operation) => (initializing ? operation() : this.#track(operation)),\n\t\t\t)\n\t\t\tawait cursor.next()\n\t\t\tinitializing = false\n\t\t\treturn cursor\n\t\t})\n\t}\n\n\tasync *#scan(input?: QueryInput, options?: OperationOptions): AsyncIterable<T> {\n\t\tcheckAbort(options?.signal)\n\t\tawait this.#ready()\n\t\tconst conditions = input?.conditions\n\t\tconst offset = input?.offset ?? 0\n\t\tconst limit = input?.limit\n\t\tlet matched = 0\n\t\tlet yielded = 0\n\t\tconst source =\n\t\t\tthis.#driver.stream === undefined\n\t\t\t\t? this.#driver.scan(this.#name)\n\t\t\t\t: this.#driver.stream(this.#name, conditions === undefined ? {} : { conditions })\n\t\tconst iterator = source[Symbol.asyncIterator]()\n\t\ttry {\n\t\t\twhile (true) {\n\t\t\t\tawait this.#ready()\n\t\t\t\tcheckAbort(options?.signal)\n\t\t\t\tif (limit !== undefined && yielded >= limit) return\n\t\t\t\tconst step = await iterator.next()\n\t\t\t\tawait this.#ready()\n\t\t\t\tcheckAbort(options?.signal)\n\t\t\t\tif (step.done === true) return\n\t\t\t\tconst row = step.value\n\t\t\t\tif (conditions !== undefined && conditions.length > 0 && !matchesQuery(row, conditions)) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tconst narrowed = this.#cast(row)\n\t\t\t\tif (narrowed === undefined) continue\n\t\t\t\tif (matched < offset) {\n\t\t\t\t\tmatched += 1\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tmatched += 1\n\t\t\t\tyielded += 1\n\t\t\t\tyield narrowed\n\t\t\t}\n\t\t} finally {\n\t\t\tawait iterator.return?.()\n\t\t}\n\t}\n\n\t// Run a single-item operation across each item in order — the batch overloads\n\t// loop one item at a time (sequential, so writes never race) rather than\n\t// pushing batch logic into the thin driver. `signal` (write batches only) is\n\t// checked before EVERY item, so an abort mid-batch stops before the next\n\t// item runs — already-applied items stay applied (no rollback).\n\tasync #each<I, R>(\n\t\telements: readonly I[],\n\t\toperation: (element: I) => Promise<R>,\n\t\tsignal?: AbortSignal,\n\t): Promise<readonly R[]> {\n\t\tconst results: R[] = []\n\t\tfor (const element of elements) {\n\t\t\tcheckAbort(signal)\n\t\t\tresults.push(await operation(element))\n\t\t}\n\t\treturn results\n\t}\n\n\t// Read and narrow one row (assumes the driver is connected).\n\tasync #read(key: Key): Promise<T | undefined> {\n\t\treturn this.#cast(await this.#driver.read(this.#name, key))\n\t}\n\n\tasync #readCursor(key: Key): Promise<T | undefined> {\n\t\tawait this.#ready()\n\t\treturn this.#read(key)\n\t}\n\n\t// Read one row or throw NOT_FOUND.\n\tasync #resolveOne(key: Key): Promise<T> {\n\t\tconst row = await this.#read(key)\n\t\tif (row === undefined) {\n\t\t\tthrow new DatabaseError('NOT_FOUND', `No row '${key}' in table '${this.#name}'`, {\n\t\t\t\ttable: this.#name,\n\t\t\t\tkey,\n\t\t\t})\n\t\t}\n\t\treturn row\n\t}\n\n\t// Coerce/validate and write one row; `insert` selects the atomic insert primitive.\n\tasync #put(row: T, insert: boolean, options?: OperationOptions): Promise<Key> {\n\t\tconst validated = this.#validate(this.#prepare(row))\n\t\tconst key = this.#resolveKey(validated)\n\t\tif (insert) await this.#driver.insert(this.#name, key, validated, options)\n\t\telse await this.#driver.write(this.#name, key, validated, options)\n\t\t// Observe the written row — AFTER the driver write succeeded; carries the KEY only\n\t\t// (set / add / update all emit one `write`, the consumer re-reads if it needs the\n\t\t// value). A swallowed listener throw can't perturb the write (or its transaction).\n\t\tthis.#emitter.emit('write', key)\n\t\treturn key\n\t}\n\n\t// Merge changes into one existing row and re-validate; `false` when it is absent.\n\tasync #updateOne(key: Key, changes: Partial<T>, options?: OperationOptions): Promise<boolean> {\n\t\tconst existing = await this.#driver.read(this.#name, key)\n\t\tif (existing === undefined) {\n\t\t\tcheckAbort(options?.signal)\n\t\t\treturn false\n\t\t}\n\t\tconst input: unknown = changes\n\t\tif (isRecord(input) && Object.hasOwn(input, this.#key) && !equalsValue(input[this.#key], key)) {\n\t\t\tthrow new DatabaseError(\n\t\t\t\t'VALIDATION',\n\t\t\t\t`Update cannot change primary column '${this.#key}' on table '${this.#name}'`,\n\t\t\t\t{ table: this.#name, column: this.#key, key },\n\t\t\t)\n\t\t}\n\t\tawait this.#driver.write(\n\t\t\tthis.#name,\n\t\t\tkey,\n\t\t\tthis.#validate(Object.assign({}, existing, changes)),\n\t\t\toptions,\n\t\t)\n\t\t// Observe the updated row — AFTER the driver write, and only on the path that wrote\n\t\t// (an absent key returned `false` above, emitting nothing).\n\t\tthis.#emitter.emit('write', key)\n\t\treturn true\n\t}\n\n\tasync #updateCursor(key: Key, changes: Partial<T>): Promise<boolean> {\n\t\tawait this.#wait(undefined)\n\t\treturn this.#updateOne(key, changes)\n\t}\n\n\t// Delete one row, emitting `remove` only when a row was actually removed (a delete of\n\t// an absent key returns `false` and emits nothing) — AFTER the driver delete completes.\n\tasync #delete(key: Key, options?: OperationOptions): Promise<boolean> {\n\t\tconst removed = await this.#driver.delete(this.#name, key, options)\n\t\tif (removed) this.#emitter.emit('remove', key)\n\t\treturn removed\n\t}\n\n\tasync #deleteCursor(key: Key): Promise<boolean> {\n\t\tawait this.#wait(undefined)\n\t\treturn this.#delete(key)\n\t}\n\n\t// Await the shared lazy-open promise without tying its lifetime to this\n\t// mutation. An abort rejects this waiter promptly and consumes the open\n\t// promise's later settlement; the driver still checks the same signal at its\n\t// commit point, so a readiness completion after abort can never dispatch a\n\t// row mutation.\n\tasync #wait(signal: AbortSignal | undefined): Promise<void> {\n\t\tcheckAbort(signal)\n\t\tconst ready = this.#ready()\n\t\tif (signal === undefined) {\n\t\t\tawait ready\n\t\t\treturn\n\t\t}\n\t\tconst cleanup = new AbortController()\n\t\ttry {\n\t\t\tawait new Promise<void>((resolve, reject) => {\n\t\t\t\tsignal.addEventListener(\n\t\t\t\t\t'abort',\n\t\t\t\t\t() => {\n\t\t\t\t\t\tready.catch(() => {})\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\tcheckAbort(signal)\n\t\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\t\treject(error)\n\t\t\t\t\t\t}\n\t\t\t\t\t},\n\t\t\t\t\t{ once: true, signal: cleanup.signal },\n\t\t\t\t)\n\t\t\t\tready.then(resolve, reject)\n\t\t\t\tif (signal.aborted) {\n\t\t\t\t\tready.catch(() => {})\n\t\t\t\t\ttry {\n\t\t\t\t\t\tcheckAbort(signal)\n\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\treject(error)\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t})\n\t\t} finally {\n\t\t\tcleanup.abort()\n\t\t}\n\t\tcheckAbort(signal)\n\t}\n\n\t// Gather the table's full contents from the driver's ordered scan.\n\tasync #collect(): Promise<readonly Row[]> {\n\t\tconst rows: Row[] = []\n\t\tfor await (const row of this.#driver.scan(this.#name)) rows.push(row)\n\t\treturn rows\n\t}\n\n\t// Copy the input and assign a generated key when the key column is empty.\n\t#prepare(row: T): Row {\n\t\tif (!isRecord(row)) {\n\t\t\tthrow new DatabaseError('VALIDATION', `Row for table '${this.#name}' is not a record`, {\n\t\t\t\ttable: this.#name,\n\t\t\t})\n\t\t}\n\t\tconst prepared: Row = { ...row }\n\t\tif (prepared[this.#key] === undefined) {\n\t\t\tif (this.#generate !== undefined) {\n\t\t\t\ttry {\n\t\t\t\t\tprepared[this.#key] = this.#generate()\n\t\t\t\t} catch (cause) {\n\t\t\t\t\tthrow new DatabaseError(\n\t\t\t\t\t\t'VALIDATION',\n\t\t\t\t\t\t`Failed to generate primary column '${this.#key}' for table '${this.#name}'`,\n\t\t\t\t\t\t{ table: this.#name, column: this.#key, cause },\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\ttry {\n\t\t\t\t\tprepared[this.#key] = crypto.randomUUID()\n\t\t\t\t} catch (cause) {\n\t\t\t\t\tthrow new DatabaseError(\n\t\t\t\t\t\t'DRIVER',\n\t\t\t\t\t\t`Host failed to generate primary column '${this.#key}' for table '${this.#name}'`,\n\t\t\t\t\t\t{ table: this.#name, column: this.#key, cause },\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\treturn prepared\n\t}\n\n\t// Coerce *and* validate through the contract in one step: the contract's `parse`\n\t// now coerces types (`'36'` → `36`) AND enforces every leaf refinement (`min` /\n\t// `max` / `pattern`), so a non-`undefined` result already satisfies the guard\n\t// (AGENTS §14 parse↔guard soundness) — no separate `is` re-check is needed.\n\t// `isRecord` is kept solely to narrow the parsed `T` back to a storable `Row`\n\t// without an assertion (AGENTS §1); a table contract is always an object shape,\n\t// so it never rejects a genuinely-parsed row.\n\t#validate(row: Row): Row {\n\t\tconst parsed = this.#contract.parse(row)\n\t\tif (parsed === undefined || !isRecord(parsed)) {\n\t\t\tconst [fault] = this.#contract.explain(row)\n\t\t\tthrow new DatabaseError('VALIDATION', `Row failed the '${this.#name}' contract`, {\n\t\t\t\ttable: this.#name,\n\t\t\t\t...(fault === undefined ? {} : { field: fault.path, reason: fault.reason }),\n\t\t\t})\n\t\t}\n\t\treturn parsed\n\t}\n\n\t#resolveKey(row: Row): Key {\n\t\tconst key = extractKey(row, this.#key)\n\t\tif (key === undefined) {\n\t\t\tthrow new DatabaseError('VALIDATION', `Row has no usable key in column '${this.#key}'`, {\n\t\t\t\ttable: this.#name,\n\t\t\t\tcolumn: this.#key,\n\t\t\t})\n\t\t}\n\t\treturn key\n\t}\n\n\t// Narrow a stored row to the table's type through the contract guard.\n\t#cast(row: Row | undefined): T | undefined {\n\t\treturn row !== undefined && this.#guard(row) ? row : undefined\n\t}\n\n\t#track<R>(operation: () => Promise<R>): Promise<R> {\n\t\tif (this.#context !== undefined) return this.#context.track(operation)\n\t\treturn this.#scope === undefined ? operation() : this.#scope.track(operation)\n\t}\n}\n","import type { ContractInterface } from '@orkestrel/contract'\nimport type { EmitterErrorHandler } from '@orkestrel/emitter'\nimport type {\n\tColumnMap,\n\tDatabaseStorageInterface,\n\tKeyFunction,\n\tRowOf,\n\tTableInterface,\n\tPrimaryMap,\n\tTableMap,\n\tStorageInterface,\n} from './types.js'\nimport { createContract, objectShape } from '@orkestrel/contract'\nimport { DEFAULT_PRIMARY } from './constants.js'\nimport { DatabaseError } from './errors.js'\nimport { Table } from './Table.js'\nimport type { TransactionScope } from './TransactionScope.js'\n\n/**\n * A table-only database view bound to one driver transaction scope.\n *\n * @typeParam T - The declared table shape map\n *\n * @remarks\n * The view gives transaction work the same typed tables as its owning database\n * while enforcing a materially narrower contract and lifetime. `table` and\n * every table operation call the owning scope check, so a captured capability\n * cannot escape its transaction.\n */\nexport class DatabaseTransaction<\n\tT extends TableMap = TableMap,\n> implements DatabaseStorageInterface<T> {\n\treadonly #driver: StorageInterface\n\treadonly #tables: T\n\treadonly #primary: PrimaryMap\n\treadonly #generate: KeyFunction | undefined\n\treadonly #error: EmitterErrorHandler | undefined\n\treadonly #scope: TransactionScope\n\n\tconstructor(\n\t\tdriver: StorageInterface,\n\t\ttables: T,\n\t\tprimary: PrimaryMap,\n\t\tgenerate: KeyFunction | undefined,\n\t\terror: EmitterErrorHandler | undefined,\n\t\tscope: TransactionScope,\n\t) {\n\t\tthis.#driver = driver\n\t\tthis.#tables = tables\n\t\tthis.#primary = primary\n\t\tthis.#generate = generate\n\t\tthis.#error = error\n\t\tthis.#scope = scope\n\t}\n\n\ttable<K extends keyof T & string>(name: K): TableInterface<RowOf<T[K]>> {\n\t\tthis.#scope.check()\n\t\tconst columns = this.#columns(name)\n\t\treturn this.#build(name, this.#key(name), createContract(objectShape(columns)))\n\t}\n\n\t#build<R>(name: string, key: string, contract: ContractInterface<R>): TableInterface<R> {\n\t\treturn new Table(\n\t\t\t() => Promise.resolve(),\n\t\t\tthis.#driver,\n\t\t\tname,\n\t\t\tkey,\n\t\t\tcontract,\n\t\t\tthis.#generate,\n\t\t\tthis.#error,\n\t\t\tundefined,\n\t\t\tthis.#scope,\n\t\t)\n\t}\n\n\t#key(name: string): string {\n\t\treturn this.#primary[name] ?? DEFAULT_PRIMARY\n\t}\n\n\t#columns<K extends keyof T & string>(name: K): T[K]\n\t#columns(name: string): ColumnMap\n\t#columns(name: string): ColumnMap {\n\t\tconst columns = this.#tables[name]\n\t\tif (columns === undefined) {\n\t\t\tthrow new DatabaseError('NOT_FOUND', `Table '${name}' is not declared`, { table: name })\n\t\t}\n\t\treturn columns\n\t}\n}\n","import type { ContractInterface, Result } from '@orkestrel/contract'\nimport type { EmitterInterface } from '@orkestrel/emitter'\nimport type {\n\tColumnMap,\n\tDatabaseEventMap,\n\tDatabaseInterface,\n\tDatabaseOptions,\n\tDatabaseStatus,\n\tDatabaseStorageInterface,\n\tIndexMap,\n\tKeyFunction,\n\tMigration,\n\tOperationOptions,\n\tPrimaryMap,\n\tRowOf,\n\tTableDefinition,\n\tTableInterface,\n\tTableMap,\n\tTableSchema,\n} from './types.js'\nimport { compileSchema, createContract, objectShape } from '@orkestrel/contract'\nimport { DEFAULT_PRIMARY } from './constants.js'\nimport { DatabaseError } from './errors.js'\nimport { shapeToColumnSchema } from './helpers.js'\nimport { DatabaseContext } from './DatabaseContext.js'\nimport { DatabaseTransaction } from './DatabaseTransaction.js'\nimport { Table } from './Table.js'\nimport type { TransactionScope } from './TransactionScope.js'\n\n/**\n * A typed database view over one shared internal lifecycle and storage context.\n *\n * @remarks\n * Each view owns only its table contracts, primary columns, indexes, and key\n * generator. Imported views register their physical schemas with the same\n * internal context before opening begins, so every view observes one driver,\n * merged schema, emitter, status, transaction boundary, and terminal close.\n */\nexport class Database<T extends TableMap = TableMap> implements DatabaseInterface<T> {\n\t#context: DatabaseContext\n\treadonly #tables: T\n\treadonly #primary: PrimaryMap\n\treadonly #indexes: IndexMap\n\treadonly #generate: KeyFunction | undefined\n\n\tconstructor(options: DatabaseOptions<T>) {\n\t\tthis.#tables = options.tables\n\t\tthis.#primary = options.primary ?? {}\n\t\tthis.#indexes = options.indexes ?? {}\n\t\tthis.#generate = options.generator\n\t\tthis.#context = new DatabaseContext(options)\n\t\tthis.#context.register(this.#schema())\n\t}\n\n\tget emitter(): EmitterInterface<DatabaseEventMap> {\n\t\treturn this.#context.emitter\n\t}\n\n\tget name(): string {\n\t\treturn this.#context.name\n\t}\n\n\tget status(): DatabaseStatus {\n\t\treturn this.#context.status\n\t}\n\n\ttable<K extends keyof T & string>(name: K): TableInterface<RowOf<T[K]>> {\n\t\tif (this.#context.status === 'closed') {\n\t\t\tthrow new DatabaseError('CLOSED', `Database '${this.#context.name}' is closed`, {\n\t\t\t\tname: this.#context.name,\n\t\t\t})\n\t\t}\n\t\tconst columns = this.#columns(name)\n\t\treturn this.#build(name, this.#key(name), createContract(objectShape(columns)))\n\t}\n\n\timport<U extends TableMap>(tables: U, primary?: PrimaryMap): DatabaseInterface<U> {\n\t\treturn this.#spawn(tables, { ...this.#primary, ...primary })\n\t}\n\n\texport(): Readonly<Record<string, TableDefinition>> {\n\t\tconst result: Record<string, TableDefinition> = {}\n\t\tfor (const name of Object.keys(this.#tables)) {\n\t\t\tconst columns = this.#columns(name)\n\t\t\tresult[name] = {\n\t\t\t\tprimary: this.#key(name),\n\t\t\t\tcolumns,\n\t\t\t\tschema: compileSchema(objectShape(columns)),\n\t\t\t}\n\t\t}\n\t\treturn result\n\t}\n\n\topen(): Promise<void> {\n\t\treturn this.#context.open()\n\t}\n\n\tclose(): Promise<void> {\n\t\treturn this.#context.close()\n\t}\n\n\ttransaction<R>(\n\t\tscope: (transaction: DatabaseStorageInterface<T>) => Promise<R>,\n\t\toptions?: OperationOptions,\n\t): Promise<R> {\n\t\treturn this.#context.transaction(async (storage, lifetime) => {\n\t\t\tconst transaction = new DatabaseTransaction(\n\t\t\t\tstorage,\n\t\t\t\tthis.#tables,\n\t\t\t\tthis.#primary,\n\t\t\t\tthis.#generate,\n\t\t\t\tthis.#context.error,\n\t\t\t\tlifetime,\n\t\t\t)\n\t\t\treturn this.#settle(scope, transaction, lifetime)\n\t\t}, options)\n\t}\n\n\tmigrate(deployed: readonly TableSchema[], options?: OperationOptions): Promise<Migration> {\n\t\treturn this.#context.migrate(deployed, options)\n\t}\n\n\t#build<R>(name: string, key: string, contract: ContractInterface<R>): TableInterface<R> {\n\t\treturn new Table(\n\t\t\t() => this.#context.connect(),\n\t\t\tthis.#context.driver,\n\t\t\tname,\n\t\t\tkey,\n\t\t\tcontract,\n\t\t\tthis.#generate,\n\t\t\tthis.#context.error,\n\t\t\tthis.#context,\n\t\t)\n\t}\n\n\t#spawn<X extends TableMap>(tables: X, primary: PrimaryMap): DatabaseInterface<X> {\n\t\treturn Database.#attach(\n\t\t\t{\n\t\t\t\tdriver: this.#context.driver,\n\t\t\t\ttables,\n\t\t\t\tprimary,\n\t\t\t\tname: this.#context.name,\n\t\t\t\t...(this.#context.error === undefined ? {} : { error: this.#context.error }),\n\t\t\t\t...(this.#generate === undefined ? {} : { generator: this.#generate }),\n\t\t\t\t...(this.#context.version === undefined ? {} : { version: this.#context.version }),\n\t\t\t},\n\t\t\tthis.#context,\n\t\t)\n\t}\n\n\t#key(name: string): string {\n\t\treturn this.#primary[name] ?? DEFAULT_PRIMARY\n\t}\n\n\t#columns<K extends keyof T & string>(name: K): T[K]\n\t#columns(name: string): ColumnMap\n\t#columns(name: string): ColumnMap {\n\t\tconst columns = this.#tables[name]\n\t\tif (columns === undefined) {\n\t\t\tthrow new DatabaseError('NOT_FOUND', `Table '${name}' is not declared`, { table: name })\n\t\t}\n\t\treturn columns\n\t}\n\n\t#schema(): readonly TableSchema[] {\n\t\treturn Object.keys(this.#tables).map((name) => {\n\t\t\tconst columns = this.#columns(name)\n\t\t\treturn {\n\t\t\t\tname,\n\t\t\t\tprimary: this.#key(name),\n\t\t\t\tcolumns: Object.entries(columns).map(([column, shape]) =>\n\t\t\t\t\tshapeToColumnSchema(column, shape),\n\t\t\t\t),\n\t\t\t\tindexes: this.#indexes[name] ?? [],\n\t\t\t}\n\t\t})\n\t}\n\n\tasync #settle<R>(\n\t\tscope: (transaction: DatabaseStorageInterface<T>) => Promise<R>,\n\t\ttransaction: DatabaseStorageInterface<T>,\n\t\tlifetime: TransactionScope,\n\t): Promise<Result<R, unknown>> {\n\t\tconst outcome: Result<R, unknown> = await Promise.resolve()\n\t\t\t.then(() => scope(transaction))\n\t\t\t.then(\n\t\t\t\t(value) => ({ success: true, value }),\n\t\t\t\t(error: unknown) => ({ success: false, error }),\n\t\t\t)\n\t\tlifetime.stop()\n\t\tconst drained: Result<void, unknown> = await lifetime.drain().then(\n\t\t\t() => ({ success: true, value: undefined }),\n\t\t\t(error: unknown) => ({ success: false, error }),\n\t\t)\n\t\tif (!outcome.success) return outcome\n\t\tif (!drained.success) return drained\n\t\treturn outcome\n\t}\n\n\tstatic #attach<X extends TableMap>(\n\t\toptions: DatabaseOptions<X>,\n\t\tcontext: DatabaseContext,\n\t): Database<X> {\n\t\tconst database = new Database(options)\n\t\tdatabase.#context = context\n\t\tcontext.register(database.#schema())\n\t\treturn database\n\t}\n}\n","import type {\n\tQueryInput,\n\tDriverInterface,\n\tDriverMetadata,\n\tKey,\n\tMigrationInput,\n\tMigrationStep,\n\tOperationOptions,\n\tRow,\n\tTableSchema,\n} from '../types.js'\nimport { cloneDriverMetadata, cloneMigrationInput } from '../cloners.js'\nimport { DatabaseError } from '../errors.js'\nimport {\n\tbindRowKey,\n\tcheckAbort,\n\tcompareValues,\n\tequalsValue,\n\tmatchesQuery,\n\tmigrateRows,\n\tnormalizeDriverSchema,\n\tplanMigration,\n\tprojectMigrationSchema,\n} from '../helpers.js'\nimport { isKey, validatePage } from '../validators.js'\n\n/**\n * The reference {@link DriverInterface} — nested maps, no I/O.\n *\n * @remarks\n * The in-between made concrete: it runs identically in a browser or on a server,\n * so it is the storage behind tests, ephemeral caches, and any code that wants\n * the database API without a persistent backend. Rows are DEEP-copied (via\n * `structuredClone`) in and out — at `write`, `read`, `scan`, `stream`, and both\n * snapshot capture and restore — so a caller mutating a nested field of an input\n * row, a returned row, or a row mutated in place between snapshot and rollback\n * can never perturb stored state (AGENTS §11); a shallow `{ ...row }` spread\n * would still share nested object/array references. Metadata instead routes\n * through `cloneDriverMetadata`: `stamp` and migration snapshot exact JSON at\n * ingress, and `metadata` returns a distinct deeply frozen owned copy. `snapshot`\n * clones every table to give transactions an exact rollback point. `scan` and\n * `keys` yield in key order — sorted by the core {@link compareValues} total\n * order, the same contract the SQLite (`ORDER BY`) and IndexedDB (key-ordered\n * reads) backends honor, so an unordered read agrees across every backend rather\n * than leaking Map insertion order. A persistent backend (IndexedDB, SQLite)\n * implements the same required methods over real storage.\n */\nexport class MemoryDriver implements DriverInterface {\n\treadonly #tables = new Map<string, Map<Key, Row>>()\n\t#identities = new Map<string, object>()\n\t#schema: readonly TableSchema[] = []\n\t#metadata: DriverMetadata | undefined\n\n\tasync open(schema: readonly TableSchema[]): Promise<void> {\n\t\tconst owned = normalizeDriverSchema(schema)\n\t\tconst deployed = normalizeDriverSchema(this.#metadata?.schema ?? owned)\n\t\tconst names = new Set(deployed.map((table) => table.name))\n\t\tfor (const name of this.#identities.keys()) {\n\t\t\tif (!names.has(name)) this.#identities.delete(name)\n\t\t}\n\t\tfor (const table of deployed) {\n\t\t\tif (!this.#tables.has(table.name)) this.#tables.set(table.name, new Map())\n\t\t\tif (!this.#identities.has(table.name)) this.#identities.set(table.name, {})\n\t\t}\n\t\tthis.#schema = deployed\n\t}\n\n\tasync close(): Promise<void> {}\n\n\tasync read(table: string, key: Key): Promise<Row | undefined> {\n\t\tconst row = this.#store(table).get(key)\n\t\treturn row === undefined ? undefined : structuredClone(row)\n\t}\n\n\tasync write(table: string, key: Key, row: Row, options?: OperationOptions): Promise<void> {\n\t\tcheckAbort(options?.signal)\n\t\tconst primary = this.#table(table).primary\n\t\tthis.#store(table).set(key, structuredClone(bindRowKey(row, primary, key)))\n\t}\n\n\tasync insert(table: string, key: Key, row: Row, options?: OperationOptions): Promise<void> {\n\t\tcheckAbort(options?.signal)\n\t\tconst store = this.#store(table)\n\t\tif (store.has(key)) {\n\t\t\tthrow new DatabaseError('CONFLICT', `Row '${key}' already exists in table '${table}'`, {\n\t\t\t\ttable,\n\t\t\t\tkey,\n\t\t\t})\n\t\t}\n\t\tconst primary = this.#table(table).primary\n\t\tstore.set(key, structuredClone(bindRowKey(row, primary, key)))\n\t}\n\n\tasync delete(table: string, key: Key, options?: OperationOptions): Promise<boolean> {\n\t\tcheckAbort(options?.signal)\n\t\treturn this.#store(table).delete(key)\n\t}\n\n\tasync keys(table: string): Promise<readonly Key[]> {\n\t\treturn this.#ordered(table)\n\t}\n\n\tasync *scan(table: string): AsyncIterable<Row> {\n\t\tconst store = this.#store(table)\n\t\tfor (const key of this.#ordered(table)) {\n\t\t\tconst row = store.get(key)\n\t\t\tif (row !== undefined) yield structuredClone(row)\n\t\t}\n\t}\n\n\t/**\n\t * Natively filtered lazy iteration — the {@link DriverInterface.stream} hook.\n\t *\n\t * @remarks\n\t * Iterates the table's keys in the same key order `scan` and `keys` yield\n\t * (sorted by {@link compareValues}), testing each row against\n\t * `input.conditions` (via {@link matchesQuery}) before counting it\n\t * toward `offset` / `limit`. Both are applied lazily as matches are found —\n\t * `offset` matches are skipped without being yielded, and iteration stops the\n\t * instant `limit` yields have been produced, so a large table is never fully\n\t * walked for a small page. `input.order` is IGNORED (the same contract as\n\t * `TableInterface.scan` and `QueryInterface.stream`): streaming yields key\n\t * order, sorted output is `records()`'s job. Rows yield copy-out (AGENTS\n\t * §11), and an unknown table mirrors `scan`'s empty-yield behavior.\n\t *\n\t * @param table - The table to stream\n\t * @param input - The filter / offset / limit to apply lazily\n\t *\n\t * @example\n\t * ```ts\n\t * for await (const row of driver.stream('users', { conditions, limit: 10 })) {\n\t * // one matched row at a time, in key order\n\t * }\n\t * ```\n\t */\n\tstream(table: string, input: QueryInput): AsyncIterable<Row> {\n\t\tvalidatePage(input)\n\t\treturn this.#stream(table, input)\n\t}\n\n\tasync *#stream(table: string, input: QueryInput): AsyncIterable<Row> {\n\t\tconst store = this.#store(table)\n\t\tconst conditions = input.conditions\n\t\tconst offset = input.offset ?? 0\n\t\tconst limit = input.limit\n\t\tlet skipped = 0\n\t\tlet yielded = 0\n\t\tfor (const key of this.#ordered(table)) {\n\t\t\tif (limit !== undefined && yielded >= limit) return\n\t\t\tconst row = store.get(key)\n\t\t\tif (row === undefined) continue\n\t\t\tif (conditions !== undefined && conditions.length > 0 && !matchesQuery(row, conditions)) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif (skipped < offset) {\n\t\t\t\tskipped += 1\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tyield structuredClone(row)\n\t\t\tyielded += 1\n\t\t}\n\t}\n\n\tasync clear(table: string): Promise<void> {\n\t\tthis.#store(table).clear()\n\t}\n\n\t/**\n\t * Capture the current state and return a thunk that rolls back to it.\n\t *\n\t * @remarks\n\t * Capture owns rows, schema, and one session-local table identity. Replay\n\t * adapts rows to each surviving same-identity table's current schema before\n\t * changing storage. Removed or replaced tables are skipped; uncaptured and\n\t * later-added tables retain their current rows. Schema and metadata are never\n\t * restored.\n\t *\n\t * @param tables - The table names to scope the snapshot to; omitted captures every table\n\t * @returns A thunk that restores the captured tables\n\t */\n\tasync snapshot(tables?: readonly string[]): Promise<() => Promise<void>> {\n\t\tconst names =\n\t\t\ttables === undefined\n\t\t\t\t? this.#schema.map((table) => table.name)\n\t\t\t\t: [...new Set(tables)].filter((name) => this.#schema.some((table) => table.name === name))\n\t\tconst captured = new Map<\n\t\t\tstring,\n\t\t\t{\n\t\t\t\treadonly identity: object\n\t\t\t\treadonly rows: ReadonlyMap<Key, Row>\n\t\t\t\treadonly schema: TableSchema\n\t\t\t}\n\t\t>()\n\t\tfor (const name of names) {\n\t\t\tconst schema = this.#schema.find((table) => table.name === name)\n\t\t\tconst store = this.#tables.get(name)\n\t\t\tconst identity = this.#identities.get(name)\n\t\t\tif (schema === undefined || store === undefined || identity === undefined) continue\n\t\t\tconst rows = new Map<Key, Row>()\n\t\t\tfor (const [key, row] of store) rows.set(key, structuredClone(row))\n\t\t\tcaptured.set(name, { identity, rows, schema })\n\t\t}\n\t\treturn async () => {\n\t\t\tconst replacements = new Map<Map<Key, Row>, ReadonlyMap<Key, Row>>()\n\t\t\tfor (const [name, capture] of captured) {\n\t\t\t\tconst schema = this.#schema.find((table) => table.name === name)\n\t\t\t\tconst store = this.#tables.get(name)\n\t\t\t\tif (\n\t\t\t\t\tschema === undefined ||\n\t\t\t\t\tstore === undefined ||\n\t\t\t\t\tthis.#identities.get(name) !== capture.identity\n\t\t\t\t) {\n\t\t\t\t\tcontinue\n\t\t\t\t}\n\t\t\t\tconst plan = planMigration([capture.schema], [schema])\n\t\t\t\tconst entries = [...capture.rows.entries()]\n\t\t\t\tconst migrated = migrateRows(\n\t\t\t\t\tentries.map(([, row]) => row),\n\t\t\t\t\tplan.steps,\n\t\t\t\t)\n\t\t\t\tif (migrated.length !== entries.length) {\n\t\t\t\t\tthrow new DatabaseError('MIGRATION', 'Snapshot row count changed during migration', {\n\t\t\t\t\t\ttable: name,\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t\tconst rows = new Map<Key, Row>()\n\t\t\t\tfor (const [index, [key]] of entries.entries()) {\n\t\t\t\t\tconst row = migrated[index]\n\t\t\t\t\tif (!isKey(key) || row === undefined) {\n\t\t\t\t\t\tthrow new DatabaseError('MIGRATION', 'Snapshot row has no usable primary key', {\n\t\t\t\t\t\t\ttable: name,\n\t\t\t\t\t\t\tcolumn: schema.primary,\n\t\t\t\t\t\t\tindex,\n\t\t\t\t\t\t})\n\t\t\t\t\t}\n\t\t\t\t\trows.set(key, structuredClone(bindRowKey(row, schema.primary, key)))\n\t\t\t\t}\n\t\t\t\treplacements.set(store, rows)\n\t\t\t}\n\t\t\tfor (const [store, rows] of replacements) {\n\t\t\t\tstore.clear()\n\t\t\t\tfor (const [key, row] of rows) store.set(key, row)\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Return the persisted {@link DriverMetadata}, or `undefined` when the store has\n\t * never been stamped.\n\t *\n\t * @remarks\n\t * In-process only — the metadata lives in this instance's memory, exactly\n\t * like the rest of this driver's storage. The returned value is a distinct\n\t * deeply frozen owned snapshot. A driver-conformance-valid implementation of\n\t * the optional `metadata` / `stamp` pair.\n\t *\n\t * @returns The last-stamped {@link DriverMetadata}, or `undefined`\n\t */\n\tasync metadata(): Promise<DriverMetadata | undefined> {\n\t\treturn this.#metadata === undefined ? undefined : cloneDriverMetadata(this.#metadata)\n\t}\n\n\t/**\n\t * Persist an owned snapshot for a later `metadata()` to return.\n\t *\n\t * @param metadata - The {@link DriverMetadata} to persist\n\t */\n\tasync stamp(metadata: DriverMetadata): Promise<void> {\n\t\tthis.#metadata = cloneDriverMetadata(metadata)\n\t}\n\n\t/**\n\t * Apply a {@link Migration} plan's steps against the in-memory store.\n\t *\n\t * @remarks\n\t * Steps apply against an isolated candidate. Rows, schema changes, and\n\t * optional metadata publish together only after the whole request succeeds.\n\t *\n\t * @param input - The migration plan and optional metadata to settle atomically\n\t */\n\tasync migrate(input: MigrationInput): Promise<void> {\n\t\tconst owned = cloneMigrationInput(input)\n\t\tconst schema = projectMigrationSchema(this.#schema, owned.plan.steps)\n\t\tif (\n\t\t\towned.metadata !== undefined &&\n\t\t\t!equalsValue(normalizeDriverSchema(owned.metadata.schema), schema)\n\t\t) {\n\t\t\tthrow new DatabaseError('MIGRATION', 'Migration metadata schema does not match the plan', {\n\t\t\t\tprojected: schema,\n\t\t\t\tmetadata: owned.metadata.schema,\n\t\t\t})\n\t\t}\n\t\tconst candidate = this.#copy(this.#tables)\n\t\tconst identities = this.#projectIdentities(this.#identities, owned.plan.steps)\n\t\tfor (const step of owned.plan.steps) this.#migrate(candidate, step)\n\t\tthis.#tables.clear()\n\t\tfor (const [name, store] of candidate) this.#tables.set(name, store)\n\t\tthis.#identities = identities\n\t\tthis.#schema = schema\n\t\tif (owned.metadata !== undefined) this.#metadata = owned.metadata\n\t}\n\n\t// A table's keys in key order — the contract `scan` and `keys` yield in.\n\t// `compareValues` is the core total order (number < string, natural within),\n\t// matching the SQLite `ORDER BY` and IndexedDB key-range orderings.\n\t#ordered(table: string): readonly Key[] {\n\t\treturn [...this.#store(table).keys()].sort(compareValues)\n\t}\n\n\t// A migration step's table must already exist — unlike `#store`, a missing\n\t// table here is a MIGRATION error rather than an as-yet-untouched table.\n\t#require(tables: Map<string, Map<Key, Row>>, table: string): Map<Key, Row> {\n\t\tconst store = tables.get(table)\n\t\tif (store === undefined) {\n\t\t\tthrow new DatabaseError('MIGRATION', `migrate: unknown table '${table}'`, { table })\n\t\t}\n\t\treturn store\n\t}\n\n\t#copy(tables: Map<string, Map<Key, Row>>): Map<string, Map<Key, Row>> {\n\t\tconst copy = new Map<string, Map<Key, Row>>()\n\t\tfor (const [name, store] of tables) {\n\t\t\tconst cloned = new Map<Key, Row>()\n\t\t\tfor (const [key, row] of store) cloned.set(key, structuredClone(row))\n\t\t\tcopy.set(name, cloned)\n\t\t}\n\t\treturn copy\n\t}\n\n\t#projectIdentities(\n\t\tidentities: ReadonlyMap<string, object>,\n\t\tsteps: readonly MigrationStep[],\n\t): Map<string, object> {\n\t\tconst projected = new Map(identities)\n\t\tfor (const step of steps) {\n\t\t\tif (step.operation === 'table.add') projected.set(step.table.name, {})\n\t\t\tif (step.operation === 'table.remove') projected.delete(step.table)\n\t\t}\n\t\treturn projected\n\t}\n\n\t#table(name: string): TableSchema {\n\t\tconst schema = this.#schema.find((table) => table.name === name)\n\t\tif (schema === undefined) {\n\t\t\tthrow new DatabaseError('NOT_FOUND', `Unknown table '${name}'`, { table: name })\n\t\t}\n\t\treturn schema\n\t}\n\n\t#migrate(tables: Map<string, Map<Key, Row>>, step: MigrationStep): void {\n\t\tswitch (step.operation) {\n\t\t\tcase 'table.add':\n\t\t\t\tif (!tables.has(step.table.name)) tables.set(step.table.name, new Map())\n\t\t\t\tbreak\n\t\t\tcase 'table.remove':\n\t\t\t\tthis.#require(tables, step.table)\n\t\t\t\ttables.delete(step.table)\n\t\t\t\tbreak\n\t\t\tcase 'column.add':\n\t\t\tcase 'column.remove': {\n\t\t\t\tconst store = this.#require(tables, step.table)\n\t\t\t\tconst rows = [...store.entries()]\n\t\t\t\tconst migrated = migrateRows(\n\t\t\t\t\trows.map(([, row]) => row),\n\t\t\t\t\t[step],\n\t\t\t\t)\n\t\t\t\tfor (const [index, [key]] of rows.entries()) {\n\t\t\t\t\tconst row = migrated[index]\n\t\t\t\t\tif (row === undefined) {\n\t\t\t\t\t\tthrow new DatabaseError('MIGRATION', 'migrate: transformed row is missing', {\n\t\t\t\t\t\t\ttable: step.table,\n\t\t\t\t\t\t\tindex,\n\t\t\t\t\t\t})\n\t\t\t\t\t}\n\t\t\t\t\tstore.set(key, row)\n\t\t\t\t}\n\t\t\t\tbreak\n\t\t\t}\n\t\t\tcase 'index.add':\n\t\t\tcase 'index.remove':\n\t\t\t\tthis.#require(tables, step.table)\n\t\t\t\tbreak\n\t\t}\n\t}\n\n\t// Resolve only a currently declared table. `open` creates every backing map,\n\t// so a missing map is a lookup failure rather than an implicit declaration.\n\t#store(table: string): Map<Key, Row> {\n\t\tthis.#table(table)\n\t\tconst store = this.#tables.get(table)\n\t\tif (store === undefined) {\n\t\t\tthrow new DatabaseError('NOT_FOUND', `Table '${table}' has no backing store`, { table })\n\t\t}\n\t\treturn store\n\t}\n}\n","import type { DatabaseInterface, DatabaseOptions, DriverInterface, TableMap } from './types.js'\nimport { Database } from './Database.js'\nimport { MemoryDriver } from './drivers/MemoryDriver.js'\n\n/**\n * Create a database over a driver and a declared `tables` schema.\n *\n * @remarks\n * `tables` maps each name to its columns (a `column → shape` map); the database\n * wraps each in an `objectShape`, so you never write `objectShape` at the table\n * level. The `const` type parameter captures the literal names and columns, so\n * `db.table('users')` is checked against the schema and typed by `Infer` of its\n * columns — no annotations. Name a non-`id` primary-key column per table via the\n * optional `primary` and `indexes` maps.\n *\n * @param options - The driver, `tables`, and optional `primary`, `indexes`,\n * `name`, `generator`, `version`, and emitter hooks\n * @returns A typed {@link DatabaseInterface}\n *\n * @example\n * ```ts\n * import { createDatabase, createMemoryDriver } from '@orkestrel/database'\n * import { integerShape, stringShape } from '@orkestrel/contract'\n *\n * const db = createDatabase({\n * \tdriver: createMemoryDriver(),\n * \ttables: {\n * \t\tusers: { id: stringShape(), age: integerShape() },\n * \t\tposts: { slug: stringShape(), title: stringShape() },\n * \t},\n * \tprimary: { posts: 'slug' },\n * })\n * await db.table('users').set({ id: 'u1', age: 36 }) // typed; coerced + validated\n * ```\n */\nexport function createDatabase<const T extends TableMap>(\n\toptions: DatabaseOptions<T>,\n): DatabaseInterface<T> {\n\treturn new Database(options)\n}\n\n/**\n * Create the in-memory reference {@link DriverInterface}.\n *\n * @remarks\n * Backed by nested maps with no I/O — the same driver runs in a browser or on a\n * server, making it the natural choice for tests and ephemeral storage.\n *\n * @returns A fresh in-memory driver\n */\nexport function createMemoryDriver(): DriverInterface {\n\treturn new MemoryDriver()\n}\n"],"mappings":";;;;;;;;;;AASA,IAAa,kBAAkB;;;;;;;;;;;;;;AAe/B,IAAa,qBAAqB;;;;;;;;;;;;;;;;;;;ACDlC,IAAa,gBAAb,cAAmC,MAAM;CACxC;CACA;CAEA,YACC,MACA,SACA,SACC;EACD,MAAM,OAAO;EACb,KAAK,OAAO;EACZ,KAAK,OAAO;EACZ,IAAI,YAAY,KAAA,GAAW,KAAK,UAAU;CAC3C;AACD;;;;;;;;;;;;;;;;AAiBA,SAAgB,gBAAgB,OAAwC;CACvE,OAAO,iBAAiB;AACzB;;;;;;;;;;;;;;;AC/BA,SAAgB,aAAa,OAA0B;CACtD,MAAM,QAAQ,OAAO;CACrB,IAAI,UAAU,KAAA,MAAc,CAAC,OAAO,UAAU,KAAK,KAAK,QAAQ,IAC/D,MAAM,IAAI,cAAc,cAAc,6CAA6C;EAClF,OAAO;EACP,OAAO,OAAO,SAAS,KAAK,IAAI,QAAQ,OAAO,KAAK;CACrD,CAAC;CAEF,MAAM,SAAS,OAAO;CACtB,IAAI,WAAW,KAAA,MA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