@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
package/dist/index14.mjs CHANGED
@@ -1,207 +1,45 @@
1
- import queryId from "./index20.mjs";
2
- import randomId from "./index8.mjs";
3
- import batchOnNextTick from "./index27.mjs";
4
- class WorkerDataAdapter {
5
- worker;
6
- options;
7
- id;
8
- isDisposed = false;
9
- workerReady;
10
- log = () => {
11
- };
12
- collectionReady = /* @__PURE__ */ new Map();
13
- batchExecutionHelpers = /* @__PURE__ */ new Map();
14
- queries = {};
15
- constructor(worker, options) {
16
- this.worker = worker;
17
- this.options = options;
18
- this.id = this.options.id || "default-worker-data-adapter";
19
- if (this.options.log)
20
- this.log = this.options.log;
21
- this.workerReady = new Promise((resolve, reject) => {
22
- const timeoutId = setTimeout(() => {
23
- reject(new Error("WorkerDataAdapter initialization timed out"));
24
- }, 5e3);
25
- const handleMessage = (event) => {
26
- const { type, workerId } = event.data;
27
- if (workerId !== this.id)
28
- return;
29
- if (type === "ready") {
30
- resolve();
31
- clearTimeout(timeoutId);
32
- this.worker.removeEventListener("message", handleMessage);
33
- }
34
- };
35
- this.worker.addEventListener("message", handleMessage);
36
- });
37
- }
38
- async exec(method, collectionName, ...args) {
39
- await this.workerReady;
40
- if (method !== "isReady") {
41
- const collectionReady = this.collectionReady.get(collectionName);
42
- if (!collectionReady)
43
- throw new Error(`Collection "${collectionName}" is not registered in WorkerDataAdapter`);
44
- await collectionReady;
45
- }
46
- if (this.isDisposed) {
47
- throw new Error("WorkerDataAdapter is disposed");
48
- }
49
- return new Promise((resolve, reject) => {
50
- const messageId = randomId();
51
- const handleMessage = (event) => {
52
- const { id, workerId, type, data, error } = event.data;
53
- if (workerId !== this.id)
54
- return;
55
- if (type !== "response")
56
- return;
57
- if (id !== messageId)
58
- return;
59
- this.log(method, "result", data ?? error);
60
- if (error) {
61
- reject(error);
62
- } else {
63
- resolve(data);
64
- }
65
- this.worker.removeEventListener("message", handleMessage);
66
- };
67
- this.worker.addEventListener("message", handleMessage);
68
- this.worker.postMessage({
69
- id: messageId,
70
- workerId: this.id,
71
- method,
72
- args: [collectionName, ...args]
73
- });
74
- });
75
- }
76
- enqueueBatched(collectionName, method, args) {
77
- const helper = this.batchExecutionHelpers.get(collectionName);
78
- if (!helper)
79
- throw new Error(`Collection "${collectionName}" is not registered in WorkerDataAdapter`);
80
- return helper.enqueue(method, args);
81
- }
82
- // ---------- end batching integration ----------
83
- updateQuery(collectionName, query, update) {
84
- const id = queryId(query.selector, query.options);
85
- const collectionQueries = this.queries[collectionName];
86
- if (!collectionQueries)
87
- return;
88
- const existing = collectionQueries.get(id);
89
- const newState = {
90
- state: "active",
91
- error: null,
92
- items: [],
93
- stateChangeCallbacks: [],
94
- eventHandler: existing?.eventHandler,
95
- ...existing,
96
- ...update
97
- };
98
- collectionQueries.set(id, newState);
99
- this.queries[collectionName] = collectionQueries;
100
- }
101
- createCollectionBackend(collection, indices) {
102
- this.queries[collection.name] = /* @__PURE__ */ new Map();
103
- void this.exec("registerCollection", collection.name, indices);
104
- this.collectionReady.set(collection.name, this.exec("isReady", collection.name));
105
- this.batchExecutionHelpers.set(collection.name, batchOnNextTick(async (method, args) => this.exec(method, collection.name, args)));
106
- return {
107
- insert: async (item) => {
108
- return this.enqueueBatched(collection.name, "insert", [item]);
109
- },
110
- updateOne: async (selector, modifier) => {
111
- return this.enqueueBatched(collection.name, "updateOne", [selector, modifier]);
112
- },
113
- updateMany: async (selector, modifier) => {
114
- return this.enqueueBatched(collection.name, "updateMany", [selector, modifier]);
115
- },
116
- replaceOne: async (selector, replacement) => {
117
- return this.enqueueBatched(collection.name, "replaceOne", [selector, replacement]);
118
- },
119
- removeOne: async (selector) => {
120
- return this.enqueueBatched(collection.name, "removeOne", [selector]);
121
- },
122
- removeMany: async (selector) => {
123
- return this.enqueueBatched(collection.name, "removeMany", [selector]);
124
- },
125
- // methods for registering and unregistering queries that will be called from the collection during find/findOne
126
- registerQuery: (selector, options) => {
127
- this.updateQuery(collection.name, { selector, options }, { state: "active", error: null, items: [] });
128
- void this.exec("registerQuery", collection.name, selector, options);
129
- const handler = (event) => {
130
- const { type, data, workerId, error } = event.data;
131
- if (type !== "queryUpdate")
132
- return;
133
- if (data == null)
134
- return;
135
- const { collectionName, selector: responseSelector, options: responseOptions, state, items } = data;
136
- if (workerId !== this.id)
137
- return;
138
- if (collectionName !== collection.name)
139
- return;
140
- if (queryId(responseSelector, responseOptions) !== queryId(selector, options))
141
- return;
142
- this.log("queryUpdate", responseSelector, responseOptions, state, data ?? error);
143
- this.updateQuery(collection.name, {
144
- selector: responseSelector,
145
- options: responseOptions
146
- }, { state, error, items });
147
- const query = this.queries[collection.name]?.get(queryId(selector, options));
148
- if (!query)
149
- return;
150
- query.stateChangeCallbacks.forEach((callback) => callback(state));
151
- };
152
- this.worker.addEventListener("message", handler);
153
- this.updateQuery(collection.name, { selector, options }, { eventHandler: handler });
154
- },
155
- unregisterQuery: (selector, options) => {
156
- const qid = queryId(selector, options);
157
- const query = this.queries[collection.name]?.get(qid);
158
- if (query?.eventHandler) {
159
- this.worker.removeEventListener("message", query.eventHandler);
160
- }
161
- this.queries[collection.name]?.delete(qid);
162
- void this.exec("unregisterQuery", collection.name, selector, options);
163
- },
164
- getQueryState: (selector, options) => {
165
- const query = this.queries[collection.name]?.get(queryId(selector, options));
166
- return query?.state || "active";
167
- },
168
- getQueryError: (selector, options) => {
169
- const query = this.queries[collection.name]?.get(queryId(selector, options));
170
- return query?.error || null;
171
- },
172
- getQueryResult: (selector, options) => {
173
- const query = this.queries[collection.name]?.get(queryId(selector, options));
174
- return query?.items || [];
175
- },
176
- onQueryStateChange: (selector, options, callback) => {
177
- this.updateQuery(collection.name, { selector, options }, {
178
- stateChangeCallbacks: [
179
- ...this.queries[collection.name]?.get(queryId(selector, options))?.stateChangeCallbacks || [],
180
- callback
181
- ]
182
- });
183
- return () => {
184
- const currentCallbacks = this.queries[collection.name]?.get(queryId(selector, options))?.stateChangeCallbacks;
185
- if (!currentCallbacks)
186
- throw new Error("State change callbacks are not defined!");
187
- this.updateQuery(collection.name, { selector, options }, {
188
- stateChangeCallbacks: currentCallbacks.filter((existingCallback) => existingCallback !== callback)
189
- });
190
- };
191
- },
192
- executeQuery: (selector, options) => this.exec("executeQuery", collection.name, selector, options),
193
- // lifecycle methods
194
- dispose: async () => {
195
- await this.exec("unregisterCollection", collection.name);
196
- this.isDisposed = true;
197
- this.worker.terminate();
198
- },
199
- isReady: async () => {
200
- await this.exec("isReady", collection.name);
201
- }
202
- };
203
- }
1
+ import isFieldExpression from "./index12.mjs";
2
+ import serializeValue from "./index13.mjs";
3
+ //#region src/utils/getMatchingKeys.ts
4
+ /**
5
+ * Extracts the matching and excluded keys for a given field in a selector.
6
+ * Supports serialized values and `$in`/`$nin` field expressions for optimization.
7
+ * Returns `null` for include/exclude if the field cannot be optimized.
8
+ * @template T - The type of the items in the selector.
9
+ * @template I - The type of the unique identifier for the items.
10
+ * @param field - The name of the field to extract matching keys for.
11
+ * @param selector - The selector object containing query criteria.
12
+ * @returns An object containing arrays of serialized included and excluded keys,
13
+ * or `null` if the field cannot be optimized.
14
+ */
15
+ function getMatchingKeys(field, selector) {
16
+ const result = {
17
+ include: null,
18
+ exclude: null
19
+ };
20
+ const fieldSelector = selector[field];
21
+ if (fieldSelector instanceof RegExp) return result;
22
+ if (fieldSelector == null) return result;
23
+ if (isFieldExpression(fieldSelector)) {
24
+ if (fieldSelector.$ne != null) {
25
+ result.exclude = [serializeValue(fieldSelector.$ne)];
26
+ return result;
27
+ }
28
+ if (Array.isArray(fieldSelector.$in) && fieldSelector.$in.length > 0) {
29
+ result.include = fieldSelector.$in.map(serializeValue);
30
+ return result;
31
+ }
32
+ if (Array.isArray(fieldSelector.$nin) && fieldSelector.$nin.length > 0) {
33
+ result.exclude = fieldSelector.$nin.map(serializeValue);
34
+ return result;
35
+ }
36
+ return {
37
+ include: null,
38
+ exclude: null
39
+ };
40
+ }
41
+ result.include = [serializeValue(fieldSelector)];
42
+ return result;
204
43
  }
205
- export {
206
- WorkerDataAdapter as default
207
- };
44
+ //#endregion
45
+ export { getMatchingKeys as default };
@@ -0,0 +1,86 @@
1
+ const require_createIndexProvider = require("./index10.cjs.js");
2
+ const require_get = require("./index11.cjs.js");
3
+ const require_serializeValue = require("./index13.cjs.js");
4
+ const require_getMatchingKeys = require("./index14.cjs.js");
5
+ //#region src/createIndex.ts
6
+ /**
7
+ * creates an index for a specific field
8
+ * @param field name of the field
9
+ * @returns an index provider to pass to the `indices` option of the collection constructor
10
+ */
11
+ function createIndex(field) {
12
+ const index = /* @__PURE__ */ new Map();
13
+ const ensureSet = (key) => {
14
+ let set = index.get(key);
15
+ if (!set) {
16
+ set = /* @__PURE__ */ new Set();
17
+ index.set(key, set);
18
+ }
19
+ return set;
20
+ };
21
+ return require_createIndexProvider.default({
22
+ query(selector) {
23
+ if (!Object.hasOwnProperty.call(selector, field)) return { matched: false };
24
+ const fieldSelector = selector[field];
25
+ const filteresForNull = fieldSelector == null || fieldSelector.$exists === false;
26
+ const keys = filteresForNull ? {
27
+ include: null,
28
+ exclude: [...index.keys()].filter((key) => key != null)
29
+ } : require_getMatchingKeys.default(field, selector);
30
+ if (keys.include == null && keys.exclude == null) return { matched: false };
31
+ let includedIds = [];
32
+ if (keys.include == null) for (const set of index.values()) for (const pos of set) includedIds.push(pos);
33
+ else for (const key of keys.include) {
34
+ const posSet = index.get(key);
35
+ if (posSet) for (const pos of posSet) includedIds.push(pos);
36
+ }
37
+ if (keys.exclude != null) {
38
+ const excludeIds = /* @__PURE__ */ new Set();
39
+ for (const key of keys.exclude) {
40
+ const posSet = index.get(key);
41
+ if (posSet) for (const pos of posSet) excludeIds.add(pos);
42
+ }
43
+ includedIds = includedIds.filter((pos) => !excludeIds.has(pos));
44
+ }
45
+ return {
46
+ matched: true,
47
+ ids: includedIds,
48
+ fields: [field],
49
+ keepSelector: filteresForNull
50
+ };
51
+ },
52
+ rebuild(items) {
53
+ index.clear();
54
+ items.forEach((item) => {
55
+ ensureSet(require_serializeValue.default(require_get.default(item, field))).add(item.id);
56
+ });
57
+ },
58
+ insert(items) {
59
+ for (const item of items) ensureSet(require_serializeValue.default(require_get.default(item, field))).add(item.id);
60
+ },
61
+ remove(items) {
62
+ for (const item of items) {
63
+ const value = require_serializeValue.default(require_get.default(item, field));
64
+ const set = index.get(value);
65
+ if (!set) continue;
66
+ set.delete(item.id);
67
+ if (set.size === 0) index.delete(value);
68
+ }
69
+ },
70
+ update(pairs) {
71
+ for (const { oldItem, newItem } of pairs) {
72
+ const oldValue = require_serializeValue.default(require_get.default(oldItem, field));
73
+ const newValue = require_serializeValue.default(require_get.default(newItem, field));
74
+ if (oldValue === newValue) continue;
75
+ const oldSet = index.get(oldValue);
76
+ if (oldSet) {
77
+ oldSet.delete(oldItem.id);
78
+ if (oldSet.size === 0) index.delete(oldValue);
79
+ }
80
+ ensureSet(newValue).add(newItem.id);
81
+ }
82
+ }
83
+ });
84
+ }
85
+ //#endregion
86
+ exports.default = createIndex;