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