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

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 (46) hide show
  1. package/README.md +3 -3
  2. package/dist/.vite/manifest.json +61 -59
  3. package/dist/AsyncDataAdapter.d.ts +15 -2
  4. package/dist/AutoFetchDataAdapter.d.ts +13 -2
  5. package/dist/Collection/Observer.d.ts +1 -1
  6. package/dist/Collection/index.d.ts +11 -12
  7. package/dist/DataAdapter.d.ts +19 -3
  8. package/dist/WorkerDataAdapterHost.d.ts +19 -6
  9. package/dist/index.cjs.js +4 -4
  10. package/dist/index.d.ts +1 -1
  11. package/dist/index.mjs +4 -4
  12. package/dist/index12.cjs.js +1 -1
  13. package/dist/index12.mjs +1 -1
  14. package/dist/index15.cjs.js +6 -2
  15. package/dist/index15.mjs +6 -2
  16. package/dist/index17.cjs.js +4 -2
  17. package/dist/index17.mjs +4 -3
  18. package/dist/index18.cjs.js +1 -0
  19. package/dist/index18.mjs +1 -1
  20. package/dist/index28.cjs.js +15 -5
  21. package/dist/index28.mjs +15 -5
  22. package/dist/index29.cjs.js +29 -16
  23. package/dist/index29.mjs +29 -16
  24. package/dist/index33.cjs.js +17 -49
  25. package/dist/index33.mjs +17 -49
  26. package/dist/index34.cjs.js +45 -445
  27. package/dist/index34.mjs +45 -445
  28. package/dist/index35.cjs.js +132 -65
  29. package/dist/index35.mjs +132 -65
  30. package/dist/index36.cjs.js +385 -531
  31. package/dist/index36.mjs +386 -533
  32. package/dist/index37.cjs.js +68 -17
  33. package/dist/index37.mjs +68 -17
  34. package/dist/index38.cjs.js +536 -319
  35. package/dist/index38.mjs +538 -321
  36. package/dist/index39.cjs.js +301 -466
  37. package/dist/index39.mjs +301 -466
  38. package/dist/index40.cjs.js +484 -0
  39. package/dist/index40.mjs +484 -0
  40. package/dist/index5.cjs.js +3 -3
  41. package/dist/index5.mjs +3 -3
  42. package/dist/index6.cjs.js +1 -0
  43. package/dist/index6.mjs +1 -1
  44. package/dist/types/StorageAdapter.d.ts +80 -0
  45. package/dist/utils/executeStorageQuery.d.ts +28 -0
  46. package/package.json +1 -1
@@ -1,513 +1,348 @@
1
- const require_isEqual = require("./index2.cjs.js");
2
- const require_getIndexInfo = require("./index17.cjs.js");
1
+ const require_queryDelta = require("./index4.cjs.js");
3
2
  const require_deepClone = require("./index18.cjs.js");
4
3
  const require_match = require("./index19.cjs.js");
5
4
  const require_modify = require("./index20.cjs.js");
6
- const require_project = require("./index22.cjs.js");
7
- const require_sortItems = require("./index24.cjs.js");
5
+ const require_incrementalQueryUpdate = require("./index25.cjs.js");
8
6
  const require_queryId = require("./index26.cjs.js");
9
- const require_idIndexQuery = require("./index27.cjs.js");
10
- const require_storageIndexQuery = require("./index33.cjs.js");
11
- //#region src/AutoFetchDataAdapter.ts
7
+ const require_compact = require("./index33.cjs.js");
8
+ const require_executeStorageQuery = require("./index35.cjs.js");
9
+ //#region src/WorkerDataAdapterHost.ts
12
10
  /**
13
- * Default merge strategy: shallow spread (right wins)
14
- * @param a first item
15
- * @param b second item
16
- * @returns merged item
11
+ * Turns the item states a write produced into the upsert/delete split a query update needs.
12
+ *
13
+ * The two lists are not symmetric: an item that is still there after the write is described by
14
+ * its new state, while an item that is gone — removed, or given a new id — is described by the id
15
+ * it used to have and nothing else. Mixing the states from before and after a write into one list
16
+ * loses exactly that distinction.
17
+ * @template T - The type of the items.
18
+ * @param previousItems - The items as they were before the write.
19
+ * @param modifiedItems - The items as they are after it.
20
+ * @returns The changeset describing the write.
17
21
  */
18
- function defaultMergeItems(a, b) {
22
+ function toChangeset(previousItems, modifiedItems) {
23
+ const modifiedIds = new Set(modifiedItems.map((item) => item.id));
19
24
  return {
20
- ...a,
21
- ...b
25
+ upserts: modifiedItems,
26
+ deletes: previousItems.map((item) => item.id).filter((id) => !modifiedIds.has(id))
22
27
  };
23
28
  }
24
- /**
25
- * Generates a stable key for a selector
26
- * @param selector - the selector
27
- * @returns the key
28
- */
29
- function selectorId(selector) {
30
- return JSON.stringify(selector ?? {});
31
- }
32
- /**
33
- * AutoFetchDataAdapter
34
- *
35
- * A DataAdapter that:
36
- * - Mirrors the CollectionBackend surface (CRUD + query registry + lifecycle)
37
- * - Executes queries against a provided StorageAdapter (local cache)
38
- * - On first registration of a selector, auto-fetches from a remote source and
39
- * ingests the result into storage (upsert), then pushes query result updates
40
- * - Optionally purges auto-fetched items for a selector once no observers remain
41
- * - Can subscribe to remote change notifications to re-fetch active selectors
42
- *
43
- * IMPORTANT: Purging only ever deletes items that were introduced via the
44
- * auto-fetch path and are no longer referenced by any active selector. Items
45
- * inserted through CRUD calls are never purged.
46
- */
47
- var AutoFetchDataAdapter = class {
29
+ var WorkerDataAdapterHost = class {
30
+ workerContext;
48
31
  options;
49
32
  id;
50
- onError;
51
- fetchQueryItems;
52
- mergeItems;
53
- purgeDelay;
33
+ log = () => {};
54
34
  storageAdapters = /* @__PURE__ */ new Map();
55
35
  storageAdapterReady = /* @__PURE__ */ new Map();
56
36
  collectionIndices = /* @__PURE__ */ new Map();
57
37
  queries = /* @__PURE__ */ new Map();
58
- activeObservers = /* @__PURE__ */ new Map();
59
- observerTimeouts = /* @__PURE__ */ new Map();
60
- selectorIds = /* @__PURE__ */ new Map();
61
- idRefCounts = /* @__PURE__ */ new Map();
62
- autoloadIds = /* @__PURE__ */ new Map();
63
- constructor(options) {
38
+ onError = (error) => {
39
+ console.error(error);
40
+ };
41
+ constructor(workerContext, options) {
42
+ this.workerContext = workerContext;
64
43
  this.options = options;
65
- this.id = options.id || "autofetch-data-adapter";
66
- this.onError = options.onError ?? ((error) => {
67
- console.error(error);
68
- });
69
- this.fetchQueryItems = options.fetchQueryItems;
70
- this.mergeItems = options.mergeItems ?? defaultMergeItems;
71
- this.purgeDelay = options.purgeDelay ?? 1e4;
72
- if (options.registerRemoteChange) options.registerRemoteChange(async () => {
73
- await this.forceRefetchAll();
74
- });
75
- }
76
- createCollectionBackend(collection, indices) {
77
- this.collectionIndices.set(collection.name, indices);
78
- this.queries.set(collection.name, /* @__PURE__ */ new Map());
79
- this.ensureStorageAdapter(collection.name);
80
- this.activeObservers.set(collection.name, /* @__PURE__ */ new Map());
81
- this.observerTimeouts.set(collection.name, /* @__PURE__ */ new Map());
82
- this.selectorIds.set(collection.name, /* @__PURE__ */ new Map());
83
- this.idRefCounts.set(collection.name, /* @__PURE__ */ new Map());
84
- this.autoloadIds.set(collection.name, /* @__PURE__ */ new Set());
85
- const ready = this.setupStorage(collection.name, indices);
86
- this.storageAdapterReady.set(collection.name, ready);
87
- const registerQuery = (selector, options) => {
88
- const qid = require_queryId.default(selector, options);
89
- const registry = this.queries.get(collection.name);
90
- if (!registry) throw new Error(`Collection ${collection.name} not initialized!`);
91
- registry.set(qid, {
92
- selector,
93
- options,
94
- items: [],
95
- listeners: /* @__PURE__ */ new Set(),
96
- ...registry.get(qid),
97
- state: "active",
98
- error: null
99
- });
100
- const key = selectorId(selector);
101
- const perColObservers = this.activeObservers.get(collection.name);
102
- const current = perColObservers?.get(key)?.count ?? 0;
103
- perColObservers?.set(key, {
104
- selector,
105
- count: current + 1
106
- });
107
- const t = this.observerTimeouts.get(collection.name)?.get(key);
108
- if (t) clearTimeout(t);
109
- if (current === 0) this.fetchAndIngest(collection.name, selector).catch(this.onError);
110
- this.fulfillQuery(collection.name, selector, options).catch(this.onError);
111
- };
112
- const unregisterQuery = (selector, options) => {
113
- const qid = require_queryId.default(selector, options);
114
- this.queries.get(collection.name)?.delete(qid);
115
- const key = selectorId(selector);
116
- const perColObservers = this.activeObservers.get(collection.name);
117
- const current = perColObservers?.get(key)?.count ?? 0;
118
- const remaining = Math.max(0, current - 1);
119
- if (remaining > 0) {
120
- perColObservers?.set(key, {
121
- selector,
122
- count: remaining
123
- });
124
- return;
125
- }
126
- const doPurge = () => {
127
- perColObservers?.delete(key);
128
- this.purgeSelector(collection.name, selector).catch(this.onError);
129
- };
130
- if (this.purgeDelay === 0) doPurge();
131
- else {
132
- const timeouts = this.observerTimeouts.get(collection.name);
133
- const t = timeouts?.get(key);
134
- if (t) clearTimeout(t);
135
- timeouts?.set(key, setTimeout(doPurge, this.purgeDelay));
136
- }
137
- };
138
- const getQueryState = (selector, options) => {
139
- return (this.queries.get(collection.name)?.get(require_queryId.default(selector, options)))?.state ?? "active";
140
- };
141
- const getQueryError = (selector, options) => {
142
- return (this.queries.get(collection.name)?.get(require_queryId.default(selector, options)))?.error ?? null;
143
- };
144
- const getQueryResult = (selector, options) => {
145
- return (this.queries.get(collection.name)?.get(require_queryId.default(selector, options)))?.items ?? [];
146
- };
147
- const onQueryStateChange = (selector, options, callback) => {
148
- const qid = require_queryId.default(selector, options);
149
- const registry = this.queries.get(collection.name);
150
- if (!registry) throw new Error(`Collection ${collection.name} not initialized!`);
151
- if (!registry.has(qid)) registry.set(qid, {
152
- selector,
153
- options,
154
- state: "active",
155
- error: null,
156
- items: [],
157
- listeners: /* @__PURE__ */ new Set()
158
- });
159
- registry.get(qid)?.listeners.add(callback);
160
- return () => {
161
- registry.get(qid)?.listeners.delete(callback);
162
- };
163
- };
164
- return {
165
- insert: async (item) => {
166
- await ready;
167
- return await this.insert(collection.name, item);
168
- },
169
- updateOne: async (selector, modifier) => {
170
- await ready;
171
- return this.updateOne(collection.name, selector, modifier);
172
- },
173
- updateMany: async (selector, modifier) => {
174
- await ready;
175
- return this.updateMany(collection.name, selector, modifier);
176
- },
177
- replaceOne: async (selector, replacement) => {
178
- await ready;
179
- return this.replaceOne(collection.name, selector, replacement);
180
- },
181
- removeOne: async (selector) => {
182
- await ready;
183
- return this.removeOne(collection.name, selector);
184
- },
185
- removeMany: async (selector) => {
186
- await ready;
187
- return this.removeMany(collection.name, selector);
188
- },
189
- registerQuery,
190
- unregisterQuery,
191
- getQueryState,
192
- getQueryError,
193
- getQueryResult,
194
- onQueryStateChange,
195
- executeQuery: async (selector, options) => {
196
- await ready;
197
- registerQuery(selector, options);
198
- await new Promise((resolve) => {
199
- let stop = () => {};
200
- stop = onQueryStateChange(selector, options, (state) => {
201
- if (state === "active") return;
202
- resolve();
203
- stop();
204
- });
205
- });
206
- const result = getQueryResult(selector, options);
207
- unregisterQuery(selector, options);
208
- return result;
209
- },
210
- dispose: async () => {
211
- this.storageAdapters.delete(collection.name);
212
- this.queries.delete(collection.name);
213
- this.collectionIndices.delete(collection.name);
214
- this.storageAdapterReady.delete(collection.name);
215
- this.activeObservers.delete(collection.name);
216
- this.observerTimeouts.delete(collection.name);
217
- this.selectorIds.delete(collection.name);
218
- this.idRefCounts.delete(collection.name);
219
- this.autoloadIds.delete(collection.name);
220
- },
221
- isReady: async () => {
222
- await ready;
44
+ this.id = this.options.id || "default-worker-data-adapter";
45
+ if (this.options.onError) this.onError = this.options.onError;
46
+ if (this.options.log) this.log = this.options.log;
47
+ this.workerContext.addEventListener("message", async (event) => {
48
+ try {
49
+ const { workerId, id, method, args } = event.data;
50
+ await this.handleMessage(workerId, id, method, args);
51
+ } catch (error) {
52
+ this.onError(error);
223
53
  }
224
- };
54
+ });
55
+ this.respond("ready", null, null, "ready");
225
56
  }
226
- async forceRefetchAll() {
227
- const tasks = [];
228
- for (const [collectionName, observers] of this.activeObservers.entries()) for (const { selector, count } of observers.values()) if (count > 0) tasks.push(this.fetchAndIngest(collectionName, selector));
229
- await Promise.all(tasks);
57
+ respond(id, data, error = null, type = "response") {
58
+ this.workerContext.postMessage({
59
+ id,
60
+ workerId: this.id,
61
+ type,
62
+ data,
63
+ error
64
+ });
230
65
  }
231
- async fetchAndIngest(collectionName, selector) {
232
- this.publishForSelector(collectionName, selector, "active", null);
66
+ async handleMessage(workerId, id, method, args) {
67
+ if (workerId !== this.id) return;
68
+ const fn = this[method];
69
+ if (typeof fn !== "function") {
70
+ this.respond(id, null, /* @__PURE__ */ new Error(`Method ${method} not found`));
71
+ return;
72
+ }
73
+ this.log(method, ...args);
233
74
  try {
234
- const items = await this.fetchQueryItems(collectionName, selector);
235
- if (!items || !Array.isArray(items)) throw new Error("AutoFetchDataAdapter: fetchQueryItems must resolve to { items: T[] }");
236
- const ids = items.map((i) => i.id);
237
- const selectorKey = selectorId(selector);
238
- const selMap = this.selectorIds.get(collectionName);
239
- const previous = selMap?.get(selectorKey) ?? /* @__PURE__ */ new Set();
240
- ids.forEach((id) => previous.add(id));
241
- selMap?.set(selectorKey, previous);
242
- await this.upsertMerged(collectionName, items);
243
- await this.checkQueryUpdates(collectionName, items);
75
+ await this.isReady(args[0]);
76
+ const result = await fn.apply(this, args);
77
+ this.respond(id, result);
244
78
  } catch (error) {
245
- this.publishForSelector(collectionName, selector, "error", error);
246
- this.onError(error);
79
+ this.respond(id, null, error);
247
80
  }
248
81
  }
249
- async purgeSelector(collectionName, selector) {
250
- const selectorKey = selectorId(selector);
251
- const selMap = this.selectorIds.get(collectionName);
252
- const ids = selMap?.get(selectorKey);
253
- selMap?.delete(selectorKey);
254
- if (!ids || ids.size === 0) return;
255
- const referenceMap = this.idRefCounts.get(collectionName);
256
- const autoload = this.autoloadIds.get(collectionName);
257
- const toRemove = [];
258
- for (const id of ids) {
259
- const current = referenceMap?.get(id) ?? 0;
260
- const next = Math.max(0, current - 1);
261
- if (next === 0) {
262
- referenceMap?.delete(id);
263
- if (autoload?.has(id)) toRemove.push(id);
264
- } else referenceMap?.set(id, next);
265
- }
266
- if (toRemove.length === 0) return;
267
- const storage = this.storageAdapters.get(collectionName);
268
- if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
269
- const items = await storage.readIds(toRemove);
270
- if (items.length > 0) {
271
- await storage.remove(items);
272
- await this.checkQueryUpdates(collectionName, items);
82
+ /**
83
+ * Reads one query's result from the storage adapter.
84
+ *
85
+ * Shares `executeStorageQuery` with `AsyncDataAdapter` and
86
+ * `AutoFetchDataAdapter`. It used to be the same eight lines in all three,
87
+ * which is how a storage-adapter capability ends up honoured by one of them
88
+ * and silently missing from the others — and how the primary-key fast path
89
+ * came to be maintained three times over.
90
+ * @template T - The type of the items.
91
+ * @template I - The type of the item ids.
92
+ * @param collectionName - The collection to read from.
93
+ * @param selector - The query's selector.
94
+ * @param options - The query's sort, window and projection.
95
+ * @returns The query result.
96
+ */
97
+ async executeQuery(collectionName, selector, options) {
98
+ const storageAdapter = this.storageAdapters.get(collectionName);
99
+ if (!storageAdapter) throw new Error(`No storage adapter for collection ${collectionName}`);
100
+ return require_executeStorageQuery.default(storageAdapter, this.collectionIndices.get(collectionName) ?? [], selector, options);
101
+ }
102
+ ensureQuery(collectionName, selector, options) {
103
+ const id = require_queryId.default(selector, options);
104
+ if (!this.queries.get(collectionName)) throw new Error(`Collection ${collectionName} not initialized!`);
105
+ let query = this.queries.get(collectionName)?.get(id);
106
+ if (!query) {
107
+ query = {
108
+ selector,
109
+ options,
110
+ items: null
111
+ };
112
+ this.queries.get(collectionName)?.set(id, query);
273
113
  }
274
- toRemove.forEach((id) => autoload?.delete(id));
114
+ return query;
115
+ }
116
+ setQueryItems(query, items) {
117
+ query.items = items;
118
+ query.itemIds = void 0;
119
+ }
120
+ queryItemIds(query) {
121
+ if (!query.itemIds) query.itemIds = new Set((query.items ?? []).map((item) => item.id));
122
+ return query.itemIds;
275
123
  }
276
- async setupStorage(collectionName, indices) {
277
- const storage = this.storageAdapters.get(collectionName);
278
- if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
279
- await storage.setup();
280
- await Promise.all(indices.map((field) => storage.createIndex(field)));
124
+ emitQueryUpdate(collectionName, selector, options, state, error, items, delta) {
125
+ const id = require_queryId.default(selector, options);
126
+ if (!this.queries.get(collectionName)) throw new Error(`Collection ${collectionName} not initialized!`);
127
+ this.respond(id, {
128
+ collectionName,
129
+ qid: id,
130
+ selector,
131
+ options,
132
+ state,
133
+ error,
134
+ items,
135
+ delta
136
+ }, null, "queryUpdate");
281
137
  }
282
138
  ensureStorageAdapter(name) {
283
139
  if (this.storageAdapters.has(name)) return;
284
- const adapter = this.options.storage && this.options.storage(name);
140
+ const adapter = this.options.storage(name);
285
141
  if (!adapter) return;
286
142
  this.storageAdapters.set(name, adapter);
287
143
  }
288
- publishForSelector(collectionName, selector, state, error) {
289
- const registry = this.queries.get(collectionName);
290
- if (!registry) return;
291
- for (const query of registry.values()) {
292
- if (!require_isEqual.default(query.selector, selector)) continue;
293
- this.publishState(collectionName, require_queryId.default(query.selector, query.options), state, error);
294
- }
295
- }
296
- publishState(collectionName, qid, state, error) {
297
- const query = this.queries.get(collectionName)?.get(qid);
298
- if (!query) return;
299
- query.state = state;
300
- query.error = error;
301
- for (const callback of query.listeners) try {
302
- callback(state);
303
- } catch (error_) {
304
- this.onError(error_);
305
- }
306
- }
307
- publishResult(collectionName, qid, items) {
308
- const query = this.queries.get(collectionName)?.get(qid);
309
- if (!query) return;
310
- query.items = items;
311
- this.queries.get(collectionName)?.set(qid, query);
312
- }
313
- async fulfillQuery(collectionName, selector, options) {
314
- const qid = require_queryId.default(selector, options);
315
- const registry = this.queries.get(collectionName);
316
- if (!registry) throw new Error(`Collection ${collectionName} not initialized!`);
317
- if (!registry.get(qid)) return;
318
- this.publishState(collectionName, qid, "active", null);
319
- try {
320
- const items = await this.executeQuery(collectionName, selector, options);
321
- this.publishResult(collectionName, qid, items);
322
- this.publishState(collectionName, qid, "complete", null);
323
- } catch (error) {
324
- this.publishState(collectionName, qid, "error", error);
325
- }
144
+ async checkQueryUpdates(collectionName, changes) {
145
+ const queries = this.queries.get(collectionName);
146
+ if (!queries) throw new Error(`Collection ${collectionName} not initialized!`);
147
+ if (changes.upserts.length === 0 && changes.deletes.length === 0) return;
148
+ const affectedQueries = [...queries.values()].filter((query) => {
149
+ const ids = this.queryItemIds(query);
150
+ if (changes.deletes.some((id) => ids.has(id))) return true;
151
+ return changes.upserts.some((item) => ids.has(item.id) || require_match.default(item, query.selector));
152
+ });
153
+ if (affectedQueries.length === 0) return;
154
+ await Promise.all(affectedQueries.map(async (query) => {
155
+ const { selector, options } = query;
156
+ const previous = query.items;
157
+ const incremental = previous == null ? null : require_incrementalQueryUpdate.default(previous, selector, options, changes);
158
+ if (incremental != null) {
159
+ const delta = require_queryDelta.diffQueryResults(previous, incremental);
160
+ if (require_queryDelta.isEmptyQueryDelta(delta)) return;
161
+ this.setQueryItems(query, incremental);
162
+ this.emitQueryUpdate(collectionName, selector, options, "complete", null, void 0, delta);
163
+ return;
164
+ }
165
+ this.emitQueryUpdate(collectionName, selector, options, "active", null);
166
+ const queryItems = await this.executeQuery(collectionName, selector, options);
167
+ if (previous == null) {
168
+ this.setQueryItems(query, queryItems);
169
+ this.emitQueryUpdate(collectionName, selector, options, "complete", null, queryItems);
170
+ return;
171
+ }
172
+ const delta = require_queryDelta.diffQueryResults(previous, queryItems);
173
+ this.setQueryItems(query, queryItems);
174
+ this.emitQueryUpdate(collectionName, selector, options, "complete", null, void 0, delta);
175
+ }));
326
176
  }
327
- async getIndexInfo(collectionName, selector) {
328
- const storage = this.storageAdapters.get(collectionName);
329
- if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
330
- if (selector != null && Object.keys(selector).length === 1 && "id" in selector) {
331
- const idResult = require_idIndexQuery.default(selector);
332
- if (idResult.matched) return {
333
- matched: true,
334
- ids: idResult.ids,
335
- optimizedSelector: {}
177
+ registerCollection = async (collectionName, indices) => {
178
+ this.collectionIndices.set(collectionName, indices);
179
+ this.queries.set(collectionName, /* @__PURE__ */ new Map());
180
+ this.ensureStorageAdapter(collectionName);
181
+ const storageAdapter = this.storageAdapters.get(collectionName);
182
+ if (!storageAdapter) throw new Error(`No storage adapter for collection ${collectionName}`);
183
+ const setupPromise = (async () => {
184
+ await storageAdapter.setup();
185
+ await Promise.all(indices.map((index) => storageAdapter.createIndex(index)));
186
+ })();
187
+ this.storageAdapterReady.set(collectionName, setupPromise);
188
+ await setupPromise;
189
+ };
190
+ unregisterCollection = async (collectionName) => {
191
+ this.storageAdapters.delete(collectionName);
192
+ this.queries.delete(collectionName);
193
+ };
194
+ registerQuery = async (collectionName, selector, options) => {
195
+ const query = this.ensureQuery(collectionName, selector, options);
196
+ const queryItems = await this.executeQuery(collectionName, selector, options);
197
+ this.setQueryItems(query, queryItems);
198
+ this.emitQueryUpdate(collectionName, selector, options, "complete", null, queryItems);
199
+ };
200
+ unregisterQuery = async (collectionName, selector, options) => {
201
+ const id = require_queryId.default(selector, options);
202
+ if (!this.queries.get(collectionName)) throw new Error(`Collection ${collectionName} not initialized!`);
203
+ this.queries.get(collectionName)?.delete(id);
204
+ };
205
+ insert = async (collectionName, input) => {
206
+ const storageAdapter = this.storageAdapters.get(collectionName);
207
+ if (!storageAdapter) throw new Error(`No storage adapter for collection ${collectionName}`);
208
+ const existingItems = await this.executeQuery(collectionName, { id: { $in: input.map((i) => i[0].id) } });
209
+ const result = input.map(([item]) => {
210
+ if (item.id == null) return /* @__PURE__ */ new Error("Item must have an id");
211
+ if (existingItems.some((existing) => existing.id === item.id)) return /* @__PURE__ */ new Error(`Item with id ${item.id} already exists`);
212
+ return item;
213
+ });
214
+ const newItems = result.filter((item) => !(item instanceof Error));
215
+ await storageAdapter.insert(newItems);
216
+ await this.checkQueryUpdates(collectionName, {
217
+ upserts: newItems,
218
+ deletes: []
219
+ });
220
+ return result;
221
+ };
222
+ updateOne = async (collectionName, parameters) => {
223
+ const storageAdapter = this.storageAdapters.get(collectionName);
224
+ if (!storageAdapter) throw new Error(`No storage adapter for collection ${collectionName}`);
225
+ const result = await Promise.all(parameters.map(async ([selector, modifier]) => {
226
+ const item = await this.executeQuery(collectionName, selector, { limit: 1 }).then((items) => items[0] ?? null);
227
+ const { $setOnInsert, ...restModifier } = modifier;
228
+ if (item == null) return {
229
+ items: [],
230
+ previousItems: []
336
231
  };
337
- }
338
- if (selector == null) return {
339
- matched: false,
340
- ids: [],
341
- optimizedSelector: {}
342
- };
343
- return require_getIndexInfo.default((this.collectionIndices.get(collectionName) ?? []).map((field) => require_storageIndexQuery.default(storage, field)), selector);
344
- }
345
- async queryItems(collectionName, selector) {
346
- const storage = this.storageAdapters.get(collectionName);
347
- if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
348
- const index = await this.getIndexInfo(collectionName, selector);
349
- const matchItems = (item) => {
350
- if (index.optimizedSelector == null) return true;
351
- if (Object.keys(index.optimizedSelector).length <= 0) return true;
352
- return require_match.default(item, index.optimizedSelector);
353
- };
354
- if (index.matched) {
355
- const items = await storage.readIds(index.ids);
356
- if (require_isEqual.default(index.optimizedSelector, {})) return items;
357
- return items.filter(matchItems);
358
- } else {
359
- const allItems = await storage.readAll();
360
- if (require_isEqual.default(selector, {})) return allItems;
361
- return allItems.filter(matchItems);
362
- }
363
- }
364
- async executeQuery(collectionName, selector, options) {
365
- const items = await this.queryItems(collectionName, selector || {});
366
- const { sort, skip, limit, fields } = options || {};
367
- const sorted = sort ? require_sortItems.default(items, sort) : items;
368
- const skipped = skip ? sorted.slice(skip) : sorted;
369
- const limited = limit ? skipped.slice(0, limit) : skipped;
370
- const idExcluded = fields && fields.id === 0;
371
- return limited.map((item) => {
372
- if (!fields) return item;
232
+ const modifiedItem = require_modify.default(require_deepClone.default(item), restModifier);
233
+ if (item.id !== modifiedItem.id) {
234
+ if ((await this.executeQuery(collectionName, { id: modifiedItem.id }, { limit: 1 })).length > 0) return /* @__PURE__ */ new Error(`Item with id ${modifiedItem.id} already exists`);
235
+ }
373
236
  return {
374
- ...idExcluded ? {} : { id: item.id },
375
- ...require_project.default(item, fields)
237
+ items: [modifiedItem],
238
+ previousItems: [item]
376
239
  };
377
- });
378
- }
379
- async checkQueryUpdates(collectionName, changedItems) {
380
- const registry = this.queries.get(collectionName);
381
- if (!registry) throw new Error(`Collection ${collectionName} not initialized!`);
382
- if (registry.size === 0) return;
383
- const affected = [...registry.values()].filter(({ selector }) => changedItems.some((item) => require_match.default(item, selector)));
384
- if (affected.length === 0) return;
385
- for (const { selector, options } of affected) {
386
- const qid = require_queryId.default(selector, options);
387
- this.publishState(collectionName, qid, "active", null);
240
+ }));
241
+ const written = result.filter((entry) => !(entry instanceof Error));
242
+ const modifiedItems = require_compact.default(written.flatMap((entry) => entry.items));
243
+ if (modifiedItems.length > 0) {
244
+ await storageAdapter.replace(modifiedItems);
245
+ await this.checkQueryUpdates(collectionName, toChangeset(require_compact.default(written.flatMap((entry) => entry.previousItems)), modifiedItems));
388
246
  }
389
- await Promise.all(affected.map(async ({ selector, options }) => {
390
- const qid = require_queryId.default(selector, options);
247
+ return result;
248
+ };
249
+ updateMany = async (collectionName, parameters) => {
250
+ const storageAdapter = this.storageAdapters.get(collectionName);
251
+ if (!storageAdapter) throw new Error(`No storage adapter for collection ${collectionName}`);
252
+ const result = await Promise.all(parameters.map(async ([selector, modifier]) => {
253
+ const items = await this.executeQuery(collectionName, selector);
254
+ if (items.length === 0) return {
255
+ items: [],
256
+ previousItems: []
257
+ };
258
+ const { $setOnInsert, ...restModifier } = modifier;
391
259
  try {
392
- const items = await this.executeQuery(collectionName, selector, options);
393
- this.publishResult(collectionName, qid, items);
394
- this.publishState(collectionName, qid, "complete", null);
260
+ return {
261
+ items: await Promise.all(items.map(async (item) => {
262
+ const modifiedItem = require_modify.default(require_deepClone.default(item), restModifier);
263
+ if (item.id !== modifiedItem.id) {
264
+ if ((await this.executeQuery(collectionName, { id: modifiedItem.id }, { limit: 1 })).length > 0) throw new Error(`Item with id ${modifiedItem.id} already exists`);
265
+ }
266
+ return modifiedItem;
267
+ })),
268
+ previousItems: items
269
+ };
395
270
  } catch (error) {
396
- this.publishState(collectionName, qid, "error", error);
271
+ return error;
397
272
  }
398
273
  }));
399
- }
400
- async insert(collectionName, newItem) {
401
- const storage = this.storageAdapters.get(collectionName);
402
- if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
403
- if ((await this.executeQuery(collectionName, { id: newItem.id }, { limit: 1 })).length > 0) throw new Error(`Item with id ${String(newItem.id)} already exists`);
404
- await storage.insert([newItem]);
405
- await this.checkQueryUpdates(collectionName, [newItem]);
406
- return newItem;
407
- }
408
- async updateOne(collectionName, selector, modifier) {
409
- const storage = this.storageAdapters.get(collectionName);
410
- if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
411
- const [item] = await this.executeQuery(collectionName, selector, { limit: 1 });
412
- const { $setOnInsert, ...rest } = modifier;
413
- if (item == null) return [];
414
- const modified = require_modify.default(require_deepClone.default(item), rest);
415
- if (item.id !== modified.id) {
416
- if ((await this.executeQuery(collectionName, { id: modified.id }, { limit: 1 })).length > 0) throw new Error(`Item with id ${String(modified.id)} already exists`);
274
+ const written = result.filter((entry) => !(entry instanceof Error));
275
+ const modifiedItems = require_compact.default(written.flatMap((entry) => entry.items));
276
+ if (modifiedItems.length > 0) {
277
+ await storageAdapter.replace(modifiedItems);
278
+ await this.checkQueryUpdates(collectionName, toChangeset(require_compact.default(written.flatMap((entry) => entry.previousItems)), modifiedItems));
417
279
  }
418
- await storage.replace([modified]);
419
- await this.checkQueryUpdates(collectionName, [item, modified]);
420
- return [modified];
421
- }
422
- async updateMany(collectionName, selector, modifier) {
423
- const storage = this.storageAdapters.get(collectionName);
424
- if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
425
- const items = await this.executeQuery(collectionName, selector);
426
- if (items.length === 0) return [];
427
- const { $setOnInsert, ...rest } = modifier;
428
- const changed = await Promise.all(items.map(async (item) => {
429
- const modified = require_modify.default(require_deepClone.default(item), rest);
430
- if (item.id !== modified.id) {
431
- if ((await this.executeQuery(collectionName, { id: modified.id }, { limit: 1 })).length > 0) throw new Error(`Item with id ${String(modified.id)} already exists`);
280
+ return result;
281
+ };
282
+ replaceOne = async (collectionName, parameters) => {
283
+ const storageAdapter = this.storageAdapters.get(collectionName);
284
+ if (!storageAdapter) throw new Error(`No storage adapter for collection ${collectionName}`);
285
+ const result = await Promise.all(parameters.map(async ([selector, replacement]) => {
286
+ const item = await this.executeQuery(collectionName, selector, { limit: 1 }).then((items) => items[0] ?? null);
287
+ if (item == null) return {
288
+ items: [],
289
+ previousItems: []
290
+ };
291
+ const modifiedItem = {
292
+ ...replacement,
293
+ id: replacement.id ?? item.id
294
+ };
295
+ if (item.id !== modifiedItem.id) {
296
+ if ((await this.executeQuery(collectionName, { id: modifiedItem.id }, { limit: 1 })).length > 0) return /* @__PURE__ */ new Error(`Item with id ${modifiedItem.id} already exists`);
432
297
  }
433
- return modified;
298
+ return {
299
+ items: [modifiedItem],
300
+ previousItems: [item]
301
+ };
434
302
  }));
435
- await storage.replace(changed);
436
- await this.checkQueryUpdates(collectionName, [...items, ...changed]);
437
- return changed;
438
- }
439
- async replaceOne(collectionName, selector, replacement) {
440
- const storage = this.storageAdapters.get(collectionName);
441
- if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
442
- const [item] = await this.executeQuery(collectionName, selector, { limit: 1 });
443
- if (item == null) return [];
444
- const modified = {
445
- ...replacement,
446
- id: replacement.id ?? item.id
447
- };
448
- if (item.id !== modified.id) {
449
- if ((await this.executeQuery(collectionName, { id: modified.id }, { limit: 1 })).length > 0) throw new Error(`Item with id ${String(modified.id)} already exists`);
303
+ const written = result.filter((entry) => !(entry instanceof Error));
304
+ const modifiedItems = require_compact.default(written.flatMap((entry) => entry.items));
305
+ if (modifiedItems.length > 0) {
306
+ await storageAdapter.replace(modifiedItems);
307
+ await this.checkQueryUpdates(collectionName, toChangeset(require_compact.default(written.flatMap((entry) => entry.previousItems)), modifiedItems));
450
308
  }
451
- await storage.replace([modified]);
452
- await this.checkQueryUpdates(collectionName, [item, modified]);
453
- return [modified];
454
- }
455
- async removeOne(collectionName, selector) {
456
- const storage = this.storageAdapters.get(collectionName);
457
- if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
458
- const [item] = await this.executeQuery(collectionName, selector, { limit: 1 });
459
- if (item == null) return [];
460
- await storage.remove([item]);
461
- await this.checkQueryUpdates(collectionName, [item]);
462
- return [item];
463
- }
464
- async removeMany(collectionName, selector) {
465
- const storage = this.storageAdapters.get(collectionName);
466
- if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
467
- const items = await this.executeQuery(collectionName, selector);
468
- if (items.length === 0) return [];
469
- await storage.remove(items);
470
- await this.checkQueryUpdates(collectionName, items);
471
- return items;
472
- }
473
- async upsertMerged(collectionName, incoming) {
474
- if (incoming.length === 0) return;
475
- const storage = this.storageAdapters.get(collectionName);
476
- if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
477
- const ids = incoming.map((i) => i.id);
478
- const existing = await storage.readIds(ids);
479
- const existingById = new Map(existing.map((it) => [it.id, it]));
480
- const toInsert = [];
481
- const toReplace = [];
482
- const referenceMap = this.idRefCounts.get(collectionName);
483
- const autoload = this.autoloadIds.get(collectionName);
484
- for (const item of incoming) {
485
- const previous = existingById.get(item.id);
486
- if (previous) toReplace.push(this.mergeItems(previous, item));
487
- else toInsert.push(item);
488
- autoload?.add(item.id);
489
- referenceMap?.set(item.id, (referenceMap?.get(item.id) ?? 0) + 1);
309
+ return result;
310
+ };
311
+ removeOne = async (collectionName, selectors) => {
312
+ const storageAdapter = this.storageAdapters.get(collectionName);
313
+ if (!storageAdapter) throw new Error(`No storage adapter for collection ${collectionName}`);
314
+ const result = await Promise.all(selectors.map(async ([selector]) => {
315
+ const item = await this.executeQuery(collectionName, selector, { limit: 1 }).then((items) => items[0] ?? null);
316
+ if (item == null) return [];
317
+ return [item];
318
+ }));
319
+ const items = result.flat();
320
+ if (items.length > 0) {
321
+ await storageAdapter.remove(items);
322
+ await this.checkQueryUpdates(collectionName, {
323
+ upserts: [],
324
+ deletes: items.map((item) => item.id)
325
+ });
490
326
  }
491
- if (toInsert.length > 0) await storage.insert(toInsert);
492
- if (toReplace.length > 0) await storage.replace(toReplace);
493
- }
327
+ return result;
328
+ };
329
+ removeMany = async (collectionName, selectors) => {
330
+ const storageAdapter = this.storageAdapters.get(collectionName);
331
+ if (!storageAdapter) throw new Error(`No storage adapter for collection ${collectionName}`);
332
+ const result = await Promise.all(selectors.map(async ([selector]) => this.executeQuery(collectionName, selector)));
333
+ const items = result.flat();
334
+ if (items.length > 0) {
335
+ await storageAdapter.remove(items);
336
+ await this.checkQueryUpdates(collectionName, {
337
+ upserts: [],
338
+ deletes: items.map((item) => item.id)
339
+ });
340
+ }
341
+ return result;
342
+ };
343
+ isReady = async (collectionName) => {
344
+ return this.storageAdapterReady.get(collectionName);
345
+ };
494
346
  };
495
- /**
496
- * Usage (example):
497
- *
498
- * const adapter = new AutoFetchDataAdapter({
499
- * storage: (name) => new IndexedDBStorage(name),
500
- * fetchQueryItems: async (collectionName, selector) => {
501
- * const res = await fetch(`/api/${collectionName}?q=${encodeURIComponent(JSON.stringify(selector||{}))}`)
502
- * const items = await res.json()
503
- * return { items }
504
- * },
505
- * registerRemoteChange: (onChange) => subscribeToWS(onChange),
506
- * mergeItems: (a, b) => ({ ...a, ...b }),
507
- * purgeDelay: 10_000,
508
- * })
509
- *
510
- * const backend = adapter.createCollectionBackend(myCollection, ['status', 'projectId'])
511
- */
512
347
  //#endregion
513
- exports.default = AutoFetchDataAdapter;
348
+ exports.default = WorkerDataAdapterHost;