@signaldb/core 2.0.0-beta.4 → 2.0.0-beta.5

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 (60) hide show
  1. package/dist/.vite/manifest.json +27 -21
  2. package/dist/Collection/Observer.d.ts +2 -0
  3. package/dist/Collection/index.d.ts +11 -10
  4. package/dist/Collection/types.d.ts +7 -0
  5. package/dist/DefaultDataAdapter.d.ts +2 -0
  6. package/dist/index.cjs.js +8 -5
  7. package/dist/index.cjs12.js +41 -346
  8. package/dist/index.cjs13.js +308 -359
  9. package/dist/index.cjs14.js +387 -177
  10. package/dist/index.cjs15.js +182 -364
  11. package/dist/index.cjs16.js +287 -478
  12. package/dist/index.cjs17.js +563 -136
  13. package/dist/index.cjs18.js +154 -23
  14. package/dist/index.cjs19.js +23 -39
  15. package/dist/index.cjs2.js +1 -1
  16. package/dist/index.cjs20.js +39 -13
  17. package/dist/index.cjs21.js +14 -102
  18. package/dist/index.cjs22.js +93 -118
  19. package/dist/index.cjs23.js +127 -5
  20. package/dist/index.cjs24.js +5 -25
  21. package/dist/index.cjs25.js +24 -7
  22. package/dist/index.cjs26.js +8 -27
  23. package/dist/index.cjs27.js +25 -42
  24. package/dist/index.cjs28.js +44 -6
  25. package/dist/index.cjs29.js +6 -7
  26. package/dist/index.cjs3.js +9 -26
  27. package/dist/index.cjs30.js +5 -3
  28. package/dist/index.cjs31.js +40 -5
  29. package/dist/index.cjs33.js +3 -40
  30. package/dist/index.cjs34.js +9 -0
  31. package/dist/index.cjs7.js +1 -1
  32. package/dist/index.d.ts +2 -1
  33. package/dist/index.mjs +10 -7
  34. package/dist/index12.mjs +40 -346
  35. package/dist/index13.mjs +308 -359
  36. package/dist/index14.mjs +387 -177
  37. package/dist/index15.mjs +182 -364
  38. package/dist/index16.mjs +287 -478
  39. package/dist/index17.mjs +563 -136
  40. package/dist/index18.mjs +154 -23
  41. package/dist/index19.mjs +23 -38
  42. package/dist/index2.mjs +1 -1
  43. package/dist/index20.mjs +38 -13
  44. package/dist/index21.mjs +14 -102
  45. package/dist/index22.mjs +93 -117
  46. package/dist/index23.mjs +126 -5
  47. package/dist/index24.mjs +5 -25
  48. package/dist/index25.mjs +24 -7
  49. package/dist/index26.mjs +8 -27
  50. package/dist/index27.mjs +25 -42
  51. package/dist/index28.mjs +44 -6
  52. package/dist/index29.mjs +6 -7
  53. package/dist/index3.mjs +9 -26
  54. package/dist/index30.mjs +5 -3
  55. package/dist/index31.mjs +40 -5
  56. package/dist/index33.mjs +3 -40
  57. package/dist/index34.mjs +10 -0
  58. package/dist/index7.mjs +1 -1
  59. package/dist/utils/reactiveOrAsync.d.ts +59 -0
  60. package/package.json +1 -1
@@ -1,152 +1,579 @@
1
1
  "use strict";
2
+ const deepClone = require("./index.cjs20.js");
3
+ const match = require("./index.cjs24.js");
4
+ const modify = require("./index.cjs7.js");
5
+ const queryId = require("./index.cjs21.js");
2
6
  const isEqual = require("./index.cjs6.js");
3
- const uniqueBy = require("./index.cjs29.js");
4
- class Observer {
5
- previousItems = [];
6
- callbacks;
7
- unbindEvents;
8
- /**
9
- * Creates a new instance of the `Observer` class.
10
- * Sets up event bindings and initializes the callbacks for tracking changes in a collection.
11
- * @param bindEvents - A function to bind external events to the observer. Must return a cleanup function to unbind those events.
12
- */
13
- constructor(bindEvents) {
14
- this.callbacks = {
15
- added: [],
16
- addedBefore: [],
17
- changed: [],
18
- changedField: [],
19
- movedBefore: [],
20
- removed: []
7
+ const getIndexInfo = require("./index.cjs23.js");
8
+ const getMatchingKeys = require("./index.cjs27.js");
9
+ const sortItems = require("./index.cjs26.js");
10
+ const project = require("./index.cjs25.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
+ }
21
214
  };
22
- this.unbindEvents = bindEvents();
23
215
  }
24
- call(event, ...args) {
25
- this.callbacks[event].forEach(({ callback, options }) => {
26
- if (!options.skipInitial || !options.isInitial) {
27
- callback(...args);
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));
28
223
  }
29
- });
224
+ }
225
+ await Promise.all(tasks);
30
226
  }
31
- hasCallbacks(events) {
32
- return events.some((event) => this.callbacks[event].length > 0);
33
- }
34
- /**
35
- * Determines if the observer has no active callbacks registered for any events.
36
- * @returns A boolean indicating whether the observer is empty (i.e., no callbacks are registered).
37
- */
38
- isEmpty() {
39
- return !this.hasCallbacks([
40
- "added",
41
- "addedBefore",
42
- "changed",
43
- "changedField",
44
- "movedBefore",
45
- "removed"
46
- ]);
47
- }
48
- /**
49
- * Compares the previous state of items with the new state and triggers the appropriate callbacks
50
- * for events such as added, removed, changed, or moved items.
51
- * @param getItems - A function that returns a promise resolving to the new items or the items themselves.
52
- */
53
- runChecks(getItems) {
54
- const result = getItems();
55
- if (result instanceof Promise) {
56
- result.then((newItems) => this.checkItems(newItems)).catch((error) => {
57
- console.error("Error while asynchronously querying items", error);
58
- });
59
- } else {
60
- this.checkItems(result);
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);
61
245
  }
62
246
  }
63
- checkItems(newItems) {
64
- const oldItemsMap = new Map(this.previousItems.map((item, index) => [
65
- item.id,
66
- { item, index, beforeItem: this.previousItems[index + 1] || null }
67
- ]));
68
- const newItemsMap = new Map(newItems.map((item, index) => [
69
- item.id,
70
- { item, index, beforeItem: newItems[index + 1] || null }
71
- ]));
72
- if (this.hasCallbacks(["changed", "changedField", "movedBefore", "removed"])) {
73
- oldItemsMap.forEach(({ item: oldItem, index, beforeItem: oldBeforeItem }) => {
74
- const newItem = newItemsMap.get(oldItem.id);
75
- if (newItem) {
76
- if (this.hasCallbacks(["changed", "changedField"]) && !isEqual(newItem.item, oldItem)) {
77
- this.call("changed", newItem.item);
78
- if (this.hasCallbacks(["changedField"])) {
79
- const keys = uniqueBy([
80
- ...Object.keys(newItem.item),
81
- ...Object.keys(oldItem)
82
- ], (value) => value);
83
- keys.forEach((key) => {
84
- if (isEqual(newItem.item[key], oldItem[key]))
85
- return;
86
- this.call("changedField", newItem.item, key, oldItem[key], newItem.item[key]);
87
- });
88
- }
89
- }
90
- if (newItem.index !== index && newItem.beforeItem?.id !== oldBeforeItem?.id) {
91
- this.call("movedBefore", newItem.item, newItem.beforeItem);
92
- }
93
- } else {
94
- this.call("removed", oldItem);
95
- }
96
- });
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
+ }
97
267
  }
98
- if (this.hasCallbacks(["added", "addedBefore"])) {
99
- newItems.forEach((newItem, index) => {
100
- const oldItem = oldItemsMap.get(newItem.id);
101
- if (oldItem)
102
- return;
103
- this.call("added", newItem);
104
- this.call("addedBefore", newItem, newItems[index + 1] || null);
105
- });
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: {} };
106
353
  }
107
- this.previousItems = newItems;
108
- Object.keys(this.callbacks).forEach((key) => {
109
- const event = key;
110
- const callbacks = this.callbacks[event];
111
- this.callbacks[event] = callbacks.map((callback) => ({
112
- ...callback,
113
- options: {
114
- ...callback.options,
115
- isInitial: false
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);
116
375
  }
117
- }));
118
- });
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);
119
394
  }
120
- /**
121
- * Stops the observer by unbinding all events and cleaning up resources.
122
- */
123
- stop() {
124
- this.unbindEvents();
125
- }
126
- /**
127
- * Registers callbacks for specific events to observe changes in the collection.
128
- * @param callbacks - An object containing the callbacks for various events (e.g., 'added', 'removed').
129
- * @param skipInitial - A boolean indicating whether to skip invoking the callbacks for the initial state of the collection.
130
- */
131
- addCallbacks(callbacks, skipInitial = false) {
132
- Object.keys(callbacks).forEach((key) => {
133
- const typedKey = key;
134
- this.callbacks[typedKey].push({
135
- callback: callbacks[typedKey],
136
- options: { skipInitial, isInitial: true }
137
- });
138
- });
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
+ }
139
418
  }
140
- /**
141
- * Removes the specified callbacks for specific events, unregistering them from the observer.
142
- * @param callbacks - An object containing the callbacks to be removed for various events.
143
- */
144
- removeCallbacks(callbacks) {
145
- Object.keys(callbacks).forEach((key) => {
146
- const typedKey = key;
147
- const index = this.callbacks[typedKey].findIndex(({ callback }) => callback === callbacks[typedKey]);
148
- this.callbacks[typedKey].splice(index, 1);
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) };
149
430
  });
150
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
+ }
151
578
  }
152
- module.exports = Observer;
579
+ module.exports = AutoFetchDataAdapter;