@yolean/evidence-sdk 0.0.0-stage → 4.1.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.
Files changed (186) hide show
  1. package/CHANGELOG.md +538 -0
  2. package/LICENSE.md +9 -0
  3. package/README.md +237 -2
  4. package/package.json +161 -3
  5. package/src/build-dev/cli/add/addServerHook.js +30 -0
  6. package/src/build-dev/cli/add/addTypeRef.js +19 -0
  7. package/src/build-dev/cli/add/updateGitIgnore.js +33 -0
  8. package/src/build-dev/cli/add/updatePackageJson.js +26 -0
  9. package/src/build-dev/cli/add/updateSvelteConfig.js +199 -0
  10. package/src/build-dev/cli/add/updateViteConfig.js +152 -0
  11. package/src/build-dev/cli/add.js +95 -0
  12. package/src/build-dev/cli/build.js +30 -0
  13. package/src/build-dev/cli/dev.js +49 -0
  14. package/src/build-dev/cli/index.js +14 -0
  15. package/src/build-dev/cli/upgrade.js +54 -0
  16. package/src/build-dev/svelte/index.js +54 -0
  17. package/src/build-dev/svelte/lib/isTargetFile.js +6 -0
  18. package/src/build-dev/svelte/metadatas.js +39 -0
  19. package/src/build-dev/svelte/processors/addBasePathToHrefAndSrc/addBasePathToHrefAndSrc.js +29 -0
  20. package/src/build-dev/svelte/processors/addBasePathToHrefAndSrc/addBasePathToHrefAndSrc.spec.js +82 -0
  21. package/src/build-dev/svelte/processors/addScriptTags.js +17 -0
  22. package/src/build-dev/svelte/processors/convertToHtml/convertToHtml.js +19 -0
  23. package/src/build-dev/svelte/processors/convertToHtml/convertToHtml.spec.js +123 -0
  24. package/src/build-dev/svelte/processors/convertToHtml/processMarkdown.js +59 -0
  25. package/src/build-dev/svelte/processors/extractQueries/extractQueries.js +49 -0
  26. package/src/build-dev/svelte/processors/extractQueries/extractQueries.spec.js +54 -0
  27. package/src/build-dev/svelte/processors/highlight/highlight.js +52 -0
  28. package/src/build-dev/svelte/processors/highlight/highlight.spec.js +31 -0
  29. package/src/build-dev/svelte/processors/initializeUniversalSql.js +15 -0
  30. package/src/build-dev/svelte/processors/injectComponents/injectComponents.js +56 -0
  31. package/src/build-dev/svelte/processors/injectHighlightStyles.js +14 -0
  32. package/src/build-dev/svelte/processors/injectQueries/injectQueries.js +77 -0
  33. package/src/build-dev/svelte/processors/markRootLayoutPrerenderable.js +56 -0
  34. package/src/build-dev/svelte/processors/obfuscateCode/obfuscateCode.js +17 -0
  35. package/src/build-dev/svelte/processors/obfuscateCode/obfuscateCode.spec.js +21 -0
  36. package/src/build-dev/svelte/processors/transformQueries/transformQueries.js +58 -0
  37. package/src/build-dev/vite/config-virtual.js +24 -0
  38. package/src/build-dev/vite/constants.js +7 -0
  39. package/src/build-dev/vite/datasource-plugins/source-query-hmr.js +117 -0
  40. package/src/build-dev/vite/evidence-config.d.ts +6 -0
  41. package/src/build-dev/vite/index.js +214 -0
  42. package/src/build-dev/vite/layout-plugins/copy-to-layout.js +71 -0
  43. package/src/build-dev/vite/plugins.js +4 -0
  44. package/src/build-dev/vite/query-directory-hmr.js +32 -0
  45. package/src/build-dev/vite/sidecar-app.js +79 -0
  46. package/src/build-dev/vite/virtuals/QuerySSR.svelte +13 -0
  47. package/src/build-dev/vite/virtuals/__tests/proxyStack.spec.js +52 -0
  48. package/src/build-dev/vite/virtuals/bootstrapContexts.svelte.js +27 -0
  49. package/src/build-dev/vite/virtuals/browser/static-assets.js +3 -0
  50. package/src/build-dev/vite/virtuals/contextKeys.js +3 -0
  51. package/src/build-dev/vite/virtuals/initUsql.js +23 -0
  52. package/src/build-dev/vite/virtuals/node/static-assets.js +56 -0
  53. package/src/build-dev/vite/virtuals/proxyStack.js +37 -0
  54. package/src/build-dev/vite/virtuals/queries.svelte.js +150 -0
  55. package/src/build-dev/vite/virtuals/ssrHook.svelte.js +60 -0
  56. package/src/build-dev/vite/virtuals/updateQueryContext.svelte.js +31 -0
  57. package/src/build-dev/vite/virtuals-dynamic.js +12 -0
  58. package/src/cli.js +5 -0
  59. package/src/cli.root.js +90 -0
  60. package/src/configuration/cli/create.js +20 -0
  61. package/src/configuration/cli/index.js +12 -0
  62. package/src/configuration/createEvidenceConfig.js +51 -0
  63. package/src/configuration/getEvidenceConfig.js +60 -0
  64. package/src/configuration/getEvidenceConfig.legacy.js +27 -0
  65. package/src/configuration/index.js +1 -0
  66. package/src/configuration/schemas/config.schema.js +11 -0
  67. package/src/configuration/schemas/deployment.schema.js +9 -0
  68. package/src/configuration/schemas/deployment.schema.spec.js +34 -0
  69. package/src/configuration/updateEvidenceConfig.js +25 -0
  70. package/src/legacy-compat.js +8 -0
  71. package/src/lib/EvidenceError.js +35 -0
  72. package/src/lib/b64Deep.js +55 -0
  73. package/src/lib/check-import.js +12 -0
  74. package/src/lib/cleanZodErrors.js +22 -0
  75. package/src/lib/cli/cmdFail.js +33 -0
  76. package/src/lib/cli/notices.js +44 -0
  77. package/src/lib/debug.js +40 -0
  78. package/src/lib/flows.d.ts +21 -0
  79. package/src/lib/forceLink.js +28 -0
  80. package/src/lib/history/index.js +70 -0
  81. package/src/lib/history/types.d.ts +8 -0
  82. package/src/lib/pkg-types.helpers.js +16 -0
  83. package/src/lib/processing-queue.js +96 -0
  84. package/src/lib/processing-queue.spec.js +148 -0
  85. package/src/lib/projectPaths.js +29 -0
  86. package/src/lib/projectRoot.js +12 -0
  87. package/src/lib/sharedPromise.js +77 -0
  88. package/src/lib/sharedPromise.spec.js +23 -0
  89. package/src/lib/strict.js +19 -0
  90. package/src/lib/tests/fs.js +21 -0
  91. package/src/lib/tokens.js +7 -0
  92. package/src/lib/unnest-zod-error.js +14 -0
  93. package/src/logger/EvidenceLogger.js +164 -0
  94. package/src/logger/index.js +4 -0
  95. package/src/meta.js +8 -0
  96. package/src/plugins/cli/index.js +13 -0
  97. package/src/plugins/cli/install.js +26 -0
  98. package/src/plugins/cli/install.prompt.js +121 -0
  99. package/src/plugins/components/getAllComponents.js +82 -0
  100. package/src/plugins/components/getComponentsInPlugin.js +33 -0
  101. package/src/plugins/components/loadComponentPlugins.js +48 -0
  102. package/src/plugins/components/loaders/file-loader.js +125 -0
  103. package/src/plugins/components/loaders/manifest-loader.js +53 -0
  104. package/src/plugins/components/schemas/component-manifest.schema.js +5 -0
  105. package/src/plugins/components/types.d.ts +21 -0
  106. package/src/plugins/components/validateOverrides.js +30 -0
  107. package/src/plugins/components/validateOverrides.spec.js +32 -0
  108. package/src/plugins/datasources/Datasources.js +85 -0
  109. package/src/plugins/datasources/Datasources.spec.js +78 -0
  110. package/src/plugins/datasources/SourceResultCache.js +62 -0
  111. package/src/plugins/datasources/buildSourceDirectoryProxy.js +21 -0
  112. package/src/plugins/datasources/cli/edit/Options.fixtures.js +69 -0
  113. package/src/plugins/datasources/cli/edit/Options.js +253 -0
  114. package/src/plugins/datasources/cli/edit/Options.spec.js +496 -0
  115. package/src/plugins/datasources/cli/edit/editSourceConfig.js +201 -0
  116. package/src/plugins/datasources/cli/edit.js +20 -0
  117. package/src/plugins/datasources/cli/edit.prompt.js +100 -0
  118. package/src/plugins/datasources/cli/index.js +18 -0
  119. package/src/plugins/datasources/cli/sources.js +77 -0
  120. package/src/plugins/datasources/evalSources.js +257 -0
  121. package/src/plugins/datasources/getPublishedDatasources.js +62 -0
  122. package/src/plugins/datasources/index.js +6 -0
  123. package/src/plugins/datasources/loadSourceConfig.js +147 -0
  124. package/src/plugins/datasources/loadSourceConfig.spec.js +127 -0
  125. package/src/plugins/datasources/loadSourcePlugins.js +49 -0
  126. package/src/plugins/datasources/loadSources.js +48 -0
  127. package/src/plugins/datasources/schemas/datasource.schema.js +52 -0
  128. package/src/plugins/datasources/schemas/datasourceCache.schema.js +3 -0
  129. package/src/plugins/datasources/schemas/datasourcePlugin.schema.js +37 -0
  130. package/src/plugins/datasources/schemas/datasourcePluginOptions.schema.js +64 -0
  131. package/src/plugins/datasources/schemas/index.js +2 -0
  132. package/src/plugins/datasources/schemas/manifest.schema.js +8 -0
  133. package/src/plugins/datasources/schemas/results.schema.js +126 -0
  134. package/src/plugins/datasources/sub-source-vars.js +55 -0
  135. package/src/plugins/datasources/sub-source-vars.spec.js +46 -0
  136. package/src/plugins/datasources/types.d.ts +53 -0
  137. package/src/plugins/datasources/updateManifest.js +115 -0
  138. package/src/plugins/datasources/updateManifest.spec.js +200 -0
  139. package/src/plugins/datasources/wrapSimpleConnector.js +95 -0
  140. package/src/plugins/datasources/writeSourceConfig.js +60 -0
  141. package/src/plugins/index.js +2 -0
  142. package/src/plugins/layouts/copyMethods/index.js +10 -0
  143. package/src/plugins/layouts/copyMethods/svelte.js +53 -0
  144. package/src/plugins/layouts/copyMethods/sveltekit.js +75 -0
  145. package/src/plugins/layouts/copyToLayout.js +106 -0
  146. package/src/plugins/layouts/loadLayoutPlugin.js +23 -0
  147. package/src/plugins/loadPluginPackage.js +77 -0
  148. package/src/plugins/schemas/index.js +1 -0
  149. package/src/plugins/schemas/plugin-config.schema.js +41 -0
  150. package/src/plugins/schemas/plugin-config.schema.spec.js +21 -0
  151. package/src/plugins/schemas/plugin-package.schema.js +154 -0
  152. package/src/plugins/schemas/plugin-package.schema.spec.js +30 -0
  153. package/src/query-builder.d.ts +1 -0
  154. package/src/query-builder.js +1 -0
  155. package/src/types/duckdb-wellknown.d.ts +35 -0
  156. package/src/types/html-attributes.ts +13 -0
  157. package/src/types/index.d.ts +18 -0
  158. package/src/types/mosaic-sql.d.ts +27 -0
  159. package/src/types/sveltekit-autoimport.d.ts +13 -0
  160. package/src/types/virtuals.d.ts +113 -0
  161. package/src/types.d.ts +1 -0
  162. package/src/usql/index.d.ts +4 -0
  163. package/src/usql/index.js +3 -0
  164. package/src/usql/query/Query.js +1391 -0
  165. package/src/usql/query/Query.spec.js +827 -0
  166. package/src/usql/query/queryScore.js +92 -0
  167. package/src/usql/query/queryScore.spec.js +26 -0
  168. package/src/usql/query/sterilizeQuery.js +89 -0
  169. package/src/usql/query/sterilizeQuery.spec.js +172 -0
  170. package/src/usql/setTrackProxy/setTrackProxy.js +80 -0
  171. package/src/usql/setTrackProxy/setTrackProxy.spec.js +166 -0
  172. package/src/usql/types.ts +86 -0
  173. package/src/usql/utilities/duckdbSerialize.js +29 -0
  174. package/src/usql/utilities/index.js +2 -0
  175. package/src/usql/utilities/resolveMaybePromise.js +30 -0
  176. package/src/utils/batchUp.js +22 -0
  177. package/src/utils/batchUp.spec.js +69 -0
  178. package/src/utils/browserDebounce.js +16 -0
  179. package/src/utils/index.js +7 -0
  180. package/src/utils/svelte/addBasePath.js +45 -0
  181. package/src/utils/svelte/addBasePath.spec.js +86 -0
  182. package/src/utils/svelte/index.js +5 -0
  183. package/src/utils/svelte/inputs.js +138 -0
  184. package/tsconfig.json +22 -0
  185. package/vite.config.js +3 -0
  186. package/vitest.config.js +15 -0
@@ -0,0 +1,1391 @@
1
+ import { nanoid } from 'nanoid';
2
+ import { isDebug } from '../../lib/debug.js';
3
+ import {
4
+ Query as QueryBuilder,
5
+ sql as taggedSql,
6
+ sum as qSum,
7
+ avg as qAvg,
8
+ min as qMin,
9
+ max as qMax,
10
+ median as qMedian,
11
+ count as qCount
12
+ } from '@uwdata/mosaic-sql';
13
+ import { sharedPromise } from '../../lib/sharedPromise.js';
14
+ import { resolveMaybePromise } from '../utilities/resolveMaybePromise.js';
15
+ import { getQueryScore } from './queryScore.js';
16
+ import { VITE_EVENTS } from '../../build-dev/vite/constants.js';
17
+ import { sterilizeQuery } from './sterilizeQuery.js';
18
+
19
+ /**
20
+ * @typedef {import("../types.js").QueryResultRow} QueryResultRow
21
+ */
22
+
23
+ /**
24
+ * @template T
25
+ * @typedef {import('../types.js').MaybePromise<T>} MaybePromise
26
+ */
27
+
28
+ /**
29
+ * @template {QueryResultRow} [RowType=QueryResultRow]
30
+ * @typedef {import('../../lib/sharedPromise.js').SharedPromise<Query<RowType>>} ChainableSharedPromise
31
+ */
32
+
33
+ /**
34
+ * @template {QueryResultRow[]} RowType
35
+ * @typedef {import("svelte/store").Readable<RowType>} Readable
36
+ */
37
+
38
+ /**
39
+ * @template {QueryResultRow} [RowType=QueryResultRow]
40
+ * @typedef {RowType[] & Query<RowType>} QueryValue
41
+ */
42
+
43
+ /**
44
+ * @typedef {Object} QueryEvents<RowType>
45
+ * @property {undefined} dataReady
46
+ * @property {number} highScore
47
+ * @property {number} longRun
48
+ * @property {Error} error
49
+ */
50
+
51
+ /**
52
+ * @typedef {Object} QueryGlobalEvents
53
+ * @property {undefined} inFlightQueryStart
54
+ * @property {undefined} inFlightQueryEnd
55
+ * @property {import('../types.js').QueryDebugPayload} queryCreated
56
+ * @property {undefined} cacheCleared
57
+ */
58
+ /** @typedef {import ("../types.js").EventEmitter<QueryGlobalEvents>} QueryGlobalEventEmitter */
59
+
60
+ /**
61
+ * @typedef {import('../types.js').EventEmitter<QueryEvents>} QueryEventEmitter
62
+ */
63
+
64
+ /**
65
+ * @class
66
+ * @template {QueryResultRow} [RowType=QueryResultRow]
67
+ * @implements {Query<RowType>}
68
+ * @implements {Readable<QueryValue<RowType>>}
69
+ * @implements {QueryEventEmitter}
70
+ */
71
+ export class Query {
72
+ ////////////////////////////
73
+ /// < State Primatives > ///
74
+ ////////////////////////////
75
+
76
+ /** @type {QueryValue<RowType>} */
77
+ #value;
78
+
79
+ get value() {
80
+ return this.#value;
81
+ }
82
+
83
+ /// Data
84
+ /** @type {RowType[]} */
85
+ #data = [];
86
+ /** @type {number} */
87
+ #dataQueryTime = -1;
88
+ get dataLoaded() {
89
+ return ['resolved', 'rejected'].includes(this.#sharedDataPromise.state);
90
+ }
91
+ get dataLoading() {
92
+ return this.#sharedDataPromise.state === 'loading';
93
+ }
94
+ get dataQueryTime() {
95
+ return this.#dataQueryTime;
96
+ }
97
+ /// Length
98
+ /** @type {number} */
99
+ #length = 0;
100
+ /** @type {number} */
101
+ #lengthQueryTime = -1;
102
+ get length() {
103
+ return this.#length;
104
+ }
105
+ get lengthLoaded() {
106
+ return ['resolved', 'rejected'].includes(this.#sharedLengthPromise.state);
107
+ }
108
+ get lengthLoading() {
109
+ return this.#sharedLengthPromise.state === 'loading';
110
+ }
111
+ get lengthQueryTime() {
112
+ return this.#lengthQueryTime;
113
+ }
114
+
115
+ /// Columns
116
+ /** @type {import('../../types/duckdb-wellknown.js').DescribeResultRow[]} */
117
+ #columns = [];
118
+ /** @type {Record<keyof RowType, undefined> | undefined} */
119
+ #mockRow = undefined;
120
+ /** @type {number} */
121
+ #columnsQueryTime = -1;
122
+
123
+ get columns() {
124
+ return this.#columns;
125
+ }
126
+ get columnsLoaded() {
127
+ return ['resolved', 'rejected'].includes(this.#sharedColumnsPromise.state);
128
+ }
129
+ get columnsLoading() {
130
+ return this.#sharedColumnsPromise.state === 'loading';
131
+ }
132
+ get columnsQueryTime() {
133
+ return this.#columnsQueryTime;
134
+ }
135
+
136
+ /**
137
+ * True when data, length, and columns have all been fetched
138
+ */
139
+ get ready() {
140
+ return (
141
+ this.#sharedLengthPromise.state === 'resolved' &&
142
+ this.#sharedColumnsPromise.state === 'resolved' &&
143
+ this.#sharedDataPromise.state === 'resolved'
144
+ );
145
+ }
146
+ /**
147
+ * True when data, length, or columns are currently being fetched
148
+ */
149
+ get loading() {
150
+ return (
151
+ this.#sharedLengthPromise.state === 'loading' ||
152
+ this.#sharedColumnsPromise.state === 'loading' ||
153
+ this.#sharedDataPromise.state === 'loading'
154
+ );
155
+ }
156
+
157
+ /**
158
+ * Use the getter/setter for #error instead of this value directly
159
+ * @type {Error | undefined}
160
+ */
161
+ #__error;
162
+
163
+ get #error() {
164
+ return this.#__error;
165
+ }
166
+ /**
167
+ * @param {Error | undefined} v
168
+ */
169
+ set #error(v) {
170
+ if (!v) return;
171
+ console.error(`${this.id} | Error in Query!`, v?.message);
172
+ this.#emit('error', v);
173
+ this.#__error = v;
174
+ }
175
+ get error() {
176
+ return this.#error;
177
+ }
178
+
179
+ /** @type {QueryBuilder} */
180
+ #query;
181
+ /** @type {string} */
182
+ #originalText;
183
+ /**
184
+ * The Query text as is was provided
185
+ */
186
+ get originalText() {
187
+ return this.#originalText;
188
+ }
189
+ /**
190
+ * The Query text as it is being executed
191
+ */
192
+ get text() {
193
+ return this.#query?.toString() ?? "SELECT 'Empty Query' WHERE 0";
194
+ }
195
+
196
+ //////////////////////////////
197
+ /// </ State Primatives /> ///
198
+ //////////////////////////////
199
+
200
+ //////////////////////////
201
+ /// < Global Loading > ///
202
+ //////////////////////////
203
+
204
+ /** @type {Set<Query>} */
205
+ static #inFlightQueries = new Set();
206
+
207
+ static get queriesInFlight() {
208
+ return Query.#inFlightQueries.size > 0;
209
+ }
210
+
211
+ /**
212
+ * @protected
213
+ */
214
+ static resetInFlightQueries() {
215
+ Query.#inFlightQueries = new Set();
216
+ }
217
+
218
+ /**
219
+ * @param {Query<any>} q
220
+ */
221
+ static #markInFlight = (q) => {
222
+ if (this.#inFlightQueries.size === 0) {
223
+ // We are starting
224
+ this.#globalEmit('inFlightQueryStart', undefined);
225
+ }
226
+ Query.#inFlightQueries.add(q);
227
+ q.#sharedDataPromise.promise.finally(() => {
228
+ Query.#inFlightQueries.delete(q);
229
+ if (this.#inFlightQueries.size === 0) {
230
+ // We are done
231
+ this.#globalEmit('inFlightQueryEnd', undefined);
232
+ }
233
+ // Remove
234
+ });
235
+ };
236
+
237
+ /** @type {import("../types.js").EventMap<QueryGlobalEvents>} */
238
+ static #globalHandlerMap = {
239
+ inFlightQueryStart: new Set(),
240
+ inFlightQueryEnd: new Set(),
241
+ queryCreated: new Set(),
242
+ cacheCleared: new Set()
243
+ };
244
+ /**
245
+ * @template {keyof QueryGlobalEvents} Event
246
+ * @param {Event} event
247
+ * @param {QueryGlobalEvents[Event]} value
248
+ */
249
+ static #globalEmit = (event, value) => {
250
+ Query.#globalHandlerMap[event].forEach((fn) => fn(value, event));
251
+ };
252
+
253
+ /** @type {QueryGlobalEventEmitter["addEventListener"]} */
254
+ static addEventListener(event, handler) {
255
+ this.#globalHandlerMap[event].add(handler);
256
+ }
257
+ /** @type {QueryGlobalEventEmitter["removeEventListener"]} */
258
+ static removeEventListener(event, handler) {
259
+ this.#globalHandlerMap[event].delete(handler);
260
+ }
261
+ /////////////////////////////
262
+ /// </ Global Loading /> ///
263
+ /////////////////////////////
264
+
265
+ ////////////////////
266
+ /// < Fetching > ///
267
+ ////////////////////
268
+
269
+ static #scoreThreshold = 10 * 1024 * 1024;
270
+ /** @type { number } */
271
+ #score = -1;
272
+ get score() {
273
+ return this.#score;
274
+ }
275
+
276
+ #calculateScore = () => {
277
+ if (this.lengthLoaded && this.columnsLoaded) {
278
+ this.#score = getQueryScore(this.length, this.columns);
279
+ if (this.#score > Query.#scoreThreshold) {
280
+ this.#emit('highScore', this.#score);
281
+ }
282
+ } else {
283
+ Promise.allSettled([this.#sharedLengthPromise.promise, this.#sharedColumnsPromise.promise])
284
+ .then(([$lengthRaw, $columnsRaw]) => {
285
+ if ($lengthRaw.status === 'rejected' || $columnsRaw.status === 'rejected') {
286
+ // TODO: Throw here?
287
+ this.#score = -1;
288
+ return;
289
+ }
290
+
291
+ if (!this.#length || !this.#columns) {
292
+ // TODO: Throw here?
293
+ this.#score = -1;
294
+ return;
295
+ }
296
+ this.#score = getQueryScore(this.length, this.columns);
297
+ if (this.#score > Query.#scoreThreshold) {
298
+ this.#emit('highScore', this.#score);
299
+ }
300
+ })
301
+ .catch((e) => {
302
+ console.error(`${this.id} | Failed to calculate Query score ${e}`);
303
+ });
304
+ }
305
+ };
306
+
307
+ /** @type {ChainableSharedPromise<RowType>} */
308
+ #sharedDataPromise = sharedPromise(() =>
309
+ this.publish(`data promise (${this.#sharedDataPromise.state})`)
310
+ );
311
+ /** @returns {MaybePromise<Query<RowType>>} */
312
+ #fetchData = () => {
313
+ if (this.#sharedDataPromise.state !== 'init') {
314
+ return this.#sharedDataPromise.promise;
315
+ }
316
+ if (this.#error) {
317
+ this.#debug('data error', 'Refusing to execute data query, store has an error state');
318
+ return this.#sharedDataPromise.promise;
319
+ }
320
+ if (this.#sharedDataPromise.state !== 'init' || this.opts.noResolve)
321
+ return this.#sharedDataPromise.promise;
322
+ this.#sharedDataPromise.start();
323
+
324
+ const dataQuery =
325
+ `
326
+ ---- Data ${this.#id} ${this.#hash}
327
+ ${this.text.trim()}
328
+ `.trim() + '\n';
329
+
330
+ this.#debugStyled('data query text', '\n' + dataQuery, 'font-family: monospace;');
331
+
332
+ // gotta love jsdoc sometimes
333
+ const typedRunner = /** @type {import('../types.js').Runner<RowType>} */ (this.#executeQuery);
334
+ Query.#markInFlight(this);
335
+ const before = performance.now();
336
+ const resolved = resolveMaybePromise(
337
+ (result, isPromise) => {
338
+ this.#data = result;
339
+ const after = performance.now();
340
+
341
+ if (before - after > 5000) {
342
+ this.#emit('longRun', before - after);
343
+ this.#debug('long-running', `Query took ${before - after}ms to execute`);
344
+ }
345
+
346
+ this.#dataQueryTime = after - before;
347
+
348
+ this.#fetchLength();
349
+ this.#sharedDataPromise.resolve(this);
350
+ this.#emit('dataReady', undefined);
351
+ if (isPromise) {
352
+ return this.#sharedDataPromise.promise;
353
+ } else {
354
+ return this;
355
+ }
356
+ },
357
+ () => typedRunner(dataQuery, `${this.#id}_data`),
358
+ (e, isPromise) => {
359
+ this.#error = e;
360
+ this.#sharedDataPromise.reject(e);
361
+ if (isPromise) {
362
+ return this.#sharedDataPromise.promise;
363
+ } else {
364
+ return this;
365
+ }
366
+ }
367
+ );
368
+ return resolved;
369
+ };
370
+ fetch = () => {
371
+ const cols = this.#fetchColumns();
372
+ if (
373
+ cols instanceof Promise &&
374
+ /* noResolve will always return a promise, even when fetch is synchronous */
375
+ !this.opts.noResolve
376
+ ) {
377
+ return Promise.allSettled([this.#fetchColumns(), this.#fetchData()]).then(() => this.value);
378
+ }
379
+ this.#fetchData();
380
+ return this.value;
381
+ };
382
+ /**
383
+ * Executes the query without actually updating the state
384
+ * This is helpful for ensuring that the related parquet files
385
+ * are available, even when SSR is used to initially hydrate the
386
+ * query / page.
387
+ *
388
+ * Does not run on the server, only in browser
389
+ */
390
+ backgroundFetch = () => {
391
+ if (typeof window === 'undefined') {
392
+ this.#debug('background fetch skip', 'Did not execute backgroundFetch in SSR');
393
+ return;
394
+ }
395
+ this.#debug('background fetch', `Executed backgroundFetch`);
396
+ resolveMaybePromise(
397
+ () => {},
398
+ async () => {
399
+ await new Promise((resolve) => setTimeout(resolve, 0));
400
+ return this.#executeQuery(`--data\n${this.text.trim()}`, this.id);
401
+ },
402
+ () => {}
403
+ );
404
+ };
405
+
406
+ /** @type {ChainableSharedPromise<RowType>} */
407
+ #sharedLengthPromise = sharedPromise(() =>
408
+ this.publish(`length promise (${this.#sharedLengthPromise.state})`)
409
+ );
410
+ /** @returns {MaybePromise<Query<RowType>>} */
411
+ #fetchLength = () => {
412
+ // If data has already been fetched, or provided
413
+ // Don't query for the length again
414
+ if (
415
+ this.#data &&
416
+ this.#sharedDataPromise.state === 'resolved' &&
417
+ this.#sharedLengthPromise.state === 'init'
418
+ ) {
419
+ this.#debug(
420
+ 'length inferred',
421
+ 'Inferred length from already-resolved data promise',
422
+ this.#data
423
+ );
424
+ this.#length = this.#data.length;
425
+ // Done
426
+ this.#sharedLengthPromise.resolve(this);
427
+ return this.#sharedLengthPromise.promise;
428
+ }
429
+ if (this.#error) {
430
+ this.#debug(
431
+ 'length error',
432
+ 'Refusing to execute length query, store has an error state',
433
+ this.#error
434
+ );
435
+ this.#sharedLengthPromise.reject(this.#error); // Is this the right call?
436
+ return this.#sharedLengthPromise.value ?? this.#sharedLengthPromise.promise;
437
+ }
438
+ if (this.#sharedLengthPromise.state !== 'init' || this.opts.noResolve)
439
+ return this.#sharedLengthPromise.promise;
440
+
441
+ this.#sharedLengthPromise.start();
442
+
443
+ const lengthQuery =
444
+ `
445
+ ---- Length ${this.#id} (${this.#hash})
446
+ SELECT COUNT(*) as rowCount FROM (${this.text.trim()})
447
+ `.trim() + '\n';
448
+
449
+ // gotta love jsdoc sometimes
450
+ const typedRunner =
451
+ /** @type {import('../types.js').Runner<{rowCount: number}>} */
452
+ (this.#executeQuery);
453
+
454
+ this.#debugStyled('length query text', '\n' + lengthQuery, 'font-family: monospace;');
455
+ const before = performance.now();
456
+ const resolved = resolveMaybePromise(
457
+ /** @returns {MaybePromise<Query<RowType>>} */
458
+ (lengthResult, isPromise) => {
459
+ const after = performance.now();
460
+ this.#lengthQueryTime = after - before;
461
+ this.#length = lengthResult[0].rowCount;
462
+ this.#sharedLengthPromise.resolve(this);
463
+ if (isPromise) {
464
+ return this.#sharedLengthPromise.promise;
465
+ } else {
466
+ return this;
467
+ }
468
+ },
469
+ () => typedRunner(lengthQuery, `${this.#id}_length`),
470
+ /** @returns {MaybePromise<Query<RowType>>} */
471
+ (e, isPromise) => {
472
+ this.#error = e;
473
+ this.#sharedLengthPromise.reject(e);
474
+ if (isPromise) {
475
+ return this.#sharedLengthPromise.promise;
476
+ } else {
477
+ return this;
478
+ }
479
+ }
480
+ );
481
+ return /** @type {MaybePromise<Query<RowType>>} */ (resolved);
482
+ };
483
+
484
+ /** @type {ChainableSharedPromise<RowType>} */
485
+ #sharedColumnsPromise = sharedPromise(() =>
486
+ this.publish(`columns promise (${this.#sharedColumnsPromise.state})`)
487
+ );
488
+ /** @returns {MaybePromise<Query<RowType>>} */
489
+ #fetchColumns = () => {
490
+ if (this.#error) {
491
+ this.#debug(
492
+ 'cols query error',
493
+ 'Refusing to execute columns query, store has an error state',
494
+ this.#error
495
+ );
496
+ // Return the value or the promise if not resolved
497
+ return this.#sharedColumnsPromise.value ?? this.#sharedColumnsPromise.promise;
498
+ }
499
+
500
+ // Store is in some started state
501
+ if (this.#sharedColumnsPromise.state !== 'init' || this.opts.noResolve)
502
+ return this.#sharedColumnsPromise.promise;
503
+ // Indicate that work has started on this promise
504
+ this.#sharedColumnsPromise.start();
505
+
506
+ const metaQuery =
507
+ `
508
+ ---- Columns ${this.#id} (${this.#hash})
509
+ DESCRIBE ${this.text.trim()}
510
+ `.trim() + '\n';
511
+
512
+ this.#debugStyled('columns query text', '\n' + metaQuery, 'font-family: monospace;');
513
+
514
+ // gotta love jsdoc sometimes
515
+ const typedRunner =
516
+ /** @type {import('../types.js').Runner<import('../../types/duckdb-wellknown.js').DescribeResultRow>} */
517
+ (this.#executeQuery);
518
+ const before = performance.now();
519
+ const resolved = resolveMaybePromise(
520
+ (description, isPromise) => {
521
+ const after = performance.now();
522
+ this.#columnsQueryTime = after - before;
523
+ // Update inner value
524
+ this.#columns = description;
525
+ // Resolve store
526
+ this.#sharedColumnsPromise.resolve(this);
527
+
528
+ this.#mockRow = /** @type {Record<keyof RowType, undefined>} */ (
529
+ Object.fromEntries(description.map((d) => [d.column_name, undefined]))
530
+ );
531
+
532
+ if (isPromise) {
533
+ return this.#sharedColumnsPromise.promise;
534
+ } else {
535
+ return this;
536
+ }
537
+ },
538
+ () => typedRunner(metaQuery, `${this.#id}_columns`),
539
+ /** @returns {MaybePromise<Query<RowType>>} */
540
+ (e, isPromise) => {
541
+ this.#error = e;
542
+ this.#sharedColumnsPromise.reject(e);
543
+
544
+ if (isPromise) {
545
+ return this.#sharedColumnsPromise.promise;
546
+ } else {
547
+ return this;
548
+ }
549
+ }
550
+ );
551
+ return /** @type {MaybePromise<Query<RowType>>} */ (resolved);
552
+ };
553
+ //////////////////////
554
+ /// </ Fetching /> ///
555
+ //////////////////////
556
+
557
+ //////////////////////////
558
+ /// < Type Narrowing > ///
559
+ //////////////////////////
560
+ /**
561
+ * @ignore
562
+ * @private
563
+ */
564
+ get isQuery() {
565
+ return true;
566
+ }
567
+
568
+ /**
569
+ * @template {QueryResultRow} RowType
570
+ * @param {unknown} q
571
+ * @returns {q is Query<RowType>}
572
+ */
573
+ static isQuery = (q) => {
574
+ // TODO: Should we type-narrow on row type as well
575
+ // Type narrow
576
+ if (typeof q !== 'object' || !q) return false;
577
+
578
+ const hasDuckType = 'isQuery' in q && q['isQuery'] === true;
579
+
580
+ return hasDuckType;
581
+ };
582
+ ////////////////////////////
583
+ /// </ Type Narrowing /> ///
584
+ ////////////////////////////
585
+
586
+ /** @param {unknown} v */
587
+ static [Symbol.hasInstance](v) {
588
+ return Query.isQuery(v);
589
+ }
590
+
591
+ /////////////////
592
+ /// < Proxy > ///
593
+ /////////////////
594
+ /** @type {string[]} */
595
+ static get ProxyFetchTriggers() {
596
+ return ['at'];
597
+ }
598
+ /** @returns {QueryValue<RowType>} */
599
+ #buildProxy = () => {
600
+ /** @type {QueryValue<RowType>} */
601
+ const proxy = /** @type {QueryValue<RowType>} */ (
602
+ new Proxy(/** @type {RowType[]} */ ([]), {
603
+ getPrototypeOf: () => {
604
+ return Object.getPrototypeOf(this.#data);
605
+ },
606
+ has: (self, prop) => {
607
+ return prop in this.#data || prop in this;
608
+ },
609
+ get: (_self, rawProp) => {
610
+ /** @type {string | symbol | number} */
611
+ let prop = rawProp;
612
+
613
+ if (typeof prop === 'string' && /^[\d.]+$/.exec(prop)) prop = parseInt(prop);
614
+ if (typeof prop === 'number' || Query.ProxyFetchTriggers.includes(prop.toString())) {
615
+ if (this.#sharedDataPromise.state === 'init') {
616
+ this.#debug('implicit fetch', `Implicit query fetch triggered by ${prop.toString()}`);
617
+ this.#fetchData(); // catches itself
618
+ }
619
+ }
620
+
621
+ if (prop === 'length') {
622
+ this.#fetchLength();
623
+ }
624
+ if (prop === 'constructor') return this.#data.constructor;
625
+ if (prop === 'toString') return this.#data.toString.bind(this.#data);
626
+
627
+ // Default field resolution
628
+ const target =
629
+ prop in this
630
+ ? this // Prop exists on Query
631
+ : this.#data && prop in this.#data
632
+ ? this.#data // Prop exists on Array
633
+ : null; // Prop exists on neither
634
+ if (target === null)
635
+ if (typeof prop !== 'number') return undefined;
636
+ else {
637
+ if (this.lengthLoaded && prop > this.#length) return undefined;
638
+ return this.#mockRow ?? {};
639
+ }
640
+
641
+ const field = target[/** @type {keyof typeof target} */ (prop)];
642
+
643
+ if (typeof field === 'function') return field.bind(target);
644
+ else return field;
645
+ }
646
+ })
647
+ );
648
+
649
+ return proxy;
650
+ };
651
+ ///////////////////
652
+ /// </ Proxy /> ///
653
+ ///////////////////
654
+
655
+ /////////////////////
656
+ /// < Factories > ///
657
+ /////////////////////
658
+ /**
659
+ * This is a fairly arbitrary number that determines how much data
660
+ * the Query will cache internally. The larger the number, the
661
+ * larger the cache will be.
662
+ *
663
+ * The number is based on our Query Score calculation, see
664
+ * queryScore.js for details on how this is calculated.
665
+ *
666
+ * @default 5 * 10 * 1024
667
+ *
668
+ */
669
+ static CacheMaxScore = 5 * 10 * 1024;
670
+ /**
671
+ * @type {Map<string, {added: number, query: Query<any>}>}
672
+ */
673
+ static #cache = new Map();
674
+
675
+ static emptyCache = () => {
676
+ this.#cache.clear();
677
+ this.#globalEmit('cacheCleared', undefined);
678
+ };
679
+
680
+ static get cacheSize() {
681
+ return this.#cache.size;
682
+ }
683
+
684
+ /**
685
+ * @param {Query<any>} q
686
+ */
687
+ static #addToCache = (q) => {
688
+ this.#cache.set(q.hash, {
689
+ query: q,
690
+ added: Date.now()
691
+ });
692
+
693
+ Query.#debugStatic('cache', `Added to cache: ${q.hash}`, {
694
+ cacheSize: this.#cache.size,
695
+ cacheScore: Array.from(this.#cache.values()).reduce((sum, q) => sum + q.query.score, 0)
696
+ });
697
+ };
698
+
699
+ /**
700
+ * @template {QueryResultRow} [RowType=QueryResultRow]
701
+ * @param {string} hash
702
+ * @returns {Query<RowType> | null}
703
+ */
704
+ static #getFromCache = (hash) => {
705
+ const cachedValue = this.#cache.get(hash);
706
+ if (cachedValue) {
707
+ return cachedValue.query;
708
+ }
709
+ return null;
710
+ };
711
+
712
+ static #cacheCleanup = () => {
713
+ let sumScore = Array.from(this.#cache.values()).reduce((sum, q) => sum + q.query.score, 0);
714
+ const sorted = Array.from(this.#cache.values()).sort((a, b) => a.added - b.added);
715
+ while (sumScore > this.CacheMaxScore) {
716
+ const oldest = sorted.shift();
717
+ if (!oldest) break;
718
+ this.#cache.delete(oldest.query.hash);
719
+ sumScore -= oldest.query.score;
720
+ }
721
+ };
722
+
723
+ /**
724
+ * @param {import('../types.js').QueryReactivityOpts<any>} reactiveOpts Callback that is executed when the new query is ready
725
+ * @param {import('../types.js').QueryOpts<any>} [opts]
726
+ * @param {QueryValue<any>} [initialQuery]
727
+ */
728
+ static createReactive = (reactiveOpts, opts, initialQuery) => {
729
+ const { loadGracePeriod = 250, callback = () => {}, execFn } = reactiveOpts;
730
+
731
+ /** @type {import('../types.js').CreateQuery<any>} */
732
+ const createFn = Query.create;
733
+ /** @type {QueryValue<any> | undefined} */
734
+ let activeQuery = initialQuery;
735
+
736
+ let changeIdx = 0;
737
+ /** @type {() => unknown} */
738
+ let unsub;
739
+ const waitFor =
740
+ /**
741
+ * @param {string | Query} nextQuery
742
+ * @param {import('../types.js').QueryOpts<any>} [newOpts]
743
+ * @returns {Promise<void> | void}
744
+ */
745
+ (nextQuery, newOpts) => {
746
+ if (!activeQuery) throw new Error();
747
+ changeIdx += 1;
748
+ const targetChangeIdx = changeIdx;
749
+ Query.#debugStatic(
750
+ `${activeQuery.id} (${hashQuery(nextQuery)}) | Reactive Updating`,
751
+ nextQuery,
752
+ {
753
+ changeIdx,
754
+ targetChangeIdx,
755
+ hash: hashQuery(nextQuery)
756
+ },
757
+ {
758
+ initialOpts: opts,
759
+ newOpts: newOpts
760
+ }
761
+ );
762
+ const newQuery = Query.isQuery(nextQuery)
763
+ ? nextQuery
764
+ : createFn(
765
+ nextQuery,
766
+ execFn,
767
+ // clear initialData and initialError on opts, but allow for newOpts to provide them
768
+ Object.assign({}, opts, { initialData: undefined, initialError: undefined }, newOpts)
769
+ );
770
+
771
+ const fetched = newQuery.fetch();
772
+ let dataMaybePromise = fetched;
773
+ if (fetched instanceof Promise) {
774
+ dataMaybePromise = Promise.race([
775
+ new Promise((r) => setTimeout(r, loadGracePeriod)),
776
+ newQuery.fetch()
777
+ ]);
778
+ }
779
+
780
+ resolveMaybePromise(
781
+ () => {
782
+ if (changeIdx !== targetChangeIdx) {
783
+ Query.#debugStatic(`changeIdx does not match, results are discarded`);
784
+ return;
785
+ }
786
+ unsub?.();
787
+ activeQuery = newQuery.value;
788
+ unsub = activeQuery.subscribe(callback);
789
+ },
790
+ dataMaybePromise,
791
+ (e) => {
792
+ console.warn(`Error while attempting to update reactive query: ${e.message}`);
793
+ throw e;
794
+ }
795
+ );
796
+ };
797
+
798
+ function removeInitialState() {
799
+ opts = { ...opts, initialData: undefined, initialError: undefined };
800
+ }
801
+
802
+ /**
803
+ * @param {string} queryText
804
+ * @param {import('../types.js').QueryOpts<any>} [newOpts]
805
+ * @returns {void}
806
+ */
807
+ return (queryText, newOpts) => {
808
+ if (activeQuery) {
809
+ resolveMaybePromise(
810
+ () => {},
811
+ waitFor(queryText, newOpts),
812
+ (e) => {
813
+ console.warn(`Error while attempting to update reactive query: ${e.message}`);
814
+ }
815
+ );
816
+ return;
817
+ }
818
+
819
+ if (import.meta.hot?.data?.hmr) removeInitialState();
820
+ activeQuery = createFn(queryText, execFn, Object.assign({}, opts, newOpts));
821
+
822
+ const fetched = activeQuery.fetch();
823
+ resolveMaybePromise(removeInitialState, fetched);
824
+
825
+ // We don't want to use this after the initial creation!
826
+ unsub = activeQuery.subscribe(callback);
827
+ callback(activeQuery);
828
+ return;
829
+ };
830
+ };
831
+
832
+ static #devModeBootstrapped = false;
833
+ static #devModeBootstraps = () => {
834
+ if (!import.meta.hot || Query.#devModeBootstrapped) return;
835
+ Query.#devModeBootstrapped = true;
836
+ // We need to do some dev mode pipeing
837
+ import.meta.hot.data.hmr = false;
838
+ import.meta.hot.on(VITE_EVENTS.RESET_QUERIES, () => {
839
+ if (import.meta.hot) import.meta.hot.data.hmr = true;
840
+ Query.emptyCache();
841
+ });
842
+ };
843
+
844
+ /**
845
+ * @template {QueryResultRow} [RowType=QueryResultRow]
846
+ * @type {import("../types.js").CreateQuery<RowType>}
847
+ */
848
+ static create = (query, executeQuery, optsOrId, maybeOpts) => {
849
+ if (import.meta.hot) {
850
+ Query.#devModeBootstraps();
851
+ }
852
+
853
+ const queryHash = hashQuery(query);
854
+ /** @type {import('../types.js').QueryOpts<RowType>} */
855
+ let opts;
856
+ if (typeof optsOrId === 'string') {
857
+ opts = {
858
+ ...maybeOpts,
859
+ id: optsOrId
860
+ };
861
+ } else if (optsOrId) {
862
+ opts = optsOrId;
863
+ if (!opts.id) opts.id = queryHash + '-' + Math.random().toString(36).substring(0, 4);
864
+ } else {
865
+ opts = {
866
+ id: queryHash + '-' + Math.random().toString(36).substring(0, 4)
867
+ };
868
+ }
869
+ if (!('autoScore' in opts)) {
870
+ opts.autoScore = true;
871
+ }
872
+
873
+ if (!opts.disableCache) {
874
+ /** @type {Query<RowType> | null} */
875
+ const cached = Query.#getFromCache(queryHash);
876
+
877
+ Query.#cacheCleanup();
878
+ if (cached) {
879
+ Query.#debugStatic(
880
+ `${opts.id ?? '[query id missing]'} (${queryHash}) | Using cached query`,
881
+ { opts, hash: hashQuery(query) },
882
+ query,
883
+ cached
884
+ );
885
+ return cached.value;
886
+ } else {
887
+ Query.#debugStatic(
888
+ `${opts.id ?? '[query id missing]'} (${queryHash}) | Cached query not found`,
889
+ {
890
+ opts,
891
+ hash: hashQuery(query)
892
+ },
893
+ query
894
+ );
895
+ }
896
+ } else
897
+ Query.#debugStatic(
898
+ `${opts.id ?? '[query id missing]'} (${queryHash}) | cache disabled`,
899
+ `Cache is disabled for ${opts.id ?? '[query id missing]'}`,
900
+ { opts, query, hash: hashQuery(query) }
901
+ );
902
+
903
+ Query.#constructing = true;
904
+ const output = new Query(query, executeQuery, opts);
905
+ Query.#globalEmit('queryCreated', {
906
+ raw: output,
907
+ proxied: output.value
908
+ });
909
+ if (!opts.disableCache) {
910
+ Query.#addToCache(output);
911
+ Query.#cacheCleanup();
912
+ }
913
+ return output.value;
914
+ };
915
+
916
+ ///////////////////////
917
+ /// </ Factories /> ///
918
+ ///////////////////////
919
+
920
+ static #debugStatic = isDebug()
921
+ ? (/** @type { string } */ label, /** @type {Parameters<typeof console.debug>} */ ...args) => {
922
+ const groupName = `${(performance.now() / 1000).toFixed(3)} | Query | ${label}`;
923
+ console.groupCollapsed(groupName);
924
+ for (const arg of args) {
925
+ if (typeof arg === 'function') console.debug(arg());
926
+ else console.debug(arg);
927
+ }
928
+ console.groupEnd();
929
+ }
930
+ : () => {};
931
+ static #debugStyledStatic = isDebug()
932
+ ? (/** @type {string} */ label, /** @type {string} */ text, /** @type {string} */ style) => {
933
+ const groupName = `${(performance.now() / 1000).toFixed(3)} | Query | ${label}`;
934
+ console.groupCollapsed(groupName);
935
+ console.debug(`%c${text}`, style);
936
+ console.groupEnd();
937
+ }
938
+ : () => {};
939
+
940
+ #debug = isDebug()
941
+ ? (/** @type {string} */ label, /** @type {Parameters<typeof console.debug>} */ ...args) => {
942
+ const groupName = `${(performance.now() / 1000).toFixed(3)} | ${this.id} (${this.hash}) | ${label}`;
943
+ console.groupCollapsed(groupName);
944
+ for (const arg of args) {
945
+ if (typeof arg === 'function') console.debug(arg());
946
+ else console.debug(arg);
947
+ }
948
+ console.groupEnd();
949
+ }
950
+ : () => {};
951
+
952
+ #debugStyled = isDebug()
953
+ ? (/** @type {string} */ label, /** @type {string} */ text, /** @type {string} */ style) => {
954
+ const groupName = `${(performance.now() / 1000).toFixed(3)} | ${this.id} (${this.hash}) | ${label}`;
955
+ console.groupCollapsed(groupName);
956
+ console.debug(`%c${text}`, style);
957
+ console.groupEnd();
958
+ }
959
+ : () => {};
960
+
961
+ static #constructing = false;
962
+
963
+ /** @type {string} */
964
+ #id;
965
+ /** @type {string} */
966
+ #hash;
967
+ /** @type {import('../types.js').QueryOpts<RowType>} */
968
+ #opts;
969
+
970
+ /** @type {Pick<import("../types.js").QueryOpts<RowType>, 'autoScore' | 'noResolve' | 'disableCache'>} */
971
+ get #inheritableOpts() {
972
+ return {
973
+ autoScore: this.#opts.autoScore,
974
+ noResolve: this.#opts.noResolve,
975
+ disableCache: this.#opts.disableCache
976
+ };
977
+ }
978
+
979
+ /** @type {string} */
980
+ get id() {
981
+ return this.#id;
982
+ }
983
+ /** @type {string} */
984
+ get hash() {
985
+ return this.#hash;
986
+ }
987
+
988
+ /** @type {import('../types.js').Runner} */
989
+ #executeQuery;
990
+
991
+ /** @type {import('../types.js').QueryOpts} */
992
+ opts;
993
+
994
+ /** @type {string | undefined} */
995
+ #createdStack;
996
+
997
+ get createdStack() {
998
+ return this.#createdStack;
999
+ }
1000
+
1001
+ // TODO: Score (this should be done in another file)
1002
+ // TODO: When dealing with builder functions, add a `select` or similar
1003
+ /**
1004
+ * @param {QueryBuilder | string} query
1005
+ * @param {import('../types.js').Runner} executeQuery
1006
+ * @param {import("../types.js").QueryOpts<RowType>} opts
1007
+ * @deprecated Use {@link Query.create} instead
1008
+ */
1009
+ constructor(query, executeQuery, opts = {}) {
1010
+ this.#createdStack = new Error().stack
1011
+ ?.split('\n')
1012
+ .slice(2)
1013
+ .map((line) => line.slice(' at '.length))
1014
+ .join('\n');
1015
+ const {
1016
+ id,
1017
+ initialData = undefined,
1018
+ knownColumns = undefined,
1019
+ initialError = undefined
1020
+ } = opts;
1021
+ this.opts = opts;
1022
+ this.#executeQuery = executeQuery;
1023
+
1024
+ if (typeof query !== 'string' && !(query instanceof QueryBuilder)) {
1025
+ console.warn(`Query ${id} has no query text`);
1026
+ opts.noResolve = true;
1027
+ }
1028
+
1029
+ if (!Query.#constructing) {
1030
+ console.warn(
1031
+ 'Directly using new Query() is not a recommended use-case. Please use Query.create()'
1032
+ );
1033
+ }
1034
+ Query.#constructing = false; // make sure we reset it
1035
+ this.#value = this.#buildProxy();
1036
+ this.#originalText = query?.toString() ?? "SELECT 'Empty Query' WHERE 0";
1037
+ this.#hash = hashQuery(this.#originalText);
1038
+ this.#id = id ?? this.#hash;
1039
+ this.#opts = opts;
1040
+
1041
+ if (query && typeof query !== 'string') this.#query = query;
1042
+ else if (query) {
1043
+ const q = new QueryBuilder()
1044
+ .from({
1045
+ /*
1046
+ Use of nanoid prevent ambiguity when dealing with nested Queries;
1047
+ in theory this could be the querystring has but that's kinda gross
1048
+ */
1049
+ [`inputQuery-${nanoid(2)}`]: taggedSql`(${sterilizeQuery(query)})`
1050
+ })
1051
+ .select('*');
1052
+ this.#query = q;
1053
+ } else {
1054
+ this.#query = new QueryBuilder();
1055
+ this.#error = new Error(`Refusing to create Query: No Query Text provided`);
1056
+ return;
1057
+ }
1058
+
1059
+ if (initialError) {
1060
+ this.#error = initialError;
1061
+ return;
1062
+ }
1063
+
1064
+ // TODO: Does this make sense?
1065
+ if (initialData) {
1066
+ this.#debug('initial data', 'Created with initial data', initialData);
1067
+
1068
+ resolveMaybePromise(
1069
+ (d) => {
1070
+ this.#data = d;
1071
+ if (opts.initialDataDirty) {
1072
+ this.publish('dataDirty');
1073
+ this.#fetchData();
1074
+ } else {
1075
+ this.#sharedDataPromise.resolve(this);
1076
+ this.#fetchLength();
1077
+ }
1078
+ },
1079
+ initialData,
1080
+ (e) => {
1081
+ this.#error = e;
1082
+ }
1083
+ );
1084
+ } else if (opts.noResolve) {
1085
+ this.#sharedDataPromise.start();
1086
+ this.#sharedLengthPromise.start();
1087
+ this.#sharedColumnsPromise.start();
1088
+ return this;
1089
+ }
1090
+
1091
+ if (knownColumns) {
1092
+ if (!Array.isArray(knownColumns))
1093
+ throw new Error(`Expected knownColumns to be an array`, { cause: knownColumns });
1094
+
1095
+ this.#debug('known columns', 'Created with known columns', knownColumns);
1096
+ this.#columns = knownColumns;
1097
+ this.#sharedColumnsPromise.resolve(this);
1098
+ } else {
1099
+ resolveMaybePromise(
1100
+ () => {
1101
+ /* We don't need to do anything with the result */
1102
+ },
1103
+ this.#fetchColumns(),
1104
+ (e, isPromise) => {
1105
+ /* Async errors are handled elsewhere */ if (!isPromise) throw e;
1106
+ }
1107
+ );
1108
+ }
1109
+ resolveMaybePromise(
1110
+ () => {
1111
+ /* We don't need to do anything with the result */
1112
+ },
1113
+ this.#fetchLength(),
1114
+ (e, isPromise) => {
1115
+ /* Async errors are handled elsewhere */ if (!isPromise) throw e;
1116
+ }
1117
+ );
1118
+ if (opts.autoScore) {
1119
+ this.#calculateScore();
1120
+ }
1121
+ }
1122
+
1123
+ ////////////////////////////////////
1124
+ /// < Implement Store Contract > ///
1125
+ ////////////////////////////////////
1126
+ /** @type {Set<import('../types.js').Subscriber<QueryValue<RowType>>>} */
1127
+ #subscribers = new Set();
1128
+
1129
+ /**
1130
+ * @param {import('../types.js').Subscriber<QueryValue<RowType>>} fn
1131
+ * @returns {() => void} Unsubscribe function
1132
+ */
1133
+ subscribe = (fn) => {
1134
+ this.#subscribers.add(fn);
1135
+ fn(this.#value);
1136
+ return () => this.#subscribers.delete(fn);
1137
+ };
1138
+
1139
+ #publishIdx = 0;
1140
+ /**
1141
+ * @protected
1142
+ */
1143
+ publish = (/** @type {string} */ source) => {
1144
+ if (this.#publishIdx++ > 100000) throw new Error('Query published too many times.');
1145
+ this.#debug('publish', `Publishing triggered by ${source}`);
1146
+ this.#subscribers.forEach((fn) => fn(this.#value));
1147
+ };
1148
+ //////////////////////////////////////
1149
+ /// </ Implement Store Contract /> ///
1150
+ //////////////////////////////////////
1151
+
1152
+ ///////////////////////////////////////
1153
+ /// < EventEmitter Implementation > ///
1154
+ ///////////////////////////////////////
1155
+ /** @type {import('../types.js').EventMap<QueryEvents>} */
1156
+ #handlerMap = {
1157
+ dataReady: new Set(),
1158
+ error: new Set(),
1159
+ highScore: new Set(),
1160
+ longRun: new Set()
1161
+ };
1162
+
1163
+ /**
1164
+ * @template {keyof QueryEvents} Event
1165
+ * @param {Event} event
1166
+ * @param {QueryEvents[Event]} value
1167
+ */
1168
+ #emit = (event, value) => {
1169
+ this.#handlerMap[event].forEach((fn) => fn(value, event));
1170
+ };
1171
+
1172
+ /**
1173
+ * @template {keyof QueryEvents} Event
1174
+ * @param {Event} event
1175
+ * @param {import('../types.js').EventHandler<QueryEvents, Event>} handler
1176
+ */
1177
+ on = (event, handler) => {
1178
+ this.#handlerMap[event].add(handler);
1179
+ };
1180
+ /**
1181
+ * @template {keyof QueryEvents} Event
1182
+ * @param {Event} event
1183
+ * @param {import('../types.js').EventHandler<QueryEvents, Event>} handler
1184
+ */
1185
+ off = (event, handler) => {
1186
+ this.#handlerMap[event].delete(handler);
1187
+ };
1188
+ addEventListener = this.on;
1189
+ removeEventListener = this.off;
1190
+
1191
+ /////////////////////////////////////////
1192
+ /// </ EventEmitter Implementation /> ///
1193
+ /////////////////////////////////////////
1194
+
1195
+ //////////////////////////////////
1196
+ /// < QueryBuilder Interface > ///
1197
+ //////////////////////////////////
1198
+ /** @param {string} filterStatement */
1199
+ where = (filterStatement) =>
1200
+ Query.create(this.#query.clone().where(taggedSql`${filterStatement}`), this.#executeQuery, {
1201
+ knownColumns: this.#columns,
1202
+ noResolve: this.#opts.noResolve
1203
+ });
1204
+
1205
+ /**
1206
+ * Attaches an `ordinal` column to the query based on some window statement
1207
+ * @example myQuery.withOrdinal('partition by a order by b')
1208
+ * @param {string} windowStatement
1209
+ * @returns
1210
+ */
1211
+ withOrdinal = (windowStatement) => {
1212
+ const newQ = this.#query.clone();
1213
+ newQ.select({
1214
+ ordinal: taggedSql`row_number() over (${windowStatement})`
1215
+ });
1216
+ return Query.create(newQ, this.#executeQuery, {
1217
+ ...this.#inheritableOpts,
1218
+ knownColumns: this.#columns
1219
+ });
1220
+ };
1221
+
1222
+ /**
1223
+ * @param {string} searchTerm
1224
+ * @param {string | string[]} searchCol
1225
+ * @param {number | undefined} [searchThreshold]
1226
+ * @returns {QueryValue<RowType & {similarity: number}>}
1227
+ */
1228
+ search = (searchTerm, searchCol, searchThreshold) => {
1229
+ if (typeof searchThreshold === 'undefined' || searchThreshold < 0 || searchThreshold > 1) {
1230
+ // By default, increase threshold as more characters are added
1231
+ searchThreshold = 1 - 1 / searchTerm.length;
1232
+ }
1233
+ /** @type {import('../../types/duckdb-wellknown.js').DescribeResultRow[]} */
1234
+ const colsWithSimilarity = [
1235
+ ...this.#columns,
1236
+ { column_name: 'similarity', column_type: 'INTEGER', nullable: 'NO' }
1237
+ ];
1238
+
1239
+ /** @type {import('../types.js').CreateQuery<any>} */
1240
+ const typedCreateFn = Query.create;
1241
+
1242
+ const escapedSearchTerm = searchTerm.replaceAll("'", "''");
1243
+
1244
+ const cols = Array.isArray(searchCol) ? searchCol : [searchCol];
1245
+ const statements = cols
1246
+ .map((col) => {
1247
+ const exactMatch = taggedSql`CASE WHEN lower("${col.trim()}") = lower('${escapedSearchTerm}') THEN 2 ELSE 0 END`;
1248
+ const similarity = taggedSql`jaccard(lower('${escapedSearchTerm}'), lower("${col}"))`;
1249
+ const exactSubMatch =
1250
+ escapedSearchTerm.length >= 1
1251
+ ? taggedSql`CASE WHEN lower("${col.trim()}") LIKE lower('%${escapedSearchTerm.split(' ').join('%')}%') THEN 1 ELSE 0 END`
1252
+ : taggedSql`0`;
1253
+ return taggedSql`GREATEST((${exactMatch}), (${similarity}), (${exactSubMatch}))`;
1254
+ })
1255
+ .join(',');
1256
+
1257
+ /** @type {QueryValue<RowType & {similarity: number}>} */
1258
+ const output = typedCreateFn(
1259
+ this.#query
1260
+ .clone()
1261
+ .$select(
1262
+ {
1263
+ similarity: taggedSql`GREATEST(${statements})`
1264
+ },
1265
+ '*'
1266
+ )
1267
+ .where(taggedSql`"similarity" > ${searchThreshold} `)
1268
+ .orderby(taggedSql`"similarity" DESC`),
1269
+ this.#executeQuery,
1270
+ {
1271
+ knownColumns: colsWithSimilarity,
1272
+ ...this.#inheritableOpts
1273
+ }
1274
+ );
1275
+ return output;
1276
+ };
1277
+
1278
+ /** @param {number} limit */
1279
+ limit = (limit) =>
1280
+ Query.create(this.#query.clone().limit(limit), this.#executeQuery, {
1281
+ knownColumns: this.#columns,
1282
+ ...this.#inheritableOpts
1283
+ });
1284
+
1285
+ /** @param {number} offset */
1286
+ offset = (offset) =>
1287
+ Query.create(this.#query.clone().offset(offset), this.#executeQuery, {
1288
+ knownColumns: this.#columns,
1289
+ ...this.#inheritableOpts
1290
+ });
1291
+ /**
1292
+ * @param {number} offset
1293
+ * @param {number} limit
1294
+ */
1295
+ paginate = (offset, limit) =>
1296
+ Query.create(this.#query.clone().offset(offset).limit(limit), this.#executeQuery, {
1297
+ knownColumns: this.#columns,
1298
+ ...this.#inheritableOpts
1299
+ });
1300
+
1301
+ /**
1302
+ * @param {string[]} columns
1303
+ * @param {boolean} [withRowCount=true]
1304
+ */
1305
+ groupBy = (columns, withRowCount) => {
1306
+ const query = this.#query.clone();
1307
+ query.$select(columns);
1308
+ if (withRowCount) query.select({ rows: qCount('*') });
1309
+ query.$groupby(columns);
1310
+
1311
+ return Query.create(query, this.#executeQuery, {
1312
+ knownColumns: this.#columns,
1313
+ ...this.#inheritableOpts
1314
+ });
1315
+ };
1316
+
1317
+ /**
1318
+ * @typedef {Object} AggArgs
1319
+ * @property {import("../types.js").MaybeAliasedCol | import("../types.js").MaybeAliasedCol[]} sum
1320
+ * @property {import("../types.js").MaybeAliasedCol | import("../types.js").MaybeAliasedCol[]} avg
1321
+ * @property {import("../types.js").MaybeAliasedCol | import("../types.js").MaybeAliasedCol[]} min
1322
+ * @property {import("../types.js").MaybeAliasedCol | import("../types.js").MaybeAliasedCol[]} max
1323
+ * @property {import("../types.js").MaybeAliasedCol | import("../types.js").MaybeAliasedCol[]} median
1324
+ */
1325
+
1326
+ /**
1327
+ * @type {Record<keyof AggArgs, CallableFunction>}
1328
+ */
1329
+ static #aggFns = {
1330
+ sum: qSum,
1331
+ avg: qAvg,
1332
+ min: qMin,
1333
+ max: qMax,
1334
+ median: qMedian
1335
+ };
1336
+ /**
1337
+ *
1338
+ * @param {string} aggKey
1339
+ * @returns {aggKey is keyof AggArgs}
1340
+ */
1341
+ static #checkAggFn = (aggKey) => {
1342
+ return aggKey in Query.#aggFns;
1343
+ };
1344
+ /**
1345
+ * @param {AggArgs} cfg
1346
+ */
1347
+ agg = (cfg) => {
1348
+ const query = this.#query.clone();
1349
+ for (const [aggType, aggArgs] of Object.entries(cfg)) {
1350
+ if (!Query.#checkAggFn(aggType)) throw new Error(`Unknown agg function: ${aggType}`);
1351
+ const aggFn = Query.#aggFns[aggType];
1352
+ const argsArray = Array.isArray(aggArgs) ? aggArgs : [aggArgs];
1353
+ for (const colSpec of argsArray) {
1354
+ const alias = typeof colSpec === 'object' ? colSpec.as : `${aggType}_${colSpec}`;
1355
+ const column = typeof colSpec === 'object' ? colSpec.col : colSpec;
1356
+ query.select({
1357
+ [alias]: aggFn(column)
1358
+ });
1359
+ }
1360
+ }
1361
+ return Query.create(query, this.#executeQuery, {
1362
+ knownColumns: this.#columns,
1363
+ ...this.#inheritableOpts
1364
+ });
1365
+ };
1366
+
1367
+ ////////////////////////////////////
1368
+ /// </ QueryBuilder Interface /> ///
1369
+ ////////////////////////////////////
1370
+ }
1371
+
1372
+ /**
1373
+ * @param {...any} args
1374
+ * @returns {string}
1375
+ */
1376
+ export const hashQuery = (...args) => {
1377
+ /**
1378
+ * @param {string} str
1379
+ * @returns {string}
1380
+ */
1381
+ const simpleHash = (str) => {
1382
+ let hash = 0;
1383
+ for (let i = 0; i < str.length; i++) {
1384
+ const char = str.charCodeAt(i);
1385
+ hash = (hash << 5) - hash + char;
1386
+ hash &= hash; // Convert to 32bit integer
1387
+ }
1388
+ return new Uint32Array([hash])[0].toString(36);
1389
+ };
1390
+ return simpleHash(JSON.stringify(args));
1391
+ };