@signaldb/core 1.7.0 → 2.0.0-beta.0

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 (88) hide show
  1. package/dist/.vite/manifest.json +125 -82
  2. package/dist/AsyncDataAdapter.d.ts +64 -0
  3. package/dist/AutoFetchDataAdapter.d.ts +113 -0
  4. package/dist/Collection/Cursor.d.ts +9 -9
  5. package/dist/Collection/Observer.d.ts +3 -2
  6. package/dist/Collection/index.d.ts +52 -46
  7. package/dist/Collection/types.d.ts +4 -9
  8. package/dist/DataAdapter.d.ts +33 -0
  9. package/dist/DefaultDataAdapter.d.ts +32 -0
  10. package/dist/WorkerDataAdapter.d.ts +22 -0
  11. package/dist/WorkerDataAdapterHost.d.ts +59 -0
  12. package/dist/createIndex.d.ts +7 -0
  13. package/dist/createStorageAdapter.d.ts +9 -0
  14. package/dist/{Collection/getIndexInfo.d.ts → getIndexInfo.d.ts} +17 -19
  15. package/dist/index.cjs.js +21 -17
  16. package/dist/index.cjs10.js +15 -26
  17. package/dist/index.cjs11.js +13 -10
  18. package/dist/index.cjs12.js +337 -3
  19. package/dist/index.cjs13.js +384 -115
  20. package/dist/index.cjs14.js +183 -67
  21. package/dist/index.cjs15.js +339 -8
  22. package/dist/index.cjs16.js +563 -20
  23. package/dist/index.cjs17.js +16 -12
  24. package/dist/index.cjs18.js +24 -5
  25. package/dist/index.cjs2.js +27 -36
  26. package/dist/index.cjs20.js +102 -13
  27. package/dist/index.cjs21.js +124 -21
  28. package/dist/index.cjs22.js +5 -68
  29. package/dist/index.cjs23.js +22 -82
  30. package/dist/index.cjs24.js +5 -16
  31. package/dist/index.cjs25.js +8 -27
  32. package/dist/index.cjs26.js +27 -7
  33. package/dist/index.cjs27.js +3 -5
  34. package/dist/index.cjs28.js +5 -27
  35. package/dist/index.cjs29.js +27 -40
  36. package/dist/index.cjs3.js +242 -497
  37. package/dist/index.cjs30.js +42 -0
  38. package/dist/index.cjs31.js +9 -0
  39. package/dist/index.cjs4.js +3 -170
  40. package/dist/index.cjs5.js +3 -67
  41. package/dist/index.cjs6.js +27 -3
  42. package/dist/index.cjs7.js +10 -3
  43. package/dist/index.cjs8.js +3 -3
  44. package/dist/index.cjs9.js +131 -3
  45. package/dist/index.d.ts +10 -8
  46. package/dist/index.mjs +15 -11
  47. package/dist/index10.mjs +15 -26
  48. package/dist/index11.mjs +13 -10
  49. package/dist/index12.mjs +337 -3
  50. package/dist/index13.mjs +384 -115
  51. package/dist/index14.mjs +183 -66
  52. package/dist/index15.mjs +339 -8
  53. package/dist/index16.mjs +563 -20
  54. package/dist/index17.mjs +16 -12
  55. package/dist/index18.mjs +24 -5
  56. package/dist/index2.mjs +27 -36
  57. package/dist/index20.mjs +102 -13
  58. package/dist/index21.mjs +123 -21
  59. package/dist/index22.mjs +5 -67
  60. package/dist/index23.mjs +22 -81
  61. package/dist/index24.mjs +5 -16
  62. package/dist/index25.mjs +8 -27
  63. package/dist/index26.mjs +27 -7
  64. package/dist/index27.mjs +3 -5
  65. package/dist/index28.mjs +5 -27
  66. package/dist/index29.mjs +27 -40
  67. package/dist/index3.mjs +241 -497
  68. package/dist/index30.mjs +43 -0
  69. package/dist/index31.mjs +10 -0
  70. package/dist/index4.mjs +3 -170
  71. package/dist/index5.mjs +3 -66
  72. package/dist/index6.mjs +27 -3
  73. package/dist/index7.mjs +10 -3
  74. package/dist/index8.mjs +3 -3
  75. package/dist/index9.mjs +131 -3
  76. package/dist/types/IndexProvider.d.ts +12 -7
  77. package/dist/types/StorageAdapter.d.ts +20 -0
  78. package/dist/utils/objectId.d.ts +8 -0
  79. package/dist/utils/queryId.d.ts +9 -0
  80. package/package.json +2 -2
  81. package/dist/AutoFetchCollection.d.ts +0 -60
  82. package/dist/Collection/createIndex.d.ts +0 -15
  83. package/dist/ReplicatedCollection.d.ts +0 -60
  84. package/dist/createMemoryAdapter.d.ts +0 -7
  85. package/dist/persistence/combinePersistenceAdapters.d.ts +0 -32
  86. package/dist/persistence/createPersistenceAdapter.d.ts +0 -9
  87. package/dist/types/MemoryAdapter.d.ts +0 -15
  88. package/dist/types/PersistenceAdapter.d.ts +0 -20
package/dist/index12.mjs CHANGED
@@ -1,6 +1,340 @@
1
- function randomId() {
2
- return Math.floor(Math.random() * 1e17).toString(16);
1
+ import createIndex from "./index20.mjs";
2
+ import getIndexInfo from "./index21.mjs";
3
+ import deepClone from "./index19.mjs";
4
+ import EventEmitter from "./index9.mjs";
5
+ import isEqual from "./index6.mjs";
6
+ import match from "./index22.mjs";
7
+ import modify from "./index7.mjs";
8
+ import project from "./index23.mjs";
9
+ import queryId from "./index24.mjs";
10
+ import serializeValue from "./index11.mjs";
11
+ import sortItems from "./index25.mjs";
12
+ function hasPendingUpdates(pendingUpdates) {
13
+ return pendingUpdates.added.length > 0 || pendingUpdates.modified.length > 0 || pendingUpdates.removed.length > 0;
14
+ }
15
+ class DefaultDataAdapter {
16
+ items = /* @__PURE__ */ new Map();
17
+ options;
18
+ storageAdapters = /* @__PURE__ */ new Map();
19
+ indices = /* @__PURE__ */ new Map();
20
+ activeQueries = /* @__PURE__ */ new Map();
21
+ queryEmitters = /* @__PURE__ */ new Map();
22
+ queuedQueryUpdates = /* @__PURE__ */ new Map();
23
+ cachedQueryResults = /* @__PURE__ */ new Map();
24
+ constructor(options) {
25
+ this.options = options || {};
26
+ }
27
+ ensureStorageAdapter(name) {
28
+ if (this.storageAdapters.get(name))
29
+ return;
30
+ if (!this.options.storage)
31
+ return;
32
+ const adapter = this.options.storage(name);
33
+ if (!adapter)
34
+ return;
35
+ this.storageAdapters.set(name, adapter);
36
+ }
37
+ rebuildIndices(collection) {
38
+ const items = this.items.get(collection.name);
39
+ if (!items)
40
+ throw new Error(`Items not found for collection ${collection.name}`);
41
+ const indices = this.indices.get(collection.name);
42
+ if (!indices)
43
+ throw new Error(`Indices not found for collection ${collection.name}`);
44
+ indices.forEach((index) => index.rebuild([...items.values()]));
45
+ }
46
+ async setupStorageAdapter(collection) {
47
+ const storageAdapter = this.storageAdapters.get(collection.name);
48
+ if (!storageAdapter)
49
+ return;
50
+ return storageAdapter.setup().then(async () => {
51
+ const items = await storageAdapter.readAll();
52
+ this.items.set(collection.name, items.reduce((map, item) => {
53
+ map.set(serializeValue(item.id), item);
54
+ return map;
55
+ }, /* @__PURE__ */ new Map()));
56
+ this.rebuildIndices(collection);
57
+ }).catch((error) => {
58
+ if (!this.options.onError) {
59
+ console.error(`Error during data persistence operation in collection ${collection.name}`, error);
60
+ return;
61
+ }
62
+ this.options.onError(collection.name, error instanceof Error ? error : new Error(error));
63
+ });
64
+ }
65
+ getIndexInfo(collection, selector) {
66
+ if (selector != null && Object.keys(selector).length === 1 && "id" in selector && typeof selector.id !== "object") {
67
+ return {
68
+ matched: true,
69
+ ids: [serializeValue(selector.id)],
70
+ optimizedSelector: {}
71
+ };
72
+ }
73
+ if (selector == null) {
74
+ return {
75
+ matched: false,
76
+ ids: [],
77
+ optimizedSelector: {}
78
+ };
79
+ }
80
+ return getIndexInfo((this.indices.get(collection.name) ?? []).map((i) => i.query.bind(i)), selector);
81
+ }
82
+ applyIndexDeltas(collection, changes) {
83
+ const indices = this.indices.get(collection.name) ?? [];
84
+ for (const index of indices) {
85
+ if (changes.modified)
86
+ index.update(changes.modified);
87
+ if (changes.added && changes.added.length > 0)
88
+ index.insert(changes.added);
89
+ if (changes.removed && changes.removed.length > 0)
90
+ index.remove(changes.removed);
91
+ }
92
+ }
93
+ getItem(collection, selector) {
94
+ const memory = this.items.get(collection.name) ?? /* @__PURE__ */ new Map();
95
+ const indexInfo = this.getIndexInfo(collection, selector);
96
+ const items = indexInfo.matched ? indexInfo.ids.map((id) => memory.get(serializeValue(id))).filter((i) => i != null) : [...memory.values()];
97
+ const item = items.find((document) => match(document, selector));
98
+ return item;
99
+ }
100
+ queryItems(collection, selector) {
101
+ const indexInfo = this.getIndexInfo(collection, selector);
102
+ const matchItems = (item) => {
103
+ if (indexInfo.optimizedSelector == null)
104
+ return true;
105
+ if (Object.keys(indexInfo.optimizedSelector).length <= 0)
106
+ return true;
107
+ const matches = match(item, indexInfo.optimizedSelector);
108
+ return matches;
109
+ };
110
+ const items = this.items.get(collection.name);
111
+ if (!indexInfo.matched) {
112
+ if (isEqual(selector, {}))
113
+ return [...items.values()];
114
+ return [...items.values()].filter(matchItems);
115
+ }
116
+ const foundItems = indexInfo.ids.map((ids) => items.get(serializeValue(ids))).filter((i) => i != null);
117
+ if (isEqual(indexInfo.optimizedSelector, {}))
118
+ return foundItems;
119
+ return foundItems.filter(matchItems);
120
+ }
121
+ executeQuery(collection, selector, options) {
122
+ const items = this.queryItems(collection, selector || {});
123
+ const { sort, skip, limit, fields } = options || {};
124
+ const sorted = sort ? sortItems(items, sort) : items;
125
+ const skipped = skip ? sorted.slice(skip) : sorted;
126
+ const limited = limit ? skipped.slice(0, limit) : skipped;
127
+ const idExcluded = fields && fields.id === 0;
128
+ return limited.map((item) => {
129
+ if (!fields)
130
+ return item;
131
+ return {
132
+ ...idExcluded ? {} : { id: item.id },
133
+ ...project(item, fields)
134
+ };
135
+ });
136
+ }
137
+ flushQueuedQueryUpdates(collection) {
138
+ if (!this.queuedQueryUpdates.get(collection.name))
139
+ return;
140
+ const changes = this.queuedQueryUpdates.get(collection.name);
141
+ if (!changes || !hasPendingUpdates(changes))
142
+ return;
143
+ this.queuedQueryUpdates.set(collection.name, { added: [], modified: [], removed: [] });
144
+ const flatItems = [...changes.added, ...changes.modified, ...changes.removed];
145
+ const queries = [...this.activeQueries.get(collection.name)?.values() ?? []].filter(({ selector }) => flatItems.some((item) => match(item, selector)));
146
+ queries.forEach(({ selector, options }) => {
147
+ const emitter = this.queryEmitters.get(collection.name);
148
+ if (!emitter)
149
+ return;
150
+ emitter.emit("change", selector, options, "complete");
151
+ });
152
+ }
153
+ updateQueries(collection, changes) {
154
+ this.queuedQueryUpdates.set(collection.name, this.queuedQueryUpdates.get(collection.name) || { added: [], modified: [], removed: [] });
155
+ this.queuedQueryUpdates.get(collection.name)?.added.push(...changes.added);
156
+ this.queuedQueryUpdates.get(collection.name)?.modified.push(...changes.modified);
157
+ this.queuedQueryUpdates.get(collection.name)?.removed.push(...changes.removed);
158
+ this.flushQueuedQueryUpdates(collection);
159
+ }
160
+ createCollectionBackend(collection, indices) {
161
+ this.ensureStorageAdapter(collection.name);
162
+ this.items.set(collection.name, this.items.get(collection.name) ?? /* @__PURE__ */ new Map());
163
+ this.queryEmitters.set(collection.name, this.queryEmitters.get(collection.name) ?? new EventEmitter());
164
+ this.activeQueries.set(collection.name, this.activeQueries.get(collection.name) || /* @__PURE__ */ new Map());
165
+ this.indices.set(collection.name, indices.map((field) => createIndex(field)));
166
+ this.rebuildIndices(collection);
167
+ const persistenceReadyPromise = this.setupStorageAdapter(collection);
168
+ const backend = {
169
+ // CRUD operations
170
+ insert: async (newItem) => {
171
+ const items = this.items.get(collection.name);
172
+ if (!items)
173
+ throw new Error(`Items not found for collection ${collection.name}`);
174
+ if (items.has(serializeValue(newItem.id))) {
175
+ throw new Error(`Item with id '${newItem.id}' already exists`);
176
+ }
177
+ this.items.get(collection.name)?.set(serializeValue(newItem.id), newItem);
178
+ await this.storageAdapters.get(collection.name)?.insert([newItem]);
179
+ this.applyIndexDeltas(collection, { added: [newItem] });
180
+ this.updateQueries(collection, {
181
+ added: [],
182
+ modified: [newItem],
183
+ removed: []
184
+ });
185
+ return newItem;
186
+ },
187
+ updateOne: async (selector, modifier) => {
188
+ const { $setOnInsert, ...restModifier } = modifier;
189
+ const item = this.getItem(collection, selector);
190
+ if (item == null)
191
+ return [];
192
+ const modifiedItem = modify(deepClone(item), restModifier);
193
+ const hasItemWithSameId = item.id !== modifiedItem.id && this.getItem(collection, { id: modifiedItem.id }) != null;
194
+ if (hasItemWithSameId) {
195
+ throw new Error(`Item with id '${modifiedItem.id}' already exists`);
196
+ }
197
+ this.items.get(collection.name)?.set(serializeValue(modifiedItem.id), modifiedItem);
198
+ await this.storageAdapters.get(collection.name)?.replace([modifiedItem]);
199
+ this.applyIndexDeltas(collection, { modified: [{ oldItem: item, newItem: modifiedItem }] });
200
+ this.updateQueries(collection, {
201
+ added: [],
202
+ modified: [modifiedItem],
203
+ removed: []
204
+ });
205
+ return [modifiedItem];
206
+ },
207
+ updateMany: async (selector, modifier) => {
208
+ const { $setOnInsert, ...restModifier } = modifier;
209
+ const items = backend.getQueryResult(selector);
210
+ const changedItems = items.map((item) => {
211
+ const modifiedItem = modify(deepClone(item), restModifier);
212
+ const hasItemWithSameId = item.id !== modifiedItem.id && this.getItem(collection, { id: modifiedItem.id }) != null;
213
+ if (hasItemWithSameId) {
214
+ throw new Error(`Item with id '${modifiedItem.id}' already exists`);
215
+ }
216
+ return modifiedItem;
217
+ });
218
+ changedItems.forEach((item) => {
219
+ this.items.get(collection.name)?.set(serializeValue(item.id), item);
220
+ });
221
+ await this.storageAdapters.get(collection.name)?.replace(changedItems);
222
+ const pairs = items.map((oldItem, index) => ({ oldItem, newItem: changedItems[index] }));
223
+ this.applyIndexDeltas(collection, { modified: pairs });
224
+ this.updateQueries(collection, {
225
+ added: [],
226
+ modified: changedItems,
227
+ removed: []
228
+ });
229
+ return changedItems;
230
+ },
231
+ replaceOne: async (selector, replacement) => {
232
+ const item = this.getItem(collection, selector);
233
+ if (item == null)
234
+ return [];
235
+ const hasItemWithSameId = item.id !== replacement.id && replacement.id != null && this.getItem(collection, { id: replacement.id }) != null;
236
+ if (hasItemWithSameId) {
237
+ throw new Error(`Item with id '${replacement.id}' already exists`);
238
+ }
239
+ const modifiedItem = { id: item.id, ...replacement };
240
+ this.items.get(collection.name)?.set(serializeValue(modifiedItem.id), modifiedItem);
241
+ await this.storageAdapters.get(collection.name)?.replace([modifiedItem]);
242
+ this.applyIndexDeltas(collection, { modified: [{ oldItem: item, newItem: modifiedItem }] });
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)
253
+ return [];
254
+ this.items.get(collection.name)?.delete(serializeValue(item.id));
255
+ await this.storageAdapters.get(collection.name)?.remove([item]);
256
+ this.applyIndexDeltas(collection, { removed: [item] });
257
+ this.updateQueries(collection, {
258
+ added: [],
259
+ modified: [],
260
+ removed: [item]
261
+ });
262
+ return [item];
263
+ },
264
+ removeMany: async (selector) => {
265
+ const items = backend.getQueryResult(selector);
266
+ items.forEach((item) => {
267
+ this.items.get(collection.name)?.delete(serializeValue(item.id));
268
+ });
269
+ await this.storageAdapters.get(collection.name)?.remove(items);
270
+ this.applyIndexDeltas(collection, { removed: items });
271
+ this.updateQueries(collection, {
272
+ added: [],
273
+ modified: [],
274
+ removed: items
275
+ });
276
+ return items;
277
+ },
278
+ // methods for registering and unregistering queries that will be called from the collection during find/findOne
279
+ registerQuery: (selector, options) => {
280
+ this.activeQueries.set(collection.name, this.activeQueries.get(collection.name) || /* @__PURE__ */ new Map());
281
+ this.activeQueries.get(collection.name)?.set(queryId(selector, options), {
282
+ selector,
283
+ options
284
+ });
285
+ const emitter = this.queryEmitters.get(collection.name);
286
+ if (!emitter)
287
+ throw new Error(`Query emitter not found for collection ${collection.name}`);
288
+ emitter.emit("change", selector, options, "complete");
289
+ },
290
+ unregisterQuery: (selector, options) => {
291
+ if (!this.activeQueries.get(collection.name))
292
+ return;
293
+ this.activeQueries.get(collection.name)?.delete(queryId(selector, options));
294
+ },
295
+ getQueryState: () => "complete",
296
+ onQueryStateChange: (selector, options, callback) => {
297
+ const emitter = this.queryEmitters.get(collection.name);
298
+ if (!emitter)
299
+ throw new Error(`Query emitter not found for collection ${collection.name}`);
300
+ const handler = (querySelector, queryOptions, state) => {
301
+ if (querySelector !== selector || queryOptions !== options)
302
+ return;
303
+ callback(state);
304
+ };
305
+ emitter.on("change", handler);
306
+ return () => {
307
+ emitter.off("change", handler);
308
+ };
309
+ },
310
+ getQueryError: () => null,
311
+ getQueryResult: (selector, options) => {
312
+ const result = this.executeQuery(collection, selector, options);
313
+ const isQueryActive = this.activeQueries.get(collection.name)?.has(queryId(selector, options));
314
+ if (isQueryActive) {
315
+ this.cachedQueryResults.set(collection.name, this.cachedQueryResults.get(collection.name) || /* @__PURE__ */ new Map());
316
+ this.cachedQueryResults.get(collection.name)?.set(queryId(selector, options), result);
317
+ }
318
+ return result;
319
+ },
320
+ // lifecycle methods
321
+ dispose: async () => {
322
+ const adapter = this.storageAdapters.get(collection.name);
323
+ if (adapter?.teardown)
324
+ await adapter.teardown();
325
+ this.storageAdapters.delete(collection.name);
326
+ this.items.delete(collection.name);
327
+ this.indices.delete(collection.name);
328
+ this.activeQueries.delete(collection.name);
329
+ this.queryEmitters.delete(collection.name);
330
+ this.queuedQueryUpdates.delete(collection.name);
331
+ this.cachedQueryResults.delete(collection.name);
332
+ },
333
+ isReady: () => persistenceReadyPromise
334
+ };
335
+ return backend;
336
+ }
3
337
  }
4
338
  export {
5
- randomId as default
339
+ DefaultDataAdapter as default
6
340
  };