@signaldb/core 2.0.0-beta.2 → 2.0.0-beta.21

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 (145) hide show
  1. package/dist/.vite/manifest.json +183 -80
  2. package/dist/AsyncDataAdapter.d.ts +53 -7
  3. package/dist/AutoFetchDataAdapter.d.ts +4 -6
  4. package/dist/Collection/Cursor.d.ts +56 -4
  5. package/dist/Collection/Observer.d.ts +33 -0
  6. package/dist/Collection/index.d.ts +66 -19
  7. package/dist/Collection/types.d.ts +9 -2
  8. package/dist/DataAdapter.d.ts +24 -6
  9. package/dist/DefaultDataAdapter.d.ts +5 -5
  10. package/dist/WorkerDataAdapter.d.ts +57 -5
  11. package/dist/WorkerDataAdapterHost.d.ts +9 -7
  12. package/dist/createIndex.d.ts +2 -2
  13. package/dist/createIndexProvider.d.ts +2 -2
  14. package/dist/createReactivityAdapter.d.ts +2 -2
  15. package/dist/createStorageAdapter.d.ts +1 -1
  16. package/dist/getIndexInfo.d.ts +3 -4
  17. package/dist/index.cjs.js +33 -31
  18. package/dist/index.d.ts +6 -1
  19. package/dist/index.mjs +17 -32
  20. package/dist/index10.cjs.js +11 -0
  21. package/dist/index10.mjs +10 -18
  22. package/dist/index11.cjs.js +23 -0
  23. package/dist/index11.mjs +22 -15
  24. package/dist/index12.cjs.js +45 -0
  25. package/dist/index12.mjs +44 -353
  26. package/dist/index13.cjs.js +22 -0
  27. package/dist/index13.mjs +21 -419
  28. package/dist/index14.cjs.js +45 -0
  29. package/dist/index14.mjs +44 -206
  30. package/dist/index15.cjs.js +86 -0
  31. package/dist/index15.mjs +85 -388
  32. package/dist/index16.cjs.js +15 -0
  33. package/dist/index16.mjs +14 -579
  34. package/dist/index17.cjs.js +141 -0
  35. package/dist/index17.mjs +140 -152
  36. package/dist/index18.cjs.js +47 -0
  37. package/dist/index18.mjs +46 -26
  38. package/dist/index19.cjs.js +15 -0
  39. package/dist/index19.mjs +14 -41
  40. package/dist/index2.cjs.js +33 -0
  41. package/dist/index2.mjs +32 -258
  42. package/dist/index20.cjs.js +23 -0
  43. package/dist/index20.mjs +22 -16
  44. package/dist/index21.cjs.js +37 -0
  45. package/dist/index21.mjs +36 -104
  46. package/dist/index22.cjs.js +35 -0
  47. package/dist/index22.mjs +34 -128
  48. package/dist/index23.cjs.js +21 -0
  49. package/dist/index23.mjs +20 -7
  50. package/dist/index24.cjs.js +19 -0
  51. package/dist/index24.mjs +18 -27
  52. package/dist/index25.cjs.js +157 -0
  53. package/dist/index25.mjs +156 -11
  54. package/dist/index26.cjs.js +38 -0
  55. package/dist/index26.mjs +37 -29
  56. package/dist/index27.cjs.js +42 -0
  57. package/dist/index27.mjs +41 -46
  58. package/dist/index28.cjs.js +347 -0
  59. package/dist/index28.mjs +345 -7
  60. package/dist/index29.cjs.js +602 -0
  61. package/dist/index29.mjs +601 -7
  62. package/dist/index3.cjs.js +19 -0
  63. package/dist/index3.mjs +18 -556
  64. package/dist/index30.cjs.js +11 -0
  65. package/dist/index30.mjs +10 -42
  66. package/dist/index31.cjs.js +11 -0
  67. package/dist/index31.mjs +10 -29
  68. package/dist/index32.cjs.js +90 -0
  69. package/dist/index32.mjs +89 -10
  70. package/dist/index33.cjs.js +53 -0
  71. package/dist/index33.mjs +52 -5
  72. package/dist/index34.cjs.js +453 -0
  73. package/dist/index34.mjs +453 -0
  74. package/dist/index35.cjs.js +72 -0
  75. package/dist/index35.mjs +72 -0
  76. package/dist/index36.cjs.js +580 -0
  77. package/dist/index36.mjs +580 -0
  78. package/dist/index37.cjs.js +21 -0
  79. package/dist/index37.mjs +21 -0
  80. package/dist/index38.cjs.js +363 -0
  81. package/dist/index38.mjs +363 -0
  82. package/dist/index39.cjs.js +513 -0
  83. package/dist/index39.mjs +513 -0
  84. package/dist/index4.cjs.js +209 -0
  85. package/dist/index4.mjs +204 -5
  86. package/dist/index5.cjs.js +187 -0
  87. package/dist/index5.mjs +186 -5
  88. package/dist/index6.cjs.js +303 -0
  89. package/dist/index6.mjs +301 -28
  90. package/dist/index7.cjs.js +125 -0
  91. package/dist/index7.mjs +124 -12
  92. package/dist/index8.cjs.js +33 -0
  93. package/dist/index8.mjs +32 -5
  94. package/dist/index9.cjs.js +12 -0
  95. package/dist/index9.mjs +11 -133
  96. package/dist/types/IndexProvider.d.ts +2 -2
  97. package/dist/types/Modifier.d.ts +1 -1
  98. package/dist/types/ReactivityAdapter.d.ts +1 -1
  99. package/dist/types/StorageAdapter.d.ts +9 -1
  100. package/dist/utils/applyQueryOptions.d.ts +15 -0
  101. package/dist/utils/createSignal.d.ts +2 -2
  102. package/dist/utils/getMatchingKeys.d.ts +2 -2
  103. package/dist/utils/idIndexQuery.d.ts +20 -0
  104. package/dist/utils/incrementalQueryUpdate.d.ts +62 -0
  105. package/dist/utils/isFieldExpression.d.ts +1 -1
  106. package/dist/utils/match.d.ts +1 -1
  107. package/dist/utils/modify.d.ts +1 -1
  108. package/dist/utils/projectItems.d.ts +12 -0
  109. package/dist/utils/queryDelta.d.ts +83 -0
  110. package/dist/utils/queryId.d.ts +2 -2
  111. package/dist/utils/reactiveOrAsync.d.ts +59 -0
  112. package/dist/utils/storageIndexQuery.d.ts +20 -0
  113. package/package.json +1 -1
  114. package/dist/index.cjs10.js +0 -18
  115. package/dist/index.cjs11.js +0 -15
  116. package/dist/index.cjs12.js +0 -353
  117. package/dist/index.cjs13.js +0 -419
  118. package/dist/index.cjs14.js +0 -206
  119. package/dist/index.cjs15.js +0 -388
  120. package/dist/index.cjs16.js +0 -579
  121. package/dist/index.cjs17.js +0 -152
  122. package/dist/index.cjs18.js +0 -26
  123. package/dist/index.cjs19.js +0 -42
  124. package/dist/index.cjs2.js +0 -259
  125. package/dist/index.cjs20.js +0 -16
  126. package/dist/index.cjs21.js +0 -104
  127. package/dist/index.cjs22.js +0 -129
  128. package/dist/index.cjs23.js +0 -7
  129. package/dist/index.cjs24.js +0 -27
  130. package/dist/index.cjs25.js +0 -10
  131. package/dist/index.cjs26.js +0 -29
  132. package/dist/index.cjs27.js +0 -46
  133. package/dist/index.cjs28.js +0 -8
  134. package/dist/index.cjs29.js +0 -7
  135. package/dist/index.cjs3.js +0 -557
  136. package/dist/index.cjs30.js +0 -42
  137. package/dist/index.cjs31.js +0 -29
  138. package/dist/index.cjs32.js +0 -9
  139. package/dist/index.cjs33.js +0 -5
  140. package/dist/index.cjs4.js +0 -5
  141. package/dist/index.cjs5.js +0 -5
  142. package/dist/index.cjs6.js +0 -29
  143. package/dist/index.cjs7.js +0 -12
  144. package/dist/index.cjs8.js +0 -5
  145. package/dist/index.cjs9.js +0 -133
@@ -1,579 +0,0 @@
1
- "use strict";
2
- const deepClone = require("./index.cjs19.js");
3
- const match = require("./index.cjs23.js");
4
- const modify = require("./index.cjs7.js");
5
- const queryId = require("./index.cjs20.js");
6
- const isEqual = require("./index.cjs6.js");
7
- const getIndexInfo = require("./index.cjs22.js");
8
- const getMatchingKeys = require("./index.cjs26.js");
9
- const sortItems = require("./index.cjs25.js");
10
- const project = require("./index.cjs24.js");
11
- function defaultMergeItems(a, b) {
12
- return { ...a, ...b };
13
- }
14
- function selectorId(selector) {
15
- return JSON.stringify(selector ?? {});
16
- }
17
- class AutoFetchDataAdapter {
18
- options;
19
- id;
20
- onError;
21
- fetchQueryItems;
22
- mergeItems;
23
- purgeDelay;
24
- // Per-collection resources
25
- storageAdapters = /* @__PURE__ */ new Map();
26
- storageAdapterReady = /* @__PURE__ */ new Map();
27
- collectionIndices = /* @__PURE__ */ new Map();
28
- // Per-collection query registries and auto-fetch bookkeeping
29
- queries = /* @__PURE__ */ new Map();
30
- activeObservers = /* @__PURE__ */ new Map();
31
- // per-collection: key->record
32
- observerTimeouts = /* @__PURE__ */ new Map();
33
- // per-collection
34
- selectorIds = /* @__PURE__ */ new Map();
35
- // per-collection: selectorKey -> Set<id>
36
- idRefCounts = /* @__PURE__ */ new Map();
37
- // per-collection: id -> refcount (for auto-fetched items)
38
- autoloadIds = /* @__PURE__ */ new Map();
39
- // per-collection: ids that were introduced via auto-fetch
40
- constructor(options) {
41
- this.options = options;
42
- this.id = options.id || "autofetch-data-adapter";
43
- this.onError = options.onError ?? ((error) => {
44
- console.error(error);
45
- });
46
- this.fetchQueryItems = options.fetchQueryItems;
47
- this.mergeItems = options.mergeItems ?? defaultMergeItems;
48
- this.purgeDelay = options.purgeDelay ?? 1e4;
49
- if (options.registerRemoteChange) {
50
- void options.registerRemoteChange(async () => {
51
- await this.forceRefetchAll();
52
- });
53
- }
54
- }
55
- createCollectionBackend(collection, indices) {
56
- this.collectionIndices.set(collection.name, indices);
57
- this.queries.set(collection.name, /* @__PURE__ */ new Map());
58
- this.ensureStorageAdapter(collection.name);
59
- this.activeObservers.set(collection.name, /* @__PURE__ */ new Map());
60
- this.observerTimeouts.set(collection.name, /* @__PURE__ */ new Map());
61
- this.selectorIds.set(collection.name, /* @__PURE__ */ new Map());
62
- this.idRefCounts.set(collection.name, /* @__PURE__ */ new Map());
63
- this.autoloadIds.set(collection.name, /* @__PURE__ */ new Set());
64
- const ready = this.setupStorage(collection.name, indices);
65
- this.storageAdapterReady.set(collection.name, ready);
66
- const registerQuery = (selector, options) => {
67
- const qid = queryId(selector, options);
68
- const registry = this.queries.get(collection.name);
69
- if (!registry)
70
- throw new Error(`Collection ${collection.name} not initialized!`);
71
- registry.set(qid, {
72
- selector,
73
- options,
74
- items: [],
75
- listeners: /* @__PURE__ */ new Set(),
76
- ...registry.get(qid),
77
- state: "active",
78
- error: null
79
- });
80
- const key = selectorId(selector);
81
- const perColObservers = this.activeObservers.get(collection.name);
82
- const current = perColObservers?.get(key)?.count ?? 0;
83
- perColObservers?.set(key, { selector, count: current + 1 });
84
- const timeouts = this.observerTimeouts.get(collection.name);
85
- const t = timeouts?.get(key);
86
- if (t)
87
- clearTimeout(t);
88
- if (current === 0) {
89
- void this.fetchAndIngest(collection.name, selector).catch(this.onError);
90
- }
91
- void this.fulfillQuery(collection.name, selector, options).catch(this.onError);
92
- };
93
- const unregisterQuery = (selector, options) => {
94
- const qid = queryId(selector, options);
95
- const registry = this.queries.get(collection.name);
96
- registry?.delete(qid);
97
- const key = selectorId(selector);
98
- const perColObservers = this.activeObservers.get(collection.name);
99
- const current = perColObservers?.get(key)?.count ?? 0;
100
- const remaining = Math.max(0, current - 1);
101
- if (remaining > 0) {
102
- perColObservers?.set(key, { selector, count: remaining });
103
- return;
104
- }
105
- const doPurge = () => {
106
- perColObservers?.delete(key);
107
- void this.purgeSelector(collection.name, selector).catch(this.onError);
108
- };
109
- if (this.purgeDelay === 0) {
110
- doPurge();
111
- } else {
112
- const timeouts = this.observerTimeouts.get(collection.name);
113
- const t = timeouts?.get(key);
114
- if (t)
115
- clearTimeout(t);
116
- timeouts?.set(key, setTimeout(doPurge, this.purgeDelay));
117
- }
118
- };
119
- const getQueryState = (selector, options) => {
120
- const q = this.queries.get(collection.name)?.get(queryId(selector, options));
121
- return q?.state ?? "active";
122
- };
123
- const getQueryError = (selector, options) => {
124
- const q = this.queries.get(collection.name)?.get(queryId(selector, options));
125
- return q?.error ?? null;
126
- };
127
- const getQueryResult = (selector, options) => {
128
- const q = this.queries.get(collection.name)?.get(queryId(selector, options));
129
- return q?.items ?? [];
130
- };
131
- const onQueryStateChange = (selector, options, callback) => {
132
- const qid = queryId(selector, options);
133
- const registry = this.queries.get(collection.name);
134
- if (!registry)
135
- throw new Error(`Collection ${collection.name} not initialized!`);
136
- if (!registry.has(qid)) {
137
- registry.set(qid, {
138
- selector,
139
- options,
140
- state: "active",
141
- error: null,
142
- items: [],
143
- listeners: /* @__PURE__ */ new Set()
144
- });
145
- }
146
- registry.get(qid)?.listeners.add(callback);
147
- return () => {
148
- registry.get(qid)?.listeners.delete(callback);
149
- };
150
- };
151
- return {
152
- insert: async (item) => {
153
- await ready;
154
- const inserted = await this.insert(collection.name, item);
155
- return inserted;
156
- },
157
- updateOne: async (selector, modifier) => {
158
- await ready;
159
- return this.updateOne(collection.name, selector, modifier);
160
- },
161
- updateMany: async (selector, modifier) => {
162
- await ready;
163
- return this.updateMany(collection.name, selector, modifier);
164
- },
165
- replaceOne: async (selector, replacement) => {
166
- await ready;
167
- return this.replaceOne(collection.name, selector, replacement);
168
- },
169
- removeOne: async (selector) => {
170
- await ready;
171
- return this.removeOne(collection.name, selector);
172
- },
173
- removeMany: async (selector) => {
174
- await ready;
175
- return this.removeMany(collection.name, selector);
176
- },
177
- registerQuery,
178
- unregisterQuery,
179
- getQueryState,
180
- getQueryError,
181
- getQueryResult,
182
- onQueryStateChange,
183
- executeQuery: async (selector, options) => {
184
- await ready;
185
- registerQuery(selector, options);
186
- await new Promise((resolve) => {
187
- let stop = () => {
188
- };
189
- stop = onQueryStateChange(selector, options, (state) => {
190
- if (state === "active")
191
- return;
192
- resolve();
193
- stop();
194
- });
195
- });
196
- const result = getQueryResult(selector, options);
197
- unregisterQuery(selector, options);
198
- return result;
199
- },
200
- dispose: async () => {
201
- this.storageAdapters.delete(collection.name);
202
- this.queries.delete(collection.name);
203
- this.collectionIndices.delete(collection.name);
204
- this.storageAdapterReady.delete(collection.name);
205
- this.activeObservers.delete(collection.name);
206
- this.observerTimeouts.delete(collection.name);
207
- this.selectorIds.delete(collection.name);
208
- this.idRefCounts.delete(collection.name);
209
- this.autoloadIds.delete(collection.name);
210
- },
211
- isReady: async () => {
212
- await ready;
213
- }
214
- };
215
- }
216
- // ===== Auto-fetch mechanics =====
217
- async forceRefetchAll() {
218
- const tasks = [];
219
- for (const [collectionName, observers] of this.activeObservers.entries()) {
220
- for (const { selector, count } of observers.values()) {
221
- if (count > 0)
222
- tasks.push(this.fetchAndIngest(collectionName, selector));
223
- }
224
- }
225
- await Promise.all(tasks);
226
- }
227
- async fetchAndIngest(collectionName, selector) {
228
- this.publishForSelector(collectionName, selector, "active", null);
229
- try {
230
- const items = await this.fetchQueryItems(collectionName, selector);
231
- if (!items || !Array.isArray(items)) {
232
- throw new Error("AutoFetchDataAdapter: fetchQueryItems must resolve to { items: T[] }");
233
- }
234
- const ids = items.map((i) => i.id);
235
- const selectorKey = selectorId(selector);
236
- const selMap = this.selectorIds.get(collectionName);
237
- const previous = selMap?.get(selectorKey) ?? /* @__PURE__ */ new Set();
238
- ids.forEach((id) => previous.add(id));
239
- selMap?.set(selectorKey, previous);
240
- await this.upsertMerged(collectionName, items);
241
- await this.checkQueryUpdates(collectionName, items);
242
- } catch (error) {
243
- this.publishForSelector(collectionName, selector, "error", error);
244
- this.onError(error);
245
- }
246
- }
247
- async purgeSelector(collectionName, selector) {
248
- const selectorKey = selectorId(selector);
249
- const selMap = this.selectorIds.get(collectionName);
250
- const ids = selMap?.get(selectorKey);
251
- selMap?.delete(selectorKey);
252
- if (!ids || ids.size === 0)
253
- return;
254
- const referenceMap = this.idRefCounts.get(collectionName);
255
- const autoload = this.autoloadIds.get(collectionName);
256
- const toRemove = [];
257
- for (const id of ids) {
258
- const current = referenceMap?.get(id) ?? 0;
259
- const next = Math.max(0, current - 1);
260
- if (next === 0) {
261
- referenceMap?.delete(id);
262
- if (autoload?.has(id))
263
- toRemove.push(id);
264
- } else {
265
- referenceMap?.set(id, next);
266
- }
267
- }
268
- if (toRemove.length === 0)
269
- return;
270
- const storage = this.storageAdapters.get(collectionName);
271
- if (!storage)
272
- throw new Error(`No persistence adapter for collection ${collectionName}`);
273
- const items = await storage.readIds(toRemove);
274
- if (items.length > 0) {
275
- await storage.remove(items);
276
- await this.checkQueryUpdates(collectionName, items);
277
- }
278
- toRemove.forEach((id) => autoload?.delete(id));
279
- }
280
- // ===== Core query + CRUD plumbing (index-aware, same semantics as AsyncDataAdapter) =====
281
- async setupStorage(collectionName, indices) {
282
- const storage = this.storageAdapters.get(collectionName);
283
- if (!storage)
284
- throw new Error(`No persistence adapter for collection ${collectionName}`);
285
- await Promise.all(indices.map((field) => storage.createIndex(field)));
286
- await storage.setup();
287
- }
288
- ensureStorageAdapter(name) {
289
- if (this.storageAdapters.has(name))
290
- return;
291
- const adapter = this.options.storage && this.options.storage(name);
292
- if (!adapter)
293
- return;
294
- this.storageAdapters.set(name, adapter);
295
- }
296
- publishForSelector(collectionName, selector, state, error) {
297
- const registry = this.queries.get(collectionName);
298
- if (!registry)
299
- return;
300
- for (const query of registry.values()) {
301
- if (!isEqual(query.selector, selector))
302
- continue;
303
- this.publishState(collectionName, queryId(query.selector, query.options), state, error);
304
- }
305
- }
306
- publishState(collectionName, qid, state, error) {
307
- const query = this.queries.get(collectionName)?.get(qid);
308
- if (!query)
309
- return;
310
- query.state = state;
311
- query.error = error;
312
- for (const callback of query.listeners) {
313
- try {
314
- callback(state);
315
- } catch (error_) {
316
- this.onError(error_);
317
- }
318
- }
319
- }
320
- publishResult(collectionName, qid, items) {
321
- const query = this.queries.get(collectionName)?.get(qid);
322
- if (!query)
323
- return;
324
- query.items = items;
325
- this.queries.get(collectionName)?.set(qid, query);
326
- }
327
- async fulfillQuery(collectionName, selector, options) {
328
- const qid = queryId(selector, options);
329
- const registry = this.queries.get(collectionName);
330
- if (!registry)
331
- throw new Error(`Collection ${collectionName} not initialized!`);
332
- const rec = registry.get(qid);
333
- if (!rec)
334
- return;
335
- this.publishState(collectionName, qid, "active", null);
336
- try {
337
- const items = await this.executeQuery(collectionName, selector, options);
338
- this.publishResult(collectionName, qid, items);
339
- this.publishState(collectionName, qid, "complete", null);
340
- } catch (error) {
341
- this.publishState(collectionName, qid, "error", error);
342
- }
343
- }
344
- async getIndexInfo(collectionName, selector) {
345
- const storage = this.storageAdapters.get(collectionName);
346
- if (!storage)
347
- throw new Error(`No persistence adapter for collection ${collectionName}`);
348
- if (selector != null && Object.keys(selector).length === 1 && "id" in selector && typeof selector.id !== "object") {
349
- return { matched: true, ids: [selector.id], optimizedSelector: {} };
350
- }
351
- if (selector == null) {
352
- return { matched: false, ids: [], optimizedSelector: {} };
353
- }
354
- const indices = this.collectionIndices.get(collectionName) ?? [];
355
- return getIndexInfo.default(indices.map((field) => async (flatSelector) => {
356
- if (!Object.hasOwnProperty.call(flatSelector, field))
357
- return { matched: false };
358
- const index = await storage.readIndex(field);
359
- const fieldSelector = flatSelector[field];
360
- const filtersForNull = fieldSelector == null || fieldSelector.$exists === false;
361
- const keys = filtersForNull ? { include: null, exclude: [...index.keys()].filter((key) => key != null) } : getMatchingKeys(field, flatSelector);
362
- if (keys.include == null && keys.exclude == null)
363
- return { matched: false };
364
- let includedIds = [];
365
- if (keys.include == null) {
366
- for (const set of index.values())
367
- for (const pos of set)
368
- includedIds.push(pos);
369
- } else {
370
- for (const key of keys.include) {
371
- const idSet = index.get(key);
372
- if (idSet)
373
- for (const id of idSet)
374
- includedIds.push(id);
375
- }
376
- }
377
- if (keys.exclude != null) {
378
- const excludeIds = /* @__PURE__ */ new Set();
379
- for (const key of keys.exclude) {
380
- const idSet = index.get(key);
381
- if (idSet)
382
- for (const id of idSet)
383
- excludeIds.add(id);
384
- }
385
- includedIds = includedIds.filter((pos) => !excludeIds.has(pos));
386
- }
387
- return {
388
- matched: true,
389
- ids: includedIds,
390
- fields: [field],
391
- keepSelector: filtersForNull
392
- };
393
- }), selector);
394
- }
395
- async queryItems(collectionName, selector) {
396
- const storage = this.storageAdapters.get(collectionName);
397
- if (!storage)
398
- throw new Error(`No persistence adapter for collection ${collectionName}`);
399
- const index = await this.getIndexInfo(collectionName, selector);
400
- const matchItems = (item) => {
401
- if (index.optimizedSelector == null)
402
- return true;
403
- if (Object.keys(index.optimizedSelector).length <= 0)
404
- return true;
405
- return match(item, index.optimizedSelector);
406
- };
407
- if (index.matched) {
408
- const items = await storage.readIds(index.ids);
409
- if (isEqual(index.optimizedSelector, {}))
410
- return items;
411
- return items.filter(matchItems);
412
- } else {
413
- const allItems = await storage.readAll();
414
- if (isEqual(selector, {}))
415
- return allItems;
416
- return allItems.filter(matchItems);
417
- }
418
- }
419
- async executeQuery(collectionName, selector, options) {
420
- const items = await this.queryItems(collectionName, selector || {});
421
- const { sort, skip, limit, fields } = options || {};
422
- const sorted = sort ? sortItems(items, sort) : items;
423
- const skipped = skip ? sorted.slice(skip) : sorted;
424
- const limited = limit ? skipped.slice(0, limit) : skipped;
425
- const idExcluded = fields && fields.id === 0;
426
- return limited.map((item) => {
427
- if (!fields)
428
- return item;
429
- return { ...idExcluded ? {} : { id: item.id }, ...project(item, fields) };
430
- });
431
- }
432
- async checkQueryUpdates(collectionName, changedItems) {
433
- const registry = this.queries.get(collectionName);
434
- if (!registry)
435
- throw new Error(`Collection ${collectionName} not initialized!`);
436
- if (registry.size === 0)
437
- return;
438
- const affected = [...registry.values()].filter(({ selector }) => changedItems.some((item) => match(item, selector)));
439
- if (affected.length === 0)
440
- return;
441
- for (const { selector, options } of affected) {
442
- const qid = queryId(selector, options);
443
- this.publishState(collectionName, qid, "active", null);
444
- }
445
- await Promise.all(affected.map(async ({ selector, options }) => {
446
- const qid = queryId(selector, options);
447
- try {
448
- const items = await this.executeQuery(collectionName, selector, options);
449
- this.publishResult(collectionName, qid, items);
450
- this.publishState(collectionName, qid, "complete", null);
451
- } catch (error) {
452
- this.publishState(collectionName, qid, "error", error);
453
- }
454
- }));
455
- }
456
- // ===== CRUD with push-style query updates =====
457
- async insert(collectionName, newItem) {
458
- const storage = this.storageAdapters.get(collectionName);
459
- if (!storage)
460
- throw new Error(`No persistence adapter for collection ${collectionName}`);
461
- const existing = await this.executeQuery(collectionName, { id: newItem.id }, { limit: 1 });
462
- if (existing.length > 0)
463
- throw new Error(`Item with id ${String(newItem.id)} already exists`);
464
- await storage.insert([newItem]);
465
- await this.checkQueryUpdates(collectionName, [newItem]);
466
- return newItem;
467
- }
468
- async updateOne(collectionName, selector, modifier) {
469
- const storage = this.storageAdapters.get(collectionName);
470
- if (!storage)
471
- throw new Error(`No persistence adapter for collection ${collectionName}`);
472
- const items = await this.executeQuery(collectionName, selector, { limit: 1 });
473
- const [item] = items;
474
- const { $setOnInsert, ...rest } = modifier;
475
- if (item == null)
476
- return [];
477
- const modified = modify(deepClone.default(item), rest);
478
- if (item.id !== modified.id) {
479
- const existing = await this.executeQuery(collectionName, { id: modified.id }, { limit: 1 });
480
- if (existing.length > 0)
481
- throw new Error(`Item with id ${String(modified.id)} already exists`);
482
- }
483
- await storage.replace([modified]);
484
- await this.checkQueryUpdates(collectionName, [item, modified]);
485
- return [modified];
486
- }
487
- async updateMany(collectionName, selector, modifier) {
488
- const storage = this.storageAdapters.get(collectionName);
489
- if (!storage)
490
- throw new Error(`No persistence adapter for collection ${collectionName}`);
491
- const items = await this.executeQuery(collectionName, selector);
492
- if (items.length === 0)
493
- return [];
494
- const { $setOnInsert, ...rest } = modifier;
495
- const changed = await Promise.all(items.map(async (item) => {
496
- const modified = modify(deepClone.default(item), rest);
497
- if (item.id !== modified.id) {
498
- const existing = await this.executeQuery(collectionName, { id: modified.id }, { limit: 1 });
499
- if (existing.length > 0)
500
- throw new Error(`Item with id ${String(modified.id)} already exists`);
501
- }
502
- return modified;
503
- }));
504
- await storage.replace(changed);
505
- await this.checkQueryUpdates(collectionName, [...items, ...changed]);
506
- return changed;
507
- }
508
- async replaceOne(collectionName, selector, replacement) {
509
- const storage = this.storageAdapters.get(collectionName);
510
- if (!storage)
511
- throw new Error(`No persistence adapter for collection ${collectionName}`);
512
- const items = await this.executeQuery(collectionName, selector, { limit: 1 });
513
- const [item] = items;
514
- if (item == null)
515
- return [];
516
- const modified = { ...replacement, id: replacement.id ?? item.id };
517
- if (item.id !== modified.id) {
518
- const existing = await this.executeQuery(collectionName, { id: modified.id }, { limit: 1 });
519
- if (existing.length > 0)
520
- throw new Error(`Item with id ${String(modified.id)} already exists`);
521
- }
522
- await storage.replace([modified]);
523
- await this.checkQueryUpdates(collectionName, [item, modified]);
524
- return [modified];
525
- }
526
- async removeOne(collectionName, selector) {
527
- const storage = this.storageAdapters.get(collectionName);
528
- if (!storage)
529
- throw new Error(`No persistence adapter for collection ${collectionName}`);
530
- const items = await this.executeQuery(collectionName, selector, { limit: 1 });
531
- const [item] = items;
532
- if (item == null)
533
- return [];
534
- await storage.remove([item]);
535
- await this.checkQueryUpdates(collectionName, [item]);
536
- return [item];
537
- }
538
- async removeMany(collectionName, selector) {
539
- const storage = this.storageAdapters.get(collectionName);
540
- if (!storage)
541
- throw new Error(`No persistence adapter for collection ${collectionName}`);
542
- const items = await this.executeQuery(collectionName, selector);
543
- if (items.length === 0)
544
- return [];
545
- await storage.remove(items);
546
- await this.checkQueryUpdates(collectionName, items);
547
- return items;
548
- }
549
- // ===== Upsert/merge helper for ingesting fetched items =====
550
- async upsertMerged(collectionName, incoming) {
551
- if (incoming.length === 0)
552
- return;
553
- const storage = this.storageAdapters.get(collectionName);
554
- if (!storage)
555
- throw new Error(`No persistence adapter for collection ${collectionName}`);
556
- const ids = incoming.map((i) => i.id);
557
- const existing = await storage.readIds(ids);
558
- const existingById = new Map(existing.map((it) => [it.id, it]));
559
- const toInsert = [];
560
- const toReplace = [];
561
- const referenceMap = this.idRefCounts.get(collectionName);
562
- const autoload = this.autoloadIds.get(collectionName);
563
- for (const item of incoming) {
564
- const previous = existingById.get(item.id);
565
- if (previous) {
566
- toReplace.push(this.mergeItems(previous, item));
567
- } else {
568
- toInsert.push(item);
569
- }
570
- autoload?.add(item.id);
571
- referenceMap?.set(item.id, (referenceMap?.get(item.id) ?? 0) + 1);
572
- }
573
- if (toInsert.length > 0)
574
- await storage.insert(toInsert);
575
- if (toReplace.length > 0)
576
- await storage.replace(toReplace);
577
- }
578
- }
579
- module.exports = AutoFetchDataAdapter;