@altertable/data-app 0.77.0 → 0.78.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/CONTRIBUTING.md +3 -0
- package/REFERENCE.md +45 -37
- package/dist/chunks/contract-gu1rDsV0.js +1588 -0
- package/dist/chunks/contract-gu1rDsV0.js.map +1 -0
- package/dist/chunks/data-client-BMdL73Ab.js +122 -0
- package/dist/chunks/data-client-BMdL73Ab.js.map +1 -0
- package/dist/chunks/{date-time-range-value-CnlTr1U1.js → date-time-range-value-D-XfqVTT.js} +21 -24
- package/dist/chunks/date-time-range-value-D-XfqVTT.js.map +1 -0
- package/dist/chunks/{diagnostics-Dyj-rUsY.js → diagnostics-CX9SsU79.js} +240 -63
- package/dist/chunks/diagnostics-CX9SsU79.js.map +1 -0
- package/dist/chunks/record-Cr4Uk1K3.js +9 -0
- package/dist/chunks/record-Cr4Uk1K3.js.map +1 -0
- package/dist/client/index.js +4 -101
- package/dist/client/index.js.map +1 -1
- package/dist/core/appearance.js +3 -5
- package/dist/core/appearance.js.map +1 -1
- package/dist/core/config.js +11 -3
- package/dist/core/config.js.map +1 -1
- package/dist/core/contract.js +3 -4
- package/dist/core/format.js +1 -1
- package/dist/internal.js +63 -11
- package/dist/internal.js.map +1 -1
- package/dist/types/chunks/contract.d.ts +254 -19
- package/dist/types/chunks/data-client.d.ts +46 -0
- package/dist/types/chunks/date-time-range-value.d.ts +28 -2
- package/dist/types/chunks/index.d.ts +65 -80
- package/dist/types/client/index.d.ts +4 -3
- package/dist/types/core/config.d.ts +10 -4
- package/dist/types/core/contract.d.ts +3 -5
- package/dist/types/core/format.d.ts +1 -2
- package/dist/types/index.d.ts +4 -4
- package/dist/types/internal.d.ts +25 -6
- package/docs/app-authoring.md +13 -9
- package/docs/client.md +14 -53
- package/docs/contract.md +35 -15
- package/docs/data-context.md +5 -2
- package/docs/diagnostics.md +16 -23
- package/docs/embed.md +60 -25
- package/docs/filters.md +33 -57
- package/docs/hosted-apps.md +9 -9
- package/docs/layout.md +21 -0
- package/docs/local-data-apps.md +5 -6
- package/docs/react-embed.md +44 -2
- package/docs/react.md +1 -2
- package/docs/server-bun.md +8 -5
- package/docs/stories-and-export.md +4 -4
- package/docs/styling.md +2 -1
- package/docs/ui.md +12 -3
- package/docs/views.md +12 -11
- package/docs/widgets.md +3 -3
- package/docs/worker.md +1 -1
- package/package.json +1 -5
- package/dist/chunks/contract-BQsFReRp.js +0 -780
- package/dist/chunks/contract-BQsFReRp.js.map +0 -1
- package/dist/chunks/contract-VhY9V9wd.js +0 -806
- package/dist/chunks/contract-VhY9V9wd.js.map +0 -1
- package/dist/chunks/date-time-range-value-CnlTr1U1.js.map +0 -1
- package/dist/chunks/diagnostics-Dyj-rUsY.js.map +0 -1
- package/dist/chunks/operation-BVRM-A5Y.js +0 -134
- package/dist/chunks/operation-BVRM-A5Y.js.map +0 -1
- package/dist/chunks/request-body-H1qPmmTN.js +0 -50
- package/dist/chunks/request-body-H1qPmmTN.js.map +0 -1
- package/dist/server/index.js +0 -90
- package/dist/server/index.js.map +0 -1
- package/dist/types/chunks/contract2.d.ts +0 -221
- package/dist/types/chunks/date-time-range.d.ts +0 -29
- package/dist/types/server/index.d.ts +0 -17
- package/docs/server.md +0 -41
|
@@ -1,780 +0,0 @@
|
|
|
1
|
-
import { i as parseDateTimeRange, l as invariant, o as serializeDateTimeRange } from "./date-time-range-value-CnlTr1U1.js";
|
|
2
|
-
//#region src/core/keys.ts
|
|
3
|
-
/** Separator for opaque composite keys, independent of URL and DOM ID formats. */
|
|
4
|
-
const KEY_SEPARATOR = ":";
|
|
5
|
-
//#endregion
|
|
6
|
-
//#region src/core/dimension.ts
|
|
7
|
-
const allSelection = { kind: "all" };
|
|
8
|
-
/** Selections are equal when they keep the same members, in any order. */
|
|
9
|
-
function selectionKey(value) {
|
|
10
|
-
return value.kind === "all" ? "all" : [value.kind, ...value.members.map(dimensionMemberKey).sort()].join(":");
|
|
11
|
-
}
|
|
12
|
-
function dimensionMemberKey(member) {
|
|
13
|
-
return member.kind === "missing" ? "missing" : [
|
|
14
|
-
"value",
|
|
15
|
-
typeof member.value,
|
|
16
|
-
member.value
|
|
17
|
-
].join(":");
|
|
18
|
-
}
|
|
19
|
-
function dimensionFilter(config) {
|
|
20
|
-
return {
|
|
21
|
-
...createDimensionFilter(config),
|
|
22
|
-
optionsSource: "fixed"
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
/** Shared fixed-option and query-backed selection behavior. */
|
|
26
|
-
function createDimensionFilter(config, dynamic = false) {
|
|
27
|
-
const { key, label, selectionMode, valueType } = config;
|
|
28
|
-
const options = config.options ?? [];
|
|
29
|
-
const optionKeys = options.map((option) => dimensionMemberKey({
|
|
30
|
-
kind: "value",
|
|
31
|
-
value: option.value
|
|
32
|
-
}));
|
|
33
|
-
invariant(!!key && options.length <= 200 && new Set(optionKeys).size === options.length, "Dimension options need a key, at most 200 distinct values.");
|
|
34
|
-
invariant(options.every((option) => typeof option.value === valueType && !!option.label.trim() && (option.count === void 0 || Number.isSafeInteger(option.count) && option.count >= 0)), "Dimension options need typed values, labels, and nonnegative counts.");
|
|
35
|
-
const allowed = new Set(optionKeys);
|
|
36
|
-
const maxSelected = config.maxSelected ?? (selectionMode === "single" ? 1 : 20);
|
|
37
|
-
invariant(Number.isInteger(maxSelected) && maxSelected >= 1 && maxSelected <= 50 && (selectionMode !== "single" || maxSelected === 1), "Dimension selection limit is invalid.");
|
|
38
|
-
const allowMissing = config.allowMissing ?? false;
|
|
39
|
-
const defaultValue = config.defaultValue ?? allSelection;
|
|
40
|
-
function validMember(member) {
|
|
41
|
-
if (!member || typeof member !== "object") return false;
|
|
42
|
-
if (member.kind === "missing") return allowMissing;
|
|
43
|
-
if (member.kind !== "value" || typeof member.value !== valueType) return false;
|
|
44
|
-
if (!dynamic) return allowed.has(dimensionMemberKey(member));
|
|
45
|
-
return typeof member.value === "number" ? Number.isFinite(member.value) : member.value.length <= 100;
|
|
46
|
-
}
|
|
47
|
-
function valid(value) {
|
|
48
|
-
if (!value || typeof value !== "object") return false;
|
|
49
|
-
if (value.kind === "all") return true;
|
|
50
|
-
if (value.kind !== "include" && !(config.allowExclusion && value.kind === "exclude") || !Array.isArray(value.members) || !value.members.length || value.members.length > maxSelected) return false;
|
|
51
|
-
if (!value.members.every(validMember)) return false;
|
|
52
|
-
const ids = value.members.map(dimensionMemberKey);
|
|
53
|
-
return new Set(ids).size === ids.length;
|
|
54
|
-
}
|
|
55
|
-
invariant(valid(defaultValue), `Invalid default selection for ${label}.`);
|
|
56
|
-
/** The default selection has no URL value; every member is explicit when it differs. */
|
|
57
|
-
function encode(value) {
|
|
58
|
-
if (same(value, defaultValue)) return null;
|
|
59
|
-
return value.kind === "all" ? JSON.stringify({ kind: "all" }) : JSON.stringify({
|
|
60
|
-
kind: value.kind,
|
|
61
|
-
members: value.members.map((member) => member.kind === "missing" ? ["m"] : ["v", member.value])
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
function same(left, right) {
|
|
65
|
-
return selectionKey(left) === selectionKey(right);
|
|
66
|
-
}
|
|
67
|
-
function parse(raw) {
|
|
68
|
-
if (raw === null) return defaultValue;
|
|
69
|
-
if (raw.length > 4096) return defaultValue;
|
|
70
|
-
try {
|
|
71
|
-
const parsed = JSON.parse(raw);
|
|
72
|
-
const tokens = Array.isArray(parsed) ? {
|
|
73
|
-
kind: "include",
|
|
74
|
-
members: parsed
|
|
75
|
-
} : parsed;
|
|
76
|
-
if (tokens?.kind === "all") return allSelection;
|
|
77
|
-
if (!tokens || !Array.isArray(tokens.members)) return defaultValue;
|
|
78
|
-
const value = {
|
|
79
|
-
kind: tokens.kind,
|
|
80
|
-
members: tokens.members.map((token) => Array.isArray(token) && token.length === 1 && token[0] === "m" ? { kind: "missing" } : Array.isArray(token) && token.length === 2 && token[0] === "v" ? {
|
|
81
|
-
kind: "value",
|
|
82
|
-
value: token[1]
|
|
83
|
-
} : { kind: "invalid" })
|
|
84
|
-
};
|
|
85
|
-
return valid(value) ? value : defaultValue;
|
|
86
|
-
} catch {
|
|
87
|
-
return defaultValue;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
return {
|
|
91
|
-
kind: "dimension",
|
|
92
|
-
optionsSource: dynamic ? "query" : "fixed",
|
|
93
|
-
presentation: config.presentation,
|
|
94
|
-
label,
|
|
95
|
-
valueType,
|
|
96
|
-
selectionMode,
|
|
97
|
-
options,
|
|
98
|
-
allowMissing,
|
|
99
|
-
allowExclusion: config.allowExclusion ?? false,
|
|
100
|
-
maxSelected,
|
|
101
|
-
urlKeys: [key],
|
|
102
|
-
defaultValue,
|
|
103
|
-
clearValue: allSelection,
|
|
104
|
-
history: config.history ?? "push",
|
|
105
|
-
valid,
|
|
106
|
-
same,
|
|
107
|
-
read(params) {
|
|
108
|
-
return parse(params.get(key));
|
|
109
|
-
},
|
|
110
|
-
write(value) {
|
|
111
|
-
return { [key]: encode(value) };
|
|
112
|
-
},
|
|
113
|
-
describe(value, suppliedOptions = options) {
|
|
114
|
-
return value.kind === "all" ? `All ${label.toLocaleLowerCase()}` : `${value.kind === "exclude" ? "Exclude: " : ""}${value.members.map((member) => member.kind === "missing" ? "Missing" : suppliedOptions.find((option) => option.value === member.value)?.label ?? String(member.value)).join(", ")}`;
|
|
115
|
-
}
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
/** Server-side parser; call from the operation input parser before building SQL. */
|
|
119
|
-
function parseDimensionSelection(value, filter) {
|
|
120
|
-
invariant(filter.valid(value), `Invalid ${filter.label} selection.`);
|
|
121
|
-
return value;
|
|
122
|
-
}
|
|
123
|
-
/** Validate bounded facet results before showing them in a picker. */
|
|
124
|
-
function parseFacetOptions(value, filter) {
|
|
125
|
-
invariant(Array.isArray(value) && value.length <= 200, "Facet options exceed their bound.");
|
|
126
|
-
const options = [];
|
|
127
|
-
const seen = /* @__PURE__ */ new Set();
|
|
128
|
-
for (const item of value) {
|
|
129
|
-
invariant(item && typeof item === "object", "Invalid facet option.");
|
|
130
|
-
const option = item;
|
|
131
|
-
invariant(typeof option.value === filter.valueType && filter.valid({
|
|
132
|
-
kind: "include",
|
|
133
|
-
members: [{
|
|
134
|
-
kind: "value",
|
|
135
|
-
value: option.value
|
|
136
|
-
}]
|
|
137
|
-
}) && typeof option.label === "string" && !!option.label.trim() && option.label.length <= 100 && (option.count === void 0 || Number.isSafeInteger(option.count) && option.count >= 0), "Invalid facet option.");
|
|
138
|
-
const id = dimensionMemberKey({
|
|
139
|
-
kind: "value",
|
|
140
|
-
value: option.value
|
|
141
|
-
});
|
|
142
|
-
invariant(!seen.has(id), "Duplicate facet option.");
|
|
143
|
-
seen.add(id);
|
|
144
|
-
options.push(option);
|
|
145
|
-
}
|
|
146
|
-
return options;
|
|
147
|
-
}
|
|
148
|
-
/** Central SQL literal encoding for transports without bound parameters. Column is an allowlisted identifier. */
|
|
149
|
-
function dimensionPredicate(column, selection, allowedColumns) {
|
|
150
|
-
invariant(allowedColumns.includes(column) && /^[A-Za-z_][A-Za-z_0-9]*(?:\.[A-Za-z_][A-Za-z_0-9]*)*$/.test(column), "Invalid dimension column.");
|
|
151
|
-
if (selection.kind === "all") return "";
|
|
152
|
-
invariant(selection.members.length > 0, "Empty dimension inclusion is invalid.");
|
|
153
|
-
const values = selection.members.filter((member) => member.kind === "value");
|
|
154
|
-
const missing = selection.members.some((member) => member.kind === "missing");
|
|
155
|
-
const literals = values.map(({ value }) => {
|
|
156
|
-
if (typeof value === "number") {
|
|
157
|
-
invariant(Number.isFinite(value), "Invalid numeric dimension value.");
|
|
158
|
-
return String(value);
|
|
159
|
-
}
|
|
160
|
-
const text = value;
|
|
161
|
-
for (let index = 0; index < text.length; index++) invariant(text.charCodeAt(index) >= 32 && text.charCodeAt(index) !== 127, "Invalid control character in dimension value.");
|
|
162
|
-
return `'${text.replaceAll("'", "''")}'`;
|
|
163
|
-
});
|
|
164
|
-
const predicate = `(${[...literals.length ? [`${column} IN (${literals.join(", ")})`] : [], ...missing ? [`${column} IS NULL`] : []].join(" OR ")})`;
|
|
165
|
-
return selection.kind === "exclude" ? missing ? `NOT ${predicate}` : `(${column} IS NULL OR NOT ${predicate})` : predicate;
|
|
166
|
-
}
|
|
167
|
-
//#endregion
|
|
168
|
-
//#region src/core/date-time-range-sql.ts
|
|
169
|
-
/** DuckDB timestamp expressions using the query session's timezone for relative anchors.
|
|
170
|
-
* Absolute fields remain UTC-rebased TIMESTAMP values. Start is inclusive, end exclusive.
|
|
171
|
-
*/
|
|
172
|
-
function dateTimeRangeSql(range, parameter = "period") {
|
|
173
|
-
invariant(/^[A-Za-z_][A-Za-z0-9_]*$/.test(parameter), "Use a SQL parameter name.");
|
|
174
|
-
return {
|
|
175
|
-
from: boundExpression(parameter, "from"),
|
|
176
|
-
to: boundExpression(parameter, "to"),
|
|
177
|
-
params: { [parameter]: serializeDateTimeRange(range) }
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
function boundExpression(parameter, bound) {
|
|
181
|
-
const json = `$${parameter}`;
|
|
182
|
-
const path = `$.${bound}`;
|
|
183
|
-
const anchor = `json_extract_string(${json}, '${path}.anchor')`;
|
|
184
|
-
const base = `CASE WHEN json_type(${json}, '${path}') = 'VARCHAR' THEN CAST(json_extract_string(${json}, '${path}') AS TIMESTAMP)
|
|
185
|
-
WHEN ${anchor} = 'RELATIVE_ANCHOR_START_OF_TODAY' THEN date_trunc('day', localtimestamp)
|
|
186
|
-
WHEN ${anchor} = 'RELATIVE_ANCHOR_START_OF_YESTERDAY' THEN date_trunc('day', localtimestamp) - INTERVAL '1 day'
|
|
187
|
-
WHEN ${anchor} = 'RELATIVE_ANCHOR_START_OF_TOMORROW' THEN date_trunc('day', localtimestamp) + INTERVAL '1 day'
|
|
188
|
-
WHEN ${anchor} = 'RELATIVE_ANCHOR_START_OF_WEEK' THEN date_trunc('week', localtimestamp)
|
|
189
|
-
WHEN ${anchor} = 'RELATIVE_ANCHOR_START_OF_MONTH' THEN date_trunc('month', localtimestamp)
|
|
190
|
-
WHEN ${anchor} = 'RELATIVE_ANCHOR_START_OF_YEAR' THEN date_trunc('year', localtimestamp)
|
|
191
|
-
WHEN ${anchor} = 'RELATIVE_ANCHOR_NOW' THEN localtimestamp
|
|
192
|
-
ELSE ${bound === "from" ? "TIMESTAMP '-infinity'" : "localtimestamp"} END`;
|
|
193
|
-
const item = `json_extract(${json}, '${path}.offset[' || i::VARCHAR || ']')`;
|
|
194
|
-
const amount = `CAST(json_extract_string(${item}, '$.amount') AS INTEGER)`;
|
|
195
|
-
return `(WITH RECURSIVE bounds(i, value) AS (
|
|
196
|
-
SELECT 0, ${base}
|
|
197
|
-
UNION ALL SELECT i + 1, value + ${`CASE json_extract_string(${item}, '$.unit') ${[
|
|
198
|
-
"SECOND",
|
|
199
|
-
"MINUTE",
|
|
200
|
-
"HOUR",
|
|
201
|
-
"DAY",
|
|
202
|
-
"WEEK",
|
|
203
|
-
"MONTH",
|
|
204
|
-
"YEAR"
|
|
205
|
-
].map((unit) => `WHEN '${unit}' THEN ${amount} * INTERVAL '1 ${unit}'`).join(" ")} ELSE INTERVAL '0 seconds' END`} FROM bounds
|
|
206
|
-
WHERE i < COALESCE(json_array_length(${json}, '${path}.offset'), 0)
|
|
207
|
-
) SELECT value FROM bounds ORDER BY i DESC LIMIT 1)`;
|
|
208
|
-
}
|
|
209
|
-
//#endregion
|
|
210
|
-
//#region src/core/parameter-sql.ts
|
|
211
|
-
/** True for rows the selection keeps. Excluding values keeps missing values unless
|
|
212
|
-
* missing itself is excluded, matching the picker's semantics. */
|
|
213
|
-
function selectionExpression(parameter, valueType, column) {
|
|
214
|
-
const json = `$${parameter}`;
|
|
215
|
-
const values = `CAST(json_extract(${json}, '$.values') AS ${valueType === "number" ? "DOUBLE" : "VARCHAR"}[])`;
|
|
216
|
-
const missing = `CAST(json_extract(${json}, '$.missing') AS BOOLEAN)`;
|
|
217
|
-
const listed = `list_contains(${values}, ${column})`;
|
|
218
|
-
return `(CASE json_extract_string(${json}, '$.kind')
|
|
219
|
-
WHEN 'all' THEN TRUE
|
|
220
|
-
WHEN 'include' THEN CASE WHEN ${column} IS NULL THEN ${missing} ELSE ${listed} END
|
|
221
|
-
WHEN 'exclude' THEN CASE WHEN ${column} IS NULL THEN NOT ${missing} ELSE NOT ${listed} END
|
|
222
|
-
END)`;
|
|
223
|
-
}
|
|
224
|
-
/** Expand `$period.from`, `$period.to`, and `$country.matches(column)` references only
|
|
225
|
-
* in SQL code, leaving quoted text and comments untouched. */
|
|
226
|
-
function expandParameterReferences(statement, parameters) {
|
|
227
|
-
const token = /[eE]'(?:\\.|''|[^'\\])*'|'(?:''|[^'])*'|"(?:""|[^"])*"|\$(?:[A-Za-z_][A-Za-z0-9_]*)?\$|--[^\n]*(?:\n|$)|\/\*|\$([A-Za-z_][A-Za-z0-9_]*)(?:\.(from|to)\b|\.matches\(\s*([A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*)\s*\))?/g;
|
|
228
|
-
let output = "";
|
|
229
|
-
let position = 0;
|
|
230
|
-
for (let match = token.exec(statement); match; match = token.exec(statement)) {
|
|
231
|
-
output += statement.slice(position, match.index);
|
|
232
|
-
const text = match[0];
|
|
233
|
-
const parameter = match[1] ? parameters.get(match[1]) : void 0;
|
|
234
|
-
if (text === "/*") {
|
|
235
|
-
let depth = 1;
|
|
236
|
-
let end = token.lastIndex;
|
|
237
|
-
while (depth && end < statement.length) if (statement.slice(end, end + 2) === "/*") {
|
|
238
|
-
depth++;
|
|
239
|
-
end += 2;
|
|
240
|
-
} else if (statement.slice(end, end + 2) === "*/") {
|
|
241
|
-
depth--;
|
|
242
|
-
end += 2;
|
|
243
|
-
} else end++;
|
|
244
|
-
output += statement.slice(match.index, end);
|
|
245
|
-
token.lastIndex = end;
|
|
246
|
-
} else if (/^\$(?:[A-Za-z_][A-Za-z0-9_]*)?\$$/.test(text)) {
|
|
247
|
-
const closing = statement.indexOf(text, token.lastIndex);
|
|
248
|
-
const end = closing < 0 ? statement.length : closing + text.length;
|
|
249
|
-
output += statement.slice(match.index, end);
|
|
250
|
-
token.lastIndex = end;
|
|
251
|
-
} else if (parameter?.type === "dateTimeRange" && match[2]) output += boundExpression(match[1], match[2]);
|
|
252
|
-
else if (parameter?.type === "dimension" && match[3]) output += selectionExpression(match[1], parameter.valueType, match[3]);
|
|
253
|
-
else output += text;
|
|
254
|
-
position = token.lastIndex;
|
|
255
|
-
}
|
|
256
|
-
return output + statement.slice(position);
|
|
257
|
-
}
|
|
258
|
-
//#endregion
|
|
259
|
-
//#region src/core/queries.ts
|
|
260
|
-
const MAX_QUERY_ROWS = 1e4;
|
|
261
|
-
const QUERY_CAST_ERROR = "##ERROR##";
|
|
262
|
-
/** Bind a selection as JSON the expanded predicate reads: kind, listed values, and
|
|
263
|
-
* whether missing values are listed. */
|
|
264
|
-
function serializeSelection(key, valueType, value) {
|
|
265
|
-
const selection = typeof value === "string" ? JSON.parse(value) : value;
|
|
266
|
-
if (!(!!selection && typeof selection === "object" && (selection.kind === "all" || (selection.kind === "include" || selection.kind === "exclude") && Array.isArray(selection.members) && selection.members.length > 0 && selection.members.every((member) => member?.kind === "missing" || member?.kind === "value" && (valueType === "number" ? typeof member.value === "number" && Number.isFinite(member.value) : typeof member.value === "string"))))) throw new Error(`Parameter ${key} is not a ${valueType} dimension selection.`);
|
|
267
|
-
if (selection.kind === "all") return JSON.stringify({ kind: "all" });
|
|
268
|
-
return JSON.stringify({
|
|
269
|
-
kind: selection.kind,
|
|
270
|
-
values: selection.members.flatMap((member) => member.kind === "value" ? [member.value] : []),
|
|
271
|
-
missing: selection.members.some((member) => member.kind === "missing")
|
|
272
|
-
});
|
|
273
|
-
}
|
|
274
|
-
function isQueryParameterValue(value) {
|
|
275
|
-
return value === null || typeof value === "string" || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value);
|
|
276
|
-
}
|
|
277
|
-
function isQueryParameters(value) {
|
|
278
|
-
return !!value && typeof value === "object" && !Array.isArray(value) && Object.values(value).every(isQueryParameterValue);
|
|
279
|
-
}
|
|
280
|
-
function snapshotField(field) {
|
|
281
|
-
invariant(field && typeof field === "object" && !Array.isArray(field) && (field.type === "string" || field.type === "number" || field.type === "boolean" || field.type === "null") && (field.nullable === void 0 || typeof field.nullable === "boolean"), "Query fields need a supported type and a boolean nullable flag.");
|
|
282
|
-
return Object.freeze({ ...field });
|
|
283
|
-
}
|
|
284
|
-
/** Snapshot the query's SQL, parameter defaults, and decoded result contract. */
|
|
285
|
-
function snapshotQueries(definitions) {
|
|
286
|
-
return Object.freeze(Object.fromEntries(Object.entries(definitions).map(([name, query]) => {
|
|
287
|
-
invariant(!!name.trim() && !!query.statement.trim(), "Queries need a name and SQL statement.");
|
|
288
|
-
const params = Object.freeze(Object.fromEntries(Object.entries(query.params).map(([key, declaration]) => {
|
|
289
|
-
if (declaration.type === "dateTimeRange" || declaration.type === "dimension") invariant(/^[A-Za-z_][A-Za-z0-9_]*$/.test(key) && declaration.nullable !== true, "Range and dimension parameters require a SQL parameter name and cannot be nullable.");
|
|
290
|
-
if (declaration.type === "dimension") {
|
|
291
|
-
invariant((declaration.valueType === "string" || declaration.valueType === "number") && !Object.hasOwn(declaration, "defaultValue"), `Dimension parameter ${key} in query ${name} needs a string or number valueType and no default.`);
|
|
292
|
-
return [key, Object.freeze({ ...declaration })];
|
|
293
|
-
}
|
|
294
|
-
invariant(!Object.hasOwn(declaration, "defaultValue") || (declaration.type === "dateTimeRange" ? !!parseDateTimeRange(declaration.defaultValue) : isQueryParameterValue(declaration.defaultValue)), `Invalid default for parameter ${key} in query ${name}.`);
|
|
295
|
-
return [key, declaration.type === "dateTimeRange" ? Object.freeze({
|
|
296
|
-
...declaration,
|
|
297
|
-
...declaration.defaultValue === void 0 ? {} : { defaultValue: parseDateTimeRange(declaration.defaultValue) }
|
|
298
|
-
}) : snapshotField(declaration)];
|
|
299
|
-
})));
|
|
300
|
-
const projectionEntries = Object.entries(query.projections);
|
|
301
|
-
invariant(projectionEntries.length > 0 && projectionEntries.every(([alias]) => !!alias.trim()), "Queries need at least one named projection.");
|
|
302
|
-
return [name, Object.freeze({
|
|
303
|
-
statement: expandParameterReferences(query.statement, new Map(Object.entries(query.params).flatMap(([key, p]) => p.type === "dateTimeRange" || p.type === "dimension" ? [[key, p]] : []))),
|
|
304
|
-
params,
|
|
305
|
-
projections: Object.freeze(Object.fromEntries(projectionEntries.map(([alias, field]) => [alias, snapshotField(field)])))
|
|
306
|
-
})];
|
|
307
|
-
})));
|
|
308
|
-
}
|
|
309
|
-
/** Decode each returned cell once, matching aliases independently of result order. */
|
|
310
|
-
function decodeQueryResult(result, projections) {
|
|
311
|
-
if (!result || !Array.isArray(result.columns) || !Array.isArray(result.rows) || result.queryId !== void 0 && typeof result.queryId !== "string") throw new Error("Query result must contain columns and rows.");
|
|
312
|
-
const returnedColumns = Array.from(result.columns, (column) => {
|
|
313
|
-
if (!column || typeof column !== "object" || Array.isArray(column) || typeof column.name !== "string" || column.type !== void 0 && typeof column.type !== "string") throw new Error("Query result columns must have names and optional type strings.");
|
|
314
|
-
return {
|
|
315
|
-
name: column.name,
|
|
316
|
-
...column.type === void 0 ? {} : { type: column.type }
|
|
317
|
-
};
|
|
318
|
-
});
|
|
319
|
-
const names = returnedColumns.map((column) => column.name);
|
|
320
|
-
return {
|
|
321
|
-
columns: returnedColumns,
|
|
322
|
-
rows: Array.from(result.rows, (row) => {
|
|
323
|
-
if (!Array.isArray(row) || row.length !== names.length) throw new Error("Query result row has the wrong width.");
|
|
324
|
-
return Object.fromEntries(names.map((name, index) => [name, castQueryCell(row[index], projections[name])]));
|
|
325
|
-
}),
|
|
326
|
-
...result.queryId === void 0 ? {} : { queryId: result.queryId }
|
|
327
|
-
};
|
|
328
|
-
}
|
|
329
|
-
function castQueryCell(value, field) {
|
|
330
|
-
if (field === void 0) {
|
|
331
|
-
if (typeof value === "string" || typeof value === "boolean" || typeof value === "number") return value;
|
|
332
|
-
if (value === null) return null;
|
|
333
|
-
return JSON.stringify(value);
|
|
334
|
-
}
|
|
335
|
-
if (value === null && field.nullable !== false) return null;
|
|
336
|
-
switch (field.type) {
|
|
337
|
-
case "string": return String(value);
|
|
338
|
-
case "number": {
|
|
339
|
-
const number = Number(value);
|
|
340
|
-
return Number.isFinite(number) ? number : QUERY_CAST_ERROR;
|
|
341
|
-
}
|
|
342
|
-
case "boolean": return Boolean(value);
|
|
343
|
-
case "null": return null;
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
/** Host context supplies protected values; callers and defaults fill the remaining parameters. */
|
|
347
|
-
function resolveQueryParameters(declarations, values, context) {
|
|
348
|
-
if (!values || typeof values !== "object" || Array.isArray(values)) throw new Error("Supply query parameter values as an object.");
|
|
349
|
-
for (const key of Object.keys(values)) {
|
|
350
|
-
if (!Object.hasOwn(declarations, key)) throw new Error(`Unknown parameter ${key}.`);
|
|
351
|
-
if (Object.hasOwn(context, key)) throw new Error(`Cannot override context parameter ${key}.`);
|
|
352
|
-
}
|
|
353
|
-
return Object.fromEntries(Object.entries(declarations).map(([key, declaration]) => {
|
|
354
|
-
const value = Object.hasOwn(context, key) ? context[key] : Object.hasOwn(values, key) && values[key] !== void 0 ? values[key] : declaration.defaultValue;
|
|
355
|
-
if (value === void 0) throw new Error(`Missing parameter ${key}.`);
|
|
356
|
-
if (declaration.type === "dimension") return [key, serializeSelection(key, declaration.valueType, value)];
|
|
357
|
-
if (declaration.type === "dateTimeRange") return [key, serializeDateTimeRange(parseDateTimeRange(typeof value === "string" ? JSON.parse(value) : value))];
|
|
358
|
-
const nullable = declaration.type === "null" || declaration.nullable === true;
|
|
359
|
-
if (!(value === null ? nullable : typeof value === declaration.type && isQueryParameterValue(value))) throw new Error(`Parameter ${key} does not match ${declaration.type}.`);
|
|
360
|
-
return [key, value];
|
|
361
|
-
}));
|
|
362
|
-
}
|
|
363
|
-
//#endregion
|
|
364
|
-
//#region src/core/dimension-source.ts
|
|
365
|
-
const dimensionSource = Symbol("data-app.dimensionSource");
|
|
366
|
-
const sources = /* @__PURE__ */ new WeakMap();
|
|
367
|
-
const selectorsKey = Symbol.for("@altertable/data-app/dimension-selectors");
|
|
368
|
-
const ownerKey = Symbol.for("@altertable/data-app/dimension-selector-owner");
|
|
369
|
-
const invalidMemberKey = Symbol.for("@altertable/data-app/dimension-member-error");
|
|
370
|
-
var DimensionMembershipError = class extends Error {
|
|
371
|
-
[invalidMemberKey] = true;
|
|
372
|
-
static [Symbol.hasInstance](value) {
|
|
373
|
-
return value instanceof Error && value[invalidMemberKey] === true;
|
|
374
|
-
}
|
|
375
|
-
};
|
|
376
|
-
/** A query supplies independent options and exact member lookup; it declares a nullable `member` parameter. */
|
|
377
|
-
function bindDimensionSource(config, queries) {
|
|
378
|
-
invariant(!config.allowMissing, "Source dimensions require concrete members.");
|
|
379
|
-
const query = queries[config.source.query];
|
|
380
|
-
invariant(query && Object.hasOwn(query.params, "member"), "Dimension sources must declare member.");
|
|
381
|
-
const member = query.params.member;
|
|
382
|
-
const { value, label, count } = query.projections;
|
|
383
|
-
invariant(member.type === config.valueType && member.nullable === true && value?.type === config.valueType && value.nullable !== true && label?.type === "string" && label.nullable !== true && (!count || count.type === "number" && count.nullable !== true), "Dimension sources require a matching nullable member and concrete value, label, and count projections.");
|
|
384
|
-
const operation = `__dimension:${config.key}`;
|
|
385
|
-
const filter = Object.assign(createDimensionFilter({
|
|
386
|
-
...config,
|
|
387
|
-
valueType: config.valueType,
|
|
388
|
-
options: []
|
|
389
|
-
}, true), {
|
|
390
|
-
optionsSource: "query",
|
|
391
|
-
[dimensionSource]: Object.freeze({ operation })
|
|
392
|
-
});
|
|
393
|
-
sources.set(filter, {
|
|
394
|
-
query: config.source.query,
|
|
395
|
-
queries
|
|
396
|
-
});
|
|
397
|
-
return filter;
|
|
398
|
-
}
|
|
399
|
-
async function loadOptions(filter, member, query) {
|
|
400
|
-
const result = await query(sources.get(filter).query, { member }, { limit: member === null ? 200 : 2 });
|
|
401
|
-
const names = result.columns.map((column) => column.name);
|
|
402
|
-
invariant(new Set(names).size === names.length && names.includes("value") && names.includes("label"), "Dimension source must return value and label.");
|
|
403
|
-
const options = parseFacetOptions(result.rows, filter);
|
|
404
|
-
if (member !== null && (options.length !== 1 || options[0].value !== member)) throw new DimensionMembershipError(`Invalid ${filter.label} member.`);
|
|
405
|
-
return options;
|
|
406
|
-
}
|
|
407
|
-
/** Source dimension selections must name members the source query returns. */
|
|
408
|
-
async function validateSourceMembers(filters, input, query) {
|
|
409
|
-
for (const [name, filter] of Object.entries(filters)) {
|
|
410
|
-
if (filter.kind !== "dimension" || !sources.has(filter)) continue;
|
|
411
|
-
const selection = input[name];
|
|
412
|
-
if (selection.kind === "all") continue;
|
|
413
|
-
for (const member of selection.members) if (member.kind === "value") await loadOptions(filter, member.value, query);
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
function attachDimensionSelectors(operation, queries) {
|
|
417
|
-
const selectors = {};
|
|
418
|
-
for (const filter of Object.values(operation.filters ?? {})) {
|
|
419
|
-
if (filter.kind !== "dimension" || !(dimensionSource in filter)) continue;
|
|
420
|
-
const source = sources.get(filter);
|
|
421
|
-
if (!source) continue;
|
|
422
|
-
invariant(JSON.stringify(source.queries[source.query]) === JSON.stringify(queries[source.query]), "Dimension sources must belong to the operation query registry.");
|
|
423
|
-
const name = filter[dimensionSource].operation;
|
|
424
|
-
invariant(!selectors[name] || selectors[name][ownerKey] === filter, "Dimension source keys must be unique across the app.");
|
|
425
|
-
selectors[name] = {
|
|
426
|
-
input(value) {
|
|
427
|
-
invariant(value && typeof value === "object", "Invalid dimension request.");
|
|
428
|
-
const member = value.member;
|
|
429
|
-
invariant(member === null || filter.valid({
|
|
430
|
-
kind: "include",
|
|
431
|
-
members: [{
|
|
432
|
-
kind: "value",
|
|
433
|
-
value: member
|
|
434
|
-
}]
|
|
435
|
-
}), "Invalid requested member.");
|
|
436
|
-
return { member };
|
|
437
|
-
},
|
|
438
|
-
checks: [{ member: null }],
|
|
439
|
-
policy: {
|
|
440
|
-
maxDurationMs: 15e3,
|
|
441
|
-
maxQueryRows: 200,
|
|
442
|
-
maxResponseBytes: 1e6
|
|
443
|
-
},
|
|
444
|
-
queryNames: { [source.query]: source.query },
|
|
445
|
-
async run(context, input) {
|
|
446
|
-
return loadOptions(filter, input.member, async (name, params, options) => {
|
|
447
|
-
const definition = queries[name];
|
|
448
|
-
return decodeQueryResult(await context.lakehouse.queryAll(definition.statement, {
|
|
449
|
-
name,
|
|
450
|
-
params: resolveQueryParameters(definition.params, params, context.queryParams ?? {}),
|
|
451
|
-
limit: options.limit,
|
|
452
|
-
signal: context.signal
|
|
453
|
-
}), definition.projections);
|
|
454
|
-
});
|
|
455
|
-
}
|
|
456
|
-
};
|
|
457
|
-
Object.defineProperty(selectors[name], ownerKey, { value: filter });
|
|
458
|
-
}
|
|
459
|
-
Object.defineProperty(operation, selectorsKey, { value: selectors });
|
|
460
|
-
}
|
|
461
|
-
function withDimensionSelectors(operations) {
|
|
462
|
-
const expanded = { ...operations };
|
|
463
|
-
for (const operation of Object.values(operations)) for (const [name, selector] of Object.entries(operation[selectorsKey] ?? {})) {
|
|
464
|
-
invariant(!Object.hasOwn(operations, name), "Reserved dimension operation name.");
|
|
465
|
-
invariant(!expanded[name] || expanded[name][ownerKey] === selector[ownerKey], "Dimension source keys must be unique across the app.");
|
|
466
|
-
expanded[name] ??= selector;
|
|
467
|
-
}
|
|
468
|
-
return expanded;
|
|
469
|
-
}
|
|
470
|
-
//#endregion
|
|
471
|
-
//#region src/core/app-filters.ts
|
|
472
|
-
/** Name filters once in the app. URL keys must be unique across its controls. */
|
|
473
|
-
function defineAppFilters(filters) {
|
|
474
|
-
const owners = /* @__PURE__ */ new Map();
|
|
475
|
-
for (const [name, filter] of Object.entries(filters)) {
|
|
476
|
-
const presentation = filter.presentation;
|
|
477
|
-
invariant(presentation === void 0 || presentation !== null && typeof presentation === "object" && !Array.isArray(presentation) && (presentation.control === void 0 || presentation.control === "toolbar" || presentation.control === "widget") && (presentation.group === void 0 || presentation.group === "scope" || presentation.group === "display") && (presentation.prominence === void 0 || presentation.prominence === "primary" || presentation.prominence === "secondary"), `Filter ${name} has invalid presentation.`);
|
|
478
|
-
for (const key of filter.urlKeys) {
|
|
479
|
-
invariant(![
|
|
480
|
-
"view",
|
|
481
|
-
"about",
|
|
482
|
-
"tab",
|
|
483
|
-
"present",
|
|
484
|
-
"step",
|
|
485
|
-
"details"
|
|
486
|
-
].includes(key), `Filter ${name} uses reserved URL key: ${key}.`);
|
|
487
|
-
invariant(!!key && !owners.has(key), `Filter ${name} has an empty or duplicate URL key: ${key}.`);
|
|
488
|
-
owners.set(key, name);
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
return filters;
|
|
492
|
-
}
|
|
493
|
-
/** Search query state; typing replaces history by default. Operations reject text
|
|
494
|
-
* longer than `maxLength` (200 by default). */
|
|
495
|
-
function searchFilter({ key, label, defaultValue = "", history = "replace", maxLength = 200 }) {
|
|
496
|
-
invariant(Number.isSafeInteger(maxLength) && maxLength > 0, `Search ${key} needs a positive maxLength.`);
|
|
497
|
-
function valid(value) {
|
|
498
|
-
return typeof value === "string" && value.length <= maxLength;
|
|
499
|
-
}
|
|
500
|
-
invariant(valid(defaultValue), `Search ${key} default exceeds maxLength.`);
|
|
501
|
-
return {
|
|
502
|
-
kind: "search",
|
|
503
|
-
label: label ?? key,
|
|
504
|
-
urlKeys: [key],
|
|
505
|
-
defaultValue,
|
|
506
|
-
clearValue: "",
|
|
507
|
-
describe: (value) => value,
|
|
508
|
-
history,
|
|
509
|
-
read(params) {
|
|
510
|
-
const value = params.get(key);
|
|
511
|
-
return value !== null && valid(value) ? value : defaultValue;
|
|
512
|
-
},
|
|
513
|
-
write(value) {
|
|
514
|
-
return { [key]: value === defaultValue ? null : value };
|
|
515
|
-
},
|
|
516
|
-
valid,
|
|
517
|
-
same(left, right) {
|
|
518
|
-
return left === right;
|
|
519
|
-
}
|
|
520
|
-
};
|
|
521
|
-
}
|
|
522
|
-
/** Validate labeled choices before controls or URL state consume them. */
|
|
523
|
-
function validateChoiceOptions(options) {
|
|
524
|
-
invariant(options.length > 0 && options.every((option) => !!option.id.trim() && !!option.label.trim()) && new Set(options.map((option) => option.id)).size === options.length, "Choices require nonempty, unique IDs and labels.");
|
|
525
|
-
}
|
|
526
|
-
/** One choice that narrows the included data. */
|
|
527
|
-
function choiceFilter(options) {
|
|
528
|
-
return createChoice(options);
|
|
529
|
-
}
|
|
530
|
-
/** One choice that changes how results are shown, such as a chart breakdown or sort order.
|
|
531
|
-
* Clearing filters keeps it; pass `view.breakdown(name)` to render it beside its chart. */
|
|
532
|
-
function displayChoice(options) {
|
|
533
|
-
const presentation = {
|
|
534
|
-
...options.presentation,
|
|
535
|
-
group: "display"
|
|
536
|
-
};
|
|
537
|
-
return {
|
|
538
|
-
...createChoice(options),
|
|
539
|
-
presentation
|
|
540
|
-
};
|
|
541
|
-
}
|
|
542
|
-
function createChoice({ key, label, defaultValue, options, history = "push", presentation }) {
|
|
543
|
-
validateChoiceOptions(options);
|
|
544
|
-
function valid(value) {
|
|
545
|
-
return typeof value === "string" && options.some((option) => option.id === value);
|
|
546
|
-
}
|
|
547
|
-
invariant(valid(defaultValue), `Choice ${key} must include its default value.`);
|
|
548
|
-
return {
|
|
549
|
-
kind: "choice",
|
|
550
|
-
presentation,
|
|
551
|
-
label: label ?? key,
|
|
552
|
-
options,
|
|
553
|
-
urlKeys: [key],
|
|
554
|
-
defaultValue,
|
|
555
|
-
history,
|
|
556
|
-
describe: (value) => options.find((option) => option.id === value).label,
|
|
557
|
-
read(params) {
|
|
558
|
-
const value = params.get(key);
|
|
559
|
-
return value !== null && valid(value) ? value : defaultValue;
|
|
560
|
-
},
|
|
561
|
-
write(value) {
|
|
562
|
-
return { [key]: value === defaultValue ? null : value };
|
|
563
|
-
},
|
|
564
|
-
valid,
|
|
565
|
-
same: (left, right) => left === right
|
|
566
|
-
};
|
|
567
|
-
}
|
|
568
|
-
function multiChoiceFilter({ key, label, defaultValue = [], options, maxSelected = options.length, history = "push", presentation }) {
|
|
569
|
-
validateChoiceOptions(options);
|
|
570
|
-
invariant(Number.isSafeInteger(maxSelected) && maxSelected >= 1 && maxSelected <= options.length, "Invalid choice selection limit.");
|
|
571
|
-
function valid(value) {
|
|
572
|
-
return Array.isArray(value) && value.length <= maxSelected && new Set(value).size === value.length && value.every((id) => options.some((option) => option.id === id));
|
|
573
|
-
}
|
|
574
|
-
function same(left, right) {
|
|
575
|
-
return left.length === right.length && left.every((id) => right.includes(id));
|
|
576
|
-
}
|
|
577
|
-
invariant(valid(defaultValue), `Invalid default choices for ${key}.`);
|
|
578
|
-
return {
|
|
579
|
-
kind: "multiChoice",
|
|
580
|
-
presentation,
|
|
581
|
-
label: label ?? key,
|
|
582
|
-
options,
|
|
583
|
-
maxSelected,
|
|
584
|
-
urlKeys: [key],
|
|
585
|
-
defaultValue,
|
|
586
|
-
history,
|
|
587
|
-
describe: (values) => values.map((id) => options.find((option) => option.id === id).label).join(", ") || "None",
|
|
588
|
-
read(params) {
|
|
589
|
-
const raw = params.get(key);
|
|
590
|
-
if (raw === null || raw.length > 4096) return defaultValue;
|
|
591
|
-
try {
|
|
592
|
-
const values = JSON.parse(raw);
|
|
593
|
-
return valid(values) ? values : defaultValue;
|
|
594
|
-
} catch {
|
|
595
|
-
return defaultValue;
|
|
596
|
-
}
|
|
597
|
-
},
|
|
598
|
-
write(value) {
|
|
599
|
-
return { [key]: same(value, defaultValue) ? null : JSON.stringify(options.filter((option) => value.includes(option.id)).map((option) => option.id)) };
|
|
600
|
-
},
|
|
601
|
-
valid,
|
|
602
|
-
same
|
|
603
|
-
};
|
|
604
|
-
}
|
|
605
|
-
//#endregion
|
|
606
|
-
//#region src/core/operation-filters.ts
|
|
607
|
-
function isDateFilter(filter) {
|
|
608
|
-
return filter.kind === "dateTimeRange";
|
|
609
|
-
}
|
|
610
|
-
/** Validate an operation's filters once: unique URL keys and at most one date filter. */
|
|
611
|
-
function defineOperationFilters(filters) {
|
|
612
|
-
defineAppFilters(filters);
|
|
613
|
-
invariant(Object.values(filters).filter(isDateFilter).length <= 1, "An operation reads at most one date filter.");
|
|
614
|
-
return Object.freeze({ ...filters });
|
|
615
|
-
}
|
|
616
|
-
/** The name of the operation's date filter, which sets its reporting period. */
|
|
617
|
-
function dateFilterName(filters) {
|
|
618
|
-
return Object.keys(filters).find((name) => isDateFilter(filters[name]));
|
|
619
|
-
}
|
|
620
|
-
/** Resolve URL selections to the request the browser sends. Relative date ranges
|
|
621
|
-
* resolve against one reference time so the range and its comparison agree. */
|
|
622
|
-
function resolveFilterInput(filters, values, reference = /* @__PURE__ */ new Date()) {
|
|
623
|
-
const input = {};
|
|
624
|
-
for (const [name, filter] of Object.entries(filters)) {
|
|
625
|
-
const value = values[name];
|
|
626
|
-
input[name] = filter.kind === "dateTimeRange" ? filter.request(value, reference) : value;
|
|
627
|
-
}
|
|
628
|
-
return input;
|
|
629
|
-
}
|
|
630
|
-
/** Validate untrusted operation input against the declared filters. */
|
|
631
|
-
function parseFilterInput(filters, value) {
|
|
632
|
-
invariant(value && typeof value === "object" && !Array.isArray(value), "Expected filter input.");
|
|
633
|
-
const fields = value;
|
|
634
|
-
for (const name of Object.keys(fields)) invariant(Object.hasOwn(filters, name), `Unknown filter input: ${name}.`);
|
|
635
|
-
const input = {};
|
|
636
|
-
for (const [name, filter] of Object.entries(filters)) {
|
|
637
|
-
const selected = fields[name];
|
|
638
|
-
if (filter.kind === "dateTimeRange") input[name] = filter.parseInput(selected);
|
|
639
|
-
else {
|
|
640
|
-
invariant(filter.valid(selected), `Invalid ${filter.label ?? name}.`);
|
|
641
|
-
input[name] = selected;
|
|
642
|
-
}
|
|
643
|
-
}
|
|
644
|
-
return input;
|
|
645
|
-
}
|
|
646
|
-
/** The default selections, resolved: every filter operation can run with them. */
|
|
647
|
-
function defaultFilterInput(filters) {
|
|
648
|
-
return Object.fromEntries(Object.entries(filters).map(([name, filter]) => {
|
|
649
|
-
if (filter.kind !== "dateTimeRange") return [name, filter.defaultValue];
|
|
650
|
-
const { range, comparison } = filter.request(filter.defaultValue);
|
|
651
|
-
return [name, {
|
|
652
|
-
range,
|
|
653
|
-
comparison
|
|
654
|
-
}];
|
|
655
|
-
}));
|
|
656
|
-
}
|
|
657
|
-
/** Every operation in an app shares one filter per name and one owner per URL key. */
|
|
658
|
-
function combineOperationFilters(operations) {
|
|
659
|
-
const combined = {};
|
|
660
|
-
for (const [operation, filters] of Object.entries(operations)) for (const [name, filter] of Object.entries(filters)) {
|
|
661
|
-
invariant(!combined[name] || combined[name] === filter, `Operations read different filters named ${name}; declare it once and share it (${operation}).`);
|
|
662
|
-
combined[name] = filter;
|
|
663
|
-
}
|
|
664
|
-
return defineAppFilters(combined);
|
|
665
|
-
}
|
|
666
|
-
//#endregion
|
|
667
|
-
//#region src/authoring/contract.ts
|
|
668
|
-
function parseTrue(value) {
|
|
669
|
-
invariant(value === true, "Expected a successful check.");
|
|
670
|
-
return true;
|
|
671
|
-
}
|
|
672
|
-
/** Accepts numeric strings from DuckDB; rejects negative, fractional, and unsafe integers. */
|
|
673
|
-
function parseCount(value) {
|
|
674
|
-
const number = typeof value === "number" || typeof value === "string" ? Number(value) : NaN;
|
|
675
|
-
invariant(Number.isSafeInteger(number) && number >= 0 && (typeof value !== "string" || !!value.trim()), "Invalid count in query result.");
|
|
676
|
-
return number;
|
|
677
|
-
}
|
|
678
|
-
function parseLabel(value, maxLength = 100) {
|
|
679
|
-
invariant(typeof value === "string" && !!value && value.length <= maxLength, "Invalid label in query result.");
|
|
680
|
-
return value;
|
|
681
|
-
}
|
|
682
|
-
function defineQueryNames(names) {
|
|
683
|
-
const values = Object.values(names);
|
|
684
|
-
invariant(values.every((name) => !!name.trim()) && new Set(values).size === values.length, "Query names must be nonempty and unique.");
|
|
685
|
-
return names;
|
|
686
|
-
}
|
|
687
|
-
function defineOperation(operation) {
|
|
688
|
-
const { policy } = operation;
|
|
689
|
-
invariant(Number.isInteger(policy.maxQueryRows) && policy.maxQueryRows > 0 && policy.maxQueryRows <= 1e4 && Number.isInteger(policy.maxDurationMs) && policy.maxDurationMs > 0 && (policy.maxResponseBytes === void 0 || Number.isInteger(policy.maxResponseBytes) && policy.maxResponseBytes > 0), `Each data operation needs positive row and duration limits, with at most ${MAX_QUERY_ROWS.toLocaleString("en-US")} rows.`);
|
|
690
|
-
const contract = operationInputContract(operation);
|
|
691
|
-
const queries = snapshotQueries(operation.queries);
|
|
692
|
-
const queryNames = Object.fromEntries(Object.keys(queries).map((name) => [name, name]));
|
|
693
|
-
const defined = {
|
|
694
|
-
...contract,
|
|
695
|
-
policy,
|
|
696
|
-
queryNames,
|
|
697
|
-
async run(context, input) {
|
|
698
|
-
async function query(name, values = {}, options) {
|
|
699
|
-
invariant(Object.hasOwn(queries, name), `Unknown query name: ${name}.`);
|
|
700
|
-
const definition = queries[name];
|
|
701
|
-
const params = resolveQueryParameters(definition.params, values, context.queryParams ?? {});
|
|
702
|
-
return decodeQueryResult(await context.lakehouse.queryAll(definition.statement, {
|
|
703
|
-
name,
|
|
704
|
-
params,
|
|
705
|
-
limit: options?.limit ?? policy.maxQueryRows,
|
|
706
|
-
signal: context.signal
|
|
707
|
-
}), definition.projections);
|
|
708
|
-
}
|
|
709
|
-
if (contract.filters) await validateSourceMembers(contract.filters, input, query);
|
|
710
|
-
context.signal.throwIfAborted();
|
|
711
|
-
return operation.run({
|
|
712
|
-
signal: context.signal,
|
|
713
|
-
query
|
|
714
|
-
}, input);
|
|
715
|
-
}
|
|
716
|
-
};
|
|
717
|
-
attachDimensionSelectors(defined, queries);
|
|
718
|
-
return defined;
|
|
719
|
-
}
|
|
720
|
-
/** Filters parse themselves; an operation's own `input` parses only its other fields. */
|
|
721
|
-
function operationInputContract(operation) {
|
|
722
|
-
const filters = defineOperationFilters(operation.filters ?? {});
|
|
723
|
-
const defaults = defaultFilterInput(filters);
|
|
724
|
-
if (!operation.input) return {
|
|
725
|
-
filters,
|
|
726
|
-
input: (value) => parseFilterInput(filters, value),
|
|
727
|
-
checks: [defaults]
|
|
728
|
-
};
|
|
729
|
-
const parseOwn = operation.input;
|
|
730
|
-
invariant(Array.isArray(operation.checks) && operation.checks.length > 0, "An operation with its own input needs check inputs.");
|
|
731
|
-
if (!Object.keys(filters).length) {
|
|
732
|
-
for (const check of operation.checks) parseOwn(check);
|
|
733
|
-
return {
|
|
734
|
-
input: parseOwn,
|
|
735
|
-
checks: operation.checks
|
|
736
|
-
};
|
|
737
|
-
}
|
|
738
|
-
function input(value) {
|
|
739
|
-
invariant(value && typeof value === "object" && !Array.isArray(value), "Expected operation input.");
|
|
740
|
-
const own = {};
|
|
741
|
-
const selected = {};
|
|
742
|
-
for (const [name, field] of Object.entries(value)) (Object.hasOwn(filters, name) ? selected : own)[name] = field;
|
|
743
|
-
const parsed = parseOwn(own);
|
|
744
|
-
invariant(!!parsed && typeof parsed === "object", "Expected parsed input object.");
|
|
745
|
-
for (const name of Object.keys(parsed)) invariant(!Object.hasOwn(filters, name), `Input field ${name} is also a filter; the filter parses it.`);
|
|
746
|
-
return {
|
|
747
|
-
...parseFilterInput(filters, selected),
|
|
748
|
-
...parsed
|
|
749
|
-
};
|
|
750
|
-
}
|
|
751
|
-
const checks = operation.checks.map((check) => ({
|
|
752
|
-
...defaults,
|
|
753
|
-
...check
|
|
754
|
-
}));
|
|
755
|
-
for (const check of checks) input(check);
|
|
756
|
-
return {
|
|
757
|
-
filters,
|
|
758
|
-
input,
|
|
759
|
-
checks
|
|
760
|
-
};
|
|
761
|
-
}
|
|
762
|
-
/** A connectivity probe uses the app's registered connection query. */
|
|
763
|
-
function connectionCheck(queries) {
|
|
764
|
-
const connection = queries.connection;
|
|
765
|
-
return defineOperation({
|
|
766
|
-
queries: { connection },
|
|
767
|
-
policy: {
|
|
768
|
-
maxQueryRows: 1,
|
|
769
|
-
maxDurationMs: 15e3
|
|
770
|
-
},
|
|
771
|
-
async run({ query }) {
|
|
772
|
-
await query("connection");
|
|
773
|
-
return true;
|
|
774
|
-
}
|
|
775
|
-
});
|
|
776
|
-
}
|
|
777
|
-
//#endregion
|
|
778
|
-
export { KEY_SEPARATOR as A, dateTimeRangeSql as C, dimensionPredicate as D, dimensionMemberKey as E, parseDimensionSelection as O, snapshotQueries as S, dimensionFilter as T, bindDimensionSource as _, parseLabel as a, MAX_QUERY_ROWS as b, dateFilterName as c, defineAppFilters as d, displayChoice as f, DimensionMembershipError as g, validateChoiceOptions as h, parseCount as i, parseFacetOptions as k, resolveFilterInput as l, searchFilter as m, defineOperation as n, parseTrue as o, multiChoiceFilter as p, defineQueryNames as r, combineOperationFilters as s, connectionCheck as t, choiceFilter as u, dimensionSource as v, createDimensionFilter as w, isQueryParameters as x, withDimensionSelectors as y };
|
|
779
|
-
|
|
780
|
-
//# sourceMappingURL=contract-BQsFReRp.js.map
|