@signaldb/core 2.0.0-beta.1 → 2.0.0-beta.11

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