@eidos.space/eidos-file 1.0.0 → 2.0.2

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.
Files changed (47) hide show
  1. package/DEVELOPER_PLATFORM.md +17 -11
  2. package/FIELD-CONVERSION.md +75 -0
  3. package/README.md +18 -2
  4. package/dist/{adapter-contract-B3p-48Yb.d.mts → adapter-contract-CPhqAl4b.d.mts} +46 -65
  5. package/dist/adapter-contract-CPhqAl4b.d.mts.map +1 -0
  6. package/dist/better-sqlite3.d.mts +2 -2
  7. package/dist/better-sqlite3.d.mts.map +1 -1
  8. package/dist/better-sqlite3.mjs +13 -5
  9. package/dist/better-sqlite3.mjs.map +1 -1
  10. package/dist/browser.d.mts +4 -4
  11. package/dist/browser.mjs +3 -3
  12. package/dist/browser.mjs.map +1 -1
  13. package/dist/{csv-WEud-i_H.mjs → csv-CvzHNNGW.mjs} +48 -11
  14. package/dist/csv-CvzHNNGW.mjs.map +1 -0
  15. package/dist/csv.d.mts +4 -2
  16. package/dist/csv.d.mts.map +1 -1
  17. package/dist/csv.mjs +1 -1
  18. package/dist/index.d.mts +40 -14
  19. package/dist/index.d.mts.map +1 -1
  20. package/dist/index.mjs +578 -200
  21. package/dist/index.mjs.map +1 -1
  22. package/dist/node-sqlite.d.mts +111 -0
  23. package/dist/node-sqlite.d.mts.map +1 -0
  24. package/dist/node-sqlite.mjs +708 -0
  25. package/dist/node-sqlite.mjs.map +1 -0
  26. package/dist/{runtime-DCo5E0Kr.d.mts → runtime-CkL1OOJT.d.mts} +129 -7
  27. package/dist/runtime-CkL1OOJT.d.mts.map +1 -0
  28. package/dist/{runtime-CMdfE7Gx.mjs → runtime-qbuZMbV4.mjs} +674 -198
  29. package/dist/runtime-qbuZMbV4.mjs.map +1 -0
  30. package/dist/{select-options-DihMTuoa.mjs → select-options-Dr8XCYOS.mjs} +145 -80
  31. package/dist/select-options-Dr8XCYOS.mjs.map +1 -0
  32. package/dist/{sqlite-wasm-B86yaLG6.mjs → sqlite-wasm-fT8Z-5Ww.mjs} +34 -13
  33. package/dist/sqlite-wasm-fT8Z-5Ww.mjs.map +1 -0
  34. package/dist/system-metadata-merge-BaX38cp-.d.mts +77 -0
  35. package/dist/system-metadata-merge-BaX38cp-.d.mts.map +1 -0
  36. package/dist/system-metadata-merge-CjTqAwCi.mjs +819 -0
  37. package/dist/system-metadata-merge-CjTqAwCi.mjs.map +1 -0
  38. package/dist/{temporal-CVzLU63l.d.mts → temporal-ysCLNSub.d.mts} +8 -3
  39. package/dist/temporal-ysCLNSub.d.mts.map +1 -0
  40. package/package.json +10 -2
  41. package/dist/adapter-contract-B3p-48Yb.d.mts.map +0 -1
  42. package/dist/csv-WEud-i_H.mjs.map +0 -1
  43. package/dist/runtime-CMdfE7Gx.mjs.map +0 -1
  44. package/dist/runtime-DCo5E0Kr.d.mts.map +0 -1
  45. package/dist/select-options-DihMTuoa.mjs.map +0 -1
  46. package/dist/sqlite-wasm-B86yaLG6.mjs.map +0 -1
  47. package/dist/temporal-CVzLU63l.d.mts.map +0 -1
@@ -1,4 +1,4 @@
1
- import { A as parseEidosFileJson, C as createEidosFileUuid, E as quoteIdentifier, O as canonicalizeEidosFileJson, T as isEidosFileUuid, a as eidosFileInstantSqlCheck, c as normalizeEidosFileDate, i as eidosFileDateSqlCheck, j as EidosFileError, k as isCanonicalEidosFileJson, l as normalizeEidosFileInstant, o as isCanonicalEidosFileDate, r as currentEidosFileInstant, s as isCanonicalEidosFileInstant, t as assertEidosFileSelectOptions, u as assertEidosFileValues, w as eidosFilePhysicalName, x as assertEidosFileUuid, y as assertEidosFileDisplayName } from "./select-options-DihMTuoa.mjs";
1
+ import { A as isCanonicalEidosFileJson, C as assertEidosFileUuid, D as isEidosFileUuid, E as isEidosFileReservedTableName, M as EidosFileError, O as quoteIdentifier, S as assertEidosFileTableName, T as createEidosFileUuid, a as eidosFileInstantSqlCheck, b as assertEidosFileDisplayName, c as normalizeEidosFileDate, f as decodeEidosFileValues, i as eidosFileDateSqlCheck, j as parseEidosFileJson, k as canonicalizeEidosFileJson, l as normalizeEidosFileInstant, o as isCanonicalEidosFileDate, r as currentEidosFileInstant, s as isCanonicalEidosFileInstant, t as assertEidosFileSelectOptions, u as assertEidosFileValues } from "./select-options-Dr8XCYOS.mjs";
2
2
  //#region src/connection-port.ts
3
3
  const INT64_MIN$2 = -9223372036854775808n;
4
4
  const INT64_MAX$2 = 9223372036854775807n;
@@ -157,14 +157,22 @@ function nativeToSqlValue(value) {
157
157
  tag: "text",
158
158
  value: assertUnicodeString(value)
159
159
  };
160
- if (value instanceof Uint8Array) return {
161
- tag: "blob",
162
- value: new Uint8Array(value)
163
- };
164
- if (value instanceof ArrayBuffer) return {
165
- tag: "blob",
166
- value: new Uint8Array(value.slice(0))
167
- };
160
+ if (ArrayBuffer.isView(value) && Object.prototype.toString.call(value) === "[object Uint8Array]") {
161
+ const view = value;
162
+ const bytes = new Uint8Array(view.byteLength);
163
+ bytes.set(new Uint8Array(view.buffer, view.byteOffset, view.byteLength));
164
+ return {
165
+ tag: "blob",
166
+ value: bytes
167
+ };
168
+ }
169
+ if (Object.prototype.toString.call(value) === "[object ArrayBuffer]") {
170
+ const source = new Uint8Array(value);
171
+ return {
172
+ tag: "blob",
173
+ value: new Uint8Array(source)
174
+ };
175
+ }
168
176
  throw new EidosAdapterError("invalid-sql-value", "Unsupported SQLite storage value: " + typeof value, false, true);
169
177
  }
170
178
  /**
@@ -186,6 +194,7 @@ var ConnectionPortEidosFileConnection = class {
186
194
  };
187
195
  }
188
196
  exec(sql) {
197
+ if (sql.trim() === "PRAGMA foreign_keys = ON; PRAGMA trusted_schema = OFF;") return;
189
198
  this.port.execSchema(sql);
190
199
  }
191
200
  query(sql, params = []) {
@@ -291,7 +300,11 @@ const DATE_STATS = [
291
300
  "min",
292
301
  "max"
293
302
  ];
294
- const CHECKBOX_STATS = COMMON_STATS;
303
+ const CHECKBOX_STATS = [
304
+ ...COMMON_STATS,
305
+ "percent-checked",
306
+ "percent-unchecked"
307
+ ];
295
308
  const MULTI_VALUE_STATS = [...COMMON_STATS];
296
309
  const RELATION_STATS = [
297
310
  ...MULTI_VALUE_STATS,
@@ -304,6 +317,8 @@ const LABELS = {
304
317
  "count-non-null": "Count non-null",
305
318
  "count-distinct": "Count distinct",
306
319
  "count-empty": "Count empty",
320
+ "percent-checked": "Percent checked",
321
+ "percent-unchecked": "Percent unchecked",
307
322
  sum: "Sum",
308
323
  average: "Average",
309
324
  min: "Minimum",
@@ -773,6 +788,7 @@ function assertExactFormulaAst(root) {
773
788
  function formulaOperandType(field) {
774
789
  let type;
775
790
  if (field.systemRole === "row-id" || field.type === "row-id") type = "text";
791
+ else if (field.type === "rating") type = "integer";
776
792
  else if (field.systemRole === "created-time" || field.systemRole === "updated-time" || field.type === "created-time" || field.type === "last-edited-time") type = "datetime";
777
793
  else if (field.type === "select") type = "text";
778
794
  else if (field.type === "lookup" && field.property?.aggregate === "values") type = void 0;
@@ -1283,7 +1299,7 @@ function eidosFileLookupTargetDisplayType(target) {
1283
1299
  if (type === "date") return "date";
1284
1300
  if (type === "datetime") return "datetime";
1285
1301
  if (type === "url") return "url";
1286
- if (type === "json" || type === "file") return "json";
1302
+ if (type === "file") return "json";
1287
1303
  return "text";
1288
1304
  }
1289
1305
  function eidosFileLookupDisplayType(aggregate, target) {
@@ -1419,6 +1435,16 @@ function normalizeEidosFileRowQuery(value) {
1419
1435
  function escapeLike(value) {
1420
1436
  return value.replace(/[\\%_]/g, "\\$&");
1421
1437
  }
1438
+ function requiresUnicodeCasefold(value) {
1439
+ const nonAscii = value.replace(/[\x00-\x7f]/g, "");
1440
+ return nonAscii.toUpperCase() !== nonAscii.toLowerCase();
1441
+ }
1442
+ function caseInsensitiveLike(expression, pattern, searchText, params) {
1443
+ params.push(pattern);
1444
+ return requiresUnicodeCasefold(searchText) ? `eidos_casefold(COALESCE(CAST(${expression} AS TEXT), ''))
1445
+ LIKE eidos_casefold(?) ESCAPE '\\'` : `COALESCE(CAST(${expression} AS TEXT), '')
1446
+ LIKE ? COLLATE NOCASE ESCAPE '\\'`;
1447
+ }
1422
1448
  function searchableFields(fields, selected) {
1423
1449
  const selection = selected ? new Set(selected) : null;
1424
1450
  return fields.filter((field) => (!selection || !!field.id && selection.has(field.id)) && !field.isHidden && (field.isRecordLabel || field.valueKind === "source" || field.valueKind === "materialized" || field.valueKind === "derived"));
@@ -1522,18 +1548,15 @@ function likeExpression(field, operator, value, params) {
1522
1548
  ELSE '[]'
1523
1549
  END
1524
1550
  )
1525
- WHERE eidos_casefold(COALESCE(CAST(value AS TEXT), ''))
1526
- LIKE eidos_casefold(?) ESCAPE '\\'
1527
- )` : `eidos_casefold(COALESCE(CAST(${column} AS TEXT), ''))
1528
- LIKE eidos_casefold(?) ESCAPE '\\'`;
1529
- params.push(pattern);
1551
+ WHERE ${caseInsensitiveLike("value", pattern, value, params)}
1552
+ )` : `COALESCE(${caseInsensitiveLike(column, pattern, value, params)}, 0)`;
1530
1553
  return operator === "not-contains" ? `NOT (${expression})` : expression;
1531
1554
  }
1532
1555
  function compileRule(rule, fields, params) {
1533
1556
  const field = requireField(fields, rule.field);
1534
1557
  const column = quoteIdentifier(field.tableColumnName);
1535
1558
  const arrayCodec = field.storageCodec === "json_array" || field.storageCodec === "relation";
1536
- const empty = arrayCodec ? `(json_valid(${column}) AND json_type(${column}) = 'array' AND json_array_length(${column}) = 0)` : `(${column} IS NULL)`;
1559
+ const empty = arrayCodec ? `(${column} IS NULL OR (json_valid(${column}) AND json_type(${column}) = 'array' AND json_array_length(${column}) = 0))` : `(${column} IS NULL)`;
1537
1560
  if (rule.operator === "is-empty") return empty;
1538
1561
  if (rule.operator === "is-not-empty") return `NOT ${empty}`;
1539
1562
  if (rule.operator === "is-any-of" || rule.operator === "is-all-of" || rule.operator === "is-none-of") {
@@ -1558,18 +1581,18 @@ function compileRule(rule, fields, params) {
1558
1581
  }
1559
1582
  params.push(...values.map((entry) => sqlFieldValue(field, entry)));
1560
1583
  if (rule.operator === "is-all-of") return "0";
1561
- const expression = `${column} IN (${values.map(() => "?").join(", ")})`;
1584
+ const expression = `COALESCE(${column} IN (${values.map(() => "?").join(", ")}), 0)`;
1562
1585
  return rule.operator === "is-none-of" ? `NOT (${expression})` : expression;
1563
1586
  }
1564
1587
  const value = Array.isArray(rule.value) ? rule.value[0] : rule.value;
1565
1588
  if (rule.operator === "equals" || rule.operator === "not-equals") {
1566
1589
  if (arrayCodec && Array.isArray(rule.value)) {
1567
- params.push(JSON.stringify(rule.value.map(sqlValue)));
1568
- return rule.operator === "not-equals" ? `${column} <> ?` : `${column} = ?`;
1590
+ params.push(canonicalizeEidosFileJson(rule.value));
1591
+ return rule.operator === "not-equals" ? `${column} IS NOT ?` : `${column} IS ?`;
1569
1592
  }
1570
1593
  if (value === null || value === void 0) return `${column} IS ${rule.operator === "not-equals" ? "NOT " : ""}NULL`;
1571
1594
  params.push(sqlFieldValue(field, value));
1572
- return rule.operator === "not-equals" ? `${column} <> ?` : `${column} = ?`;
1595
+ return rule.operator === "not-equals" ? `${column} IS NOT ?` : `${column} IS ?`;
1573
1596
  }
1574
1597
  if (rule.operator === "contains" || rule.operator === "not-contains" || rule.operator === "starts-with" || rule.operator === "ends-with") return likeExpression(field, rule.operator, String(value ?? ""), params);
1575
1598
  if (value === null || value === void 0) return "0";
@@ -1581,7 +1604,7 @@ function compileRule(rule, fields, params) {
1581
1604
  }[rule.operator];
1582
1605
  if (!comparison) return "0";
1583
1606
  params.push(sqlFieldValue(field, value));
1584
- return `${column} ${comparison} ?`;
1607
+ return `COALESCE(${column} ${comparison} ?, 0)`;
1585
1608
  }
1586
1609
  function compileGroup(group, fields, params) {
1587
1610
  const children = group.children.map((child) => child.type === "group" ? compileGroup(child, fields, params) : compileRule(child, fields, params));
@@ -1614,11 +1637,7 @@ function compileEidosFileRowQuery(fields, query = {}) {
1614
1637
  const search = query.search;
1615
1638
  if (search !== void 0 && search !== "") {
1616
1639
  const pattern = `%${escapeLike(search)}%`;
1617
- const searchClauses = searchableFields(fields, query.searchFields).map((field) => {
1618
- params.push(pattern);
1619
- return `eidos_casefold(COALESCE(CAST(${quoteIdentifier(field.tableColumnName)} AS TEXT), ''))
1620
- LIKE eidos_casefold(?) ESCAPE '\\'`;
1621
- });
1640
+ const searchClauses = searchableFields(fields, query.searchFields).map((field) => caseInsensitiveLike(quoteIdentifier(field.tableColumnName), pattern, search, params));
1622
1641
  if (searchClauses.length > 0) where.push(`(${searchClauses.join(" OR ")})`);
1623
1642
  }
1624
1643
  if (query.filter) where.push(compileGroup(query.filter, byColumn, params));
@@ -2034,10 +2053,6 @@ function decodeLookupAtom(value, type) {
2034
2053
  return Object.is(number, -0) ? 0 : number;
2035
2054
  }
2036
2055
  if (type === "checkbox") return value === true || value === 1;
2037
- if (type === "json") {
2038
- const source = typeof value === "string" ? value : JSON.stringify(value);
2039
- return canonicalizeEidosFileJson(JSON.parse(source));
2040
- }
2041
2056
  if (type === "file-entry") {
2042
2057
  const object = typeof value === "string" ? JSON.parse(value) : value;
2043
2058
  if (!object || typeof object !== "object" || Array.isArray(object)) throw new TypeError("Invalid File entry");
@@ -2049,7 +2064,6 @@ function decodeLookupAtom(value, type) {
2049
2064
  function publicLookupAtom(value, type) {
2050
2065
  if (value === null) return null;
2051
2066
  if (type === "integer") return String(value);
2052
- if (type === "json") return String(value);
2053
2067
  return value;
2054
2068
  }
2055
2069
  function sqlLookupAtom(value, type) {
@@ -2147,11 +2161,14 @@ CREATE TABLE eidos__tables(
2147
2161
  AND substr(id,20,1) IN ('8','9','a','b') AND substr(id,24,1)='-'
2148
2162
  AND lower(id)=id AND length(CAST(replace(id,'-','') AS BLOB))=32
2149
2163
  AND replace(id,'-','') NOT GLOB '*[^0-9a-f]*'),
2150
- name TEXT NOT NULL
2151
- CHECK(length(CAST(name AS BLOB)) BETWEEN 1 AND 1024 AND instr(name,char(0))=0),
2164
+ name TEXT NOT NULL COLLATE NOCASE UNIQUE
2165
+ CHECK(length(CAST(name AS BLOB)) BETWEEN 1 AND 1024
2166
+ AND instr(name,char(0))=0
2167
+ AND lower(substr(name,1,7)) NOT IN ('sqlite_','eidos__')),
2152
2168
  physical_name TEXT NOT NULL COLLATE NOCASE UNIQUE
2153
2169
  CHECK(length(CAST(physical_name AS BLOB)) BETWEEN 1 AND 1024
2154
- AND instr(physical_name,char(0))=0),
2170
+ AND instr(physical_name,char(0))=0
2171
+ AND physical_name COLLATE BINARY = name COLLATE BINARY),
2155
2172
  label_field_id TEXT NOT NULL COLLATE BINARY,
2156
2173
  position INTEGER NOT NULL DEFAULT 0,
2157
2174
  settings_json TEXT NOT NULL DEFAULT '{}'
@@ -2189,7 +2206,8 @@ CREATE TABLE eidos__fields(
2189
2206
  physical_name TEXT COLLATE NOCASE
2190
2207
  CHECK(physical_name IS NULL OR
2191
2208
  (length(CAST(physical_name AS BLOB)) BETWEEN 1 AND 1024
2192
- AND instr(physical_name,char(0))=0)),
2209
+ AND instr(physical_name,char(0))=0
2210
+ AND physical_name COLLATE BINARY = name COLLATE BINARY)),
2193
2211
  type TEXT NOT NULL CHECK(type IN (
2194
2212
  'text','number','integer','checkbox','date','datetime','url','json',
2195
2213
  'select','multi-select','file','relation','formula','lookup'
@@ -2409,6 +2427,41 @@ function setEidosFileMetadata(connection, entries) {
2409
2427
  /** Compatibility constant retained for existing callers. */
2410
2428
  const EIDOS_FILE_FORMAT_VERSION_TEXT = "1.0";
2411
2429
  //#endregion
2430
+ //#region src/stable-row-scan.ts
2431
+ const DEFAULT_STABLE_ROW_PAGE_SIZE = 256;
2432
+ function isQueryResultLimit(error) {
2433
+ return typeof error === "object" && error !== null && "code" in error && error.code === "resource-limit" && "message" in error && typeof error.message === "string" && /Query result exceeds maxResult(?:Rows|Bytes)/.test(error.message);
2434
+ }
2435
+ /**
2436
+ * Reads a whole user Table without asking an Adapter to materialize the whole
2437
+ * result at once. Pages follow the canonical binary Row-ID order, and shrink
2438
+ * automatically when a page is still too large for the active Adapter.
2439
+ */
2440
+ function scanStableRowPages(connection, options, visit) {
2441
+ let afterRowId = null;
2442
+ let pageSize = Math.max(1, Math.floor(options.pageSize ?? DEFAULT_STABLE_ROW_PAGE_SIZE));
2443
+ while (true) {
2444
+ let page;
2445
+ while (true) {
2446
+ const where = afterRowId === null ? "" : ` WHERE ${options.rowIdSql} COLLATE BINARY > ?`;
2447
+ const bindings = afterRowId === null ? [pageSize] : [afterRowId, pageSize];
2448
+ try {
2449
+ page = connection.query(`SELECT ${options.columnsSql} FROM ${options.tableSql}${where} ORDER BY ${options.rowIdSql} COLLATE BINARY LIMIT ?`, bindings);
2450
+ break;
2451
+ } catch (error) {
2452
+ if (!isQueryResultLimit(error) || pageSize === 1) throw error;
2453
+ pageSize = Math.max(1, Math.floor(pageSize / 2));
2454
+ }
2455
+ }
2456
+ if (page.length === 0) return;
2457
+ visit(page);
2458
+ const nextRowId = page.at(-1)?.[options.rowIdKey];
2459
+ if (typeof nextRowId !== "string" || nextRowId === afterRowId) throw new Error("Stable row scan requires unique text Row IDs");
2460
+ afterRowId = nextRowId;
2461
+ if (page.length < pageSize) return;
2462
+ }
2463
+ }
2464
+ //#endregion
2412
2465
  //#region src/dependency-graph.ts
2413
2466
  /** Returns the globally smallest normalized simple directed cycle. */
2414
2467
  function smallestDependencyCycle(graph) {
@@ -2536,7 +2589,6 @@ const FIELD_TYPES = new Set([
2536
2589
  "datetime",
2537
2590
  "url",
2538
2591
  "file",
2539
- "json",
2540
2592
  "select",
2541
2593
  "multi-select",
2542
2594
  "relation",
@@ -2560,8 +2612,7 @@ const FORMULA_RESULT_TYPES = new Set([
2560
2612
  "checkbox",
2561
2613
  "date",
2562
2614
  "datetime",
2563
- "url",
2564
- "json"
2615
+ "url"
2565
2616
  ]);
2566
2617
  const NUMERIC_LOOKUP_TYPES = new Set(["number", "integer"]);
2567
2618
  const FILTER_OPERATORS = new Set([
@@ -2605,32 +2656,8 @@ function valueKind(field, relation) {
2605
2656
  if (field.type === "relation") return relation?.direction === "inverse" ? "derived" : "relation";
2606
2657
  return "source";
2607
2658
  }
2608
- function validPhysicalMapping(kind, name, physicalName, id) {
2609
- const folded = name.replace(/[A-Z]/g, (character) => character.toLowerCase());
2610
- const reserved = kind === "table" ? [
2611
- "sqlite_",
2612
- "eidos__",
2613
- "x__"
2614
- ].some((prefix) => folded.startsWith(prefix)) : [
2615
- "_id",
2616
- "_created_at",
2617
- "_updated_at"
2618
- ].includes(folded);
2619
- const hex = id.replace(/-/g, "");
2620
- if (kind === "table" && reserved) return [
2621
- 8,
2622
- 12,
2623
- 32
2624
- ].some((length) => {
2625
- const prefix = `t__${hex.slice(0, length)}__`;
2626
- return physicalName.startsWith(prefix) && new TextEncoder().encode(physicalName).byteLength <= 1024;
2627
- });
2628
- if (physicalName === name) return !reserved;
2629
- return [
2630
- 8,
2631
- 12,
2632
- 32
2633
- ].some((length) => physicalName === `${name}__${hex.slice(0, length)}`);
2659
+ function validPhysicalMapping(kind, name, physicalName) {
2660
+ return physicalName === name && (kind === "field" || !isEidosFileReservedTableName(name));
2634
2661
  }
2635
2662
  function expectedColumnType(type) {
2636
2663
  if ([
@@ -2640,7 +2667,6 @@ function expectedColumnType(type) {
2640
2667
  "date",
2641
2668
  "datetime",
2642
2669
  "file",
2643
- "json",
2644
2670
  "multi-select",
2645
2671
  "relation"
2646
2672
  ].includes(type)) return "TEXT";
@@ -2760,6 +2786,7 @@ function validateEidosFile(connection, options = {}) {
2760
2786
  const tableRows = sqliteTables.has("eidos__tables") ? connection.query(`SELECT * FROM ${EIDOS_FILE_TABLES_TABLE} ORDER BY position, id`) : [];
2761
2787
  const tables = [];
2762
2788
  const tableRowsById = /* @__PURE__ */ new Map();
2789
+ const tableNames = /* @__PURE__ */ new Set();
2763
2790
  for (const row of tableRows) try {
2764
2791
  const id = uuid$1(row.id, "Table ID");
2765
2792
  tableRowsById.set(id, row);
@@ -2778,7 +2805,10 @@ function validateEidosFile(connection, options = {}) {
2778
2805
  });
2779
2806
  if (!isCanonicalEidosFileInstant(row.created_at) || !isCanonicalEidosFileInstant(row.updated_at)) add(errors, "invalid-schema", `Table ${row.name} metadata timestamps are not canonical instants`, row.physical_name);
2780
2807
  if (!isCanonicalEidosFileJson(row.settings_json)) add(errors, "invalid-schema", `Table ${row.name} has non-canonical settings_json`, row.physical_name);
2781
- if (!validPhysicalMapping("table", row.name, row.physical_name, id)) add(errors, "invalid-schema", `Table ${row.name} has an invalid physical name`, row.physical_name);
2808
+ const foldedTableName = row.name.replace(/[A-Z]/g, (character) => character.toLowerCase());
2809
+ if (tableNames.has(foldedTableName)) add(errors, "invalid-schema", `Duplicate Table name under SQLite NOCASE: ${row.name}`, row.physical_name);
2810
+ tableNames.add(foldedTableName);
2811
+ if (!validPhysicalMapping("table", row.name, row.physical_name)) add(errors, "invalid-schema", `Table ${row.name} has an invalid physical name`, row.physical_name);
2782
2812
  const tableObject = sqliteTables.get(row.physical_name);
2783
2813
  if (!tableObject) add(errors, "invalid-schema", `Missing user table ${row.physical_name}`, row.physical_name);
2784
2814
  else if (!/\bSTRICT\b/i.test(tableObject.sql ?? "")) add(errors, "invalid-schema", `User table ${row.physical_name} must be STRICT`, row.physical_name);
@@ -2827,8 +2857,7 @@ function validateEidosFile(connection, options = {}) {
2827
2857
  if (requiresNonNull && row.nullable !== 0 || requiresNullable && row.nullable !== 1) add(errors, "invalid-schema", `Field ${row.name} has invalid nullable metadata for ${row.type}`);
2828
2858
  if (virtual && row.physical_name !== null) add(errors, "invalid-schema", `Virtual Field ${row.name} must not have a physical column`);
2829
2859
  if (!virtual && row.physical_name === null) add(errors, "invalid-schema", `Stored Field ${row.name} requires a physical column`);
2830
- const systemMapping = row.system_role === "row-id" && row.type === "text" && row.physical_name === "_id" && row.nullable === 0 || row.system_role === "created-time" && row.type === "datetime" && row.physical_name === "_created_at" && row.nullable === 0 || row.system_role === "updated-time" && row.type === "datetime" && row.physical_name === "_updated_at" && row.nullable === 0;
2831
- if (row.physical_name && !systemMapping && !validPhysicalMapping("field", row.name, row.physical_name, id)) add(errors, "invalid-schema", `Field ${row.name} has an invalid physical name`);
2860
+ if (row.physical_name && !validPhysicalMapping("field", row.name, row.physical_name)) add(errors, "invalid-schema", `Field ${row.name} has an invalid physical name`);
2832
2861
  const formula = formulas.get(id);
2833
2862
  const lookup = lookups.get(id);
2834
2863
  const property = formula ? {
@@ -2882,8 +2911,26 @@ function validateEidosFile(connection, options = {}) {
2882
2911
  ["last-edited-time", "_updated_at"]
2883
2912
  ]) if (fields.filter((field) => field.type === type && field.physicalName === physicalName).length !== 1) add(errors, "invalid-schema", `Table ${table.name} must register exactly one ${type} Field mapped to ${physicalName}`);
2884
2913
  const mappedNames = fields.flatMap((field) => field.physicalName ? [field.physicalName] : []);
2914
+ const displayNames = fields.map((field) => field.name);
2915
+ if (new Set(displayNames.map((name) => name.replace(/[A-Z]/g, (character) => character.toLowerCase()))).size !== displayNames.length) add(errors, "invalid-schema", `Table ${table.name} contains duplicate Field names under SQLite NOCASE`);
2885
2916
  if (new Set(mappedNames.map((name) => name.replace(/[A-Z]/g, (character) => character.toLowerCase()))).size !== mappedNames.length) add(errors, "invalid-schema", `Table ${table.name} maps more than one Field to a physical column`);
2886
2917
  }
2918
+ if (sqliteTables.has("eidos__views")) {
2919
+ const viewNamesByTable = /* @__PURE__ */ new Map();
2920
+ for (const view of connection.query(`SELECT id, table_id, name FROM ${EIDOS_FILE_VIEWS_TABLE}`)) try {
2921
+ uuid$1(view.id, "View ID");
2922
+ const tableId = uuid$1(view.table_id, "View Table ID");
2923
+ assertEidosFileDisplayName(view.name, "View name");
2924
+ if (!tableRowsById.has(tableId)) throw new Error("View references an unknown Table");
2925
+ const foldedName = view.name.replace(/[A-Z]/g, (character) => character.toLowerCase());
2926
+ const names = viewNamesByTable.get(tableId) ?? /* @__PURE__ */ new Set();
2927
+ if (names.has(foldedName)) add(errors, "invalid-schema", `Table ${tableRowsById.get(tableId).name} contains duplicate View names under SQLite NOCASE`);
2928
+ names.add(foldedName);
2929
+ viewNamesByTable.set(tableId, names);
2930
+ } catch (error) {
2931
+ add(errors, "invalid-schema", error instanceof Error ? error.message : "Invalid View metadata");
2932
+ }
2933
+ }
2887
2934
  const triggerObjects = new Map(sqliteObjects.filter((object) => object.type === "trigger").map((object) => [object.name, object]));
2888
2935
  const expectedTriggers = /* @__PURE__ */ new Map();
2889
2936
  for (const [tableId, table] of tableRowsById) expectedTriggers.set(`eidos__row_id_immutable__${tableId.replace(/-/g, "")}`, {
@@ -3174,16 +3221,16 @@ function validateEidosFile(connection, options = {}) {
3174
3221
  "last-edited-time"
3175
3222
  ].includes(field.type) && !isCanonicalEidosFileInstant(value)) add(errors, "invalid-value", `${table.name}.${field.name} must be canonical UTC millisecond text`);
3176
3223
  if (["multi-select", "file"].includes(field.type)) {
3177
- const parsed = typeof value === "string" && isCanonicalEidosFileJson(value) ? parseEidosFileJson(value) : null;
3178
- if (!Array.isArray(parsed)) add(errors, "invalid-value", `${table.name}.${field.name} must be a canonical JSON array`);
3179
- if (field.type === "multi-select" && Array.isArray(parsed) && (!parsed.every((entry) => typeof entry === "string") || new Set(parsed).size !== parsed.length)) add(errors, "invalid-value", `${table.name}.${field.name} must be a unique string array`);
3180
- if (field.type === "file" && Array.isArray(parsed)) try {
3181
- assertEidosFileValues(parsed);
3224
+ let parsed = null;
3225
+ if (field.type === "file" && typeof value === "string") try {
3226
+ parsed = decodeEidosFileValues(value);
3182
3227
  } catch (error) {
3183
3228
  add(errors, "invalid-value", `${table.name}.${field.name}: ${error instanceof Error ? error.message : "invalid File value"}`);
3184
3229
  }
3230
+ else if (typeof value === "string" && isCanonicalEidosFileJson(value)) parsed = parseEidosFileJson(value);
3231
+ if (!Array.isArray(parsed)) add(errors, "invalid-value", `${table.name}.${field.name} must be a canonical JSON array`);
3232
+ if (field.type === "multi-select" && Array.isArray(parsed) && (!parsed.every((entry) => typeof entry === "string") || new Set(parsed).size !== parsed.length)) add(errors, "invalid-value", `${table.name}.${field.name} must be a unique string array`);
3185
3233
  }
3186
- if (field.type === "json" && (typeof value !== "string" || !isCanonicalEidosFileJson(value))) add(errors, "invalid-value", `${table.name}.${field.name} must be canonical JSON`);
3187
3234
  if (field.type === "relation") {
3188
3235
  const ids = relationIds(value);
3189
3236
  const relation = relations.get(field.id);
@@ -3221,11 +3268,16 @@ function validateEidosFile(connection, options = {}) {
3221
3268
  }
3222
3269
  //#endregion
3223
3270
  //#region src/runtime.ts
3271
+ const ROW_MUTATION_UNDO_ENTRIES_MAX = 50;
3272
+ const ROW_MUTATION_UNDO_BYTES_MAX = 64 * 1024 * 1024;
3224
3273
  function sameSqlValue(left, right) {
3225
3274
  if (left instanceof Uint8Array && right instanceof Uint8Array) return left.byteLength === right.byteLength && left.every((value, index) => value === right[index]);
3226
3275
  if ((typeof left === "number" || typeof left === "bigint") && (typeof right === "number" || typeof right === "bigint") && (typeof left === "bigint" || Number.isInteger(left)) && (typeof right === "bigint" || Number.isInteger(right))) return BigInt(left) === BigInt(right);
3227
3276
  return Object.is(left, right);
3228
3277
  }
3278
+ function systemFieldsLast(fields) {
3279
+ return [...fields.filter((field) => field.systemRole === null || field.systemRole === void 0), ...fields.filter((field) => field.systemRole !== null && field.systemRole !== void 0)];
3280
+ }
3229
3281
  const SYSTEM_FIELD_NAMES = {
3230
3282
  "row-id": "_id",
3231
3283
  "created-time": "_created_at",
@@ -3306,7 +3358,6 @@ function fieldColumnSql(field) {
3306
3358
  case "datetime": return `${column} TEXT${field.nullable ? "" : " NOT NULL"} CHECK(${eidosFileInstantSqlCheck(column)})`;
3307
3359
  case "rating": return `${column} INTEGER`;
3308
3360
  case "checkbox": return `${column} INTEGER${field.nullable ? "" : " NOT NULL"} CHECK(${column} IS NULL OR ${column} IN (0, 1))`;
3309
- case "json": return `${column} TEXT${field.nullable ? "" : " NOT NULL"} CHECK(${column} IS NULL OR json_valid(${column}))`;
3310
3361
  case "file":
3311
3362
  case "multi-select":
3312
3363
  case "relation": return `${column} TEXT NOT NULL DEFAULT '[]' CHECK(json_valid(${column}) AND json_type(${column}) = 'array')`;
@@ -3441,6 +3492,53 @@ function compileKeysetAfter(sorts, values, lastId) {
3441
3492
  params
3442
3493
  };
3443
3494
  }
3495
+ function compileKeysetBefore(sorts, values, firstId) {
3496
+ if (values.length !== sorts.length) throw new EidosFileError("invalid-query", "Sort boundary is invalid");
3497
+ const params = [];
3498
+ const branches = [];
3499
+ sorts.forEach(({ field, sort }, index) => {
3500
+ const expression = eidosFileSortExpression(field);
3501
+ const value = values[index] ?? null;
3502
+ let before;
3503
+ if (value === null) before = sort.nulls === "last" ? `${expression} IS NOT NULL` : "0";
3504
+ else {
3505
+ const comparison = sort.direction === "desc" ? ">" : "<";
3506
+ before = sort.nulls === "first" ? `(${expression} ${comparison} ? OR ${expression} IS NULL)` : `${expression} ${comparison} ?`;
3507
+ }
3508
+ if (before !== "0") {
3509
+ const parts = [];
3510
+ for (let prefixIndex = 0; prefixIndex < index; prefixIndex += 1) {
3511
+ const prefixExpression = eidosFileSortExpression(sorts[prefixIndex].field);
3512
+ const prefixValue = values[prefixIndex] ?? null;
3513
+ if (prefixValue === null) parts.push(`${prefixExpression} IS NULL`);
3514
+ else {
3515
+ parts.push(`${prefixExpression} IS ?`);
3516
+ params.push(prefixValue);
3517
+ }
3518
+ }
3519
+ parts.push(before);
3520
+ if (value !== null) params.push(value);
3521
+ branches.push(`(${parts.join(" AND ")})`);
3522
+ }
3523
+ });
3524
+ const finalParts = [];
3525
+ sorts.forEach(({ field }, index) => {
3526
+ const expression = eidosFileSortExpression(field);
3527
+ const value = values[index] ?? null;
3528
+ if (value === null) finalParts.push(`${expression} IS NULL`);
3529
+ else {
3530
+ finalParts.push(`${expression} IS ?`);
3531
+ params.push(value);
3532
+ }
3533
+ });
3534
+ finalParts.push("\"__base_rowid\" < ?");
3535
+ params.push(assertEidosFileUuid(firstId, "Row ID"));
3536
+ branches.push(`(${finalParts.join(" AND ")})`);
3537
+ return {
3538
+ sql: `(${branches.join(" OR ")})`,
3539
+ params
3540
+ };
3541
+ }
3444
3542
  function filterToStorage(filter, fields) {
3445
3543
  const normalized = normalizeEidosFileFilter(filter);
3446
3544
  if (!normalized) return null;
@@ -3502,6 +3600,9 @@ function filterFromStorage(value, fields) {
3502
3600
  var EidosFileRuntime = class {
3503
3601
  mutationDepth = 0;
3504
3602
  mutationInstant = null;
3603
+ rowMutationUndoEntries = /* @__PURE__ */ new Map();
3604
+ rowMutationUndoBytes = 0;
3605
+ nestedMutationInvalidatesRowUndo = false;
3505
3606
  schemaCache;
3506
3607
  nowInstant;
3507
3608
  allocateId;
@@ -3516,6 +3617,7 @@ var EidosFileRuntime = class {
3516
3617
  registerEidosLookupFunctions(connection);
3517
3618
  }
3518
3619
  close() {
3620
+ this.clearRowMutationUndoEntries();
3519
3621
  if (this.ownsConnection) this.connection.close?.();
3520
3622
  }
3521
3623
  inspect() {
@@ -3703,29 +3805,46 @@ var EidosFileRuntime = class {
3703
3805
  applyCanonicalMutation(operation, expectedRevision) {
3704
3806
  return this.mutate(() => operation(this.operationInstant()), expectedRevision);
3705
3807
  }
3706
- mutate(operation, expectedRevision) {
3707
- if (this.mutationDepth > 0) return operation();
3708
- return this.connection.transaction(() => {
3709
- this.schemaCache = void 0;
3710
- const revision = this.info().revision ?? 0;
3711
- if (expectedRevision !== void 0 && BigInt(revision) !== BigInt(expectedRevision)) throw new EidosFileError("stale-revision", `Expected revision ${expectedRevision}, found ${revision}`);
3712
- const totalChangesBefore = this.connection.get("SELECT total_changes() AS total")?.total ?? 0;
3713
- this.mutationInstant = this.nowInstant();
3714
- this.mutationDepth += 1;
3715
- try {
3716
- const result = operation();
3717
- const totalChangesAfter = this.connection.get("SELECT total_changes() AS total")?.total ?? totalChangesBefore;
3718
- if (BigInt(totalChangesAfter) === BigInt(totalChangesBefore)) return result;
3719
- const validation = validateEidosFile(this.connection, { level: "semantic" });
3720
- if (!validation.valid) throw new EidosFileError("invalid-schema", validation.errors.map((issue) => issue.message).join("; "));
3721
- incrementEidosFileRevision(this.connection, this.operationInstant());
3722
- return result;
3723
- } finally {
3724
- this.mutationDepth -= 1;
3725
- this.mutationInstant = null;
3808
+ mutate(operation, expectedRevision, invalidatesRowUndo = false) {
3809
+ if (this.mutationDepth > 0) {
3810
+ const result = operation();
3811
+ if (invalidatesRowUndo) this.nestedMutationInvalidatesRowUndo = true;
3812
+ return result;
3813
+ }
3814
+ const previousInvalidation = this.nestedMutationInvalidatesRowUndo;
3815
+ this.nestedMutationInvalidatesRowUndo = false;
3816
+ let clearRowUndoAfterCommit = false;
3817
+ try {
3818
+ const result = this.connection.transaction(() => {
3726
3819
  this.schemaCache = void 0;
3727
- }
3728
- });
3820
+ const revision = this.info().revision ?? 0;
3821
+ if (expectedRevision !== void 0 && BigInt(revision) !== BigInt(expectedRevision)) throw new EidosFileError("stale-revision", `Expected revision ${expectedRevision}, found ${revision}`);
3822
+ const totalChangesBefore = this.connection.get("SELECT total_changes() AS total")?.total ?? 0;
3823
+ this.mutationInstant = this.nowInstant();
3824
+ this.mutationDepth += 1;
3825
+ try {
3826
+ const operationResult = operation();
3827
+ const totalChangesAfter = this.connection.get("SELECT total_changes() AS total")?.total ?? totalChangesBefore;
3828
+ if (BigInt(totalChangesAfter) === BigInt(totalChangesBefore)) return operationResult;
3829
+ const validation = validateEidosFile(this.connection, { level: "semantic" });
3830
+ if (!validation.valid) throw new EidosFileError("invalid-schema", validation.errors.map((issue) => issue.message).join("; "));
3831
+ incrementEidosFileRevision(this.connection, this.operationInstant());
3832
+ clearRowUndoAfterCommit = invalidatesRowUndo || this.nestedMutationInvalidatesRowUndo;
3833
+ return operationResult;
3834
+ } finally {
3835
+ this.mutationDepth -= 1;
3836
+ this.mutationInstant = null;
3837
+ this.schemaCache = void 0;
3838
+ }
3839
+ });
3840
+ if (clearRowUndoAfterCommit) this.clearRowMutationUndoEntries();
3841
+ return result;
3842
+ } finally {
3843
+ this.nestedMutationInvalidatesRowUndo = previousInvalidation;
3844
+ }
3845
+ }
3846
+ mutateSchemaState(operation) {
3847
+ return this.mutate(operation, void 0, true);
3729
3848
  }
3730
3849
  operationInstant() {
3731
3850
  return this.mutationInstant ?? this.nowInstant();
@@ -3780,23 +3899,20 @@ var EidosFileRuntime = class {
3780
3899
  const firstScalar = userFields.find((field) => LABEL_TYPES.has(field.type) && !isVirtualField(field.type, field.input));
3781
3900
  (firstScalar ?? system[0]).isRecordLabel = true;
3782
3901
  }
3783
- const existing = new Set(system.map((field) => field.physicalName));
3784
3902
  for (const field of userFields) {
3785
3903
  if (isVirtualField(field.type, field.input)) continue;
3786
- field.physicalName = eidosFilePhysicalName("field", field.name, field.id, existing);
3787
- existing.add(field.physicalName);
3904
+ field.physicalName = field.name;
3788
3905
  }
3789
3906
  return [...system, ...userFields];
3790
3907
  }
3791
3908
  createTable(input) {
3792
- assertEidosFileDisplayName(input.name, "Table name");
3909
+ const physicalName = assertEidosFileTableName(input.name);
3910
+ if (this.listTables().some((table) => table.name.replace(/[A-Z]/g, (character) => character.toLowerCase()) === input.name.replace(/[A-Z]/g, (character) => character.toLowerCase()))) throw new EidosFileError("constraint-conflict", `Duplicate Table name: ${input.name}`);
3793
3911
  const tableId = input.id ?? this.allocateId();
3794
3912
  assertEidosFileUuid(tableId, "Table ID");
3795
- const existing = this.listTables().map((table) => table.physicalName ?? table.rawTableName);
3796
- const physicalName = eidosFilePhysicalName("table", input.name, tableId, existing);
3797
3913
  const fields = this.prepareFields(input);
3798
3914
  const position = this.listTables().length;
3799
- return this.mutate(() => {
3915
+ return this.mutateSchemaState(() => {
3800
3916
  const now = this.operationInstant();
3801
3917
  const definitions = fields.flatMap((field) => {
3802
3918
  const definition = fieldColumnSql(field);
@@ -3832,7 +3948,7 @@ var EidosFileRuntime = class {
3832
3948
  for (const field of fields) if (field.type === "relation" && field.input) this.installRelationTriggers(field.id);
3833
3949
  if (input.createDefaultView !== false) {
3834
3950
  const viewId = this.allocateId();
3835
- const fieldOrder = fields.map((field) => field.id);
3951
+ const fieldOrder = systemFieldsLast(fields).map((field) => field.id);
3836
3952
  this.connection.run(`INSERT INTO ${EIDOS_FILE_VIEWS_TABLE}
3837
3953
  (id, table_id, name, type, query_json, layout_json, position, created_at, updated_at)
3838
3954
  VALUES (?, ?, ?, 'grid', '{}', ?, 0, ?, ?)`, [
@@ -4011,19 +4127,20 @@ var EidosFileRuntime = class {
4011
4127
  }
4012
4128
  updateTable(tableId, changes) {
4013
4129
  const table = this.getTable(tableId);
4014
- return this.mutate(() => {
4130
+ return this.mutateSchemaState(() => {
4015
4131
  let name = table.name;
4016
4132
  let physicalName = table.physicalName ?? table.rawTableName;
4017
4133
  if (changes.name !== void 0 && changes.name !== table.name) {
4018
- name = assertEidosFileDisplayName(changes.name, "Table name");
4019
- physicalName = eidosFilePhysicalName("table", name, tableId, this.listTables().filter((candidate) => candidate.id !== tableId).map((candidate) => candidate.physicalName ?? candidate.rawTableName));
4134
+ name = assertEidosFileTableName(changes.name);
4135
+ if (this.listTables().some((candidate) => candidate.id !== tableId && candidate.name.replace(/[A-Z]/g, (character) => character.toLowerCase()) === name.replace(/[A-Z]/g, (character) => character.toLowerCase()))) throw new EidosFileError("constraint-conflict", `Duplicate Table name: ${name}`);
4136
+ physicalName = name;
4020
4137
  if (physicalName !== (table.physicalName ?? table.rawTableName)) {
4021
4138
  const previousPhysicalName = table.physicalName ?? table.rawTableName;
4022
4139
  if (previousPhysicalName !== physicalName && previousPhysicalName.replace(/[A-Z]/g, (character) => character.toLowerCase()) === physicalName.replace(/[A-Z]/g, (character) => character.toLowerCase())) {
4023
- const baseTemporary = `t__rename__${tableId.replace(/-/g, "")}`;
4140
+ const baseTemporary = `eidos__rename_table__${tableId.replace(/-/g, "")}`;
4024
4141
  let temporary = baseTemporary;
4025
4142
  let suffix = 0;
4026
- const occupied = new Set(this.listTables().map((candidate) => (candidate.physicalName ?? candidate.rawTableName).replace(/[A-Z]/g, (character) => character.toLowerCase())));
4143
+ const occupied = new Set(this.connection.query("SELECT name FROM sqlite_schema").map((object) => object.name.replace(/[A-Z]/g, (character) => character.toLowerCase())));
4027
4144
  while (occupied.has(temporary.toLowerCase())) {
4028
4145
  suffix += 1;
4029
4146
  temporary = `${baseTemporary}__${suffix}`;
@@ -4052,7 +4169,7 @@ var EidosFileRuntime = class {
4052
4169
  }
4053
4170
  deleteTable(tableId) {
4054
4171
  const table = this.getTable(tableId);
4055
- return this.mutate(() => {
4172
+ return this.mutateSchemaState(() => {
4056
4173
  this.connection.exec(`DROP TABLE ${quoteIdentifier(table.physicalName ?? table.rawTableName)}`);
4057
4174
  return this.connection.run(`DELETE FROM ${EIDOS_FILE_TABLES_TABLE} WHERE id = ?`, [tableId]).changes > 0;
4058
4175
  });
@@ -4065,7 +4182,7 @@ var EidosFileRuntime = class {
4065
4182
  const type = canonicalFieldType(input);
4066
4183
  const fields = this.listFields(tableId);
4067
4184
  if (fields.some((field) => field.name.replace(/[A-Z]/g, (character) => character.toLowerCase()) === input.name.replace(/[A-Z]/g, (character) => character.toLowerCase()))) throw new EidosFileError("constraint-conflict", `Duplicate Field name: ${input.name}`);
4068
- const physicalName = isVirtualField(type, input) ? null : eidosFilePhysicalName("field", input.name, id, fields.flatMap((field) => field.physicalName ? [field.physicalName] : []));
4185
+ const physicalName = isVirtualField(type, input) ? null : input.name;
4069
4186
  const prepared = {
4070
4187
  id,
4071
4188
  name: input.name,
@@ -4085,7 +4202,7 @@ var EidosFileRuntime = class {
4085
4202
  "multi-select",
4086
4203
  "relation"
4087
4204
  ].includes(prepared.type) && this.countRows(tableId) > 0) throw new EidosFileError("constraint-conflict", "A populated Table cannot add a non-null scalar Field without a default");
4088
- return this.mutate(() => {
4205
+ return this.mutateSchemaState(() => {
4089
4206
  if (prepared.isRecordLabel && prepared.type === "lookup") throw new EidosFileError("invalid-schema", "A Lookup cannot be the Record Label Field in Eidos File 1.0");
4090
4207
  const definition = fieldColumnSql(prepared);
4091
4208
  if (definition) this.connection.exec(`ALTER TABLE ${quoteIdentifier(table.physicalName ?? table.rawTableName)} ADD COLUMN ${definition}`);
@@ -4128,15 +4245,14 @@ var EidosFileRuntime = class {
4128
4245
  const field = this.fieldByKey(tableId, fieldKey);
4129
4246
  const fieldId = field.id;
4130
4247
  if (changes.type !== void 0 && canonicalFieldType({ type: changes.type }) !== field.type) throw new EidosFileError("invalid-schema", "Field type conversion requires an explicit canonical conversion operation");
4131
- return this.mutate(() => {
4248
+ return this.mutateSchemaState(() => {
4132
4249
  let name = field.name;
4133
4250
  let physicalName = field.physicalName ?? null;
4134
4251
  if (changes.name !== void 0 && changes.name !== field.name) {
4135
4252
  name = assertEidosFileDisplayName(changes.name, "Field name");
4136
- const fields = this.listFields(tableId);
4137
- if (fields.some((candidate) => candidate.id !== field.id && candidate.name.replace(/[A-Z]/g, (character) => character.toLowerCase()) === name.replace(/[A-Z]/g, (character) => character.toLowerCase()))) throw new EidosFileError("constraint-conflict", `Duplicate Field name: ${name}`);
4253
+ if (this.listFields(tableId).some((candidate) => candidate.id !== field.id && candidate.name.replace(/[A-Z]/g, (character) => character.toLowerCase()) === name.replace(/[A-Z]/g, (character) => character.toLowerCase()))) throw new EidosFileError("constraint-conflict", `Duplicate Field name: ${name}`);
4138
4254
  if (field.physicalName) {
4139
- physicalName = eidosFilePhysicalName("field", name, fieldId, fields.filter((candidate) => candidate.id !== field.id).flatMap((candidate) => candidate.physicalName ? [candidate.physicalName] : []));
4255
+ physicalName = name;
4140
4256
  if (physicalName !== field.physicalName) this.connection.exec(`ALTER TABLE ${quoteIdentifier(table.physicalName ?? table.rawTableName)} RENAME COLUMN ${quoteIdentifier(field.physicalName)} TO ${quoteIdentifier(physicalName)}`);
4141
4257
  }
4142
4258
  const formulaRows = this.connection.query(`SELECT formula.field_id, formula.source_text
@@ -4302,7 +4418,7 @@ var EidosFileRuntime = class {
4302
4418
  "lookup"
4303
4419
  ].includes(field.type)) throw new EidosFileError("protected-field", "Only stored scalar and JSON Fields have configurable nullability");
4304
4420
  if ((field.nullable ?? true) === nullable) return field;
4305
- return this.mutate(() => {
4421
+ return this.mutateSchemaState(() => {
4306
4422
  const table = this.getTable(field.tableId);
4307
4423
  if (!nullable) {
4308
4424
  const nulls = this.connection.get("SELECT count(*) AS count FROM " + quoteIdentifier(table.physicalName ?? table.rawTableName) + " WHERE " + quoteIdentifier(field.physicalName) + " IS NULL")?.count ?? 0;
@@ -4314,6 +4430,21 @@ var EidosFileRuntime = class {
4314
4430
  });
4315
4431
  }
4316
4432
  /** @internal Applies a preflighted Runtime 1.0 stored-type conversion. */
4433
+ convertStoredFieldMetadataOnly(fieldId, targetType, nullable) {
4434
+ const field = Array.from(this.allSchema().fields.values()).find((candidate) => candidate.id === fieldId);
4435
+ if (!field || !field.physicalName || ![
4436
+ "text",
4437
+ "select",
4438
+ "url"
4439
+ ].includes(field.type) || (field.nullable ?? false) !== nullable) throw new EidosFileError("invalid-schema", "Metadata-only conversion requires an unchanged Text, Select, or URL storage shape");
4440
+ return this.mutateSchemaState(() => {
4441
+ this.connection.run(`UPDATE ${EIDOS_FILE_FIELDS_TABLE} SET type = ? WHERE id = ?`, [targetType, fieldId]);
4442
+ this.connection.run(`UPDATE ${EIDOS_FILE_TABLES_TABLE} SET updated_at = ? WHERE id = ?`, [this.operationInstant(), field.tableId]);
4443
+ this.schemaCache = void 0;
4444
+ return this.fieldByKey(field.tableId, fieldId);
4445
+ });
4446
+ }
4447
+ /** @internal Applies a preflighted Runtime 1.0 stored-type conversion. */
4317
4448
  convertStoredField(fieldId, targetType, nullable, rows, relation) {
4318
4449
  const field = Array.from(this.allSchema().fields.values()).find((candidate) => candidate.id === fieldId);
4319
4450
  if (!field || field.systemRole || !field.physicalName || ["formula", "lookup"].includes(field.type) || [
@@ -4326,7 +4457,7 @@ var EidosFileRuntime = class {
4326
4457
  if (targetType === "relation" && !relation) throw new EidosFileError("invalid-schema", "Relation conversion requires a forward Relation definition");
4327
4458
  const expectedRows = this.countRows(field.tableId);
4328
4459
  if (rows.length !== expectedRows || new Set(rows.map((row) => row.id)).size !== rows.length) throw new EidosFileError("stale-revision", "Conversion rows no longer match the Table");
4329
- return this.mutate(() => {
4460
+ return this.mutateSchemaState(() => {
4330
4461
  const relationFieldIdsBefore = Array.from(this.allSchema().relations.keys());
4331
4462
  for (const relationFieldId of relationFieldIdsBefore) this.dropRelationTriggers(relationFieldId);
4332
4463
  this.connection.run(`DELETE FROM ${EIDOS_FILE_RELATION_FIELDS_TABLE} WHERE field_id = ?`, [fieldId]);
@@ -4368,8 +4499,8 @@ var EidosFileRuntime = class {
4368
4499
  systemRole: field.systemRole ?? null,
4369
4500
  nullable: field.nullable ?? false
4370
4501
  }));
4371
- const temporary = this.transientTableName("t__rebuild__" + tableId.replace(/-/g, ""));
4372
- const staging = this.transientTableName("t__rebuild_old__" + tableId.replace(/-/g, ""));
4502
+ const temporary = this.transientTableName("eidos__rebuild_table__" + tableId.replace(/-/g, ""));
4503
+ const staging = this.transientTableName("eidos__rebuild_old_table__" + tableId.replace(/-/g, ""));
4373
4504
  const relationFieldIds = Array.from(new Set([...relationFieldIdsBefore ?? [], ...Array.from(this.allSchema().relations.keys())]));
4374
4505
  for (const relationFieldId of relationFieldIds) this.dropRelationTriggers(relationFieldId);
4375
4506
  const organization = this.connection.get("SELECT sql FROM sqlite_schema WHERE type='table' AND name=?", [physicalName])?.sql;
@@ -4377,20 +4508,27 @@ var EidosFileRuntime = class {
4377
4508
  this.connection.exec("CREATE TABLE " + quoteIdentifier(temporary) + " (" + definitions.join(", ") + `) STRICT${withoutRowid ? ", WITHOUT ROWID" : ""}`);
4378
4509
  const columns = stored.map((field) => quoteIdentifier(field.physicalName));
4379
4510
  if (transformed) {
4380
- const parameterSets = this.connection.query(`SELECT * FROM ${quoteIdentifier(physicalName)} ORDER BY "_id" COLLATE BINARY`).map((row) => {
4381
- const rowId = String(row._id);
4382
- const conversion = transformed.rows.get(rowId);
4383
- if (!conversion) throw new EidosFileError("stale-revision", "Conversion row set changed before apply");
4384
- return stored.map((storedField) => {
4385
- if (storedField.id === transformed.fieldId) return conversion.value;
4386
- if (storedField.systemRole === "updated-time" && conversion.changed) return this.operationInstant();
4387
- return row[storedField.physicalName];
4388
- });
4389
- });
4390
4511
  const placeholders = columns.map(() => "?").join(", ");
4391
4512
  const sql = `INSERT INTO ${quoteIdentifier(temporary)} (${columns.join(", ")}) VALUES (${placeholders})`;
4392
- if (this.connection.runMany) this.connection.runMany(sql, parameterSets);
4393
- else for (const parameters of parameterSets) this.connection.run(sql, parameters);
4513
+ scanStableRowPages(this.connection, {
4514
+ columnsSql: "*",
4515
+ tableSql: quoteIdentifier(physicalName),
4516
+ rowIdSql: `"_id"`,
4517
+ rowIdKey: "_id"
4518
+ }, (sourceRows) => {
4519
+ const parameterSets = sourceRows.map((row) => {
4520
+ const rowId = String(row._id);
4521
+ const conversion = transformed.rows.get(rowId);
4522
+ if (!conversion) throw new EidosFileError("stale-revision", "Conversion row set changed before apply");
4523
+ return stored.map((storedField) => {
4524
+ if (storedField.id === transformed.fieldId) return conversion.value;
4525
+ if (storedField.systemRole === "updated-time" && conversion.changed) return this.operationInstant();
4526
+ return row[storedField.physicalName];
4527
+ });
4528
+ });
4529
+ if (this.connection.runMany) this.connection.runMany(sql, parameterSets);
4530
+ else for (const parameters of parameterSets) this.connection.run(sql, parameters);
4531
+ });
4394
4532
  } else this.connection.exec("INSERT INTO " + quoteIdentifier(temporary) + " (" + columns.join(", ") + ") SELECT " + columns.join(", ") + " FROM " + quoteIdentifier(physicalName));
4395
4533
  this.connection.exec(`DROP TRIGGER IF EXISTS ${quoteIdentifier(`eidos__row_id_immutable__${tableId.replace(/-/g, "")}`)}`);
4396
4534
  this.connection.exec(`ALTER TABLE ${quoteIdentifier(physicalName)} RENAME TO ${quoteIdentifier(staging)}`);
@@ -4409,6 +4547,31 @@ var EidosFileRuntime = class {
4409
4547
  }
4410
4548
  throw new EidosFileError("resource-limit", "No collision-free rebuild Table name is available");
4411
4549
  }
4550
+ removeFieldFromViewLayouts(tableId, fieldId) {
4551
+ const views = this.connection.query(`SELECT id, layout_json FROM ${EIDOS_FILE_VIEWS_TABLE} WHERE table_id = ?`, [tableId]);
4552
+ for (const view of views) {
4553
+ const layout = jsonObject(view.layout_json);
4554
+ for (const key of [
4555
+ "cardFields",
4556
+ "fieldOrder",
4557
+ "hiddenFields"
4558
+ ]) if (Array.isArray(layout[key])) layout[key] = layout[key].filter((value) => value !== fieldId);
4559
+ for (const key of ["coverField", "groupField"]) if (layout[key] === fieldId) layout[key] = null;
4560
+ if (layout.fieldWidths && typeof layout.fieldWidths === "object" && !Array.isArray(layout.fieldWidths)) {
4561
+ const fieldWidths = { ...layout.fieldWidths };
4562
+ delete fieldWidths[fieldId];
4563
+ layout.fieldWidths = fieldWidths;
4564
+ }
4565
+ const next = canonicalizeEidosFileJson(layout);
4566
+ if (next === view.layout_json) continue;
4567
+ this.connection.run(`UPDATE ${EIDOS_FILE_VIEWS_TABLE}
4568
+ SET layout_json = ?, updated_at = ? WHERE id = ?`, [
4569
+ next,
4570
+ this.operationInstant(),
4571
+ view.id
4572
+ ]);
4573
+ }
4574
+ }
4412
4575
  deleteField(tableId, fieldKey, replacementRecordLabelFieldId) {
4413
4576
  const table = this.getTable(tableId);
4414
4577
  const field = this.fieldByKey(tableId, fieldKey);
@@ -4417,7 +4580,7 @@ var EidosFileRuntime = class {
4417
4580
  if (field.isRecordLabel && !replacementRecordLabelFieldId) throw new EidosFileError("constraint-conflict", "Deleting the Record Label Field requires a replacement Field ID");
4418
4581
  const replacement = replacementRecordLabelFieldId ? this.fieldByKey(tableId, replacementRecordLabelFieldId) : void 0;
4419
4582
  if (replacement?.id === field.id) throw new EidosFileError("constraint-conflict", "The replacement Record Label Field must be a different Field");
4420
- return this.mutate(() => {
4583
+ return this.mutateSchemaState(() => {
4421
4584
  if (field.isRecordLabel && replacement?.id) this.connection.run(`UPDATE ${EIDOS_FILE_TABLES_TABLE}
4422
4585
  SET label_field_id = ?, updated_at = ? WHERE id = ?`, [
4423
4586
  replacement.id,
@@ -4430,6 +4593,7 @@ var EidosFileRuntime = class {
4430
4593
  WHERE inverse_of_field_id = ? LIMIT 1`, [field.id]);
4431
4594
  if (dependentLookup || dependentInverse) throw new EidosFileError("constraint-conflict", `Field ${field.name} has structural dependents`);
4432
4595
  for (const formula of this.listFields(tableId).filter((candidate) => candidate.type === "formula")) if (compileEidosFileFormula(formula, this.listFields(tableId)).dependencyFieldIds.includes(field.id)) throw new EidosFileError("formula-in-use", `Formula ${formula.name} depends on ${field.name}`);
4596
+ this.removeFieldFromViewLayouts(tableId, field.id);
4433
4597
  if (field.type === "relation") this.dropRelationTriggers(field.id);
4434
4598
  if (field.physicalName) this.connection.exec(`ALTER TABLE ${quoteIdentifier(table.physicalName ?? table.rawTableName)} DROP COLUMN ${quoteIdentifier(field.physicalName)}`);
4435
4599
  return this.connection.run(`DELETE FROM ${EIDOS_FILE_FIELDS_TABLE} WHERE id = ?`, [field.id]).changes > 0;
@@ -4472,6 +4636,9 @@ var EidosFileRuntime = class {
4472
4636
  }
4473
4637
  createView(tableId, input) {
4474
4638
  const fields = this.listFields(tableId);
4639
+ const name = assertEidosFileDisplayName(input.name, "View name");
4640
+ const foldedName = name.replace(/[A-Z]/g, (character) => character.toLowerCase());
4641
+ if (this.listViews(tableId).some((view) => view.name.replace(/[A-Z]/g, (character) => character.toLowerCase()) === foldedName)) throw new EidosFileError("constraint-conflict", `Duplicate View name: ${name}`);
4475
4642
  const id = input.id ?? this.allocateId();
4476
4643
  const now = this.operationInstant();
4477
4644
  const fieldByKey = /* @__PURE__ */ new Map();
@@ -4496,7 +4663,7 @@ var EidosFileRuntime = class {
4496
4663
  const id = fieldByKey.get(key);
4497
4664
  return id ? [id] : [];
4498
4665
  });
4499
- const fieldOrder = (input.orderMap ? Object.entries(input.orderMap).sort((left, right) => left[1] - right[1]).map(([key]) => key) : Array.isArray(properties.fieldOrder) ? properties.fieldOrder.flatMap((key) => typeof key === "string" ? [key] : []) : fields.flatMap((field) => field.id ? [field.id] : [])).flatMap((key) => {
4666
+ const fieldOrder = (input.orderMap ? Object.entries(input.orderMap).sort((left, right) => left[1] - right[1]).map(([key]) => key) : Array.isArray(properties.fieldOrder) ? properties.fieldOrder.flatMap((key) => typeof key === "string" ? [key] : []) : systemFieldsLast(fields).flatMap((field) => field.id ? [field.id] : [])).flatMap((key) => {
4500
4667
  const id = fieldByKey.get(key);
4501
4668
  return id ? [id] : [];
4502
4669
  });
@@ -4511,7 +4678,7 @@ var EidosFileRuntime = class {
4511
4678
  VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, [
4512
4679
  assertEidosFileUuid(id, "View ID"),
4513
4680
  assertEidosFileUuid(tableId, "Table ID"),
4514
- input.name,
4681
+ name,
4515
4682
  input.type,
4516
4683
  canonicalizeEidosFileJson({
4517
4684
  ...filterToStorage(input.filter, fields) ? { filter: filterToStorage(input.filter, fields) } : {},
@@ -4536,8 +4703,11 @@ var EidosFileRuntime = class {
4536
4703
  updateView(viewId, changes) {
4537
4704
  const current = this.mapView(this.viewRow(viewId));
4538
4705
  const fields = this.listFields(current.tableId);
4706
+ const name = assertEidosFileDisplayName(changes.name ?? current.name, "View name");
4707
+ const foldedName = name.replace(/[A-Z]/g, (character) => character.toLowerCase());
4708
+ if (this.listViews(current.tableId).some((view) => view.id !== viewId && view.name.replace(/[A-Z]/g, (character) => character.toLowerCase()) === foldedName)) throw new EidosFileError("constraint-conflict", `Duplicate View name: ${name}`);
4539
4709
  const next = {
4540
- name: changes.name ?? current.name,
4710
+ name,
4541
4711
  type: changes.type ?? current.type,
4542
4712
  position: changes.position ?? current.position,
4543
4713
  properties: changes.properties === void 0 ? current.properties : changes.properties,
@@ -4562,7 +4732,7 @@ var EidosFileRuntime = class {
4562
4732
  const fieldOrder = next.orderMap ? Object.entries(next.orderMap).sort((left, right) => left[1] - right[1]).flatMap(([key]) => {
4563
4733
  const id = fieldByKey.get(key);
4564
4734
  return id ? [id] : [];
4565
- }) : fields.flatMap((field) => field.id ? [field.id] : []);
4735
+ }) : systemFieldsLast(fields).flatMap((field) => field.id ? [field.id] : []);
4566
4736
  const layout = {
4567
4737
  ...next.properties ?? {},
4568
4738
  fieldOrder,
@@ -4675,7 +4845,7 @@ var EidosFileRuntime = class {
4675
4845
  return `eidos_lookup_aggregate(
4676
4846
  ${`(SELECT coalesce(
4677
4847
  json_group_array(
4678
- json_object('v', ${targetResultType === "integer" ? "CASE WHEN value IS NULL THEN NULL ELSE CAST(value AS TEXT) END" : targetResultType === "json" || targetResultType === "file-entry" ? "CASE WHEN value IS NULL THEN NULL ELSE json(value) END" : "value"})
4848
+ json_object('v', ${targetResultType === "integer" ? "CASE WHEN value IS NULL THEN NULL ELSE CAST(value AS TEXT) END" : targetResultType === "file-entry" ? "CASE WHEN value IS NULL THEN NULL ELSE json(value) END" : "value"})
4679
4849
  ORDER BY relation_order, nested_order
4680
4850
  ),
4681
4851
  '[]'
@@ -4874,6 +5044,24 @@ var EidosFileRuntime = class {
4874
5044
  ...nextCursor ? { nextCursor } : {}
4875
5045
  };
4876
5046
  }
5047
+ /** Returns the zero-based position of a Row ID in a filtered/sorted query. */
5048
+ getRowIndex(tableId, rowId, query = {}) {
5049
+ const id = assertEidosFileUuid(rowId, "Row ID");
5050
+ const compatibleQuery = this.compatibilityQuery(tableId, query);
5051
+ const fields = this.listFields(tableId);
5052
+ const source = this.logicalSource(tableId, this.requiredQueryFieldKeys(fields, compatibleQuery), false, false);
5053
+ const compiled = compileEidosFileRowQuery(source.fields, compatibleQuery);
5054
+ const sorts = uniqueSortFields(source.fields, compatibleQuery.sorts);
5055
+ const targetWhere = compiled.whereSql ? `${compiled.whereSql} AND "__base_rowid" = ?` : "WHERE \"__base_rowid\" = ?";
5056
+ const target = this.connection.get(`WITH logical AS (${source.sql})
5057
+ SELECT * FROM logical ${targetWhere} LIMIT 1`, [...compiled.params, id]);
5058
+ if (!target) return null;
5059
+ const before = compileKeysetBefore(sorts, sorts.map(({ field }) => cursorSortValue(target, field)), id);
5060
+ const beforeWhere = compiled.whereSql ? `${compiled.whereSql} AND ${before.sql}` : `WHERE ${before.sql}`;
5061
+ const result = this.connection.get(`WITH logical AS (${source.sql})
5062
+ SELECT COUNT(*) AS count FROM logical ${beforeWhere}`, [...compiled.params, ...before.params]);
5063
+ return Number(result?.count ?? 0);
5064
+ }
4877
5065
  getRow(tableId, rowId) {
4878
5066
  const source = this.logicalSource(tableId);
4879
5067
  const row = this.connection.get(`WITH logical AS (${source.sql}) SELECT * FROM logical WHERE "__base_rowid" = ?`, [assertEidosFileUuid(rowId, "Row ID")]);
@@ -4892,35 +5080,62 @@ var EidosFileRuntime = class {
4892
5080
  includeRelationDisplays: options.resolveRelations === true
4893
5081
  });
4894
5082
  return {
4895
- rows: page.rows.map((row) => {
4896
- const logicalFields = Object.fromEntries(requested.map((field) => {
4897
- const raw = row[field.tableColumnName];
4898
- let value = raw ?? null;
4899
- const resultType = field.type === "formula" || field.type === "lookup" ? String(field.property?.displayType ?? field.type) : field.type;
4900
- if (typeof raw === "string" && (field.type === "file" || field.type === "multi-select" || field.type === "relation" || field.type === "lookup" && field.property?.aggregate === "values" || field.type === "json")) value = parseEidosFileJson(raw);
4901
- else if (resultType === "checkbox" && typeof raw === "number") value = raw === 1;
4902
- return [field.id, value];
4903
- }));
4904
- const resolved = options.resolveRelations ? Object.fromEntries(requested.flatMap((field) => {
4905
- if (field.type !== "relation") return [];
4906
- const display = row[`${field.tableColumnName}__display`];
4907
- if (typeof display !== "string") return [[field.id, []]];
4908
- const parsed = parseEidosFileJson(display);
4909
- if (!Array.isArray(parsed)) return [[field.id, []]];
4910
- return [[field.id, parsed.flatMap((entry) => entry && !Array.isArray(entry) && typeof entry === "object" && typeof entry.id === "string" ? [{
4911
- id: entry.id,
4912
- label: entry.title ?? null
4913
- }] : [])]];
4914
- })) : void 0;
4915
- return {
4916
- id: String(row[rowIdField.tableColumnName]),
4917
- fields: logicalFields,
4918
- ...resolved && Object.keys(resolved).length > 0 ? { resolved } : {}
4919
- };
4920
- }),
5083
+ rows: this.logicalRows(rowIdField, requested, page.rows, options.resolveRelations === true),
4921
5084
  ...page.nextCursor ? { nextCursor: page.nextCursor } : {}
4922
5085
  };
4923
5086
  }
5087
+ /** @internal Exact primary-key lookup without page totals or cursor work. */
5088
+ getLogicalRowsByIds(tableId, rowIds, options = {}) {
5089
+ if (rowIds.length === 0) return [];
5090
+ if (rowIds.length > 500) throw new EidosFileError("resource-limit", "getLogicalRowsByIds accepts at most 500 Row IDs");
5091
+ const ids = rowIds.map((rowId) => assertEidosFileUuid(rowId, "Row ID"));
5092
+ const fields = this.listFields(tableId);
5093
+ const requested = options.fields ? options.fields.map((id) => this.fieldByKey(tableId, id)) : fields;
5094
+ const rowIdField = fields.find((field) => field.type === "row-id");
5095
+ const source = this.logicalSource(tableId, requested.map((field) => field.id), false, options.resolveRelations === true);
5096
+ const rawRows = this.connection.query(`WITH logical AS (${source.sql})
5097
+ SELECT * FROM logical
5098
+ WHERE "__base_rowid" IN (${ids.map(() => "?").join(", ")})`, ids);
5099
+ const logicalRows = this.logicalRows(rowIdField, requested, rawRows.map((row) => Object.fromEntries(Object.entries(row).flatMap(([key, value]) => key === "__base_rowid" ? [] : [[key, rowValue(value)]]))), options.resolveRelations === true);
5100
+ const byId = new Map(logicalRows.map((row) => [row.id, row]));
5101
+ return ids.flatMap((rowId) => {
5102
+ const row = byId.get(rowId);
5103
+ return row ? [row] : [];
5104
+ });
5105
+ }
5106
+ logicalRows(rowIdField, requested, rows, resolveRelations) {
5107
+ return rows.map((row) => {
5108
+ const logicalFields = Object.fromEntries(requested.map((field) => {
5109
+ const raw = row[field.tableColumnName];
5110
+ let value = raw ?? null;
5111
+ const resultType = field.type === "formula" || field.type === "lookup" ? String(field.property?.displayType ?? field.type) : field.type;
5112
+ if (typeof raw === "string" && (field.type === "file" || field.type === "multi-select" || field.type === "relation" || field.type === "lookup" && field.property?.aggregate === "values")) if (field.type === "file") try {
5113
+ value = decodeEidosFileValues(raw);
5114
+ } catch {
5115
+ throw new EidosFileError("invalid-sqlite", `${field.name} contains an invalid stored File value`);
5116
+ }
5117
+ else value = parseEidosFileJson(raw);
5118
+ else if (resultType === "checkbox" && typeof raw === "number") value = raw === 1;
5119
+ return [field.id, value];
5120
+ }));
5121
+ const resolved = resolveRelations ? Object.fromEntries(requested.flatMap((field) => {
5122
+ if (field.type !== "relation") return [];
5123
+ const display = row[`${field.tableColumnName}__display`];
5124
+ if (typeof display !== "string") return [[field.id, []]];
5125
+ const parsed = parseEidosFileJson(display);
5126
+ if (!Array.isArray(parsed)) return [[field.id, []]];
5127
+ return [[field.id, parsed.flatMap((entry) => entry && !Array.isArray(entry) && typeof entry === "object" && typeof entry.id === "string" ? [{
5128
+ id: entry.id,
5129
+ label: entry.title ?? null
5130
+ }] : [])]];
5131
+ })) : void 0;
5132
+ return {
5133
+ id: String(row[rowIdField.tableColumnName]),
5134
+ fields: logicalFields,
5135
+ ...resolved && Object.keys(resolved).length > 0 ? { resolved } : {}
5136
+ };
5137
+ });
5138
+ }
4924
5139
  /**
4925
5140
  * @internal Exact Runtime aggregate/group scan. One set-based SQL statement
4926
5141
  * returns typed logical inputs in effective query order; the public Runtime
@@ -5044,6 +5259,8 @@ var EidosFileRuntime = class {
5044
5259
  "count-all": "count(*)",
5045
5260
  "count-non-null": list ? "count(*)" : `count(${column})`,
5046
5261
  "count-empty": `sum(CASE WHEN ${column} IS NULL${list ? ` OR json_array_length(${column}) = 0` : ""} THEN 1 ELSE 0 END)`,
5262
+ "percent-checked": `CASE WHEN count(*) = 0 THEN 0 ELSE round(100.0 * sum(CASE WHEN ${column} = 1 THEN 1 ELSE 0 END) / count(*), 2) END`,
5263
+ "percent-unchecked": `CASE WHEN count(*) = 0 THEN 0 ELSE round(100.0 * sum(CASE WHEN ${column} = 1 THEN 0 ELSE 1 END) / count(*), 2) END`,
5047
5264
  "count-distinct": list ? `(SELECT count(DISTINCT value_type || ':' || json_quote(value)) FROM ${flattened})` : `count(DISTINCT ${column})`,
5048
5265
  sum: list ? `(SELECT sum(value) FROM ${flattened} WHERE value_type IN ('integer','real'))` : `sum(${column})`,
5049
5266
  average: list ? `(SELECT avg(value) FROM ${flattened} WHERE value_type IN ('integer','real'))` : `avg(${column})`,
@@ -5101,13 +5318,12 @@ var EidosFileRuntime = class {
5101
5318
  if (typeof value !== "string") throw new EidosFileError("invalid-value", `${field.name} must be text or NULL`);
5102
5319
  return value;
5103
5320
  }
5104
- if (field.type === "json") return canonicalizeEidosFileJson(parseEidosFileJson(typeof value === "string" ? value : canonicalizeEidosFileJson(value)));
5105
5321
  if (field.type === "multi-select") {
5106
5322
  const parsed = typeof value === "string" ? parseEidosFileJson(value) : value;
5107
5323
  if (!Array.isArray(parsed) || !parsed.every((entry) => typeof entry === "string") || new Set(parsed).size !== parsed.length) throw new EidosFileError("invalid-value", `${field.name} must be a unique string array`);
5108
5324
  return canonicalizeEidosFileJson(parsed);
5109
5325
  }
5110
- if (field.type === "file") return canonicalizeEidosFileJson(assertEidosFileValues(typeof value === "string" ? parseEidosFileJson(value) : value));
5326
+ if (field.type === "file") return typeof value === "string" ? canonicalizeEidosFileJson(decodeEidosFileValues(value)) : canonicalizeEidosFileJson(assertEidosFileValues(value));
5111
5327
  if (field.type === "relation") {
5112
5328
  const ids = Array.isArray(value) ? value.map(String) : decodeEidosFileRelationIds(typeof value === "string" ? value : void 0);
5113
5329
  const encoded = encodeEidosFileRelationIds(ids);
@@ -5162,6 +5378,62 @@ var EidosFileRuntime = class {
5162
5378
  VALUES (${values.map(() => "?").join(", ")})`, values);
5163
5379
  return rowId;
5164
5380
  }
5381
+ insertImportedRowsInTransaction(tableId, rows) {
5382
+ if (rows.length === 0) return [];
5383
+ const table = this.getTable(tableId);
5384
+ const fields = this.listFields(tableId);
5385
+ const fieldByKey = /* @__PURE__ */ new Map();
5386
+ for (const field of fields) for (const key of [
5387
+ field.id,
5388
+ field.tableColumnName,
5389
+ field.physicalName,
5390
+ field.name
5391
+ ]) if (key && !fieldByKey.has(key)) fieldByKey.set(key, field);
5392
+ const tableName = table.physicalName ?? table.rawTableName;
5393
+ const plans = /* @__PURE__ */ new Map();
5394
+ const now = this.operationInstant();
5395
+ const ids = rows.map((row) => {
5396
+ const requestedId = row._id ?? row.id;
5397
+ const rowId = typeof requestedId === "string" ? assertEidosFileUuid(requestedId, "Row ID") : this.allocateId();
5398
+ const keys = Object.keys(row).filter((key) => key !== "_id" && key !== "id" && !key.endsWith("__display"));
5399
+ const signature = JSON.stringify(keys);
5400
+ let plan = plans.get(signature);
5401
+ if (!plan) {
5402
+ const plannedFields = keys.map((key) => {
5403
+ const field = fieldByKey.get(key);
5404
+ if (!field) throw new EidosFileError("field-not-found", `Eidos File Field not found: ${key}`);
5405
+ if (!field.physicalName || field.valueKind === "system") throw new EidosFileError("protected-field", `Field ${field.name} is read-only`);
5406
+ return field;
5407
+ });
5408
+ const columns = [
5409
+ "_id",
5410
+ "_created_at",
5411
+ "_updated_at",
5412
+ ...plannedFields.map((field) => field.physicalName)
5413
+ ];
5414
+ plan = {
5415
+ keys,
5416
+ fields: plannedFields,
5417
+ sql: `INSERT INTO ${quoteIdentifier(tableName)}
5418
+ (${columns.map(quoteIdentifier).join(", ")})
5419
+ VALUES (${columns.map(() => "?").join(", ")})`,
5420
+ parameterSets: []
5421
+ };
5422
+ plans.set(signature, plan);
5423
+ }
5424
+ const values = [
5425
+ rowId,
5426
+ now,
5427
+ now,
5428
+ ...plan.keys.map((key, index) => this.normalizeStoredValue(plan.fields[index], row[key], true))
5429
+ ];
5430
+ plan.parameterSets.push(values);
5431
+ return rowId;
5432
+ });
5433
+ for (const plan of plans.values()) if (this.connection.runMany) this.connection.runMany(plan.sql, plan.parameterSets);
5434
+ else for (const values of plan.parameterSets) this.connection.run(plan.sql, values);
5435
+ return ids;
5436
+ }
5165
5437
  insertRow(tableId, row) {
5166
5438
  const id = this.mutate(() => this.insertRowInTransaction(tableId, row, true));
5167
5439
  return this.getRow(tableId, id);
@@ -5171,13 +5443,17 @@ var EidosFileRuntime = class {
5171
5443
  return this.getRow(tableId, id);
5172
5444
  }
5173
5445
  insertImportedRows(tableId, rows) {
5174
- return this.mutate(() => rows.map((row) => this.insertRowInTransaction(tableId, row, true))).map((id) => this.getRow(tableId, id));
5446
+ return this.mutate(() => this.insertImportedRowsInTransaction(tableId, rows)).map((id) => this.getRow(tableId, id));
5447
+ }
5448
+ /** @internal Appends rows inside a caller-owned import transaction. */
5449
+ appendImportedRows(tableId, rows) {
5450
+ this.insertImportedRowsInTransaction(tableId, rows);
5175
5451
  }
5176
5452
  /** @internal Used by the transactional CSV interchange layer. */
5177
5453
  importTable(table, rows) {
5178
5454
  return this.mutate(() => {
5179
5455
  const created = this.createTable(table);
5180
- rows.forEach((row) => this.insertRowInTransaction(created.id, row, true));
5456
+ this.insertImportedRowsInTransaction(created.id, rows);
5181
5457
  return created;
5182
5458
  });
5183
5459
  }
@@ -5205,35 +5481,248 @@ var EidosFileRuntime = class {
5205
5481
  return updates.map((update) => update.rowId);
5206
5482
  }).map((id) => this.getRow(tableId, id));
5207
5483
  }
5484
+ rowMutationUndoEntryBytes(entry) {
5485
+ const json = JSON.stringify(entry, (_key, value) => {
5486
+ if (typeof value === "bigint") return { $int64: value.toString() };
5487
+ if (value instanceof Uint8Array) return { $blob: Array.from(value) };
5488
+ return value;
5489
+ });
5490
+ return new TextEncoder().encode(json).byteLength;
5491
+ }
5492
+ prepareRowMutationUndo(entry) {
5493
+ const bytes = this.rowMutationUndoEntryBytes(entry);
5494
+ if (bytes > ROW_MUTATION_UNDO_BYTES_MAX) throw new EidosFileError("resource-limit", "This deletion is too large to retain for undo");
5495
+ const base = `row-undo:${this.allocateId()}`;
5496
+ let token = base;
5497
+ let suffix = 1;
5498
+ while (this.rowMutationUndoEntries.has(token)) {
5499
+ token = `${base}:${suffix}`;
5500
+ suffix += 1;
5501
+ }
5502
+ return {
5503
+ token,
5504
+ retained: {
5505
+ entry,
5506
+ bytes
5507
+ }
5508
+ };
5509
+ }
5510
+ deleteRowMutationUndoEntry(token) {
5511
+ const retained = this.rowMutationUndoEntries.get(token);
5512
+ if (!retained) return false;
5513
+ this.rowMutationUndoEntries.delete(token);
5514
+ this.rowMutationUndoBytes = Math.max(0, this.rowMutationUndoBytes - retained.bytes);
5515
+ return true;
5516
+ }
5517
+ clearRowMutationUndoEntries() {
5518
+ this.rowMutationUndoEntries.clear();
5519
+ this.rowMutationUndoBytes = 0;
5520
+ }
5521
+ commitRowMutationUndo(prepared, consumedToken) {
5522
+ if (consumedToken) this.deleteRowMutationUndoEntry(consumedToken);
5523
+ this.rowMutationUndoEntries.set(prepared.token, prepared.retained);
5524
+ this.rowMutationUndoBytes += prepared.retained.bytes;
5525
+ while (this.rowMutationUndoEntries.size > ROW_MUTATION_UNDO_ENTRIES_MAX || this.rowMutationUndoBytes > ROW_MUTATION_UNDO_BYTES_MAX) {
5526
+ const redoToken = Array.from(this.rowMutationUndoEntries).find(([token, retained]) => token !== prepared.token && retained.entry.kind === "repeat-delete")?.[0];
5527
+ const oldestToken = this.rowMutationUndoEntries.keys().next().value;
5528
+ const token = redoToken ?? oldestToken;
5529
+ if (typeof token !== "string" || token === prepared.token) break;
5530
+ this.deleteRowMutationUndoEntry(token);
5531
+ }
5532
+ return prepared.token;
5533
+ }
5534
+ captureRowDeletionUndoState(tableId, rowIds) {
5535
+ const ids = Array.from(new Set(rowIds.map((id) => assertEidosFileUuid(id, "Row ID"))));
5536
+ const table = this.getTable(tableId);
5537
+ const physicalTable = table.physicalName ?? table.rawTableName;
5538
+ const rawRows = ids.length === 0 ? [] : this.connection.query(`SELECT * FROM ${quoteIdentifier(physicalTable)} WHERE "_id" IN (${ids.map(() => "?").join(", ")})`, ids);
5539
+ const rawById = new Map(rawRows.flatMap((row) => typeof row._id === "string" ? [[row._id, row]] : []));
5540
+ for (const rowId of ids) if (!rawById.has(rowId)) throw new EidosFileError("row-not-found", `Row not found: ${rowId}`);
5541
+ const deleteSet = new Set(ids);
5542
+ const detachedRelations = [];
5543
+ const schema = this.allSchema();
5544
+ for (const [fieldId, relation] of schema.relations) {
5545
+ if (relation.direction !== "forward" || relation.on_delete !== "detach" || uuid(relation.target_table_id) !== tableId) continue;
5546
+ const field = Array.from(schema.fieldsByTable.values()).flat().find((candidate) => candidate.id === fieldId);
5547
+ if (!field?.tableId || !field.physicalName) continue;
5548
+ const sourceTable = schema.tables.get(field.tableId);
5549
+ if (!sourceTable) continue;
5550
+ const sourceRows = this.connection.query(`SELECT "_id", ${quoteIdentifier(field.physicalName)} AS relation_value FROM ${quoteIdentifier(sourceTable.physical_name)} ORDER BY "_id" COLLATE BINARY`);
5551
+ for (const sourceRow of sourceRows) {
5552
+ if (field.tableId === tableId && deleteSet.has(sourceRow._id)) continue;
5553
+ if (typeof sourceRow.relation_value !== "string") throw new EidosFileError("invalid-value", `Relation ${fieldId} has a non-text stored value`);
5554
+ const parsed = parseEidosFileJson(sourceRow.relation_value);
5555
+ if (!Array.isArray(parsed)) throw new EidosFileError("invalid-value", `Relation ${fieldId} has a non-array stored value`);
5556
+ if (!parsed.some((entry) => typeof entry === "string" && deleteSet.has(entry))) continue;
5557
+ const survivors = parsed.filter((entry) => typeof entry !== "string" || !deleteSet.has(entry));
5558
+ detachedRelations.push({
5559
+ tableId: field.tableId,
5560
+ physicalTable: sourceTable.physical_name,
5561
+ physicalColumn: field.physicalName,
5562
+ rowId: sourceRow._id,
5563
+ value: sourceRow.relation_value,
5564
+ detachedValue: canonicalizeEidosFileJson(survivors)
5565
+ });
5566
+ }
5567
+ }
5568
+ return {
5569
+ tableId,
5570
+ rowIds: ids,
5571
+ rows: ids.map((rowId) => ({
5572
+ tableId,
5573
+ physicalTable,
5574
+ row: rawById.get(rowId)
5575
+ })),
5576
+ detachedRelations
5577
+ };
5578
+ }
5579
+ sameUndoPhysicalRow(left, right) {
5580
+ const leftKeys = Object.keys(left).filter((key) => key !== "_updated_at").sort();
5581
+ const rightKeys = Object.keys(right).filter((key) => key !== "_updated_at").sort();
5582
+ return leftKeys.length === rightKeys.length && leftKeys.every((key, index) => key === rightKeys[index] && sameSqlValue(left[key], right[key]));
5583
+ }
5584
+ assertRepeatRowDeletionApplicable(state) {
5585
+ const current = this.captureRowDeletionUndoState(state.tableId, state.rowIds);
5586
+ if (current.rows.length !== state.rows.length || current.rows.some((entry, index) => {
5587
+ const expected = state.rows[index];
5588
+ return !expected || entry.physicalTable !== expected.physicalTable || !this.sameUndoPhysicalRow(entry.row, expected.row);
5589
+ })) throw new EidosFileError("file-conflict", "The restored records changed and cannot be redone");
5590
+ const relationKey = (entry) => `${entry.tableId}\u0000${entry.rowId}\u0000${entry.physicalColumn}`;
5591
+ const expectedRelations = new Map(state.detachedRelations.map((entry) => [relationKey(entry), entry]));
5592
+ if (current.detachedRelations.length !== expectedRelations.size || current.detachedRelations.some((entry) => {
5593
+ const expected = expectedRelations.get(relationKey(entry));
5594
+ return !expected || !sameSqlValue(entry.value, expected.value);
5595
+ })) throw new EidosFileError("file-conflict", "Related records changed and this deletion cannot be redone");
5596
+ }
5597
+ restoreRowDeletionUndoState(state) {
5598
+ return this.mutate(() => {
5599
+ const table = this.getTable(state.tableId);
5600
+ const physicalTable = table.physicalName ?? table.rawTableName;
5601
+ if (state.rows.some((entry) => entry.physicalTable !== physicalTable)) throw new EidosFileError("stale-revision", "The table changed after these rows were deleted");
5602
+ if (state.rowIds.length > 0) {
5603
+ const existing = this.connection.get(`SELECT count(*) AS count FROM ${quoteIdentifier(physicalTable)} WHERE "_id" IN (${state.rowIds.map(() => "?").join(", ")})`, state.rowIds)?.count;
5604
+ if (Number(existing ?? 0) !== 0) throw new EidosFileError("file-conflict", "A deleted Row ID is already in use");
5605
+ }
5606
+ for (const relation of state.detachedRelations) {
5607
+ const current = this.connection.get(`SELECT ${quoteIdentifier(relation.physicalColumn)} AS value FROM ${quoteIdentifier(relation.physicalTable)} WHERE "_id" = ?`, [relation.rowId]);
5608
+ if (!current || !sameSqlValue(current.value, relation.detachedValue)) throw new EidosFileError("file-conflict", `Related Row ${relation.rowId} changed after the deletion`);
5609
+ }
5610
+ const schema = this.allSchema();
5611
+ const outgoingRelationColumns = new Set((schema.fieldsByTable.get(state.tableId) ?? []).flatMap((field) => {
5612
+ return (field.id ? schema.relations.get(field.id) : void 0)?.direction === "forward" && field.physicalName ? [field.physicalName] : [];
5613
+ }));
5614
+ for (const entry of state.rows) {
5615
+ const keys = Object.keys(entry.row);
5616
+ const values = keys.map((key) => outgoingRelationColumns.has(key) ? "[]" : entry.row[key]);
5617
+ this.connection.run(`INSERT INTO ${quoteIdentifier(physicalTable)} (${keys.map(quoteIdentifier).join(", ")}) VALUES (${keys.map(() => "?").join(", ")})`, values);
5618
+ }
5619
+ for (const entry of state.rows) {
5620
+ const rowId = entry.row._id;
5621
+ if (typeof rowId !== "string") throw new EidosFileError("invalid-schema", "Deleted row state is missing its Row ID");
5622
+ const keys = Object.keys(entry.row).filter((key) => key !== "_id");
5623
+ if (keys.length > 0) {
5624
+ if (this.connection.run(`UPDATE ${quoteIdentifier(physicalTable)} SET ${keys.map((key) => `${quoteIdentifier(key)} = ?`).join(", ")} WHERE "_id" = ?`, [...keys.map((key) => key === "_updated_at" ? this.operationInstant() : entry.row[key]), rowId]).changes === 0) throw new EidosFileError("stale-revision", `Could not restore Row ${rowId}`);
5625
+ }
5626
+ }
5627
+ for (const relation of state.detachedRelations) if (this.connection.run(`UPDATE ${quoteIdentifier(relation.physicalTable)} SET ${quoteIdentifier(relation.physicalColumn)} = ?, "_updated_at" = ? WHERE "_id" = ?`, [
5628
+ relation.value,
5629
+ this.operationInstant(),
5630
+ relation.rowId
5631
+ ]).changes === 0) throw new EidosFileError("stale-revision", `Could not restore Relation on Row ${relation.rowId}`);
5632
+ return [...state.rows.map((entry) => ({
5633
+ tableId: entry.tableId,
5634
+ rowId: String(entry.row._id)
5635
+ })), ...state.detachedRelations.map((entry) => ({
5636
+ tableId: entry.tableId,
5637
+ rowId: entry.rowId
5638
+ }))];
5639
+ });
5640
+ }
5641
+ deleteRowsReversible(tableId, rowIds, expectedRevision, consumedUndoToken) {
5642
+ if (rowIds.length === 0) return {
5643
+ revision: this.info().revision ?? 0,
5644
+ deleted: [],
5645
+ affected: [],
5646
+ rowCount: this.countRows(tableId)
5647
+ };
5648
+ const rowCountBefore = this.countRows(tableId);
5649
+ const state = this.captureRowDeletionUndoState(tableId, rowIds);
5650
+ const preparedUndo = this.prepareRowMutationUndo({
5651
+ kind: "restore-delete",
5652
+ state
5653
+ });
5654
+ const result = this.mutateRows({
5655
+ tableId,
5656
+ delete: state.rowIds,
5657
+ expectedRevision
5658
+ });
5659
+ const undoToken = this.commitRowMutationUndo(preparedUndo, consumedUndoToken);
5660
+ return {
5661
+ revision: result.revision,
5662
+ deleted: result.deleted,
5663
+ affected: result.affected ?? [],
5664
+ rowCount: rowCountBefore - result.deleted.length,
5665
+ undoToken
5666
+ };
5667
+ }
5668
+ revertRowMutation(undoToken) {
5669
+ const retained = this.rowMutationUndoEntries.get(undoToken);
5670
+ if (!retained) throw new EidosFileError("stale-revision", "This row change can no longer be undone");
5671
+ const entry = retained.entry;
5672
+ if (entry.kind === "restore-delete") {
5673
+ const rowCount = this.countRows(entry.state.tableId) + entry.state.rowIds.length;
5674
+ const preparedUndo = this.prepareRowMutationUndo({
5675
+ kind: "repeat-delete",
5676
+ state: entry.state
5677
+ });
5678
+ const affected = this.restoreRowDeletionUndoState(entry.state);
5679
+ return {
5680
+ revision: this.info().revision ?? 0,
5681
+ affected,
5682
+ rowCount,
5683
+ undoToken: this.commitRowMutationUndo(preparedUndo, undoToken)
5684
+ };
5685
+ }
5686
+ this.assertRepeatRowDeletionApplicable(entry.state);
5687
+ const result = this.deleteRowsReversible(entry.state.tableId, entry.state.rowIds, void 0, undoToken);
5688
+ return {
5689
+ revision: result.revision,
5690
+ affected: result.affected,
5691
+ rowCount: result.rowCount,
5692
+ undoToken: result.undoToken
5693
+ };
5694
+ }
5208
5695
  deleteRow(tableId, rowId) {
5209
5696
  return this.deleteRows(tableId, [rowId]).length === 1;
5210
5697
  }
5211
5698
  deleteRows(tableId, rowIds) {
5212
5699
  if (rowIds.length === 0) return [];
5213
- const table = this.getTable(tableId);
5214
5700
  const ids = Array.from(new Set(rowIds.map((id) => assertEidosFileUuid(id, "Row ID"))));
5215
5701
  if (ids.length > 500) throw new EidosFileError("resource-limit", "deleteRows accepts at most 500 Row IDs");
5216
- return this.mutate(() => {
5217
- const existing = this.connection.query(`SELECT "_id" AS id
5218
- FROM ${quoteIdentifier(table.physicalName ?? table.rawTableName)}
5219
- WHERE "_id" IN (${ids.map(() => "?").join(", ")})`, ids).map((row) => uuid(row.id));
5220
- if (existing.length > 0) this.connection.run(`DELETE FROM ${quoteIdentifier(table.physicalName ?? table.rawTableName)}
5221
- WHERE "_id" IN (${existing.map(() => "?").join(", ")})`, existing);
5222
- return existing;
5223
- });
5702
+ return this.mutateRows({
5703
+ tableId,
5704
+ delete: ids
5705
+ }).deleted;
5224
5706
  }
5225
- deleteRowRanges(tableId, ranges, query = {}) {
5707
+ rowIdsForRanges(tableId, ranges, query) {
5226
5708
  const rowIdField = this.listFields(tableId).find((field) => field.type === "row-id");
5227
- const ids = ranges.flatMap((range) => {
5709
+ return ranges.flatMap((range) => {
5228
5710
  const start = Math.max(0, Math.trunc(range.startIndex));
5229
5711
  const end = Math.max(start, Math.trunc(range.endIndex));
5230
- return this.getRowPage(tableId, start, end - start + 1, query).rows.flatMap((row) => {
5712
+ if (end === start) return [];
5713
+ return this.getRowPage(tableId, start, end - start, query).rows.flatMap((row) => {
5231
5714
  const id = row[rowIdField.tableColumnName];
5232
5715
  return typeof id === "string" ? [id] : [];
5233
5716
  });
5234
5717
  });
5718
+ }
5719
+ deleteRowRanges(tableId, ranges, query = {}) {
5720
+ const ids = this.rowIdsForRanges(tableId, ranges, query);
5235
5721
  return this.deleteRows(tableId, ids).length;
5236
5722
  }
5723
+ deleteRowRangesReversible(tableId, ranges, query = {}) {
5724
+ return this.deleteRowsReversible(tableId, this.rowIdsForRanges(tableId, ranges, query));
5725
+ }
5237
5726
  previewFormula(tableId, input) {
5238
5727
  const fields = this.listFields(tableId);
5239
5728
  const compiled = compileEidosFileFormula({
@@ -5428,20 +5917,7 @@ var EidosFileRuntime = class {
5428
5917
  };
5429
5918
  }, input.expectedRevision);
5430
5919
  const requestedIds = [...new Set([...result.inserted, ...result.updated])];
5431
- const rowIdField = this.listFields(input.tableId).find((field) => field.type === "row-id");
5432
- const queried = requestedIds.length === 0 ? [] : this.queryRows(input.tableId, {
5433
- query: { filter: {
5434
- type: "group",
5435
- conjunction: "and",
5436
- children: [{
5437
- type: "rule",
5438
- field: rowIdField.id,
5439
- operator: "is-any-of",
5440
- value: requestedIds
5441
- }]
5442
- } },
5443
- limit: requestedIds.length
5444
- }).rows;
5920
+ const queried = this.getLogicalRowsByIds(input.tableId, requestedIds);
5445
5921
  const rowsById = new Map(queried.map((row) => [row.id, row]));
5446
5922
  return {
5447
5923
  revision: this.info().revision ?? 0,
@@ -5471,6 +5947,6 @@ function fieldValueTypeForScan(field) {
5471
5947
  return field.type;
5472
5948
  }
5473
5949
  //#endregion
5474
- export { EIDOS_FILE_SYSTEM_COLUMNS as $, eidosFileLookupTargetDisplayType as A, EIDOS_FILE_APPLICATION_ID as B, normalizeEidosFileSorts as C, eidosFileLookupDisplayType as D, eidosFileLookupAggregateSupportsTarget as E, compileEidosFileFormula as F, EIDOS_FILE_FORMAT_VERSION as G, EIDOS_FILE_FEATURES_TABLE as H, compileEidosFileFormulaFields as I, EIDOS_FILE_META_TABLE as J, EIDOS_FILE_FORMULA_FIELDS_TABLE as K, compileEidosFileFormulaSource as L, eidosFileLookupValueType as M, EIDOS_FILE_FORMULA_FIELD_FUNCTION_NAMES as N, eidosFileLookupElementType as O, EIDOS_FILE_FORMULA_FUNCTION_NAMES as P, EIDOS_FILE_SCHEMA_VERSION as Q, eidosFileFormulaReferences as R, normalizeEidosFileRowQuery as S, eidosFileFieldStoresJsonArray as T, EIDOS_FILE_FIELDS_TABLE as U, EIDOS_FILE_EXTENSION as V, EIDOS_FILE_FORMAT as W, EIDOS_FILE_RELATION_FIELDS_TABLE as X, EIDOS_FILE_MIME_TYPE as Y, EIDOS_FILE_REQUIRED_TABLES as Z, compileEidosFileRowQuery as _, EIDOS_FILE_SCHEMA_SQL as a, eidosFileColumnStatTypesForField as at, eidosFileSortExpression as b, initializeEidosFileSchema as c, assertExactConnectionBindings as ct, decodeEidosFileRelationIds as d, exactConnectionBindingCount as dt, EIDOS_FILE_TABLES_TABLE as et, encodeEidosFileRelationIds as f, nativeToSqlValue as ft, assertEidosFileRowQuery as g, parseNonNegativeInt64 as h, EIDOS_FILE_FORMAT_VERSION_TEXT as i, eidosFileColumnStatLabel as it, eidosFileLookupTargetValueType as j, eidosFileLookupStorageCodec as k, setEidosFileMetadata as l, assertInt64Decimal as lt, cancellationPortFromSignal as m, validateEidosFile as n, SQLITE_HEADER as nt, configureEidosFileConnection as o, ConnectionPortEidosFileConnection as ot, MemoryByteSource as p, sqlValueToNative as pt, EIDOS_FILE_LOOKUP_FIELDS_TABLE as q, smallestDependencyCycle as r, hasEidosFileSqliteHeader as rt, incrementEidosFileRevision as s, EidosAdapterError as st, EidosFileRuntime as t, EIDOS_FILE_VIEWS_TABLE as tt, decodeEidosFileRelationDisplay as u, assertUnicodeString as ut, eidosFileRowQueryAffectedByFieldChanges as v, removeEidosFileFilterField as w, normalizeEidosFileFilter as x, eidosFileRowQueryPredicateColumns as y, rewriteEidosFileFormulaFieldReferences as z };
5950
+ export { EIDOS_FILE_SCHEMA_VERSION as $, eidosFileLookupStorageCodec as A, rewriteEidosFileFormulaFieldReferences as B, normalizeEidosFileRowQuery as C, eidosFileLookupAggregateSupportsTarget as D, eidosFileFieldStoresJsonArray as E, EIDOS_FILE_FORMULA_FUNCTION_NAMES as F, EIDOS_FILE_FORMAT as G, EIDOS_FILE_EXTENSION as H, compileEidosFileFormula as I, EIDOS_FILE_LOOKUP_FIELDS_TABLE as J, EIDOS_FILE_FORMAT_VERSION as K, compileEidosFileFormulaFields as L, eidosFileLookupTargetValueType as M, eidosFileLookupValueType as N, eidosFileLookupDisplayType as O, EIDOS_FILE_FORMULA_FIELD_FUNCTION_NAMES as P, EIDOS_FILE_REQUIRED_TABLES as Q, compileEidosFileFormulaSource as R, normalizeEidosFileFilter as S, removeEidosFileFilterField as T, EIDOS_FILE_FEATURES_TABLE as U, EIDOS_FILE_APPLICATION_ID as V, EIDOS_FILE_FIELDS_TABLE as W, EIDOS_FILE_MIME_TYPE as X, EIDOS_FILE_META_TABLE as Y, EIDOS_FILE_RELATION_FIELDS_TABLE as Z, assertEidosFileRowQuery as _, EIDOS_FILE_FORMAT_VERSION_TEXT as a, eidosFileColumnStatLabel as at, eidosFileRowQueryPredicateColumns as b, incrementEidosFileRevision as c, EidosAdapterError as ct, decodeEidosFileRelationDisplay as d, assertUnicodeString as dt, EIDOS_FILE_SYSTEM_COLUMNS as et, decodeEidosFileRelationIds as f, exactConnectionBindingCount as ft, parseNonNegativeInt64 as g, cancellationPortFromSignal as h, scanStableRowPages as i, hasEidosFileSqliteHeader as it, eidosFileLookupTargetDisplayType as j, eidosFileLookupElementType as k, initializeEidosFileSchema as l, assertExactConnectionBindings as lt, MemoryByteSource as m, sqlValueToNative as mt, validateEidosFile as n, EIDOS_FILE_VIEWS_TABLE as nt, EIDOS_FILE_SCHEMA_SQL as o, eidosFileColumnStatTypesForField as ot, encodeEidosFileRelationIds as p, nativeToSqlValue as pt, EIDOS_FILE_FORMULA_FIELDS_TABLE as q, smallestDependencyCycle as r, SQLITE_HEADER as rt, configureEidosFileConnection as s, ConnectionPortEidosFileConnection as st, EidosFileRuntime as t, EIDOS_FILE_TABLES_TABLE as tt, setEidosFileMetadata as u, assertInt64Decimal as ut, compileEidosFileRowQuery as v, normalizeEidosFileSorts as w, eidosFileSortExpression as x, eidosFileRowQueryAffectedByFieldChanges as y, eidosFileFormulaReferences as z };
5475
5951
 
5476
- //# sourceMappingURL=runtime-CMdfE7Gx.mjs.map
5952
+ //# sourceMappingURL=runtime-qbuZMbV4.mjs.map