@makoai/app-sdk 2.7.3 → 2.8.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/README.md +73 -5
- package/data-core.js +212 -0
- package/index.d.ts +199 -5
- package/index.js +352 -98
- package/package.json +6 -1
- package/progress.js +233 -0
- package/ui.js +5 -2
- package/vite.d.ts +5 -3
- package/vite.js +179 -18
package/index.js
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
// @makoai/app-sdk — see package.json. Plain ESM on purpose: no build step.
|
|
2
2
|
import * as React from "react";
|
|
3
|
+
import {
|
|
4
|
+
PROGRESS_ACCEPT,
|
|
5
|
+
combineBindingProgress,
|
|
6
|
+
isProgressResponse,
|
|
7
|
+
progressError,
|
|
8
|
+
readBytes,
|
|
9
|
+
readProgressStream,
|
|
10
|
+
} from "./progress.js";
|
|
11
|
+
export {
|
|
12
|
+
BINDING_PHASES,
|
|
13
|
+
BINDING_PHASE_LABELS,
|
|
14
|
+
BINDING_STAGE_LABELS,
|
|
15
|
+
bindingPhaseLabel,
|
|
16
|
+
bindingPhaseStage,
|
|
17
|
+
} from "./progress.js";
|
|
18
|
+
|
|
19
|
+
import {
|
|
20
|
+
missingBinding,
|
|
21
|
+
mentionedBindings,
|
|
22
|
+
quoteIdent,
|
|
23
|
+
readsCatalog,
|
|
24
|
+
quoteString,
|
|
25
|
+
toPlainRows,
|
|
26
|
+
} from "./data-core.js";
|
|
3
27
|
|
|
4
28
|
const DEFAULT_ROW_LIMIT = 500000;
|
|
5
29
|
const DUCKDB_ESM =
|
|
@@ -8,21 +32,48 @@ const DUCKDB_ESM =
|
|
|
8
32
|
// ---------------------------------------------------------------------------
|
|
9
33
|
// DuckDB: one lazy instance per page, bindings registered on first use. The
|
|
10
34
|
// engine arrives from the CDN at runtime, so this package needs no install
|
|
11
|
-
// step and no dependencies of its own.
|
|
35
|
+
// step and no dependencies of its own. The first data hook to render starts
|
|
36
|
+
// the boot (WASM download + instantiate, ~1 s) so it overlaps the first
|
|
37
|
+
// parquet download instead of waiting for it.
|
|
12
38
|
// ---------------------------------------------------------------------------
|
|
13
39
|
let dbPromise = null;
|
|
14
40
|
function getDb() {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
41
|
+
if (!dbPromise) {
|
|
42
|
+
dbPromise = (async () => {
|
|
43
|
+
const duckdb = await import(/* @vite-ignore */ DUCKDB_ESM);
|
|
44
|
+
const bundle = await duckdb.selectBundle(duckdb.getJsDelivrBundles());
|
|
45
|
+
const worker = await duckdb.createWorker(bundle.mainWorker);
|
|
46
|
+
const db = new duckdb.AsyncDuckDB(new duckdb.VoidLogger(), worker);
|
|
47
|
+
await db.instantiate(bundle.mainModule, bundle.pthreadWorker);
|
|
48
|
+
// read_parquet lives in an extension DuckDB fetches on first use
|
|
49
|
+
// (~660 kB from extensions.duckdb.org). Load it now, while the
|
|
50
|
+
// binding bytes are still downloading, not after they arrive.
|
|
51
|
+
const conn = await db.connect();
|
|
52
|
+
try {
|
|
53
|
+
await conn.query("LOAD parquet");
|
|
54
|
+
} catch {
|
|
55
|
+
/* autoload on first read_parquet still applies */
|
|
56
|
+
} finally {
|
|
57
|
+
await conn.close();
|
|
58
|
+
}
|
|
59
|
+
return db;
|
|
60
|
+
})();
|
|
61
|
+
// A failed boot (CDN unreachable) is retried by the next query.
|
|
62
|
+
dbPromise.catch(() => {
|
|
63
|
+
dbPromise = null;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
23
66
|
return dbPromise;
|
|
24
67
|
}
|
|
25
68
|
|
|
69
|
+
/** Start the engine early; a no-op outside a browser and once started. */
|
|
70
|
+
function bootEngine() {
|
|
71
|
+
if (typeof window === "undefined" || typeof Worker === "undefined") return;
|
|
72
|
+
getDb().catch(() => {
|
|
73
|
+
/* the query that needs it reports the failure */
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
26
77
|
// The server answers failures with JSON { error, hint, retryAfterMs } —
|
|
27
78
|
// show that, not a bare status code (and never let it reach DuckDB, where it
|
|
28
79
|
// would surface as a baffling Catalog Error).
|
|
@@ -40,51 +91,221 @@ async function responseError(res, what) {
|
|
|
40
91
|
return error;
|
|
41
92
|
}
|
|
42
93
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
94
|
+
// ---------------------------------------------------------------------------
|
|
95
|
+
// Progress — what each pending binding is waiting on (see progress.js). One
|
|
96
|
+
// record per binding while it loads or refreshes, gone when it settles.
|
|
97
|
+
// Notifications are batched: 34 bindings downloading at once must not
|
|
98
|
+
// re-render every hook on every chunk.
|
|
99
|
+
// ---------------------------------------------------------------------------
|
|
100
|
+
let progressState = {};
|
|
101
|
+
const progressListeners = new Set();
|
|
102
|
+
const progressWatchers = new Map(); // name -> Set<(record|null) => void>
|
|
103
|
+
let progressFlush = null;
|
|
104
|
+
function setProgress(name, patch) {
|
|
105
|
+
const next = { ...progressState };
|
|
106
|
+
if (patch === null) delete next[name];
|
|
107
|
+
else next[name] = { ...progressState[name], ...patch };
|
|
108
|
+
progressState = next;
|
|
109
|
+
const watchers = progressWatchers.get(name);
|
|
110
|
+
if (watchers) for (const w of [...watchers]) w(next[name] ?? null);
|
|
111
|
+
progressFlush ??= setTimeout(() => {
|
|
112
|
+
progressFlush = null;
|
|
113
|
+
for (const l of [...progressListeners]) l();
|
|
114
|
+
}, 50);
|
|
115
|
+
}
|
|
116
|
+
function subscribeProgress(l) {
|
|
117
|
+
progressListeners.add(l);
|
|
118
|
+
return () => progressListeners.delete(l);
|
|
119
|
+
}
|
|
120
|
+
function getProgressState() {
|
|
121
|
+
return progressState;
|
|
122
|
+
}
|
|
123
|
+
function watchProgress(name, fn) {
|
|
124
|
+
if (!progressWatchers.has(name)) progressWatchers.set(name, new Set());
|
|
125
|
+
progressWatchers.get(name).add(fn);
|
|
126
|
+
return () => progressWatchers.get(name)?.delete(fn);
|
|
127
|
+
}
|
|
128
|
+
/** A server event as a progress patch: the phase and whatever it counted. */
|
|
129
|
+
function progressPatch(event) {
|
|
130
|
+
return {
|
|
131
|
+
phase: event.phase,
|
|
132
|
+
...(typeof event.rowsWritten === "number" ? { rowsWritten: event.rowsWritten } : {}),
|
|
133
|
+
...(typeof event.bytes === "number" ? { bytes: event.bytes } : {}),
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Live progress: one binding's record (null when it is not loading), or
|
|
139
|
+
* every pending binding's when called without a name.
|
|
140
|
+
*/
|
|
141
|
+
export function useBindingProgress(name) {
|
|
142
|
+
const all = React.useSyncExternalStore(subscribeProgress, getProgressState, getProgressState);
|
|
143
|
+
return name === undefined ? all : (all[name] ?? null);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Each binding becomes a TABLE, decoded from its parquet once: a view over
|
|
147
|
+
// read_parquet re-decoded the file on every query (SELECT * of a 149k-row
|
|
148
|
+
// binding: 366 ms through a view, 174 ms from a table). The file buffer is
|
|
149
|
+
// dropped as soon as the table exists. CREATE OR REPLACE swaps the new table
|
|
150
|
+
// in with one commit, so a query running during a reload reads the old rows
|
|
151
|
+
// or the new ones, never a missing table. Every load registers its bytes
|
|
152
|
+
// under its own file name, so a reload never drops a file another load is
|
|
153
|
+
// still reading.
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* A binding's parquet bytes, phase-aware: asks for NDJSON phase events (a
|
|
157
|
+
* build reports each step, a stored file resolving → signing and a signed
|
|
158
|
+
* URL) and reports them through `report(patch)`. A server that predates the
|
|
159
|
+
* events answers with the bytes, as it always did.
|
|
160
|
+
*/
|
|
161
|
+
async function downloadBinding(name, bust, report) {
|
|
162
|
+
const path = "__data/" + encodeURIComponent(name) + ".parquet";
|
|
163
|
+
const what = 'Data for binding "' + name + '" could not be loaded';
|
|
164
|
+
report({ phase: "requesting", rowsWritten: null, bytes: null, bytesTotal: null });
|
|
165
|
+
let res = await fetch(path + (bust ? "?refresh=" + Date.now() : ""), {
|
|
166
|
+
headers: { accept: PROGRESS_ACCEPT },
|
|
167
|
+
});
|
|
168
|
+
if (isProgressResponse(res)) {
|
|
169
|
+
const final = await readProgressStream(res, e => report(progressPatch(e)));
|
|
170
|
+
if (final.phase === "failed") throw progressError(final, what);
|
|
171
|
+
report({ phase: "downloading", rowsWritten: null, bytes: 0 });
|
|
172
|
+
// No url: the server streams the bytes itself — the same path, asked
|
|
173
|
+
// plainly, serves them (the build already staged or cached them).
|
|
174
|
+
res = await fetch(final.url || path);
|
|
49
175
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
176
|
+
if (!res.ok) throw await responseError(res, what);
|
|
177
|
+
return readBytes(res, (bytes, bytesTotal) =>
|
|
178
|
+
report({ phase: "downloading", rowsWritten: null, bytes, bytesTotal }),
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
let fileSeq = 0;
|
|
183
|
+
async function loadBinding(name, bust) {
|
|
184
|
+
const db = getDb(); // boot in parallel with the download
|
|
185
|
+
db.catch(() => {});
|
|
55
186
|
try {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
);
|
|
187
|
+
const buf = await downloadBinding(name, bust, patch => setProgress(name, patch));
|
|
188
|
+
setProgress(name, { phase: "loading_duckdb" });
|
|
189
|
+
const engine = await db;
|
|
190
|
+
const file = "mako-" + ++fileSeq + ".parquet";
|
|
191
|
+
await engine.registerFileBuffer(file, buf);
|
|
192
|
+
try {
|
|
193
|
+
const conn = await engine.connect();
|
|
194
|
+
try {
|
|
195
|
+
await conn.query(
|
|
196
|
+
"CREATE OR REPLACE TABLE " + quoteIdent(name) +
|
|
197
|
+
" AS SELECT * FROM read_parquet(" + quoteString(file) + ")",
|
|
198
|
+
);
|
|
199
|
+
} finally {
|
|
200
|
+
await conn.close();
|
|
201
|
+
}
|
|
202
|
+
} finally {
|
|
203
|
+
await engine.dropFile(file).catch(() => {});
|
|
204
|
+
}
|
|
61
205
|
} finally {
|
|
62
|
-
|
|
206
|
+
// A refresh owns the record until its reload is done (refreshBinding).
|
|
207
|
+
if (!refreshes.has(name)) setProgress(name, null);
|
|
63
208
|
}
|
|
64
209
|
}
|
|
65
210
|
|
|
66
211
|
const registered = new Map(); // name -> Promise<void>
|
|
212
|
+
// Refreshed on the server while never loaded on this page: the next load
|
|
213
|
+
// must skip any cache between here and the server.
|
|
214
|
+
const staleBindings = new Set();
|
|
67
215
|
function registerBinding(name) {
|
|
68
216
|
if (!registered.has(name)) {
|
|
69
|
-
const load = loadBinding(name,
|
|
217
|
+
const load = loadBinding(name, staleBindings.delete(name));
|
|
70
218
|
registered.set(name, load);
|
|
71
|
-
load.catch(() =>
|
|
219
|
+
load.catch(() => {
|
|
220
|
+
if (registered.get(name) === load) registered.delete(name);
|
|
221
|
+
});
|
|
72
222
|
}
|
|
73
223
|
return registered.get(name);
|
|
74
224
|
}
|
|
75
225
|
|
|
226
|
+
// ---------------------------------------------------------------------------
|
|
227
|
+
// Escape hatches — the same engine and the same loader the hooks use, for
|
|
228
|
+
// apps that need more than useQuery/useDuckDB. One DuckDB per page: an app
|
|
229
|
+
// that boots its own pays a second ~1 s boot and a second copy of the data.
|
|
230
|
+
// ---------------------------------------------------------------------------
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* The page's shared DuckDB (an AsyncDuckDB), booted on first call. Never
|
|
234
|
+
* close or terminate it: every hook on the page reads from it.
|
|
235
|
+
*/
|
|
236
|
+
export function getDuckDB() {
|
|
237
|
+
return getDb();
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** Run `fn` with a fresh connection to the shared DuckDB; always closed after. */
|
|
241
|
+
export async function withConnection(fn) {
|
|
242
|
+
const db = await getDb();
|
|
243
|
+
const conn = await db.connect();
|
|
244
|
+
try {
|
|
245
|
+
return await fn(conn);
|
|
246
|
+
} finally {
|
|
247
|
+
await conn.close();
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Make sure a binding's table exists in the shared DuckDB, loading it once
|
|
253
|
+
* per page (concurrent callers share the download). Resolves with the table
|
|
254
|
+
* name — the binding name; quote it in SQL when it is not a bare identifier.
|
|
255
|
+
*/
|
|
256
|
+
export async function ensureBinding(name) {
|
|
257
|
+
await registerBinding(name);
|
|
258
|
+
return name;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Forget a loaded binding: drops its table so its memory is freed. The next
|
|
263
|
+
* query or ensureBinding() downloads it again.
|
|
264
|
+
*/
|
|
265
|
+
export async function unloadBinding(name) {
|
|
266
|
+
const pending = registered.get(name);
|
|
267
|
+
if (!pending) return;
|
|
268
|
+
registered.delete(name);
|
|
269
|
+
await pending.catch(() => {});
|
|
270
|
+
await withConnection(conn => conn.query("DROP TABLE IF EXISTS " + quoteIdent(name)));
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* The binding's parquet bytes, for an app with its own reader (e.g.
|
|
275
|
+
* read_parquet options such as file_row_number). Not registered anywhere.
|
|
276
|
+
* `fresh` skips caches; `onProgress(loaded, total)` reports the download.
|
|
277
|
+
*/
|
|
278
|
+
export async function fetchBindingBytes(name, opts) {
|
|
279
|
+
const fresh = !!(opts && opts.fresh) || staleBindings.has(name);
|
|
280
|
+
const onProgress = opts && opts.onProgress;
|
|
281
|
+
let record = null;
|
|
282
|
+
try {
|
|
283
|
+
return await downloadBinding(name, fresh, patch => {
|
|
284
|
+
record = { ...record, ...patch };
|
|
285
|
+
if (onProgress) onProgress(record);
|
|
286
|
+
});
|
|
287
|
+
} finally {
|
|
288
|
+
if (onProgress) onProgress(null);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
76
292
|
/** Replace a binding's loaded bytes with what the server has now. */
|
|
77
293
|
async function reloadBinding(name) {
|
|
294
|
+
const previous = registered.get(name);
|
|
295
|
+
if (!previous) {
|
|
296
|
+
// Nothing on this page reads it yet: load it fresh when a query does.
|
|
297
|
+
staleBindings.add(name);
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
78
300
|
// Let a load in flight settle first, or its (older) bytes could land after
|
|
79
301
|
// the fresh ones and win.
|
|
80
|
-
|
|
81
|
-
if (previous) await previous.catch(() => {});
|
|
302
|
+
await previous.catch(() => {});
|
|
82
303
|
const load = loadBinding(name, true);
|
|
83
304
|
registered.set(name, load);
|
|
84
305
|
try {
|
|
85
306
|
await load;
|
|
86
307
|
} catch (error) {
|
|
87
|
-
registered.delete(name);
|
|
308
|
+
if (registered.get(name) === load) registered.delete(name);
|
|
88
309
|
throw error;
|
|
89
310
|
}
|
|
90
311
|
}
|
|
@@ -122,17 +343,32 @@ const refreshes = new Map(); // name -> Promise<RefreshResult>
|
|
|
122
343
|
* built; rejects with an Error carrying `status` (403: not allowed here,
|
|
123
344
|
* 429: refreshed too recently — see `retryAfterMs`, 502: the query failed —
|
|
124
345
|
* the message says why). Concurrent calls for the same binding share one
|
|
125
|
-
* request.
|
|
346
|
+
* request. `opts.onProgress` hears every phase of it: the build, then the
|
|
347
|
+
* reload (link, download, DuckDB), then `null` when it is done.
|
|
126
348
|
*/
|
|
127
|
-
export function refreshBinding(name) {
|
|
349
|
+
export function refreshBinding(name, opts) {
|
|
350
|
+
const onProgress = opts && opts.onProgress;
|
|
351
|
+
const unwatch = onProgress ? watchProgress(name, onProgress) : null;
|
|
128
352
|
const pending = refreshes.get(name);
|
|
129
|
-
if (pending)
|
|
353
|
+
if (pending) {
|
|
354
|
+
if (unwatch) pending.finally(unwatch).catch(() => {});
|
|
355
|
+
return pending;
|
|
356
|
+
}
|
|
357
|
+
const what = 'Binding "' + name + '" could not be refreshed';
|
|
130
358
|
const run = (async () => {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
359
|
+
setProgress(name, { phase: "requesting", rowsWritten: null, bytes: null, bytesTotal: null });
|
|
360
|
+
const res = await fetch("__data/" + encodeURIComponent(name) + "/refresh", {
|
|
361
|
+
method: "POST",
|
|
362
|
+
headers: { accept: PROGRESS_ACCEPT },
|
|
363
|
+
});
|
|
364
|
+
let body;
|
|
365
|
+
if (isProgressResponse(res)) {
|
|
366
|
+
body = await readProgressStream(res, e => setProgress(name, progressPatch(e)));
|
|
367
|
+
if (body.phase === "failed") throw progressError(body, what);
|
|
368
|
+
} else {
|
|
369
|
+
if (!res.ok) throw await responseError(res, what);
|
|
370
|
+
body = await res.json();
|
|
134
371
|
}
|
|
135
|
-
const body = await res.json();
|
|
136
372
|
await reloadBinding(name);
|
|
137
373
|
setBindingState({ ...bindingState, version: bindingState.version + 1 });
|
|
138
374
|
return {
|
|
@@ -148,6 +384,8 @@ export function refreshBinding(name) {
|
|
|
148
384
|
run
|
|
149
385
|
.finally(() => {
|
|
150
386
|
refreshes.delete(name);
|
|
387
|
+
setProgress(name, null);
|
|
388
|
+
if (unwatch) unwatch();
|
|
151
389
|
const refreshing = new Set(bindingState.refreshing);
|
|
152
390
|
refreshing.delete(name);
|
|
153
391
|
setBindingState({ ...bindingState, refreshing });
|
|
@@ -238,49 +476,18 @@ function bindingIndex() {
|
|
|
238
476
|
return indexPromise;
|
|
239
477
|
}
|
|
240
478
|
|
|
241
|
-
|
|
242
|
-
// TIMESTAMP columns to JS as epoch milliseconds; v1 apps (JSON over the wire)
|
|
243
|
-
// always saw strings, and a migrated formatDate() that did
|
|
244
|
-
// new Date(`${d}T00:00:00`) on a number threw and unmounted the whole app
|
|
245
|
-
// (apps.md §15.4). So: DATE → "YYYY-MM-DD", TIMESTAMP → ISO 8601, BigInt →
|
|
246
|
-
// Number when it fits. Everything else passes through untouched.
|
|
247
|
-
function columnDecoder(field) {
|
|
248
|
-
const type = String(field.type);
|
|
249
|
-
if (type.startsWith("Date")) {
|
|
250
|
-
return v => (v == null ? v : new Date(Number(v)).toISOString().slice(0, 10));
|
|
251
|
-
}
|
|
252
|
-
if (type.startsWith("Timestamp")) {
|
|
253
|
-
return v => (v == null ? v : new Date(Number(v)).toISOString());
|
|
254
|
-
}
|
|
255
|
-
return v =>
|
|
256
|
-
typeof v === "bigint" && v >= Number.MIN_SAFE_INTEGER && v <= Number.MAX_SAFE_INTEGER
|
|
257
|
-
? Number(v)
|
|
258
|
-
: v;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
function toPlainRows(table, cap) {
|
|
262
|
-
const rows = [];
|
|
263
|
-
const fields = table.schema.fields.map(f => f.name);
|
|
264
|
-
const decoders = table.schema.fields.map(columnDecoder);
|
|
265
|
-
const limit = cap == null ? Infinity : cap;
|
|
266
|
-
for (const row of table) {
|
|
267
|
-
if (rows.length >= limit) break;
|
|
268
|
-
const out = {};
|
|
269
|
-
for (let i = 0; i < fields.length; i++) {
|
|
270
|
-
out[fields[i]] = decoders[i](row[fields[i]]);
|
|
271
|
-
}
|
|
272
|
-
rows.push(out);
|
|
273
|
-
}
|
|
274
|
-
return { rows, fields };
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
async function runSql(sql, rowLimit) {
|
|
479
|
+
async function runSql(sql, rowLimit, format) {
|
|
278
480
|
const cap = rowLimit === null ? Infinity : (rowLimit ?? DEFAULT_ROW_LIMIT);
|
|
279
481
|
const db = await getDb();
|
|
280
482
|
const conn = await db.connect();
|
|
281
483
|
try {
|
|
282
484
|
const table = await conn.query(sql);
|
|
283
485
|
const rowCount = table.numRows;
|
|
486
|
+
if (format === "arrow") {
|
|
487
|
+
// The Arrow Table as DuckDB returned it: no row objects, no
|
|
488
|
+
// normalization, no row limit.
|
|
489
|
+
return { rows: table, fields: table.schema.fields.map(f => f.name), rowCount, truncated: false };
|
|
490
|
+
}
|
|
284
491
|
const { rows, fields } = toPlainRows(table, cap);
|
|
285
492
|
return { rows, fields, rowCount, truncated: rowCount > rows.length };
|
|
286
493
|
} finally {
|
|
@@ -336,22 +543,33 @@ function useAsyncQuery(run, deps, isRefreshing) {
|
|
|
336
543
|
};
|
|
337
544
|
}
|
|
338
545
|
|
|
546
|
+
// A hook's view of progress: the binding's own record while it loads, else
|
|
547
|
+
// `running_sql` while the hook's SQL runs, else nothing.
|
|
548
|
+
function hookProgress(record, state) {
|
|
549
|
+
const p = record ?? (state.loading || state.refreshing ? { phase: "running_sql" } : null);
|
|
550
|
+
return {
|
|
551
|
+
phase: p ? p.phase : null,
|
|
552
|
+
rowsWritten: p && typeof p.rowsWritten === "number" ? p.rowsWritten : null,
|
|
553
|
+
bytes: p && typeof p.bytes === "number" ? p.bytes : null,
|
|
554
|
+
bytesTotal: p && typeof p.bytesTotal === "number" ? p.bytesTotal : null,
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
|
|
339
558
|
export function useQuery(name, opts) {
|
|
559
|
+
bootEngine();
|
|
340
560
|
const rowLimit = opts ? opts.rowLimit : undefined;
|
|
341
561
|
const state = useAsyncQuery(
|
|
342
562
|
async () => {
|
|
343
563
|
await registerBinding(name);
|
|
344
|
-
const r = await runSql(
|
|
345
|
-
'SELECT * FROM "' + name.replace(/"/g, '""') + '"',
|
|
346
|
-
rowLimit,
|
|
347
|
-
);
|
|
564
|
+
const r = await runSql("SELECT * FROM " + quoteIdent(name), rowLimit);
|
|
348
565
|
return { data: r.rows, fields: r.fields, truncated: r.truncated, rowCount: r.rowCount };
|
|
349
566
|
},
|
|
350
567
|
[name, rowLimit],
|
|
351
568
|
refreshing => refreshing.has(name),
|
|
352
569
|
);
|
|
570
|
+
const progress = useBindingProgress(name);
|
|
353
571
|
const refresh = React.useCallback(() => refreshBinding(name), [name]);
|
|
354
|
-
return { ...state, refresh };
|
|
572
|
+
return { ...state, ...hookProgress(progress, state), progress, refresh };
|
|
355
573
|
}
|
|
356
574
|
|
|
357
575
|
/**
|
|
@@ -385,24 +603,39 @@ export function useViewer() {
|
|
|
385
603
|
return state;
|
|
386
604
|
}
|
|
387
605
|
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
606
|
+
/**
|
|
607
|
+
* Run `sql` with the bindings it reads loaded — and only those. The ones
|
|
608
|
+
* its text names start downloading together, alongside the engine boot; a
|
|
609
|
+
* table the hint missed shows up as DuckDB's "Table with name X does not
|
|
610
|
+
* exist", which loads X and runs the query again. Joins across bindings
|
|
611
|
+
* work by name without declaring anything, as before, and an app with 34
|
|
612
|
+
* bindings no longer downloads all 34 to draw one chart.
|
|
613
|
+
*
|
|
614
|
+
* A binding that fails to load must not sink the query silently: when the
|
|
615
|
+
* SQL then fails, the load failure is what surfaces — it is the cause.
|
|
616
|
+
*/
|
|
617
|
+
async function runOverBindings(sql, rowLimit, format) {
|
|
618
|
+
const names = await bindingIndex();
|
|
619
|
+
const failures = new Map();
|
|
620
|
+
const attempted = new Set();
|
|
621
|
+
const load = n => {
|
|
622
|
+
attempted.add(n);
|
|
623
|
+
return registerBinding(n).catch(e => {
|
|
624
|
+
failures.set(n, e);
|
|
625
|
+
});
|
|
626
|
+
};
|
|
627
|
+
await Promise.all((readsCatalog(sql) ? names : mentionedBindings(sql, names)).map(load));
|
|
628
|
+
for (;;) {
|
|
401
629
|
try {
|
|
402
|
-
|
|
403
|
-
return { data: r.rows, fields: r.fields, truncated: r.truncated, rowCount: r.rowCount };
|
|
630
|
+
return await runSql(sql, rowLimit, format);
|
|
404
631
|
} catch (error) {
|
|
405
632
|
const message = error instanceof Error ? error.message : String(error);
|
|
633
|
+
const missing = missingBinding(message, names);
|
|
634
|
+
if (missing && !attempted.has(missing)) {
|
|
635
|
+
await load(missing);
|
|
636
|
+
if (failures.has(missing)) throw failures.get(missing);
|
|
637
|
+
continue;
|
|
638
|
+
}
|
|
406
639
|
for (const [n, cause] of failures) {
|
|
407
640
|
if (message.includes(n)) throw cause;
|
|
408
641
|
}
|
|
@@ -414,10 +647,31 @@ export function useDuckDB(sql, opts) {
|
|
|
414
647
|
}
|
|
415
648
|
throw error;
|
|
416
649
|
}
|
|
417
|
-
}
|
|
418
|
-
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
export function useDuckDB(sql, opts) {
|
|
654
|
+
bootEngine();
|
|
655
|
+
const rowLimit = opts ? opts.rowLimit : undefined;
|
|
656
|
+
const format = opts && opts.format === "arrow" ? "arrow" : "rows";
|
|
657
|
+
const state = useAsyncQuery(async () => {
|
|
658
|
+
const r = await runOverBindings(sql, rowLimit, format);
|
|
659
|
+
return { data: r.rows, fields: r.fields, truncated: r.truncated, rowCount: r.rowCount };
|
|
660
|
+
}, [sql, rowLimit, format], refreshing => refreshing.size > 0);
|
|
661
|
+
// Every binding the query can read: the combined record says what the
|
|
662
|
+
// slowest of them is waiting on; `bindings` has each one.
|
|
663
|
+
const bindings = useBindingProgress();
|
|
664
|
+
const combined = combineBindingProgress(bindings);
|
|
665
|
+
// A query over every binding refreshes every binding (the ones this page
|
|
666
|
+
// has not loaded yet are rebuilt and fetched fresh when first read).
|
|
419
667
|
const refresh = React.useCallback(() => refreshBindings(), []);
|
|
420
|
-
return {
|
|
668
|
+
return {
|
|
669
|
+
...state,
|
|
670
|
+
...hookProgress(combined, state),
|
|
671
|
+
progress: combined,
|
|
672
|
+
bindings,
|
|
673
|
+
refresh,
|
|
674
|
+
};
|
|
421
675
|
}
|
|
422
676
|
|
|
423
677
|
// ---------------------------------------------------------------------------
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@makoai/app-sdk",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.8.0",
|
|
4
4
|
"description": "Mako app SDK: data bindings (useQuery/useDuckDB), URL state, theme, and the house dashboard kit (/ui).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -41,6 +41,8 @@
|
|
|
41
41
|
"files": [
|
|
42
42
|
"index.js",
|
|
43
43
|
"index.d.ts",
|
|
44
|
+
"progress.js",
|
|
45
|
+
"data-core.js",
|
|
44
46
|
"vite.js",
|
|
45
47
|
"vite.d.ts",
|
|
46
48
|
"credentials.js",
|
|
@@ -60,5 +62,8 @@
|
|
|
60
62
|
},
|
|
61
63
|
"scripts": {
|
|
62
64
|
"test": "node --test"
|
|
65
|
+
},
|
|
66
|
+
"devDependencies": {
|
|
67
|
+
"apache-arrow": "17.0.0"
|
|
63
68
|
}
|
|
64
69
|
}
|