@signaldb/core 2.0.0-beta.17 → 2.0.0-beta.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index27.mjs CHANGED
@@ -1,343 +1,42 @@
1
- import isEqual from "./index2.mjs";
2
- import { callWithDelta, diffQueryResults, isEmptyQueryDelta } from "./index4.mjs";
3
- import EventEmitter from "./index7.mjs";
4
- import serializeValue from "./index13.mjs";
5
- import createIndex from "./index15.mjs";
6
- import getIndexInfo from "./index17.mjs";
7
- import deepClone from "./index18.mjs";
8
- import match from "./index19.mjs";
9
- import modify from "./index20.mjs";
10
- import projectItems from "./index23.mjs";
11
- import sortItems from "./index24.mjs";
12
- import incrementalQueryUpdate from "./index25.mjs";
13
- import queryId from "./index26.mjs";
14
- //#region src/DefaultDataAdapter.ts
1
+ import isFieldExpression from "./index12.mjs";
2
+ //#region src/utils/idIndexQuery.ts
15
3
  /**
16
- * Checks if there are any pending updates in the given changeset.
17
- * @template T - The type of the items in the changeset.
18
- * @param pendingUpdates - The changeset to check for pending updates.
19
- * @returns `true` if there are pending updates, otherwise `false`.
4
+ * Resolves a selector on `id` into the ids it names, without consulting an index.
5
+ *
6
+ * `id` is the one field every storage adapter can look up directly — that is what
7
+ * `readIds` is so a query on it never needs an index to be declared and never
8
+ * needs the whole collection to be read. This behaves like an index provider that
9
+ * happens to need no stored index, because the ids are already in the selector.
10
+ *
11
+ * Only inclusive forms can be answered this way. `$ne`/`$nin` describe everything
12
+ * except* something, which cannot be enumerated without knowing every id, so they
13
+ * report no match and take the ordinary path.
14
+ * @template T - The type of the items in the collection.
15
+ * @template I - The type of the unique identifier for the items.
16
+ * @param selector - The flat selector to resolve.
17
+ * @returns An index result naming the matched ids, or `{ matched: false }`.
20
18
  */
21
- function hasPendingUpdates(pendingUpdates) {
22
- return pendingUpdates.added.length > 0 || pendingUpdates.modified.length > 0 || pendingUpdates.removed.length > 0;
23
- }
24
- var DefaultDataAdapter = class {
25
- items = /* @__PURE__ */ new Map();
26
- options;
27
- storageAdapters = /* @__PURE__ */ new Map();
28
- collections = /* @__PURE__ */ new Set();
29
- indices = /* @__PURE__ */ new Map();
30
- activeQueries = /* @__PURE__ */ new Map();
31
- queryEmitters = /* @__PURE__ */ new Map();
32
- queuedQueryUpdates = /* @__PURE__ */ new Map();
33
- cachedQueryResults = /* @__PURE__ */ new Map();
34
- constructor(options) {
35
- this.options = options || {};
36
- }
37
- ensureStorageAdapter(name) {
38
- if (this.storageAdapters.get(name)) return;
39
- if (!this.options.storage) return;
40
- const adapter = this.options.storage(name);
41
- if (!adapter) return;
42
- this.storageAdapters.set(name, adapter);
43
- }
44
- rebuildIndices(collection) {
45
- const items = this.items.get(collection.name);
46
- if (!items) throw new Error(`Items not found for collection ${collection.name}`);
47
- const indices = this.indices.get(collection.name);
48
- if (!indices) throw new Error(`Indices not found for collection ${collection.name}`);
49
- indices.forEach((index) => index.rebuild([...items.values()]));
50
- }
51
- async setupStorageAdapter(collection) {
52
- const storageAdapter = this.storageAdapters.get(collection.name);
53
- if (!storageAdapter) return;
54
- return storageAdapter.setup().then(async () => {
55
- await this.fetchItemsFromStorage(collection);
56
- }).catch((error) => {
57
- if (!this.options.onError) {
58
- console.error(`Error during data persistence operation in collection ${collection.name}`, error);
59
- return;
60
- }
61
- this.options.onError(collection.name, error instanceof Error ? error : new Error(error));
62
- });
63
- }
64
- getIndexInfo(collection, selector) {
65
- if (selector != null && Object.keys(selector).length === 1 && "id" in selector && typeof selector.id !== "object") return {
19
+ function idIndexQuery(selector) {
20
+ if (selector == null || !Object.hasOwnProperty.call(selector, "id")) return { matched: false };
21
+ const fieldSelector = selector.id;
22
+ if (fieldSelector == null || fieldSelector instanceof RegExp) return { matched: false };
23
+ if (isFieldExpression(fieldSelector)) {
24
+ const values = fieldSelector.$in;
25
+ if (!Array.isArray(values) || values.length <= 0) return { matched: false };
26
+ return {
66
27
  matched: true,
67
- ids: [serializeValue(selector.id)],
68
- optimizedSelector: {}
69
- };
70
- if (selector == null) return {
71
- matched: false,
72
- ids: [],
73
- optimizedSelector: {}
74
- };
75
- return getIndexInfo((this.indices.get(collection.name) ?? []).map((i) => i.query.bind(i)), selector);
76
- }
77
- applyIndexDeltas(collection, changes) {
78
- const indices = this.indices.get(collection.name) ?? [];
79
- for (const index of indices) {
80
- if (changes.modified) index.update(changes.modified);
81
- if (changes.added && changes.added.length > 0) index.insert(changes.added);
82
- if (changes.removed && changes.removed.length > 0) index.remove(changes.removed);
83
- }
84
- }
85
- getItem(collection, selector) {
86
- const memory = this.items.get(collection.name) ?? /* @__PURE__ */ new Map();
87
- const indexInfo = this.getIndexInfo(collection, selector);
88
- return (indexInfo.matched ? indexInfo.ids.map((id) => memory.get(serializeValue(id))).filter((i) => i != null) : [...memory.values()]).find((document) => match(document, selector));
89
- }
90
- queryItems(collection, selector) {
91
- const indexInfo = this.getIndexInfo(collection, selector);
92
- const matchItems = (item) => {
93
- if (indexInfo.optimizedSelector == null) return true;
94
- if (Object.keys(indexInfo.optimizedSelector).length <= 0) return true;
95
- return match(item, indexInfo.optimizedSelector);
28
+ ids: values,
29
+ fields: ["id"],
30
+ keepSelector: false
96
31
  };
97
- const items = this.items.get(collection.name);
98
- if (!indexInfo.matched) {
99
- if (isEqual(selector, {})) return [...items.values()];
100
- return [...items.values()].filter(matchItems);
101
- }
102
- const foundItems = indexInfo.ids.map((ids) => items.get(serializeValue(ids))).filter((i) => i != null);
103
- if (isEqual(indexInfo.optimizedSelector, {})) return foundItems;
104
- return foundItems.filter(matchItems);
105
32
  }
106
- executeQuery(collection, selector, options) {
107
- const items = this.queryItems(collection, selector || {});
108
- const { sort, skip, limit, fields } = options || {};
109
- const sorted = sort ? sortItems(items, sort) : items;
110
- const skipped = skip ? sorted.slice(skip) : sorted;
111
- return projectItems(limit ? skipped.slice(0, limit) : skipped, fields);
112
- }
113
- flushQueuedQueryUpdates(collection) {
114
- if (!this.queuedQueryUpdates.get(collection.name)) return;
115
- const changes = this.queuedQueryUpdates.get(collection.name);
116
- if (!changes || !hasPendingUpdates(changes)) return;
117
- this.queuedQueryUpdates.set(collection.name, {
118
- added: [],
119
- modified: [],
120
- removed: []
121
- });
122
- const changeset = {
123
- upserts: [...changes.added, ...changes.modified],
124
- deletes: changes.removed.map((item) => item.id)
125
- };
126
- const flatItems = [
127
- ...changes.added,
128
- ...changes.modified,
129
- ...changes.removed
130
- ];
131
- const itemIds = new Set(flatItems.map((i) => i.id));
132
- [...this.activeQueries.get(collection.name)?.values() ?? []].filter(({ selector, options }) => {
133
- if ((this.cachedQueryResults.get(collection.name)?.get(queryId(selector, options))?.map((i) => i.id) ?? []).some((id) => itemIds.has(id))) return true;
134
- return flatItems.some((item) => match(item, selector));
135
- }).forEach(({ selector, options }) => {
136
- this.executeAndCacheQuery(collection, selector, options, changeset);
137
- });
138
- }
139
- executeAndCacheQuery(collection, selector, options, changes) {
140
- const cached = this.cachedQueryResults.get(collection.name)?.get(queryId(selector, options));
141
- const result = (changes && cached ? incrementalQueryUpdate(cached, selector, options, changes) : null) ?? this.executeQuery(collection, selector, options);
142
- const delta = cached ? diffQueryResults(cached, result) : void 0;
143
- this.cachedQueryResults.set(collection.name, this.cachedQueryResults.get(collection.name) || /* @__PURE__ */ new Map());
144
- this.cachedQueryResults.get(collection.name)?.set(queryId(selector, options), result);
145
- const emitter = this.queryEmitters.get(collection.name);
146
- if (!emitter) return;
147
- if (delta && isEmptyQueryDelta(delta)) return;
148
- emitter.emit("change", selector, options, "complete", delta);
149
- }
150
- updateQueries(collection, changes) {
151
- this.queuedQueryUpdates.set(collection.name, this.queuedQueryUpdates.get(collection.name) || {
152
- added: [],
153
- modified: [],
154
- removed: []
155
- });
156
- this.queuedQueryUpdates.get(collection.name)?.added.push(...changes.added);
157
- this.queuedQueryUpdates.get(collection.name)?.modified.push(...changes.modified);
158
- this.queuedQueryUpdates.get(collection.name)?.removed.push(...changes.removed);
159
- this.flushQueuedQueryUpdates(collection);
160
- }
161
- createCollectionBackend(collection, indices) {
162
- this.collections.add(collection);
163
- this.ensureStorageAdapter(collection.name);
164
- this.items.set(collection.name, this.items.get(collection.name) ?? /* @__PURE__ */ new Map());
165
- this.queryEmitters.set(collection.name, this.queryEmitters.get(collection.name) ?? new EventEmitter());
166
- this.queryEmitters.get(collection.name)?.setMaxListeners(Infinity);
167
- this.activeQueries.set(collection.name, this.activeQueries.get(collection.name) || /* @__PURE__ */ new Map());
168
- this.indices.set(collection.name, indices.map((field) => createIndex(field)));
169
- this.rebuildIndices(collection);
170
- const persistenceReadyPromise = this.setupStorageAdapter(collection);
171
- const backend = {
172
- insert: async (newItem) => {
173
- const items = this.items.get(collection.name);
174
- if (!items) throw new Error(`Items not found for collection ${collection.name}`);
175
- if (items.has(serializeValue(newItem.id))) throw new Error(`Item with id '${newItem.id}' already exists`);
176
- this.items.get(collection.name)?.set(serializeValue(newItem.id), newItem);
177
- await this.storageAdapters.get(collection.name)?.insert([newItem]);
178
- this.applyIndexDeltas(collection, { added: [newItem] });
179
- this.updateQueries(collection, {
180
- added: [],
181
- modified: [newItem],
182
- removed: []
183
- });
184
- return newItem;
185
- },
186
- updateOne: async (selector, modifier) => {
187
- const { $setOnInsert, ...restModifier } = modifier;
188
- const item = this.getItem(collection, selector);
189
- if (item == null) return [];
190
- const modifiedItem = modify(deepClone(item), restModifier);
191
- if (item.id !== modifiedItem.id && this.getItem(collection, { id: modifiedItem.id }) != null) throw new Error(`Item with id '${modifiedItem.id}' already exists`);
192
- this.items.get(collection.name)?.set(serializeValue(modifiedItem.id), modifiedItem);
193
- await this.storageAdapters.get(collection.name)?.replace([modifiedItem]);
194
- this.applyIndexDeltas(collection, { modified: [{
195
- oldItem: item,
196
- newItem: modifiedItem
197
- }] });
198
- this.updateQueries(collection, {
199
- added: [],
200
- modified: [modifiedItem],
201
- removed: []
202
- });
203
- return [modifiedItem];
204
- },
205
- updateMany: async (selector, modifier) => {
206
- const { $setOnInsert, ...restModifier } = modifier;
207
- const items = this.executeQuery(collection, selector, {});
208
- const changedItems = items.map((item) => {
209
- const modifiedItem = modify(deepClone(item), restModifier);
210
- if (item.id !== modifiedItem.id && this.getItem(collection, { id: modifiedItem.id }) != null) throw new Error(`Item with id '${modifiedItem.id}' already exists`);
211
- return modifiedItem;
212
- });
213
- changedItems.forEach((item) => {
214
- this.items.get(collection.name)?.set(serializeValue(item.id), item);
215
- });
216
- await this.storageAdapters.get(collection.name)?.replace(changedItems);
217
- const pairs = items.map((oldItem, index) => ({
218
- oldItem,
219
- newItem: changedItems[index]
220
- }));
221
- this.applyIndexDeltas(collection, { modified: pairs });
222
- this.updateQueries(collection, {
223
- added: [],
224
- modified: changedItems,
225
- removed: []
226
- });
227
- return changedItems;
228
- },
229
- replaceOne: async (selector, replacement) => {
230
- const item = this.getItem(collection, selector);
231
- if (item == null) return [];
232
- if (item.id !== replacement.id && replacement.id != null && this.getItem(collection, { id: replacement.id }) != null) throw new Error(`Item with id '${replacement.id}' already exists`);
233
- const modifiedItem = {
234
- id: item.id,
235
- ...replacement
236
- };
237
- this.items.get(collection.name)?.set(serializeValue(modifiedItem.id), modifiedItem);
238
- await this.storageAdapters.get(collection.name)?.replace([modifiedItem]);
239
- this.applyIndexDeltas(collection, { modified: [{
240
- oldItem: item,
241
- newItem: modifiedItem
242
- }] });
243
- this.updateQueries(collection, {
244
- added: [],
245
- modified: [modifiedItem],
246
- removed: []
247
- });
248
- return [modifiedItem];
249
- },
250
- removeOne: async (selector) => {
251
- const item = this.getItem(collection, selector);
252
- if (item == null) return [];
253
- this.items.get(collection.name)?.delete(serializeValue(item.id));
254
- await this.storageAdapters.get(collection.name)?.remove([item]);
255
- this.applyIndexDeltas(collection, { removed: [item] });
256
- this.updateQueries(collection, {
257
- added: [],
258
- modified: [],
259
- removed: [item]
260
- });
261
- return [item];
262
- },
263
- removeMany: async (selector) => {
264
- const items = backend.getQueryResult(selector, {});
265
- items.forEach((item) => {
266
- this.items.get(collection.name)?.delete(serializeValue(item.id));
267
- });
268
- await this.storageAdapters.get(collection.name)?.remove(items);
269
- this.applyIndexDeltas(collection, { removed: items });
270
- this.updateQueries(collection, {
271
- added: [],
272
- modified: [],
273
- removed: items
274
- });
275
- return items;
276
- },
277
- registerQuery: (selector, options) => {
278
- this.activeQueries.set(collection.name, this.activeQueries.get(collection.name) || /* @__PURE__ */ new Map());
279
- this.activeQueries.get(collection.name)?.set(queryId(selector, options), {
280
- selector,
281
- options
282
- });
283
- this.executeAndCacheQuery(collection, selector, options);
284
- },
285
- unregisterQuery: (selector, options) => {
286
- if (!this.activeQueries.get(collection.name)) return;
287
- this.activeQueries.get(collection.name)?.delete(queryId(selector, options));
288
- },
289
- getQueryState: () => "complete",
290
- onQueryStateChange: (selector, options, callback) => {
291
- const emitter = this.queryEmitters.get(collection.name);
292
- if (!emitter) throw new Error(`Query emitter not found for collection ${collection.name}`);
293
- const handler = (querySelector, queryOptions, state, delta) => {
294
- if (queryId(querySelector, queryOptions) !== queryId(selector, options)) return;
295
- callWithDelta(callback, state, delta);
296
- };
297
- emitter.on("change", handler);
298
- return () => {
299
- emitter.off("change", handler);
300
- };
301
- },
302
- getQueryError: () => null,
303
- getQueryResult: (selector, options) => {
304
- if (this.activeQueries.get(collection.name)?.has(queryId(selector, options))) {
305
- const results = this.cachedQueryResults.get(collection.name)?.get(queryId(selector, options));
306
- if (!results) throw new Error("Cached query results are not defined!");
307
- return results;
308
- }
309
- return this.executeQuery(collection, selector, options);
310
- },
311
- executeQuery: (selector, options) => Promise.resolve(this.executeQuery(collection, selector, options)),
312
- dispose: async () => {
313
- const adapter = this.storageAdapters.get(collection.name);
314
- if (adapter?.teardown) await adapter.teardown();
315
- this.storageAdapters.delete(collection.name);
316
- this.items.delete(collection.name);
317
- this.indices.delete(collection.name);
318
- this.activeQueries.delete(collection.name);
319
- this.queryEmitters.delete(collection.name);
320
- this.queuedQueryUpdates.delete(collection.name);
321
- this.cachedQueryResults.delete(collection.name);
322
- },
323
- isReady: () => persistenceReadyPromise
324
- };
325
- return backend;
326
- }
327
- async fetchItemsFromStorage(collection) {
328
- if (!collection) {
329
- await Promise.all([...this.collections].map((currentCollection) => this.fetchItemsFromStorage(currentCollection)));
330
- return;
331
- }
332
- const storageAdapter = this.storageAdapters.get(collection.name);
333
- if (!storageAdapter) return;
334
- const items = await storageAdapter.readAll();
335
- this.items.set(collection.name, items.reduce((map, item) => {
336
- map.set(serializeValue(item.id), item);
337
- return map;
338
- }, /* @__PURE__ */ new Map()));
339
- this.rebuildIndices(collection);
340
- }
341
- };
33
+ if (typeof fieldSelector === "object") return { matched: false };
34
+ return {
35
+ matched: true,
36
+ ids: [fieldSelector],
37
+ fields: ["id"],
38
+ keepSelector: false
39
+ };
40
+ }
342
41
  //#endregion
343
- export { DefaultDataAdapter as default };
42
+ export { idIndexQuery as default };