@eidos.space/eidos-file 0.1.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DEVELOPER_PLATFORM.md +55 -53
- package/FIELD-CONVERSION.md +75 -0
- package/README.md +89 -106
- package/dist/adapter-contract-BRKKEulb.d.mts +1110 -0
- package/dist/adapter-contract-BRKKEulb.d.mts.map +1 -0
- package/dist/better-sqlite3.d.mts +64 -3
- package/dist/better-sqlite3.d.mts.map +1 -1
- package/dist/better-sqlite3.mjs +395 -14
- package/dist/better-sqlite3.mjs.map +1 -1
- package/dist/browser.d.mts +4 -3
- package/dist/browser.d.mts.map +1 -1
- package/dist/browser.mjs +30 -11
- package/dist/browser.mjs.map +1 -1
- package/dist/csv-BQPkzC5Y.mjs +386 -0
- package/dist/csv-BQPkzC5Y.mjs.map +1 -0
- package/dist/csv.d.mts +4 -2
- package/dist/csv.d.mts.map +1 -1
- package/dist/csv.mjs +1 -248
- package/dist/index.d.mts +327 -18
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +3555 -4
- package/dist/index.mjs.map +1 -0
- package/dist/node-sqlite.d.mts +98 -0
- package/dist/node-sqlite.d.mts.map +1 -0
- package/dist/node-sqlite.mjs +684 -0
- package/dist/node-sqlite.mjs.map +1 -0
- package/dist/runtime-9_adwQ0y.mjs +5637 -0
- package/dist/runtime-9_adwQ0y.mjs.map +1 -0
- package/dist/runtime-C9IcEeTi.d.mts +728 -0
- package/dist/runtime-C9IcEeTi.d.mts.map +1 -0
- package/dist/select-options-CfNwfc_a.mjs +561 -0
- package/dist/select-options-CfNwfc_a.mjs.map +1 -0
- package/dist/sqlite-wasm-BM6hsxH8.mjs +2007 -0
- package/dist/sqlite-wasm-BM6hsxH8.mjs.map +1 -0
- package/dist/temporal-fyRCzJjy.d.mts +517 -0
- package/dist/temporal-fyRCzJjy.d.mts.map +1 -0
- package/package.json +11 -4
- package/dist/csv.mjs.map +0 -1
- package/dist/host-CqP1MeMK.d.mts +0 -213
- package/dist/host-CqP1MeMK.d.mts.map +0 -1
- package/dist/host-D5kZAhga.mjs +0 -478
- package/dist/host-D5kZAhga.mjs.map +0 -1
- package/dist/relation-values-B0JhHBjZ.mjs +0 -232
- package/dist/relation-values-B0JhHBjZ.mjs.map +0 -1
- package/dist/runtime-C3bstvB8.d.mts +0 -465
- package/dist/runtime-C3bstvB8.d.mts.map +0 -1
- package/dist/runtime-CWC5y95G.mjs +0 -2907
- package/dist/runtime-CWC5y95G.mjs.map +0 -1
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,3555 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { A as
|
|
4
|
-
|
|
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, _ as normalizeEidosFileAttachmentPath, b as assertEidosFileDisplayName, c as normalizeEidosFileDate, d as decodeEidosFileAttachmentPaths, f as decodeEidosFileValues, g as isSafeEidosFileUri, h as encodeEidosFileValues, j as parseEidosFileJson, k as canonicalizeEidosFileJson, l as normalizeEidosFileInstant, m as encodeEidosFileAttachmentPaths, n as parseEidosFileSelectOptions, o as isCanonicalEidosFileDate, p as eidosFileUriClass, r as currentEidosFileInstant, s as isCanonicalEidosFileInstant, t as assertEidosFileSelectOptions, u as assertEidosFileValues, v as EidosUuidV7Generator, w as createEidosFileIdentifier, x as assertEidosFileTableId, y as assertEidosFileColumnName } from "./select-options-CfNwfc_a.mjs";
|
|
2
|
+
import { a as eidosFileTypeHandler, c as AdapterTransportRuntimeClient, d as transportFitsHostLimit, f as validateAdapterCarrier, i as EidosFileSession, l as AdapterTransportServer, n as EidosFileHandlerRegistry, o as EidosFileRuntimeDataSource, p as validateAdapterEnvelope, r as EidosFileHostError, s as eidosFileSnapshot, t as SQLiteWasmConnectionPort, u as EIDOS_ADAPTER_TRANSPORT_LIMITS } from "./sqlite-wasm-BM6hsxH8.mjs";
|
|
3
|
+
import { $ as EIDOS_FILE_SCHEMA_VERSION, A as eidosFileLookupStorageCodec, B as rewriteEidosFileFormulaFieldReferences, C as normalizeEidosFileRowQuery, D as eidosFileLookupAggregateSupportsTarget, E as eidosFileFieldStoresJsonArray, F as EIDOS_FILE_FORMULA_FUNCTION_NAMES, G as EIDOS_FILE_FORMAT, H as EIDOS_FILE_EXTENSION, I as compileEidosFileFormula, J as EIDOS_FILE_LOOKUP_FIELDS_TABLE, K as EIDOS_FILE_FORMAT_VERSION, L as compileEidosFileFormulaFields, M as eidosFileLookupTargetValueType, N as eidosFileLookupValueType, O as eidosFileLookupDisplayType, P as EIDOS_FILE_FORMULA_FIELD_FUNCTION_NAMES, Q as EIDOS_FILE_REQUIRED_TABLES, R as compileEidosFileFormulaSource, S as normalizeEidosFileFilter, T as removeEidosFileFilterField, U as EIDOS_FILE_FEATURES_TABLE, V as EIDOS_FILE_APPLICATION_ID, W as EIDOS_FILE_FIELDS_TABLE, X as EIDOS_FILE_MIME_TYPE, Y as EIDOS_FILE_META_TABLE, Z as EIDOS_FILE_RELATION_FIELDS_TABLE, _ as assertEidosFileRowQuery, a as EIDOS_FILE_FORMAT_VERSION_TEXT, at as eidosFileColumnStatLabel, b as eidosFileRowQueryPredicateColumns, c as incrementEidosFileRevision, ct as EidosAdapterError, d as decodeEidosFileRelationDisplay, dt as assertUnicodeString, et as EIDOS_FILE_SYSTEM_COLUMNS, f as decodeEidosFileRelationIds, g as parseNonNegativeInt64, h as cancellationPortFromSignal, i as scanStableRowPages, it as hasEidosFileSqliteHeader, j as eidosFileLookupTargetDisplayType, k as eidosFileLookupElementType, l as initializeEidosFileSchema, m as MemoryByteSource, mt as sqlValueToNative, n as validateEidosFile, nt as EIDOS_FILE_VIEWS_TABLE, o as EIDOS_FILE_SCHEMA_SQL, ot as eidosFileColumnStatTypesForField, p as encodeEidosFileRelationIds, pt as nativeToSqlValue, q as EIDOS_FILE_FORMULA_FIELDS_TABLE, r as smallestDependencyCycle, rt as SQLITE_HEADER, s as configureEidosFileConnection, st as ConnectionPortEidosFileConnection, t as EidosFileRuntime, tt as EIDOS_FILE_TABLES_TABLE, u as setEidosFileMetadata, ut as assertInt64Decimal, v as compileEidosFileRowQuery, w as normalizeEidosFileSorts, x as eidosFileSortExpression, y as eidosFileRowQueryAffectedByFieldChanges, z as eidosFileFormulaReferences } from "./runtime-9_adwQ0y.mjs";
|
|
4
|
+
import { _ as isMutableEidosFileFieldType, a as eidosFileCsvExportHeader, b as decodeEidosFileStringArray, c as importEidosFileCsv, d as prepareEidosFileCsvImport, f as MUTABLE_BASE_FIELD_TYPES, g as encodeEidosFileMultiSelectValues, h as eidosFileFieldDisplayValues, i as createEidosFileCsvRowEncoder, l as parseEidosFileCsvRows, m as eidosFileFieldConversionMayRequireLossyConfirmation, n as EIDOS_FILE_CSV_PREVIEW_ROW_COUNT, o as eidosFileCsvRowToEidosFileRow, p as decodeEidosFileMultiSelectValues, r as createEidosFileCsvImportPlan, s as encodeEidosFileCsvRecord, t as EIDOS_FILE_CSV_INFERENCE_ROW_COUNT, u as planEidosFileCsvImport, v as planEidosFileFieldConversion, x as encodeEidosFileJsonArray, y as decodeEidosFileJsonArray } from "./csv-BQPkzC5Y.mjs";
|
|
5
|
+
//#region src/canonical-conversion.ts
|
|
6
|
+
/**
|
|
7
|
+
* Product defaults for conversions requested by the Eidos File editor.
|
|
8
|
+
*
|
|
9
|
+
* These policies are intentionally explicit: the Runtime still reports an
|
|
10
|
+
* `explicit-lossy` preflight and requires confirmation whenever the values in
|
|
11
|
+
* the current Field would actually lose information.
|
|
12
|
+
*/
|
|
13
|
+
function recommendedEidosFileConversionPolicies(from, to) {
|
|
14
|
+
const policies = [];
|
|
15
|
+
if (from === "integer" && to === "number") policies.push("round-binary64");
|
|
16
|
+
if (from === "number" && to === "integer") policies.push("round-ties-even");
|
|
17
|
+
if ((from === "integer" || from === "number") && to === "checkbox") policies.push("zero-false-nonzero-true");
|
|
18
|
+
if (from === "datetime" && to === "date") policies.push("utc-date");
|
|
19
|
+
if (from === "multi-select" && to === "select") policies.push("first");
|
|
20
|
+
if (!LIST_TYPES.has(from) && LIST_TYPES.has(to)) policies.push("null-to-empty-list");
|
|
21
|
+
return policies;
|
|
22
|
+
}
|
|
23
|
+
/** Returns the nullable shape used by editor-initiated conversions. */
|
|
24
|
+
function eidosFileConversionTargetNullable(from, to, sourceNullable) {
|
|
25
|
+
return sourceNullable || from === "multi-select" && to === "select";
|
|
26
|
+
}
|
|
27
|
+
const INT64_MIN = -9223372036854775808n;
|
|
28
|
+
const INT64_MAX = 9223372036854775807n;
|
|
29
|
+
const INT64 = /^(?:0|-[1-9][0-9]*|[1-9][0-9]*)$/u;
|
|
30
|
+
const LIST_TYPES = new Set([
|
|
31
|
+
"multi-select",
|
|
32
|
+
"file",
|
|
33
|
+
"relation"
|
|
34
|
+
]);
|
|
35
|
+
const TEXT_TYPES = new Set([
|
|
36
|
+
"text",
|
|
37
|
+
"date",
|
|
38
|
+
"datetime",
|
|
39
|
+
"url",
|
|
40
|
+
"select"
|
|
41
|
+
]);
|
|
42
|
+
const SHARED_TEXT_COLUMN_TYPES = new Set([
|
|
43
|
+
"text",
|
|
44
|
+
"select",
|
|
45
|
+
"url"
|
|
46
|
+
]);
|
|
47
|
+
/** True when a conversion can retain the exact nullable SQLite column DDL. */
|
|
48
|
+
function eidosFileConversionCanReusePhysicalColumn(from, to, sourceNullable, targetNullable) {
|
|
49
|
+
return sourceNullable === targetNullable && SHARED_TEXT_COLUMN_TYPES.has(from) && SHARED_TEXT_COLUMN_TYPES.has(to);
|
|
50
|
+
}
|
|
51
|
+
function planCanonicalFieldConversion(input) {
|
|
52
|
+
const policies = new Set(input.policies ?? []);
|
|
53
|
+
const sourceHasEmptyList = input.rows.some((row) => {
|
|
54
|
+
if (row.value === null || typeof row.value !== "string") return false;
|
|
55
|
+
return row.value === "[]";
|
|
56
|
+
});
|
|
57
|
+
const changes = /* @__PURE__ */ new Map();
|
|
58
|
+
const rows = [];
|
|
59
|
+
let severity = 0;
|
|
60
|
+
let error;
|
|
61
|
+
for (const row of input.rows) try {
|
|
62
|
+
let converted;
|
|
63
|
+
if (row.value === null) converted = convertNull(input, policies, sourceHasEmptyList);
|
|
64
|
+
else converted = convertNonNull(input, row.value, policies);
|
|
65
|
+
if (converted.value === null && !input.toNullable && !LIST_TYPES.has(input.to)) throw new Error("Destination non-nullability rejects SQL NULL");
|
|
66
|
+
validateDestination(input, converted.value);
|
|
67
|
+
const changed = storageClass(input.from) !== storageClass(input.to) || !sameStorageValue(row.value, converted.value);
|
|
68
|
+
rows.push({
|
|
69
|
+
id: row.id,
|
|
70
|
+
value: converted.value,
|
|
71
|
+
changed
|
|
72
|
+
});
|
|
73
|
+
if (changed) {
|
|
74
|
+
severity = Math.max(severity, converted.class);
|
|
75
|
+
changes.set("value-reencoded", (changes.get("value-reencoded") ?? 0) + 1);
|
|
76
|
+
if (converted.code) changes.set(converted.code, (changes.get(converted.code) ?? 0) + 1);
|
|
77
|
+
}
|
|
78
|
+
} catch (cause) {
|
|
79
|
+
severity = 3;
|
|
80
|
+
error = cause instanceof Error ? cause.message : "Conversion is forbidden";
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
if (severity === 3) return {
|
|
84
|
+
classification: "forbidden",
|
|
85
|
+
rows: [],
|
|
86
|
+
affectedRows: "0",
|
|
87
|
+
valueChanges: [],
|
|
88
|
+
error
|
|
89
|
+
};
|
|
90
|
+
const affectedRows = rows.filter((row) => row.changed).length;
|
|
91
|
+
return {
|
|
92
|
+
classification: severity === 2 ? "explicit-lossy" : severity === 1 ? "lossless-rewrite" : "metadata-only",
|
|
93
|
+
rows,
|
|
94
|
+
affectedRows: String(affectedRows),
|
|
95
|
+
valueChanges: Array.from(changes, ([code, count]) => ({
|
|
96
|
+
code,
|
|
97
|
+
rows: String(count)
|
|
98
|
+
}))
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
function convertNull(input, policies, sourceHasEmptyList) {
|
|
102
|
+
if (LIST_TYPES.has(input.to)) {
|
|
103
|
+
if (!policies.has("null-to-empty-list")) throw new Error("SQL NULL requires null-to-empty-list for a list destination");
|
|
104
|
+
return {
|
|
105
|
+
value: "[]",
|
|
106
|
+
class: sourceHasEmptyList ? 2 : 1,
|
|
107
|
+
code: "null-to-empty-list"
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
if (!input.toNullable) throw new Error("Destination non-nullability rejects SQL NULL");
|
|
111
|
+
return {
|
|
112
|
+
value: null,
|
|
113
|
+
class: 0
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
function convertNonNull(input, value, policies) {
|
|
117
|
+
const { from, to } = input;
|
|
118
|
+
if (from === to) return {
|
|
119
|
+
value,
|
|
120
|
+
class: 0
|
|
121
|
+
};
|
|
122
|
+
if (TEXT_TYPES.has(from) && TEXT_TYPES.has(to)) {
|
|
123
|
+
if (to === "datetime" && from === "date") return {
|
|
124
|
+
value: `${asText(value)}T00:00:00.000Z`,
|
|
125
|
+
class: 1
|
|
126
|
+
};
|
|
127
|
+
if (to === "date" && from === "datetime") {
|
|
128
|
+
const text = asText(value);
|
|
129
|
+
if (text.endsWith("T00:00:00.000Z")) return {
|
|
130
|
+
value: text.slice(0, 10),
|
|
131
|
+
class: 1
|
|
132
|
+
};
|
|
133
|
+
if (!policies.has("utc-date")) throw new Error("Datetime with a time component requires utc-date");
|
|
134
|
+
return {
|
|
135
|
+
value: text.slice(0, 10),
|
|
136
|
+
class: 2,
|
|
137
|
+
code: "datetime-to-date"
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
return {
|
|
141
|
+
value: asText(value),
|
|
142
|
+
class: 0
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
if (to === "text") {
|
|
146
|
+
if (from === "number") return {
|
|
147
|
+
value: canonicalNumber(asNumber(value)),
|
|
148
|
+
class: 1
|
|
149
|
+
};
|
|
150
|
+
if (from === "integer") return {
|
|
151
|
+
value: String(asInteger(value)),
|
|
152
|
+
class: 1
|
|
153
|
+
};
|
|
154
|
+
if (from === "checkbox") return {
|
|
155
|
+
value: asCheckbox(value) ? "true" : "false",
|
|
156
|
+
class: 1
|
|
157
|
+
};
|
|
158
|
+
if (LIST_TYPES.has(from)) return {
|
|
159
|
+
value: asText(value),
|
|
160
|
+
class: 0
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
if (to === "select") {
|
|
164
|
+
if (from === "number") return {
|
|
165
|
+
value: canonicalNumber(asNumber(value)),
|
|
166
|
+
class: 1
|
|
167
|
+
};
|
|
168
|
+
if (from === "integer") return {
|
|
169
|
+
value: String(asInteger(value)),
|
|
170
|
+
class: 1
|
|
171
|
+
};
|
|
172
|
+
if (from === "checkbox") return {
|
|
173
|
+
value: asCheckbox(value) ? "true" : "false",
|
|
174
|
+
class: 1
|
|
175
|
+
};
|
|
176
|
+
if (from === "multi-select") {
|
|
177
|
+
const list = parseStringList(value);
|
|
178
|
+
if (list.length === 1) return {
|
|
179
|
+
value: list[0],
|
|
180
|
+
class: 1
|
|
181
|
+
};
|
|
182
|
+
if (!policies.has("first")) throw new Error("Multi-select to Select requires a singleton or first");
|
|
183
|
+
if (list.length === 0) {
|
|
184
|
+
if (!input.toNullable) throw new Error("Empty list requires nullable Select");
|
|
185
|
+
return {
|
|
186
|
+
value: null,
|
|
187
|
+
class: 1,
|
|
188
|
+
code: "list-empty-to-null"
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
return {
|
|
192
|
+
value: list[0],
|
|
193
|
+
class: 2,
|
|
194
|
+
code: "list-tail-dropped"
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
if (to === "number") {
|
|
199
|
+
if (from === "integer") {
|
|
200
|
+
const integer = asInteger(value);
|
|
201
|
+
const number = Number(integer);
|
|
202
|
+
if (Number.isInteger(number) && BigInt(number) === integer) return {
|
|
203
|
+
value: number,
|
|
204
|
+
class: 1
|
|
205
|
+
};
|
|
206
|
+
if (!policies.has("round-binary64") || !Number.isFinite(number)) throw new Error("Integer is not exactly representable as binary64");
|
|
207
|
+
return {
|
|
208
|
+
value: number,
|
|
209
|
+
class: 2,
|
|
210
|
+
code: "binary64-rounded"
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
if (from === "checkbox") return {
|
|
214
|
+
value: asCheckbox(value) ? 1 : 0,
|
|
215
|
+
class: 1
|
|
216
|
+
};
|
|
217
|
+
if (from === "text" || from === "select") {
|
|
218
|
+
const text = asText(value);
|
|
219
|
+
const number = Number(text);
|
|
220
|
+
if (!Number.isFinite(number) || canonicalNumber(number) !== text) throw new Error("Text is not the exact inverse binary64 spelling");
|
|
221
|
+
return {
|
|
222
|
+
value: number,
|
|
223
|
+
class: 1
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
if (to === "integer") {
|
|
228
|
+
if (from === "checkbox") return {
|
|
229
|
+
value: asCheckbox(value) ? 1n : 0n,
|
|
230
|
+
class: 0
|
|
231
|
+
};
|
|
232
|
+
if (from === "text" || from === "select") return {
|
|
233
|
+
value: parseInt64(asText(value)),
|
|
234
|
+
class: 1
|
|
235
|
+
};
|
|
236
|
+
if (from === "number") return numberToInteger(asNumber(value), policies);
|
|
237
|
+
}
|
|
238
|
+
if (to === "checkbox") {
|
|
239
|
+
if (from === "text" || from === "select") {
|
|
240
|
+
const text = asText(value);
|
|
241
|
+
if (text === "true") return {
|
|
242
|
+
value: 1n,
|
|
243
|
+
class: 1
|
|
244
|
+
};
|
|
245
|
+
if (text === "false") return {
|
|
246
|
+
value: 0n,
|
|
247
|
+
class: 1
|
|
248
|
+
};
|
|
249
|
+
throw new Error("Text is not lowercase true or false");
|
|
250
|
+
}
|
|
251
|
+
if (from === "integer") {
|
|
252
|
+
const integer = asInteger(value);
|
|
253
|
+
if (integer === 0n || integer === 1n) return {
|
|
254
|
+
value: integer,
|
|
255
|
+
class: 0
|
|
256
|
+
};
|
|
257
|
+
if (!policies.has("zero-false-nonzero-true")) throw new Error("Integer Checkbox conversion requires exact 0/1");
|
|
258
|
+
return {
|
|
259
|
+
value: integer === 0n ? 0n : 1n,
|
|
260
|
+
class: 2,
|
|
261
|
+
code: "numeric-to-checkbox"
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
if (from === "number") {
|
|
265
|
+
const number = asNumber(value);
|
|
266
|
+
if (number === 0 || number === 1) return {
|
|
267
|
+
value: BigInt(number),
|
|
268
|
+
class: 1
|
|
269
|
+
};
|
|
270
|
+
if (!policies.has("zero-false-nonzero-true")) throw new Error("Number Checkbox conversion requires exact 0.0/1.0");
|
|
271
|
+
return {
|
|
272
|
+
value: number === 0 ? 0n : 1n,
|
|
273
|
+
class: 2,
|
|
274
|
+
code: "numeric-to-checkbox"
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
if (LIST_TYPES.has(to)) {
|
|
279
|
+
if (from === "select" && to === "multi-select") return {
|
|
280
|
+
value: canonicalizeEidosFileJson([asText(value)]),
|
|
281
|
+
class: 1
|
|
282
|
+
};
|
|
283
|
+
if (from === "text" && to === "multi-select") return {
|
|
284
|
+
value: canonicalizeEidosFileJson([asText(value)]),
|
|
285
|
+
class: 1
|
|
286
|
+
};
|
|
287
|
+
if (from === "multi-select" && to === "relation" || from === "relation" && to === "multi-select" || from === to) return {
|
|
288
|
+
value: asText(value),
|
|
289
|
+
class: 0
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
throw new Error(`Conversion from ${from} to ${to} is forbidden`);
|
|
293
|
+
}
|
|
294
|
+
function numberToInteger(number, policies) {
|
|
295
|
+
let result = number;
|
|
296
|
+
let code;
|
|
297
|
+
let classification = 1;
|
|
298
|
+
if (!Number.isInteger(result)) {
|
|
299
|
+
if (policies.has("truncate-toward-zero")) {
|
|
300
|
+
result = Math.trunc(result);
|
|
301
|
+
code = "fraction-truncated";
|
|
302
|
+
} else if (policies.has("round-ties-even")) {
|
|
303
|
+
result = roundTiesEven(result);
|
|
304
|
+
code = "integer-rounded";
|
|
305
|
+
} else throw new Error("Fractional Number requires an Integer rounding policy");
|
|
306
|
+
classification = 2;
|
|
307
|
+
}
|
|
308
|
+
if (!Number.isInteger(result)) throw new Error("Integer result is not integral");
|
|
309
|
+
const integer = BigInt(result);
|
|
310
|
+
if (integer < INT64_MIN || integer > INT64_MAX) throw new Error("Integer result is outside signed int64");
|
|
311
|
+
return {
|
|
312
|
+
value: integer,
|
|
313
|
+
class: classification,
|
|
314
|
+
...code ? { code } : {}
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
function validateDestination(input, value) {
|
|
318
|
+
if (value === null) return;
|
|
319
|
+
switch (input.to) {
|
|
320
|
+
case "number":
|
|
321
|
+
asNumber(value);
|
|
322
|
+
return;
|
|
323
|
+
case "integer":
|
|
324
|
+
asInteger(value);
|
|
325
|
+
return;
|
|
326
|
+
case "checkbox":
|
|
327
|
+
asCheckbox(value);
|
|
328
|
+
return;
|
|
329
|
+
case "date":
|
|
330
|
+
if (!isCanonicalEidosFileDate(value)) throw new Error("Invalid Date value");
|
|
331
|
+
return;
|
|
332
|
+
case "datetime":
|
|
333
|
+
if (!isCanonicalEidosFileInstant(value)) throw new Error("Invalid Datetime value");
|
|
334
|
+
return;
|
|
335
|
+
case "url":
|
|
336
|
+
if (!isUriReference(asText(value))) throw new Error("Invalid URI-reference");
|
|
337
|
+
return;
|
|
338
|
+
case "multi-select":
|
|
339
|
+
parseStringList(value);
|
|
340
|
+
return;
|
|
341
|
+
case "file":
|
|
342
|
+
decodeEidosFileValues(asText(value));
|
|
343
|
+
return;
|
|
344
|
+
case "relation": {
|
|
345
|
+
const ids = parseStringList(value);
|
|
346
|
+
if (!ids.every(isEidosFileUuid)) throw new Error("Invalid Relation Row ID");
|
|
347
|
+
if (input.relationCardinality === "one" && ids.length > 1) throw new Error("One-cardinality Relation contains more than one Row ID");
|
|
348
|
+
if (input.relationIdValid && !ids.every(input.relationIdValid)) throw new Error("Relation Row ID does not resolve in the target Table");
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
case "text":
|
|
352
|
+
case "select":
|
|
353
|
+
asText(value);
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
function parseStringList(value) {
|
|
358
|
+
const text = asText(value);
|
|
359
|
+
if (!isCanonicalEidosFileJson(text)) throw new Error("List is not canonical JSON");
|
|
360
|
+
const parsed = parseEidosFileJson(text);
|
|
361
|
+
if (!Array.isArray(parsed) || !parsed.every((entry) => typeof entry === "string")) throw new Error("List must contain only strings");
|
|
362
|
+
if (new Set(parsed).size !== parsed.length) throw new Error("List values must be unique");
|
|
363
|
+
return parsed;
|
|
364
|
+
}
|
|
365
|
+
function asText(value) {
|
|
366
|
+
if (typeof value !== "string") throw new Error("Expected TEXT storage");
|
|
367
|
+
return value;
|
|
368
|
+
}
|
|
369
|
+
function asNumber(value) {
|
|
370
|
+
if (typeof value !== "number" || !Number.isFinite(value)) throw new Error("Expected finite REAL storage");
|
|
371
|
+
return Object.is(value, -0) ? 0 : value;
|
|
372
|
+
}
|
|
373
|
+
function asInteger(value) {
|
|
374
|
+
if (typeof value === "bigint") {
|
|
375
|
+
if (value < INT64_MIN || value > INT64_MAX) throw new Error("INTEGER outside int64");
|
|
376
|
+
return value;
|
|
377
|
+
}
|
|
378
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value)) throw new Error("Expected exact INTEGER storage");
|
|
379
|
+
return BigInt(value);
|
|
380
|
+
}
|
|
381
|
+
function asCheckbox(value) {
|
|
382
|
+
const integer = asInteger(value);
|
|
383
|
+
if (integer !== 0n && integer !== 1n) throw new Error("Checkbox must be 0 or 1");
|
|
384
|
+
return integer === 1n;
|
|
385
|
+
}
|
|
386
|
+
function parseInt64(value) {
|
|
387
|
+
if (!INT64.test(value) || value === "-0") throw new Error("Invalid int64 spelling");
|
|
388
|
+
const integer = BigInt(value);
|
|
389
|
+
if (integer < INT64_MIN || integer > INT64_MAX) throw new Error("INTEGER outside int64");
|
|
390
|
+
return integer;
|
|
391
|
+
}
|
|
392
|
+
function canonicalNumber(value) {
|
|
393
|
+
return canonicalizeEidosFileJson(value);
|
|
394
|
+
}
|
|
395
|
+
function roundTiesEven(value) {
|
|
396
|
+
const floor = Math.floor(value);
|
|
397
|
+
const fraction = value - floor;
|
|
398
|
+
if (fraction < .5) return floor;
|
|
399
|
+
if (fraction > .5) return floor + 1;
|
|
400
|
+
return floor % 2 === 0 ? floor : floor + 1;
|
|
401
|
+
}
|
|
402
|
+
function isUriReference(value) {
|
|
403
|
+
return !/[\u0000-\u0020<>"{}|\\^`]/u.test(value) && !/%(?![0-9A-Fa-f]{2})/u.test(value);
|
|
404
|
+
}
|
|
405
|
+
function sameStorageValue(left, right) {
|
|
406
|
+
if (left instanceof Uint8Array || right instanceof Uint8Array) return left instanceof Uint8Array && right instanceof Uint8Array && left.byteLength === right.byteLength && left.every((byte, index) => byte === right[index]);
|
|
407
|
+
if ((typeof left === "bigint" || typeof left === "number" && Number.isSafeInteger(left)) && (typeof right === "bigint" || typeof right === "number" && Number.isSafeInteger(right))) return BigInt(left) === BigInt(right);
|
|
408
|
+
return typeof left === typeof right && Object.is(left, right);
|
|
409
|
+
}
|
|
410
|
+
function storageClass(type) {
|
|
411
|
+
if (type === "integer" || type === "checkbox") return "integer";
|
|
412
|
+
if (type === "number") return "real";
|
|
413
|
+
return "text";
|
|
414
|
+
}
|
|
415
|
+
//#endregion
|
|
416
|
+
//#region src/runtime-service.ts
|
|
417
|
+
const EIDOS_RUNTIME_LIMITS = Object.freeze({
|
|
418
|
+
requestBytesMax: 8 * 1024 * 1024,
|
|
419
|
+
responseBytesMax: 16 * 1024 * 1024,
|
|
420
|
+
schemaPageSizeMax: 1e3,
|
|
421
|
+
pageSizeMax: 1e3,
|
|
422
|
+
projectionFieldsMax: 256,
|
|
423
|
+
rowsByIdMax: 1e3,
|
|
424
|
+
mutationRowsMax: 500,
|
|
425
|
+
mutationCellsMax: 25e3,
|
|
426
|
+
mutationBytesMax: 8 * 1024 * 1024,
|
|
427
|
+
aggregateItemsMax: 128,
|
|
428
|
+
groupPageSizeMax: 256,
|
|
429
|
+
formulaPreviewRowsMax: 100,
|
|
430
|
+
filterDepthMax: 8,
|
|
431
|
+
filterNodesMax: 100,
|
|
432
|
+
sortFieldsMax: 32,
|
|
433
|
+
groupFieldsMax: 8,
|
|
434
|
+
searchBytesMax: 4096,
|
|
435
|
+
listElementsMax: 1e4,
|
|
436
|
+
logicalValueBytesMax: 2 * 1048576,
|
|
437
|
+
formulaBytesMax: 4096,
|
|
438
|
+
formulaNodesMax: 1e4,
|
|
439
|
+
formulaDepthMax: 256,
|
|
440
|
+
diagnosticsMax: 1e3,
|
|
441
|
+
foregroundTimeMsMax: 3e4,
|
|
442
|
+
csvBytesMax: 16 * 1024 * 1024,
|
|
443
|
+
schemaPlanEntriesMax: 64,
|
|
444
|
+
schemaPlanBytesMax: 8 * 1024 * 1024,
|
|
445
|
+
undoEntriesMax: 1,
|
|
446
|
+
undoBytesMax: 1
|
|
447
|
+
});
|
|
448
|
+
const READONLY_CAPABILITIES = Object.freeze({
|
|
449
|
+
readRows: true,
|
|
450
|
+
schemaPaging: true,
|
|
451
|
+
cursorPaging: true,
|
|
452
|
+
aggregate: true,
|
|
453
|
+
groupRows: true,
|
|
454
|
+
formulaPreview: true,
|
|
455
|
+
mutateRows: false,
|
|
456
|
+
mutationUndo: false,
|
|
457
|
+
mutateView: false,
|
|
458
|
+
schemaPreflight: false,
|
|
459
|
+
mutateSchema: false,
|
|
460
|
+
validate: true,
|
|
461
|
+
events: false,
|
|
462
|
+
csvExport: false,
|
|
463
|
+
csvImport: false
|
|
464
|
+
});
|
|
465
|
+
function runtimeCapabilities(writable) {
|
|
466
|
+
return {
|
|
467
|
+
...READONLY_CAPABILITIES,
|
|
468
|
+
mutateRows: writable,
|
|
469
|
+
mutateView: writable,
|
|
470
|
+
schemaPreflight: writable,
|
|
471
|
+
mutateSchema: writable,
|
|
472
|
+
csvImport: writable
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
var Runtime = class {
|
|
476
|
+
static async open(connection, environment, mode, context) {
|
|
477
|
+
assertFactoryContext(context, environment);
|
|
478
|
+
assertConnectionCapabilities(connection);
|
|
479
|
+
if (mode === "readwrite" && !connection.capabilities().snapshot) throw runtimeError("adapter-error", "Adapter snapshot capability is required");
|
|
480
|
+
const bridge = new ConnectionPortEidosFileConnection(connection);
|
|
481
|
+
const validation = connection.transaction("read", () => validateEidosFile(bridge, { level: mode === "readwrite" ? "structural" : "identity" }));
|
|
482
|
+
if (!validation.valid) throw runtimeError("corrupt-file", validation.errors.map((issue) => issue.message).join("; ") || "Eidos File validation failed");
|
|
483
|
+
return createRuntimeBinding(connection, environment, mode);
|
|
484
|
+
}
|
|
485
|
+
static async create(connection, environment, createInput, context) {
|
|
486
|
+
assertFactoryContext(context, environment);
|
|
487
|
+
assertConnectionCapabilities(connection);
|
|
488
|
+
assertEidosFileDisplayName(createInput.title, "File title");
|
|
489
|
+
if (createInput.createdAt !== void 0 && !isCanonicalEidosFileInstant(createInput.createdAt)) throw runtimeError("invalid-request", "createdAt must be a canonical millisecond UTC instant", { path: "/createdAt" });
|
|
490
|
+
if (createInput.fileId !== void 0) try {
|
|
491
|
+
assertEidosFileUuid(createInput.fileId, "File ID");
|
|
492
|
+
} catch {
|
|
493
|
+
throw runtimeError("invalid-request", "fileId must be UUIDv7", { path: "/fileId" });
|
|
494
|
+
}
|
|
495
|
+
const generator = uuidGenerator(environment);
|
|
496
|
+
const createdAt = createInput.createdAt ?? checkedNowInstant(environment);
|
|
497
|
+
const fileId = createInput.fileId ?? generator.next();
|
|
498
|
+
const bridge = new ConnectionPortEidosFileConnection(connection);
|
|
499
|
+
connection.transaction("write", () => {
|
|
500
|
+
if (connection.query("SELECT name FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%'").rows.length !== 0) throw runtimeError("invalid-request", "Runtime.create requires an empty database");
|
|
501
|
+
initializeEidosFileSchema(bridge, {
|
|
502
|
+
fileId,
|
|
503
|
+
title: createInput.title,
|
|
504
|
+
createdAt
|
|
505
|
+
}, false);
|
|
506
|
+
const validation = validateEidosFile(bridge, { level: "structural" });
|
|
507
|
+
if (!validation.valid) throw runtimeError("corrupt-file", validation.errors.map((issue) => issue.message).join("; "));
|
|
508
|
+
});
|
|
509
|
+
return createRuntimeBinding(connection, environment, "readwrite", generator);
|
|
510
|
+
}
|
|
511
|
+
};
|
|
512
|
+
function createRuntimeBinding(connection, environment, mode, generator = uuidGenerator(environment)) {
|
|
513
|
+
const service = new EidosRuntimeService(connection, new EidosFileRuntime(new ConnectionPortEidosFileConnection(connection), false, {
|
|
514
|
+
nowInstant: () => checkedNowInstant(environment),
|
|
515
|
+
allocateId: () => generator.next()
|
|
516
|
+
}), environment, mode === "readwrite", generator);
|
|
517
|
+
return {
|
|
518
|
+
service,
|
|
519
|
+
hostBridge: service.hostBridge
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
var EidosRuntimeService = class {
|
|
523
|
+
hostBridge;
|
|
524
|
+
capabilitiesValue;
|
|
525
|
+
epoch;
|
|
526
|
+
cursorSecret;
|
|
527
|
+
requestControls = /* @__PURE__ */ new Map();
|
|
528
|
+
schemaPlans = /* @__PURE__ */ new Map();
|
|
529
|
+
schemaPlanSequence = 0;
|
|
530
|
+
queue = Promise.resolve();
|
|
531
|
+
state = "open";
|
|
532
|
+
constructor(connection, core, environment, writable, generator) {
|
|
533
|
+
this.connection = connection;
|
|
534
|
+
this.core = core;
|
|
535
|
+
this.environment = environment;
|
|
536
|
+
this.writable = writable;
|
|
537
|
+
this.generator = generator;
|
|
538
|
+
this.capabilitiesValue = runtimeCapabilities(writable);
|
|
539
|
+
if (writable) this.importCsv = (request, context) => this.importCsvRequest(request, context);
|
|
540
|
+
this.epoch = generator.next();
|
|
541
|
+
this.cursorSecret = environment.entropy.randomBytes(16).slice();
|
|
542
|
+
if (this.cursorSecret.byteLength !== 16) throw runtimeError("adapter-error", "EntropyPort returned an invalid cursor authentication key");
|
|
543
|
+
this.hostBridge = {
|
|
544
|
+
allocateFileEntry: (request, context) => this.invoke(context, () => this.allocateFileEntry(request), false, request),
|
|
545
|
+
createPublicationSnapshot: (request, context) => this.invoke(context, async () => {
|
|
546
|
+
if (parseRevision(request.maxBytes, "maxBytes") < 0n) throw runtimeError("invalid-request", "maxBytes must be non-negative");
|
|
547
|
+
return this.connection.transaction("read", async () => {
|
|
548
|
+
const identity = this.connection.get("SELECT file_id, revision FROM eidos__meta WHERE singleton=1");
|
|
549
|
+
if (!identity.row) throw runtimeError("corrupt-file", "Missing Eidos File identity");
|
|
550
|
+
const fileId = taggedText(identity.row[0]);
|
|
551
|
+
const revision = taggedInteger(identity.row[1]);
|
|
552
|
+
const snapshot = await this.connection.snapshot({
|
|
553
|
+
cancellation: cancellationPortFromSignal(context.signal),
|
|
554
|
+
...context.deadlineMilliseconds === void 0 ? {} : { deadlineMilliseconds: context.deadlineMilliseconds },
|
|
555
|
+
maxBytes: request.maxBytes
|
|
556
|
+
});
|
|
557
|
+
return {
|
|
558
|
+
fileId,
|
|
559
|
+
revision,
|
|
560
|
+
bytes: snapshot.bytes,
|
|
561
|
+
release: snapshot.release
|
|
562
|
+
};
|
|
563
|
+
});
|
|
564
|
+
}, false, request, false)
|
|
565
|
+
};
|
|
566
|
+
}
|
|
567
|
+
negotiate(request, context) {
|
|
568
|
+
return this.invoke(context, () => {
|
|
569
|
+
if (request.protocol !== "eidos-runtime" || !Array.isArray(request.versions) || request.versions.length !== 1 || request.versions[0] !== "1.0") throw runtimeError("unsupported", "Eidos Runtime 1.0 is required");
|
|
570
|
+
return {
|
|
571
|
+
version: "1.0",
|
|
572
|
+
capabilities: { ...this.capabilitiesValue },
|
|
573
|
+
limits: { ...EIDOS_RUNTIME_LIMITS }
|
|
574
|
+
};
|
|
575
|
+
}, false, request);
|
|
576
|
+
}
|
|
577
|
+
getSnapshot(request, context) {
|
|
578
|
+
return this.invoke(context, () => this.snapshotAtMinimumRevision(request.minimumRevision, context), false, request);
|
|
579
|
+
}
|
|
580
|
+
async snapshotAtMinimumRevision(minimumRevision, context) {
|
|
581
|
+
const target = minimumRevision === void 0 ? null : parseRevision(minimumRevision, "minimumRevision");
|
|
582
|
+
const startedAt = this.environment.clock.nowMilliseconds();
|
|
583
|
+
const budget = Math.min(context.deadlineMilliseconds ?? EIDOS_RUNTIME_LIMITS.foregroundTimeMsMax, EIDOS_RUNTIME_LIMITS.foregroundTimeMsMax);
|
|
584
|
+
let current = this.read(() => this.snapshot());
|
|
585
|
+
while (target !== null && BigInt(current.revision) < target) {
|
|
586
|
+
if (this.requestControls.get(context.requestId)?.cancelled || context.signal?.aborted) throw runtimeError("cancelled", "Snapshot wait was cancelled", {
|
|
587
|
+
retryable: true,
|
|
588
|
+
currentRevision: current.revision
|
|
589
|
+
});
|
|
590
|
+
if (this.state !== "open") throw runtimeError("closed", "Runtime closed during snapshot wait");
|
|
591
|
+
const elapsed = this.environment.clock.nowMilliseconds() - startedAt;
|
|
592
|
+
if (!Number.isFinite(elapsed) || elapsed < 0) throw runtimeError("fatal", "Monotonic clock moved backward");
|
|
593
|
+
if (elapsed >= budget) throw runtimeError("deadline-exceeded", "minimumRevision was not reached before the deadline", {
|
|
594
|
+
retryable: true,
|
|
595
|
+
currentRevision: current.revision
|
|
596
|
+
});
|
|
597
|
+
await new Promise((resolve) => setTimeout(resolve, 10));
|
|
598
|
+
this.connection.dataVersion();
|
|
599
|
+
current = this.read(() => this.snapshot());
|
|
600
|
+
}
|
|
601
|
+
return current;
|
|
602
|
+
}
|
|
603
|
+
getSchemaPage(request, context) {
|
|
604
|
+
return this.invoke(context, async () => this.read(async () => {
|
|
605
|
+
assertLimit(request.limit, EIDOS_RUNTIME_LIMITS.schemaPageSizeMax, "limit");
|
|
606
|
+
const snapshot = this.snapshot();
|
|
607
|
+
assertCurrentRevision(request.revision, snapshot.revision);
|
|
608
|
+
let offset = 0;
|
|
609
|
+
if (request.cursor !== void 0) {
|
|
610
|
+
const payload = this.decodeCursor(request.cursor, "schema");
|
|
611
|
+
if (payload.fileId !== snapshot.fileId || payload.revision !== request.revision || typeof payload.offset !== "number" || !Number.isSafeInteger(payload.offset) || payload.offset < 0) throw runtimeError("invalid-query", "Invalid schema cursor");
|
|
612
|
+
offset = payload.offset;
|
|
613
|
+
}
|
|
614
|
+
const objects = this.schemaDescriptors();
|
|
615
|
+
const page = objects.slice(offset, offset + request.limit);
|
|
616
|
+
if (offset > objects.length) throw runtimeError("invalid-query", "Schema cursor is past the end");
|
|
617
|
+
const responseFor = (prefix) => {
|
|
618
|
+
const nextOffset = offset + prefix.length;
|
|
619
|
+
return {
|
|
620
|
+
fileId: snapshot.fileId,
|
|
621
|
+
revision: snapshot.revision,
|
|
622
|
+
objects: prefix,
|
|
623
|
+
nextCursor: nextOffset < objects.length ? this.encodeCursor("schema", {
|
|
624
|
+
fileId: snapshot.fileId,
|
|
625
|
+
revision: snapshot.revision,
|
|
626
|
+
offset: nextOffset
|
|
627
|
+
}) : null
|
|
628
|
+
};
|
|
629
|
+
};
|
|
630
|
+
while (page.length > 0 && jcsByteLength(responseFor(page)) > EIDOS_RUNTIME_LIMITS.responseBytesMax) page.pop();
|
|
631
|
+
if (jcsByteLength(responseFor(page)) > EIDOS_RUNTIME_LIMITS.responseBytesMax || offset < objects.length && page.length === 0) throw runtimeError("resource-limit", "Schema descriptor cannot fit responseBytesMax");
|
|
632
|
+
return responseFor(page);
|
|
633
|
+
}), false, request);
|
|
634
|
+
}
|
|
635
|
+
queryRows(request, context) {
|
|
636
|
+
return this.invoke(context, async () => this.read(async () => {
|
|
637
|
+
assertLimit(request.limit, EIDOS_RUNTIME_LIMITS.pageSizeMax, "limit");
|
|
638
|
+
assertProjection(request.projection);
|
|
639
|
+
if (request.offset !== void 0) {
|
|
640
|
+
if (!Number.isSafeInteger(request.offset) || request.offset < 0) throw runtimeError("invalid-request", "offset must be a non-negative JSON safe integer");
|
|
641
|
+
if (request.cursor !== void 0) throw runtimeError("invalid-request", "offset and cursor cannot be used together");
|
|
642
|
+
if (request.direction === "backward") throw runtimeError("invalid-request", "offset only supports forward row queries");
|
|
643
|
+
}
|
|
644
|
+
const query = this.compatibilityQuery(request.tableId, request.query);
|
|
645
|
+
const direction = request.direction ?? "forward";
|
|
646
|
+
const metadata = this.core.info();
|
|
647
|
+
const projectionDigest = await projectionHash(request.projection);
|
|
648
|
+
const queryDigest = await hashJson(normalizedRuntimeQuery(request.query));
|
|
649
|
+
const backwardQuery = reverseRuntimeQuery(request.query, this.core.listFields(request.tableId));
|
|
650
|
+
const traversalQuery = direction === "forward" ? request.query : backwardQuery;
|
|
651
|
+
const traversal = this.compatibilityQuery(request.tableId, traversalQuery);
|
|
652
|
+
let coreCursor;
|
|
653
|
+
if (request.cursor !== void 0) {
|
|
654
|
+
const cursor = this.decodeCursor(request.cursor, "rows");
|
|
655
|
+
if (cursor.fileId !== metadata.fileId || cursor.tableId !== request.tableId || cursor.queryHash !== queryDigest || cursor.projectionHash !== projectionDigest) throw runtimeError("invalid-query", "Row cursor binding mismatch");
|
|
656
|
+
if (cursor.revision !== String(metadata.revision)) throw runtimeError("stale-revision", "Row cursor revision is stale", { currentRevision: String(metadata.revision) });
|
|
657
|
+
const selected = direction === "forward" ? cursor.forward : cursor.backward;
|
|
658
|
+
if (typeof selected !== "string") throw runtimeError("invalid-query", "Row cursor boundary is invalid");
|
|
659
|
+
coreCursor = selected;
|
|
660
|
+
}
|
|
661
|
+
const extraFields = (request.query.sort ?? []).map((sort) => sort.fieldId);
|
|
662
|
+
const fields = Array.from(new Set([...request.projection.fields, ...extraFields]));
|
|
663
|
+
const result = this.core.queryRows(request.tableId, {
|
|
664
|
+
fields,
|
|
665
|
+
query: traversal,
|
|
666
|
+
limit: request.limit + 1,
|
|
667
|
+
...request.offset !== void 0 ? { offset: request.offset } : {},
|
|
668
|
+
cursor: coreCursor,
|
|
669
|
+
resolveRelations: request.projection.resolveRelations.length > 0
|
|
670
|
+
});
|
|
671
|
+
const hasMoreInTraversal = result.rows.length > request.limit;
|
|
672
|
+
const traversalRows = result.rows.slice(0, request.limit);
|
|
673
|
+
const logicalRows = direction === "forward" ? traversalRows : traversalRows.reverse();
|
|
674
|
+
const columns = this.columns(request.tableId, request.projection);
|
|
675
|
+
const rows = this.projectRows(request.tableId, request.projection, logicalRows, columns);
|
|
676
|
+
const boundaryCursor = (row) => {
|
|
677
|
+
if (!row) return null;
|
|
678
|
+
return this.encodeCursor("rows", {
|
|
679
|
+
fileId: metadata.fileId,
|
|
680
|
+
tableId: request.tableId,
|
|
681
|
+
revision: String(metadata.revision),
|
|
682
|
+
queryHash: queryDigest,
|
|
683
|
+
projectionHash: projectionDigest,
|
|
684
|
+
forward: this.core.createRowCursor(request.tableId, query, row),
|
|
685
|
+
backward: this.core.createRowCursor(request.tableId, this.compatibilityQuery(request.tableId, backwardQuery), row)
|
|
686
|
+
});
|
|
687
|
+
};
|
|
688
|
+
const hasEarlier = direction === "backward" ? hasMoreInTraversal : request.cursor !== void 0 || (request.offset ?? 0) > 0;
|
|
689
|
+
const hasLater = direction === "forward" ? hasMoreInTraversal : request.cursor !== void 0;
|
|
690
|
+
return {
|
|
691
|
+
fileId: metadata.fileId,
|
|
692
|
+
tableId: request.tableId,
|
|
693
|
+
revision: String(metadata.revision),
|
|
694
|
+
projectionHash: projectionDigest,
|
|
695
|
+
columns,
|
|
696
|
+
rows,
|
|
697
|
+
nextCursor: hasLater ? boundaryCursor(logicalRows.at(-1)) : null,
|
|
698
|
+
previousCursor: hasEarlier ? boundaryCursor(logicalRows[0]) : null
|
|
699
|
+
};
|
|
700
|
+
}), false, request);
|
|
701
|
+
}
|
|
702
|
+
getRowsById(request, context) {
|
|
703
|
+
return this.invoke(context, async () => this.read(async () => {
|
|
704
|
+
if (request.rowIds.length > EIDOS_RUNTIME_LIMITS.rowsByIdMax || new Set(request.rowIds).size !== request.rowIds.length) throw runtimeError("invalid-request", "rowIds must be unique and within rowsByIdMax");
|
|
705
|
+
request.rowIds.forEach((id) => assertEidosFileUuid(id, "Row ID"));
|
|
706
|
+
assertProjection(request.projection);
|
|
707
|
+
return this.getRowsByIdDirect(request.tableId, request.rowIds, request.projection);
|
|
708
|
+
}), false, request);
|
|
709
|
+
}
|
|
710
|
+
aggregate(request, context) {
|
|
711
|
+
return this.invoke(context, () => this.read(() => this.aggregateNow(request)), false, request);
|
|
712
|
+
}
|
|
713
|
+
groupRows(request, context) {
|
|
714
|
+
return this.invoke(context, async () => this.read(async () => this.groupRowsNow(request)), false, request);
|
|
715
|
+
}
|
|
716
|
+
queryGroupRows(request, context) {
|
|
717
|
+
return this.invoke(context, async () => this.read(async () => {
|
|
718
|
+
assertLimit(request.limit, EIDOS_RUNTIME_LIMITS.pageSizeMax, "limit");
|
|
719
|
+
const cursor = this.decodeCursor(request.cursor, "group-rows");
|
|
720
|
+
const source = cursor.request;
|
|
721
|
+
if (!source || typeof source !== "object" || Array.isArray(source)) throw runtimeError("invalid-query", "Invalid group row cursor");
|
|
722
|
+
assertProjection(source.projection);
|
|
723
|
+
const metadata = this.core.info();
|
|
724
|
+
const projectionDigest = await projectionHash(source.projection);
|
|
725
|
+
const bindingDigest = await groupBindingHash(source);
|
|
726
|
+
if (cursor.fileId !== metadata.fileId || cursor.tableId !== source.tableId || cursor.projectionHash !== projectionDigest || cursor.bindingHash !== bindingDigest || !Array.isArray(cursor.groupKey) || typeof cursor.forward !== "string" || typeof cursor.backward !== "string") throw runtimeError("invalid-query", "Group row cursor binding mismatch");
|
|
727
|
+
if (cursor.revision !== String(metadata.revision)) throw runtimeError("stale-revision", "Group row cursor revision is stale", { currentRevision: String(metadata.revision) });
|
|
728
|
+
const groupKey = cursor.groupKey;
|
|
729
|
+
if (groupKey.length !== source.groupBy.length) throw runtimeError("invalid-query", "Group row cursor key is invalid");
|
|
730
|
+
const groupQuery = queryForGroup(source.query, source.groupBy, groupKey);
|
|
731
|
+
const direction = request.direction ?? "forward";
|
|
732
|
+
const backwardQuery = reverseRuntimeQuery(groupQuery, this.core.listFields(source.tableId));
|
|
733
|
+
const traversalQuery = direction === "forward" ? groupQuery : backwardQuery;
|
|
734
|
+
const fields = Array.from(new Set([...source.projection.fields, ...(groupQuery.sort ?? []).map((sort) => sort.fieldId)]));
|
|
735
|
+
const result = this.core.queryRows(source.tableId, {
|
|
736
|
+
fields,
|
|
737
|
+
query: this.compatibilityQuery(source.tableId, traversalQuery),
|
|
738
|
+
limit: request.limit + 1,
|
|
739
|
+
cursor: direction === "forward" ? cursor.forward : cursor.backward,
|
|
740
|
+
resolveRelations: source.projection.resolveRelations.length > 0
|
|
741
|
+
});
|
|
742
|
+
const hasMore = result.rows.length > request.limit;
|
|
743
|
+
const traversalRows = result.rows.slice(0, request.limit);
|
|
744
|
+
const logicalRows = direction === "forward" ? traversalRows : traversalRows.reverse();
|
|
745
|
+
const columns = this.columns(source.tableId, source.projection);
|
|
746
|
+
const rowCursor = (row) => {
|
|
747
|
+
if (!row) return null;
|
|
748
|
+
return this.encodeCursor("group-rows", {
|
|
749
|
+
fileId: metadata.fileId,
|
|
750
|
+
tableId: source.tableId,
|
|
751
|
+
revision: String(metadata.revision),
|
|
752
|
+
projectionHash: projectionDigest,
|
|
753
|
+
bindingHash: bindingDigest,
|
|
754
|
+
request: groupCursorSource(source),
|
|
755
|
+
groupKey,
|
|
756
|
+
forward: this.core.createRowCursor(source.tableId, this.compatibilityQuery(source.tableId, groupQuery), row),
|
|
757
|
+
backward: this.core.createRowCursor(source.tableId, this.compatibilityQuery(source.tableId, backwardQuery), row)
|
|
758
|
+
});
|
|
759
|
+
};
|
|
760
|
+
const hasEarlier = direction === "backward" ? hasMore : request.cursor !== void 0;
|
|
761
|
+
const hasLater = direction === "forward" ? hasMore : request.cursor !== void 0;
|
|
762
|
+
return {
|
|
763
|
+
fileId: metadata.fileId,
|
|
764
|
+
tableId: source.tableId,
|
|
765
|
+
revision: String(metadata.revision),
|
|
766
|
+
projectionHash: projectionDigest,
|
|
767
|
+
columns,
|
|
768
|
+
groupKey,
|
|
769
|
+
rows: this.projectRows(source.tableId, source.projection, logicalRows, columns),
|
|
770
|
+
nextCursor: hasLater ? rowCursor(logicalRows.at(-1)) : null,
|
|
771
|
+
previousCursor: hasEarlier ? rowCursor(logicalRows[0]) : null
|
|
772
|
+
};
|
|
773
|
+
}), false, request);
|
|
774
|
+
}
|
|
775
|
+
previewFormula(request, context) {
|
|
776
|
+
return this.invoke(context, () => {
|
|
777
|
+
if (!this.capabilitiesValue.formulaPreview) throw runtimeError("unsupported", "formulaPreview is unavailable");
|
|
778
|
+
return this.read(() => {
|
|
779
|
+
const name = request.fieldId === void 0 ? request.candidateName : this.core.listFields(request.tableId).find((field) => field.id === request.fieldId)?.name;
|
|
780
|
+
if (!name) throw runtimeError("invalid-request", "Formula candidate name is required");
|
|
781
|
+
try {
|
|
782
|
+
const result = this.core.previewFormula(request.tableId, {
|
|
783
|
+
name,
|
|
784
|
+
columnName: name,
|
|
785
|
+
formula: request.sourceText,
|
|
786
|
+
displayType: request.declaredResultType
|
|
787
|
+
});
|
|
788
|
+
const metadata = this.core.info();
|
|
789
|
+
return {
|
|
790
|
+
fileId: metadata.fileId,
|
|
791
|
+
revision: String(metadata.revision),
|
|
792
|
+
valid: true,
|
|
793
|
+
inferredType: request.declaredResultType,
|
|
794
|
+
dependencies: result.dependencies.flatMap((dependency) => {
|
|
795
|
+
const field = this.core.listFields(request.tableId).find((entry) => entry.name === dependency.name || entry.tableColumnName === dependency.columnName);
|
|
796
|
+
return field?.id ? [field.id] : [];
|
|
797
|
+
}),
|
|
798
|
+
rows: result.samples.map((sample) => ({
|
|
799
|
+
rowId: sample.rowId,
|
|
800
|
+
value: compatibilityLogicalValue(sample.value, request.declaredResultType)
|
|
801
|
+
})),
|
|
802
|
+
diagnostics: [],
|
|
803
|
+
diagnosticsTruncated: false
|
|
804
|
+
};
|
|
805
|
+
} catch (error) {
|
|
806
|
+
const mapped = mapRuntimeFailure(error);
|
|
807
|
+
return {
|
|
808
|
+
fileId: this.core.info().fileId,
|
|
809
|
+
revision: String(this.core.info().revision),
|
|
810
|
+
valid: false,
|
|
811
|
+
diagnostics: [{
|
|
812
|
+
code: "formula-parse-invalid",
|
|
813
|
+
severity: "error",
|
|
814
|
+
message: mapped.message
|
|
815
|
+
}],
|
|
816
|
+
diagnosticsTruncated: false
|
|
817
|
+
};
|
|
818
|
+
}
|
|
819
|
+
});
|
|
820
|
+
}, false, request);
|
|
821
|
+
}
|
|
822
|
+
mutateRows(request, context) {
|
|
823
|
+
return this.invoke(context, async () => {
|
|
824
|
+
this.assertWritable("mutateRows");
|
|
825
|
+
if (request.changes.length < 1 || request.changes.length > EIDOS_RUNTIME_LIMITS.mutationRowsMax) throw runtimeError("invalid-request", "changes must be within mutationRowsMax");
|
|
826
|
+
if (request.changes.reduce((total, change) => total + ("values" in change ? Object.keys(change.values).length : 0), 0) > EIDOS_RUNTIME_LIMITS.mutationCellsMax) throw runtimeError("resource-limit", "Mutation exceeds mutationCellsMax");
|
|
827
|
+
assertJcsBytes(request, EIDOS_RUNTIME_LIMITS.mutationBytesMax, "Row mutation");
|
|
828
|
+
const current = this.core.info();
|
|
829
|
+
assertCurrentRevision(request.expectedRevision, String(current.revision));
|
|
830
|
+
const clientKeys = /* @__PURE__ */ new Set();
|
|
831
|
+
const rowIds = /* @__PURE__ */ new Set();
|
|
832
|
+
for (const change of request.changes) if (change.kind === "create") {
|
|
833
|
+
if (!change.clientKey || clientKeys.has(change.clientKey)) throw runtimeError("invalid-request", "Create clientKey values must be non-empty and unique");
|
|
834
|
+
clientKeys.add(change.clientKey);
|
|
835
|
+
} else {
|
|
836
|
+
assertEidosFileUuid(change.rowId, "Row ID");
|
|
837
|
+
if (rowIds.has(change.rowId)) throw runtimeError("invalid-request", "A Row ID may occur only once per mutation");
|
|
838
|
+
rowIds.add(change.rowId);
|
|
839
|
+
}
|
|
840
|
+
if (rowIds.size > 0) {
|
|
841
|
+
const rowIdField = this.core.listFields(request.tableId).find((field) => field.systemRole === "row-id" || field.type === "row-id");
|
|
842
|
+
if (!rowIdField?.id) throw runtimeError("corrupt-file", "Table is missing Row-ID Field");
|
|
843
|
+
const existing = new Set(this.fetchRowsByIds(request.tableId, Array.from(rowIds), {
|
|
844
|
+
fields: [rowIdField.id],
|
|
845
|
+
resolveRelations: []
|
|
846
|
+
}).map((row) => row.id));
|
|
847
|
+
for (const rowId of rowIds) if (!existing.has(rowId)) throw runtimeError("not-found", "Row not found", {
|
|
848
|
+
tableId: request.tableId,
|
|
849
|
+
rowId
|
|
850
|
+
});
|
|
851
|
+
}
|
|
852
|
+
const inserts = request.changes.filter((change) => change.kind === "create").map((change) => ({ fields: this.mutationValues(request.tableId, change.values) }));
|
|
853
|
+
const updates = request.changes.filter((change) => change.kind === "update").map((change) => ({
|
|
854
|
+
id: change.rowId,
|
|
855
|
+
fields: this.mutationValues(request.tableId, change.values)
|
|
856
|
+
}));
|
|
857
|
+
const deletes = request.changes.filter((change) => change.kind === "delete").map((change) => change.rowId);
|
|
858
|
+
const beforeRevision = String(this.core.info().revision);
|
|
859
|
+
return this.withCommitBarrier("mutateRows", beforeRevision, context, async () => {
|
|
860
|
+
const result = this.core.mutateRows({
|
|
861
|
+
tableId: request.tableId,
|
|
862
|
+
insert: inserts,
|
|
863
|
+
update: updates,
|
|
864
|
+
delete: deletes,
|
|
865
|
+
expectedRevision: parseRevision(request.expectedRevision)
|
|
866
|
+
});
|
|
867
|
+
const after = this.core.info();
|
|
868
|
+
const created = request.changes.filter((change) => change.kind === "create").map((change, index) => ({
|
|
869
|
+
clientKey: change.clientKey,
|
|
870
|
+
rowId: result.rows[index].id
|
|
871
|
+
}));
|
|
872
|
+
const affectedRows = result.affected ?? [];
|
|
873
|
+
const response = {
|
|
874
|
+
fileId: after.fileId,
|
|
875
|
+
revision: String(after.revision),
|
|
876
|
+
changed: String(after.revision) !== beforeRevision,
|
|
877
|
+
created,
|
|
878
|
+
affectedRows
|
|
879
|
+
};
|
|
880
|
+
if (request.returning) response.returnedRows = await this.getRowsByIdDirect(request.tableId, request.changes.flatMap((change) => {
|
|
881
|
+
if (change.kind === "create") {
|
|
882
|
+
const allocated = created.find((entry) => entry.clientKey === change.clientKey);
|
|
883
|
+
return allocated ? [allocated.rowId] : [];
|
|
884
|
+
}
|
|
885
|
+
if (change.kind === "update") return [change.rowId];
|
|
886
|
+
return [];
|
|
887
|
+
}), request.returning);
|
|
888
|
+
return response;
|
|
889
|
+
}, (result) => ({
|
|
890
|
+
operation: "mutateRows",
|
|
891
|
+
result: {
|
|
892
|
+
fileId: result.fileId,
|
|
893
|
+
revision: result.revision,
|
|
894
|
+
changed: true,
|
|
895
|
+
created: result.created,
|
|
896
|
+
affectedRows: result.affectedRows
|
|
897
|
+
}
|
|
898
|
+
}));
|
|
899
|
+
}, false, request);
|
|
900
|
+
}
|
|
901
|
+
mutationValues(tableId, values) {
|
|
902
|
+
const fields = new Map(this.core.listFields(tableId).map((field) => [field.id, field]));
|
|
903
|
+
const normalized = {};
|
|
904
|
+
for (const [fieldId, value] of Object.entries(values)) {
|
|
905
|
+
const field = fields.get(fieldId);
|
|
906
|
+
if (!field) throw runtimeError("not-found", "Mutation Field not found", { fieldId });
|
|
907
|
+
if (!fieldWritable(field)) throw runtimeError("forbidden", "Mutation Field is read-only", { fieldId });
|
|
908
|
+
const type = fieldValueType(field);
|
|
909
|
+
assertJcsBytes(value, EIDOS_RUNTIME_LIMITS.logicalValueBytesMax, "Logical value");
|
|
910
|
+
if (value === null) {
|
|
911
|
+
if (!field.nullable) throw runtimeError("constraint", "Field is not nullable", { fieldId });
|
|
912
|
+
normalized[fieldId] = null;
|
|
913
|
+
continue;
|
|
914
|
+
}
|
|
915
|
+
if (type === "file") assertEidosFileValues(value);
|
|
916
|
+
else if (type === "file-entry") assertEidosFileValues([value]);
|
|
917
|
+
if (!logicalValueMatchesType(value, type)) throw runtimeError("invalid-value", "Mutation value type is invalid", { fieldId });
|
|
918
|
+
if (Array.isArray(value) && value.length > EIDOS_RUNTIME_LIMITS.listElementsMax) throw runtimeError("resource-limit", "List exceeds listElementsMax", { fieldId });
|
|
919
|
+
if (type === "date" && !isCanonicalEidosFileDate(value)) throw runtimeError("invalid-value", "Date is not canonical", { fieldId });
|
|
920
|
+
if (type === "datetime" && !isCanonicalEidosFileInstant(value)) throw runtimeError("invalid-value", "Datetime is not canonical", { fieldId });
|
|
921
|
+
if (type === "integer") normalized[fieldId] = parseSignedInt64(value, "Integer value");
|
|
922
|
+
else normalized[fieldId] = value;
|
|
923
|
+
}
|
|
924
|
+
return normalized;
|
|
925
|
+
}
|
|
926
|
+
importCsvRequest(request, context) {
|
|
927
|
+
const requestSummary = {
|
|
928
|
+
tableId: request.tableId,
|
|
929
|
+
expectedRevision: request.expectedRevision,
|
|
930
|
+
hasHeader: request.hasHeader,
|
|
931
|
+
columns: request.columns,
|
|
932
|
+
csvBytes: request.csv instanceof Uint8Array ? request.csv.byteLength : -1
|
|
933
|
+
};
|
|
934
|
+
return this.invoke(context, async () => {
|
|
935
|
+
this.assertWritable("importCsv");
|
|
936
|
+
if (!(request.csv instanceof Uint8Array)) throw runtimeError("invalid-request", "CSV bytes are required");
|
|
937
|
+
if (typeof request.hasHeader !== "boolean") throw runtimeError("invalid-request", "hasHeader must be boolean");
|
|
938
|
+
if (request.csv.byteLength > EIDOS_RUNTIME_LIMITS.csvBytesMax) throw runtimeError("resource-limit", "CSV exceeds csvBytesMax");
|
|
939
|
+
if (!Array.isArray(request.columns) || request.columns.length < 1) throw runtimeError("invalid-request", "CSV columns are required");
|
|
940
|
+
if (request.columns.length > EIDOS_RUNTIME_LIMITS.projectionFieldsMax) throw runtimeError("resource-limit", "CSV column mapping is too large");
|
|
941
|
+
const before = this.core.info();
|
|
942
|
+
assertCurrentRevision(request.expectedRevision, String(before.revision));
|
|
943
|
+
const table = this.core.getTable(request.tableId);
|
|
944
|
+
const fields = new Map(this.core.listFields(request.tableId).map((field) => [field.id, field]));
|
|
945
|
+
const csvIndexes = /* @__PURE__ */ new Set();
|
|
946
|
+
const fieldIds = /* @__PURE__ */ new Set();
|
|
947
|
+
const columns = request.columns.map(({ csvIndex, fieldId }) => {
|
|
948
|
+
if (!Number.isSafeInteger(csvIndex) || csvIndex < 0) throw runtimeError("invalid-request", "CSV column indexes must be non-negative integers");
|
|
949
|
+
if (csvIndexes.has(csvIndex) || fieldIds.has(fieldId)) throw runtimeError("invalid-request", "CSV column mappings must be unique");
|
|
950
|
+
csvIndexes.add(csvIndex);
|
|
951
|
+
fieldIds.add(fieldId);
|
|
952
|
+
const field = fields.get(fieldId);
|
|
953
|
+
if (!field) throw runtimeError("not-found", "CSV destination Field not found", { fieldId });
|
|
954
|
+
if (!fieldWritable(field)) throw runtimeError("forbidden", "CSV destination Field is read-only", { fieldId });
|
|
955
|
+
const type = csvImportFieldType(field);
|
|
956
|
+
if (!type) throw runtimeError("unsupported", `CSV import does not support ${field.type} Fields`, { fieldId });
|
|
957
|
+
return {
|
|
958
|
+
sourceIndex: csvIndex,
|
|
959
|
+
sourceName: field.name,
|
|
960
|
+
name: field.name,
|
|
961
|
+
columnName: field.id,
|
|
962
|
+
type
|
|
963
|
+
};
|
|
964
|
+
});
|
|
965
|
+
let content;
|
|
966
|
+
try {
|
|
967
|
+
content = new TextDecoder("utf-8", { fatal: true }).decode(request.csv);
|
|
968
|
+
} catch {
|
|
969
|
+
throw runtimeError("invalid-value", "CSV must be valid UTF-8");
|
|
970
|
+
}
|
|
971
|
+
const plan = {
|
|
972
|
+
fileName: "runtime-import.csv",
|
|
973
|
+
tableName: table.name,
|
|
974
|
+
rowCount: 0,
|
|
975
|
+
skippedRowCount: 0,
|
|
976
|
+
columns,
|
|
977
|
+
sampleRows: [],
|
|
978
|
+
issues: []
|
|
979
|
+
};
|
|
980
|
+
const rows = parseEidosFileCsvRows({
|
|
981
|
+
name: plan.fileName,
|
|
982
|
+
content
|
|
983
|
+
}, plan, { hasHeader: request.hasHeader });
|
|
984
|
+
if (rows.length === 0) return {
|
|
985
|
+
fileId: before.fileId,
|
|
986
|
+
tableId: request.tableId,
|
|
987
|
+
revision: String(before.revision),
|
|
988
|
+
changed: false,
|
|
989
|
+
createdRows: []
|
|
990
|
+
};
|
|
991
|
+
const beforeRevision = String(before.revision);
|
|
992
|
+
const importedRows = rows.map((row) => {
|
|
993
|
+
const rowId = this.generator.next();
|
|
994
|
+
return {
|
|
995
|
+
rowId,
|
|
996
|
+
row: {
|
|
997
|
+
...row,
|
|
998
|
+
_id: rowId
|
|
999
|
+
}
|
|
1000
|
+
};
|
|
1001
|
+
});
|
|
1002
|
+
const createdRows = importedRows.map(({ rowId }, index) => ({
|
|
1003
|
+
recordIndex: index + (request.hasHeader ? 1 : 0),
|
|
1004
|
+
rowId
|
|
1005
|
+
}));
|
|
1006
|
+
assertJcsBytes({
|
|
1007
|
+
fileId: before.fileId,
|
|
1008
|
+
tableId: request.tableId,
|
|
1009
|
+
revision: "-9223372036854775808",
|
|
1010
|
+
changed: true,
|
|
1011
|
+
createdRows
|
|
1012
|
+
}, EIDOS_RUNTIME_LIMITS.responseBytesMax, "CSV import result");
|
|
1013
|
+
return this.withCommitBarrier("importCsv", beforeRevision, context, () => {
|
|
1014
|
+
this.core.applyCanonicalMutation(() => this.core.appendImportedRows(request.tableId, importedRows.map((entry) => entry.row)), parseRevision(request.expectedRevision));
|
|
1015
|
+
const after = this.core.info();
|
|
1016
|
+
return {
|
|
1017
|
+
fileId: after.fileId,
|
|
1018
|
+
tableId: request.tableId,
|
|
1019
|
+
revision: String(after.revision),
|
|
1020
|
+
changed: String(after.revision) !== beforeRevision,
|
|
1021
|
+
createdRows
|
|
1022
|
+
};
|
|
1023
|
+
}, (result) => ({
|
|
1024
|
+
operation: "importCsv",
|
|
1025
|
+
result: {
|
|
1026
|
+
fileId: result.fileId,
|
|
1027
|
+
tableId: result.tableId,
|
|
1028
|
+
revision: result.revision,
|
|
1029
|
+
changed: true,
|
|
1030
|
+
createdRows: result.createdRows
|
|
1031
|
+
}
|
|
1032
|
+
}));
|
|
1033
|
+
}, false, requestSummary);
|
|
1034
|
+
}
|
|
1035
|
+
async mutateView(request, context) {
|
|
1036
|
+
return this.invoke(context, () => {
|
|
1037
|
+
this.assertWritable("mutateView");
|
|
1038
|
+
if (request.changes.length < 1) throw runtimeError("invalid-request", "View mutation changes are required");
|
|
1039
|
+
const before = this.core.info();
|
|
1040
|
+
assertCurrentRevision(request.expectedRevision, String(before.revision));
|
|
1041
|
+
const clientKeys = /* @__PURE__ */ new Set();
|
|
1042
|
+
const viewIds = /* @__PURE__ */ new Set();
|
|
1043
|
+
const createdViews = [];
|
|
1044
|
+
const affected = /* @__PURE__ */ new Set();
|
|
1045
|
+
for (const change of request.changes) if (change.kind === "create-view") {
|
|
1046
|
+
if (!change.clientKey || clientKeys.has(change.clientKey)) throw runtimeError("invalid-request", "View clientKey values must be non-empty and unique");
|
|
1047
|
+
clientKeys.add(change.clientKey);
|
|
1048
|
+
} else {
|
|
1049
|
+
if (viewIds.has(change.viewId)) throw runtimeError("invalid-request", "A View ID may occur only once per request");
|
|
1050
|
+
viewIds.add(change.viewId);
|
|
1051
|
+
}
|
|
1052
|
+
return this.withCommitBarrier("mutateView", String(before.revision), context, () => {
|
|
1053
|
+
this.core.applyCanonicalMutation(() => {
|
|
1054
|
+
for (const change of request.changes) {
|
|
1055
|
+
if (change.kind === "create-view") {
|
|
1056
|
+
const view = this.core.createView(change.tableId, {
|
|
1057
|
+
name: assertEidosFileDisplayName(change.name, "View name"),
|
|
1058
|
+
type: change.type,
|
|
1059
|
+
position: int64ToSafeNumber(change.position, "position"),
|
|
1060
|
+
properties: objectValue(change.layout),
|
|
1061
|
+
filter: change.query.filter ? runtimeFilterToCompatibility(change.query.filter) : null,
|
|
1062
|
+
sorts: (change.query.sort ?? []).map((sort) => ({
|
|
1063
|
+
field: sort.fieldId,
|
|
1064
|
+
direction: sort.direction,
|
|
1065
|
+
nulls: sort.nulls ?? "last"
|
|
1066
|
+
}))
|
|
1067
|
+
});
|
|
1068
|
+
createdViews.push({
|
|
1069
|
+
clientKey: change.clientKey,
|
|
1070
|
+
viewId: view.id
|
|
1071
|
+
});
|
|
1072
|
+
affected.add(view.id);
|
|
1073
|
+
continue;
|
|
1074
|
+
}
|
|
1075
|
+
if (!this.allViews().find((view) => view.id === change.viewId)) throw runtimeError("not-found", "View not found");
|
|
1076
|
+
if (change.kind === "delete-view") {
|
|
1077
|
+
this.core.deleteView(change.viewId);
|
|
1078
|
+
affected.add(change.viewId);
|
|
1079
|
+
continue;
|
|
1080
|
+
}
|
|
1081
|
+
const layout = change.patch.layout === void 0 ? void 0 : objectValue(change.patch.layout);
|
|
1082
|
+
const fieldOrder = Array.isArray(layout?.fieldOrder) ? layout.fieldOrder.filter((fieldId) => typeof fieldId === "string") : null;
|
|
1083
|
+
const hiddenFields = Array.isArray(layout?.hiddenFields) ? layout.hiddenFields.filter((fieldId) => typeof fieldId === "string") : [];
|
|
1084
|
+
this.core.updateView(change.viewId, {
|
|
1085
|
+
...change.patch.name === void 0 ? {} : { name: change.patch.name },
|
|
1086
|
+
...change.patch.type === void 0 ? {} : { type: change.patch.type },
|
|
1087
|
+
...change.patch.position === void 0 ? {} : { position: int64ToSafeNumber(change.patch.position, "position") },
|
|
1088
|
+
...layout === void 0 ? {} : {
|
|
1089
|
+
properties: layout,
|
|
1090
|
+
orderMap: fieldOrder ? Object.fromEntries(fieldOrder.map((fieldId, index) => [fieldId, index])) : null,
|
|
1091
|
+
hiddenFields
|
|
1092
|
+
},
|
|
1093
|
+
...change.patch.query === void 0 ? {} : {
|
|
1094
|
+
filter: change.patch.query.filter ? runtimeFilterToCompatibility(change.patch.query.filter) : null,
|
|
1095
|
+
sorts: (change.patch.query.sort ?? []).map((sort) => ({
|
|
1096
|
+
field: sort.fieldId,
|
|
1097
|
+
direction: sort.direction,
|
|
1098
|
+
nulls: sort.nulls ?? "last"
|
|
1099
|
+
}))
|
|
1100
|
+
}
|
|
1101
|
+
});
|
|
1102
|
+
affected.add(change.viewId);
|
|
1103
|
+
}
|
|
1104
|
+
}, parseRevision(request.expectedRevision));
|
|
1105
|
+
const after = this.core.info();
|
|
1106
|
+
return {
|
|
1107
|
+
fileId: after.fileId,
|
|
1108
|
+
revision: String(after.revision),
|
|
1109
|
+
changed: String(after.revision) !== String(before.revision),
|
|
1110
|
+
createdViews,
|
|
1111
|
+
affectedViewIds: Array.from(affected).sort(binaryCompare)
|
|
1112
|
+
};
|
|
1113
|
+
}, (result) => ({
|
|
1114
|
+
operation: "mutateView",
|
|
1115
|
+
result: {
|
|
1116
|
+
fileId: result.fileId,
|
|
1117
|
+
revision: result.revision,
|
|
1118
|
+
changed: true,
|
|
1119
|
+
createdViews: result.createdViews,
|
|
1120
|
+
affectedViewIds: result.affectedViewIds
|
|
1121
|
+
}
|
|
1122
|
+
}));
|
|
1123
|
+
}, false, request);
|
|
1124
|
+
}
|
|
1125
|
+
async preflightSchema(request, context) {
|
|
1126
|
+
return this.invoke(context, async () => {
|
|
1127
|
+
this.assertWritable("preflightSchema");
|
|
1128
|
+
const metadata = this.core.info();
|
|
1129
|
+
assertCurrentRevision(request.expectedRevision, String(metadata.revision));
|
|
1130
|
+
const analysis = this.analyzeSchemaChange(request.change);
|
|
1131
|
+
const actionsHash = await hashJson(request.change);
|
|
1132
|
+
const token = this.generator.next();
|
|
1133
|
+
const expiresInMilliseconds = 6e5;
|
|
1134
|
+
const wall = checkedNowInstant(this.environment);
|
|
1135
|
+
const expiresAt = new Date(Date.parse(wall) + expiresInMilliseconds).toISOString();
|
|
1136
|
+
const plan = {
|
|
1137
|
+
token,
|
|
1138
|
+
change: request.change,
|
|
1139
|
+
actionsHash,
|
|
1140
|
+
baseRevision: request.expectedRevision,
|
|
1141
|
+
classification: analysis.classification,
|
|
1142
|
+
dependencies: analysis.dependencies,
|
|
1143
|
+
warnings: analysis.warnings,
|
|
1144
|
+
affectedRows: analysis.affectedRows,
|
|
1145
|
+
valueChanges: analysis.valueChanges,
|
|
1146
|
+
createdAtSequence: ++this.schemaPlanSequence,
|
|
1147
|
+
expiresAtMonotonic: this.environment.clock.nowMilliseconds() + expiresInMilliseconds,
|
|
1148
|
+
expiresAt
|
|
1149
|
+
};
|
|
1150
|
+
const retainedBytes = jcsByteLength(plan);
|
|
1151
|
+
if (retainedBytes > EIDOS_RUNTIME_LIMITS.schemaPlanBytesMax) throw runtimeError("resource-limit", "Schema plan exceeds schemaPlanBytesMax");
|
|
1152
|
+
const dependencies = plan.dependencies.slice(0, EIDOS_RUNTIME_LIMITS.schemaPageSizeMax);
|
|
1153
|
+
const responseBase = {
|
|
1154
|
+
fileId: metadata.fileId,
|
|
1155
|
+
planToken: token,
|
|
1156
|
+
baseRevision: request.expectedRevision,
|
|
1157
|
+
actionsHash,
|
|
1158
|
+
classification: plan.classification,
|
|
1159
|
+
affectedRows: plan.affectedRows,
|
|
1160
|
+
dependencyCount: String(plan.dependencies.length),
|
|
1161
|
+
warnings: plan.warnings.slice(0, EIDOS_RUNTIME_LIMITS.diagnosticsMax),
|
|
1162
|
+
warningsTruncated: plan.warnings.length > EIDOS_RUNTIME_LIMITS.diagnosticsMax,
|
|
1163
|
+
valueChanges: plan.valueChanges.slice(0, EIDOS_RUNTIME_LIMITS.diagnosticsMax),
|
|
1164
|
+
valueChangesTruncated: plan.valueChanges.length > EIDOS_RUNTIME_LIMITS.diagnosticsMax,
|
|
1165
|
+
expiresInMilliseconds,
|
|
1166
|
+
expiresAt
|
|
1167
|
+
};
|
|
1168
|
+
const responseFor = (prefix) => ({
|
|
1169
|
+
...responseBase,
|
|
1170
|
+
dependencies: prefix,
|
|
1171
|
+
...prefix.length < plan.dependencies.length ? { dependencyCursor: this.encodeCursor("schema-dependencies", {
|
|
1172
|
+
planToken: token,
|
|
1173
|
+
offset: prefix.length
|
|
1174
|
+
}) } : {}
|
|
1175
|
+
});
|
|
1176
|
+
while (dependencies.length > 0 && jcsByteLength(responseFor(dependencies)) > EIDOS_RUNTIME_LIMITS.responseBytesMax) dependencies.pop();
|
|
1177
|
+
if (jcsByteLength(responseFor(dependencies)) > EIDOS_RUNTIME_LIMITS.responseBytesMax || plan.dependencies.length > 0 && dependencies.length === 0) throw runtimeError("resource-limit", "Schema preflight result cannot fit responseBytesMax");
|
|
1178
|
+
this.evictSchemaPlans(retainedBytes);
|
|
1179
|
+
this.schemaPlans.set(token, plan);
|
|
1180
|
+
return responseFor(dependencies);
|
|
1181
|
+
}, false, request);
|
|
1182
|
+
}
|
|
1183
|
+
async getSchemaPlanDependencies(request, context) {
|
|
1184
|
+
return this.invoke(context, () => {
|
|
1185
|
+
assertLimit(request.limit, EIDOS_RUNTIME_LIMITS.schemaPageSizeMax, "limit");
|
|
1186
|
+
const plan = this.retainedSchemaPlan(request.planToken);
|
|
1187
|
+
let offset = 0;
|
|
1188
|
+
if (request.cursor !== void 0) {
|
|
1189
|
+
const cursor = this.decodeCursor(request.cursor, "schema-dependencies");
|
|
1190
|
+
if (cursor.planToken !== request.planToken || typeof cursor.offset !== "number" || !Number.isSafeInteger(cursor.offset) || cursor.offset < 0) throw runtimeError("invalid-plan", "Invalid dependency cursor");
|
|
1191
|
+
offset = cursor.offset;
|
|
1192
|
+
}
|
|
1193
|
+
const dependencies = plan.dependencies.slice(offset, offset + request.limit);
|
|
1194
|
+
if (offset > plan.dependencies.length) throw runtimeError("invalid-plan", "Dependency cursor is past the end");
|
|
1195
|
+
const responseFor = (prefix) => {
|
|
1196
|
+
const nextOffset = offset + prefix.length;
|
|
1197
|
+
return {
|
|
1198
|
+
fileId: this.core.info().fileId,
|
|
1199
|
+
revision: plan.baseRevision,
|
|
1200
|
+
dependencyCount: String(plan.dependencies.length),
|
|
1201
|
+
dependencies: prefix,
|
|
1202
|
+
nextCursor: nextOffset < plan.dependencies.length ? this.encodeCursor("schema-dependencies", {
|
|
1203
|
+
planToken: plan.token,
|
|
1204
|
+
offset: nextOffset
|
|
1205
|
+
}) : null
|
|
1206
|
+
};
|
|
1207
|
+
};
|
|
1208
|
+
while (dependencies.length > 0 && jcsByteLength(responseFor(dependencies)) > EIDOS_RUNTIME_LIMITS.responseBytesMax) dependencies.pop();
|
|
1209
|
+
if (jcsByteLength(responseFor(dependencies)) > EIDOS_RUNTIME_LIMITS.responseBytesMax || offset < plan.dependencies.length && dependencies.length === 0) throw runtimeError("resource-limit", "Schema dependency cannot fit responseBytesMax");
|
|
1210
|
+
return responseFor(dependencies);
|
|
1211
|
+
}, false, request);
|
|
1212
|
+
}
|
|
1213
|
+
async mutateSchema(request, context) {
|
|
1214
|
+
return this.invoke(context, () => {
|
|
1215
|
+
this.assertWritable("mutateSchema");
|
|
1216
|
+
if (!request.planToken || !/^[0-9a-f-]{36}$/.test(request.planToken)) throw runtimeError("invalid-plan", "Invalid schema plan token");
|
|
1217
|
+
const plan = this.retainedSchemaPlan(request.planToken);
|
|
1218
|
+
if (request.actionsHash !== plan.actionsHash || request.expectedRevision !== plan.baseRevision) throw runtimeError("invalid-plan", "Schema plan binding mismatch");
|
|
1219
|
+
const before = this.core.info();
|
|
1220
|
+
assertCurrentRevision(plan.baseRevision, String(before.revision));
|
|
1221
|
+
if (plan.classification === "forbidden") throw runtimeError("forbidden", "Schema plan is forbidden");
|
|
1222
|
+
if (plan.classification === "explicit-lossy" && request.confirmLossy !== true) throw runtimeError("lossy-confirmation-required", "Schema plan requires confirmLossy:true");
|
|
1223
|
+
this.schemaPlans.delete(plan.token);
|
|
1224
|
+
const createdObjects = [];
|
|
1225
|
+
const affectedTableIds = /* @__PURE__ */ new Set();
|
|
1226
|
+
const affectedFieldIds = /* @__PURE__ */ new Set();
|
|
1227
|
+
return this.withCommitBarrier("mutateSchema", String(before.revision), context, () => {
|
|
1228
|
+
this.core.applyCanonicalMutation(() => {
|
|
1229
|
+
this.applySchemaChange(plan.change, createdObjects, affectedTableIds, affectedFieldIds);
|
|
1230
|
+
}, parseRevision(request.expectedRevision));
|
|
1231
|
+
const after = this.core.info();
|
|
1232
|
+
return {
|
|
1233
|
+
fileId: after.fileId,
|
|
1234
|
+
revision: String(after.revision),
|
|
1235
|
+
changed: String(after.revision) !== String(before.revision),
|
|
1236
|
+
createdObjects,
|
|
1237
|
+
affectedTableIds: Array.from(affectedTableIds).sort(binaryCompare),
|
|
1238
|
+
affectedFieldIds: Array.from(affectedFieldIds).sort(binaryCompare)
|
|
1239
|
+
};
|
|
1240
|
+
}, (result) => ({
|
|
1241
|
+
operation: "mutateSchema",
|
|
1242
|
+
result: {
|
|
1243
|
+
fileId: result.fileId,
|
|
1244
|
+
revision: result.revision,
|
|
1245
|
+
changed: true,
|
|
1246
|
+
createdObjects: result.createdObjects,
|
|
1247
|
+
affectedTableIds: result.affectedTableIds,
|
|
1248
|
+
affectedFieldIds: result.affectedFieldIds
|
|
1249
|
+
}
|
|
1250
|
+
}));
|
|
1251
|
+
}, false, request);
|
|
1252
|
+
}
|
|
1253
|
+
validate(request, context) {
|
|
1254
|
+
return this.invoke(context, () => this.read(() => {
|
|
1255
|
+
assertLimit(request.diagnosticsLimit, EIDOS_RUNTIME_LIMITS.diagnosticsMax, "diagnosticsLimit");
|
|
1256
|
+
const result = this.core.validate({ level: request.level });
|
|
1257
|
+
const metadata = result.metadata;
|
|
1258
|
+
const diagnostics = [...result.errors.map((issue) => validationDiagnostic(issue, "error")), ...result.warnings.map((issue) => validationDiagnostic(issue, "warning"))];
|
|
1259
|
+
const retained = diagnostics.slice(0, request.diagnosticsLimit);
|
|
1260
|
+
return {
|
|
1261
|
+
...metadata ? {
|
|
1262
|
+
fileId: metadata.fileId,
|
|
1263
|
+
revision: String(metadata.revision)
|
|
1264
|
+
} : {},
|
|
1265
|
+
level: request.level,
|
|
1266
|
+
valid: result.valid,
|
|
1267
|
+
diagnostics: retained,
|
|
1268
|
+
truncated: retained.length < diagnostics.length
|
|
1269
|
+
};
|
|
1270
|
+
}), false, request);
|
|
1271
|
+
}
|
|
1272
|
+
async cancel(request) {
|
|
1273
|
+
const control = this.requestControls.get(request.requestId);
|
|
1274
|
+
if (!control) return;
|
|
1275
|
+
control.cancelled = true;
|
|
1276
|
+
if (this.connection.capabilities().interrupt) try {
|
|
1277
|
+
this.connection.interrupt();
|
|
1278
|
+
} catch {}
|
|
1279
|
+
}
|
|
1280
|
+
close(context) {
|
|
1281
|
+
if (this.state === "closed") return Promise.resolve();
|
|
1282
|
+
try {
|
|
1283
|
+
assertRequestContext(context);
|
|
1284
|
+
} catch (error) {
|
|
1285
|
+
return Promise.reject(mapRuntimeFailure(error));
|
|
1286
|
+
}
|
|
1287
|
+
if (context.signal?.aborted) return Promise.reject(runtimeError("cancelled", "Close was cancelled"));
|
|
1288
|
+
if (this.state === "open") this.state = "closing";
|
|
1289
|
+
return this.invoke(context, () => {
|
|
1290
|
+
this.schemaPlans.clear();
|
|
1291
|
+
this.state = "closed";
|
|
1292
|
+
}, true);
|
|
1293
|
+
}
|
|
1294
|
+
snapshot() {
|
|
1295
|
+
const metadata = this.core.info();
|
|
1296
|
+
const tables = this.core.listTables();
|
|
1297
|
+
const fields = tables.flatMap((table) => this.core.listFields(table.id));
|
|
1298
|
+
const views = tables.flatMap((table) => this.core.listViews(table.id));
|
|
1299
|
+
return {
|
|
1300
|
+
fileId: metadata.fileId,
|
|
1301
|
+
format: {
|
|
1302
|
+
major: 1,
|
|
1303
|
+
minor: 0
|
|
1304
|
+
},
|
|
1305
|
+
revision: String(metadata.revision),
|
|
1306
|
+
title: metadata.title ?? "Untitled",
|
|
1307
|
+
defaultTableId: metadata.defaultTableId ?? null,
|
|
1308
|
+
schemaCounts: {
|
|
1309
|
+
tables: String(tables.length),
|
|
1310
|
+
fields: String(fields.length),
|
|
1311
|
+
views: String(views.length),
|
|
1312
|
+
features: "0"
|
|
1313
|
+
}
|
|
1314
|
+
};
|
|
1315
|
+
}
|
|
1316
|
+
schemaDescriptors() {
|
|
1317
|
+
const tables = this.core.listTables();
|
|
1318
|
+
const tableDescriptors = tables.map((table) => {
|
|
1319
|
+
const label = this.core.listFields(table.id).find((field) => field.isRecordLabel);
|
|
1320
|
+
if (!label?.id) throw runtimeError("corrupt-file", "Table has no Record Label Field", { tableId: table.id });
|
|
1321
|
+
return {
|
|
1322
|
+
object: "table",
|
|
1323
|
+
id: table.id,
|
|
1324
|
+
name: table.name,
|
|
1325
|
+
labelFieldId: label.id,
|
|
1326
|
+
position: String(table.position ?? 0),
|
|
1327
|
+
settings: objectValue({
|
|
1328
|
+
...table.icon === null ? {} : { icon: table.icon },
|
|
1329
|
+
...table.description === null ? {} : { description: table.description }
|
|
1330
|
+
})
|
|
1331
|
+
};
|
|
1332
|
+
});
|
|
1333
|
+
const fieldDescriptors = tables.flatMap((table) => this.core.listFields(table.id).map((field) => fieldDescriptor(field)));
|
|
1334
|
+
const views = tables.flatMap((table) => this.core.listViews(table.id).map((view) => ({
|
|
1335
|
+
object: "view",
|
|
1336
|
+
id: view.id,
|
|
1337
|
+
tableId: view.tableId,
|
|
1338
|
+
name: view.name,
|
|
1339
|
+
type: view.type,
|
|
1340
|
+
query: {
|
|
1341
|
+
...view.filter ? { filter: compatibilityFilterToRuntime(view.filter, this.core.listFields(view.tableId)) } : {},
|
|
1342
|
+
...view.sorts.length > 0 ? { sort: view.sorts.map((sort) => ({
|
|
1343
|
+
fieldId: sort.field,
|
|
1344
|
+
direction: sort.direction,
|
|
1345
|
+
...sort.nulls ? { nulls: sort.nulls } : {}
|
|
1346
|
+
})) } : {}
|
|
1347
|
+
},
|
|
1348
|
+
layout: objectValue(view.properties ?? {}),
|
|
1349
|
+
position: String(view.position ?? 0)
|
|
1350
|
+
})));
|
|
1351
|
+
return [
|
|
1352
|
+
...tableDescriptors,
|
|
1353
|
+
...fieldDescriptors,
|
|
1354
|
+
...views
|
|
1355
|
+
];
|
|
1356
|
+
}
|
|
1357
|
+
columns(tableId, projection) {
|
|
1358
|
+
const fields = this.core.listFields(tableId);
|
|
1359
|
+
return projection.fields.map((fieldId) => {
|
|
1360
|
+
const field = fields.find((entry) => entry.id === fieldId);
|
|
1361
|
+
if (!field) throw runtimeError("not-found", "Projection Field not found", {
|
|
1362
|
+
tableId,
|
|
1363
|
+
fieldId
|
|
1364
|
+
});
|
|
1365
|
+
const relation = relationDefinition(field);
|
|
1366
|
+
return {
|
|
1367
|
+
fieldId,
|
|
1368
|
+
name: field.name,
|
|
1369
|
+
valueType: fieldValueType(field),
|
|
1370
|
+
source: field.type === "formula" ? "formula" : field.type === "lookup" ? "lookup" : field.type === "relation" && relation?.direction === "inverse" ? "inverse-relation" : "stored",
|
|
1371
|
+
writable: fieldWritable(field)
|
|
1372
|
+
};
|
|
1373
|
+
});
|
|
1374
|
+
}
|
|
1375
|
+
projectRows(tableId, projection, rows, columns) {
|
|
1376
|
+
const fields = this.core.listFields(tableId);
|
|
1377
|
+
return rows.map((row) => {
|
|
1378
|
+
const projected = {
|
|
1379
|
+
id: row.id,
|
|
1380
|
+
values: projection.fields.map((fieldId, index) => compatibilityLogicalValue(row.fields[fieldId] ?? null, columns[index].valueType))
|
|
1381
|
+
};
|
|
1382
|
+
const resolvedRelations = projection.resolveRelations.flatMap((fieldId) => {
|
|
1383
|
+
const column = projection.fields.indexOf(fieldId);
|
|
1384
|
+
if (column < 0) return [];
|
|
1385
|
+
const field = fields.find((entry) => entry.id === fieldId);
|
|
1386
|
+
const definition = field ? relationDefinition(field) : void 0;
|
|
1387
|
+
if (!field || !definition) return [];
|
|
1388
|
+
const labelField = this.core.listFields(definition.targetTableId).find((entry) => entry.isRecordLabel);
|
|
1389
|
+
if (!labelField?.id) return [];
|
|
1390
|
+
return [{
|
|
1391
|
+
column,
|
|
1392
|
+
items: (row.resolved?.[fieldId] ?? []).map((item) => ({
|
|
1393
|
+
id: item.id,
|
|
1394
|
+
state: "resolved",
|
|
1395
|
+
labelFieldId: labelField.id,
|
|
1396
|
+
labelType: fieldValueType(labelField),
|
|
1397
|
+
label: item.label
|
|
1398
|
+
}))
|
|
1399
|
+
}];
|
|
1400
|
+
});
|
|
1401
|
+
if (resolvedRelations.length > 0) projected.resolvedRelations = resolvedRelations;
|
|
1402
|
+
if (projected.values.length !== columns.length) throw runtimeError("fatal", "Columnar projection length mismatch");
|
|
1403
|
+
return projected;
|
|
1404
|
+
});
|
|
1405
|
+
}
|
|
1406
|
+
aggregateNow(request) {
|
|
1407
|
+
if (request.items.length < 1 || request.items.length > EIDOS_RUNTIME_LIMITS.aggregateItemsMax) throw runtimeError("invalid-request", "Aggregate items exceed aggregateItemsMax");
|
|
1408
|
+
const keys = /* @__PURE__ */ new Set();
|
|
1409
|
+
for (const item of request.items) {
|
|
1410
|
+
if (!item.key || keys.has(item.key)) throw runtimeError("invalid-request", "Aggregate keys must be non-empty and unique");
|
|
1411
|
+
keys.add(item.key);
|
|
1412
|
+
}
|
|
1413
|
+
const query = this.compatibilityQuery(request.tableId, request.query ?? {});
|
|
1414
|
+
if (request.items.every((item) => item.op === "count-all")) {
|
|
1415
|
+
const metadata = this.core.info();
|
|
1416
|
+
const count = String(this.core.countRows(request.tableId, query));
|
|
1417
|
+
return {
|
|
1418
|
+
fileId: metadata.fileId,
|
|
1419
|
+
tableId: request.tableId,
|
|
1420
|
+
revision: String(metadata.revision),
|
|
1421
|
+
results: request.items.map((item) => ({
|
|
1422
|
+
key: item.key,
|
|
1423
|
+
value: count
|
|
1424
|
+
}))
|
|
1425
|
+
};
|
|
1426
|
+
}
|
|
1427
|
+
const fields = this.core.listFields(request.tableId);
|
|
1428
|
+
const fieldsById = new Map(fields.map((field) => [field.id, field]));
|
|
1429
|
+
if (!(request.query !== void 0 && Object.keys(request.query).length > 0) && request.items.every((item) => item.op === "count-all" || item.op === "distinct-count" && fieldsById.get(item.fieldId)?.physicalName || item.op === "distinct-values" && fieldsById.get(item.fieldId)?.physicalName && [
|
|
1430
|
+
"text",
|
|
1431
|
+
"select",
|
|
1432
|
+
"url"
|
|
1433
|
+
].includes(fieldsById.get(item.fieldId).type))) {
|
|
1434
|
+
const metadata = this.core.info();
|
|
1435
|
+
const table = this.core.getTable(request.tableId);
|
|
1436
|
+
const tableSql = quoteSqlIdentifier(table.physicalName ?? table.rawTableName);
|
|
1437
|
+
return {
|
|
1438
|
+
fileId: metadata.fileId,
|
|
1439
|
+
tableId: request.tableId,
|
|
1440
|
+
revision: String(metadata.revision),
|
|
1441
|
+
results: request.items.map((item) => {
|
|
1442
|
+
if (item.op === "count-all") return {
|
|
1443
|
+
key: item.key,
|
|
1444
|
+
value: String(this.core.countRows(request.tableId))
|
|
1445
|
+
};
|
|
1446
|
+
const field = fieldsById.get(item.fieldId);
|
|
1447
|
+
if (item.op === "distinct-values") {
|
|
1448
|
+
const rows = this.core.connection.query(`SELECT DISTINCT ${quoteSqlIdentifier(field.physicalName)} AS value
|
|
1449
|
+
FROM ${tableSql}
|
|
1450
|
+
WHERE ${quoteSqlIdentifier(field.physicalName)} IS NOT NULL
|
|
1451
|
+
ORDER BY ${quoteSqlIdentifier(field.physicalName)} COLLATE BINARY
|
|
1452
|
+
LIMIT ?`, [item.limit + 1]);
|
|
1453
|
+
return {
|
|
1454
|
+
key: item.key,
|
|
1455
|
+
values: rows.slice(0, item.limit).map((row) => String(row.value)),
|
|
1456
|
+
truncated: rows.length > item.limit
|
|
1457
|
+
};
|
|
1458
|
+
}
|
|
1459
|
+
const count = this.core.connection.get(`SELECT count(DISTINCT ${quoteSqlIdentifier(field.physicalName)}) AS count FROM ${tableSql}`)?.count;
|
|
1460
|
+
return {
|
|
1461
|
+
key: item.key,
|
|
1462
|
+
value: String(count ?? 0)
|
|
1463
|
+
};
|
|
1464
|
+
})
|
|
1465
|
+
};
|
|
1466
|
+
}
|
|
1467
|
+
const fieldIds = Array.from(new Set([...request.items.flatMap((item) => item.op === "count-all" ? [] : [item.fieldId]), ...runtimeQueryFieldIds(request.query)]));
|
|
1468
|
+
for (const fieldId of fieldIds) if (!fieldsById.has(fieldId)) throw runtimeError("not-found", "Aggregate Field not found", { fieldId });
|
|
1469
|
+
const rows = this.core.runtimeScanLogicalRows(request.tableId, fieldIds, query).sort((left, right) => binaryCompare(left.id, right.id));
|
|
1470
|
+
const results = request.items.map((item) => {
|
|
1471
|
+
if (item.op === "count-all") return {
|
|
1472
|
+
key: item.key,
|
|
1473
|
+
value: String(rows.length)
|
|
1474
|
+
};
|
|
1475
|
+
return aggregateItemResult(item, fieldValueType(fieldsById.get(item.fieldId)), rows.map((row) => row.fields[item.fieldId] ?? null));
|
|
1476
|
+
});
|
|
1477
|
+
const metadata = this.core.info();
|
|
1478
|
+
return {
|
|
1479
|
+
fileId: metadata.fileId,
|
|
1480
|
+
tableId: request.tableId,
|
|
1481
|
+
revision: String(metadata.revision),
|
|
1482
|
+
results
|
|
1483
|
+
};
|
|
1484
|
+
}
|
|
1485
|
+
compatibilityQuery(tableId, query) {
|
|
1486
|
+
assertRuntimeRowQuery(this.core.listFields(tableId), query);
|
|
1487
|
+
return runtimeQueryToCompatibility(query);
|
|
1488
|
+
}
|
|
1489
|
+
async groupRowsNow(request) {
|
|
1490
|
+
assertLimit(request.groupLimit, EIDOS_RUNTIME_LIMITS.groupPageSizeMax, "groupLimit");
|
|
1491
|
+
assertLimit(request.rowsPerGroup, EIDOS_RUNTIME_LIMITS.pageSizeMax, "rowsPerGroup");
|
|
1492
|
+
if (request.groupBy.length < 1 || request.groupBy.length > EIDOS_RUNTIME_LIMITS.groupFieldsMax || new Set(request.groupBy).size !== request.groupBy.length) throw runtimeError("invalid-query", "groupBy is invalid");
|
|
1493
|
+
assertProjection(request.projection);
|
|
1494
|
+
if (request.aggregates.length > EIDOS_RUNTIME_LIMITS.aggregateItemsMax) throw runtimeError("invalid-request", "Group aggregates exceed aggregateItemsMax");
|
|
1495
|
+
const fields = this.core.listFields(request.tableId);
|
|
1496
|
+
const fieldsById = new Map(fields.map((field) => [field.id, field]));
|
|
1497
|
+
const groupTypes = request.groupBy.map((fieldId) => {
|
|
1498
|
+
const field = fieldsById.get(fieldId);
|
|
1499
|
+
const type = field ? fieldValueType(field) : void 0;
|
|
1500
|
+
if (!field || typeof type !== "string" || !isSortableType(type)) throw runtimeError("invalid-query", "Group Field is not groupable", { fieldId });
|
|
1501
|
+
return type;
|
|
1502
|
+
});
|
|
1503
|
+
assertAggregateItems(request.aggregates, fieldsById);
|
|
1504
|
+
const required = Array.from(new Set([
|
|
1505
|
+
...request.projection.fields,
|
|
1506
|
+
...request.groupBy,
|
|
1507
|
+
...request.aggregates.flatMap((item) => item.op === "count-all" ? [] : [item.fieldId]),
|
|
1508
|
+
...runtimeQueryFieldIds(request.query)
|
|
1509
|
+
]));
|
|
1510
|
+
const sourceRows = this.core.runtimeScanLogicalRows(request.tableId, required, this.compatibilityQuery(request.tableId, request.query));
|
|
1511
|
+
const grouped = /* @__PURE__ */ new Map();
|
|
1512
|
+
for (const row of sourceRows) {
|
|
1513
|
+
const rawKey = request.groupBy.map((fieldId) => row.fields[fieldId] ?? null);
|
|
1514
|
+
const identity = groupIdentity(rawKey, groupTypes);
|
|
1515
|
+
const existing = grouped.get(identity);
|
|
1516
|
+
if (existing) existing.rows.push(row);
|
|
1517
|
+
else grouped.set(identity, {
|
|
1518
|
+
identity,
|
|
1519
|
+
rawKey,
|
|
1520
|
+
key: rawKey.map((value, index) => value === null ? null : publicAggregateValue(value, groupTypes[index])),
|
|
1521
|
+
rows: [row]
|
|
1522
|
+
});
|
|
1523
|
+
}
|
|
1524
|
+
const ordered = Array.from(grouped.values()).sort((left, right) => compareGroupKeys(left.rawKey, right.rawKey, groupTypes));
|
|
1525
|
+
const metadata = this.core.info();
|
|
1526
|
+
const projectionDigest = await projectionHash(request.projection);
|
|
1527
|
+
const bindingDigest = await groupBindingHash(request);
|
|
1528
|
+
const direction = request.direction ?? "forward";
|
|
1529
|
+
let boundary = direction === "forward" ? -1 : ordered.length;
|
|
1530
|
+
if (request.cursor !== void 0) {
|
|
1531
|
+
const cursor = this.decodeCursor(request.cursor, "groups");
|
|
1532
|
+
if (cursor.fileId !== metadata.fileId || cursor.tableId !== request.tableId || cursor.bindingHash !== bindingDigest || cursor.projectionHash !== projectionDigest || typeof cursor.groupIdentity !== "string") throw runtimeError("invalid-query", "Group cursor binding mismatch");
|
|
1533
|
+
if (cursor.revision !== String(metadata.revision)) throw runtimeError("stale-revision", "Group cursor revision is stale", { currentRevision: String(metadata.revision) });
|
|
1534
|
+
boundary = ordered.findIndex((entry) => entry.identity === cursor.groupIdentity);
|
|
1535
|
+
if (boundary < 0) throw runtimeError("invalid-query", "Group cursor boundary is invalid");
|
|
1536
|
+
}
|
|
1537
|
+
const start = direction === "forward" ? boundary + 1 : Math.max(0, boundary - request.groupLimit);
|
|
1538
|
+
const end = direction === "forward" ? Math.min(ordered.length, start + request.groupLimit) : boundary;
|
|
1539
|
+
const selected = ordered.slice(start, end);
|
|
1540
|
+
const visibleRowIds = selected.flatMap((group) => group.rows.slice(0, request.rowsPerGroup).map((row) => row.id));
|
|
1541
|
+
let projectionRows = new Map(sourceRows.map((row) => [row.id, row]));
|
|
1542
|
+
if (request.projection.resolveRelations.length > 0) projectionRows = new Map(this.fetchRowsByIds(request.tableId, visibleRowIds, request.projection).map((row) => [row.id, row]));
|
|
1543
|
+
const columns = this.columns(request.tableId, request.projection);
|
|
1544
|
+
const cursorForGroup = (group) => group ? this.encodeCursor("groups", {
|
|
1545
|
+
fileId: metadata.fileId,
|
|
1546
|
+
tableId: request.tableId,
|
|
1547
|
+
revision: String(metadata.revision),
|
|
1548
|
+
projectionHash: projectionDigest,
|
|
1549
|
+
bindingHash: bindingDigest,
|
|
1550
|
+
groupIdentity: group.identity
|
|
1551
|
+
}) : null;
|
|
1552
|
+
return {
|
|
1553
|
+
fileId: metadata.fileId,
|
|
1554
|
+
tableId: request.tableId,
|
|
1555
|
+
revision: String(metadata.revision),
|
|
1556
|
+
projectionHash: projectionDigest,
|
|
1557
|
+
columns,
|
|
1558
|
+
groups: selected.map((group) => {
|
|
1559
|
+
const visible = group.rows.slice(0, request.rowsPerGroup);
|
|
1560
|
+
const projected = visible.map((row) => projectionRows.get(row.id) ?? row);
|
|
1561
|
+
const groupedQuery = queryForGroup(request.query, request.groupBy, group.key);
|
|
1562
|
+
const backwardGroupedQuery = reverseRuntimeQuery(groupedQuery, this.core.listFields(request.tableId));
|
|
1563
|
+
const rowCursor = (row) => this.encodeCursor("group-rows", {
|
|
1564
|
+
fileId: metadata.fileId,
|
|
1565
|
+
tableId: request.tableId,
|
|
1566
|
+
revision: String(metadata.revision),
|
|
1567
|
+
projectionHash: projectionDigest,
|
|
1568
|
+
bindingHash: bindingDigest,
|
|
1569
|
+
request: groupCursorSource(request),
|
|
1570
|
+
groupKey: group.key,
|
|
1571
|
+
forward: this.core.createRowCursor(request.tableId, this.compatibilityQuery(request.tableId, groupedQuery), row),
|
|
1572
|
+
backward: this.core.createRowCursor(request.tableId, this.compatibilityQuery(request.tableId, backwardGroupedQuery), row)
|
|
1573
|
+
});
|
|
1574
|
+
return {
|
|
1575
|
+
key: group.key,
|
|
1576
|
+
count: String(group.rows.length),
|
|
1577
|
+
aggregates: aggregateResultsForRows(request.aggregates, fieldsById, group.rows),
|
|
1578
|
+
rows: this.projectRows(request.tableId, request.projection, projected, columns),
|
|
1579
|
+
nextRowCursor: visible.length < group.rows.length && visible.length > 0 ? rowCursor(visible.at(-1)) : null
|
|
1580
|
+
};
|
|
1581
|
+
}),
|
|
1582
|
+
nextCursor: end < ordered.length ? cursorForGroup(selected.at(-1)) : null,
|
|
1583
|
+
previousCursor: start > 0 ? cursorForGroup(selected[0]) : null
|
|
1584
|
+
};
|
|
1585
|
+
}
|
|
1586
|
+
async getRowsByIdDirect(tableId, rowIds, projection) {
|
|
1587
|
+
const rows = this.fetchRowsByIds(tableId, rowIds, projection);
|
|
1588
|
+
const found = new Set(rows.map((row) => row.id));
|
|
1589
|
+
const metadata = this.core.info();
|
|
1590
|
+
const columns = this.columns(tableId, projection);
|
|
1591
|
+
return {
|
|
1592
|
+
fileId: metadata.fileId,
|
|
1593
|
+
tableId,
|
|
1594
|
+
revision: String(metadata.revision),
|
|
1595
|
+
projectionHash: await projectionHash(projection),
|
|
1596
|
+
columns,
|
|
1597
|
+
rows: this.projectRows(tableId, projection, rows, columns),
|
|
1598
|
+
missingRowIds: rowIds.filter((id) => !found.has(id))
|
|
1599
|
+
};
|
|
1600
|
+
}
|
|
1601
|
+
fetchRowsByIds(tableId, rowIds, projection) {
|
|
1602
|
+
if (rowIds.length === 0) return [];
|
|
1603
|
+
const rowIdField = this.core.listFields(tableId).find((field) => field.systemRole === "row-id" || field.type === "row-id");
|
|
1604
|
+
if (!rowIdField?.id) throw runtimeError("corrupt-file", "Table is missing its Row-ID Field");
|
|
1605
|
+
const found = /* @__PURE__ */ new Map();
|
|
1606
|
+
for (let offset = 0; offset < rowIds.length; offset += EIDOS_RUNTIME_LIMITS.rowsByIdMax) {
|
|
1607
|
+
const ids = rowIds.slice(offset, offset + EIDOS_RUNTIME_LIMITS.rowsByIdMax);
|
|
1608
|
+
const rows = this.core.queryRows(tableId, {
|
|
1609
|
+
fields: projection.fields,
|
|
1610
|
+
query: this.compatibilityQuery(tableId, { filter: {
|
|
1611
|
+
op: "in",
|
|
1612
|
+
fieldId: rowIdField.id,
|
|
1613
|
+
values: ids
|
|
1614
|
+
} }),
|
|
1615
|
+
limit: ids.length,
|
|
1616
|
+
resolveRelations: projection.resolveRelations.length > 0
|
|
1617
|
+
}).rows;
|
|
1618
|
+
for (const row of rows) found.set(row.id, row);
|
|
1619
|
+
}
|
|
1620
|
+
return rowIds.flatMap((rowId) => {
|
|
1621
|
+
const row = found.get(rowId);
|
|
1622
|
+
return row ? [row] : [];
|
|
1623
|
+
});
|
|
1624
|
+
}
|
|
1625
|
+
allViews() {
|
|
1626
|
+
return this.core.listTables().flatMap((table) => this.core.listViews(table.id));
|
|
1627
|
+
}
|
|
1628
|
+
analyzeSchemaChange(change) {
|
|
1629
|
+
const leaves = change.kind === "batch" ? change.changes : [change];
|
|
1630
|
+
if (leaves.length === 0) throw runtimeError("invalid-request", "Schema batch must be non-empty");
|
|
1631
|
+
const dependencies = /* @__PURE__ */ new Map();
|
|
1632
|
+
const warnings = [];
|
|
1633
|
+
let classification = "metadata-only";
|
|
1634
|
+
let affectedRows = 0n;
|
|
1635
|
+
const valueChanges = [];
|
|
1636
|
+
const forbid = (code, message, members = {}) => {
|
|
1637
|
+
classification = "forbidden";
|
|
1638
|
+
warnings.push({
|
|
1639
|
+
code,
|
|
1640
|
+
severity: "error",
|
|
1641
|
+
message,
|
|
1642
|
+
...members
|
|
1643
|
+
});
|
|
1644
|
+
};
|
|
1645
|
+
const lossy = (message, members = {}) => {
|
|
1646
|
+
if (classification !== "forbidden") classification = "explicit-lossy";
|
|
1647
|
+
warnings.push({
|
|
1648
|
+
code: "object-delete-loss",
|
|
1649
|
+
severity: "warning",
|
|
1650
|
+
message,
|
|
1651
|
+
...members
|
|
1652
|
+
});
|
|
1653
|
+
};
|
|
1654
|
+
const tableIds = new Set(this.core.listTables().map((table) => table.id));
|
|
1655
|
+
const fields = this.core.listTables().flatMap((table) => this.core.listFields(table.id));
|
|
1656
|
+
const fieldsById = new Map(fields.map((field) => [field.id, field]));
|
|
1657
|
+
const proposedFields = new Map(fields.map((field) => [field.id, {
|
|
1658
|
+
...field,
|
|
1659
|
+
settings: { ...field.settings ?? {} },
|
|
1660
|
+
property: field.property ? { ...field.property } : null
|
|
1661
|
+
}]));
|
|
1662
|
+
const tableNames = new Map(this.core.listTables().map((table) => [table.id, table.name]));
|
|
1663
|
+
let syntheticField = 0;
|
|
1664
|
+
let rewrittenFormulaSources = 0;
|
|
1665
|
+
for (const leaf of leaves) switch (leaf.kind) {
|
|
1666
|
+
case "create-table": {
|
|
1667
|
+
const tableId = `new-table:${leaf.clientKey}`;
|
|
1668
|
+
tableNames.set(tableId, leaf.name);
|
|
1669
|
+
for (const system of simulatedSystemFields(tableId, leaf.name)) proposedFields.set(system.id, system);
|
|
1670
|
+
for (const field of leaf.fields) {
|
|
1671
|
+
const candidate = simulatedNewField(tableId, leaf.name, field, ++syntheticField);
|
|
1672
|
+
proposedFields.set(candidate.id, candidate);
|
|
1673
|
+
}
|
|
1674
|
+
break;
|
|
1675
|
+
}
|
|
1676
|
+
case "delete-table":
|
|
1677
|
+
for (const [id, field] of proposedFields) if (field.tableId === leaf.tableId) proposedFields.delete(id);
|
|
1678
|
+
tableNames.delete(leaf.tableId);
|
|
1679
|
+
break;
|
|
1680
|
+
case "create-field": {
|
|
1681
|
+
const candidate = simulatedNewField(leaf.tableId, tableNames.get(leaf.tableId) ?? leaf.tableId, leaf.field, ++syntheticField);
|
|
1682
|
+
proposedFields.set(candidate.id, candidate);
|
|
1683
|
+
break;
|
|
1684
|
+
}
|
|
1685
|
+
case "delete-field":
|
|
1686
|
+
proposedFields.delete(leaf.fieldId);
|
|
1687
|
+
break;
|
|
1688
|
+
case "rename-field": {
|
|
1689
|
+
const candidate = proposedFields.get(leaf.fieldId);
|
|
1690
|
+
if (!candidate) break;
|
|
1691
|
+
const oldName = candidate.name;
|
|
1692
|
+
for (const formula of proposedFields.values()) {
|
|
1693
|
+
if (formula.tableId !== candidate.tableId || formula.type !== "formula") continue;
|
|
1694
|
+
const source = String(formula.property?.formula ?? "");
|
|
1695
|
+
const rewritten = rewriteEidosFileFormulaFieldReferences(source, oldName, leaf.name);
|
|
1696
|
+
if (rewritten !== source) {
|
|
1697
|
+
formula.property = {
|
|
1698
|
+
...formula.property ?? {},
|
|
1699
|
+
formula: rewritten
|
|
1700
|
+
};
|
|
1701
|
+
rewrittenFormulaSources += 1;
|
|
1702
|
+
}
|
|
1703
|
+
}
|
|
1704
|
+
candidate.name = leaf.name;
|
|
1705
|
+
break;
|
|
1706
|
+
}
|
|
1707
|
+
case "set-formula": {
|
|
1708
|
+
const candidate = proposedFields.get(leaf.fieldId);
|
|
1709
|
+
if (candidate?.type === "formula") candidate.property = {
|
|
1710
|
+
...candidate.property ?? {},
|
|
1711
|
+
formula: leaf.definition.sourceText,
|
|
1712
|
+
displayType: leaf.definition.resultType
|
|
1713
|
+
};
|
|
1714
|
+
break;
|
|
1715
|
+
}
|
|
1716
|
+
case "set-lookup": {
|
|
1717
|
+
const candidate = proposedFields.get(leaf.fieldId);
|
|
1718
|
+
if (candidate?.type === "lookup") candidate.property = {
|
|
1719
|
+
...candidate.property ?? {},
|
|
1720
|
+
relationField: leaf.definition.relationFieldId,
|
|
1721
|
+
targetField: leaf.definition.targetFieldId,
|
|
1722
|
+
aggregate: leaf.definition.aggregate,
|
|
1723
|
+
distinct: leaf.definition.distinctValues
|
|
1724
|
+
};
|
|
1725
|
+
break;
|
|
1726
|
+
}
|
|
1727
|
+
case "set-relation": {
|
|
1728
|
+
const candidate = proposedFields.get(leaf.fieldId);
|
|
1729
|
+
if (candidate?.type === "relation") {
|
|
1730
|
+
candidate.property = simulatedRelationProperty(leaf.definition);
|
|
1731
|
+
candidate.physicalName = leaf.definition.direction === "forward" ? candidate.physicalName ?? candidate.tableColumnName : null;
|
|
1732
|
+
}
|
|
1733
|
+
break;
|
|
1734
|
+
}
|
|
1735
|
+
case "convert-field": {
|
|
1736
|
+
const candidate = proposedFields.get(leaf.fieldId);
|
|
1737
|
+
if (candidate) {
|
|
1738
|
+
candidate.type = leaf.to;
|
|
1739
|
+
candidate.property = leaf.to === "relation" ? simulatedRelationProperty(leaf.definition) : null;
|
|
1740
|
+
candidate.storageCodec = leaf.to === "relation" ? "relation" : leaf.to === "file" || leaf.to === "multi-select" ? "json_array" : "scalar";
|
|
1741
|
+
candidate.valueKind = leaf.to === "relation" ? "relation" : "source";
|
|
1742
|
+
}
|
|
1743
|
+
break;
|
|
1744
|
+
}
|
|
1745
|
+
}
|
|
1746
|
+
for (let pass = 0; pass < proposedFields.size; pass += 1) {
|
|
1747
|
+
let changed = false;
|
|
1748
|
+
for (const field of proposedFields.values()) {
|
|
1749
|
+
if (field.type !== "lookup" || !field.property) continue;
|
|
1750
|
+
const target = proposedFields.get(String(field.property.targetField));
|
|
1751
|
+
if (!target) continue;
|
|
1752
|
+
const displayType = eidosFileLookupDisplayType(String(field.property.aggregate), target);
|
|
1753
|
+
const valueType = eidosFileLookupValueType(String(field.property.aggregate), target);
|
|
1754
|
+
if (field.property.displayType !== displayType || JSON.stringify(field.property.valueType) !== JSON.stringify(valueType)) {
|
|
1755
|
+
field.property.displayType = displayType;
|
|
1756
|
+
field.property.valueType = valueType;
|
|
1757
|
+
changed = true;
|
|
1758
|
+
}
|
|
1759
|
+
}
|
|
1760
|
+
if (!changed) break;
|
|
1761
|
+
}
|
|
1762
|
+
const proposedByTable = /* @__PURE__ */ new Map();
|
|
1763
|
+
for (const field of proposedFields.values()) {
|
|
1764
|
+
const owner = proposedByTable.get(field.tableId) ?? [];
|
|
1765
|
+
owner.push(field);
|
|
1766
|
+
proposedByTable.set(field.tableId, owner);
|
|
1767
|
+
}
|
|
1768
|
+
const dependencyGraph = /* @__PURE__ */ new Map();
|
|
1769
|
+
for (const field of proposedFields.values()) dependencyGraph.set(field.id, /* @__PURE__ */ new Set());
|
|
1770
|
+
for (const [tableId, tableFields] of proposedByTable) {
|
|
1771
|
+
const names = /* @__PURE__ */ new Map();
|
|
1772
|
+
for (const field of tableFields) {
|
|
1773
|
+
const folded = asciiNoCase(field.name);
|
|
1774
|
+
const duplicate = names.get(folded);
|
|
1775
|
+
if (duplicate && duplicate !== field.id) forbid("semantic-field-invalid", "Field names are not unique", {
|
|
1776
|
+
tableId,
|
|
1777
|
+
fieldId: field.id
|
|
1778
|
+
});
|
|
1779
|
+
else names.set(folded, field.id);
|
|
1780
|
+
}
|
|
1781
|
+
try {
|
|
1782
|
+
for (const compiled of compileEidosFileFormulaFields(tableFields)) for (const dependency of compiled.dependencyFieldIds) dependencyGraph.get(dependency)?.add(compiled.field.id);
|
|
1783
|
+
} catch (error) {
|
|
1784
|
+
forbid("formula-type-invalid", error instanceof Error ? error.message : "Invalid Formula", { tableId });
|
|
1785
|
+
}
|
|
1786
|
+
}
|
|
1787
|
+
for (const field of proposedFields.values()) if (field.type === "lookup") {
|
|
1788
|
+
const relationId = String(field.property?.relationField ?? "");
|
|
1789
|
+
const targetId = String(field.property?.targetField ?? "");
|
|
1790
|
+
const aggregate = String(field.property?.aggregate ?? "");
|
|
1791
|
+
const relation = proposedFields.get(relationId);
|
|
1792
|
+
const target = proposedFields.get(targetId);
|
|
1793
|
+
const targetTableId = String(relation?.property?.targetTableId ?? "");
|
|
1794
|
+
if (relation?.type !== "relation" || relation.tableId !== field.tableId || !target || target.tableId !== targetTableId || ![
|
|
1795
|
+
"values",
|
|
1796
|
+
"first",
|
|
1797
|
+
"count",
|
|
1798
|
+
"sum",
|
|
1799
|
+
"average",
|
|
1800
|
+
"min",
|
|
1801
|
+
"max"
|
|
1802
|
+
].includes(aggregate) || !eidosFileLookupAggregateSupportsTarget(aggregate, target)) forbid("lookup-invalid", `Lookup ${field.name} is invalid`, {
|
|
1803
|
+
tableId: field.tableId,
|
|
1804
|
+
fieldId: field.id
|
|
1805
|
+
});
|
|
1806
|
+
dependencyGraph.get(relationId)?.add(field.id);
|
|
1807
|
+
dependencyGraph.get(targetId)?.add(field.id);
|
|
1808
|
+
} else if (field.type === "relation" && field.property?.direction === "inverse") {
|
|
1809
|
+
const sourceId = String(field.property.sourceFieldId ?? "");
|
|
1810
|
+
dependencyGraph.get(sourceId)?.add(field.id);
|
|
1811
|
+
}
|
|
1812
|
+
const cycle = smallestDependencyCycle(dependencyGraph);
|
|
1813
|
+
if (cycle) forbid("semantic-cycle", "Derived Field dependency cycle", { relatedFieldIds: cycle });
|
|
1814
|
+
if (rewrittenFormulaSources > 0) classification = "lossless-rewrite";
|
|
1815
|
+
for (const leaf of leaves) {
|
|
1816
|
+
if ("position" in leaf) parseSignedInt64(leaf.position, "position");
|
|
1817
|
+
switch (leaf.kind) {
|
|
1818
|
+
case "create-table":
|
|
1819
|
+
if (leaf.fields.some((field) => field.kind === "relation" || field.kind === "lookup")) forbid("relation-definition-invalid", "Relation and Lookup Fields cannot be created inside create-table");
|
|
1820
|
+
break;
|
|
1821
|
+
case "set-file-title":
|
|
1822
|
+
assertEidosFileDisplayName(leaf.title, "File title");
|
|
1823
|
+
break;
|
|
1824
|
+
case "set-default-table":
|
|
1825
|
+
if (leaf.tableId !== null && !tableIds.has(leaf.tableId)) forbid("dependency-blocked", "Default Table does not exist");
|
|
1826
|
+
break;
|
|
1827
|
+
case "delete-table":
|
|
1828
|
+
if (!tableIds.has(leaf.tableId)) {
|
|
1829
|
+
forbid("dependency-blocked", "Table does not exist", { tableId: leaf.tableId });
|
|
1830
|
+
break;
|
|
1831
|
+
}
|
|
1832
|
+
if (this.core.info().defaultTableId === leaf.tableId) forbid("dependency-blocked", "Clear or retarget the default Table before deletion", { tableId: leaf.tableId });
|
|
1833
|
+
lossy("Deleting a Table removes its rows and Fields", { tableId: leaf.tableId });
|
|
1834
|
+
affectedRows += BigInt(this.core.countRows(leaf.tableId));
|
|
1835
|
+
dependencies.set("table:" + leaf.tableId, {
|
|
1836
|
+
object: "table",
|
|
1837
|
+
id: leaf.tableId
|
|
1838
|
+
});
|
|
1839
|
+
for (const field of this.core.listFields(leaf.tableId)) dependencies.set("field:" + field.id, {
|
|
1840
|
+
object: "field",
|
|
1841
|
+
id: field.id
|
|
1842
|
+
});
|
|
1843
|
+
for (const view of this.core.listViews(leaf.tableId)) dependencies.set("view:" + view.id, {
|
|
1844
|
+
object: "view",
|
|
1845
|
+
id: view.id
|
|
1846
|
+
});
|
|
1847
|
+
break;
|
|
1848
|
+
case "rename-table":
|
|
1849
|
+
case "set-table-settings":
|
|
1850
|
+
case "set-table-position":
|
|
1851
|
+
if (!tableIds.has(leaf.tableId)) forbid("dependency-blocked", "Table does not exist", { tableId: leaf.tableId });
|
|
1852
|
+
break;
|
|
1853
|
+
case "create-field":
|
|
1854
|
+
if (!tableIds.has(leaf.tableId)) forbid("dependency-blocked", "Owning Table does not exist", { tableId: leaf.tableId });
|
|
1855
|
+
if (leaf.field.kind !== "formula" && leaf.field.kind !== "lookup" && leaf.field.kind !== "relation" && leaf.field.nullable === false && this.core.countRows(leaf.tableId) > 0) forbid("non-nullability-blocked", "A populated Table cannot add a non-null scalar Field", { tableId: leaf.tableId });
|
|
1856
|
+
break;
|
|
1857
|
+
case "delete-field": {
|
|
1858
|
+
const field = fieldsById.get(leaf.fieldId);
|
|
1859
|
+
if (!field) {
|
|
1860
|
+
forbid("dependency-blocked", "Field does not exist", { fieldId: leaf.fieldId });
|
|
1861
|
+
break;
|
|
1862
|
+
}
|
|
1863
|
+
if (field.systemRole) forbid("dependency-blocked", "System Fields cannot be deleted", { fieldId: leaf.fieldId });
|
|
1864
|
+
else {
|
|
1865
|
+
lossy("Deleting a Field removes its values", {
|
|
1866
|
+
tableId: field.tableId,
|
|
1867
|
+
fieldId: leaf.fieldId
|
|
1868
|
+
});
|
|
1869
|
+
affectedRows += BigInt(this.core.countRows(field.tableId));
|
|
1870
|
+
}
|
|
1871
|
+
dependencies.set("field:" + leaf.fieldId, {
|
|
1872
|
+
object: "field",
|
|
1873
|
+
id: leaf.fieldId
|
|
1874
|
+
});
|
|
1875
|
+
for (const view of this.core.listViews(field.tableId)) {
|
|
1876
|
+
const queryReferencesField = view.sorts.some((sort) => sort.field === leaf.fieldId) || compatibilityFilterReferencesField(view.filter, leaf.fieldId);
|
|
1877
|
+
const layoutReferencesField = knownViewLayoutReferencesField(view.properties, leaf.fieldId);
|
|
1878
|
+
if (!queryReferencesField && !layoutReferencesField) continue;
|
|
1879
|
+
dependencies.set("view:" + view.id, {
|
|
1880
|
+
object: "view",
|
|
1881
|
+
id: view.id
|
|
1882
|
+
});
|
|
1883
|
+
if (queryReferencesField) forbid("dependency-blocked", "Saved View query still references the Field", {
|
|
1884
|
+
tableId: field.tableId,
|
|
1885
|
+
fieldId: leaf.fieldId,
|
|
1886
|
+
viewId: view.id
|
|
1887
|
+
});
|
|
1888
|
+
}
|
|
1889
|
+
break;
|
|
1890
|
+
}
|
|
1891
|
+
case "rename-field":
|
|
1892
|
+
case "set-field-settings":
|
|
1893
|
+
case "set-field-position":
|
|
1894
|
+
case "set-formula":
|
|
1895
|
+
case "set-lookup":
|
|
1896
|
+
case "set-relation":
|
|
1897
|
+
case "rename-option":
|
|
1898
|
+
if (!fieldsById.has(leaf.fieldId)) forbid("dependency-blocked", "Field does not exist", { fieldId: leaf.fieldId });
|
|
1899
|
+
if (leaf.kind === "rename-option" && leaf.collision === "merge" && !warnings.some((warning) => warning.severity === "error")) {
|
|
1900
|
+
classification = "explicit-lossy";
|
|
1901
|
+
warnings.push({
|
|
1902
|
+
code: "option-merge-loss",
|
|
1903
|
+
severity: "warning",
|
|
1904
|
+
fieldId: leaf.fieldId,
|
|
1905
|
+
message: "Option values may merge"
|
|
1906
|
+
});
|
|
1907
|
+
}
|
|
1908
|
+
break;
|
|
1909
|
+
case "set-record-label":
|
|
1910
|
+
if (!tableIds.has(leaf.tableId) || fieldsById.get(leaf.fieldId)?.tableId !== leaf.tableId) forbid("record-label-blocked", "Record Label Field is invalid", {
|
|
1911
|
+
tableId: leaf.tableId,
|
|
1912
|
+
fieldId: leaf.fieldId
|
|
1913
|
+
});
|
|
1914
|
+
break;
|
|
1915
|
+
case "set-field-nullable": {
|
|
1916
|
+
const field = fieldsById.get(leaf.fieldId);
|
|
1917
|
+
if (!field || field.systemRole || !field.physicalName) forbid("non-nullability-blocked", "Only stored scalar/JSON Fields support nullable changes", { fieldId: leaf.fieldId });
|
|
1918
|
+
else if (leaf.nullable === false) {
|
|
1919
|
+
const table = this.core.getTable(field.tableId);
|
|
1920
|
+
const nulls = this.core.connection.get("SELECT count(*) AS count FROM " + quoteSqlIdentifier(table.physicalName ?? table.rawTableName) + " WHERE " + quoteSqlIdentifier(field.physicalName) + " IS NULL")?.count;
|
|
1921
|
+
if (BigInt(nulls ?? 0) > 0n) forbid("non-nullability-blocked", "Existing SQL NULL values block non-nullability", {
|
|
1922
|
+
tableId: field.tableId,
|
|
1923
|
+
fieldId: leaf.fieldId
|
|
1924
|
+
});
|
|
1925
|
+
}
|
|
1926
|
+
break;
|
|
1927
|
+
}
|
|
1928
|
+
case "convert-field": {
|
|
1929
|
+
const field = fieldsById.get(leaf.fieldId);
|
|
1930
|
+
if (!field || field.systemRole || !field.physicalName || ["formula", "lookup"].includes(field.type)) {
|
|
1931
|
+
forbid("conversion-domain-invalid", "Only stored user Fields support convert-field", { fieldId: leaf.fieldId });
|
|
1932
|
+
break;
|
|
1933
|
+
}
|
|
1934
|
+
const conversion = this.conversionPlan(leaf);
|
|
1935
|
+
if (conversion.classification === "forbidden") {
|
|
1936
|
+
forbid("conversion-domain-invalid", conversion.error ?? "Stored values cannot be converted", {
|
|
1937
|
+
tableId: field.tableId,
|
|
1938
|
+
fieldId: leaf.fieldId
|
|
1939
|
+
});
|
|
1940
|
+
break;
|
|
1941
|
+
}
|
|
1942
|
+
classification = highestSchemaClassification(classification, conversion.classification);
|
|
1943
|
+
affectedRows += BigInt(conversion.affectedRows);
|
|
1944
|
+
valueChanges.push(...conversion.valueChanges.map((change) => ({
|
|
1945
|
+
...change,
|
|
1946
|
+
tableId: field.tableId,
|
|
1947
|
+
fieldId: leaf.fieldId
|
|
1948
|
+
})));
|
|
1949
|
+
break;
|
|
1950
|
+
}
|
|
1951
|
+
}
|
|
1952
|
+
}
|
|
1953
|
+
const orderedDependencies = Array.from(dependencies.values()).sort((left, right) => {
|
|
1954
|
+
const order = {
|
|
1955
|
+
table: 0,
|
|
1956
|
+
field: 1,
|
|
1957
|
+
view: 2
|
|
1958
|
+
};
|
|
1959
|
+
return order[left.object] - order[right.object] || binaryCompare(left.id, right.id);
|
|
1960
|
+
});
|
|
1961
|
+
return {
|
|
1962
|
+
classification,
|
|
1963
|
+
dependencies: orderedDependencies,
|
|
1964
|
+
warnings,
|
|
1965
|
+
affectedRows: String(affectedRows),
|
|
1966
|
+
valueChanges
|
|
1967
|
+
};
|
|
1968
|
+
}
|
|
1969
|
+
conversionPlan(change) {
|
|
1970
|
+
const field = this.field(change.fieldId);
|
|
1971
|
+
if (!field.physicalName) return {
|
|
1972
|
+
classification: "forbidden",
|
|
1973
|
+
rows: [],
|
|
1974
|
+
affectedRows: "0",
|
|
1975
|
+
valueChanges: [],
|
|
1976
|
+
error: "Virtual and inverse Fields cannot be converted"
|
|
1977
|
+
};
|
|
1978
|
+
const targetNullable = "toNullable" in change ? change.toNullable : false;
|
|
1979
|
+
if (eidosFileConversionCanReusePhysicalColumn(field.type, change.to, field.nullable ?? false, targetNullable) && change.to !== "url") return {
|
|
1980
|
+
classification: "metadata-only",
|
|
1981
|
+
rows: [],
|
|
1982
|
+
affectedRows: "0",
|
|
1983
|
+
valueChanges: []
|
|
1984
|
+
};
|
|
1985
|
+
const table = this.core.getTable(field.tableId);
|
|
1986
|
+
const rows = [];
|
|
1987
|
+
scanStableRowPages(this.core.connection, {
|
|
1988
|
+
columnsSql: `"_id" AS id, ${quoteSqlIdentifier(field.physicalName)} AS value`,
|
|
1989
|
+
tableSql: quoteSqlIdentifier(table.physicalName ?? table.rawTableName),
|
|
1990
|
+
rowIdSql: `"_id"`,
|
|
1991
|
+
rowIdKey: "id"
|
|
1992
|
+
}, (page) => rows.push(...page));
|
|
1993
|
+
let relationIds;
|
|
1994
|
+
if (change.to === "relation") {
|
|
1995
|
+
const target = this.core.getTable(change.definition.targetTableId);
|
|
1996
|
+
relationIds = /* @__PURE__ */ new Set();
|
|
1997
|
+
scanStableRowPages(this.core.connection, {
|
|
1998
|
+
columnsSql: `"_id" AS id`,
|
|
1999
|
+
tableSql: quoteSqlIdentifier(target.physicalName ?? target.rawTableName),
|
|
2000
|
+
rowIdSql: `"_id"`,
|
|
2001
|
+
rowIdKey: "id"
|
|
2002
|
+
}, (page) => {
|
|
2003
|
+
for (const row of page) relationIds.add(row.id);
|
|
2004
|
+
});
|
|
2005
|
+
}
|
|
2006
|
+
return planCanonicalFieldConversion({
|
|
2007
|
+
from: field.type,
|
|
2008
|
+
to: change.to,
|
|
2009
|
+
toNullable: "toNullable" in change ? change.toNullable : false,
|
|
2010
|
+
policies: change.policies,
|
|
2011
|
+
rows,
|
|
2012
|
+
...relationIds ? { relationIdValid: (id) => relationIds.has(id) } : {},
|
|
2013
|
+
...change.to === "relation" ? { relationCardinality: change.definition.cardinality } : {}
|
|
2014
|
+
});
|
|
2015
|
+
}
|
|
2016
|
+
applySchemaChange(change, createdObjects, affectedTableIds, affectedFieldIds) {
|
|
2017
|
+
const leaves = change.kind === "batch" ? change.changes : [change];
|
|
2018
|
+
for (const leaf of leaves) switch (leaf.kind) {
|
|
2019
|
+
case "create-table": {
|
|
2020
|
+
const table = this.core.createTable({
|
|
2021
|
+
name: leaf.name,
|
|
2022
|
+
createDefaultView: false,
|
|
2023
|
+
fields: leaf.fields.map(newFieldInput)
|
|
2024
|
+
});
|
|
2025
|
+
affectedTableIds.add(table.id);
|
|
2026
|
+
const fields = this.core.listFields(table.id);
|
|
2027
|
+
createdObjects.push({
|
|
2028
|
+
id: table.id,
|
|
2029
|
+
object: "table",
|
|
2030
|
+
clientKey: leaf.clientKey
|
|
2031
|
+
});
|
|
2032
|
+
for (const field of fields) {
|
|
2033
|
+
if (field.systemRole) createdObjects.push({
|
|
2034
|
+
id: field.id,
|
|
2035
|
+
object: "field",
|
|
2036
|
+
systemRole: field.systemRole
|
|
2037
|
+
});
|
|
2038
|
+
else {
|
|
2039
|
+
const source = leaf.fields.find((candidate) => candidate.name === field.name);
|
|
2040
|
+
if (source) createdObjects.push({
|
|
2041
|
+
id: field.id,
|
|
2042
|
+
object: "field",
|
|
2043
|
+
clientKey: source.clientKey
|
|
2044
|
+
});
|
|
2045
|
+
}
|
|
2046
|
+
affectedFieldIds.add(field.id);
|
|
2047
|
+
}
|
|
2048
|
+
this.core.connection.run("UPDATE eidos__tables SET position=?, settings_json=? WHERE id=?", [
|
|
2049
|
+
parseSignedInt64(leaf.position, "position"),
|
|
2050
|
+
canonicalizeEidosFileJson(leaf.settings ?? {}),
|
|
2051
|
+
table.id
|
|
2052
|
+
]);
|
|
2053
|
+
for (const source of leaf.fields) {
|
|
2054
|
+
const field = fields.find((entry) => entry.name === source.name);
|
|
2055
|
+
if (!field) continue;
|
|
2056
|
+
this.core.connection.run("UPDATE eidos__fields SET position=?, settings_json=? WHERE id=?", [
|
|
2057
|
+
parseSignedInt64(source.position, "position"),
|
|
2058
|
+
canonicalizeEidosFileJson(source.settings ?? {}),
|
|
2059
|
+
field.id
|
|
2060
|
+
]);
|
|
2061
|
+
}
|
|
2062
|
+
if (leaf.labelFieldClientKey) {
|
|
2063
|
+
const source = leaf.fields.find((field) => field.clientKey === leaf.labelFieldClientKey);
|
|
2064
|
+
const label = source ? fields.find((field) => field.name === source.name) : void 0;
|
|
2065
|
+
if (!label?.id) throw runtimeError("invalid-request", "labelFieldClientKey does not identify a supplied Field");
|
|
2066
|
+
this.core.updateField(table.id, label.id, { isRecordLabel: true });
|
|
2067
|
+
}
|
|
2068
|
+
break;
|
|
2069
|
+
}
|
|
2070
|
+
case "set-file-title":
|
|
2071
|
+
this.core.connection.run("UPDATE eidos__meta SET title=? WHERE singleton=1", [assertEidosFileDisplayName(leaf.title, "File title")]);
|
|
2072
|
+
break;
|
|
2073
|
+
case "set-default-table":
|
|
2074
|
+
this.core.connection.run("UPDATE eidos__meta SET default_table_id=? WHERE singleton=1", [leaf.tableId]);
|
|
2075
|
+
break;
|
|
2076
|
+
case "delete-table":
|
|
2077
|
+
this.core.deleteTable(leaf.tableId);
|
|
2078
|
+
affectedTableIds.add(leaf.tableId);
|
|
2079
|
+
break;
|
|
2080
|
+
case "rename-table":
|
|
2081
|
+
this.core.updateTable(leaf.tableId, { name: leaf.name });
|
|
2082
|
+
affectedTableIds.add(leaf.tableId);
|
|
2083
|
+
break;
|
|
2084
|
+
case "set-table-settings":
|
|
2085
|
+
this.core.connection.run("UPDATE eidos__tables SET settings_json=? WHERE id=?", [canonicalizeEidosFileJson(leaf.settings), leaf.tableId]);
|
|
2086
|
+
affectedTableIds.add(leaf.tableId);
|
|
2087
|
+
break;
|
|
2088
|
+
case "set-table-position":
|
|
2089
|
+
this.core.connection.run("UPDATE eidos__tables SET position=? WHERE id=?", [parseSignedInt64(leaf.position, "position"), leaf.tableId]);
|
|
2090
|
+
affectedTableIds.add(leaf.tableId);
|
|
2091
|
+
break;
|
|
2092
|
+
case "create-field": {
|
|
2093
|
+
const field = this.core.addField(leaf.tableId, newFieldInput(leaf.field));
|
|
2094
|
+
this.core.connection.run("UPDATE eidos__fields SET position=?, settings_json=? WHERE id=?", [
|
|
2095
|
+
parseSignedInt64(leaf.field.position, "position"),
|
|
2096
|
+
canonicalizeEidosFileJson(leaf.field.settings ?? {}),
|
|
2097
|
+
field.id
|
|
2098
|
+
]);
|
|
2099
|
+
createdObjects.push({
|
|
2100
|
+
id: field.id,
|
|
2101
|
+
object: "field",
|
|
2102
|
+
clientKey: leaf.field.clientKey
|
|
2103
|
+
});
|
|
2104
|
+
affectedTableIds.add(leaf.tableId);
|
|
2105
|
+
affectedFieldIds.add(field.id);
|
|
2106
|
+
break;
|
|
2107
|
+
}
|
|
2108
|
+
case "delete-field": {
|
|
2109
|
+
const field = this.field(leaf.fieldId);
|
|
2110
|
+
if (field.isRecordLabel && leaf.replacementLabelFieldId) this.core.updateField(field.tableId, leaf.replacementLabelFieldId, { isRecordLabel: true });
|
|
2111
|
+
this.core.deleteField(field.tableId, leaf.fieldId);
|
|
2112
|
+
affectedTableIds.add(field.tableId);
|
|
2113
|
+
affectedFieldIds.add(leaf.fieldId);
|
|
2114
|
+
break;
|
|
2115
|
+
}
|
|
2116
|
+
case "rename-field": {
|
|
2117
|
+
const field = this.field(leaf.fieldId);
|
|
2118
|
+
this.core.updateField(field.tableId, leaf.fieldId, { name: leaf.name });
|
|
2119
|
+
affectedTableIds.add(field.tableId);
|
|
2120
|
+
affectedFieldIds.add(leaf.fieldId);
|
|
2121
|
+
break;
|
|
2122
|
+
}
|
|
2123
|
+
case "set-field-settings": {
|
|
2124
|
+
const field = this.field(leaf.fieldId);
|
|
2125
|
+
this.core.connection.run("UPDATE eidos__fields SET settings_json=? WHERE id=?", [canonicalizeEidosFileJson(leaf.settings), leaf.fieldId]);
|
|
2126
|
+
affectedTableIds.add(field.tableId);
|
|
2127
|
+
affectedFieldIds.add(leaf.fieldId);
|
|
2128
|
+
break;
|
|
2129
|
+
}
|
|
2130
|
+
case "set-field-position": {
|
|
2131
|
+
const field = this.field(leaf.fieldId);
|
|
2132
|
+
this.core.connection.run("UPDATE eidos__fields SET position=? WHERE id=?", [parseSignedInt64(leaf.position, "position"), leaf.fieldId]);
|
|
2133
|
+
affectedTableIds.add(field.tableId);
|
|
2134
|
+
affectedFieldIds.add(leaf.fieldId);
|
|
2135
|
+
break;
|
|
2136
|
+
}
|
|
2137
|
+
case "set-record-label":
|
|
2138
|
+
this.core.updateField(leaf.tableId, leaf.fieldId, { isRecordLabel: true });
|
|
2139
|
+
affectedTableIds.add(leaf.tableId);
|
|
2140
|
+
affectedFieldIds.add(leaf.fieldId);
|
|
2141
|
+
break;
|
|
2142
|
+
case "set-formula": {
|
|
2143
|
+
const field = this.field(leaf.fieldId);
|
|
2144
|
+
this.core.updateField(field.tableId, leaf.fieldId, { property: {
|
|
2145
|
+
...field.settings ?? {},
|
|
2146
|
+
formula: leaf.definition.sourceText,
|
|
2147
|
+
displayType: leaf.definition.resultType
|
|
2148
|
+
} });
|
|
2149
|
+
affectedTableIds.add(field.tableId);
|
|
2150
|
+
affectedFieldIds.add(leaf.fieldId);
|
|
2151
|
+
break;
|
|
2152
|
+
}
|
|
2153
|
+
case "set-lookup": {
|
|
2154
|
+
const field = this.field(leaf.fieldId);
|
|
2155
|
+
this.core.updateField(field.tableId, leaf.fieldId, { property: {
|
|
2156
|
+
...field.settings ?? {},
|
|
2157
|
+
relationField: leaf.definition.relationFieldId,
|
|
2158
|
+
targetField: leaf.definition.targetFieldId,
|
|
2159
|
+
aggregate: leaf.definition.aggregate,
|
|
2160
|
+
distinct: leaf.definition.distinctValues
|
|
2161
|
+
} });
|
|
2162
|
+
affectedTableIds.add(field.tableId);
|
|
2163
|
+
affectedFieldIds.add(leaf.fieldId);
|
|
2164
|
+
break;
|
|
2165
|
+
}
|
|
2166
|
+
case "set-relation": {
|
|
2167
|
+
const field = this.field(leaf.fieldId);
|
|
2168
|
+
this.core.updateField(field.tableId, leaf.fieldId, { property: leaf.definition.direction === "forward" ? {
|
|
2169
|
+
...field.settings ?? {},
|
|
2170
|
+
direction: "forward",
|
|
2171
|
+
targetTableId: leaf.definition.targetTableId,
|
|
2172
|
+
cardinality: leaf.definition.cardinality,
|
|
2173
|
+
onDelete: leaf.definition.onDelete
|
|
2174
|
+
} : {
|
|
2175
|
+
...field.settings ?? {},
|
|
2176
|
+
direction: "inverse",
|
|
2177
|
+
targetTableId: leaf.definition.targetTableId,
|
|
2178
|
+
sourceFieldId: leaf.definition.inverseOfFieldId,
|
|
2179
|
+
cardinality: "many"
|
|
2180
|
+
} });
|
|
2181
|
+
affectedTableIds.add(field.tableId);
|
|
2182
|
+
affectedFieldIds.add(leaf.fieldId);
|
|
2183
|
+
break;
|
|
2184
|
+
}
|
|
2185
|
+
case "rename-option": {
|
|
2186
|
+
const field = this.field(leaf.fieldId);
|
|
2187
|
+
this.core.updateField(field.tableId, leaf.fieldId, { optionValueChanges: [{
|
|
2188
|
+
from: leaf.from,
|
|
2189
|
+
to: leaf.to
|
|
2190
|
+
}] });
|
|
2191
|
+
affectedTableIds.add(field.tableId);
|
|
2192
|
+
affectedFieldIds.add(leaf.fieldId);
|
|
2193
|
+
break;
|
|
2194
|
+
}
|
|
2195
|
+
case "set-field-nullable": {
|
|
2196
|
+
const field = this.field(leaf.fieldId);
|
|
2197
|
+
this.core.setFieldNullable(leaf.fieldId, leaf.nullable);
|
|
2198
|
+
affectedTableIds.add(field.tableId);
|
|
2199
|
+
affectedFieldIds.add(leaf.fieldId);
|
|
2200
|
+
break;
|
|
2201
|
+
}
|
|
2202
|
+
case "convert-field": {
|
|
2203
|
+
const field = this.field(leaf.fieldId);
|
|
2204
|
+
const conversion = this.conversionPlan(leaf);
|
|
2205
|
+
if (conversion.classification === "forbidden") throw runtimeError("forbidden", conversion.error ?? "Conversion predicates no longer hold");
|
|
2206
|
+
const targetNullable = "toNullable" in leaf ? leaf.toNullable : false;
|
|
2207
|
+
if (conversion.classification === "metadata-only" && (leaf.to === "text" || leaf.to === "select" || leaf.to === "url") && eidosFileConversionCanReusePhysicalColumn(field.type, leaf.to, field.nullable ?? false, targetNullable)) this.core.convertStoredFieldMetadataOnly(leaf.fieldId, leaf.to, targetNullable);
|
|
2208
|
+
else this.core.convertStoredField(leaf.fieldId, leaf.to, targetNullable, conversion.rows, leaf.to === "relation" ? {
|
|
2209
|
+
targetTableId: leaf.definition.targetTableId,
|
|
2210
|
+
cardinality: leaf.definition.cardinality,
|
|
2211
|
+
onDelete: leaf.definition.onDelete
|
|
2212
|
+
} : void 0);
|
|
2213
|
+
affectedTableIds.add(field.tableId);
|
|
2214
|
+
affectedFieldIds.add(leaf.fieldId);
|
|
2215
|
+
break;
|
|
2216
|
+
}
|
|
2217
|
+
}
|
|
2218
|
+
}
|
|
2219
|
+
field(fieldId) {
|
|
2220
|
+
for (const table of this.core.listTables()) {
|
|
2221
|
+
const field = this.core.listFields(table.id).find((candidate) => candidate.id === fieldId);
|
|
2222
|
+
if (field) return field;
|
|
2223
|
+
}
|
|
2224
|
+
throw runtimeError("not-found", "Field not found", { fieldId });
|
|
2225
|
+
}
|
|
2226
|
+
retainedSchemaPlan(token) {
|
|
2227
|
+
const plan = this.schemaPlans.get(token);
|
|
2228
|
+
if (!plan) throw runtimeError("plan-expired", "Schema plan is not retained");
|
|
2229
|
+
if (this.environment.clock.nowMilliseconds() > plan.expiresAtMonotonic) {
|
|
2230
|
+
this.schemaPlans.delete(token);
|
|
2231
|
+
throw runtimeError("plan-expired", "Schema plan expired");
|
|
2232
|
+
}
|
|
2233
|
+
return plan;
|
|
2234
|
+
}
|
|
2235
|
+
evictSchemaPlans(incomingBytes = 0) {
|
|
2236
|
+
const now = this.environment.clock.nowMilliseconds();
|
|
2237
|
+
for (const [token, plan] of this.schemaPlans) if (now > plan.expiresAtMonotonic) this.schemaPlans.delete(token);
|
|
2238
|
+
const retainedBytes = () => Array.from(this.schemaPlans.values()).reduce((total, plan) => total + jcsByteLength(plan), 0);
|
|
2239
|
+
while (this.schemaPlans.size >= EIDOS_RUNTIME_LIMITS.schemaPlanEntriesMax || retainedBytes() + incomingBytes > EIDOS_RUNTIME_LIMITS.schemaPlanBytesMax) {
|
|
2240
|
+
const oldest = Array.from(this.schemaPlans.values()).sort((left, right) => left.createdAtSequence - right.createdAtSequence)[0];
|
|
2241
|
+
if (!oldest) break;
|
|
2242
|
+
this.schemaPlans.delete(oldest.token);
|
|
2243
|
+
}
|
|
2244
|
+
}
|
|
2245
|
+
allocateFileEntry(request) {
|
|
2246
|
+
const extensions = request.extensions ?? {};
|
|
2247
|
+
for (const key of [
|
|
2248
|
+
"id",
|
|
2249
|
+
"name",
|
|
2250
|
+
"mediaType",
|
|
2251
|
+
"size",
|
|
2252
|
+
"uri"
|
|
2253
|
+
]) if (Object.prototype.hasOwnProperty.call(extensions, key)) throw runtimeError("invalid-request", "File entry extension collides with required member", { path: "/extensions/" + key });
|
|
2254
|
+
const candidate = {
|
|
2255
|
+
...extensions,
|
|
2256
|
+
id: this.generator.next(),
|
|
2257
|
+
name: request.name,
|
|
2258
|
+
mediaType: request.mediaType,
|
|
2259
|
+
size: request.size,
|
|
2260
|
+
uri: request.uri
|
|
2261
|
+
};
|
|
2262
|
+
try {
|
|
2263
|
+
return assertEidosFileValues([candidate])[0];
|
|
2264
|
+
} catch (error) {
|
|
2265
|
+
throw runtimeError(error instanceof EidosFileError && error.code === "resource-limit" ? "resource-limit" : "invalid-value", error instanceof Error ? error.message : "Invalid File entry");
|
|
2266
|
+
}
|
|
2267
|
+
}
|
|
2268
|
+
read(operation) {
|
|
2269
|
+
return this.connection.transaction("read", operation);
|
|
2270
|
+
}
|
|
2271
|
+
async withCommitBarrier(operation, baseRevision, context, execute, reconciliation) {
|
|
2272
|
+
const barrier = this.environment.transportCommitBarrier;
|
|
2273
|
+
if (!barrier) return execute();
|
|
2274
|
+
return this.connection.transaction("write", async () => {
|
|
2275
|
+
const result = await execute();
|
|
2276
|
+
if (result.changed) {
|
|
2277
|
+
const record = reconciliation(result);
|
|
2278
|
+
if (record.operation !== operation) throw runtimeError("fatal", "Commit reconciliation operation mismatch");
|
|
2279
|
+
assertJcsBytes(record, EIDOS_RUNTIME_LIMITS.responseBytesMax, "Commit reconciliation");
|
|
2280
|
+
await barrier.prepare({
|
|
2281
|
+
fileID: result.fileId,
|
|
2282
|
+
baseRevision,
|
|
2283
|
+
commitRevision: result.revision,
|
|
2284
|
+
reconciliation: record
|
|
2285
|
+
}, context);
|
|
2286
|
+
}
|
|
2287
|
+
return result;
|
|
2288
|
+
});
|
|
2289
|
+
}
|
|
2290
|
+
assertWritable(operation) {
|
|
2291
|
+
if (!this.writable) throw runtimeError("unsupported", operation + " is unavailable read-only");
|
|
2292
|
+
}
|
|
2293
|
+
invoke(context, operation, allowClosing = false, requestPayload, accountResponse = true) {
|
|
2294
|
+
try {
|
|
2295
|
+
assertRequestContext(context);
|
|
2296
|
+
if (requestPayload !== void 0) assertJcsBytes(requestPayload, EIDOS_RUNTIME_LIMITS.requestBytesMax, "Runtime request");
|
|
2297
|
+
} catch (error) {
|
|
2298
|
+
return Promise.reject(mapRuntimeFailure(error));
|
|
2299
|
+
}
|
|
2300
|
+
if (this.state === "closed" || this.state === "fatal" || !allowClosing && this.state !== "open") return Promise.reject(runtimeError(this.state === "fatal" ? "fatal" : "closed", "Runtime is closed"));
|
|
2301
|
+
if (this.requestControls.has(context.requestId)) return Promise.reject(runtimeError("invalid-request", "requestId is already unresolved", { path: "/requestId" }));
|
|
2302
|
+
const acceptedAt = this.environment.clock.nowMilliseconds();
|
|
2303
|
+
const control = {
|
|
2304
|
+
cancelled: context.signal?.aborted === true,
|
|
2305
|
+
unsubscribe: () => void 0
|
|
2306
|
+
};
|
|
2307
|
+
control.unsubscribe = context.signal?.onAbort(() => {
|
|
2308
|
+
control.cancelled = true;
|
|
2309
|
+
}) ?? (() => void 0);
|
|
2310
|
+
this.requestControls.set(context.requestId, control);
|
|
2311
|
+
const execute = async () => {
|
|
2312
|
+
try {
|
|
2313
|
+
if (!allowClosing) this.checkControl(context, control, acceptedAt);
|
|
2314
|
+
const result = await operation();
|
|
2315
|
+
if (result !== void 0 && accountResponse) assertJcsBytes(result, EIDOS_RUNTIME_LIMITS.responseBytesMax, "Runtime response");
|
|
2316
|
+
return result;
|
|
2317
|
+
} catch (error) {
|
|
2318
|
+
const failure = mapRuntimeFailure(error);
|
|
2319
|
+
if (failure.code === "fatal" || failure.code === "unknown-commit") this.state = "fatal";
|
|
2320
|
+
throw failure;
|
|
2321
|
+
} finally {
|
|
2322
|
+
control.unsubscribe();
|
|
2323
|
+
this.requestControls.delete(context.requestId);
|
|
2324
|
+
}
|
|
2325
|
+
};
|
|
2326
|
+
const result = this.queue.then(execute, execute);
|
|
2327
|
+
this.queue = result.then(() => void 0, () => void 0);
|
|
2328
|
+
return result;
|
|
2329
|
+
}
|
|
2330
|
+
checkControl(context, control, acceptedAt) {
|
|
2331
|
+
if (control.cancelled) throw runtimeError("cancelled", "Request was cancelled", { retryable: true });
|
|
2332
|
+
const elapsed = this.environment.clock.nowMilliseconds() - acceptedAt;
|
|
2333
|
+
const budget = Math.min(context.deadlineMilliseconds ?? EIDOS_RUNTIME_LIMITS.foregroundTimeMsMax, EIDOS_RUNTIME_LIMITS.foregroundTimeMsMax);
|
|
2334
|
+
if (!Number.isFinite(elapsed) || elapsed < 0) {
|
|
2335
|
+
this.state = "fatal";
|
|
2336
|
+
throw runtimeError("fatal", "Monotonic clock moved backward");
|
|
2337
|
+
}
|
|
2338
|
+
if (elapsed > budget) throw runtimeError("deadline-exceeded", "Request deadline exceeded", { retryable: true });
|
|
2339
|
+
}
|
|
2340
|
+
encodeCursor(kind, value) {
|
|
2341
|
+
const payload = canonicalizeEidosFileJson({
|
|
2342
|
+
version: 1,
|
|
2343
|
+
epoch: this.epoch,
|
|
2344
|
+
kind,
|
|
2345
|
+
...value
|
|
2346
|
+
});
|
|
2347
|
+
return encodeURIComponent(payload + "." + cursorMac(this.cursorSecret, payload));
|
|
2348
|
+
}
|
|
2349
|
+
decodeCursor(cursor, kind) {
|
|
2350
|
+
try {
|
|
2351
|
+
const envelope = decodeURIComponent(cursor);
|
|
2352
|
+
const separator = envelope.lastIndexOf(".");
|
|
2353
|
+
if (separator < 1) throw new Error("cursor authentication missing");
|
|
2354
|
+
const payload = envelope.slice(0, separator);
|
|
2355
|
+
if (!constantTimeTextEqual(envelope.slice(separator + 1), cursorMac(this.cursorSecret, payload))) throw new Error("cursor authentication failed");
|
|
2356
|
+
const parsed = JSON.parse(payload);
|
|
2357
|
+
if (parsed.version !== 1 || parsed.epoch !== this.epoch || parsed.kind !== kind) throw new Error("cursor binding mismatch");
|
|
2358
|
+
return parsed;
|
|
2359
|
+
} catch {
|
|
2360
|
+
throw runtimeError("invalid-query", "Invalid or foreign cursor");
|
|
2361
|
+
}
|
|
2362
|
+
}
|
|
2363
|
+
};
|
|
2364
|
+
function fieldDescriptor(field) {
|
|
2365
|
+
const definition = field.type === "relation" ? relationDefinition(field) : field.type === "formula" ? {
|
|
2366
|
+
sourceText: String(field.property?.formula ?? ""),
|
|
2367
|
+
resultType: String(field.property?.displayType ?? "text")
|
|
2368
|
+
} : field.type === "lookup" ? {
|
|
2369
|
+
relationFieldId: String(field.property?.relationField ?? ""),
|
|
2370
|
+
targetFieldId: String(field.property?.targetField ?? ""),
|
|
2371
|
+
aggregate: String(field.property?.aggregate ?? "values"),
|
|
2372
|
+
distinctValues: field.property?.distinct === true
|
|
2373
|
+
} : void 0;
|
|
2374
|
+
return {
|
|
2375
|
+
object: "field",
|
|
2376
|
+
id: field.id,
|
|
2377
|
+
tableId: field.tableId,
|
|
2378
|
+
name: field.name,
|
|
2379
|
+
kind: field.type === "created-time" || field.type === "last-edited-time" ? "datetime" : field.type === "row-id" ? "text" : field.type,
|
|
2380
|
+
valueType: fieldValueType(field),
|
|
2381
|
+
systemRole: field.systemRole ?? null,
|
|
2382
|
+
nullable: field.nullable ?? false,
|
|
2383
|
+
position: String(field.position ?? 0),
|
|
2384
|
+
settings: objectValue(field.settings ?? {}),
|
|
2385
|
+
writable: fieldWritable(field),
|
|
2386
|
+
...definition ? { definition } : {}
|
|
2387
|
+
};
|
|
2388
|
+
}
|
|
2389
|
+
function fieldValueType(field) {
|
|
2390
|
+
if (field.systemRole === "row-id" || field.type === "row-id") return "row-id";
|
|
2391
|
+
if (field.systemRole === "created-time" || field.systemRole === "updated-time" || field.type === "created-time" || field.type === "last-edited-time") return "datetime";
|
|
2392
|
+
if (field.type === "formula") return String(field.property?.displayType ?? "text");
|
|
2393
|
+
if (field.type === "lookup") {
|
|
2394
|
+
const generated = field.property?.valueType;
|
|
2395
|
+
const generatedObject = generated && typeof generated === "object" ? generated : void 0;
|
|
2396
|
+
if (typeof generated === "string" || generatedObject?.kind === "list" && typeof generatedObject.element === "string") return generated;
|
|
2397
|
+
const display = String(field.property?.displayType ?? "text");
|
|
2398
|
+
if (field.property?.aggregate === "values") return {
|
|
2399
|
+
kind: "list",
|
|
2400
|
+
element: display === "file" ? "file-entry" : display === "relation" ? "row-id" : display === "multi-select" ? "select" : display
|
|
2401
|
+
};
|
|
2402
|
+
return display;
|
|
2403
|
+
}
|
|
2404
|
+
return field.type;
|
|
2405
|
+
}
|
|
2406
|
+
function csvImportFieldType(field) {
|
|
2407
|
+
if (field.isRecordLabel) return "record-label";
|
|
2408
|
+
return [
|
|
2409
|
+
"text",
|
|
2410
|
+
"number",
|
|
2411
|
+
"checkbox",
|
|
2412
|
+
"date",
|
|
2413
|
+
"datetime",
|
|
2414
|
+
"url"
|
|
2415
|
+
].includes(field.type) ? field.type : null;
|
|
2416
|
+
}
|
|
2417
|
+
function fieldWritable(field) {
|
|
2418
|
+
const relation = field.type === "relation" ? relationDefinition(field) : void 0;
|
|
2419
|
+
return !field.systemRole && field.type !== "formula" && field.type !== "lookup" && !(field.type === "relation" && relation?.direction === "inverse");
|
|
2420
|
+
}
|
|
2421
|
+
function relationDefinition(field) {
|
|
2422
|
+
if (field.type !== "relation" || !field.property) return void 0;
|
|
2423
|
+
const direction = field.property.direction === "inverse" ? "inverse" : "forward";
|
|
2424
|
+
if (direction === "inverse") return {
|
|
2425
|
+
direction,
|
|
2426
|
+
targetTableId: String(field.property.targetTableId),
|
|
2427
|
+
cardinality: "many",
|
|
2428
|
+
inverseOfFieldId: String(field.property.sourceFieldId)
|
|
2429
|
+
};
|
|
2430
|
+
return {
|
|
2431
|
+
direction,
|
|
2432
|
+
targetTableId: String(field.property.targetTableId),
|
|
2433
|
+
cardinality: field.property.cardinality === "one" ? "one" : "many",
|
|
2434
|
+
onDelete: [
|
|
2435
|
+
"restrict",
|
|
2436
|
+
"detach",
|
|
2437
|
+
"preserve"
|
|
2438
|
+
].includes(String(field.property.onDelete)) ? field.property.onDelete : "restrict"
|
|
2439
|
+
};
|
|
2440
|
+
}
|
|
2441
|
+
function normalizedRuntimeQuery(query) {
|
|
2442
|
+
return objectValue({
|
|
2443
|
+
...query.filter === void 0 ? {} : { filter: query.filter },
|
|
2444
|
+
...query.search === void 0 ? {} : { search: query.search },
|
|
2445
|
+
sort: (query.sort ?? []).map((sort) => ({
|
|
2446
|
+
fieldId: sort.fieldId,
|
|
2447
|
+
direction: sort.direction,
|
|
2448
|
+
nulls: sort.nulls ?? "last"
|
|
2449
|
+
}))
|
|
2450
|
+
});
|
|
2451
|
+
}
|
|
2452
|
+
function runtimeQueryFieldIds(query) {
|
|
2453
|
+
if (!query) return [];
|
|
2454
|
+
const ids = new Set([...query.search?.fields ?? [], ...(query.sort ?? []).map((sort) => sort.fieldId)]);
|
|
2455
|
+
const visit = (node) => {
|
|
2456
|
+
if (node.op === "and" || node.op === "or") {
|
|
2457
|
+
node.args.forEach(visit);
|
|
2458
|
+
return;
|
|
2459
|
+
}
|
|
2460
|
+
if (node.op === "not") {
|
|
2461
|
+
visit(node.arg);
|
|
2462
|
+
return;
|
|
2463
|
+
}
|
|
2464
|
+
if ("fieldId" in node) ids.add(node.fieldId);
|
|
2465
|
+
};
|
|
2466
|
+
if (query.filter) visit(query.filter);
|
|
2467
|
+
return [...ids];
|
|
2468
|
+
}
|
|
2469
|
+
function compatibilityFilterReferencesField(filter, fieldId) {
|
|
2470
|
+
if (!filter) return false;
|
|
2471
|
+
return filter.children.some((child) => child.type === "group" ? compatibilityFilterReferencesField(child, fieldId) : child.field === fieldId);
|
|
2472
|
+
}
|
|
2473
|
+
function knownViewLayoutReferencesField(layout, fieldId) {
|
|
2474
|
+
if (!layout) return false;
|
|
2475
|
+
if ([
|
|
2476
|
+
"cardFields",
|
|
2477
|
+
"fieldOrder",
|
|
2478
|
+
"hiddenFields"
|
|
2479
|
+
].some((key) => Array.isArray(layout[key]) && layout[key].includes(fieldId))) return true;
|
|
2480
|
+
if (layout.coverField === fieldId || layout.groupField === fieldId || layout.fieldWidths !== null && typeof layout.fieldWidths === "object" && !Array.isArray(layout.fieldWidths) && Object.prototype.hasOwnProperty.call(layout.fieldWidths, fieldId)) return true;
|
|
2481
|
+
return false;
|
|
2482
|
+
}
|
|
2483
|
+
function reverseRuntimeQuery(query, fields) {
|
|
2484
|
+
const sorts = (query.sort ?? []).map((sort) => ({
|
|
2485
|
+
...sort,
|
|
2486
|
+
direction: sort.direction === "asc" ? "desc" : "asc"
|
|
2487
|
+
}));
|
|
2488
|
+
const rowId = fields.find((field) => field.systemRole === "row-id" || field.type === "row-id");
|
|
2489
|
+
if (rowId && !sorts.some((sort) => sort.fieldId === rowId.id)) sorts.push({
|
|
2490
|
+
fieldId: rowId.id,
|
|
2491
|
+
direction: "desc",
|
|
2492
|
+
nulls: "last"
|
|
2493
|
+
});
|
|
2494
|
+
return {
|
|
2495
|
+
...query.filter === void 0 ? {} : { filter: query.filter },
|
|
2496
|
+
...query.search === void 0 ? {} : { search: query.search },
|
|
2497
|
+
sort: sorts
|
|
2498
|
+
};
|
|
2499
|
+
}
|
|
2500
|
+
function runtimeQueryToCompatibility(query) {
|
|
2501
|
+
return {
|
|
2502
|
+
...query.search ? { search: query.search.text } : {},
|
|
2503
|
+
...query.search ? { searchFields: [...query.search.fields] } : {},
|
|
2504
|
+
...query.filter ? { filter: runtimeFilterToCompatibility(query.filter) } : {},
|
|
2505
|
+
sorts: (query.sort ?? []).map((sort) => ({
|
|
2506
|
+
field: sort.fieldId,
|
|
2507
|
+
direction: sort.direction,
|
|
2508
|
+
nulls: sort.nulls ?? "last"
|
|
2509
|
+
}))
|
|
2510
|
+
};
|
|
2511
|
+
}
|
|
2512
|
+
function runtimeListElementType(type) {
|
|
2513
|
+
if (typeof type === "object") return type.element;
|
|
2514
|
+
if (type === "multi-select") return "select";
|
|
2515
|
+
if (type === "relation") return "row-id";
|
|
2516
|
+
if (type === "file") return "file-entry";
|
|
2517
|
+
}
|
|
2518
|
+
function assertRuntimeRowQuery(fields, query) {
|
|
2519
|
+
if (!query || typeof query !== "object" || Array.isArray(query)) throw runtimeError("invalid-query", "RowQuery must be an object");
|
|
2520
|
+
const byId = new Map(fields.map((field) => [field.id, field]));
|
|
2521
|
+
if (query.search !== void 0) {
|
|
2522
|
+
if (typeof query.search.text !== "string" || new TextEncoder().encode(query.search.text).byteLength > EIDOS_RUNTIME_LIMITS.searchBytesMax || !Array.isArray(query.search.fields) || query.search.fields.length < 1 || query.search.fields.length > EIDOS_RUNTIME_LIMITS.projectionFieldsMax || new Set(query.search.fields).size !== query.search.fields.length) throw runtimeError("invalid-query", "Search request is invalid");
|
|
2523
|
+
for (const fieldId of query.search.fields) {
|
|
2524
|
+
const field = byId.get(fieldId);
|
|
2525
|
+
const type = field ? fieldValueType(field) : void 0;
|
|
2526
|
+
if (!field || typeof type !== "string" || ![
|
|
2527
|
+
"text",
|
|
2528
|
+
"url",
|
|
2529
|
+
"select",
|
|
2530
|
+
"row-id"
|
|
2531
|
+
].includes(type)) throw runtimeError("invalid-query", "Search Field is not searchable", { fieldId });
|
|
2532
|
+
}
|
|
2533
|
+
}
|
|
2534
|
+
if (query.sort !== void 0) {
|
|
2535
|
+
if (!Array.isArray(query.sort) || query.sort.length > EIDOS_RUNTIME_LIMITS.sortFieldsMax || new Set(query.sort.map((sort) => sort.fieldId)).size !== query.sort.length) throw runtimeError("invalid-query", "Sort list is invalid");
|
|
2536
|
+
query.sort.forEach((sort, index) => {
|
|
2537
|
+
const field = byId.get(sort.fieldId);
|
|
2538
|
+
const type = field ? fieldValueType(field) : void 0;
|
|
2539
|
+
if (!field || typeof type !== "string" || ![
|
|
2540
|
+
"text",
|
|
2541
|
+
"number",
|
|
2542
|
+
"integer",
|
|
2543
|
+
"checkbox",
|
|
2544
|
+
"date",
|
|
2545
|
+
"datetime",
|
|
2546
|
+
"url",
|
|
2547
|
+
"select",
|
|
2548
|
+
"row-id"
|
|
2549
|
+
].includes(type) || !["asc", "desc"].includes(sort.direction) || sort.nulls !== void 0 && !["first", "last"].includes(sort.nulls) || type === "row-id" && index !== query.sort.length - 1) throw runtimeError("invalid-query", "Sort Field is invalid", { fieldId: sort.fieldId });
|
|
2550
|
+
});
|
|
2551
|
+
}
|
|
2552
|
+
let nodes = 0;
|
|
2553
|
+
const visit = (node, depth) => {
|
|
2554
|
+
nodes += 1;
|
|
2555
|
+
if (depth > EIDOS_RUNTIME_LIMITS.filterDepthMax || nodes > EIDOS_RUNTIME_LIMITS.filterNodesMax) throw runtimeError("resource-limit", "Filter complexity exceeds limits");
|
|
2556
|
+
if (!node || typeof node !== "object" || typeof node.op !== "string") throw runtimeError("invalid-query", "Filter node is invalid");
|
|
2557
|
+
if (node.op === "and" || node.op === "or") {
|
|
2558
|
+
if (!Array.isArray(node.args)) throw runtimeError("invalid-query", "Logical Filter args are invalid");
|
|
2559
|
+
node.args.forEach((child) => visit(child, depth + 1));
|
|
2560
|
+
return;
|
|
2561
|
+
}
|
|
2562
|
+
if (node.op === "not") {
|
|
2563
|
+
visit(node.arg, depth + 1);
|
|
2564
|
+
return;
|
|
2565
|
+
}
|
|
2566
|
+
if (!("fieldId" in node)) throw runtimeError("invalid-query", "Filter Field is required");
|
|
2567
|
+
const field = byId.get(node.fieldId);
|
|
2568
|
+
if (!field) throw runtimeError("invalid-query", "Filter Field does not exist", { fieldId: node.fieldId });
|
|
2569
|
+
const type = fieldValueType(field);
|
|
2570
|
+
if (node.op === "is-null" || node.op === "is-not-null") return;
|
|
2571
|
+
if (node.op === "contains" || node.op === "starts-with" || node.op === "ends-with") {
|
|
2572
|
+
if (typeof type !== "string" || ![
|
|
2573
|
+
"text",
|
|
2574
|
+
"url",
|
|
2575
|
+
"select",
|
|
2576
|
+
"row-id"
|
|
2577
|
+
].includes(type) || typeof node.value !== "string") throw runtimeError("invalid-query", "String Filter operand is invalid");
|
|
2578
|
+
return;
|
|
2579
|
+
}
|
|
2580
|
+
if (node.op === "has-any" || node.op === "has-all") {
|
|
2581
|
+
const elementType = runtimeListElementType(type);
|
|
2582
|
+
if (!elementType || !Array.isArray(node.values) || !node.values.every((value) => value !== null && filterValueMatchesType(value, elementType))) throw runtimeError("invalid-query", "List Filter operand is invalid");
|
|
2583
|
+
return;
|
|
2584
|
+
}
|
|
2585
|
+
if (node.op === "relation-has") {
|
|
2586
|
+
if (field.type !== "relation") throw runtimeError("invalid-query", "relation-has requires Relation");
|
|
2587
|
+
try {
|
|
2588
|
+
assertEidosFileUuid(node.rowId, "Row ID");
|
|
2589
|
+
} catch {
|
|
2590
|
+
throw runtimeError("invalid-query", "relation-has Row ID is invalid");
|
|
2591
|
+
}
|
|
2592
|
+
return;
|
|
2593
|
+
}
|
|
2594
|
+
if (node.op === "in") {
|
|
2595
|
+
if (!Array.isArray(node.values) || !node.values.every((value) => value !== null && filterValueMatchesType(value, type))) throw runtimeError("invalid-query", "in operands are invalid");
|
|
2596
|
+
return;
|
|
2597
|
+
}
|
|
2598
|
+
if (node.op === "between") {
|
|
2599
|
+
if (typeof type !== "string" || !isSortableType(type) || node.lower === null || node.upper === null || !filterValueMatchesType(node.lower, type) || !filterValueMatchesType(node.upper, type)) throw runtimeError("invalid-query", "between operands are invalid");
|
|
2600
|
+
return;
|
|
2601
|
+
}
|
|
2602
|
+
if (!("value" in node) || node.value === null || !filterValueMatchesType(node.value, type) || [
|
|
2603
|
+
"lt",
|
|
2604
|
+
"lte",
|
|
2605
|
+
"gt",
|
|
2606
|
+
"gte"
|
|
2607
|
+
].includes(node.op) && (typeof type !== "string" || !isSortableType(type))) throw runtimeError("invalid-query", "Filter operand type is invalid");
|
|
2608
|
+
};
|
|
2609
|
+
if (query.filter) visit(query.filter, 1);
|
|
2610
|
+
}
|
|
2611
|
+
function filterValueMatchesType(value, type) {
|
|
2612
|
+
if (type === "row-id") return typeof value === "string";
|
|
2613
|
+
return logicalValueMatchesType(value, type);
|
|
2614
|
+
}
|
|
2615
|
+
function isSortableType(type) {
|
|
2616
|
+
return [
|
|
2617
|
+
"text",
|
|
2618
|
+
"number",
|
|
2619
|
+
"integer",
|
|
2620
|
+
"checkbox",
|
|
2621
|
+
"date",
|
|
2622
|
+
"datetime",
|
|
2623
|
+
"url",
|
|
2624
|
+
"select",
|
|
2625
|
+
"row-id"
|
|
2626
|
+
].includes(type);
|
|
2627
|
+
}
|
|
2628
|
+
function logicalValueMatchesType(value, type) {
|
|
2629
|
+
if (typeof type === "object") return Array.isArray(value) && value.every((entry) => entry !== null && logicalValueMatchesType(entry, type.element));
|
|
2630
|
+
switch (type) {
|
|
2631
|
+
case "number": return typeof value === "number" && Number.isFinite(value);
|
|
2632
|
+
case "integer": return typeof value === "string" && /^(?:0|-[1-9][0-9]*|[1-9][0-9]*)$/.test(value);
|
|
2633
|
+
case "checkbox": return typeof value === "boolean";
|
|
2634
|
+
case "row-id":
|
|
2635
|
+
if (typeof value !== "string") return false;
|
|
2636
|
+
try {
|
|
2637
|
+
assertEidosFileUuid(value, "Row ID");
|
|
2638
|
+
return true;
|
|
2639
|
+
} catch {
|
|
2640
|
+
return false;
|
|
2641
|
+
}
|
|
2642
|
+
case "text":
|
|
2643
|
+
case "url":
|
|
2644
|
+
case "select":
|
|
2645
|
+
case "date":
|
|
2646
|
+
case "datetime": return typeof value === "string";
|
|
2647
|
+
case "multi-select": return Array.isArray(value) && value.every((entry) => typeof entry === "string");
|
|
2648
|
+
case "relation": return Array.isArray(value) && value.every((entry) => typeof entry === "string");
|
|
2649
|
+
case "file": try {
|
|
2650
|
+
assertEidosFileValues(value);
|
|
2651
|
+
return true;
|
|
2652
|
+
} catch {
|
|
2653
|
+
return false;
|
|
2654
|
+
}
|
|
2655
|
+
case "file-entry": try {
|
|
2656
|
+
assertEidosFileValues([value]);
|
|
2657
|
+
return true;
|
|
2658
|
+
} catch {
|
|
2659
|
+
return false;
|
|
2660
|
+
}
|
|
2661
|
+
case "json":
|
|
2662
|
+
if (typeof value !== "string") return false;
|
|
2663
|
+
try {
|
|
2664
|
+
return canonicalizeEidosFileJson(JSON.parse(value)) === value;
|
|
2665
|
+
} catch {
|
|
2666
|
+
return false;
|
|
2667
|
+
}
|
|
2668
|
+
}
|
|
2669
|
+
}
|
|
2670
|
+
function runtimeFilterToCompatibility(node) {
|
|
2671
|
+
const filter = runtimeFilterNodeToCompatibility(node);
|
|
2672
|
+
return filter.type === "group" ? filter : {
|
|
2673
|
+
type: "group",
|
|
2674
|
+
conjunction: "and",
|
|
2675
|
+
children: [filter]
|
|
2676
|
+
};
|
|
2677
|
+
}
|
|
2678
|
+
function runtimeFilterNodeToCompatibility(node) {
|
|
2679
|
+
const group = (conjunction, children) => ({
|
|
2680
|
+
type: "group",
|
|
2681
|
+
conjunction,
|
|
2682
|
+
children
|
|
2683
|
+
});
|
|
2684
|
+
if (node.op === "and" || node.op === "or") return group(node.op, node.args.map(runtimeFilterNodeToCompatibility));
|
|
2685
|
+
if (node.op === "not") {
|
|
2686
|
+
const filter = runtimeFilterNodeToCompatibility(node.arg);
|
|
2687
|
+
const nested = filter.type === "group" ? filter : group("and", [filter]);
|
|
2688
|
+
return {
|
|
2689
|
+
...nested,
|
|
2690
|
+
negated: nested.negated !== true
|
|
2691
|
+
};
|
|
2692
|
+
}
|
|
2693
|
+
if (node.op === "between") return group("and", [{
|
|
2694
|
+
type: "rule",
|
|
2695
|
+
field: node.fieldId,
|
|
2696
|
+
operator: "greater-than-or-equal",
|
|
2697
|
+
value: node.lower
|
|
2698
|
+
}, {
|
|
2699
|
+
type: "rule",
|
|
2700
|
+
field: node.fieldId,
|
|
2701
|
+
operator: "less-than-or-equal",
|
|
2702
|
+
value: node.upper
|
|
2703
|
+
}]);
|
|
2704
|
+
if (node.op === "has-all") return {
|
|
2705
|
+
type: "rule",
|
|
2706
|
+
field: node.fieldId,
|
|
2707
|
+
operator: "is-all-of",
|
|
2708
|
+
value: node.values
|
|
2709
|
+
};
|
|
2710
|
+
if (node.op === "in") return group("or", node.values.map((value) => ({
|
|
2711
|
+
type: "rule",
|
|
2712
|
+
field: node.fieldId,
|
|
2713
|
+
operator: "equals",
|
|
2714
|
+
value
|
|
2715
|
+
})));
|
|
2716
|
+
if (node.op === "is-null" || node.op === "is-not-null") return {
|
|
2717
|
+
type: "rule",
|
|
2718
|
+
field: node.fieldId,
|
|
2719
|
+
operator: node.op === "is-null" ? "equals" : "not-equals",
|
|
2720
|
+
value: null
|
|
2721
|
+
};
|
|
2722
|
+
const leaf = node;
|
|
2723
|
+
const operator = {
|
|
2724
|
+
eq: "equals",
|
|
2725
|
+
ne: "not-equals",
|
|
2726
|
+
lt: "less-than",
|
|
2727
|
+
lte: "less-than-or-equal",
|
|
2728
|
+
gt: "greater-than",
|
|
2729
|
+
gte: "greater-than-or-equal",
|
|
2730
|
+
contains: "contains",
|
|
2731
|
+
"starts-with": "starts-with",
|
|
2732
|
+
"ends-with": "ends-with",
|
|
2733
|
+
"has-any": "is-any-of",
|
|
2734
|
+
"relation-has": "is-any-of"
|
|
2735
|
+
}[leaf.op];
|
|
2736
|
+
if (!operator) throw runtimeError("invalid-query", "Filter operation is unsupported");
|
|
2737
|
+
const value = leaf.op === "in" || leaf.op === "has-any" ? leaf.values : leaf.op === "relation-has" ? [leaf.rowId] : "value" in leaf ? leaf.value : void 0;
|
|
2738
|
+
return {
|
|
2739
|
+
type: "rule",
|
|
2740
|
+
field: leaf.fieldId,
|
|
2741
|
+
operator,
|
|
2742
|
+
...value === void 0 ? {} : { value }
|
|
2743
|
+
};
|
|
2744
|
+
}
|
|
2745
|
+
function compatibilityFilterToRuntime(group, fields = []) {
|
|
2746
|
+
const node = {
|
|
2747
|
+
op: group.conjunction,
|
|
2748
|
+
args: group.children.map((child) => {
|
|
2749
|
+
if (child.type === "group") return compatibilityFilterToRuntime(child, fields);
|
|
2750
|
+
return compatibilityFilterRuleToRuntime(child, fields);
|
|
2751
|
+
})
|
|
2752
|
+
};
|
|
2753
|
+
return group.negated ? {
|
|
2754
|
+
op: "not",
|
|
2755
|
+
arg: node
|
|
2756
|
+
} : node;
|
|
2757
|
+
}
|
|
2758
|
+
function compatibilityFilterRuleToRuntime(rule, fields) {
|
|
2759
|
+
const field = fields.find((candidate) => candidate.id === rule.field);
|
|
2760
|
+
const type = field ? fieldValueType(field) : void 0;
|
|
2761
|
+
const list = type !== void 0 && runtimeListElementType(type) !== void 0;
|
|
2762
|
+
const values = Array.isArray(rule.value) ? rule.value : [rule.value ?? null];
|
|
2763
|
+
const scalarValue = values[0] ?? null;
|
|
2764
|
+
const equalityValue = list && Array.isArray(rule.value) ? rule.value : scalarValue;
|
|
2765
|
+
const requireValue = () => {
|
|
2766
|
+
if (scalarValue === null) throw runtimeError("corrupt-file", "Stored View filter operand is missing");
|
|
2767
|
+
return scalarValue;
|
|
2768
|
+
};
|
|
2769
|
+
const requireValues = () => {
|
|
2770
|
+
if (values.some((value) => value === null)) throw runtimeError("corrupt-file", "Stored View filter operand is null");
|
|
2771
|
+
return values;
|
|
2772
|
+
};
|
|
2773
|
+
const membership = () => list ? {
|
|
2774
|
+
op: "has-any",
|
|
2775
|
+
fieldId: rule.field,
|
|
2776
|
+
values: requireValues()
|
|
2777
|
+
} : {
|
|
2778
|
+
op: "in",
|
|
2779
|
+
fieldId: rule.field,
|
|
2780
|
+
values: requireValues()
|
|
2781
|
+
};
|
|
2782
|
+
switch (rule.operator) {
|
|
2783
|
+
case "is-empty": return list ? {
|
|
2784
|
+
op: "eq",
|
|
2785
|
+
fieldId: rule.field,
|
|
2786
|
+
value: []
|
|
2787
|
+
} : {
|
|
2788
|
+
op: "is-null",
|
|
2789
|
+
fieldId: rule.field
|
|
2790
|
+
};
|
|
2791
|
+
case "is-not-empty": return list ? {
|
|
2792
|
+
op: "ne",
|
|
2793
|
+
fieldId: rule.field,
|
|
2794
|
+
value: []
|
|
2795
|
+
} : {
|
|
2796
|
+
op: "is-not-null",
|
|
2797
|
+
fieldId: rule.field
|
|
2798
|
+
};
|
|
2799
|
+
case "equals": return equalityValue === null ? {
|
|
2800
|
+
op: "is-null",
|
|
2801
|
+
fieldId: rule.field
|
|
2802
|
+
} : {
|
|
2803
|
+
op: "eq",
|
|
2804
|
+
fieldId: rule.field,
|
|
2805
|
+
value: equalityValue
|
|
2806
|
+
};
|
|
2807
|
+
case "not-equals": return equalityValue === null ? {
|
|
2808
|
+
op: "is-not-null",
|
|
2809
|
+
fieldId: rule.field
|
|
2810
|
+
} : {
|
|
2811
|
+
op: "ne",
|
|
2812
|
+
fieldId: rule.field,
|
|
2813
|
+
value: equalityValue
|
|
2814
|
+
};
|
|
2815
|
+
case "less-than": return {
|
|
2816
|
+
op: "lt",
|
|
2817
|
+
fieldId: rule.field,
|
|
2818
|
+
value: requireValue()
|
|
2819
|
+
};
|
|
2820
|
+
case "less-than-or-equal": return {
|
|
2821
|
+
op: "lte",
|
|
2822
|
+
fieldId: rule.field,
|
|
2823
|
+
value: requireValue()
|
|
2824
|
+
};
|
|
2825
|
+
case "greater-than": return {
|
|
2826
|
+
op: "gt",
|
|
2827
|
+
fieldId: rule.field,
|
|
2828
|
+
value: requireValue()
|
|
2829
|
+
};
|
|
2830
|
+
case "greater-than-or-equal": return {
|
|
2831
|
+
op: "gte",
|
|
2832
|
+
fieldId: rule.field,
|
|
2833
|
+
value: requireValue()
|
|
2834
|
+
};
|
|
2835
|
+
case "contains": return list ? membership() : {
|
|
2836
|
+
op: "contains",
|
|
2837
|
+
fieldId: rule.field,
|
|
2838
|
+
value: String(requireValue())
|
|
2839
|
+
};
|
|
2840
|
+
case "not-contains": return {
|
|
2841
|
+
op: "not",
|
|
2842
|
+
arg: list ? membership() : {
|
|
2843
|
+
op: "contains",
|
|
2844
|
+
fieldId: rule.field,
|
|
2845
|
+
value: String(requireValue())
|
|
2846
|
+
}
|
|
2847
|
+
};
|
|
2848
|
+
case "starts-with": return {
|
|
2849
|
+
op: "starts-with",
|
|
2850
|
+
fieldId: rule.field,
|
|
2851
|
+
value: String(requireValue())
|
|
2852
|
+
};
|
|
2853
|
+
case "ends-with": return {
|
|
2854
|
+
op: "ends-with",
|
|
2855
|
+
fieldId: rule.field,
|
|
2856
|
+
value: String(requireValue())
|
|
2857
|
+
};
|
|
2858
|
+
case "is-any-of": return membership();
|
|
2859
|
+
case "is-all-of": return {
|
|
2860
|
+
op: "has-all",
|
|
2861
|
+
fieldId: rule.field,
|
|
2862
|
+
values: requireValues()
|
|
2863
|
+
};
|
|
2864
|
+
case "is-none-of": return {
|
|
2865
|
+
op: "not",
|
|
2866
|
+
arg: membership()
|
|
2867
|
+
};
|
|
2868
|
+
}
|
|
2869
|
+
}
|
|
2870
|
+
function compatibilityLogicalValue(value, type) {
|
|
2871
|
+
if (type === "integer" && (typeof value === "number" || typeof value === "bigint")) return String(value);
|
|
2872
|
+
if (typeof value === "string" && typeof type === "object") {
|
|
2873
|
+
const parsed = JSON.parse(value);
|
|
2874
|
+
if (!Array.isArray(parsed)) return null;
|
|
2875
|
+
return parsed.map((entry) => compatibilityLogicalValue(entry, type.element));
|
|
2876
|
+
}
|
|
2877
|
+
if (typeof value === "string" && typeof type === "string" && ["multi-select", "relation"].includes(type)) return JSON.parse(value);
|
|
2878
|
+
if (type === "file") try {
|
|
2879
|
+
return typeof value === "string" ? decodeEidosFileValues(value) : assertEidosFileValues(value);
|
|
2880
|
+
} catch {
|
|
2881
|
+
throw runtimeError("corrupt-file", "Stored File value is invalid");
|
|
2882
|
+
}
|
|
2883
|
+
if (typeof value === "string" && type === "file-entry") try {
|
|
2884
|
+
const parsed = JSON.parse(value);
|
|
2885
|
+
if (canonicalizeEidosFileJson(parsed) !== value) throw new Error();
|
|
2886
|
+
return assertEidosFileValues([parsed])[0];
|
|
2887
|
+
} catch {
|
|
2888
|
+
throw runtimeError("corrupt-file", "Stored File entry is invalid");
|
|
2889
|
+
}
|
|
2890
|
+
if (type === "file-entry") try {
|
|
2891
|
+
return assertEidosFileValues([value])[0];
|
|
2892
|
+
} catch {
|
|
2893
|
+
throw runtimeError("corrupt-file", "Stored File entry is invalid");
|
|
2894
|
+
}
|
|
2895
|
+
if (type === "checkbox" && typeof value === "number") return value === 1;
|
|
2896
|
+
if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean" || Array.isArray(value) || typeof value === "object" && value !== null) return value;
|
|
2897
|
+
return null;
|
|
2898
|
+
}
|
|
2899
|
+
function validationDiagnostic(issue, severity) {
|
|
2900
|
+
return {
|
|
2901
|
+
code: validationCode(issue.code),
|
|
2902
|
+
severity,
|
|
2903
|
+
message: issue.message,
|
|
2904
|
+
...issue.table ? { tableId: issue.table } : {}
|
|
2905
|
+
};
|
|
2906
|
+
}
|
|
2907
|
+
function validationCode(code) {
|
|
2908
|
+
return new Set([
|
|
2909
|
+
"file-not-sqlite",
|
|
2910
|
+
"file-identity-invalid",
|
|
2911
|
+
"file-format-unsupported",
|
|
2912
|
+
"file-feature-unsupported",
|
|
2913
|
+
"file-core-object-invalid",
|
|
2914
|
+
"file-metadata-invalid",
|
|
2915
|
+
"file-foreign-key-invalid",
|
|
2916
|
+
"file-physical-schema-invalid",
|
|
2917
|
+
"file-definition-invalid",
|
|
2918
|
+
"file-trigger-invalid",
|
|
2919
|
+
"file-index-invalid",
|
|
2920
|
+
"file-extension-invalid",
|
|
2921
|
+
"file-cell-invalid",
|
|
2922
|
+
"file-json-invalid",
|
|
2923
|
+
"file-reference-invalid",
|
|
2924
|
+
"file-unresolved-relation",
|
|
2925
|
+
"file-integrity-invalid"
|
|
2926
|
+
]).has(code) ? code : "file-core-object-invalid";
|
|
2927
|
+
}
|
|
2928
|
+
function objectValue(value) {
|
|
2929
|
+
return JSON.parse(canonicalizeEidosFileJson(value));
|
|
2930
|
+
}
|
|
2931
|
+
function simulatedSystemFields(tableId, tableName) {
|
|
2932
|
+
return [
|
|
2933
|
+
[
|
|
2934
|
+
"row-id",
|
|
2935
|
+
"row-id",
|
|
2936
|
+
"_id",
|
|
2937
|
+
-3
|
|
2938
|
+
],
|
|
2939
|
+
[
|
|
2940
|
+
"created-time",
|
|
2941
|
+
"created-time",
|
|
2942
|
+
"_created_at",
|
|
2943
|
+
-2
|
|
2944
|
+
],
|
|
2945
|
+
[
|
|
2946
|
+
"updated-time",
|
|
2947
|
+
"last-edited-time",
|
|
2948
|
+
"_updated_at",
|
|
2949
|
+
-1
|
|
2950
|
+
]
|
|
2951
|
+
].map(([role, type, name, position]) => ({
|
|
2952
|
+
id: `${tableId}:system:${role}`,
|
|
2953
|
+
tableId,
|
|
2954
|
+
name: String(name),
|
|
2955
|
+
type,
|
|
2956
|
+
tableName,
|
|
2957
|
+
tableColumnName: String(name),
|
|
2958
|
+
physicalName: String(name),
|
|
2959
|
+
systemRole: role,
|
|
2960
|
+
nullable: false,
|
|
2961
|
+
isRecordLabel: false,
|
|
2962
|
+
position: Number(position),
|
|
2963
|
+
settings: {},
|
|
2964
|
+
property: {},
|
|
2965
|
+
storageCodec: "scalar",
|
|
2966
|
+
valueKind: "system",
|
|
2967
|
+
isHidden: true,
|
|
2968
|
+
isDerived: false,
|
|
2969
|
+
sourceTableColumnName: null,
|
|
2970
|
+
dependsOn: null
|
|
2971
|
+
}));
|
|
2972
|
+
}
|
|
2973
|
+
function simulatedNewField(tableId, tableName, field, sequence) {
|
|
2974
|
+
const id = `${tableId}:new-field:${sequence}:${field.clientKey}`;
|
|
2975
|
+
let property = field.settings ? { ...field.settings } : {};
|
|
2976
|
+
if (field.kind === "formula") {
|
|
2977
|
+
const definition = field.definition;
|
|
2978
|
+
if (!definition || !("sourceText" in definition) || !("resultType" in definition)) throw runtimeError("invalid-request", "Formula Field requires definition");
|
|
2979
|
+
property = {
|
|
2980
|
+
...property,
|
|
2981
|
+
formula: definition.sourceText,
|
|
2982
|
+
displayType: definition.resultType
|
|
2983
|
+
};
|
|
2984
|
+
} else if (field.kind === "lookup") {
|
|
2985
|
+
const definition = field.definition;
|
|
2986
|
+
if (!definition || !("relationFieldId" in definition) || !("targetFieldId" in definition) || !("aggregate" in definition)) throw runtimeError("invalid-request", "Lookup Field requires definition");
|
|
2987
|
+
property = {
|
|
2988
|
+
...property,
|
|
2989
|
+
relationField: definition.relationFieldId,
|
|
2990
|
+
targetField: definition.targetFieldId,
|
|
2991
|
+
aggregate: definition.aggregate,
|
|
2992
|
+
distinct: definition.distinctValues,
|
|
2993
|
+
displayType: "text"
|
|
2994
|
+
};
|
|
2995
|
+
} else if (field.kind === "relation") {
|
|
2996
|
+
const definition = field.definition;
|
|
2997
|
+
if (!definition || !("direction" in definition)) throw runtimeError("invalid-request", "Relation Field requires definition");
|
|
2998
|
+
property = simulatedRelationProperty(definition);
|
|
2999
|
+
}
|
|
3000
|
+
const virtual = field.kind === "formula" || field.kind === "lookup" || field.kind === "relation" && property.direction === "inverse";
|
|
3001
|
+
const list = ["multi-select", "file"].includes(field.kind);
|
|
3002
|
+
return {
|
|
3003
|
+
id,
|
|
3004
|
+
tableId,
|
|
3005
|
+
name: field.name,
|
|
3006
|
+
type: field.kind,
|
|
3007
|
+
tableName,
|
|
3008
|
+
tableColumnName: virtual ? id : field.name,
|
|
3009
|
+
physicalName: virtual ? null : field.name,
|
|
3010
|
+
systemRole: null,
|
|
3011
|
+
nullable: field.kind === "formula" || field.kind === "lookup" ? true : list || field.kind === "relation" ? false : field.nullable !== false,
|
|
3012
|
+
isRecordLabel: false,
|
|
3013
|
+
position: Number(field.position),
|
|
3014
|
+
settings: field.settings ?? {},
|
|
3015
|
+
property,
|
|
3016
|
+
storageCodec: field.kind === "relation" ? "relation" : list || field.kind === "lookup" && property.aggregate === "values" ? "json_array" : "scalar",
|
|
3017
|
+
valueKind: virtual ? "derived" : field.kind === "relation" ? "relation" : "source",
|
|
3018
|
+
isHidden: false,
|
|
3019
|
+
isDerived: virtual,
|
|
3020
|
+
sourceTableColumnName: field.kind === "relation" && property.direction === "inverse" ? String(property.sourceFieldId ?? "") : null,
|
|
3021
|
+
dependsOn: null
|
|
3022
|
+
};
|
|
3023
|
+
}
|
|
3024
|
+
function simulatedRelationProperty(definition) {
|
|
3025
|
+
return definition.direction === "forward" ? {
|
|
3026
|
+
direction: "forward",
|
|
3027
|
+
targetTableId: definition.targetTableId,
|
|
3028
|
+
cardinality: definition.cardinality,
|
|
3029
|
+
onDelete: definition.onDelete
|
|
3030
|
+
} : {
|
|
3031
|
+
direction: "inverse",
|
|
3032
|
+
targetTableId: definition.targetTableId,
|
|
3033
|
+
sourceFieldId: definition.inverseOfFieldId,
|
|
3034
|
+
cardinality: "many"
|
|
3035
|
+
};
|
|
3036
|
+
}
|
|
3037
|
+
function asciiNoCase(value) {
|
|
3038
|
+
return value.replace(/[A-Z]/g, (character) => character.toLowerCase());
|
|
3039
|
+
}
|
|
3040
|
+
function highestSchemaClassification(left, right) {
|
|
3041
|
+
const rank = {
|
|
3042
|
+
"metadata-only": 0,
|
|
3043
|
+
"lossless-rewrite": 1,
|
|
3044
|
+
"explicit-lossy": 2,
|
|
3045
|
+
forbidden: 3
|
|
3046
|
+
};
|
|
3047
|
+
return rank[left] >= rank[right] ? left : right;
|
|
3048
|
+
}
|
|
3049
|
+
function assertConnectionCapabilities(connection) {
|
|
3050
|
+
const capabilities = connection.capabilities();
|
|
3051
|
+
if (capabilities.adapterVersion !== "1.0" || !capabilities.json1 || !capabilities.returning || !capabilities.strict || !capabilities.int64 || !capabilities.scalarFunctions || !capabilities.snapshot) throw runtimeError("adapter-error", "ConnectionPort does not satisfy EA-Connection-1.0");
|
|
3052
|
+
}
|
|
3053
|
+
function assertFactoryContext(context, environment) {
|
|
3054
|
+
if (context.cancellation.cancelled()) throw runtimeError("cancelled", "Runtime factory was cancelled", { retryable: true });
|
|
3055
|
+
if (context.deadlineMilliseconds !== void 0) assertPositiveSafeInteger(context.deadlineMilliseconds, "deadlineMilliseconds");
|
|
3056
|
+
checkedNowInstant(environment);
|
|
3057
|
+
const monotonic = environment.clock.nowMilliseconds();
|
|
3058
|
+
if (!Number.isFinite(monotonic) || monotonic < 0) throw runtimeError("adapter-error", "ClockPort.nowMilliseconds must be non-negative and finite");
|
|
3059
|
+
}
|
|
3060
|
+
function checkedNowInstant(environment) {
|
|
3061
|
+
const instant = environment.clock.nowInstant();
|
|
3062
|
+
if (!isCanonicalEidosFileInstant(instant)) throw runtimeError("adapter-error", "ClockPort.nowInstant returned a non-canonical instant");
|
|
3063
|
+
return instant;
|
|
3064
|
+
}
|
|
3065
|
+
function uuidGenerator(environment) {
|
|
3066
|
+
return new EidosUuidV7Generator(() => checkedNowInstant(environment), (length) => environment.entropy.randomBytes(length).slice());
|
|
3067
|
+
}
|
|
3068
|
+
function assertRequestContext(context) {
|
|
3069
|
+
if (typeof context.requestId !== "string" || context.requestId.length === 0 || context.requestId.includes("\0") || new TextEncoder().encode(context.requestId).byteLength > 128) throw runtimeError("invalid-request", "Invalid requestId", { path: "/requestId" });
|
|
3070
|
+
if (context.deadlineMilliseconds !== void 0) assertPositiveSafeInteger(context.deadlineMilliseconds, "deadlineMilliseconds");
|
|
3071
|
+
}
|
|
3072
|
+
function assertPositiveSafeInteger(value, label) {
|
|
3073
|
+
if (!Number.isSafeInteger(value) || value < 1) throw runtimeError("invalid-request", label + " must be a positive JSON safe integer");
|
|
3074
|
+
}
|
|
3075
|
+
function assertLimit(value, maximum, label) {
|
|
3076
|
+
if (!Number.isSafeInteger(value) || value < 1 || value > maximum) throw runtimeError("invalid-request", label + " must be between 1 and " + maximum);
|
|
3077
|
+
}
|
|
3078
|
+
function assertProjection(projection) {
|
|
3079
|
+
if (projection.fields.length > EIDOS_RUNTIME_LIMITS.projectionFieldsMax || new Set(projection.fields).size !== projection.fields.length || new Set(projection.resolveRelations).size !== projection.resolveRelations.length || projection.resolveRelations.some((fieldId) => !projection.fields.includes(fieldId))) throw runtimeError("invalid-request", "Invalid ProjectionSpec");
|
|
3080
|
+
}
|
|
3081
|
+
function assertCurrentRevision(expected, current) {
|
|
3082
|
+
parseRevision(expected, "revision");
|
|
3083
|
+
if (expected !== current) throw runtimeError("stale-revision", "File revision has changed", {
|
|
3084
|
+
retryable: true,
|
|
3085
|
+
currentRevision: current
|
|
3086
|
+
});
|
|
3087
|
+
}
|
|
3088
|
+
function parseRevision(value, label = "revision") {
|
|
3089
|
+
if (!/^(?:0|[1-9][0-9]*)$/.test(value)) throw runtimeError("invalid-request", label + " must be canonical non-negative int64 decimal");
|
|
3090
|
+
const parsed = BigInt(value);
|
|
3091
|
+
if (parsed > 9223372036854775807n) throw runtimeError("invalid-request", label + " exceeds int64");
|
|
3092
|
+
return parsed;
|
|
3093
|
+
}
|
|
3094
|
+
function taggedText(value) {
|
|
3095
|
+
if (value?.tag !== "text") throw runtimeError("corrupt-file", "Expected SQLite TEXT storage class");
|
|
3096
|
+
return value.value;
|
|
3097
|
+
}
|
|
3098
|
+
function taggedInteger(value) {
|
|
3099
|
+
if (value?.tag !== "integer") throw runtimeError("corrupt-file", "Expected SQLite INTEGER storage class");
|
|
3100
|
+
return value.value;
|
|
3101
|
+
}
|
|
3102
|
+
async function projectionHash(projection) {
|
|
3103
|
+
return hashJson(projection);
|
|
3104
|
+
}
|
|
3105
|
+
async function hashJson(value) {
|
|
3106
|
+
const bytes = new TextEncoder().encode(canonicalizeEidosFileJson(value));
|
|
3107
|
+
const digest = await globalThis.crypto.subtle.digest("SHA-256", bytes);
|
|
3108
|
+
return Array.from(new Uint8Array(digest), (value) => value.toString(16).padStart(2, "0")).join("");
|
|
3109
|
+
}
|
|
3110
|
+
const UINT64_MASK = (1n << 64n) - 1n;
|
|
3111
|
+
function cursorMac(key, payload) {
|
|
3112
|
+
const message = new TextEncoder().encode(payload);
|
|
3113
|
+
const read64 = (bytes, offset) => {
|
|
3114
|
+
let value = 0n;
|
|
3115
|
+
for (let index = 0; index < 8; index += 1) value |= BigInt(bytes[offset + index] ?? 0) << BigInt(index * 8);
|
|
3116
|
+
return value;
|
|
3117
|
+
};
|
|
3118
|
+
const rotate = (value, bits) => (value << bits | value >> 64n - bits) & UINT64_MASK;
|
|
3119
|
+
let v0 = 8317987319222330741n ^ read64(key, 0);
|
|
3120
|
+
let v1 = 7237128888997146477n ^ read64(key, 8);
|
|
3121
|
+
let v2 = 7816392313619706465n ^ read64(key, 0);
|
|
3122
|
+
let v3 = 8387220255154660723n ^ read64(key, 8);
|
|
3123
|
+
const round = () => {
|
|
3124
|
+
v0 = v0 + v1 & UINT64_MASK;
|
|
3125
|
+
v1 = rotate(v1, 13n) ^ v0;
|
|
3126
|
+
v0 = rotate(v0, 32n);
|
|
3127
|
+
v2 = v2 + v3 & UINT64_MASK;
|
|
3128
|
+
v3 = rotate(v3, 16n) ^ v2;
|
|
3129
|
+
v0 = v0 + v3 & UINT64_MASK;
|
|
3130
|
+
v3 = rotate(v3, 21n) ^ v0;
|
|
3131
|
+
v2 = v2 + v1 & UINT64_MASK;
|
|
3132
|
+
v1 = rotate(v1, 17n) ^ v2;
|
|
3133
|
+
v2 = rotate(v2, 32n);
|
|
3134
|
+
};
|
|
3135
|
+
let offset = 0;
|
|
3136
|
+
while (offset + 8 <= message.byteLength) {
|
|
3137
|
+
const block = read64(message, offset);
|
|
3138
|
+
v3 ^= block;
|
|
3139
|
+
round();
|
|
3140
|
+
round();
|
|
3141
|
+
v0 ^= block;
|
|
3142
|
+
offset += 8;
|
|
3143
|
+
}
|
|
3144
|
+
let finalBlock = BigInt(message.byteLength) << 56n;
|
|
3145
|
+
for (let index = 0; offset + index < message.byteLength; index += 1) finalBlock |= BigInt(message[offset + index]) << BigInt(index * 8);
|
|
3146
|
+
v3 ^= finalBlock;
|
|
3147
|
+
round();
|
|
3148
|
+
round();
|
|
3149
|
+
v0 ^= finalBlock;
|
|
3150
|
+
v2 ^= 255n;
|
|
3151
|
+
round();
|
|
3152
|
+
round();
|
|
3153
|
+
round();
|
|
3154
|
+
round();
|
|
3155
|
+
return ((v0 ^ v1 ^ v2 ^ v3) & UINT64_MASK).toString(16).padStart(16, "0");
|
|
3156
|
+
}
|
|
3157
|
+
function constantTimeTextEqual(left, right) {
|
|
3158
|
+
let difference = left.length ^ right.length;
|
|
3159
|
+
const length = Math.max(left.length, right.length);
|
|
3160
|
+
for (let index = 0; index < length; index += 1) difference |= (left.charCodeAt(index) || 0) ^ (right.charCodeAt(index) || 0);
|
|
3161
|
+
return difference === 0;
|
|
3162
|
+
}
|
|
3163
|
+
function parseSignedInt64(value, label) {
|
|
3164
|
+
if (!/^(?:0|-[1-9][0-9]*|[1-9][0-9]*)$/.test(value) || value === "-0") throw runtimeError("invalid-request", label + " must be canonical signed int64 decimal");
|
|
3165
|
+
const parsed = BigInt(value);
|
|
3166
|
+
if (parsed < -9223372036854775808n || parsed > 9223372036854775807n) throw runtimeError("invalid-request", label + " exceeds signed int64");
|
|
3167
|
+
return parsed;
|
|
3168
|
+
}
|
|
3169
|
+
function int64ToSafeNumber(value, label) {
|
|
3170
|
+
const parsed = parseSignedInt64(value, label);
|
|
3171
|
+
if (parsed < BigInt(Number.MIN_SAFE_INTEGER) || parsed > BigInt(Number.MAX_SAFE_INTEGER)) throw runtimeError("resource-limit", label + " cannot be represented by this compatibility engine");
|
|
3172
|
+
return Number(parsed);
|
|
3173
|
+
}
|
|
3174
|
+
function quoteSqlIdentifier(value) {
|
|
3175
|
+
return "\"" + value.replace(/"/g, "\"\"") + "\"";
|
|
3176
|
+
}
|
|
3177
|
+
function newFieldInput(field) {
|
|
3178
|
+
const base = {
|
|
3179
|
+
name: field.name,
|
|
3180
|
+
nullable: field.nullable
|
|
3181
|
+
};
|
|
3182
|
+
if (field.kind === "formula") {
|
|
3183
|
+
const definition = field.definition;
|
|
3184
|
+
return {
|
|
3185
|
+
name: field.name,
|
|
3186
|
+
type: "formula",
|
|
3187
|
+
property: {
|
|
3188
|
+
...field.settings ?? {},
|
|
3189
|
+
formula: definition.sourceText,
|
|
3190
|
+
displayType: definition.resultType
|
|
3191
|
+
}
|
|
3192
|
+
};
|
|
3193
|
+
}
|
|
3194
|
+
if (field.kind === "lookup") {
|
|
3195
|
+
const definition = field.definition;
|
|
3196
|
+
return {
|
|
3197
|
+
name: field.name,
|
|
3198
|
+
type: "lookup",
|
|
3199
|
+
property: {
|
|
3200
|
+
...field.settings ?? {},
|
|
3201
|
+
relationField: definition.relationFieldId,
|
|
3202
|
+
targetField: definition.targetFieldId,
|
|
3203
|
+
aggregate: definition.aggregate,
|
|
3204
|
+
displayType: "text",
|
|
3205
|
+
distinct: definition.distinctValues
|
|
3206
|
+
}
|
|
3207
|
+
};
|
|
3208
|
+
}
|
|
3209
|
+
if (field.kind === "relation") {
|
|
3210
|
+
const definition = field.definition;
|
|
3211
|
+
return {
|
|
3212
|
+
name: field.name,
|
|
3213
|
+
type: "relation",
|
|
3214
|
+
property: definition.direction === "forward" ? {
|
|
3215
|
+
...field.settings ?? {},
|
|
3216
|
+
direction: "forward",
|
|
3217
|
+
targetTableId: definition.targetTableId,
|
|
3218
|
+
cardinality: definition.cardinality,
|
|
3219
|
+
onDelete: definition.onDelete
|
|
3220
|
+
} : {
|
|
3221
|
+
...field.settings ?? {},
|
|
3222
|
+
direction: "inverse",
|
|
3223
|
+
targetTableId: definition.targetTableId,
|
|
3224
|
+
sourceFieldId: definition.inverseOfFieldId,
|
|
3225
|
+
cardinality: "many"
|
|
3226
|
+
}
|
|
3227
|
+
};
|
|
3228
|
+
}
|
|
3229
|
+
return {
|
|
3230
|
+
...base,
|
|
3231
|
+
type: field.kind,
|
|
3232
|
+
property: field.settings
|
|
3233
|
+
};
|
|
3234
|
+
}
|
|
3235
|
+
function runtimeError(code, message, members = {}) {
|
|
3236
|
+
return {
|
|
3237
|
+
code,
|
|
3238
|
+
message: message.slice(0, 4096),
|
|
3239
|
+
retryable: false,
|
|
3240
|
+
...members
|
|
3241
|
+
};
|
|
3242
|
+
}
|
|
3243
|
+
function mapRuntimeFailure(error) {
|
|
3244
|
+
if (isRuntimeError(error)) return error;
|
|
3245
|
+
if (error instanceof EidosFileError) {
|
|
3246
|
+
const code = {
|
|
3247
|
+
"invalid-sqlite": "corrupt-file",
|
|
3248
|
+
"not-eidos-file": "corrupt-file",
|
|
3249
|
+
"unsupported-version": "unsupported",
|
|
3250
|
+
"unsupported-feature": "unsupported",
|
|
3251
|
+
"invalid-schema": "corrupt-file",
|
|
3252
|
+
"invalid-formula": "invalid-formula",
|
|
3253
|
+
"invalid-value": "invalid-value",
|
|
3254
|
+
"constraint-conflict": "constraint",
|
|
3255
|
+
"dependency-cycle": "cycle",
|
|
3256
|
+
"stale-revision": "stale-revision",
|
|
3257
|
+
"file-conflict": "conflict",
|
|
3258
|
+
"permission-denied": "forbidden",
|
|
3259
|
+
"query-limit": "resource-limit",
|
|
3260
|
+
"resource-limit": "resource-limit",
|
|
3261
|
+
"unsupported-format": "unsupported",
|
|
3262
|
+
"invalid-identifier": "invalid-request",
|
|
3263
|
+
"invalid-query": "invalid-query",
|
|
3264
|
+
"invalid-range": "invalid-request",
|
|
3265
|
+
"invalid-csv": "invalid-value",
|
|
3266
|
+
"protected-field": "forbidden",
|
|
3267
|
+
"protected-view": "forbidden",
|
|
3268
|
+
"relation-in-use": "constraint",
|
|
3269
|
+
"formula-in-use": "constraint",
|
|
3270
|
+
"lookup-in-use": "constraint",
|
|
3271
|
+
"table-not-found": "not-found",
|
|
3272
|
+
"row-not-found": "not-found",
|
|
3273
|
+
"field-not-found": "not-found",
|
|
3274
|
+
"view-not-found": "not-found",
|
|
3275
|
+
"file-exists": "already-exists",
|
|
3276
|
+
"file-not-found": "not-found"
|
|
3277
|
+
}[error.code];
|
|
3278
|
+
return runtimeError(code, error.message, { retryable: code === "stale-revision" || code === "conflict" });
|
|
3279
|
+
}
|
|
3280
|
+
if (typeof error === "object" && error !== null && "code" in error && "fatal" in error) {
|
|
3281
|
+
const adapter = error;
|
|
3282
|
+
return runtimeError(adapter.code === "commit-outcome-unknown" ? "unknown-commit" : adapter.fatal ? "fatal" : adapter.code === "busy" ? "busy" : adapter.code === "cancelled" ? "cancelled" : adapter.code === "deadline-exceeded" ? "deadline-exceeded" : adapter.code === "resource-limit" ? "resource-limit" : adapter.code === "corrupt" || adapter.code === "not-a-database" ? "corrupt-file" : adapter.code === "adapter-closed" ? "closed" : "adapter-error", adapter.message ?? "Adapter failure", adapter.code === "commit-outcome-unknown" ? {
|
|
3283
|
+
retryable: false,
|
|
3284
|
+
details: { reconciliationRequired: true }
|
|
3285
|
+
} : { retryable: adapter.retryable === true });
|
|
3286
|
+
}
|
|
3287
|
+
return runtimeError("fatal", error instanceof Error ? error.message : "Unknown Runtime failure");
|
|
3288
|
+
}
|
|
3289
|
+
function isRuntimeError(value) {
|
|
3290
|
+
return typeof value === "object" && value !== null && typeof value.code === "string" && typeof value.message === "string" && typeof value.retryable === "boolean";
|
|
3291
|
+
}
|
|
3292
|
+
function binaryCompare(left, right) {
|
|
3293
|
+
const a = new TextEncoder().encode(left);
|
|
3294
|
+
const b = new TextEncoder().encode(right);
|
|
3295
|
+
for (let index = 0; index < Math.min(a.length, b.length); index += 1) if (a[index] !== b[index]) return a[index] - b[index];
|
|
3296
|
+
return a.length - b.length;
|
|
3297
|
+
}
|
|
3298
|
+
function assertAggregateItems(items, fieldsById) {
|
|
3299
|
+
const keys = /* @__PURE__ */ new Set();
|
|
3300
|
+
for (const item of items) {
|
|
3301
|
+
if (!item.key || keys.has(item.key)) throw runtimeError("invalid-request", "Aggregate keys must be non-empty and unique");
|
|
3302
|
+
keys.add(item.key);
|
|
3303
|
+
if (item.op !== "count-all" && !fieldsById.has(item.fieldId)) throw runtimeError("not-found", "Aggregate Field not found", { fieldId: item.fieldId });
|
|
3304
|
+
if (item.op === "distinct-values" && (!Number.isInteger(item.limit) || item.limit < 1 || item.limit > EIDOS_RUNTIME_LIMITS.pageSizeMax)) throw runtimeError("invalid-request", "Distinct value limit exceeds pageSizeMax", { fieldId: item.fieldId });
|
|
3305
|
+
}
|
|
3306
|
+
}
|
|
3307
|
+
function aggregateResultsForRows(items, fieldsById, rows) {
|
|
3308
|
+
const rowIdOrder = [...rows].sort((left, right) => binaryCompare(left.id, right.id));
|
|
3309
|
+
return items.map((item) => {
|
|
3310
|
+
if (item.op === "count-all") return {
|
|
3311
|
+
key: item.key,
|
|
3312
|
+
value: String(rows.length)
|
|
3313
|
+
};
|
|
3314
|
+
return aggregateItemResult(item, fieldValueType(fieldsById.get(item.fieldId)), rowIdOrder.map((row) => row.fields[item.fieldId] ?? null));
|
|
3315
|
+
});
|
|
3316
|
+
}
|
|
3317
|
+
function groupIdentity(values, types) {
|
|
3318
|
+
return canonicalizeEidosFileJson(values.map((value, index) => value === null ? "null" : typedAggregateKey(value, types[index])));
|
|
3319
|
+
}
|
|
3320
|
+
function compareGroupKeys(left, right, types) {
|
|
3321
|
+
for (let index = 0; index < types.length; index += 1) {
|
|
3322
|
+
const a = left[index] ?? null;
|
|
3323
|
+
const b = right[index] ?? null;
|
|
3324
|
+
if (a === null || b === null) {
|
|
3325
|
+
if (a === b) continue;
|
|
3326
|
+
return a === null ? -1 : 1;
|
|
3327
|
+
}
|
|
3328
|
+
const comparison = compareAggregateValues(a, b, types[index]);
|
|
3329
|
+
if (comparison !== 0) return comparison;
|
|
3330
|
+
}
|
|
3331
|
+
return 0;
|
|
3332
|
+
}
|
|
3333
|
+
function groupCursorSource(request) {
|
|
3334
|
+
return objectValue({
|
|
3335
|
+
tableId: request.tableId,
|
|
3336
|
+
query: normalizedRuntimeQuery(request.query),
|
|
3337
|
+
groupBy: request.groupBy,
|
|
3338
|
+
aggregates: request.aggregates,
|
|
3339
|
+
projection: request.projection,
|
|
3340
|
+
groupLimit: 1,
|
|
3341
|
+
rowsPerGroup: 1
|
|
3342
|
+
});
|
|
3343
|
+
}
|
|
3344
|
+
function queryForGroup(query, groupBy, key) {
|
|
3345
|
+
const rules = groupBy.map((fieldId, index) => key[index] === null ? {
|
|
3346
|
+
op: "is-null",
|
|
3347
|
+
fieldId
|
|
3348
|
+
} : {
|
|
3349
|
+
op: "eq",
|
|
3350
|
+
fieldId,
|
|
3351
|
+
value: key[index]
|
|
3352
|
+
});
|
|
3353
|
+
return {
|
|
3354
|
+
...query,
|
|
3355
|
+
filter: query.filter === void 0 ? {
|
|
3356
|
+
op: "and",
|
|
3357
|
+
args: rules
|
|
3358
|
+
} : {
|
|
3359
|
+
op: "and",
|
|
3360
|
+
args: [query.filter, ...rules]
|
|
3361
|
+
}
|
|
3362
|
+
};
|
|
3363
|
+
}
|
|
3364
|
+
function groupBindingHash(request) {
|
|
3365
|
+
return hashJson({
|
|
3366
|
+
tableId: request.tableId,
|
|
3367
|
+
query: normalizedRuntimeQuery(request.query),
|
|
3368
|
+
groupBy: request.groupBy,
|
|
3369
|
+
aggregates: request.aggregates,
|
|
3370
|
+
projection: request.projection
|
|
3371
|
+
});
|
|
3372
|
+
}
|
|
3373
|
+
function aggregateItemResult(item, valueType, values) {
|
|
3374
|
+
const nonNull = values.filter((value) => value !== null);
|
|
3375
|
+
const distinct = /* @__PURE__ */ new Map();
|
|
3376
|
+
for (const value of nonNull) {
|
|
3377
|
+
const key = typedAggregateKey(value, valueType);
|
|
3378
|
+
if (!distinct.has(key)) distinct.set(key, value);
|
|
3379
|
+
}
|
|
3380
|
+
if (item.op === "count") return {
|
|
3381
|
+
key: item.key,
|
|
3382
|
+
value: String(nonNull.length)
|
|
3383
|
+
};
|
|
3384
|
+
if (item.op === "distinct-count") return {
|
|
3385
|
+
key: item.key,
|
|
3386
|
+
value: String(distinct.size)
|
|
3387
|
+
};
|
|
3388
|
+
if (item.op === "distinct-values") {
|
|
3389
|
+
const ordered = Array.from(distinct.values()).sort((left, right) => compareAggregateValues(left, right, valueType));
|
|
3390
|
+
return {
|
|
3391
|
+
key: item.key,
|
|
3392
|
+
values: ordered.slice(0, item.limit).map((value) => publicAggregateValue(value, valueType)),
|
|
3393
|
+
truncated: ordered.length > item.limit
|
|
3394
|
+
};
|
|
3395
|
+
}
|
|
3396
|
+
const sortable = typeof valueType === "string" && isSortableType(valueType);
|
|
3397
|
+
const numeric = valueType === "integer" || valueType === "number";
|
|
3398
|
+
if ((item.op === "sum" || item.op === "average") && !numeric) throw runtimeError("invalid-request", `${item.op} requires an Integer or Number Field`, { fieldId: item.fieldId });
|
|
3399
|
+
if ((item.op === "min" || item.op === "max") && !sortable) throw runtimeError("invalid-request", `${item.op} requires a sortable scalar Field`, { fieldId: item.fieldId });
|
|
3400
|
+
const sum = () => {
|
|
3401
|
+
if (nonNull.length === 0) return null;
|
|
3402
|
+
if (valueType === "integer") {
|
|
3403
|
+
const total = nonNull.reduce((accumulator, value) => accumulator + BigInt(value), 0n);
|
|
3404
|
+
if (total < -9223372036854775808n || total > 9223372036854775807n) throw runtimeError("constraint", "Integer aggregate sum exceeds int64", { fieldId: item.fieldId });
|
|
3405
|
+
return String(total);
|
|
3406
|
+
}
|
|
3407
|
+
const result = pairwiseBinary64(nonNull.map(Number));
|
|
3408
|
+
if (!Number.isFinite(result)) throw runtimeError("constraint", "Number aggregate is non-finite", { fieldId: item.fieldId });
|
|
3409
|
+
return Object.is(result, -0) ? 0 : result;
|
|
3410
|
+
};
|
|
3411
|
+
const average = () => {
|
|
3412
|
+
if (nonNull.length === 0) return null;
|
|
3413
|
+
const result = valueType === "integer" ? rationalToBinary64(nonNull.reduce((accumulator, value) => accumulator + BigInt(value), 0n), BigInt(nonNull.length)) : pairwiseBinary64(nonNull.map(Number)) / nonNull.length;
|
|
3414
|
+
if (!Number.isFinite(result)) throw runtimeError("constraint", "Average aggregate is non-finite", { fieldId: item.fieldId });
|
|
3415
|
+
return Object.is(result, -0) ? 0 : result;
|
|
3416
|
+
};
|
|
3417
|
+
const extreme = (kind) => {
|
|
3418
|
+
if (nonNull.length === 0) return null;
|
|
3419
|
+
let selected = nonNull[0];
|
|
3420
|
+
for (const candidate of nonNull.slice(1)) {
|
|
3421
|
+
const compared = compareAggregateValues(candidate, selected, valueType);
|
|
3422
|
+
if (kind === "min" && compared < 0 || kind === "max" && compared > 0) selected = candidate;
|
|
3423
|
+
}
|
|
3424
|
+
return publicAggregateValue(selected, valueType);
|
|
3425
|
+
};
|
|
3426
|
+
if (item.op === "sum") return {
|
|
3427
|
+
key: item.key,
|
|
3428
|
+
value: sum()
|
|
3429
|
+
};
|
|
3430
|
+
if (item.op === "average") return {
|
|
3431
|
+
key: item.key,
|
|
3432
|
+
value: average()
|
|
3433
|
+
};
|
|
3434
|
+
if (item.op === "min" || item.op === "max") return {
|
|
3435
|
+
key: item.key,
|
|
3436
|
+
value: extreme(item.op)
|
|
3437
|
+
};
|
|
3438
|
+
const statistics = {
|
|
3439
|
+
rows: String(values.length),
|
|
3440
|
+
nulls: String(values.length - nonNull.length),
|
|
3441
|
+
distinct: String(distinct.size)
|
|
3442
|
+
};
|
|
3443
|
+
if (sortable) {
|
|
3444
|
+
statistics.min = extreme("min");
|
|
3445
|
+
statistics.max = extreme("max");
|
|
3446
|
+
}
|
|
3447
|
+
if (numeric) {
|
|
3448
|
+
statistics.sum = sum();
|
|
3449
|
+
statistics.average = average();
|
|
3450
|
+
}
|
|
3451
|
+
return {
|
|
3452
|
+
key: item.key,
|
|
3453
|
+
statistics
|
|
3454
|
+
};
|
|
3455
|
+
}
|
|
3456
|
+
function publicAggregateValue(value, type) {
|
|
3457
|
+
if (value === null) return null;
|
|
3458
|
+
if (type === "integer") return String(value);
|
|
3459
|
+
if (type === "checkbox") return value === true || value === 1 || value === 1n;
|
|
3460
|
+
if (typeof value === "number") return Object.is(value, -0) ? 0 : value;
|
|
3461
|
+
if (typeof value === "string" || typeof value === "boolean" || Array.isArray(value)) return value;
|
|
3462
|
+
throw runtimeError("fatal", "Aggregate produced an invalid logical value");
|
|
3463
|
+
}
|
|
3464
|
+
function typedAggregateKey(value, type) {
|
|
3465
|
+
if (type === "integer") return `integer:${String(value)}`;
|
|
3466
|
+
if (type === "number") return `number:${Object.is(value, -0) ? 0 : Number(value)}`;
|
|
3467
|
+
if (type === "checkbox") return `checkbox:${value === true || value === 1 ? 1 : 0}`;
|
|
3468
|
+
if (typeof type === "object" || [
|
|
3469
|
+
"file",
|
|
3470
|
+
"multi-select",
|
|
3471
|
+
"relation",
|
|
3472
|
+
"file-entry"
|
|
3473
|
+
].includes(String(type))) try {
|
|
3474
|
+
return `structured:${canonicalizeEidosFileJson(value)}`;
|
|
3475
|
+
} catch {
|
|
3476
|
+
throw runtimeError("fatal", "Aggregate encountered an invalid structured value");
|
|
3477
|
+
}
|
|
3478
|
+
return `${String(type)}:${String(value)}`;
|
|
3479
|
+
}
|
|
3480
|
+
function compareAggregateValues(left, right, type) {
|
|
3481
|
+
if (type === "integer") {
|
|
3482
|
+
const a = BigInt(left);
|
|
3483
|
+
const b = BigInt(right);
|
|
3484
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
3485
|
+
}
|
|
3486
|
+
if (type === "number") {
|
|
3487
|
+
const a = Number(left);
|
|
3488
|
+
const b = Number(right);
|
|
3489
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
3490
|
+
}
|
|
3491
|
+
if (type === "checkbox") {
|
|
3492
|
+
const a = left === true || left === 1 || left === 1n;
|
|
3493
|
+
return a === (right === true || right === 1 || right === 1n) ? 0 : a ? 1 : -1;
|
|
3494
|
+
}
|
|
3495
|
+
return binaryCompare(String(left), String(right));
|
|
3496
|
+
}
|
|
3497
|
+
function pairwiseBinary64(values) {
|
|
3498
|
+
let level = values.map((value) => Object.is(value, -0) ? 0 : value);
|
|
3499
|
+
while (level.length > 1) {
|
|
3500
|
+
const next = [];
|
|
3501
|
+
for (let index = 0; index < level.length; index += 2) next.push(index + 1 < level.length ? level[index] + level[index + 1] : level[index]);
|
|
3502
|
+
level = next;
|
|
3503
|
+
}
|
|
3504
|
+
return level[0] ?? 0;
|
|
3505
|
+
}
|
|
3506
|
+
function rationalToBinary64(numerator, denominator) {
|
|
3507
|
+
if (denominator <= 0n) throw runtimeError("fatal", "Invalid rational denominator");
|
|
3508
|
+
if (numerator === 0n) return 0;
|
|
3509
|
+
const negative = numerator < 0n;
|
|
3510
|
+
const absolute = negative ? -numerator : numerator;
|
|
3511
|
+
let exponent = bitLength(absolute) - bitLength(denominator);
|
|
3512
|
+
if (exponent >= 0) {
|
|
3513
|
+
if (absolute < denominator << BigInt(exponent)) exponent -= 1;
|
|
3514
|
+
} else if (absolute << BigInt(-exponent) < denominator) exponent -= 1;
|
|
3515
|
+
if (exponent > 1023) return negative ? -Infinity : Infinity;
|
|
3516
|
+
let result;
|
|
3517
|
+
if (exponent >= -1022) {
|
|
3518
|
+
const shift = 52 - exponent;
|
|
3519
|
+
let significand = divideRoundTiesEven(shift >= 0 ? absolute << BigInt(shift) : absolute, shift >= 0 ? denominator : denominator << BigInt(-shift));
|
|
3520
|
+
if (significand === 9007199254740992n) {
|
|
3521
|
+
significand >>= 1n;
|
|
3522
|
+
exponent += 1;
|
|
3523
|
+
if (exponent > 1023) return negative ? -Infinity : Infinity;
|
|
3524
|
+
}
|
|
3525
|
+
result = Number(significand) * 2 ** (exponent - 52);
|
|
3526
|
+
} else {
|
|
3527
|
+
const significand = divideRoundTiesEven(absolute << 1074n, denominator);
|
|
3528
|
+
result = Number(significand) * 2 ** -1074;
|
|
3529
|
+
}
|
|
3530
|
+
return negative ? -result : result;
|
|
3531
|
+
}
|
|
3532
|
+
function divideRoundTiesEven(numerator, denominator) {
|
|
3533
|
+
const quotient = numerator / denominator;
|
|
3534
|
+
const doubled = numerator % denominator * 2n;
|
|
3535
|
+
return doubled > denominator || doubled === denominator && quotient % 2n !== 0n ? quotient + 1n : quotient;
|
|
3536
|
+
}
|
|
3537
|
+
function bitLength(value) {
|
|
3538
|
+
return value.toString(2).length;
|
|
3539
|
+
}
|
|
3540
|
+
function assertJcsBytes(value, maximum, label) {
|
|
3541
|
+
let bytes;
|
|
3542
|
+
try {
|
|
3543
|
+
bytes = jcsByteLength(value);
|
|
3544
|
+
} catch {
|
|
3545
|
+
throw runtimeError("invalid-request", `${label} is not canonical JSON data`);
|
|
3546
|
+
}
|
|
3547
|
+
if (bytes > maximum) throw runtimeError("resource-limit", `${label} exceeds its byte limit`);
|
|
3548
|
+
}
|
|
3549
|
+
function jcsByteLength(value) {
|
|
3550
|
+
return new TextEncoder().encode(canonicalizeEidosFileJson(value)).byteLength;
|
|
3551
|
+
}
|
|
3552
|
+
//#endregion
|
|
3553
|
+
export { AdapterTransportRuntimeClient, AdapterTransportServer, ConnectionPortEidosFileConnection, EIDOS_ADAPTER_TRANSPORT_LIMITS, EIDOS_FILE_APPLICATION_ID, EIDOS_FILE_CSV_INFERENCE_ROW_COUNT, EIDOS_FILE_CSV_PREVIEW_ROW_COUNT, EIDOS_FILE_EXTENSION, EIDOS_FILE_FEATURES_TABLE, EIDOS_FILE_FIELDS_TABLE, EIDOS_FILE_FORMAT, EIDOS_FILE_FORMAT_VERSION, EIDOS_FILE_FORMAT_VERSION_TEXT, EIDOS_FILE_FORMULA_FIELDS_TABLE, EIDOS_FILE_FORMULA_FIELD_FUNCTION_NAMES, EIDOS_FILE_FORMULA_FUNCTION_NAMES, EIDOS_FILE_LOOKUP_FIELDS_TABLE, EIDOS_FILE_META_TABLE, EIDOS_FILE_MIME_TYPE, EIDOS_FILE_RELATION_FIELDS_TABLE, EIDOS_FILE_REQUIRED_TABLES, EIDOS_FILE_SCHEMA_SQL, EIDOS_FILE_SCHEMA_VERSION, EIDOS_FILE_SYSTEM_COLUMNS, EIDOS_FILE_TABLES_TABLE, EIDOS_FILE_VIEWS_TABLE, EIDOS_RUNTIME_LIMITS, EidosAdapterError, EidosFileError, EidosFileHandlerRegistry, EidosFileHostError, EidosFileRuntime, EidosFileRuntimeDataSource, EidosFileSession, EidosRuntimeService, EidosUuidV7Generator, MUTABLE_BASE_FIELD_TYPES, MemoryByteSource, Runtime, SQLITE_HEADER, SQLiteWasmConnectionPort, assertEidosFileColumnName, assertEidosFileDisplayName, assertEidosFileRowQuery, assertEidosFileSelectOptions, assertEidosFileTableId, assertEidosFileTableName, assertEidosFileUuid, assertEidosFileValues, assertInt64Decimal, assertUnicodeString, cancellationPortFromSignal, canonicalizeEidosFileJson, compileEidosFileFormula, compileEidosFileFormulaFields, compileEidosFileFormulaSource, compileEidosFileRowQuery, configureEidosFileConnection, createEidosFileCsvImportPlan, createEidosFileCsvRowEncoder, createEidosFileIdentifier, createEidosFileUuid, currentEidosFileInstant, decodeEidosFileAttachmentPaths, decodeEidosFileJsonArray, decodeEidosFileMultiSelectValues, decodeEidosFileRelationDisplay, decodeEidosFileRelationIds, decodeEidosFileStringArray, decodeEidosFileValues, eidosFileColumnStatLabel, eidosFileColumnStatTypesForField, eidosFileConversionCanReusePhysicalColumn, eidosFileConversionTargetNullable, eidosFileCsvExportHeader, eidosFileCsvRowToEidosFileRow, eidosFileFieldConversionMayRequireLossyConfirmation, eidosFileFieldDisplayValues, eidosFileFieldStoresJsonArray, eidosFileFormulaReferences, eidosFileLookupAggregateSupportsTarget, eidosFileLookupDisplayType, eidosFileLookupElementType, eidosFileLookupStorageCodec, eidosFileLookupTargetDisplayType, eidosFileLookupTargetValueType, eidosFileLookupValueType, eidosFileRowQueryAffectedByFieldChanges, eidosFileRowQueryPredicateColumns, eidosFileSnapshot, eidosFileSortExpression, eidosFileTypeHandler, eidosFileUriClass, encodeEidosFileAttachmentPaths, encodeEidosFileCsvRecord, encodeEidosFileJsonArray, encodeEidosFileMultiSelectValues, encodeEidosFileRelationIds, encodeEidosFileValues, hasEidosFileSqliteHeader, importEidosFileCsv, incrementEidosFileRevision, initializeEidosFileSchema, isCanonicalEidosFileDate, isCanonicalEidosFileInstant, isCanonicalEidosFileJson, isEidosFileReservedTableName, isEidosFileUuid, isMutableEidosFileFieldType, isSafeEidosFileUri, nativeToSqlValue, normalizeEidosFileAttachmentPath, normalizeEidosFileDate, normalizeEidosFileFilter, normalizeEidosFileInstant, normalizeEidosFileRowQuery, normalizeEidosFileSorts, parseEidosFileCsvRows, parseEidosFileJson, parseEidosFileSelectOptions, parseNonNegativeInt64, planCanonicalFieldConversion, planEidosFileCsvImport, planEidosFileFieldConversion, prepareEidosFileCsvImport, quoteIdentifier, recommendedEidosFileConversionPolicies, removeEidosFileFilterField, rewriteEidosFileFormulaFieldReferences, setEidosFileMetadata, sqlValueToNative, transportFitsHostLimit, validateAdapterCarrier, validateAdapterEnvelope, validateEidosFile };
|
|
3554
|
+
|
|
3555
|
+
//# sourceMappingURL=index.mjs.map
|