@foretag/tanstack-db-surrealdb 0.3.1 → 0.3.2

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/dist/index.js CHANGED
@@ -1,1810 +1,10 @@
1
- var __defProp = Object.defineProperty;
2
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
- var __getOwnPropNames = Object.getOwnPropertyNames;
4
- var __hasOwnProp = Object.prototype.hasOwnProperty;
5
- var __export = (target, all) => {
6
- for (var name in all)
7
- __defProp(target, name, { get: all[name], enumerable: true });
8
- };
9
- var __copyProps = (to, from, except, desc) => {
10
- if (from && typeof from === "object" || typeof from === "function") {
11
- for (let key of __getOwnPropNames(from))
12
- if (!__hasOwnProp.call(to, key) && key !== except)
13
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
- }
15
- return to;
16
- };
17
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
-
19
- // src/index.ts
20
- var index_exports = {};
21
- __export(index_exports, {
22
- surrealCollectionOptions: () => surrealCollectionOptions
23
- });
24
- module.exports = __toCommonJS(index_exports);
25
-
26
- // node_modules/@tanstack/query-core/build/modern/subscribable.js
27
- var Subscribable = class {
28
- constructor() {
29
- this.listeners = /* @__PURE__ */ new Set();
30
- this.subscribe = this.subscribe.bind(this);
31
- }
32
- subscribe(listener) {
33
- this.listeners.add(listener);
34
- this.onSubscribe();
35
- return () => {
36
- this.listeners.delete(listener);
37
- this.onUnsubscribe();
38
- };
39
- }
40
- hasListeners() {
41
- return this.listeners.size > 0;
42
- }
43
- onSubscribe() {
44
- }
45
- onUnsubscribe() {
46
- }
47
- };
48
-
49
- // node_modules/@tanstack/query-core/build/modern/timeoutManager.js
50
- var defaultTimeoutProvider = {
51
- // We need the wrapper function syntax below instead of direct references to
52
- // global setTimeout etc.
53
- //
54
- // BAD: `setTimeout: setTimeout`
55
- // GOOD: `setTimeout: (cb, delay) => setTimeout(cb, delay)`
56
- //
57
- // If we use direct references here, then anything that wants to spy on or
58
- // replace the global setTimeout (like tests) won't work since we'll already
59
- // have a hard reference to the original implementation at the time when this
60
- // file was imported.
61
- setTimeout: (callback, delay) => setTimeout(callback, delay),
62
- clearTimeout: (timeoutId) => clearTimeout(timeoutId),
63
- setInterval: (callback, delay) => setInterval(callback, delay),
64
- clearInterval: (intervalId) => clearInterval(intervalId)
65
- };
66
- var TimeoutManager = class {
67
- // We cannot have TimeoutManager<T> as we must instantiate it with a concrete
68
- // type at app boot; and if we leave that type, then any new timer provider
69
- // would need to support ReturnType<typeof setTimeout>, which is infeasible.
70
- //
71
- // We settle for type safety for the TimeoutProvider type, and accept that
72
- // this class is unsafe internally to allow for extension.
73
- #provider = defaultTimeoutProvider;
74
- #providerCalled = false;
75
- setTimeoutProvider(provider) {
76
- if (process.env.NODE_ENV !== "production") {
77
- if (this.#providerCalled && provider !== this.#provider) {
78
- console.error(
79
- `[timeoutManager]: Switching provider after calls to previous provider might result in unexpected behavior.`,
80
- { previous: this.#provider, provider }
81
- );
82
- }
83
- }
84
- this.#provider = provider;
85
- if (process.env.NODE_ENV !== "production") {
86
- this.#providerCalled = false;
87
- }
88
- }
89
- setTimeout(callback, delay) {
90
- if (process.env.NODE_ENV !== "production") {
91
- this.#providerCalled = true;
92
- }
93
- return this.#provider.setTimeout(callback, delay);
94
- }
95
- clearTimeout(timeoutId) {
96
- this.#provider.clearTimeout(timeoutId);
97
- }
98
- setInterval(callback, delay) {
99
- if (process.env.NODE_ENV !== "production") {
100
- this.#providerCalled = true;
101
- }
102
- return this.#provider.setInterval(callback, delay);
103
- }
104
- clearInterval(intervalId) {
105
- this.#provider.clearInterval(intervalId);
106
- }
107
- };
108
- var timeoutManager = new TimeoutManager();
109
- function systemSetTimeoutZero(callback) {
110
- setTimeout(callback, 0);
111
- }
112
-
113
- // node_modules/@tanstack/query-core/build/modern/utils.js
114
- var isServer = typeof window === "undefined" || "Deno" in globalThis;
115
- function noop() {
116
- }
117
- function isValidTimeout(value) {
118
- return typeof value === "number" && value >= 0 && value !== Infinity;
119
- }
120
- function timeUntilStale(updatedAt, staleTime) {
121
- return Math.max(updatedAt + (staleTime || 0) - Date.now(), 0);
122
- }
123
- function resolveStaleTime(staleTime, query) {
124
- return typeof staleTime === "function" ? staleTime(query) : staleTime;
125
- }
126
- function resolveEnabled(enabled, query) {
127
- return typeof enabled === "function" ? enabled(query) : enabled;
128
- }
129
- function hashKey(queryKey) {
130
- return JSON.stringify(
131
- queryKey,
132
- (_, val) => isPlainObject(val) ? Object.keys(val).sort().reduce((result, key) => {
133
- result[key] = val[key];
134
- return result;
135
- }, {}) : val
136
- );
137
- }
138
- var hasOwn = Object.prototype.hasOwnProperty;
139
- function replaceEqualDeep(a, b) {
140
- if (a === b) {
141
- return a;
142
- }
143
- const array = isPlainArray(a) && isPlainArray(b);
144
- if (!array && !(isPlainObject(a) && isPlainObject(b))) return b;
145
- const aItems = array ? a : Object.keys(a);
146
- const aSize = aItems.length;
147
- const bItems = array ? b : Object.keys(b);
148
- const bSize = bItems.length;
149
- const copy = array ? new Array(bSize) : {};
150
- let equalItems = 0;
151
- for (let i = 0; i < bSize; i++) {
152
- const key = array ? i : bItems[i];
153
- const aItem = a[key];
154
- const bItem = b[key];
155
- if (aItem === bItem) {
156
- copy[key] = aItem;
157
- if (array ? i < aSize : hasOwn.call(a, key)) equalItems++;
158
- continue;
159
- }
160
- if (aItem === null || bItem === null || typeof aItem !== "object" || typeof bItem !== "object") {
161
- copy[key] = bItem;
162
- continue;
163
- }
164
- const v = replaceEqualDeep(aItem, bItem);
165
- copy[key] = v;
166
- if (v === aItem) equalItems++;
167
- }
168
- return aSize === bSize && equalItems === aSize ? a : copy;
169
- }
170
- function shallowEqualObjects(a, b) {
171
- if (!b || Object.keys(a).length !== Object.keys(b).length) {
172
- return false;
173
- }
174
- for (const key in a) {
175
- if (a[key] !== b[key]) {
176
- return false;
177
- }
178
- }
179
- return true;
180
- }
181
- function isPlainArray(value) {
182
- return Array.isArray(value) && value.length === Object.keys(value).length;
183
- }
184
- function isPlainObject(o) {
185
- if (!hasObjectPrototype(o)) {
186
- return false;
187
- }
188
- const ctor = o.constructor;
189
- if (ctor === void 0) {
190
- return true;
191
- }
192
- const prot = ctor.prototype;
193
- if (!hasObjectPrototype(prot)) {
194
- return false;
195
- }
196
- if (!prot.hasOwnProperty("isPrototypeOf")) {
197
- return false;
198
- }
199
- if (Object.getPrototypeOf(o) !== Object.prototype) {
200
- return false;
201
- }
202
- return true;
203
- }
204
- function hasObjectPrototype(o) {
205
- return Object.prototype.toString.call(o) === "[object Object]";
206
- }
207
- function replaceData(prevData, data, options) {
208
- if (typeof options.structuralSharing === "function") {
209
- return options.structuralSharing(prevData, data);
210
- } else if (options.structuralSharing !== false) {
211
- if (process.env.NODE_ENV !== "production") {
212
- try {
213
- return replaceEqualDeep(prevData, data);
214
- } catch (error) {
215
- console.error(
216
- `Structural sharing requires data to be JSON serializable. To fix this, turn off structuralSharing or return JSON-serializable data from your queryFn. [${options.queryHash}]: ${error}`
217
- );
218
- throw error;
219
- }
220
- }
221
- return replaceEqualDeep(prevData, data);
222
- }
223
- return data;
224
- }
225
- var skipToken = Symbol();
226
-
227
- // node_modules/@tanstack/query-core/build/modern/focusManager.js
228
- var FocusManager = class extends Subscribable {
229
- #focused;
230
- #cleanup;
231
- #setup;
232
- constructor() {
233
- super();
234
- this.#setup = (onFocus) => {
235
- if (!isServer && window.addEventListener) {
236
- const listener = () => onFocus();
237
- window.addEventListener("visibilitychange", listener, false);
238
- return () => {
239
- window.removeEventListener("visibilitychange", listener);
240
- };
241
- }
242
- return;
243
- };
244
- }
245
- onSubscribe() {
246
- if (!this.#cleanup) {
247
- this.setEventListener(this.#setup);
248
- }
249
- }
250
- onUnsubscribe() {
251
- if (!this.hasListeners()) {
252
- this.#cleanup?.();
253
- this.#cleanup = void 0;
254
- }
255
- }
256
- setEventListener(setup) {
257
- this.#setup = setup;
258
- this.#cleanup?.();
259
- this.#cleanup = setup((focused) => {
260
- if (typeof focused === "boolean") {
261
- this.setFocused(focused);
262
- } else {
263
- this.onFocus();
264
- }
265
- });
266
- }
267
- setFocused(focused) {
268
- const changed = this.#focused !== focused;
269
- if (changed) {
270
- this.#focused = focused;
271
- this.onFocus();
272
- }
273
- }
274
- onFocus() {
275
- const isFocused = this.isFocused();
276
- this.listeners.forEach((listener) => {
277
- listener(isFocused);
278
- });
279
- }
280
- isFocused() {
281
- if (typeof this.#focused === "boolean") {
282
- return this.#focused;
283
- }
284
- return globalThis.document?.visibilityState !== "hidden";
285
- }
286
- };
287
- var focusManager = new FocusManager();
288
-
289
- // node_modules/@tanstack/query-core/build/modern/thenable.js
290
- function pendingThenable() {
291
- let resolve;
292
- let reject;
293
- const thenable = new Promise((_resolve, _reject) => {
294
- resolve = _resolve;
295
- reject = _reject;
296
- });
297
- thenable.status = "pending";
298
- thenable.catch(() => {
299
- });
300
- function finalize(data) {
301
- Object.assign(thenable, data);
302
- delete thenable.resolve;
303
- delete thenable.reject;
304
- }
305
- thenable.resolve = (value) => {
306
- finalize({
307
- status: "fulfilled",
308
- value
309
- });
310
- resolve(value);
311
- };
312
- thenable.reject = (reason) => {
313
- finalize({
314
- status: "rejected",
315
- reason
316
- });
317
- reject(reason);
318
- };
319
- return thenable;
320
- }
321
-
322
- // node_modules/@tanstack/query-core/build/modern/notifyManager.js
323
- var defaultScheduler = systemSetTimeoutZero;
324
- function createNotifyManager() {
325
- let queue = [];
326
- let transactions = 0;
327
- let notifyFn = (callback) => {
328
- callback();
329
- };
330
- let batchNotifyFn = (callback) => {
331
- callback();
332
- };
333
- let scheduleFn = defaultScheduler;
334
- const schedule = (callback) => {
335
- if (transactions) {
336
- queue.push(callback);
337
- } else {
338
- scheduleFn(() => {
339
- notifyFn(callback);
340
- });
341
- }
342
- };
343
- const flush = () => {
344
- const originalQueue = queue;
345
- queue = [];
346
- if (originalQueue.length) {
347
- scheduleFn(() => {
348
- batchNotifyFn(() => {
349
- originalQueue.forEach((callback) => {
350
- notifyFn(callback);
351
- });
352
- });
353
- });
354
- }
355
- };
356
- return {
357
- batch: (callback) => {
358
- let result;
359
- transactions++;
360
- try {
361
- result = callback();
362
- } finally {
363
- transactions--;
364
- if (!transactions) {
365
- flush();
366
- }
367
- }
368
- return result;
369
- },
370
- /**
371
- * All calls to the wrapped function will be batched.
372
- */
373
- batchCalls: (callback) => {
374
- return (...args) => {
375
- schedule(() => {
376
- callback(...args);
377
- });
378
- };
379
- },
380
- schedule,
381
- /**
382
- * Use this method to set a custom notify function.
383
- * This can be used to for example wrap notifications with `React.act` while running tests.
384
- */
385
- setNotifyFunction: (fn) => {
386
- notifyFn = fn;
387
- },
388
- /**
389
- * Use this method to set a custom function to batch notifications together into a single tick.
390
- * By default React Query will use the batch function provided by ReactDOM or React Native.
391
- */
392
- setBatchNotifyFunction: (fn) => {
393
- batchNotifyFn = fn;
394
- },
395
- setScheduler: (fn) => {
396
- scheduleFn = fn;
397
- }
398
- };
399
- }
400
- var notifyManager = createNotifyManager();
401
-
402
- // node_modules/@tanstack/query-core/build/modern/onlineManager.js
403
- var OnlineManager = class extends Subscribable {
404
- #online = true;
405
- #cleanup;
406
- #setup;
407
- constructor() {
408
- super();
409
- this.#setup = (onOnline) => {
410
- if (!isServer && window.addEventListener) {
411
- const onlineListener = () => onOnline(true);
412
- const offlineListener = () => onOnline(false);
413
- window.addEventListener("online", onlineListener, false);
414
- window.addEventListener("offline", offlineListener, false);
415
- return () => {
416
- window.removeEventListener("online", onlineListener);
417
- window.removeEventListener("offline", offlineListener);
418
- };
419
- }
420
- return;
421
- };
422
- }
423
- onSubscribe() {
424
- if (!this.#cleanup) {
425
- this.setEventListener(this.#setup);
426
- }
427
- }
428
- onUnsubscribe() {
429
- if (!this.hasListeners()) {
430
- this.#cleanup?.();
431
- this.#cleanup = void 0;
432
- }
433
- }
434
- setEventListener(setup) {
435
- this.#setup = setup;
436
- this.#cleanup?.();
437
- this.#cleanup = setup(this.setOnline.bind(this));
438
- }
439
- setOnline(online) {
440
- const changed = this.#online !== online;
441
- if (changed) {
442
- this.#online = online;
443
- this.listeners.forEach((listener) => {
444
- listener(online);
445
- });
446
- }
447
- }
448
- isOnline() {
449
- return this.#online;
450
- }
451
- };
452
- var onlineManager = new OnlineManager();
453
-
454
- // node_modules/@tanstack/query-core/build/modern/retryer.js
455
- function canFetch(networkMode) {
456
- return (networkMode ?? "online") === "online" ? onlineManager.isOnline() : true;
457
- }
458
-
459
- // node_modules/@tanstack/query-core/build/modern/query.js
460
- function fetchState(data, options) {
461
- return {
462
- fetchFailureCount: 0,
463
- fetchFailureReason: null,
464
- fetchStatus: canFetch(options.networkMode) ? "fetching" : "paused",
465
- ...data === void 0 && {
466
- error: null,
467
- status: "pending"
468
- }
469
- };
470
- }
471
-
472
- // node_modules/@tanstack/query-core/build/modern/queryObserver.js
473
- var QueryObserver = class extends Subscribable {
474
- constructor(client, options) {
475
- super();
476
- this.options = options;
477
- this.#client = client;
478
- this.#selectError = null;
479
- this.#currentThenable = pendingThenable();
480
- this.bindMethods();
481
- this.setOptions(options);
482
- }
483
- #client;
484
- #currentQuery = void 0;
485
- #currentQueryInitialState = void 0;
486
- #currentResult = void 0;
487
- #currentResultState;
488
- #currentResultOptions;
489
- #currentThenable;
490
- #selectError;
491
- #selectFn;
492
- #selectResult;
493
- // This property keeps track of the last query with defined data.
494
- // It will be used to pass the previous data and query to the placeholder function between renders.
495
- #lastQueryWithDefinedData;
496
- #staleTimeoutId;
497
- #refetchIntervalId;
498
- #currentRefetchInterval;
499
- #trackedProps = /* @__PURE__ */ new Set();
500
- bindMethods() {
501
- this.refetch = this.refetch.bind(this);
502
- }
503
- onSubscribe() {
504
- if (this.listeners.size === 1) {
505
- this.#currentQuery.addObserver(this);
506
- if (shouldFetchOnMount(this.#currentQuery, this.options)) {
507
- this.#executeFetch();
508
- } else {
509
- this.updateResult();
510
- }
511
- this.#updateTimers();
512
- }
513
- }
514
- onUnsubscribe() {
515
- if (!this.hasListeners()) {
516
- this.destroy();
517
- }
518
- }
519
- shouldFetchOnReconnect() {
520
- return shouldFetchOn(
521
- this.#currentQuery,
522
- this.options,
523
- this.options.refetchOnReconnect
524
- );
525
- }
526
- shouldFetchOnWindowFocus() {
527
- return shouldFetchOn(
528
- this.#currentQuery,
529
- this.options,
530
- this.options.refetchOnWindowFocus
531
- );
532
- }
533
- destroy() {
534
- this.listeners = /* @__PURE__ */ new Set();
535
- this.#clearStaleTimeout();
536
- this.#clearRefetchInterval();
537
- this.#currentQuery.removeObserver(this);
538
- }
539
- setOptions(options) {
540
- const prevOptions = this.options;
541
- const prevQuery = this.#currentQuery;
542
- this.options = this.#client.defaultQueryOptions(options);
543
- if (this.options.enabled !== void 0 && typeof this.options.enabled !== "boolean" && typeof this.options.enabled !== "function" && typeof resolveEnabled(this.options.enabled, this.#currentQuery) !== "boolean") {
544
- throw new Error(
545
- "Expected enabled to be a boolean or a callback that returns a boolean"
546
- );
547
- }
548
- this.#updateQuery();
549
- this.#currentQuery.setOptions(this.options);
550
- if (prevOptions._defaulted && !shallowEqualObjects(this.options, prevOptions)) {
551
- this.#client.getQueryCache().notify({
552
- type: "observerOptionsUpdated",
553
- query: this.#currentQuery,
554
- observer: this
555
- });
556
- }
557
- const mounted = this.hasListeners();
558
- if (mounted && shouldFetchOptionally(
559
- this.#currentQuery,
560
- prevQuery,
561
- this.options,
562
- prevOptions
563
- )) {
564
- this.#executeFetch();
565
- }
566
- this.updateResult();
567
- if (mounted && (this.#currentQuery !== prevQuery || resolveEnabled(this.options.enabled, this.#currentQuery) !== resolveEnabled(prevOptions.enabled, this.#currentQuery) || resolveStaleTime(this.options.staleTime, this.#currentQuery) !== resolveStaleTime(prevOptions.staleTime, this.#currentQuery))) {
568
- this.#updateStaleTimeout();
569
- }
570
- const nextRefetchInterval = this.#computeRefetchInterval();
571
- if (mounted && (this.#currentQuery !== prevQuery || resolveEnabled(this.options.enabled, this.#currentQuery) !== resolveEnabled(prevOptions.enabled, this.#currentQuery) || nextRefetchInterval !== this.#currentRefetchInterval)) {
572
- this.#updateRefetchInterval(nextRefetchInterval);
573
- }
574
- }
575
- getOptimisticResult(options) {
576
- const query = this.#client.getQueryCache().build(this.#client, options);
577
- const result = this.createResult(query, options);
578
- if (shouldAssignObserverCurrentProperties(this, result)) {
579
- this.#currentResult = result;
580
- this.#currentResultOptions = this.options;
581
- this.#currentResultState = this.#currentQuery.state;
582
- }
583
- return result;
584
- }
585
- getCurrentResult() {
586
- return this.#currentResult;
587
- }
588
- trackResult(result, onPropTracked) {
589
- return new Proxy(result, {
590
- get: (target, key) => {
591
- this.trackProp(key);
592
- onPropTracked?.(key);
593
- if (key === "promise") {
594
- this.trackProp("data");
595
- if (!this.options.experimental_prefetchInRender && this.#currentThenable.status === "pending") {
596
- this.#currentThenable.reject(
597
- new Error(
598
- "experimental_prefetchInRender feature flag is not enabled"
599
- )
600
- );
601
- }
602
- }
603
- return Reflect.get(target, key);
604
- }
605
- });
606
- }
607
- trackProp(key) {
608
- this.#trackedProps.add(key);
609
- }
610
- getCurrentQuery() {
611
- return this.#currentQuery;
612
- }
613
- refetch({ ...options } = {}) {
614
- return this.fetch({
615
- ...options
616
- });
617
- }
618
- fetchOptimistic(options) {
619
- const defaultedOptions = this.#client.defaultQueryOptions(options);
620
- const query = this.#client.getQueryCache().build(this.#client, defaultedOptions);
621
- return query.fetch().then(() => this.createResult(query, defaultedOptions));
622
- }
623
- fetch(fetchOptions) {
624
- return this.#executeFetch({
625
- ...fetchOptions,
626
- cancelRefetch: fetchOptions.cancelRefetch ?? true
627
- }).then(() => {
628
- this.updateResult();
629
- return this.#currentResult;
630
- });
631
- }
632
- #executeFetch(fetchOptions) {
633
- this.#updateQuery();
634
- let promise = this.#currentQuery.fetch(
635
- this.options,
636
- fetchOptions
637
- );
638
- if (!fetchOptions?.throwOnError) {
639
- promise = promise.catch(noop);
640
- }
641
- return promise;
642
- }
643
- #updateStaleTimeout() {
644
- this.#clearStaleTimeout();
645
- const staleTime = resolveStaleTime(
646
- this.options.staleTime,
647
- this.#currentQuery
648
- );
649
- if (isServer || this.#currentResult.isStale || !isValidTimeout(staleTime)) {
650
- return;
651
- }
652
- const time = timeUntilStale(this.#currentResult.dataUpdatedAt, staleTime);
653
- const timeout = time + 1;
654
- this.#staleTimeoutId = timeoutManager.setTimeout(() => {
655
- if (!this.#currentResult.isStale) {
656
- this.updateResult();
657
- }
658
- }, timeout);
659
- }
660
- #computeRefetchInterval() {
661
- return (typeof this.options.refetchInterval === "function" ? this.options.refetchInterval(this.#currentQuery) : this.options.refetchInterval) ?? false;
662
- }
663
- #updateRefetchInterval(nextInterval) {
664
- this.#clearRefetchInterval();
665
- this.#currentRefetchInterval = nextInterval;
666
- if (isServer || resolveEnabled(this.options.enabled, this.#currentQuery) === false || !isValidTimeout(this.#currentRefetchInterval) || this.#currentRefetchInterval === 0) {
667
- return;
668
- }
669
- this.#refetchIntervalId = timeoutManager.setInterval(() => {
670
- if (this.options.refetchIntervalInBackground || focusManager.isFocused()) {
671
- this.#executeFetch();
672
- }
673
- }, this.#currentRefetchInterval);
674
- }
675
- #updateTimers() {
676
- this.#updateStaleTimeout();
677
- this.#updateRefetchInterval(this.#computeRefetchInterval());
678
- }
679
- #clearStaleTimeout() {
680
- if (this.#staleTimeoutId) {
681
- timeoutManager.clearTimeout(this.#staleTimeoutId);
682
- this.#staleTimeoutId = void 0;
683
- }
684
- }
685
- #clearRefetchInterval() {
686
- if (this.#refetchIntervalId) {
687
- timeoutManager.clearInterval(this.#refetchIntervalId);
688
- this.#refetchIntervalId = void 0;
689
- }
690
- }
691
- createResult(query, options) {
692
- const prevQuery = this.#currentQuery;
693
- const prevOptions = this.options;
694
- const prevResult = this.#currentResult;
695
- const prevResultState = this.#currentResultState;
696
- const prevResultOptions = this.#currentResultOptions;
697
- const queryChange = query !== prevQuery;
698
- const queryInitialState = queryChange ? query.state : this.#currentQueryInitialState;
699
- const { state } = query;
700
- let newState = { ...state };
701
- let isPlaceholderData = false;
702
- let data;
703
- if (options._optimisticResults) {
704
- const mounted = this.hasListeners();
705
- const fetchOnMount = !mounted && shouldFetchOnMount(query, options);
706
- const fetchOptionally = mounted && shouldFetchOptionally(query, prevQuery, options, prevOptions);
707
- if (fetchOnMount || fetchOptionally) {
708
- newState = {
709
- ...newState,
710
- ...fetchState(state.data, query.options)
711
- };
712
- }
713
- if (options._optimisticResults === "isRestoring") {
714
- newState.fetchStatus = "idle";
715
- }
716
- }
717
- let { error, errorUpdatedAt, status } = newState;
718
- data = newState.data;
719
- let skipSelect = false;
720
- if (options.placeholderData !== void 0 && data === void 0 && status === "pending") {
721
- let placeholderData;
722
- if (prevResult?.isPlaceholderData && options.placeholderData === prevResultOptions?.placeholderData) {
723
- placeholderData = prevResult.data;
724
- skipSelect = true;
725
- } else {
726
- placeholderData = typeof options.placeholderData === "function" ? options.placeholderData(
727
- this.#lastQueryWithDefinedData?.state.data,
728
- this.#lastQueryWithDefinedData
729
- ) : options.placeholderData;
730
- }
731
- if (placeholderData !== void 0) {
732
- status = "success";
733
- data = replaceData(
734
- prevResult?.data,
735
- placeholderData,
736
- options
737
- );
738
- isPlaceholderData = true;
739
- }
740
- }
741
- if (options.select && data !== void 0 && !skipSelect) {
742
- if (prevResult && data === prevResultState?.data && options.select === this.#selectFn) {
743
- data = this.#selectResult;
744
- } else {
745
- try {
746
- this.#selectFn = options.select;
747
- data = options.select(data);
748
- data = replaceData(prevResult?.data, data, options);
749
- this.#selectResult = data;
750
- this.#selectError = null;
751
- } catch (selectError) {
752
- this.#selectError = selectError;
753
- }
754
- }
755
- }
756
- if (this.#selectError) {
757
- error = this.#selectError;
758
- data = this.#selectResult;
759
- errorUpdatedAt = Date.now();
760
- status = "error";
761
- }
762
- const isFetching = newState.fetchStatus === "fetching";
763
- const isPending = status === "pending";
764
- const isError = status === "error";
765
- const isLoading = isPending && isFetching;
766
- const hasData = data !== void 0;
767
- const result = {
768
- status,
769
- fetchStatus: newState.fetchStatus,
770
- isPending,
771
- isSuccess: status === "success",
772
- isError,
773
- isInitialLoading: isLoading,
774
- isLoading,
775
- data,
776
- dataUpdatedAt: newState.dataUpdatedAt,
777
- error,
778
- errorUpdatedAt,
779
- failureCount: newState.fetchFailureCount,
780
- failureReason: newState.fetchFailureReason,
781
- errorUpdateCount: newState.errorUpdateCount,
782
- isFetched: newState.dataUpdateCount > 0 || newState.errorUpdateCount > 0,
783
- isFetchedAfterMount: newState.dataUpdateCount > queryInitialState.dataUpdateCount || newState.errorUpdateCount > queryInitialState.errorUpdateCount,
784
- isFetching,
785
- isRefetching: isFetching && !isPending,
786
- isLoadingError: isError && !hasData,
787
- isPaused: newState.fetchStatus === "paused",
788
- isPlaceholderData,
789
- isRefetchError: isError && hasData,
790
- isStale: isStale(query, options),
791
- refetch: this.refetch,
792
- promise: this.#currentThenable,
793
- isEnabled: resolveEnabled(options.enabled, query) !== false
794
- };
795
- const nextResult = result;
796
- if (this.options.experimental_prefetchInRender) {
797
- const finalizeThenableIfPossible = (thenable) => {
798
- if (nextResult.status === "error") {
799
- thenable.reject(nextResult.error);
800
- } else if (nextResult.data !== void 0) {
801
- thenable.resolve(nextResult.data);
802
- }
803
- };
804
- const recreateThenable = () => {
805
- const pending = this.#currentThenable = nextResult.promise = pendingThenable();
806
- finalizeThenableIfPossible(pending);
807
- };
808
- const prevThenable = this.#currentThenable;
809
- switch (prevThenable.status) {
810
- case "pending":
811
- if (query.queryHash === prevQuery.queryHash) {
812
- finalizeThenableIfPossible(prevThenable);
813
- }
814
- break;
815
- case "fulfilled":
816
- if (nextResult.status === "error" || nextResult.data !== prevThenable.value) {
817
- recreateThenable();
818
- }
819
- break;
820
- case "rejected":
821
- if (nextResult.status !== "error" || nextResult.error !== prevThenable.reason) {
822
- recreateThenable();
823
- }
824
- break;
825
- }
826
- }
827
- return nextResult;
828
- }
829
- updateResult() {
830
- const prevResult = this.#currentResult;
831
- const nextResult = this.createResult(this.#currentQuery, this.options);
832
- this.#currentResultState = this.#currentQuery.state;
833
- this.#currentResultOptions = this.options;
834
- if (this.#currentResultState.data !== void 0) {
835
- this.#lastQueryWithDefinedData = this.#currentQuery;
836
- }
837
- if (shallowEqualObjects(nextResult, prevResult)) {
838
- return;
839
- }
840
- this.#currentResult = nextResult;
841
- const shouldNotifyListeners = () => {
842
- if (!prevResult) {
843
- return true;
844
- }
845
- const { notifyOnChangeProps } = this.options;
846
- const notifyOnChangePropsValue = typeof notifyOnChangeProps === "function" ? notifyOnChangeProps() : notifyOnChangeProps;
847
- if (notifyOnChangePropsValue === "all" || !notifyOnChangePropsValue && !this.#trackedProps.size) {
848
- return true;
849
- }
850
- const includedProps = new Set(
851
- notifyOnChangePropsValue ?? this.#trackedProps
852
- );
853
- if (this.options.throwOnError) {
854
- includedProps.add("error");
855
- }
856
- return Object.keys(this.#currentResult).some((key) => {
857
- const typedKey = key;
858
- const changed = this.#currentResult[typedKey] !== prevResult[typedKey];
859
- return changed && includedProps.has(typedKey);
860
- });
861
- };
862
- this.#notify({ listeners: shouldNotifyListeners() });
863
- }
864
- #updateQuery() {
865
- const query = this.#client.getQueryCache().build(this.#client, this.options);
866
- if (query === this.#currentQuery) {
867
- return;
868
- }
869
- const prevQuery = this.#currentQuery;
870
- this.#currentQuery = query;
871
- this.#currentQueryInitialState = query.state;
872
- if (this.hasListeners()) {
873
- prevQuery?.removeObserver(this);
874
- query.addObserver(this);
875
- }
876
- }
877
- onQueryUpdate() {
878
- this.updateResult();
879
- if (this.hasListeners()) {
880
- this.#updateTimers();
881
- }
882
- }
883
- #notify(notifyOptions) {
884
- notifyManager.batch(() => {
885
- if (notifyOptions.listeners) {
886
- this.listeners.forEach((listener) => {
887
- listener(this.#currentResult);
888
- });
889
- }
890
- this.#client.getQueryCache().notify({
891
- query: this.#currentQuery,
892
- type: "observerResultsUpdated"
893
- });
894
- });
895
- }
896
- };
897
- function shouldLoadOnMount(query, options) {
898
- return resolveEnabled(options.enabled, query) !== false && query.state.data === void 0 && !(query.state.status === "error" && options.retryOnMount === false);
899
- }
900
- function shouldFetchOnMount(query, options) {
901
- return shouldLoadOnMount(query, options) || query.state.data !== void 0 && shouldFetchOn(query, options, options.refetchOnMount);
902
- }
903
- function shouldFetchOn(query, options, field) {
904
- if (resolveEnabled(options.enabled, query) !== false && resolveStaleTime(options.staleTime, query) !== "static") {
905
- const value = typeof field === "function" ? field(query) : field;
906
- return value === "always" || value !== false && isStale(query, options);
907
- }
908
- return false;
909
- }
910
- function shouldFetchOptionally(query, prevQuery, options, prevOptions) {
911
- return (query !== prevQuery || resolveEnabled(prevOptions.enabled, query) === false) && (!options.suspense || query.state.status !== "error") && isStale(query, options);
912
- }
913
- function isStale(query, options) {
914
- return resolveEnabled(options.enabled, query) !== false && query.isStaleByTime(resolveStaleTime(options.staleTime, query));
915
- }
916
- function shouldAssignObserverCurrentProperties(observer, optimisticResult) {
917
- if (!shallowEqualObjects(observer.getCurrentResult(), optimisticResult)) {
918
- return true;
919
- }
920
- return false;
921
- }
922
-
923
- // node_modules/@tanstack/query-db-collection/dist/esm/query.js
924
- var import_db2 = require("@tanstack/db");
925
-
926
- // node_modules/@tanstack/query-db-collection/dist/esm/errors.js
927
- var import_db = require("@tanstack/db");
928
- var QueryCollectionError = class extends import_db.TanStackDBError {
929
- constructor(message) {
930
- super(message);
931
- this.name = `QueryCollectionError`;
932
- }
933
- };
934
- var QueryKeyRequiredError = class extends QueryCollectionError {
935
- constructor() {
936
- super(`[QueryCollection] queryKey must be provided.`);
937
- this.name = `QueryKeyRequiredError`;
938
- }
939
- };
940
- var QueryFnRequiredError = class extends QueryCollectionError {
941
- constructor() {
942
- super(`[QueryCollection] queryFn must be provided.`);
943
- this.name = `QueryFnRequiredError`;
944
- }
945
- };
946
- var QueryClientRequiredError = class extends QueryCollectionError {
947
- constructor() {
948
- super(`[QueryCollection] queryClient must be provided.`);
949
- this.name = `QueryClientRequiredError`;
950
- }
951
- };
952
- var GetKeyRequiredError = class extends QueryCollectionError {
953
- constructor() {
954
- super(`[QueryCollection] getKey must be provided.`);
955
- this.name = `GetKeyRequiredError`;
956
- }
957
- };
958
- var SyncNotInitializedError = class extends QueryCollectionError {
959
- constructor() {
960
- super(
961
- `Collection must be in 'ready' state for manual sync operations. Sync not initialized yet.`
962
- );
963
- this.name = `SyncNotInitializedError`;
964
- }
965
- };
966
- var DuplicateKeyInBatchError = class extends QueryCollectionError {
967
- constructor(key) {
968
- super(`Duplicate key '${key}' found within batch operations`);
969
- this.name = `DuplicateKeyInBatchError`;
970
- }
971
- };
972
- var UpdateOperationItemNotFoundError = class extends QueryCollectionError {
973
- constructor(key) {
974
- super(`Update operation: Item with key '${key}' does not exist`);
975
- this.name = `UpdateOperationItemNotFoundError`;
976
- }
977
- };
978
- var DeleteOperationItemNotFoundError = class extends QueryCollectionError {
979
- constructor(key) {
980
- super(`Delete operation: Item with key '${key}' does not exist`);
981
- this.name = `DeleteOperationItemNotFoundError`;
982
- }
983
- };
984
-
985
- // node_modules/@tanstack/query-db-collection/dist/esm/manual-sync.js
986
- var activeBatchContexts = /* @__PURE__ */ new WeakMap();
987
- function normalizeOperations(ops, ctx) {
988
- const operations = Array.isArray(ops) ? ops : [ops];
989
- const normalized = [];
990
- for (const op of operations) {
991
- if (op.type === `delete`) {
992
- const keys = Array.isArray(op.key) ? op.key : [op.key];
993
- for (const key of keys) {
994
- normalized.push({ type: `delete`, key });
995
- }
996
- } else {
997
- const items = Array.isArray(op.data) ? op.data : [op.data];
998
- for (const item of items) {
999
- let key;
1000
- if (op.type === `update`) {
1001
- key = ctx.getKey(item);
1002
- } else {
1003
- const resolved = ctx.collection.validateData(
1004
- item,
1005
- op.type === `upsert` ? `insert` : op.type
1006
- );
1007
- key = ctx.getKey(resolved);
1008
- }
1009
- normalized.push({ type: op.type, key, data: item });
1010
- }
1011
- }
1012
- }
1013
- return normalized;
1014
- }
1015
- function validateOperations(operations, ctx) {
1016
- const seenKeys = /* @__PURE__ */ new Set();
1017
- for (const op of operations) {
1018
- if (seenKeys.has(op.key)) {
1019
- throw new DuplicateKeyInBatchError(op.key);
1020
- }
1021
- seenKeys.add(op.key);
1022
- if (op.type === `update`) {
1023
- if (!ctx.collection._state.syncedData.has(op.key)) {
1024
- throw new UpdateOperationItemNotFoundError(op.key);
1025
- }
1026
- } else if (op.type === `delete`) {
1027
- if (!ctx.collection._state.syncedData.has(op.key)) {
1028
- throw new DeleteOperationItemNotFoundError(op.key);
1029
- }
1030
- }
1031
- }
1032
- }
1033
- function performWriteOperations(operations, ctx) {
1034
- const normalized = normalizeOperations(operations, ctx);
1035
- validateOperations(normalized, ctx);
1036
- ctx.begin();
1037
- for (const op of normalized) {
1038
- switch (op.type) {
1039
- case `insert`: {
1040
- const resolved = ctx.collection.validateData(op.data, `insert`);
1041
- ctx.write({
1042
- type: `insert`,
1043
- value: resolved
1044
- });
1045
- break;
1046
- }
1047
- case `update`: {
1048
- const currentItem = ctx.collection._state.syncedData.get(op.key);
1049
- const updatedItem = {
1050
- ...currentItem,
1051
- ...op.data
1052
- };
1053
- const resolved = ctx.collection.validateData(
1054
- updatedItem,
1055
- `update`,
1056
- op.key
1057
- );
1058
- ctx.write({
1059
- type: `update`,
1060
- value: resolved
1061
- });
1062
- break;
1063
- }
1064
- case `delete`: {
1065
- const currentItem = ctx.collection._state.syncedData.get(op.key);
1066
- ctx.write({
1067
- type: `delete`,
1068
- value: currentItem
1069
- });
1070
- break;
1071
- }
1072
- case `upsert`: {
1073
- const existsInSyncedStore = ctx.collection._state.syncedData.has(op.key);
1074
- const resolved = ctx.collection.validateData(
1075
- op.data,
1076
- existsInSyncedStore ? `update` : `insert`,
1077
- op.key
1078
- );
1079
- if (existsInSyncedStore) {
1080
- ctx.write({
1081
- type: `update`,
1082
- value: resolved
1083
- });
1084
- } else {
1085
- ctx.write({
1086
- type: `insert`,
1087
- value: resolved
1088
- });
1089
- }
1090
- break;
1091
- }
1092
- }
1093
- }
1094
- ctx.commit();
1095
- const updatedData = Array.from(ctx.collection._state.syncedData.values());
1096
- if (ctx.updateCacheData) {
1097
- ctx.updateCacheData(updatedData);
1098
- } else {
1099
- ctx.queryClient.setQueryData(ctx.queryKey, updatedData);
1100
- }
1101
- }
1102
- function createWriteUtils(getContext) {
1103
- function ensureContext() {
1104
- const context = getContext();
1105
- if (!context) {
1106
- throw new SyncNotInitializedError();
1107
- }
1108
- return context;
1109
- }
1110
- return {
1111
- writeInsert(data) {
1112
- const operation = {
1113
- type: `insert`,
1114
- data
1115
- };
1116
- const ctx = ensureContext();
1117
- const batchContext = activeBatchContexts.get(ctx);
1118
- if (batchContext?.isActive) {
1119
- batchContext.operations.push(operation);
1120
- return;
1121
- }
1122
- performWriteOperations(operation, ctx);
1123
- },
1124
- writeUpdate(data) {
1125
- const operation = {
1126
- type: `update`,
1127
- data
1128
- };
1129
- const ctx = ensureContext();
1130
- const batchContext = activeBatchContexts.get(ctx);
1131
- if (batchContext?.isActive) {
1132
- batchContext.operations.push(operation);
1133
- return;
1134
- }
1135
- performWriteOperations(operation, ctx);
1136
- },
1137
- writeDelete(key) {
1138
- const operation = {
1139
- type: `delete`,
1140
- key
1141
- };
1142
- const ctx = ensureContext();
1143
- const batchContext = activeBatchContexts.get(ctx);
1144
- if (batchContext?.isActive) {
1145
- batchContext.operations.push(operation);
1146
- return;
1147
- }
1148
- performWriteOperations(operation, ctx);
1149
- },
1150
- writeUpsert(data) {
1151
- const operation = {
1152
- type: `upsert`,
1153
- data
1154
- };
1155
- const ctx = ensureContext();
1156
- const batchContext = activeBatchContexts.get(ctx);
1157
- if (batchContext?.isActive) {
1158
- batchContext.operations.push(operation);
1159
- return;
1160
- }
1161
- performWriteOperations(operation, ctx);
1162
- },
1163
- writeBatch(callback) {
1164
- const ctx = ensureContext();
1165
- const existingBatch = activeBatchContexts.get(ctx);
1166
- if (existingBatch?.isActive) {
1167
- throw new Error(
1168
- `Cannot nest writeBatch calls. Complete the current batch before starting a new one.`
1169
- );
1170
- }
1171
- const batchContext = {
1172
- operations: [],
1173
- isActive: true
1174
- };
1175
- activeBatchContexts.set(ctx, batchContext);
1176
- try {
1177
- const result = callback();
1178
- if (
1179
- // @ts-expect-error - Runtime check for async callback, callback is typed as () => void but user might pass async
1180
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
1181
- result && typeof result === `object` && `then` in result && // @ts-expect-error - Runtime check for async callback, callback is typed as () => void but user might pass async
1182
- typeof result.then === `function`
1183
- ) {
1184
- throw new Error(
1185
- `writeBatch does not support async callbacks. The callback must be synchronous.`
1186
- );
1187
- }
1188
- if (batchContext.operations.length > 0) {
1189
- performWriteOperations(batchContext.operations, ctx);
1190
- }
1191
- } finally {
1192
- batchContext.isActive = false;
1193
- activeBatchContexts.delete(ctx);
1194
- }
1195
- }
1196
- };
1197
- }
1198
-
1199
- // node_modules/@tanstack/query-db-collection/dist/esm/serialization.js
1200
- function serializeLoadSubsetOptions(options) {
1201
- if (!options) {
1202
- return void 0;
1203
- }
1204
- const result = {};
1205
- if (options.where) {
1206
- result.where = serializeExpression(options.where);
1207
- }
1208
- if (options.orderBy?.length) {
1209
- result.orderBy = options.orderBy.map((clause) => {
1210
- const baseOrderBy = {
1211
- expression: serializeExpression(clause.expression),
1212
- direction: clause.compareOptions.direction,
1213
- nulls: clause.compareOptions.nulls,
1214
- stringSort: clause.compareOptions.stringSort
1215
- };
1216
- if (clause.compareOptions.stringSort === `locale`) {
1217
- return {
1218
- ...baseOrderBy,
1219
- locale: clause.compareOptions.locale,
1220
- localeOptions: clause.compareOptions.localeOptions
1221
- };
1222
- }
1223
- return baseOrderBy;
1224
- });
1225
- }
1226
- if (options.limit !== void 0) {
1227
- result.limit = options.limit;
1228
- }
1229
- if (options.offset !== void 0) {
1230
- result.offset = options.offset;
1231
- }
1232
- return Object.keys(result).length === 0 ? void 0 : JSON.stringify(result);
1233
- }
1234
- function serializeExpression(expr) {
1235
- if (!expr) {
1236
- return null;
1237
- }
1238
- switch (expr.type) {
1239
- case `val`:
1240
- return {
1241
- type: `val`,
1242
- value: serializeValue(expr.value)
1243
- };
1244
- case `ref`:
1245
- return {
1246
- type: `ref`,
1247
- path: [...expr.path]
1248
- };
1249
- case `func`:
1250
- return {
1251
- type: `func`,
1252
- name: expr.name,
1253
- args: expr.args.map((arg) => serializeExpression(arg))
1254
- };
1255
- default:
1256
- return null;
1257
- }
1258
- }
1259
- function serializeValue(value) {
1260
- if (value === void 0) {
1261
- return { __type: `undefined` };
1262
- }
1263
- if (typeof value === `number`) {
1264
- if (Number.isNaN(value)) {
1265
- return { __type: `nan` };
1266
- }
1267
- if (value === Number.POSITIVE_INFINITY) {
1268
- return { __type: `infinity`, sign: 1 };
1269
- }
1270
- if (value === Number.NEGATIVE_INFINITY) {
1271
- return { __type: `infinity`, sign: -1 };
1272
- }
1273
- }
1274
- if (value === null || typeof value === `string` || typeof value === `number` || typeof value === `boolean`) {
1275
- return value;
1276
- }
1277
- if (value instanceof Date) {
1278
- return { __type: `date`, value: value.toJSON() };
1279
- }
1280
- if (Array.isArray(value)) {
1281
- return value.map((item) => serializeValue(item));
1282
- }
1283
- if (typeof value === `object`) {
1284
- return Object.fromEntries(
1285
- Object.entries(value).map(([key, val]) => [
1286
- key,
1287
- serializeValue(val)
1288
- ])
1289
- );
1290
- }
1291
- return value;
1292
- }
1293
-
1294
- // node_modules/@tanstack/query-db-collection/dist/esm/query.js
1295
- var QueryCollectionUtilsImpl = class {
1296
- constructor(state, refetch, writeUtils) {
1297
- this.state = state;
1298
- this.refetchFn = refetch;
1299
- this.refetch = refetch;
1300
- this.writeInsert = writeUtils.writeInsert;
1301
- this.writeUpdate = writeUtils.writeUpdate;
1302
- this.writeDelete = writeUtils.writeDelete;
1303
- this.writeUpsert = writeUtils.writeUpsert;
1304
- this.writeBatch = writeUtils.writeBatch;
1305
- }
1306
- async clearError() {
1307
- this.state.lastError = void 0;
1308
- this.state.errorCount = 0;
1309
- this.state.lastErrorUpdatedAt = 0;
1310
- await this.refetchFn({ throwOnError: true });
1311
- }
1312
- // Getters for error state
1313
- get lastError() {
1314
- return this.state.lastError;
1315
- }
1316
- get isError() {
1317
- return !!this.state.lastError;
1318
- }
1319
- get errorCount() {
1320
- return this.state.errorCount;
1321
- }
1322
- // Getters for QueryObserver state
1323
- get isFetching() {
1324
- return Array.from(this.state.observers.values()).some(
1325
- (observer) => observer.getCurrentResult().isFetching
1326
- );
1327
- }
1328
- get isRefetching() {
1329
- return Array.from(this.state.observers.values()).some(
1330
- (observer) => observer.getCurrentResult().isRefetching
1331
- );
1332
- }
1333
- get isLoading() {
1334
- return Array.from(this.state.observers.values()).some(
1335
- (observer) => observer.getCurrentResult().isLoading
1336
- );
1337
- }
1338
- get dataUpdatedAt() {
1339
- return Math.max(
1340
- 0,
1341
- ...Array.from(this.state.observers.values()).map(
1342
- (observer) => observer.getCurrentResult().dataUpdatedAt
1343
- )
1344
- );
1345
- }
1346
- get fetchStatus() {
1347
- return Array.from(this.state.observers.values()).map(
1348
- (observer) => observer.getCurrentResult().fetchStatus
1349
- );
1350
- }
1351
- };
1352
- function queryCollectionOptions(config) {
1353
- const {
1354
- queryKey,
1355
- queryFn,
1356
- select,
1357
- queryClient,
1358
- enabled,
1359
- refetchInterval,
1360
- retry,
1361
- retryDelay,
1362
- staleTime,
1363
- getKey,
1364
- onInsert,
1365
- onUpdate,
1366
- onDelete,
1367
- meta,
1368
- ...baseCollectionConfig
1369
- } = config;
1370
- const syncMode = baseCollectionConfig.syncMode ?? `eager`;
1371
- if (!queryKey) {
1372
- throw new QueryKeyRequiredError();
1373
- }
1374
- if (!queryFn) {
1375
- throw new QueryFnRequiredError();
1376
- }
1377
- if (!queryClient) {
1378
- throw new QueryClientRequiredError();
1379
- }
1380
- if (!getKey) {
1381
- throw new GetKeyRequiredError();
1382
- }
1383
- const state = {
1384
- lastError: void 0,
1385
- errorCount: 0,
1386
- lastErrorUpdatedAt: 0,
1387
- observers: /* @__PURE__ */ new Map()
1388
- };
1389
- const hashToQueryKey = /* @__PURE__ */ new Map();
1390
- const queryToRows = /* @__PURE__ */ new Map();
1391
- const rowToQueries = /* @__PURE__ */ new Map();
1392
- const unsubscribes = /* @__PURE__ */ new Map();
1393
- const queryRefCounts = /* @__PURE__ */ new Map();
1394
- const addRow = (rowKey, hashedQueryKey) => {
1395
- const rowToQueriesSet = rowToQueries.get(rowKey) || /* @__PURE__ */ new Set();
1396
- rowToQueriesSet.add(hashedQueryKey);
1397
- rowToQueries.set(rowKey, rowToQueriesSet);
1398
- const queryToRowsSet = queryToRows.get(hashedQueryKey) || /* @__PURE__ */ new Set();
1399
- queryToRowsSet.add(rowKey);
1400
- queryToRows.set(hashedQueryKey, queryToRowsSet);
1401
- };
1402
- const removeRow = (rowKey, hashedQuerKey) => {
1403
- const rowToQueriesSet = rowToQueries.get(rowKey) || /* @__PURE__ */ new Set();
1404
- rowToQueriesSet.delete(hashedQuerKey);
1405
- rowToQueries.set(rowKey, rowToQueriesSet);
1406
- const queryToRowsSet = queryToRows.get(hashedQuerKey) || /* @__PURE__ */ new Set();
1407
- queryToRowsSet.delete(rowKey);
1408
- queryToRows.set(hashedQuerKey, queryToRowsSet);
1409
- return rowToQueriesSet.size === 0;
1410
- };
1411
- const internalSync = (params) => {
1412
- const { begin, write, commit, markReady, collection } = params;
1413
- let syncStarted = false;
1414
- const generateQueryKeyFromOptions = (opts) => {
1415
- if (typeof queryKey === `function`) {
1416
- return queryKey(opts);
1417
- } else if (syncMode === `on-demand`) {
1418
- const serialized = serializeLoadSubsetOptions(opts);
1419
- return serialized !== void 0 ? [...queryKey, serialized] : queryKey;
1420
- } else {
1421
- return queryKey;
1422
- }
1423
- };
1424
- const createQueryFromOpts = (opts = {}, queryFunction = queryFn) => {
1425
- const key = generateQueryKeyFromOptions(opts);
1426
- const hashedQueryKey = hashKey(key);
1427
- const extendedMeta = { ...meta, loadSubsetOptions: opts };
1428
- if (state.observers.has(hashedQueryKey)) {
1429
- queryRefCounts.set(
1430
- hashedQueryKey,
1431
- (queryRefCounts.get(hashedQueryKey) || 0) + 1
1432
- );
1433
- const observer = state.observers.get(hashedQueryKey);
1434
- const currentResult = observer.getCurrentResult();
1435
- if (currentResult.isSuccess) {
1436
- return true;
1437
- } else if (currentResult.isError) {
1438
- return Promise.reject(currentResult.error);
1439
- } else {
1440
- return new Promise((resolve, reject) => {
1441
- const unsubscribe = observer.subscribe((result) => {
1442
- queueMicrotask(() => {
1443
- if (result.isSuccess) {
1444
- unsubscribe();
1445
- resolve();
1446
- } else if (result.isError) {
1447
- unsubscribe();
1448
- reject(result.error);
1449
- }
1450
- });
1451
- });
1452
- });
1453
- }
1454
- }
1455
- const observerOptions = {
1456
- queryKey: key,
1457
- queryFn: queryFunction,
1458
- meta: extendedMeta,
1459
- structuralSharing: true,
1460
- notifyOnChangeProps: `all`,
1461
- // Only include options that are explicitly defined to allow QueryClient defaultOptions to be used
1462
- ...enabled !== void 0 && { enabled },
1463
- ...refetchInterval !== void 0 && { refetchInterval },
1464
- ...retry !== void 0 && { retry },
1465
- ...retryDelay !== void 0 && { retryDelay },
1466
- ...staleTime !== void 0 && { staleTime }
1467
- };
1468
- const localObserver = new QueryObserver(queryClient, observerOptions);
1469
- hashToQueryKey.set(hashedQueryKey, key);
1470
- state.observers.set(hashedQueryKey, localObserver);
1471
- queryRefCounts.set(
1472
- hashedQueryKey,
1473
- (queryRefCounts.get(hashedQueryKey) || 0) + 1
1474
- );
1475
- const readyPromise = new Promise((resolve, reject) => {
1476
- const unsubscribe = localObserver.subscribe((result) => {
1477
- queueMicrotask(() => {
1478
- if (result.isSuccess) {
1479
- unsubscribe();
1480
- resolve();
1481
- } else if (result.isError) {
1482
- unsubscribe();
1483
- reject(result.error);
1484
- }
1485
- });
1486
- });
1487
- });
1488
- if (syncStarted || collection.subscriberCount > 0) {
1489
- subscribeToQuery(localObserver, hashedQueryKey);
1490
- }
1491
- return readyPromise;
1492
- };
1493
- const makeQueryResultHandler = (queryKey2) => {
1494
- const hashedQueryKey = hashKey(queryKey2);
1495
- const handleQueryResult = (result) => {
1496
- if (result.isSuccess) {
1497
- state.lastError = void 0;
1498
- state.errorCount = 0;
1499
- const rawData = result.data;
1500
- const newItemsArray = select ? select(rawData) : rawData;
1501
- if (!Array.isArray(newItemsArray) || newItemsArray.some((item) => typeof item !== `object`)) {
1502
- const errorMessage = select ? `@tanstack/query-db-collection: select() must return an array of objects. Got: ${typeof newItemsArray} for queryKey ${JSON.stringify(queryKey2)}` : `@tanstack/query-db-collection: queryFn must return an array of objects. Got: ${typeof newItemsArray} for queryKey ${JSON.stringify(queryKey2)}`;
1503
- console.error(errorMessage);
1504
- return;
1505
- }
1506
- const currentSyncedItems = new Map(
1507
- collection._state.syncedData.entries()
1508
- );
1509
- const newItemsMap = /* @__PURE__ */ new Map();
1510
- newItemsArray.forEach((item) => {
1511
- const key = getKey(item);
1512
- newItemsMap.set(key, item);
1513
- });
1514
- begin();
1515
- currentSyncedItems.forEach((oldItem, key) => {
1516
- const newItem = newItemsMap.get(key);
1517
- if (!newItem) {
1518
- const needToRemove = removeRow(key, hashedQueryKey);
1519
- if (needToRemove) {
1520
- write({ type: `delete`, value: oldItem });
1521
- }
1522
- } else if (!(0, import_db2.deepEquals)(oldItem, newItem)) {
1523
- write({ type: `update`, value: newItem });
1524
- }
1525
- });
1526
- newItemsMap.forEach((newItem, key) => {
1527
- addRow(key, hashedQueryKey);
1528
- if (!currentSyncedItems.has(key)) {
1529
- write({ type: `insert`, value: newItem });
1530
- }
1531
- });
1532
- commit();
1533
- markReady();
1534
- } else if (result.isError) {
1535
- if (result.errorUpdatedAt !== state.lastErrorUpdatedAt) {
1536
- state.lastError = result.error;
1537
- state.errorCount++;
1538
- state.lastErrorUpdatedAt = result.errorUpdatedAt;
1539
- }
1540
- console.error(
1541
- `[QueryCollection] Error observing query ${String(queryKey2)}:`,
1542
- result.error
1543
- );
1544
- markReady();
1545
- }
1546
- };
1547
- return handleQueryResult;
1548
- };
1549
- const isSubscribed = (hashedQueryKey) => {
1550
- return unsubscribes.has(hashedQueryKey);
1551
- };
1552
- const subscribeToQuery = (observer, hashedQueryKey) => {
1553
- if (!isSubscribed(hashedQueryKey)) {
1554
- const cachedQueryKey = hashToQueryKey.get(hashedQueryKey);
1555
- const handleQueryResult = makeQueryResultHandler(cachedQueryKey);
1556
- const unsubscribeFn = observer.subscribe(handleQueryResult);
1557
- unsubscribes.set(hashedQueryKey, unsubscribeFn);
1558
- const currentResult = observer.getCurrentResult();
1559
- if (currentResult.isSuccess || currentResult.isError) {
1560
- handleQueryResult(currentResult);
1561
- }
1562
- }
1563
- };
1564
- const subscribeToQueries = () => {
1565
- state.observers.forEach(subscribeToQuery);
1566
- };
1567
- const unsubscribeFromQueries = () => {
1568
- unsubscribes.forEach((unsubscribeFn) => {
1569
- unsubscribeFn();
1570
- });
1571
- unsubscribes.clear();
1572
- };
1573
- syncStarted = true;
1574
- const unsubscribeFromCollectionEvents = collection.on(
1575
- `subscribers:change`,
1576
- ({ subscriberCount }) => {
1577
- if (subscriberCount > 0) {
1578
- subscribeToQueries();
1579
- } else if (subscriberCount === 0) {
1580
- unsubscribeFromQueries();
1581
- }
1582
- }
1583
- );
1584
- if (syncMode === `eager`) {
1585
- const initialResult = createQueryFromOpts({});
1586
- if (initialResult instanceof Promise) {
1587
- initialResult.catch(() => {
1588
- });
1589
- }
1590
- } else {
1591
- markReady();
1592
- }
1593
- subscribeToQueries();
1594
- state.observers.forEach((observer, hashedQueryKey) => {
1595
- const cachedQueryKey = hashToQueryKey.get(hashedQueryKey);
1596
- const handleQueryResult = makeQueryResultHandler(cachedQueryKey);
1597
- handleQueryResult(observer.getCurrentResult());
1598
- });
1599
- const cleanupQueryInternal = (hashedQueryKey) => {
1600
- unsubscribes.get(hashedQueryKey)?.();
1601
- unsubscribes.delete(hashedQueryKey);
1602
- const rowKeys = queryToRows.get(hashedQueryKey) ?? /* @__PURE__ */ new Set();
1603
- const rowsToDelete = [];
1604
- rowKeys.forEach((rowKey) => {
1605
- const queries = rowToQueries.get(rowKey);
1606
- if (!queries) {
1607
- return;
1608
- }
1609
- queries.delete(hashedQueryKey);
1610
- if (queries.size === 0) {
1611
- rowToQueries.delete(rowKey);
1612
- if (collection.has(rowKey)) {
1613
- rowsToDelete.push(collection.get(rowKey));
1614
- }
1615
- }
1616
- });
1617
- if (rowsToDelete.length > 0) {
1618
- begin();
1619
- rowsToDelete.forEach((row) => {
1620
- write({ type: `delete`, value: row });
1621
- });
1622
- commit();
1623
- }
1624
- state.observers.delete(hashedQueryKey);
1625
- queryToRows.delete(hashedQueryKey);
1626
- hashToQueryKey.delete(hashedQueryKey);
1627
- queryRefCounts.delete(hashedQueryKey);
1628
- };
1629
- const cleanupQueryIfIdle = (hashedQueryKey) => {
1630
- const refcount = queryRefCounts.get(hashedQueryKey) || 0;
1631
- const observer = state.observers.get(hashedQueryKey);
1632
- if (refcount <= 0) {
1633
- unsubscribes.get(hashedQueryKey)?.();
1634
- unsubscribes.delete(hashedQueryKey);
1635
- }
1636
- const hasListeners = observer?.hasListeners() ?? false;
1637
- if (hasListeners) {
1638
- queryRefCounts.set(hashedQueryKey, 0);
1639
- return;
1640
- }
1641
- if (refcount > 0) {
1642
- console.warn(
1643
- `[cleanupQueryIfIdle] Invariant violation: refcount=${refcount} but no listeners. Cleaning up to prevent leak.`,
1644
- { hashedQueryKey }
1645
- );
1646
- }
1647
- cleanupQueryInternal(hashedQueryKey);
1648
- };
1649
- const forceCleanupQuery = (hashedQueryKey) => {
1650
- cleanupQueryInternal(hashedQueryKey);
1651
- };
1652
- const unsubscribeQueryCache = queryClient.getQueryCache().subscribe((event) => {
1653
- const hashedKey = event.query.queryHash;
1654
- if (event.type === `removed`) {
1655
- if (hashToQueryKey.has(hashedKey)) {
1656
- cleanupQueryIfIdle(hashedKey);
1657
- }
1658
- }
1659
- });
1660
- const cleanup = async () => {
1661
- unsubscribeFromCollectionEvents();
1662
- unsubscribeFromQueries();
1663
- const allQueryKeys = [...hashToQueryKey.values()];
1664
- const allHashedKeys = [...state.observers.keys()];
1665
- for (const hashedKey of allHashedKeys) {
1666
- forceCleanupQuery(hashedKey);
1667
- }
1668
- unsubscribeQueryCache();
1669
- await Promise.all(
1670
- allQueryKeys.map(async (qKey) => {
1671
- await queryClient.cancelQueries({ queryKey: qKey, exact: true });
1672
- queryClient.removeQueries({ queryKey: qKey, exact: true });
1673
- })
1674
- );
1675
- };
1676
- const unloadSubset = (options) => {
1677
- const key = generateQueryKeyFromOptions(options);
1678
- const hashedQueryKey = hashKey(key);
1679
- const currentCount = queryRefCounts.get(hashedQueryKey) || 0;
1680
- const newCount = currentCount - 1;
1681
- if (newCount <= 0) {
1682
- queryRefCounts.set(hashedQueryKey, 0);
1683
- cleanupQueryIfIdle(hashedQueryKey);
1684
- } else {
1685
- queryRefCounts.set(hashedQueryKey, newCount);
1686
- }
1687
- };
1688
- const loadSubsetDedupe = syncMode === `eager` ? void 0 : createQueryFromOpts;
1689
- return {
1690
- loadSubset: loadSubsetDedupe,
1691
- unloadSubset: syncMode === `eager` ? void 0 : unloadSubset,
1692
- cleanup
1693
- };
1694
- };
1695
- const refetch = async (opts) => {
1696
- const allQueryKeys = [...hashToQueryKey.values()];
1697
- const refetchPromises = allQueryKeys.map((qKey) => {
1698
- const queryObserver = state.observers.get(hashKey(qKey));
1699
- return queryObserver.refetch({
1700
- throwOnError: opts?.throwOnError
1701
- });
1702
- });
1703
- await Promise.all(refetchPromises);
1704
- };
1705
- const updateCacheData = (items) => {
1706
- const key = typeof queryKey === `function` ? queryKey({}) : queryKey;
1707
- if (select) {
1708
- queryClient.setQueryData(key, (oldData) => {
1709
- if (!oldData || typeof oldData !== `object`) {
1710
- return oldData;
1711
- }
1712
- if (Array.isArray(oldData)) {
1713
- return items;
1714
- }
1715
- const selectedArray = select(oldData);
1716
- if (Array.isArray(selectedArray)) {
1717
- for (const propKey of Object.keys(oldData)) {
1718
- if (oldData[propKey] === selectedArray) {
1719
- return { ...oldData, [propKey]: items };
1720
- }
1721
- }
1722
- }
1723
- if (Array.isArray(oldData.data)) {
1724
- return { ...oldData, data: items };
1725
- }
1726
- if (Array.isArray(oldData.items)) {
1727
- return { ...oldData, items };
1728
- }
1729
- if (Array.isArray(oldData.results)) {
1730
- return { ...oldData, results: items };
1731
- }
1732
- for (const propKey of Object.keys(oldData)) {
1733
- if (Array.isArray(oldData[propKey])) {
1734
- return { ...oldData, [propKey]: items };
1735
- }
1736
- }
1737
- return oldData;
1738
- });
1739
- } else {
1740
- queryClient.setQueryData(key, items);
1741
- }
1742
- };
1743
- let writeContext = null;
1744
- const enhancedInternalSync = (params) => {
1745
- const { begin, write, commit, collection } = params;
1746
- const contextQueryKey = typeof queryKey === `function` ? queryKey({}) : queryKey;
1747
- writeContext = {
1748
- collection,
1749
- queryClient,
1750
- queryKey: contextQueryKey,
1751
- getKey,
1752
- begin,
1753
- write,
1754
- commit,
1755
- updateCacheData
1756
- };
1757
- return internalSync(params);
1758
- };
1759
- const writeUtils = createWriteUtils(
1760
- () => writeContext
1761
- );
1762
- const wrappedOnInsert = onInsert ? async (params) => {
1763
- const handlerResult = await onInsert(params) ?? {};
1764
- const shouldRefetch = handlerResult.refetch !== false;
1765
- if (shouldRefetch) {
1766
- await refetch();
1767
- }
1768
- return handlerResult;
1769
- } : void 0;
1770
- const wrappedOnUpdate = onUpdate ? async (params) => {
1771
- const handlerResult = await onUpdate(params) ?? {};
1772
- const shouldRefetch = handlerResult.refetch !== false;
1773
- if (shouldRefetch) {
1774
- await refetch();
1775
- }
1776
- return handlerResult;
1777
- } : void 0;
1778
- const wrappedOnDelete = onDelete ? async (params) => {
1779
- const handlerResult = await onDelete(params) ?? {};
1780
- const shouldRefetch = handlerResult.refetch !== false;
1781
- if (shouldRefetch) {
1782
- await refetch();
1783
- }
1784
- return handlerResult;
1785
- } : void 0;
1786
- const utils = new QueryCollectionUtilsImpl(state, refetch, writeUtils);
1787
- return {
1788
- ...baseCollectionConfig,
1789
- getKey,
1790
- syncMode,
1791
- sync: { sync: enhancedInternalSync },
1792
- onInsert: wrappedOnInsert,
1793
- onUpdate: wrappedOnUpdate,
1794
- onDelete: wrappedOnDelete,
1795
- utils
1796
- };
1797
- }
1
+ 'use strict';
1798
2
 
1799
- // node_modules/@tanstack/query-db-collection/dist/esm/index.js
1800
- var import_db3 = require("@tanstack/db");
3
+ var queryDbCollection = require('@tanstack/query-db-collection');
4
+ var loroCrdt = require('loro-crdt');
5
+ var surrealdb = require('surrealdb');
1801
6
 
1802
7
  // src/index.ts
1803
- var import_loro_crdt = require("loro-crdt");
1804
- var import_surrealdb2 = require("surrealdb");
1805
-
1806
- // src/table.ts
1807
- var import_surrealdb = require("surrealdb");
1808
8
  var normalizeFields = (raw) => {
1809
9
  if (!raw || raw === "*") return ["*"];
1810
10
  return raw;
@@ -1817,8 +17,8 @@ function manageTable(db, useLoro, { name, ...args }) {
1817
17
  const fields = normalizeFields(args.fields);
1818
18
  const baseWhere = () => {
1819
19
  if (!useLoro) return args.where;
1820
- const alive = (0, import_surrealdb.eq)("sync_deleted", false);
1821
- return args.where ? (0, import_surrealdb.and)(args.where, alive) : alive;
20
+ const alive = surrealdb.eq("sync_deleted", false);
21
+ return args.where ? surrealdb.and(args.where, alive) : alive;
1822
22
  };
1823
23
  const listAll = async () => {
1824
24
  const where = baseWhere();
@@ -1833,7 +33,7 @@ function manageTable(db, useLoro, { name, ...args }) {
1833
33
  const loadSubset = async (subset) => {
1834
34
  const b = baseWhere();
1835
35
  const w = subset?.where;
1836
- const where = b && w ? (0, import_surrealdb.and)(b, w) : b ?? w;
36
+ const where = b && w ? surrealdb.and(b, w) : b ?? w;
1837
37
  const whereSql = where ? " WHERE $where" : "";
1838
38
  const order = joinOrderBy(subset?.orderBy);
1839
39
  const orderSql = order ? ` ORDER BY ${order}` : "";
@@ -1849,7 +49,7 @@ function manageTable(db, useLoro, { name, ...args }) {
1849
49
  return res ?? [];
1850
50
  };
1851
51
  const create = async (data) => {
1852
- await db.create(new import_surrealdb.Table(name)).content(data);
52
+ await db.create(new surrealdb.Table(name)).content(data);
1853
53
  };
1854
54
  const update = async (id, data) => {
1855
55
  if (!useLoro) {
@@ -1887,8 +87,8 @@ function manageTable(db, useLoro, { name, ...args }) {
1887
87
  cb({ type: "delete", row: { id: value.id } });
1888
88
  };
1889
89
  const start = async () => {
1890
- if (!db.isFeatureSupported(import_surrealdb.Features.LiveQueries)) return;
1891
- live = await db.live(new import_surrealdb.Table(name)).where(args.where);
90
+ if (!db.isFeatureSupported(surrealdb.Features.LiveQueries)) return;
91
+ live = await db.live(new surrealdb.Table(name)).where(args.where);
1892
92
  live.subscribe(on);
1893
93
  };
1894
94
  void start();
@@ -1939,7 +139,7 @@ function surrealCollectionOptions({
1939
139
  ...config
1940
140
  }) {
1941
141
  let loro;
1942
- if (useLoro) loro = { doc: new import_loro_crdt.LoroDoc(), key: id };
142
+ if (useLoro) loro = { doc: new loroCrdt.LoroDoc(), key: id };
1943
143
  const table = manageTable(db, useLoro, config.table);
1944
144
  const keyOf = (rid) => typeof rid === "string" ? rid : rid.toString();
1945
145
  const getKey = (row) => keyOf(row.id);
@@ -1975,7 +175,7 @@ function surrealCollectionOptions({
1975
175
  }
1976
176
  return out;
1977
177
  };
1978
- const base = queryCollectionOptions({
178
+ const base = queryDbCollection.queryCollectionOptions({
1979
179
  getKey: (row) => getKey(row),
1980
180
  queryKey,
1981
181
  queryClient,
@@ -2017,7 +217,7 @@ function surrealCollectionOptions({
2017
217
  const row = useLoro ? { ...baseRow, updated_at: now() } : baseRow;
2018
218
  if (useLoro) loroPut(row);
2019
219
  await table.update(
2020
- new import_surrealdb2.RecordId(config.table.name, keyOf(idKey)),
220
+ new surrealdb.RecordId(config.table.name, keyOf(idKey)),
2021
221
  row
2022
222
  );
2023
223
  resultRows.push(row);
@@ -2030,7 +230,7 @@ function surrealCollectionOptions({
2030
230
  const idKey = m.key;
2031
231
  if (useLoro) loroRemove(keyOf(idKey));
2032
232
  await table.softDelete(
2033
- new import_surrealdb2.RecordId(config.table.name, keyOf(idKey))
233
+ new surrealdb.RecordId(config.table.name, keyOf(idKey))
2034
234
  );
2035
235
  }
2036
236
  return [];
@@ -2040,7 +240,7 @@ function surrealCollectionOptions({
2040
240
  const sync = baseSync ? {
2041
241
  sync: (ctx) => {
2042
242
  const offBase = baseSync(ctx);
2043
- if (!db.isFeatureSupported(import_surrealdb2.Features.LiveQueries))
243
+ if (!db.isFeatureSupported(surrealdb.Features.LiveQueries))
2044
244
  return offBase;
2045
245
  const offLive = table.subscribe((evt) => {
2046
246
  if (useLoro) {
@@ -2062,7 +262,7 @@ function surrealCollectionOptions({
2062
262
  sync: sync ?? base.sync
2063
263
  };
2064
264
  }
2065
- // Annotate the CommonJS export names for ESM import in node:
2066
- 0 && (module.exports = {
2067
- surrealCollectionOptions
2068
- });
265
+
266
+ exports.surrealCollectionOptions = surrealCollectionOptions;
267
+ //# sourceMappingURL=index.js.map
268
+ //# sourceMappingURL=index.js.map