@signaldb/core 2.0.0-beta.2 → 2.0.0-beta.21

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