@altertable/data-app 0.71.0 → 0.73.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/AGENTS.md +5 -61
- package/CONTRIBUTING.md +93 -173
- package/README.md +3 -3
- package/REFERENCE.md +30 -341
- package/dist/chunks/contract-BeLPJwlO.js +558 -0
- package/dist/chunks/contract-BeLPJwlO.js.map +1 -0
- package/dist/chunks/contract-Di8SP74I.js +462 -0
- package/dist/chunks/contract-Di8SP74I.js.map +1 -0
- package/dist/chunks/format-BdFlv_xa.js +148 -0
- package/dist/chunks/format-BdFlv_xa.js.map +1 -0
- package/dist/chunks/iframe-C3uCZEXh.js +809 -0
- package/dist/chunks/iframe-C3uCZEXh.js.map +1 -0
- package/dist/chunks/operation-CBxhjNhK.js +131 -0
- package/dist/chunks/operation-CBxhjNhK.js.map +1 -0
- package/dist/chunks/request-body-H1qPmmTN.js +50 -0
- package/dist/chunks/request-body-H1qPmmTN.js.map +1 -0
- package/dist/client/index.js +253 -285
- package/dist/client/index.js.map +1 -13
- package/dist/core/appearance.js +171 -162
- package/dist/core/appearance.js.map +1 -10
- package/dist/core/config.js +49 -11
- package/dist/core/config.js.map +1 -9
- package/dist/core/contract.js +3 -73
- package/dist/core/format.js +1 -26
- package/dist/index.js +1 -12
- package/dist/internal.js +352 -397
- package/dist/internal.js.map +1 -15
- package/dist/server/index.js +85 -97
- package/dist/server/index.js.map +1 -10
- package/dist/types/chunks/appearance.d.ts +45 -0
- package/dist/types/chunks/contract.d.ts +720 -0
- package/dist/types/chunks/index.d.ts +189 -0
- package/dist/types/client/index.d.ts +3 -13
- package/dist/types/core/appearance.d.ts +2 -42
- package/dist/types/core/config.d.ts +44 -48
- package/dist/types/core/contract.d.ts +2 -114
- package/dist/types/core/format.d.ts +44 -17
- package/dist/types/index.d.ts +3 -3
- package/dist/types/internal.d.ts +312 -22
- package/dist/types/server/index.d.ts +15 -4
- package/docs/app-authoring.md +72 -112
- package/docs/client.md +29 -63
- package/docs/composition.md +44 -75
- package/docs/contract.md +60 -100
- package/docs/data-context.md +35 -61
- package/docs/embed.md +83 -55
- package/docs/formatting-and-appearance.md +17 -38
- package/docs/hosted-apps.md +12 -15
- package/docs/layout.md +30 -47
- package/docs/local-data-apps.md +7 -7
- package/docs/react-embed.md +20 -26
- package/docs/react.md +10 -27
- package/docs/server-bun.md +3 -28
- package/docs/server.md +6 -12
- package/docs/stories-and-export.md +38 -40
- package/docs/styling.md +14 -22
- package/docs/ui-quality.md +5 -5
- package/docs/ui.md +68 -70
- package/docs/variables.md +142 -254
- package/docs/views.md +85 -116
- package/docs/widgets.md +100 -153
- package/docs/worker.md +15 -27
- package/package.json +3 -3
- package/dist/chunks/index-5a27erdg.js +0 -54
- package/dist/chunks/index-5a27erdg.js.map +0 -10
- package/dist/chunks/index-9yxr7nmm.js +0 -742
- package/dist/chunks/index-9yxr7nmm.js.map +0 -17
- package/dist/chunks/index-j5q5n3ra.js +0 -151
- package/dist/chunks/index-j5q5n3ra.js.map +0 -10
- package/dist/chunks/index-q75zm3a7.js +0 -153
- package/dist/chunks/index-q75zm3a7.js.map +0 -11
- package/dist/chunks/index-vhaec7m5.js +0 -30
- package/dist/chunks/index-vhaec7m5.js.map +0 -10
- package/dist/chunks/index-zp78caex.js +0 -1047
- package/dist/chunks/index-zp78caex.js.map +0 -19
- package/dist/core/contract.js.map +0 -9
- package/dist/core/format.js.map +0 -9
- package/dist/index.js.map +0 -9
- package/dist/types/client/annotations.d.ts +0 -27
- package/dist/types/client/data-client.d.ts +0 -43
- package/dist/types/client/download.d.ts +0 -5
- package/dist/types/client/iframe.d.ts +0 -59
- package/dist/types/client/location.d.ts +0 -13
- package/dist/types/client/logger.d.ts +0 -14
- package/dist/types/client/messages.d.ts +0 -7
- package/dist/types/client/navigation.d.ts +0 -15
- package/dist/types/client/transport.d.ts +0 -15
- package/dist/types/core/annotations.d.ts +0 -99
- package/dist/types/core/bridge-endpoint.d.ts +0 -21
- package/dist/types/core/bridge.d.ts +0 -172
- package/dist/types/core/data-view.d.ts +0 -46
- package/dist/types/core/date-format.d.ts +0 -24
- package/dist/types/core/date-range.d.ts +0 -20
- package/dist/types/core/dimension-source.d.ts +0 -64
- package/dist/types/core/dimension.d.ts +0 -71
- package/dist/types/core/facets.d.ts +0 -13
- package/dist/types/core/filters.d.ts +0 -45
- package/dist/types/core/invariant.d.ts +0 -2
- package/dist/types/core/keys.d.ts +0 -2
- package/dist/types/core/logger.d.ts +0 -12
- package/dist/types/core/messages.d.ts +0 -62
- package/dist/types/core/navigation.d.ts +0 -11
- package/dist/types/core/operation-types.d.ts +0 -80
- package/dist/types/core/operation.d.ts +0 -20
- package/dist/types/core/presentation.d.ts +0 -9
- package/dist/types/core/queries.d.ts +0 -84
- package/dist/types/core/reading.d.ts +0 -17
- package/dist/types/core/uuid.d.ts +0 -2
- package/dist/types/core/variables.d.ts +0 -133
- package/dist/types/server/handler.d.ts +0 -13
- package/dist/types/server/request-body.d.ts +0 -4
- package/examples/starter-data-app/index.tsx +0 -193
- package/examples/starter-local-data-app/.bun-version +0 -1
- package/examples/starter-local-data-app/.node-version +0 -1
- package/examples/starter-local-data-app/.oxfmtrc.json +0 -4
- package/examples/starter-local-data-app/.oxlintrc.json +0 -33
- package/examples/starter-local-data-app/AGENTS.md +0 -22
- package/examples/starter-local-data-app/README.md +0 -56
- package/examples/starter-local-data-app/app.ts +0 -28
- package/examples/starter-local-data-app/bunfig.toml +0 -2
- package/examples/starter-local-data-app/package.json +0 -35
- package/examples/starter-local-data-app/src/App.tsx +0 -8
- package/examples/starter-local-data-app/src/assets.d.ts +0 -1
- package/examples/starter-local-data-app/src/data-context.ts +0 -21
- package/examples/starter-local-data-app/src/favicon.svg +0 -13
- package/examples/starter-local-data-app/src/index.html +0 -14
- package/examples/starter-local-data-app/src/main.tsx +0 -7
- package/examples/starter-local-data-app/src/operations.ts +0 -6
- package/examples/starter-local-data-app/src/server.ts +0 -6
- package/examples/starter-local-data-app/tsconfig.json +0 -21
|
@@ -0,0 +1,558 @@
|
|
|
1
|
+
import { n as formatDateRange } from "./format-BdFlv_xa.js";
|
|
2
|
+
import { parseDate, today } from "@internationalized/date";
|
|
3
|
+
//#region src/core/invariant.ts
|
|
4
|
+
/** Assert an author or data contract while preserving a useful failure message. */
|
|
5
|
+
function invariant(condition, message) {
|
|
6
|
+
if (!condition) throw new Error(`Invariant failed: ${message}`);
|
|
7
|
+
}
|
|
8
|
+
//#endregion
|
|
9
|
+
//#region src/core/keys.ts
|
|
10
|
+
/** Separator for opaque composite keys, independent of URL and DOM ID formats. */
|
|
11
|
+
const KEY_SEPARATOR = ":";
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/core/dimension.ts
|
|
14
|
+
const allSelection = { kind: "all" };
|
|
15
|
+
function dimensionMemberKey(member) {
|
|
16
|
+
return member.kind === "missing" ? "missing" : [
|
|
17
|
+
"value",
|
|
18
|
+
typeof member.value,
|
|
19
|
+
member.value
|
|
20
|
+
].join(":");
|
|
21
|
+
}
|
|
22
|
+
function dimensionFilter(config) {
|
|
23
|
+
return {
|
|
24
|
+
...createDimensionFilter(config),
|
|
25
|
+
optionsSource: "fixed"
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/** Shared fixed-option and query-backed selection behavior. */
|
|
29
|
+
function createDimensionFilter(config, dynamic = false) {
|
|
30
|
+
const { key, label, selectionMode, valueType } = config;
|
|
31
|
+
const options = config.options ?? [];
|
|
32
|
+
const optionKeys = options.map((option) => dimensionMemberKey({
|
|
33
|
+
kind: "value",
|
|
34
|
+
value: option.value
|
|
35
|
+
}));
|
|
36
|
+
invariant(!!key && options.length <= 200 && new Set(optionKeys).size === options.length, "Dimension options need a key, at most 200 distinct values.");
|
|
37
|
+
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.");
|
|
38
|
+
const allowed = new Set(optionKeys);
|
|
39
|
+
const maxSelected = config.maxSelected ?? (selectionMode === "single" ? 1 : 20);
|
|
40
|
+
invariant(Number.isInteger(maxSelected) && maxSelected >= 1 && maxSelected <= 50 && (selectionMode !== "single" || maxSelected === 1), "Dimension selection limit is invalid.");
|
|
41
|
+
const allowMissing = config.allowMissing ?? false;
|
|
42
|
+
const defaultValue = allSelection;
|
|
43
|
+
function validMember(member) {
|
|
44
|
+
if (!member || typeof member !== "object") return false;
|
|
45
|
+
if (member.kind === "missing") return allowMissing;
|
|
46
|
+
if (member.kind !== "value" || typeof member.value !== valueType) return false;
|
|
47
|
+
if (!dynamic) return allowed.has(dimensionMemberKey(member));
|
|
48
|
+
return typeof member.value === "number" ? Number.isFinite(member.value) : member.value.length <= 100;
|
|
49
|
+
}
|
|
50
|
+
function valid(value) {
|
|
51
|
+
if (!value || typeof value !== "object") return false;
|
|
52
|
+
if (value.kind === "all") return true;
|
|
53
|
+
if (value.kind !== "include" && !(config.allowExclusion && value.kind === "exclude") || !Array.isArray(value.members) || !value.members.length || value.members.length > maxSelected) return false;
|
|
54
|
+
if (!value.members.every(validMember)) return false;
|
|
55
|
+
const ids = value.members.map(dimensionMemberKey);
|
|
56
|
+
return new Set(ids).size === ids.length;
|
|
57
|
+
}
|
|
58
|
+
function encode(value) {
|
|
59
|
+
return value.kind === "all" ? null : JSON.stringify({
|
|
60
|
+
kind: value.kind,
|
|
61
|
+
members: value.members.map((member) => member.kind === "missing" ? ["m"] : ["v", member.value])
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
function parse(raw) {
|
|
65
|
+
if (raw === null) return defaultValue;
|
|
66
|
+
if (raw.length > 4096) return defaultValue;
|
|
67
|
+
try {
|
|
68
|
+
const parsed = JSON.parse(raw);
|
|
69
|
+
const tokens = Array.isArray(parsed) ? {
|
|
70
|
+
kind: "include",
|
|
71
|
+
members: parsed
|
|
72
|
+
} : parsed;
|
|
73
|
+
if (!tokens || !Array.isArray(tokens.members)) return defaultValue;
|
|
74
|
+
const value = {
|
|
75
|
+
kind: tokens.kind,
|
|
76
|
+
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" ? {
|
|
77
|
+
kind: "value",
|
|
78
|
+
value: token[1]
|
|
79
|
+
} : { kind: "invalid" })
|
|
80
|
+
};
|
|
81
|
+
return valid(value) ? value : defaultValue;
|
|
82
|
+
} catch {
|
|
83
|
+
return defaultValue;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
kind: "dimension",
|
|
88
|
+
optionsSource: dynamic ? "query" : "fixed",
|
|
89
|
+
presentation: config.presentation,
|
|
90
|
+
label,
|
|
91
|
+
valueType,
|
|
92
|
+
selectionMode,
|
|
93
|
+
options,
|
|
94
|
+
allowMissing,
|
|
95
|
+
allowExclusion: config.allowExclusion ?? false,
|
|
96
|
+
maxSelected,
|
|
97
|
+
urlKeys: [key],
|
|
98
|
+
defaultValue,
|
|
99
|
+
clearValue: allSelection,
|
|
100
|
+
history: config.history ?? "push",
|
|
101
|
+
valid,
|
|
102
|
+
same(left, right) {
|
|
103
|
+
return encode(left) === encode(right);
|
|
104
|
+
},
|
|
105
|
+
read(params) {
|
|
106
|
+
return parse(params.get(key));
|
|
107
|
+
},
|
|
108
|
+
write(value) {
|
|
109
|
+
return { [key]: encode(value) };
|
|
110
|
+
},
|
|
111
|
+
describe(value, suppliedOptions = options) {
|
|
112
|
+
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(", ")}`;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
/** Server-side parser; call from the operation input parser before building SQL. */
|
|
117
|
+
function parseDimensionSelection(value, filter) {
|
|
118
|
+
invariant(filter.valid(value), `Invalid ${filter.label} selection.`);
|
|
119
|
+
return value;
|
|
120
|
+
}
|
|
121
|
+
/** Validate bounded facet results before showing them in a picker. */
|
|
122
|
+
function parseFacetOptions(value, filter) {
|
|
123
|
+
invariant(Array.isArray(value) && value.length <= 200, "Facet options exceed their bound.");
|
|
124
|
+
const options = [];
|
|
125
|
+
const seen = /* @__PURE__ */ new Set();
|
|
126
|
+
for (const item of value) {
|
|
127
|
+
invariant(item && typeof item === "object", "Invalid facet option.");
|
|
128
|
+
const option = item;
|
|
129
|
+
invariant(typeof option.value === filter.valueType && filter.valid({
|
|
130
|
+
kind: "include",
|
|
131
|
+
members: [{
|
|
132
|
+
kind: "value",
|
|
133
|
+
value: option.value
|
|
134
|
+
}]
|
|
135
|
+
}) && 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.");
|
|
136
|
+
const id = dimensionMemberKey({
|
|
137
|
+
kind: "value",
|
|
138
|
+
value: option.value
|
|
139
|
+
});
|
|
140
|
+
invariant(!seen.has(id), "Duplicate facet option.");
|
|
141
|
+
seen.add(id);
|
|
142
|
+
options.push(option);
|
|
143
|
+
}
|
|
144
|
+
return options;
|
|
145
|
+
}
|
|
146
|
+
/** Central SQL literal encoding for transports without bound parameters. Column is an allowlisted identifier. */
|
|
147
|
+
function dimensionPredicate(column, selection, allowedColumns) {
|
|
148
|
+
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.");
|
|
149
|
+
if (selection.kind === "all") return "";
|
|
150
|
+
invariant(selection.members.length > 0, "Empty dimension inclusion is invalid.");
|
|
151
|
+
const values = selection.members.filter((member) => member.kind === "value");
|
|
152
|
+
const missing = selection.members.some((member) => member.kind === "missing");
|
|
153
|
+
const literals = values.map(({ value }) => {
|
|
154
|
+
if (typeof value === "number") {
|
|
155
|
+
invariant(Number.isFinite(value), "Invalid numeric dimension value.");
|
|
156
|
+
return String(value);
|
|
157
|
+
}
|
|
158
|
+
const text = value;
|
|
159
|
+
for (let index = 0; index < text.length; index++) invariant(text.charCodeAt(index) >= 32 && text.charCodeAt(index) !== 127, "Invalid control character in dimension value.");
|
|
160
|
+
return `'${text.replaceAll("'", "''")}'`;
|
|
161
|
+
});
|
|
162
|
+
const predicate = `(${[...literals.length ? [`${column} IN (${literals.join(", ")})`] : [], ...missing ? [`${column} IS NULL`] : []].join(" OR ")})`;
|
|
163
|
+
return selection.kind === "exclude" ? missing ? `NOT ${predicate}` : `(${column} IS NULL OR NOT ${predicate})` : predicate;
|
|
164
|
+
}
|
|
165
|
+
//#endregion
|
|
166
|
+
//#region src/core/queries.ts
|
|
167
|
+
const QUERY_CAST_ERROR = "##ERROR##";
|
|
168
|
+
function isQueryParameterValue(value) {
|
|
169
|
+
return value === null || typeof value === "string" || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value);
|
|
170
|
+
}
|
|
171
|
+
function isQueryParameters(value) {
|
|
172
|
+
return !!value && typeof value === "object" && !Array.isArray(value) && Object.values(value).every(isQueryParameterValue);
|
|
173
|
+
}
|
|
174
|
+
function snapshotField(field) {
|
|
175
|
+
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.");
|
|
176
|
+
return Object.freeze({ ...field });
|
|
177
|
+
}
|
|
178
|
+
/** Snapshot the query's SQL, parameter defaults, and decoded result contract. */
|
|
179
|
+
function snapshotQueries(definitions) {
|
|
180
|
+
return Object.freeze(Object.fromEntries(Object.entries(definitions).map(([name, query]) => {
|
|
181
|
+
invariant(!!name.trim() && !!query.statement.trim(), "Queries need a name and SQL statement.");
|
|
182
|
+
const params = Object.freeze(Object.fromEntries(Object.entries(query.params).map(([key, declaration]) => {
|
|
183
|
+
invariant(!Object.hasOwn(declaration, "defaultValue") || isQueryParameterValue(declaration.defaultValue), `Invalid default for parameter ${key} in query ${name}.`);
|
|
184
|
+
return [key, snapshotField(declaration)];
|
|
185
|
+
})));
|
|
186
|
+
const projectionEntries = Object.entries(query.projections);
|
|
187
|
+
invariant(projectionEntries.length > 0 && projectionEntries.every(([alias]) => !!alias.trim()), "Queries need at least one named projection.");
|
|
188
|
+
return [name, Object.freeze({
|
|
189
|
+
statement: query.statement,
|
|
190
|
+
params,
|
|
191
|
+
projections: Object.freeze(Object.fromEntries(projectionEntries.map(([alias, field]) => [alias, snapshotField(field)])))
|
|
192
|
+
})];
|
|
193
|
+
})));
|
|
194
|
+
}
|
|
195
|
+
/** Decode each returned cell once, matching aliases independently of result order. */
|
|
196
|
+
function decodeQueryResult(result, projections) {
|
|
197
|
+
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.");
|
|
198
|
+
const returnedColumns = Array.from(result.columns, (column) => {
|
|
199
|
+
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.");
|
|
200
|
+
return {
|
|
201
|
+
name: column.name,
|
|
202
|
+
...column.type === void 0 ? {} : { type: column.type }
|
|
203
|
+
};
|
|
204
|
+
});
|
|
205
|
+
const names = returnedColumns.map((column) => column.name);
|
|
206
|
+
return {
|
|
207
|
+
columns: returnedColumns,
|
|
208
|
+
rows: Array.from(result.rows, (row) => {
|
|
209
|
+
if (!Array.isArray(row) || row.length !== names.length) throw new Error("Query result row has the wrong width.");
|
|
210
|
+
return Object.fromEntries(names.map((name, index) => [name, castQueryCell(row[index], projections[name])]));
|
|
211
|
+
}),
|
|
212
|
+
...result.queryId === void 0 ? {} : { queryId: result.queryId }
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
function castQueryCell(value, field) {
|
|
216
|
+
if (field === void 0) {
|
|
217
|
+
if (typeof value === "string" || typeof value === "boolean" || typeof value === "number") return value;
|
|
218
|
+
if (value === null) return null;
|
|
219
|
+
return JSON.stringify(value);
|
|
220
|
+
}
|
|
221
|
+
if (value === null && field.nullable !== false) return null;
|
|
222
|
+
switch (field.type) {
|
|
223
|
+
case "string": return String(value);
|
|
224
|
+
case "number": {
|
|
225
|
+
const number = Number(value);
|
|
226
|
+
return Number.isFinite(number) ? number : QUERY_CAST_ERROR;
|
|
227
|
+
}
|
|
228
|
+
case "boolean": return Boolean(value);
|
|
229
|
+
case "null": return null;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
/** Host context supplies protected values; callers and defaults fill the remaining parameters. */
|
|
233
|
+
function resolveQueryParameters(declarations, values, context) {
|
|
234
|
+
if (!values || typeof values !== "object" || Array.isArray(values)) throw new Error("Supply query parameter values as an object.");
|
|
235
|
+
for (const key of Object.keys(values)) {
|
|
236
|
+
if (!Object.hasOwn(declarations, key)) throw new Error(`Unknown parameter ${key}.`);
|
|
237
|
+
if (Object.hasOwn(context, key)) throw new Error(`Cannot override context parameter ${key}.`);
|
|
238
|
+
}
|
|
239
|
+
return Object.fromEntries(Object.entries(declarations).map(([key, declaration]) => {
|
|
240
|
+
const value = Object.hasOwn(context, key) ? context[key] : Object.hasOwn(values, key) && values[key] !== void 0 ? values[key] : declaration.defaultValue;
|
|
241
|
+
if (value === void 0) throw new Error(`Missing parameter ${key}.`);
|
|
242
|
+
const nullable = declaration.type === "null" || declaration.nullable === true;
|
|
243
|
+
if (!(value === null ? nullable : typeof value === declaration.type && isQueryParameterValue(value))) throw new Error(`Parameter ${key} does not match ${declaration.type}.`);
|
|
244
|
+
return [key, value];
|
|
245
|
+
}));
|
|
246
|
+
}
|
|
247
|
+
//#endregion
|
|
248
|
+
//#region src/core/dimension-source.ts
|
|
249
|
+
const dimensionSource = Symbol("data-app.dimensionSource");
|
|
250
|
+
const sources = /* @__PURE__ */ new WeakMap();
|
|
251
|
+
const parserVariables = /* @__PURE__ */ new WeakMap();
|
|
252
|
+
const selectorsKey = Symbol.for("@altertable/data-app/dimension-selectors");
|
|
253
|
+
const ownerKey = Symbol.for("@altertable/data-app/dimension-selector-owner");
|
|
254
|
+
const invalidMemberKey = Symbol.for("@altertable/data-app/dimension-member-error");
|
|
255
|
+
var DimensionMembershipError = class extends Error {
|
|
256
|
+
[invalidMemberKey] = true;
|
|
257
|
+
static [Symbol.hasInstance](value) {
|
|
258
|
+
return value instanceof Error && value[invalidMemberKey] === true;
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
/** Validate named variable values at the operation boundary. Date inputs belong to the base parser. */
|
|
262
|
+
function variableInput(parse, variables) {
|
|
263
|
+
const previous = parserVariables.get(parse) ?? {};
|
|
264
|
+
const definitions = { ...variables };
|
|
265
|
+
for (const [name, variable] of Object.entries(definitions)) invariant(!previous[name] || previous[name] === variable, `Variable input ${name} is already declared.`);
|
|
266
|
+
function parser(value) {
|
|
267
|
+
invariant(value && typeof value === "object" && !Array.isArray(value), "Expected variable input.");
|
|
268
|
+
const input = parse(value);
|
|
269
|
+
invariant(input && typeof input === "object" && !Array.isArray(input), "Expected parsed input object.");
|
|
270
|
+
const values = {};
|
|
271
|
+
for (const [name, variable] of Object.entries(definitions)) {
|
|
272
|
+
const selected = value[name];
|
|
273
|
+
invariant(variable.valid(selected), `Invalid ${variable.label ?? name}.`);
|
|
274
|
+
values[name] = selected;
|
|
275
|
+
}
|
|
276
|
+
return {
|
|
277
|
+
...input,
|
|
278
|
+
...values
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
parserVariables.set(parser, {
|
|
282
|
+
...previous,
|
|
283
|
+
...definitions
|
|
284
|
+
});
|
|
285
|
+
return parser;
|
|
286
|
+
}
|
|
287
|
+
/** A query supplies independent options and exact member lookup; it declares a nullable `member` parameter. */
|
|
288
|
+
function bindDimensionSource(config, queries) {
|
|
289
|
+
invariant(!config.allowMissing, "Source dimensions require concrete members.");
|
|
290
|
+
const query = queries[config.source.query];
|
|
291
|
+
invariant(query && Object.hasOwn(query.params, "member"), "Dimension sources must declare member.");
|
|
292
|
+
const member = query.params.member;
|
|
293
|
+
const { value, label, count } = query.projections;
|
|
294
|
+
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.");
|
|
295
|
+
const operation = `__dimension:${config.key}`;
|
|
296
|
+
const variable = Object.assign(createDimensionFilter({
|
|
297
|
+
...config,
|
|
298
|
+
valueType: config.valueType,
|
|
299
|
+
options: []
|
|
300
|
+
}, true), {
|
|
301
|
+
optionsSource: "query",
|
|
302
|
+
[dimensionSource]: Object.freeze({ operation })
|
|
303
|
+
});
|
|
304
|
+
sources.set(variable, {
|
|
305
|
+
query: config.source.query,
|
|
306
|
+
queries
|
|
307
|
+
});
|
|
308
|
+
return variable;
|
|
309
|
+
}
|
|
310
|
+
async function loadOptions(variable, member, query) {
|
|
311
|
+
const result = await query(sources.get(variable).query, { member }, { limit: member === null ? 200 : 2 });
|
|
312
|
+
const names = result.columns.map((column) => column.name);
|
|
313
|
+
invariant(new Set(names).size === names.length && names.includes("value") && names.includes("label"), "Dimension source must return value and label.");
|
|
314
|
+
const options = parseFacetOptions(result.rows, variable);
|
|
315
|
+
if (member !== null && (options.length !== 1 || options[0].value !== member)) throw new DimensionMembershipError(`Invalid ${variable.label} member.`);
|
|
316
|
+
return options;
|
|
317
|
+
}
|
|
318
|
+
async function validateVariableInput(parser, input, query) {
|
|
319
|
+
for (const [name, variable] of Object.entries(parserVariables.get(parser) ?? {})) {
|
|
320
|
+
if (variable.kind !== "dimension" || !sources.has(variable)) continue;
|
|
321
|
+
const selection = input[name];
|
|
322
|
+
if (selection.kind === "all") continue;
|
|
323
|
+
for (const member of selection.members) if (member.kind === "value") await loadOptions(variable, member.value, query);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
function attachDimensionSelectors(operation, queries) {
|
|
327
|
+
const selectors = {};
|
|
328
|
+
for (const variable of Object.values(parserVariables.get(operation.input) ?? {})) {
|
|
329
|
+
if (variable.kind !== "dimension" || !(dimensionSource in variable)) continue;
|
|
330
|
+
const source = sources.get(variable);
|
|
331
|
+
if (!source) continue;
|
|
332
|
+
invariant(JSON.stringify(source.queries[source.query]) === JSON.stringify(queries[source.query]), "Dimension sources must belong to the operation query registry.");
|
|
333
|
+
const name = variable[dimensionSource].operation;
|
|
334
|
+
invariant(!selectors[name] || selectors[name][ownerKey] === variable, "Dimension source keys must be unique across the app.");
|
|
335
|
+
selectors[name] = {
|
|
336
|
+
input(value) {
|
|
337
|
+
invariant(value && typeof value === "object", "Invalid dimension request.");
|
|
338
|
+
const member = value.member;
|
|
339
|
+
invariant(member === null || variable.valid({
|
|
340
|
+
kind: "include",
|
|
341
|
+
members: [{
|
|
342
|
+
kind: "value",
|
|
343
|
+
value: member
|
|
344
|
+
}]
|
|
345
|
+
}), "Invalid requested member.");
|
|
346
|
+
return { member };
|
|
347
|
+
},
|
|
348
|
+
checks: [{ member: null }],
|
|
349
|
+
policy: {
|
|
350
|
+
maxDurationMs: 15e3,
|
|
351
|
+
maxQueryRows: 200,
|
|
352
|
+
maxResponseBytes: 1e6
|
|
353
|
+
},
|
|
354
|
+
queryNames: { [source.query]: source.query },
|
|
355
|
+
async run(context, input) {
|
|
356
|
+
return loadOptions(variable, input.member, async (name, params, options) => {
|
|
357
|
+
const definition = queries[name];
|
|
358
|
+
return decodeQueryResult(await context.lakehouse.queryAll(definition.statement, {
|
|
359
|
+
name,
|
|
360
|
+
params: resolveQueryParameters(definition.params, params, context.queryParams ?? {}),
|
|
361
|
+
limit: options.limit,
|
|
362
|
+
signal: context.signal
|
|
363
|
+
}), definition.projections);
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
Object.defineProperty(selectors[name], ownerKey, { value: variable });
|
|
368
|
+
}
|
|
369
|
+
Object.defineProperty(operation, selectorsKey, { value: selectors });
|
|
370
|
+
}
|
|
371
|
+
function withDimensionSelectors(operations) {
|
|
372
|
+
const expanded = { ...operations };
|
|
373
|
+
for (const operation of Object.values(operations)) for (const [name, selector] of Object.entries(operation[selectorsKey] ?? {})) {
|
|
374
|
+
invariant(!Object.hasOwn(operations, name), "Reserved dimension operation name.");
|
|
375
|
+
invariant(!expanded[name] || expanded[name][ownerKey] === selector[ownerKey], "Dimension source keys must be unique across the app.");
|
|
376
|
+
expanded[name] ??= selector;
|
|
377
|
+
}
|
|
378
|
+
return expanded;
|
|
379
|
+
}
|
|
380
|
+
//#endregion
|
|
381
|
+
//#region src/authoring/contract.ts
|
|
382
|
+
/**
|
|
383
|
+
* Shared operation contracts and input validation for browser and server.
|
|
384
|
+
* @module @altertable/data-app/contract
|
|
385
|
+
* @see https://github.com/altertable-ai/data-app/blob/main/docs/contract.md
|
|
386
|
+
*/
|
|
387
|
+
function parseEmptyInput(value) {
|
|
388
|
+
invariant(typeof value === "object" && value !== null && !Array.isArray(value) && Object.keys(value).length === 0, "This operation takes no inputs.");
|
|
389
|
+
return {};
|
|
390
|
+
}
|
|
391
|
+
function parseTrue(value) {
|
|
392
|
+
invariant(value === true, "Expected a successful check.");
|
|
393
|
+
return true;
|
|
394
|
+
}
|
|
395
|
+
/** The immediately preceding, equally long set of calendar days. */
|
|
396
|
+
function previousDateRange({ start, end }) {
|
|
397
|
+
const first = parseDate(start);
|
|
398
|
+
const days = (parseDate(end).toDate("UTC").getTime() - first.toDate("UTC").getTime()) / 864e5 + 1;
|
|
399
|
+
invariant(days >= 1, "Date range must start before it ends.");
|
|
400
|
+
const previousEnd = first.subtract({ days: 1 });
|
|
401
|
+
return {
|
|
402
|
+
start: previousEnd.subtract({ days: days - 1 }).toString(),
|
|
403
|
+
end: previousEnd.toString()
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
/** One source policy for the server parser, URL variable, and displayed reporting period. */
|
|
407
|
+
function defineDateRangeContract(options) {
|
|
408
|
+
invariant(Number.isInteger(options.maxRangeDays) && options.maxRangeDays > 0, "Date range needs a positive maxRangeDays.");
|
|
409
|
+
if (options.minDate) parseDate(options.minDate);
|
|
410
|
+
if (typeof options.maxDate === "string") parseDate(options.maxDate);
|
|
411
|
+
today(options.timeZone);
|
|
412
|
+
function bounds() {
|
|
413
|
+
const latest = today(options.timeZone).subtract({ days: options.completeDays === false ? 0 : 1 });
|
|
414
|
+
const sourceMax = typeof options.maxDate === "function" ? options.maxDate() : options.maxDate;
|
|
415
|
+
const maxDate = sourceMax && parseDate(sourceMax).compare(latest) < 0 ? parseDate(sourceMax).toString() : latest.toString();
|
|
416
|
+
return {
|
|
417
|
+
minDate: options.minDate,
|
|
418
|
+
maxDate,
|
|
419
|
+
maxRangeDays: options.maxRangeDays,
|
|
420
|
+
timeZone: options.timeZone
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
function period({ start, end }) {
|
|
424
|
+
return {
|
|
425
|
+
kind: "calendar",
|
|
426
|
+
start,
|
|
427
|
+
end,
|
|
428
|
+
timeZone: options.timeZone
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
function describeInput(input) {
|
|
432
|
+
return `${formatDateRange(input)} ${options.timeZone}`;
|
|
433
|
+
}
|
|
434
|
+
function comparison(input) {
|
|
435
|
+
const previous = previousDateRange(input);
|
|
436
|
+
return options.minDate && previous.start < options.minDate ? null : previous;
|
|
437
|
+
}
|
|
438
|
+
function request(value, compare = false) {
|
|
439
|
+
const range = parseDateRangeInput(value, bounds());
|
|
440
|
+
const previous = compare ? comparison(range) : null;
|
|
441
|
+
invariant(!compare || previous, "Comparison is outside available source coverage.");
|
|
442
|
+
return {
|
|
443
|
+
range,
|
|
444
|
+
comparison: previous
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
function parseRequest(value) {
|
|
448
|
+
invariant(value && typeof value === "object", "Choose a date range.");
|
|
449
|
+
const input = value;
|
|
450
|
+
const parsed = request(input.range, input.comparison != null);
|
|
451
|
+
if (input.comparison != null) {
|
|
452
|
+
const supplied = parseDateRangeInput(input.comparison, bounds());
|
|
453
|
+
invariant(supplied.start === parsed.comparison?.start && supplied.end === parsed.comparison?.end, "Comparison must be the preceding equal-length range.");
|
|
454
|
+
}
|
|
455
|
+
return parsed;
|
|
456
|
+
}
|
|
457
|
+
return {
|
|
458
|
+
request,
|
|
459
|
+
parseRequest,
|
|
460
|
+
bounds,
|
|
461
|
+
parse(value) {
|
|
462
|
+
return parseDateRangeInput(value, bounds());
|
|
463
|
+
},
|
|
464
|
+
period,
|
|
465
|
+
comparison,
|
|
466
|
+
describeInput,
|
|
467
|
+
view: {
|
|
468
|
+
period,
|
|
469
|
+
describeInput
|
|
470
|
+
}
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
/** Validate calendar dates and a bounded inclusive range on the server. */
|
|
474
|
+
function parseDateRangeInput(value, { minDate, maxDate, maxRangeDays }) {
|
|
475
|
+
invariant(value && typeof value === "object" && !Array.isArray(value), "Choose a date range.");
|
|
476
|
+
const { start, end } = value;
|
|
477
|
+
function day(input) {
|
|
478
|
+
invariant(typeof input === "string" && /^\d{4}-\d{2}-\d{2}$/.test(input), "Dates must be YYYY-MM-DD.");
|
|
479
|
+
const timestamp = Date.parse(`${input}T00:00:00Z`);
|
|
480
|
+
invariant(Number.isFinite(timestamp) && new Date(timestamp).toISOString().slice(0, 10) === input, "Choose valid calendar dates.");
|
|
481
|
+
return timestamp;
|
|
482
|
+
}
|
|
483
|
+
const first = day(start);
|
|
484
|
+
const last = day(end);
|
|
485
|
+
invariant(Number.isInteger(maxRangeDays) && maxRangeDays > 0, "Maximum range days must be positive.");
|
|
486
|
+
invariant(first <= last && (last - first) / 864e5 < maxRangeDays && (minDate === void 0 || first >= day(minDate)) && (maxDate === void 0 || last <= day(maxDate)), `Choose up to ${maxRangeDays} days within the available range.`);
|
|
487
|
+
return {
|
|
488
|
+
start,
|
|
489
|
+
end
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
/** Accepts numeric strings from DuckDB; rejects negative, fractional, and unsafe integers. */
|
|
493
|
+
function parseCount(value) {
|
|
494
|
+
const number = typeof value === "number" || typeof value === "string" ? Number(value) : NaN;
|
|
495
|
+
invariant(Number.isSafeInteger(number) && number >= 0 && (typeof value !== "string" || !!value.trim()), "Invalid count in query result.");
|
|
496
|
+
return number;
|
|
497
|
+
}
|
|
498
|
+
function parseLabel(value, maxLength = 100) {
|
|
499
|
+
invariant(typeof value === "string" && !!value && value.length <= maxLength, "Invalid label in query result.");
|
|
500
|
+
return value;
|
|
501
|
+
}
|
|
502
|
+
function defineQueryNames(names) {
|
|
503
|
+
const values = Object.values(names);
|
|
504
|
+
invariant(values.every((name) => !!name.trim()) && new Set(values).size === values.length, "Query names must be nonempty and unique.");
|
|
505
|
+
return names;
|
|
506
|
+
}
|
|
507
|
+
function defineOperation(operation) {
|
|
508
|
+
invariant(Array.isArray(operation.checks) && operation.checks.length > 0 && Number.isInteger(operation.policy.maxQueryRows) && operation.policy.maxQueryRows > 0 && Number.isInteger(operation.policy.maxDurationMs) && operation.policy.maxDurationMs > 0 && (operation.policy.maxResponseBytes === void 0 || Number.isInteger(operation.policy.maxResponseBytes) && operation.policy.maxResponseBytes > 0), "Each data operation needs check inputs and positive row and duration limits.");
|
|
509
|
+
const queries = snapshotQueries(operation.queries);
|
|
510
|
+
const queryNames = Object.fromEntries(Object.keys(queries).map((name) => [name, name]));
|
|
511
|
+
for (const input of operation.checks) operation.input(input);
|
|
512
|
+
const defined = {
|
|
513
|
+
...operation,
|
|
514
|
+
queryNames,
|
|
515
|
+
async run(context, input) {
|
|
516
|
+
async function query(name, values = {}, options) {
|
|
517
|
+
invariant(Object.hasOwn(queries, name), `Unknown query name: ${name}.`);
|
|
518
|
+
const definition = queries[name];
|
|
519
|
+
const params = resolveQueryParameters(definition.params, values, context.queryParams ?? {});
|
|
520
|
+
return decodeQueryResult(await context.lakehouse.queryAll(definition.statement, {
|
|
521
|
+
name,
|
|
522
|
+
params,
|
|
523
|
+
limit: options?.limit ?? operation.policy.maxQueryRows,
|
|
524
|
+
signal: context.signal
|
|
525
|
+
}), definition.projections);
|
|
526
|
+
}
|
|
527
|
+
await validateVariableInput(operation.input, input, query);
|
|
528
|
+
context.signal.throwIfAborted();
|
|
529
|
+
return operation.run({
|
|
530
|
+
signal: context.signal,
|
|
531
|
+
query
|
|
532
|
+
}, input);
|
|
533
|
+
}
|
|
534
|
+
};
|
|
535
|
+
attachDimensionSelectors(defined, queries);
|
|
536
|
+
return defined;
|
|
537
|
+
}
|
|
538
|
+
/** A connectivity probe uses the app's registered connection query. */
|
|
539
|
+
function connectionCheck(queries) {
|
|
540
|
+
const connection = queries.connection;
|
|
541
|
+
return defineOperation({
|
|
542
|
+
queries: { connection },
|
|
543
|
+
input: parseEmptyInput,
|
|
544
|
+
checks: [{}],
|
|
545
|
+
policy: {
|
|
546
|
+
maxQueryRows: 1,
|
|
547
|
+
maxDurationMs: 15e3
|
|
548
|
+
},
|
|
549
|
+
async run({ query }) {
|
|
550
|
+
await query("connection");
|
|
551
|
+
return true;
|
|
552
|
+
}
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
//#endregion
|
|
556
|
+
export { parseFacetOptions as C, parseDimensionSelection as S, invariant as T, snapshotQueries as _, parseCount as a, dimensionMemberKey as b, parseLabel as c, DimensionMembershipError as d, bindDimensionSource as f, isQueryParameters as g, withDimensionSelectors as h, defineQueryNames as i, parseTrue as l, variableInput as m, defineDateRangeContract as n, parseDateRangeInput as o, dimensionSource as p, defineOperation as r, parseEmptyInput as s, connectionCheck as t, previousDateRange as u, createDimensionFilter as v, KEY_SEPARATOR as w, dimensionPredicate as x, dimensionFilter as y };
|
|
557
|
+
|
|
558
|
+
//# sourceMappingURL=contract-BeLPJwlO.js.map
|