@signaldb/core 2.0.0-beta.0 → 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 (128) hide show
  1. package/dist/.vite/manifest.json +128 -75
  2. package/dist/AsyncDataAdapter.d.ts +44 -7
  3. package/dist/AutoFetchDataAdapter.d.ts +5 -8
  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 +36 -25
  7. package/dist/Collection/types.d.ts +10 -2
  8. package/dist/DataAdapter.d.ts +18 -11
  9. package/dist/DefaultDataAdapter.d.ts +7 -6
  10. package/dist/WorkerDataAdapter.d.ts +7 -6
  11. package/dist/WorkerDataAdapterHost.d.ts +14 -11
  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 +3 -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 -339
  26. package/dist/index13.cjs.js +45 -0
  27. package/dist/index13.mjs +44 -407
  28. package/dist/index14.cjs.js +86 -0
  29. package/dist/index14.mjs +85 -190
  30. package/dist/index15.cjs.js +15 -0
  31. package/dist/index15.mjs +14 -341
  32. package/dist/index16.cjs.js +136 -0
  33. package/dist/index16.mjs +134 -569
  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 -254
  42. package/dist/index20.cjs.js +37 -0
  43. package/dist/index20.mjs +36 -104
  44. package/dist/index21.cjs.js +35 -0
  45. package/dist/index21.mjs +34 -128
  46. package/dist/index22.cjs.js +25 -0
  47. package/dist/index22.mjs +24 -7
  48. package/dist/index23.cjs.js +19 -0
  49. package/dist/index23.mjs +17 -26
  50. package/dist/index24.cjs.js +342 -0
  51. package/dist/index24.mjs +340 -6
  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 -5
  58. package/dist/index28.cjs.js +90 -0
  59. package/dist/index28.mjs +89 -8
  60. package/dist/index29.cjs.js +424 -0
  61. package/dist/index29.mjs +423 -29
  62. package/dist/index3.cjs.js +19 -0
  63. package/dist/index3.mjs +18 -537
  64. package/dist/index30.cjs.js +72 -0
  65. package/dist/index30.mjs +71 -42
  66. package/dist/index31.cjs.js +191 -0
  67. package/dist/index31.mjs +190 -9
  68. package/dist/index32.cjs.js +21 -0
  69. package/dist/index32.mjs +21 -0
  70. package/dist/index33.cjs.js +336 -0
  71. package/dist/index33.mjs +336 -0
  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/batchOnNextTick.d.ts +16 -0
  90. package/dist/utils/createSignal.d.ts +2 -2
  91. package/dist/utils/deepClone.d.ts +1 -1
  92. package/dist/utils/getMatchingKeys.d.ts +2 -2
  93. package/dist/utils/isFieldExpression.d.ts +1 -1
  94. package/dist/utils/match.d.ts +1 -1
  95. package/dist/utils/modify.d.ts +1 -1
  96. package/dist/utils/queryId.d.ts +2 -2
  97. package/dist/utils/reactiveOrAsync.d.ts +59 -0
  98. package/package.json +3 -3
  99. package/dist/index.cjs10.js +0 -18
  100. package/dist/index.cjs11.js +0 -15
  101. package/dist/index.cjs12.js +0 -339
  102. package/dist/index.cjs13.js +0 -407
  103. package/dist/index.cjs14.js +0 -190
  104. package/dist/index.cjs15.js +0 -341
  105. package/dist/index.cjs16.js +0 -570
  106. package/dist/index.cjs17.js +0 -150
  107. package/dist/index.cjs18.js +0 -26
  108. package/dist/index.cjs19.js +0 -43
  109. package/dist/index.cjs2.js +0 -255
  110. package/dist/index.cjs20.js +0 -104
  111. package/dist/index.cjs21.js +0 -129
  112. package/dist/index.cjs22.js +0 -7
  113. package/dist/index.cjs23.js +0 -27
  114. package/dist/index.cjs24.js +0 -7
  115. package/dist/index.cjs25.js +0 -10
  116. package/dist/index.cjs26.js +0 -29
  117. package/dist/index.cjs27.js +0 -5
  118. package/dist/index.cjs28.js +0 -7
  119. package/dist/index.cjs29.js +0 -29
  120. package/dist/index.cjs3.js +0 -538
  121. package/dist/index.cjs30.js +0 -42
  122. package/dist/index.cjs31.js +0 -9
  123. package/dist/index.cjs4.js +0 -5
  124. package/dist/index.cjs5.js +0 -5
  125. package/dist/index.cjs6.js +0 -29
  126. package/dist/index.cjs7.js +0 -12
  127. package/dist/index.cjs8.js +0 -5
  128. package/dist/index.cjs9.js +0 -133
@@ -1,341 +0,0 @@
1
- "use strict";
2
- const deepClone = require("./index.cjs19.js");
3
- const match = require("./index.cjs22.js");
4
- const modify = require("./index.cjs7.js");
5
- const queryId = require("./index.cjs24.js");
6
- const isEqual = require("./index.cjs6.js");
7
- const getIndexInfo = require("./index.cjs21.js");
8
- const getMatchingKeys = require("./index.cjs26.js");
9
- const sortItems = require("./index.cjs25.js");
10
- const project = require("./index.cjs23.js");
11
- class WorkerDataAdapterHost {
12
- workerContext;
13
- options;
14
- id;
15
- storageAdapters = /* @__PURE__ */ new Map();
16
- storageAdapterReady = /* @__PURE__ */ new Map();
17
- collectionIndices = /* @__PURE__ */ new Map();
18
- queries = /* @__PURE__ */ new Map();
19
- onError = (error) => {
20
- console.error(error);
21
- };
22
- constructor(workerContext, options) {
23
- this.workerContext = workerContext;
24
- this.options = options;
25
- this.id = this.options.id || "default-worker-data-adapter";
26
- if (this.options.onError) {
27
- this.onError = this.options.onError;
28
- }
29
- if (typeof addEventListener === "undefined" || typeof postMessage === "undefined") {
30
- throw new TypeError("WorkerDataAdapterHost can only be used in a Web Worker context");
31
- }
32
- this.workerContext.addEventListener("message", async (event) => {
33
- try {
34
- const { workerId, id, method, args } = event.data;
35
- await this.handleMessage(workerId, id, method, args);
36
- } catch (error) {
37
- this.onError(error);
38
- }
39
- });
40
- this.respond("ready", null, null, "ready");
41
- }
42
- respond(id, data, error = null, type = "response") {
43
- this.workerContext.postMessage({ id, workerId: this.id, type, data, error });
44
- }
45
- async handleMessage(workerId, id, method, args) {
46
- if (workerId !== this.id)
47
- return;
48
- const fn = this[method];
49
- if (typeof fn !== "function") {
50
- this.respond(id, null, new Error(`Method ${method} not found`));
51
- return;
52
- }
53
- try {
54
- const result = await fn.apply(this, args);
55
- this.respond(id, result);
56
- } catch (error) {
57
- this.respond(id, null, error);
58
- }
59
- }
60
- async getIndexInfo(collectionName, selector) {
61
- const storageAdapter = this.storageAdapters.get(collectionName);
62
- if (!storageAdapter)
63
- throw new Error(`No persistence adapter for collection ${collectionName}`);
64
- if (selector != null && Object.keys(selector).length === 1 && "id" in selector && typeof selector.id !== "object") {
65
- return {
66
- matched: true,
67
- ids: [selector.id],
68
- optimizedSelector: {}
69
- };
70
- }
71
- if (selector == null) {
72
- return {
73
- matched: false,
74
- ids: [],
75
- optimizedSelector: {}
76
- };
77
- }
78
- const indices = this.collectionIndices.get(collectionName) ?? [];
79
- return getIndexInfo.default(indices.map((field) => async (flatSelector) => {
80
- if (!Object.hasOwnProperty.call(flatSelector, field)) {
81
- return { matched: false };
82
- }
83
- const index = await storageAdapter.readIndex(field);
84
- const fieldSelector = flatSelector[field];
85
- const filteresForNull = fieldSelector == null || fieldSelector.$exists === false;
86
- const keys = filteresForNull ? { include: null, exclude: [...index.keys()].filter((key) => key != null) } : getMatchingKeys(field, flatSelector);
87
- if (keys.include == null && keys.exclude == null)
88
- return { matched: false };
89
- let includedIds = [];
90
- if (keys.include == null) {
91
- for (const set of index.values()) {
92
- for (const pos of set) {
93
- includedIds.push(pos);
94
- }
95
- }
96
- } else {
97
- for (const key of keys.include) {
98
- const idSet = index.get(key);
99
- if (idSet) {
100
- for (const id of idSet) {
101
- includedIds.push(id);
102
- }
103
- }
104
- }
105
- }
106
- if (keys.exclude != null) {
107
- const excludeIds = /* @__PURE__ */ new Set();
108
- for (const key of keys.exclude) {
109
- const idSet = index.get(key);
110
- if (idSet) {
111
- for (const id of idSet) {
112
- excludeIds.add(id);
113
- }
114
- }
115
- }
116
- includedIds = includedIds.filter((pos) => !excludeIds.has(pos));
117
- }
118
- return {
119
- matched: true,
120
- ids: includedIds,
121
- fields: [field],
122
- keepSelector: filteresForNull
123
- };
124
- }), selector);
125
- }
126
- async queryItems(collectionName, selector) {
127
- const storageAdapter = this.storageAdapters.get(collectionName);
128
- if (!storageAdapter)
129
- throw new Error(`No persistence adapter for collection ${collectionName}`);
130
- const indexInfo = await this.getIndexInfo(collectionName, selector);
131
- const matchItems = (item) => {
132
- if (indexInfo.optimizedSelector == null)
133
- return true;
134
- if (Object.keys(indexInfo.optimizedSelector).length <= 0)
135
- return true;
136
- const matches = match(item, indexInfo.optimizedSelector);
137
- return matches;
138
- };
139
- if (indexInfo.matched) {
140
- const items = await storageAdapter.readIds(indexInfo.ids);
141
- if (isEqual(indexInfo.optimizedSelector, {}))
142
- return items;
143
- return items.filter(matchItems);
144
- } else {
145
- const allItems = await storageAdapter.readAll();
146
- if (isEqual(selector, {}))
147
- return allItems;
148
- return allItems.filter(matchItems);
149
- }
150
- }
151
- async executeQuery(collectionName, selector, options) {
152
- const items = await this.queryItems(collectionName, selector || {});
153
- const { sort, skip, limit, fields } = options || {};
154
- const sorted = sort ? sortItems(items, sort) : items;
155
- const skipped = skip ? sorted.slice(skip) : sorted;
156
- const limited = limit ? skipped.slice(0, limit) : skipped;
157
- const idExcluded = fields && fields.id === 0;
158
- return limited.map((item) => {
159
- if (!fields)
160
- return item;
161
- return {
162
- ...idExcluded ? {} : { id: item.id },
163
- ...project(item, fields)
164
- };
165
- });
166
- }
167
- ensureQuery(collectionName, selector, options) {
168
- const id = queryId(selector, options);
169
- if (!this.queries.get(collectionName)) {
170
- throw new Error(`Collection ${collectionName} not initialized!`);
171
- }
172
- let query = this.queries.get(collectionName)?.get(id);
173
- if (!query) {
174
- query = { selector, options };
175
- this.queries.get(collectionName)?.set(id, query);
176
- }
177
- return query;
178
- }
179
- emitQueryUpdate(collectionName, selector, options, state, error, items) {
180
- const id = queryId(selector, options);
181
- const collectionQueries = this.queries.get(collectionName);
182
- if (!collectionQueries)
183
- throw new Error(`Collection ${collectionName} not initialized!`);
184
- this.respond(id, { collectionName, selector, options, state, error, items }, null, "queryUpdate");
185
- }
186
- ensureStorageAdapter(name) {
187
- if (this.storageAdapters.has(name))
188
- return;
189
- const adapter = this.options.storage(name);
190
- if (!adapter)
191
- return;
192
- this.storageAdapters.set(name, adapter);
193
- }
194
- async checkQueryUpdates(collectionName, items) {
195
- const queries = this.queries.get(collectionName);
196
- if (!queries)
197
- throw new Error(`Collection ${collectionName} not initialized!`);
198
- const affectedQueries = [...queries.values()].filter(({ selector }) => items.some((item) => match(item, selector))) ?? [];
199
- if (affectedQueries.length === 0)
200
- return;
201
- affectedQueries.forEach(({ selector, options }) => {
202
- this.emitQueryUpdate(collectionName, selector, options, "active", null);
203
- });
204
- await Promise.all(affectedQueries.map(async ({ selector, options }) => {
205
- const queryItems = await this.executeQuery(collectionName, selector, options);
206
- this.emitQueryUpdate(collectionName, selector, options, "complete", null, queryItems);
207
- }));
208
- }
209
- registerCollection = async (collectionName, indices) => {
210
- this.collectionIndices.set(collectionName, indices);
211
- this.queries.set(collectionName, /* @__PURE__ */ new Map());
212
- this.ensureStorageAdapter(collectionName);
213
- const storageAdapter = this.storageAdapters.get(collectionName);
214
- if (!storageAdapter)
215
- throw new Error(`No persistence adapter for collection ${collectionName}`);
216
- const setupPromise = (async () => {
217
- await Promise.all([
218
- storageAdapter.createIndex("id"),
219
- ...indices.map((index) => storageAdapter.createIndex(index))
220
- ]);
221
- await storageAdapter.setup();
222
- })();
223
- this.storageAdapterReady.set(collectionName, setupPromise);
224
- await setupPromise;
225
- };
226
- unregisterCollection = async (collectionName) => {
227
- this.storageAdapters.delete(collectionName);
228
- this.queries.delete(collectionName);
229
- };
230
- registerQuery = async (collectionName, selector, options) => {
231
- this.ensureQuery(collectionName, selector, options);
232
- const queryItems = await this.executeQuery(collectionName, selector, options);
233
- this.emitQueryUpdate(collectionName, selector, options, "complete", null, queryItems);
234
- };
235
- unregisterQuery = async (collectionName, selector, options) => {
236
- const id = queryId(selector, options);
237
- if (!this.queries.get(collectionName))
238
- throw new Error(`Collection ${collectionName} not initialized!`);
239
- this.queries.get(collectionName)?.delete(id);
240
- };
241
- insert = async (collectionName, newItem) => {
242
- const storageAdapter = this.storageAdapters.get(collectionName);
243
- if (!storageAdapter)
244
- throw new Error(`No persistence adapter for collection ${collectionName}`);
245
- const existingItems = await this.executeQuery(collectionName, { id: newItem.id }, { limit: 1 });
246
- if (existingItems.length > 0) {
247
- throw new Error(`Item with id ${newItem.id} already exists`);
248
- }
249
- await storageAdapter.insert([newItem]);
250
- await this.checkQueryUpdates(collectionName, [newItem]);
251
- return newItem;
252
- };
253
- updateOne = async (collectionName, selector, modifier) => {
254
- const storageAdapter = this.storageAdapters.get(collectionName);
255
- if (!storageAdapter)
256
- throw new Error(`No persistence adapter for collection ${collectionName}`);
257
- const item = await this.executeQuery(collectionName, selector, { limit: 1 }).then((items) => items[0] ?? null);
258
- const { $setOnInsert, ...restModifier } = modifier;
259
- if (item == null)
260
- return [];
261
- const modifiedItem = modify(deepClone.default(item), restModifier);
262
- if (item.id !== modifiedItem.id) {
263
- const existingItems = await this.executeQuery(collectionName, { id: modifiedItem.id }, { limit: 1 });
264
- if (existingItems.length > 0) {
265
- throw new Error(`Item with id ${modifiedItem.id} already exists`);
266
- }
267
- }
268
- await storageAdapter.replace([modifiedItem]);
269
- await this.checkQueryUpdates(collectionName, [modifiedItem]);
270
- return [modifiedItem];
271
- };
272
- updateMany = async (collectionName, selector, modifier) => {
273
- const storageAdapter = this.storageAdapters.get(collectionName);
274
- if (!storageAdapter)
275
- throw new Error(`No persistence adapter for collection ${collectionName}`);
276
- const items = await this.executeQuery(collectionName, selector);
277
- if (items.length === 0)
278
- return [];
279
- const { $setOnInsert, ...restModifier } = modifier;
280
- const changedItems = await Promise.all(items.map(async (item) => {
281
- const modifiedItem = modify(deepClone.default(item), restModifier);
282
- if (item.id !== modifiedItem.id) {
283
- const existingItems = await this.executeQuery(collectionName, { id: modifiedItem.id }, { limit: 1 });
284
- if (existingItems.length > 0) {
285
- throw new Error(`Item with id ${modifiedItem.id} already exists`);
286
- }
287
- }
288
- return modifiedItem;
289
- }));
290
- await storageAdapter.replace(changedItems);
291
- await this.checkQueryUpdates(collectionName, changedItems);
292
- return changedItems;
293
- };
294
- replaceOne = async (collectionName, selector, replacement) => {
295
- const storageAdapter = this.storageAdapters.get(collectionName);
296
- if (!storageAdapter)
297
- throw new Error(`No persistence adapter for collection ${collectionName}`);
298
- const item = await this.executeQuery(collectionName, selector, { limit: 1 }).then((items) => items[0] ?? null);
299
- if (item == null)
300
- return [];
301
- const modifiedItem = {
302
- ...replacement,
303
- id: replacement.id ?? item.id
304
- };
305
- if (item.id !== modifiedItem.id) {
306
- const existingItems = await this.executeQuery(collectionName, { id: modifiedItem.id }, { limit: 1 });
307
- if (existingItems.length > 0) {
308
- throw new Error(`Item with id ${modifiedItem.id} already exists`);
309
- }
310
- }
311
- await storageAdapter.replace([modifiedItem]);
312
- await this.checkQueryUpdates(collectionName, [modifiedItem]);
313
- return [modifiedItem];
314
- };
315
- removeOne = async (collectionName, selector) => {
316
- const storageAdapter = this.storageAdapters.get(collectionName);
317
- if (!storageAdapter)
318
- throw new Error(`No persistence adapter for collection ${collectionName}`);
319
- const item = await this.executeQuery(collectionName, selector, { limit: 1 }).then((items) => items[0] ?? null);
320
- if (item == null)
321
- return [];
322
- await storageAdapter.remove([item]);
323
- await this.checkQueryUpdates(collectionName, [item]);
324
- return [item];
325
- };
326
- removeMany = async (collectionName, selector) => {
327
- const storageAdapter = this.storageAdapters.get(collectionName);
328
- if (!storageAdapter)
329
- throw new Error(`No persistence adapter for collection ${collectionName}`);
330
- const items = await this.executeQuery(collectionName, selector);
331
- if (items.length === 0)
332
- return [];
333
- await storageAdapter.remove(items);
334
- await this.checkQueryUpdates(collectionName, items);
335
- return items;
336
- };
337
- isReady = async (collectionName) => {
338
- return this.storageAdapterReady.get(collectionName);
339
- };
340
- }
341
- module.exports = WorkerDataAdapterHost;