@signaldb/core 2.0.0-beta.1 → 2.0.0-beta.10

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 (129) hide show
  1. package/dist/.vite/manifest.json +122 -82
  2. package/dist/AsyncDataAdapter.d.ts +44 -7
  3. package/dist/AutoFetchDataAdapter.d.ts +5 -7
  4. package/dist/Collection/Cursor.d.ts +6 -5
  5. package/dist/Collection/Observer.d.ts +2 -0
  6. package/dist/Collection/index.d.ts +34 -25
  7. package/dist/Collection/types.d.ts +10 -2
  8. package/dist/DataAdapter.d.ts +12 -6
  9. package/dist/DefaultDataAdapter.d.ts +6 -6
  10. package/dist/WorkerDataAdapter.d.ts +3 -5
  11. package/dist/WorkerDataAdapterHost.d.ts +5 -5
  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 +2 -1
  19. package/dist/index.mjs +17 -32
  20. package/dist/index10.cjs.js +23 -0
  21. package/dist/index10.mjs +21 -17
  22. package/dist/index11.cjs.js +45 -0
  23. package/dist/index11.mjs +44 -15
  24. package/dist/index12.cjs.js +22 -0
  25. package/dist/index12.mjs +21 -350
  26. package/dist/index13.cjs.js +45 -0
  27. package/dist/index13.mjs +44 -419
  28. package/dist/index14.cjs.js +86 -0
  29. package/dist/index14.mjs +85 -184
  30. package/dist/index15.cjs.js +15 -0
  31. package/dist/index15.mjs +14 -386
  32. package/dist/index16.cjs.js +136 -0
  33. package/dist/index16.mjs +133 -577
  34. package/dist/index17.cjs.js +47 -0
  35. package/dist/index17.mjs +46 -150
  36. package/dist/index18.cjs.js +15 -0
  37. package/dist/index18.mjs +14 -26
  38. package/dist/index19.cjs.js +23 -0
  39. package/dist/index19.mjs +22 -42
  40. package/dist/index2.cjs.js +33 -0
  41. package/dist/index2.mjs +32 -257
  42. package/dist/index20.cjs.js +37 -0
  43. package/dist/index20.mjs +36 -7
  44. package/dist/index21.cjs.js +35 -0
  45. package/dist/index21.mjs +33 -103
  46. package/dist/index22.cjs.js +25 -0
  47. package/dist/index22.mjs +23 -127
  48. package/dist/index23.cjs.js +19 -0
  49. package/dist/index23.mjs +18 -7
  50. package/dist/index24.cjs.js +342 -0
  51. package/dist/index24.mjs +340 -26
  52. package/dist/index25.cjs.js +542 -0
  53. package/dist/index25.mjs +541 -10
  54. package/dist/index26.cjs.js +11 -0
  55. package/dist/index26.mjs +10 -29
  56. package/dist/index27.cjs.js +11 -0
  57. package/dist/index27.mjs +10 -46
  58. package/dist/index28.cjs.js +90 -0
  59. package/dist/index28.mjs +87 -7
  60. package/dist/index29.cjs.js +424 -0
  61. package/dist/index29.mjs +423 -9
  62. package/dist/index3.cjs.js +19 -0
  63. package/dist/index3.mjs +18 -541
  64. package/dist/index30.cjs.js +72 -0
  65. package/dist/index30.mjs +71 -5
  66. package/dist/index31.cjs.js +191 -0
  67. package/dist/index31.mjs +190 -7
  68. package/dist/index32.cjs.js +21 -0
  69. package/dist/index32.mjs +20 -29
  70. package/dist/index33.cjs.js +336 -0
  71. package/dist/index33.mjs +335 -42
  72. package/dist/index34.cjs.js +539 -0
  73. package/dist/index34.mjs +539 -0
  74. package/dist/index4.cjs.js +150 -0
  75. package/dist/index4.mjs +149 -5
  76. package/dist/index5.cjs.js +254 -0
  77. package/dist/index5.mjs +252 -4
  78. package/dist/index6.cjs.js +125 -0
  79. package/dist/index6.mjs +124 -29
  80. package/dist/index7.cjs.js +33 -0
  81. package/dist/index7.mjs +32 -12
  82. package/dist/index8.cjs.js +12 -0
  83. package/dist/index8.mjs +10 -4
  84. package/dist/index9.cjs.js +11 -0
  85. package/dist/index9.mjs +10 -133
  86. package/dist/types/IndexProvider.d.ts +2 -2
  87. package/dist/types/Modifier.d.ts +1 -1
  88. package/dist/types/ReactivityAdapter.d.ts +1 -1
  89. package/dist/utils/createSignal.d.ts +2 -2
  90. package/dist/utils/deepClone.d.ts +1 -1
  91. package/dist/utils/getMatchingKeys.d.ts +2 -2
  92. package/dist/utils/isFieldExpression.d.ts +1 -1
  93. package/dist/utils/match.d.ts +1 -1
  94. package/dist/utils/modify.d.ts +1 -1
  95. package/dist/utils/queryId.d.ts +2 -2
  96. package/dist/utils/reactiveOrAsync.d.ts +59 -0
  97. package/package.json +3 -3
  98. package/dist/index.cjs10.js +0 -18
  99. package/dist/index.cjs11.js +0 -15
  100. package/dist/index.cjs12.js +0 -350
  101. package/dist/index.cjs13.js +0 -419
  102. package/dist/index.cjs14.js +0 -184
  103. package/dist/index.cjs15.js +0 -386
  104. package/dist/index.cjs16.js +0 -579
  105. package/dist/index.cjs17.js +0 -150
  106. package/dist/index.cjs18.js +0 -26
  107. package/dist/index.cjs19.js +0 -43
  108. package/dist/index.cjs2.js +0 -258
  109. package/dist/index.cjs20.js +0 -7
  110. package/dist/index.cjs21.js +0 -104
  111. package/dist/index.cjs22.js +0 -129
  112. package/dist/index.cjs23.js +0 -7
  113. package/dist/index.cjs24.js +0 -27
  114. package/dist/index.cjs25.js +0 -10
  115. package/dist/index.cjs26.js +0 -29
  116. package/dist/index.cjs27.js +0 -46
  117. package/dist/index.cjs28.js +0 -8
  118. package/dist/index.cjs29.js +0 -9
  119. package/dist/index.cjs3.js +0 -542
  120. package/dist/index.cjs30.js +0 -5
  121. package/dist/index.cjs31.js +0 -7
  122. package/dist/index.cjs32.js +0 -29
  123. package/dist/index.cjs33.js +0 -42
  124. package/dist/index.cjs4.js +0 -5
  125. package/dist/index.cjs5.js +0 -5
  126. package/dist/index.cjs6.js +0 -29
  127. package/dist/index.cjs7.js +0 -12
  128. package/dist/index.cjs8.js +0 -5
  129. package/dist/index.cjs9.js +0 -133
package/dist/index13.mjs CHANGED
@@ -1,420 +1,45 @@
1
- import deepClone from "./index19.mjs";
2
- import match from "./index23.mjs";
3
- import modify from "./index7.mjs";
4
- import queryId from "./index20.mjs";
5
- import isEqual from "./index6.mjs";
6
- import getIndexInfo from "./index22.mjs";
7
- import getMatchingKeys from "./index26.mjs";
8
- import sortItems from "./index25.mjs";
9
- import project from "./index24.mjs";
10
- class AsyncDataAdapter {
11
- options;
12
- id;
13
- onError;
14
- // Per-collection resources
15
- storageAdapters = /* @__PURE__ */ new Map();
16
- storageAdapterReady = /* @__PURE__ */ new Map();
17
- collectionIndices = /* @__PURE__ */ new Map();
18
- // Per-collection query registries
19
- queries = /* @__PURE__ */ new Map();
20
- constructor(options) {
21
- this.options = options;
22
- this.id = options.id || "async-data-adapter";
23
- this.onError = options.onError ?? ((error) => {
24
- console.error(error);
25
- });
26
- }
27
- createCollectionBackend(collection, indices) {
28
- this.collectionIndices.set(collection.name, indices);
29
- this.queries.set(collection.name, /* @__PURE__ */ new Map());
30
- this.ensureStorageAdapter(collection.name);
31
- const ready = (async () => {
32
- try {
33
- await this.setupStorage(collection.name, indices);
34
- } catch (error) {
35
- this.onError(error);
36
- throw error;
37
- }
38
- })();
39
- this.storageAdapterReady.set(collection.name, ready);
40
- const registerQuery = (selector, options) => {
41
- const qid = queryId(selector, options);
42
- const registry = this.queries.get(collection.name);
43
- if (!registry)
44
- throw new Error(`Collection ${collection.name} not initialized!`);
45
- registry.set(qid, {
46
- selector,
47
- options,
48
- items: [],
49
- listeners: /* @__PURE__ */ new Set(),
50
- ...registry.get(qid),
51
- state: "active",
52
- error: null
53
- });
54
- void this.fulfillQuery(collection.name, selector, options).catch(this.onError);
55
- };
56
- const unregisterQuery = (selector, options) => {
57
- const qid = queryId(selector, options);
58
- this.queries.get(collection.name)?.delete(qid);
59
- };
60
- const getQueryState = (selector, options) => {
61
- const q = this.queries.get(collection.name)?.get(queryId(selector, options));
62
- return q?.state ?? "active";
63
- };
64
- const getQueryError = (selector, options) => {
65
- const q = this.queries.get(collection.name)?.get(queryId(selector, options));
66
- return q?.error ?? null;
67
- };
68
- const getQueryResult = (selector, options) => {
69
- const q = this.queries.get(collection.name)?.get(queryId(selector, options));
70
- return q?.items ?? [];
71
- };
72
- const onQueryStateChange = (selector, options, callback) => {
73
- const qid = queryId(selector, options);
74
- const registry = this.queries.get(collection.name);
75
- if (!registry)
76
- throw new Error(`Collection ${collection.name} not initialized!`);
77
- if (!registry.has(qid)) {
78
- registry.set(qid, {
79
- selector,
80
- options,
81
- state: "active",
82
- error: null,
83
- items: [],
84
- listeners: /* @__PURE__ */ new Set()
85
- });
86
- }
87
- registry.get(qid)?.listeners.add(callback);
88
- return () => registry.get(qid)?.listeners.delete(callback);
89
- };
90
- return {
91
- insert: async (item) => {
92
- await ready;
93
- const inserted = await this.insert(collection.name, item);
94
- return inserted;
95
- },
96
- updateOne: async (selector, modifier) => {
97
- await ready;
98
- return this.updateOne(collection.name, selector, modifier);
99
- },
100
- updateMany: async (selector, modifier) => {
101
- await ready;
102
- return this.updateMany(collection.name, selector, modifier);
103
- },
104
- replaceOne: async (selector, replacement) => {
105
- await ready;
106
- return this.replaceOne(collection.name, selector, replacement);
107
- },
108
- removeOne: async (selector) => {
109
- await ready;
110
- return this.removeOne(collection.name, selector);
111
- },
112
- removeMany: async (selector) => {
113
- await ready;
114
- return this.removeMany(collection.name, selector);
115
- },
116
- registerQuery,
117
- unregisterQuery,
118
- getQueryState,
119
- getQueryError,
120
- getQueryResult,
121
- onQueryStateChange,
122
- executeQuery: async (selector, options) => {
123
- await ready;
124
- registerQuery(selector, options);
125
- const result = getQueryResult(selector, options);
126
- unregisterQuery(selector, options);
127
- return result;
128
- },
129
- dispose: async () => {
130
- this.storageAdapters.delete(collection.name);
131
- this.queries.delete(collection.name);
132
- this.collectionIndices.delete(collection.name);
133
- this.storageAdapterReady.delete(collection.name);
134
- },
135
- isReady: async () => {
136
- await ready;
137
- }
138
- };
139
- }
140
- async setupStorage(collectionName, indices) {
141
- const storage = this.storageAdapters.get(collectionName);
142
- if (!storage)
143
- throw new Error(`No persistence adapter for collection ${collectionName}`);
144
- await Promise.all(indices.map((index) => storage.createIndex(index)));
145
- await storage.setup();
146
- }
147
- ensureStorageAdapter(name) {
148
- if (this.storageAdapters.has(name))
149
- return;
150
- const adapter = this.options.storage(name);
151
- if (!adapter)
152
- return;
153
- this.storageAdapters.set(name, adapter);
154
- }
155
- /**
156
- * Compute and publish the result for a specific query
157
- * @param collectionName - name of the collection
158
- * @param selector - query selector
159
- * @param options - query options
160
- */
161
- async fulfillQuery(collectionName, selector, options) {
162
- const qid = queryId(selector, options);
163
- const registry = this.queries.get(collectionName);
164
- if (!registry)
165
- throw new Error(`Collection ${collectionName} not initialized!`);
166
- const rec = registry.get(qid);
167
- if (!rec)
168
- return;
169
- this.publishState(collectionName, qid, "active", null);
170
- try {
171
- const items = await this.executeQuery(collectionName, selector, options);
172
- this.publishResult(collectionName, qid, items);
173
- this.publishState(collectionName, qid, "complete", null);
174
- } catch (error) {
175
- this.publishState(collectionName, qid, "error", error);
176
- }
177
- }
178
- /**
179
- * Notify listeners about state changes and keep the cache updated
180
- * @param collectionName - name of the collection
181
- * @param qid - query id
182
- * @param state - new state
183
- * @param error - error if state is 'error', null otherwise
184
- */
185
- publishState(collectionName, qid, state, error) {
186
- const rec = this.queries.get(collectionName)?.get(qid);
187
- if (!rec)
188
- return;
189
- rec.state = state;
190
- rec.error = error;
191
- for (const callback of rec.listeners) {
192
- try {
193
- callback(state);
194
- } catch (error_) {
195
- this.onError(error_);
196
- }
197
- }
198
- }
199
- publishResult(collectionName, qid, items) {
200
- const rec = this.queries.get(collectionName)?.get(qid);
201
- if (!rec)
202
- return;
203
- rec.items = items;
204
- }
205
- async getIndexInfo(collectionName, selector) {
206
- const storageAdapter = this.storageAdapters.get(collectionName);
207
- if (!storageAdapter)
208
- throw new Error(`No persistence adapter for collection ${collectionName}`);
209
- if (selector != null && Object.keys(selector).length === 1 && "id" in selector && typeof selector.id !== "object") {
210
- return {
211
- matched: true,
212
- ids: [selector.id].filter(Boolean),
213
- optimizedSelector: {}
214
- };
215
- }
216
- if (selector == null) {
217
- return {
218
- matched: false,
219
- ids: [],
220
- optimizedSelector: {}
221
- };
222
- }
223
- const indices = this.collectionIndices.get(collectionName) ?? [];
224
- return getIndexInfo(indices.map((field) => async (flatSelector) => {
225
- if (!Object.hasOwnProperty.call(flatSelector, field))
226
- return { matched: false };
227
- const index = await storageAdapter.readIndex(field);
228
- const fieldSelector = flatSelector[field];
229
- const filtersForNull = fieldSelector == null || fieldSelector.$exists === false;
230
- const keys = filtersForNull ? { include: null, exclude: [...index.keys()].filter((key) => key != null) } : getMatchingKeys(field, flatSelector);
231
- if (keys.include == null && keys.exclude == null)
232
- return { matched: false };
233
- let includedIds = [];
234
- if (keys.include == null) {
235
- for (const set of index.values())
236
- for (const pos of set)
237
- includedIds.push(pos);
238
- } else {
239
- for (const key of keys.include) {
240
- const idSet = index.get(key);
241
- if (idSet)
242
- for (const id of idSet)
243
- includedIds.push(id);
244
- }
245
- }
246
- if (keys.exclude != null) {
247
- const excludeIds = /* @__PURE__ */ new Set();
248
- for (const key of keys.exclude) {
249
- const idSet = index.get(key);
250
- if (idSet)
251
- for (const id of idSet)
252
- excludeIds.add(id);
253
- }
254
- includedIds = includedIds.filter((pos) => !excludeIds.has(pos));
255
- }
256
- return { matched: true, ids: includedIds, fields: [field], keepSelector: filtersForNull };
257
- }), selector);
258
- }
259
- async queryItems(collectionName, selector) {
260
- const storage = this.storageAdapters.get(collectionName);
261
- if (!storage)
262
- throw new Error(`No persistence adapter for collection ${collectionName}`);
263
- const index = await this.getIndexInfo(collectionName, selector);
264
- const matchItems = (item) => {
265
- if (index.optimizedSelector == null)
266
- return true;
267
- if (Object.keys(index.optimizedSelector).length <= 0)
268
- return true;
269
- return match(item, index.optimizedSelector);
270
- };
271
- if (index.matched) {
272
- const items = await storage.readIds(index.ids);
273
- if (isEqual(index.optimizedSelector, {}))
274
- return items;
275
- return items.filter(matchItems);
276
- } else {
277
- const allItems = await storage.readAll();
278
- if (isEqual(selector, {}))
279
- return allItems;
280
- return allItems.filter(matchItems);
281
- }
282
- }
283
- async executeQuery(collectionName, selector, options) {
284
- const items = await this.queryItems(collectionName, selector || {});
285
- const { sort, skip, limit, fields } = options || {};
286
- const sorted = sort ? sortItems(items, sort) : items;
287
- const skipped = skip ? sorted.slice(skip) : sorted;
288
- const limited = limit ? skipped.slice(0, limit) : skipped;
289
- const idExcluded = fields && fields.id === 0;
290
- return limited.map((item) => {
291
- if (!fields)
292
- return item;
293
- return { ...idExcluded ? {} : { id: item.id }, ...project(item, fields) };
294
- });
295
- }
296
- /**
297
- * After mutations, recompute and push updates for affected active queries
298
- * @param collectionName - name of the collection
299
- * @param changedItems - items that were inserted/updated/replaced/removed
300
- */
301
- async checkQueryUpdates(collectionName, changedItems) {
302
- const registry = this.queries.get(collectionName);
303
- if (!registry)
304
- throw new Error(`Collection ${collectionName} not initialized!`);
305
- if (registry.size === 0)
306
- return;
307
- const affected = [...registry.values()].filter(({ selector }) => changedItems.some((item) => match(item, selector)));
308
- if (affected.length === 0)
309
- return;
310
- for (const { selector, options } of affected) {
311
- const qid = queryId(selector, options);
312
- this.publishState(collectionName, qid, "active", null);
313
- }
314
- await Promise.all(affected.map(async ({ selector, options }) => {
315
- const qid = queryId(selector, options);
316
- try {
317
- const items = await this.executeQuery(collectionName, selector, options);
318
- this.publishResult(collectionName, qid, items);
319
- this.publishState(collectionName, qid, "complete", null);
320
- } catch (error) {
321
- this.publishState(collectionName, qid, "error", error);
322
- }
323
- }));
324
- }
325
- async insert(collectionName, newItem) {
326
- const storage = this.storageAdapters.get(collectionName);
327
- if (!storage)
328
- throw new Error(`No persistence adapter for collection ${collectionName}`);
329
- const existing = await this.executeQuery(collectionName, { id: newItem.id }, { limit: 1 });
330
- if (existing.length > 0)
331
- throw new Error(`Item with id ${String(newItem.id)} already exists`);
332
- await storage.insert([newItem]);
333
- await this.checkQueryUpdates(collectionName, [newItem]);
334
- return newItem;
335
- }
336
- async updateOne(collectionName, selector, modifier) {
337
- const storage = this.storageAdapters.get(collectionName);
338
- if (!storage)
339
- throw new Error(`No persistence adapter for collection ${collectionName}`);
340
- const items = await this.executeQuery(collectionName, selector, { limit: 1 });
341
- const [item] = items;
342
- const { $setOnInsert, ...rest } = modifier;
343
- if (item == null)
344
- return [];
345
- const modified = modify(deepClone(item), rest);
346
- if (item.id !== modified.id) {
347
- const existing = await this.executeQuery(collectionName, { id: modified.id }, { limit: 1 });
348
- if (existing.length > 0)
349
- throw new Error(`Item with id ${String(modified.id)} already exists`);
350
- }
351
- await storage.replace([modified]);
352
- await this.checkQueryUpdates(collectionName, [modified]);
353
- return [modified];
354
- }
355
- async updateMany(collectionName, selector, modifier) {
356
- const storage = this.storageAdapters.get(collectionName);
357
- if (!storage)
358
- throw new Error(`No persistence adapter for collection ${collectionName}`);
359
- const items = await this.executeQuery(collectionName, selector);
360
- if (items.length === 0)
361
- return [];
362
- const { $setOnInsert, ...rest } = modifier;
363
- const changed = await Promise.all(items.map(async (item) => {
364
- const modified = modify(deepClone(item), rest);
365
- if (item.id !== modified.id) {
366
- const existing = await this.executeQuery(collectionName, { id: modified.id }, { limit: 1 });
367
- if (existing.length > 0)
368
- throw new Error(`Item with id ${String(modified.id)} already exists`);
369
- }
370
- return modified;
371
- }));
372
- await storage.replace(changed);
373
- await this.checkQueryUpdates(collectionName, changed);
374
- return changed;
375
- }
376
- async replaceOne(collectionName, selector, replacement) {
377
- const storage = this.storageAdapters.get(collectionName);
378
- if (!storage)
379
- throw new Error(`No persistence adapter for collection ${collectionName}`);
380
- const items = await this.executeQuery(collectionName, selector, { limit: 1 });
381
- const [item] = items;
382
- if (item == null)
383
- return [];
384
- const modified = { ...replacement, id: replacement.id ?? item.id };
385
- if (item.id !== modified.id) {
386
- const existing = await this.executeQuery(collectionName, { id: modified.id }, { limit: 1 });
387
- if (existing.length > 0)
388
- throw new Error(`Item with id ${String(modified.id)} already exists`);
389
- }
390
- await storage.replace([modified]);
391
- await this.checkQueryUpdates(collectionName, [modified]);
392
- return [modified];
393
- }
394
- async removeOne(collectionName, selector) {
395
- const storage = this.storageAdapters.get(collectionName);
396
- if (!storage)
397
- throw new Error(`No persistence adapter for collection ${collectionName}`);
398
- const items = await this.executeQuery(collectionName, selector, { limit: 1 });
399
- const [item] = items;
400
- if (item == null)
401
- return [];
402
- await storage.remove([item]);
403
- await this.checkQueryUpdates(collectionName, [item]);
404
- return [item];
405
- }
406
- async removeMany(collectionName, selector) {
407
- const storage = this.storageAdapters.get(collectionName);
408
- if (!storage)
409
- throw new Error(`No persistence adapter for collection ${collectionName}`);
410
- const items = await this.executeQuery(collectionName, selector);
411
- if (items.length === 0)
412
- return [];
413
- await storage.remove(items);
414
- await this.checkQueryUpdates(collectionName, items);
415
- return items;
416
- }
1
+ import isFieldExpression from "./index11.mjs";
2
+ import serializeValue from "./index12.mjs";
3
+ //#region src/utils/getMatchingKeys.ts
4
+ /**
5
+ * Extracts the matching and excluded keys for a given field in a selector.
6
+ * Supports serialized values and `$in`/`$nin` field expressions for optimization.
7
+ * Returns `null` for include/exclude if the field cannot be optimized.
8
+ * @template T - The type of the items in the selector.
9
+ * @template I - The type of the unique identifier for the items.
10
+ * @param field - The name of the field to extract matching keys for.
11
+ * @param selector - The selector object containing query criteria.
12
+ * @returns An object containing arrays of serialized included and excluded keys,
13
+ * or `null` if the field cannot be optimized.
14
+ */
15
+ function getMatchingKeys(field, selector) {
16
+ const result = {
17
+ include: null,
18
+ exclude: null
19
+ };
20
+ const fieldSelector = selector[field];
21
+ if (fieldSelector instanceof RegExp) return result;
22
+ if (fieldSelector == null) return result;
23
+ if (isFieldExpression(fieldSelector)) {
24
+ if (fieldSelector.$ne != null) {
25
+ result.exclude = [serializeValue(fieldSelector.$ne)];
26
+ return result;
27
+ }
28
+ if (Array.isArray(fieldSelector.$in) && fieldSelector.$in.length > 0) {
29
+ result.include = fieldSelector.$in.map(serializeValue);
30
+ return result;
31
+ }
32
+ if (Array.isArray(fieldSelector.$nin) && fieldSelector.$nin.length > 0) {
33
+ result.exclude = fieldSelector.$nin.map(serializeValue);
34
+ return result;
35
+ }
36
+ return {
37
+ include: null,
38
+ exclude: null
39
+ };
40
+ }
41
+ result.include = [serializeValue(fieldSelector)];
42
+ return result;
417
43
  }
418
- export {
419
- AsyncDataAdapter as default
420
- };
44
+ //#endregion
45
+ export { getMatchingKeys as default };
@@ -0,0 +1,86 @@
1
+ const require_createIndexProvider = require("./index9.cjs.js");
2
+ const require_get = require("./index10.cjs.js");
3
+ const require_serializeValue = require("./index12.cjs.js");
4
+ const require_getMatchingKeys = require("./index13.cjs.js");
5
+ //#region src/createIndex.ts
6
+ /**
7
+ * creates an index for a specific field
8
+ * @param field name of the field
9
+ * @returns an index provider to pass to the `indices` option of the collection constructor
10
+ */
11
+ function createIndex(field) {
12
+ const index = /* @__PURE__ */ new Map();
13
+ const ensureSet = (key) => {
14
+ let set = index.get(key);
15
+ if (!set) {
16
+ set = /* @__PURE__ */ new Set();
17
+ index.set(key, set);
18
+ }
19
+ return set;
20
+ };
21
+ return require_createIndexProvider.default({
22
+ query(selector) {
23
+ if (!Object.hasOwnProperty.call(selector, field)) return { matched: false };
24
+ const fieldSelector = selector[field];
25
+ const filteresForNull = fieldSelector == null || fieldSelector.$exists === false;
26
+ const keys = filteresForNull ? {
27
+ include: null,
28
+ exclude: [...index.keys()].filter((key) => key != null)
29
+ } : require_getMatchingKeys.default(field, selector);
30
+ if (keys.include == null && keys.exclude == null) return { matched: false };
31
+ let includedIds = [];
32
+ if (keys.include == null) for (const set of index.values()) for (const pos of set) includedIds.push(pos);
33
+ else for (const key of keys.include) {
34
+ const posSet = index.get(key);
35
+ if (posSet) for (const pos of posSet) includedIds.push(pos);
36
+ }
37
+ if (keys.exclude != null) {
38
+ const excludeIds = /* @__PURE__ */ new Set();
39
+ for (const key of keys.exclude) {
40
+ const posSet = index.get(key);
41
+ if (posSet) for (const pos of posSet) excludeIds.add(pos);
42
+ }
43
+ includedIds = includedIds.filter((pos) => !excludeIds.has(pos));
44
+ }
45
+ return {
46
+ matched: true,
47
+ ids: includedIds,
48
+ fields: [field],
49
+ keepSelector: filteresForNull
50
+ };
51
+ },
52
+ rebuild(items) {
53
+ index.clear();
54
+ items.forEach((item) => {
55
+ ensureSet(require_serializeValue.default(require_get.default(item, field))).add(item.id);
56
+ });
57
+ },
58
+ insert(items) {
59
+ for (const item of items) ensureSet(require_serializeValue.default(require_get.default(item, field))).add(item.id);
60
+ },
61
+ remove(items) {
62
+ for (const item of items) {
63
+ const value = require_serializeValue.default(require_get.default(item, field));
64
+ const set = index.get(value);
65
+ if (!set) continue;
66
+ set.delete(item.id);
67
+ if (set.size === 0) index.delete(value);
68
+ }
69
+ },
70
+ update(pairs) {
71
+ for (const { oldItem, newItem } of pairs) {
72
+ const oldValue = require_serializeValue.default(require_get.default(oldItem, field));
73
+ const newValue = require_serializeValue.default(require_get.default(newItem, field));
74
+ if (oldValue === newValue) continue;
75
+ const oldSet = index.get(oldValue);
76
+ if (oldSet) {
77
+ oldSet.delete(oldItem.id);
78
+ if (oldSet.size === 0) index.delete(oldValue);
79
+ }
80
+ ensureSet(newValue).add(newItem.id);
81
+ }
82
+ }
83
+ });
84
+ }
85
+ //#endregion
86
+ exports.default = createIndex;