@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/index2.mjs CHANGED
@@ -1,259 +1,33 @@
1
- import Observer from "./index17.mjs";
2
- function isInReactiveScope(reactivity) {
3
- if (!reactivity)
4
- return false;
5
- if (!reactivity.isInScope)
6
- return true;
7
- return reactivity.isInScope();
1
+ //#region src/utils/isEqual.ts
2
+ /**
3
+ * Compares two values for deep equality.
4
+ * @param a - The first value to compare.
5
+ * @param b - The second value to compare.
6
+ * @returns - Returns `true` if the two values are deeply equal, otherwise `false`.
7
+ * @example
8
+ * isEqual({ a: 1 }, { a: 1 }); // true
9
+ * isEqual([1, 2], [1, 2]); // true
10
+ * isEqual(new Date(0), new Date(0)); // true
11
+ * isEqual(/abc/, /abc/); // true
12
+ * isEqual({ a: 1 }, { a: 2 }); // false
13
+ * isEqual(null, null); // true
14
+ */
15
+ function isEqual(a, b) {
16
+ if (Object.is(a, b)) return true;
17
+ if (a instanceof RegExp && b instanceof RegExp) return a.toString() === b.toString();
18
+ if (a instanceof Date && b instanceof Date) return a.getTime() === b.getTime();
19
+ if (typeof a !== "object") return false;
20
+ if (typeof b !== "object") return false;
21
+ if (a === null) return false;
22
+ if (b === null) return false;
23
+ const aKeys = Object.keys(a);
24
+ const bKeys = Object.keys(b);
25
+ if (aKeys.length !== bKeys.length) return false;
26
+ for (const key of aKeys) {
27
+ if (!bKeys.includes(key)) return false;
28
+ if (!isEqual(a[key], b[key])) return false;
29
+ }
30
+ return true;
8
31
  }
9
- class Cursor {
10
- observer;
11
- getItems;
12
- options;
13
- onCleanupCallbacks = [];
14
- /**
15
- * Creates a new instance of the `Cursor` class.
16
- * Provides utilities for querying, observing, and transforming items from a collection.
17
- * @template T - The type of the items in the collection.
18
- * @template U - The transformed item type after applying transformations (default is T).
19
- * @param getItems - A function that retrieves the filtered list of items.
20
- * @param options - Optional configuration for the cursor.
21
- * @param options.transform - A transformation function to apply to each item when retrieving them.
22
- * @param options.bindEvents - A function to bind reactivity events for the cursor, which should return a cleanup function.
23
- * @param options.fields - A projection object defining which fields of the item should be included or excluded.
24
- * @param options.sort - A sort specifier to determine the order of the items.
25
- * @param options.skip - The number of items to skip from the beginning of the result set.
26
- * @param options.limit - The maximum number of items to return in the result set.
27
- * @param options.reactive - A reactivity adapter to enable observing changes in the cursor's result set.
28
- * @param options.fieldTracking - A boolean to enable fine-grained field tracking for reactivity.
29
- * @param options.transformAll - A function that will be able to solve the n+1 problem
30
- */
31
- constructor(getItems, options) {
32
- this.getItems = getItems;
33
- this.options = options || {};
34
- }
35
- addGetters(item) {
36
- if (!isInReactiveScope(this.options.reactive))
37
- return item;
38
- const depend = this.depend.bind(this);
39
- return Object.entries(item).reduce((memo, [key, value]) => {
40
- Object.defineProperty(memo, key, {
41
- get() {
42
- depend({
43
- changedField: (notify) => (changedItem, changedFieldName) => {
44
- if (changedFieldName !== key || changedItem.id !== item.id)
45
- return;
46
- notify();
47
- }
48
- });
49
- return value;
50
- },
51
- enumerable: true,
52
- configurable: true
53
- });
54
- return memo;
55
- }, {});
56
- }
57
- transform(rawItem) {
58
- const item = this.options.fieldTracking ? this.addGetters(rawItem) : rawItem;
59
- if (!this.options.transform)
60
- return item;
61
- return this.options.transform(item);
62
- }
63
- depend(changeEvents) {
64
- if (this.options?.async)
65
- return;
66
- if (!isInReactiveScope(this.options.reactive)) {
67
- console.warn("Cursor.depend() called outside of a reactive scope without async option; consider using { async: true } or wrapping in a reactive scope");
68
- }
69
- if (!this.options.reactive)
70
- return;
71
- const signal = this.options.reactive.create();
72
- signal.depend();
73
- const notify = () => signal.notify();
74
- function buildNotifier(event) {
75
- const eventHandler = changeEvents[event];
76
- return (...args) => {
77
- if (eventHandler === true) {
78
- notify();
79
- return;
80
- }
81
- if (typeof eventHandler !== "function")
82
- return;
83
- eventHandler(notify)(...args);
84
- };
85
- }
86
- const stop = this.observeRawChanges({
87
- added: buildNotifier("added"),
88
- addedBefore: buildNotifier("addedBefore"),
89
- changed: buildNotifier("changed"),
90
- changedField: buildNotifier("changedField"),
91
- movedBefore: buildNotifier("movedBefore"),
92
- removed: buildNotifier("removed")
93
- }, true);
94
- if (this.options.reactive.onDispose) {
95
- this.options.reactive.onDispose(() => stop(), signal);
96
- }
97
- this.onCleanup(stop);
98
- }
99
- ensureObserver() {
100
- if (!this.observer) {
101
- const observer = new Observer(() => {
102
- const requery = () => {
103
- observer.runChecks(this.getItems);
104
- };
105
- const cleanup = this.options.bindEvents && this.options.bindEvents(requery);
106
- return () => {
107
- if (cleanup)
108
- cleanup();
109
- };
110
- });
111
- this.onCleanup(() => observer.stop());
112
- this.observer = observer;
113
- }
114
- return this.observer;
115
- }
116
- observeRawChanges(callbacks, skipInitial = false) {
117
- const observer = this.ensureObserver();
118
- observer.addCallbacks(callbacks, skipInitial);
119
- observer.runChecks(this.getItems);
120
- return () => {
121
- observer.removeCallbacks(callbacks);
122
- if (!observer.isEmpty())
123
- return;
124
- observer.stop();
125
- this.observer = void 0;
126
- };
127
- }
128
- /**
129
- * Cleans up all resources associated with the cursor, such as reactive bindings
130
- * and event listeners. This method should be called when the cursor is no longer needed
131
- * to prevent memory leaks.
132
- */
133
- cleanup() {
134
- this.onCleanupCallbacks.forEach((callback) => {
135
- callback();
136
- });
137
- this.onCleanupCallbacks = [];
138
- }
139
- /**
140
- * Registers a cleanup callback to be executed when the `cleanup` method is called.
141
- * Useful for managing resources and ensuring proper cleanup of bindings or listeners.
142
- * @param callback - A function to be executed during cleanup.
143
- */
144
- onCleanup(callback) {
145
- this.onCleanupCallbacks.push(callback);
146
- }
147
- /**
148
- * Iterates over each item in the cursor's result set, applying the provided callback
149
- * function to each transformed item.
150
- * ⚡️ this function is reactive!
151
- * @param callback - A function to execute for each item in the result set.
152
- * @param callback.item - The transformed item.
153
- * @returns A promise that resolves when all items have been processed, or void if not in async mode.
154
- */
155
- forEach(callback) {
156
- this.depend({
157
- addedBefore: true,
158
- removed: true,
159
- movedBefore: true,
160
- ...this.options.fieldTracking ? {} : { changed: true }
161
- });
162
- const executeForEach = (items) => {
163
- items.forEach((item) => {
164
- callback(this.transform(item));
165
- });
166
- };
167
- const result = this.getItems();
168
- if (result instanceof Promise) {
169
- return result.then(executeForEach);
170
- } else {
171
- executeForEach(result);
172
- return void 0;
173
- }
174
- }
175
- /**
176
- * Creates a new array populated with the results of applying the provided callback
177
- * function to each transformed item in the cursor's result set.
178
- * ⚡️ this function is reactive!
179
- * @template V - The type of the items in the resulting array.
180
- * @param callback - A function to execute for each item in the result set.
181
- * @param callback.item - The transformed item.
182
- * @returns An array of results after applying the callback to each item.
183
- */
184
- map(callback) {
185
- const results = [];
186
- const maybePromise = this.forEach((item) => {
187
- results.push(callback(item));
188
- });
189
- if (maybePromise instanceof Promise) {
190
- return maybePromise.then(() => results);
191
- }
192
- return results;
193
- }
194
- /**
195
- * Fetches all transformed items from the cursor's result set as an array.
196
- * Automatically applies filtering, sorting, and limiting as per the cursor's options.
197
- * ⚡️ this function is reactive!
198
- * @returns An array of transformed items in the result set.
199
- */
200
- fetch() {
201
- return this.map((item) => item);
202
- }
203
- /**
204
- * Counts the total number of items in the cursor's result set after applying
205
- * filtering and other criteria.
206
- * ⚡️ this function is reactive!
207
- * @returns The total number of items in the result set.
208
- */
209
- count() {
210
- this.depend({
211
- added: true,
212
- removed: true
213
- });
214
- const maybePromise = this.getItems();
215
- return maybePromise instanceof Promise ? maybePromise.then((items) => items.length) : maybePromise.length;
216
- }
217
- /**
218
- * Observes changes to the cursor's result set and triggers the specified callbacks
219
- * when items are added, removed, or updated. Supports reactivity and transformation.
220
- * @param callbacks - An object containing the callback functions to handle different change events.
221
- * @param callbacks.added - Triggered when an item is added to the result set.
222
- * @param callbacks.removed - Triggered when an item is removed from the result set.
223
- * @param callbacks.changed - Triggered when an item in the result set is modified.
224
- * @param callbacks.addedBefore - Triggered when an item is added before another item in the result set.
225
- * @param callbacks.movedBefore - Triggered when an item is moved before another item in the result set.
226
- * @param callbacks.changedField - Triggered when a specific field of an item changes.
227
- * @param skipInitial - A boolean indicating whether to skip the initial notification of the current result set.
228
- * @returns A function to stop observing changes.
229
- */
230
- observeChanges(callbacks, skipInitial = false) {
231
- return this.observeRawChanges(Object.entries(callbacks).reduce((memo, [callbackName, callback]) => {
232
- if (!callback)
233
- return memo;
234
- return {
235
- ...memo,
236
- [callbackName]: (item, before) => {
237
- const transformedValue = this.transform(item);
238
- const hasBeforeParameter = before !== void 0;
239
- const transformedBeforeValue = hasBeforeParameter && before ? this.transform(before) : null;
240
- return callback(transformedValue, ...hasBeforeParameter ? [transformedBeforeValue] : []);
241
- }
242
- };
243
- }, {}), skipInitial);
244
- }
245
- /**
246
- * Forces the cursor to re-evaluate its result set by re-fetching items
247
- * from the collection. This is useful when the underlying data or query
248
- * criteria have changed, and you want to ensure the cursor reflects the latest state.
249
- */
250
- requery() {
251
- if (!this.observer)
252
- return;
253
- this.observer.runChecks(this.getItems);
254
- }
255
- }
256
- export {
257
- Cursor as default,
258
- isInReactiveScope
259
- };
32
+ //#endregion
33
+ export { isEqual as default };
@@ -0,0 +1,23 @@
1
+ const require_deepClone = require("./index18.cjs.js");
2
+ let mingo = require("mingo");
3
+ //#region src/utils/modify.ts
4
+ /**
5
+ * Applies a modifier to an object and returns a new modified object.
6
+ * @template T - The type of the object to be modified.
7
+ * @param item - The object to be modified.
8
+ * @param modifier - The modifier to apply. This can be any transformation logic.
9
+ * @returns - Returns a new object with the modifications applied.
10
+ * @example
11
+ * const item = { a: 1, b: 2 }
12
+ * const modifier = { $set: { b: 3, c: 4 } }
13
+ * const result = modify(item, modifier)
14
+ * // result: { a: 1, b: 3, c: 4 }
15
+ */
16
+ function modify(item, modifier) {
17
+ if (!Object.keys(modifier).some((key) => key.startsWith("$"))) return modifier;
18
+ const clonedItem = require_deepClone.default(item);
19
+ (0, mingo.update)(clonedItem, modifier);
20
+ return clonedItem;
21
+ }
22
+ //#endregion
23
+ exports.default = modify;
package/dist/index20.mjs CHANGED
@@ -1,17 +1,23 @@
1
- function isEmptyOptions(options) {
2
- if (options == null)
3
- return true;
4
- if (typeof options !== "object")
5
- return false;
6
- if (Array.isArray(options))
7
- return false;
8
- return Object.keys(options).length === 0;
1
+ import deepClone from "./index18.mjs";
2
+ import { update } from "mingo";
3
+ //#region src/utils/modify.ts
4
+ /**
5
+ * Applies a modifier to an object and returns a new modified object.
6
+ * @template T - The type of the object to be modified.
7
+ * @param item - The object to be modified.
8
+ * @param modifier - The modifier to apply. This can be any transformation logic.
9
+ * @returns - Returns a new object with the modifications applied.
10
+ * @example
11
+ * const item = { a: 1, b: 2 }
12
+ * const modifier = { $set: { b: 3, c: 4 } }
13
+ * const result = modify(item, modifier)
14
+ * // result: { a: 1, b: 3, c: 4 }
15
+ */
16
+ function modify(item, modifier) {
17
+ if (!Object.keys(modifier).some((key) => key.startsWith("$"))) return modifier;
18
+ const clonedItem = deepClone(item);
19
+ update(clonedItem, modifier);
20
+ return clonedItem;
9
21
  }
10
- function queryId(selector, options) {
11
- const selectorId = JSON.stringify(selector);
12
- const optionsId = isEmptyOptions(options) ? -1 : JSON.stringify(options);
13
- return `${selectorId}:${optionsId}`;
14
- }
15
- export {
16
- queryId as default
17
- };
22
+ //#endregion
23
+ export { modify as default };
@@ -0,0 +1,37 @@
1
+ //#region src/utils/set.ts
2
+ /**
3
+ * Sets a value at a specified path within an object. Creates nested structures
4
+ * (arrays or objects) as needed to set the value at the correct location. Supports
5
+ * deleting the key if the value is `undefined` and the `deleteIfUndefined` flag is set to `true`.
6
+ * @template T - The type of the object to modify.
7
+ * @template K - The type of the value to set.
8
+ * @param object - The object to modify. The object is mutated directly.
9
+ * @param path - The path (dot or bracket notation) where the value should be set.
10
+ * @param value - The value to set at the specified path.
11
+ * @param deleteIfUndefined - A boolean indicating whether to delete the key if the value is `undefined` (default: `false`).
12
+ * @returns The modified object.
13
+ */
14
+ function set(object, path, value, deleteIfUndefined = false) {
15
+ if (object == null) return object;
16
+ const segments = path.split(/[.[\]]/g);
17
+ if (segments[0] === "") segments.shift();
18
+ if (segments.at(-1) === "") segments.pop();
19
+ const apply = (node) => {
20
+ if (segments.length > 1) {
21
+ const key = segments.shift();
22
+ const nextIsNumber = !Number.isNaN(Number.parseInt(segments[0], 10));
23
+ if (node[key] === void 0) node[key] = nextIsNumber ? [] : {};
24
+ apply(node[key]);
25
+ } else {
26
+ if (deleteIfUndefined && value === void 0) {
27
+ delete node[segments[0]];
28
+ return;
29
+ }
30
+ node[segments[0]] = value;
31
+ }
32
+ };
33
+ apply(object);
34
+ return object;
35
+ }
36
+ //#endregion
37
+ exports.default = set;
package/dist/index21.mjs CHANGED
@@ -1,105 +1,37 @@
1
- import createIndexProvider from "./index33.mjs";
2
- import get from "./index10.mjs";
3
- import getMatchingKeys from "./index26.mjs";
4
- import serializeValue from "./index11.mjs";
5
- function createIndex(field) {
6
- const index = /* @__PURE__ */ new Map();
7
- const ensureSet = (key) => {
8
- let set = index.get(key);
9
- if (!set) {
10
- set = /* @__PURE__ */ new Set();
11
- index.set(key, set);
12
- }
13
- return set;
14
- };
15
- return createIndexProvider({
16
- query(selector) {
17
- if (!Object.hasOwnProperty.call(selector, field)) {
18
- return { matched: false };
19
- }
20
- const fieldSelector = selector[field];
21
- const filteresForNull = fieldSelector == null || fieldSelector.$exists === false;
22
- const keys = filteresForNull ? { include: null, exclude: [...index.keys()].filter((key) => key != null) } : getMatchingKeys(field, selector);
23
- if (keys.include == null && keys.exclude == null)
24
- return { matched: false };
25
- let includedIds = [];
26
- if (keys.include == null) {
27
- for (const set of index.values()) {
28
- for (const pos of set) {
29
- includedIds.push(pos);
30
- }
31
- }
32
- } else {
33
- for (const key of keys.include) {
34
- const posSet = index.get(key);
35
- if (posSet) {
36
- for (const pos of posSet) {
37
- includedIds.push(pos);
38
- }
39
- }
40
- }
41
- }
42
- if (keys.exclude != null) {
43
- const excludeIds = /* @__PURE__ */ new Set();
44
- for (const key of keys.exclude) {
45
- const posSet = index.get(key);
46
- if (posSet) {
47
- for (const pos of posSet) {
48
- excludeIds.add(pos);
49
- }
50
- }
51
- }
52
- includedIds = includedIds.filter((pos) => !excludeIds.has(pos));
53
- }
54
- return {
55
- matched: true,
56
- ids: includedIds,
57
- fields: [field],
58
- keepSelector: filteresForNull
59
- };
60
- },
61
- rebuild(items) {
62
- index.clear();
63
- items.forEach((item) => {
64
- const value = serializeValue(get(item, field));
65
- ensureSet(value).add(item.id);
66
- });
67
- },
68
- // NEW: delta methods
69
- insert(items) {
70
- for (const item of items) {
71
- const value = serializeValue(get(item, field));
72
- ensureSet(value).add(item.id);
73
- }
74
- },
75
- remove(items) {
76
- for (const item of items) {
77
- const value = serializeValue(get(item, field));
78
- const set = index.get(value);
79
- if (!set)
80
- continue;
81
- set.delete(item.id);
82
- if (set.size === 0)
83
- index.delete(value);
84
- }
85
- },
86
- update(pairs) {
87
- for (const { oldItem, newItem } of pairs) {
88
- const oldValue = serializeValue(get(oldItem, field));
89
- const newValue = serializeValue(get(newItem, field));
90
- if (oldValue === newValue)
91
- continue;
92
- const oldSet = index.get(oldValue);
93
- if (oldSet) {
94
- oldSet.delete(oldItem.id);
95
- if (oldSet.size === 0)
96
- index.delete(oldValue);
97
- }
98
- ensureSet(newValue).add(newItem.id);
99
- }
100
- }
101
- });
1
+ //#region src/utils/set.ts
2
+ /**
3
+ * Sets a value at a specified path within an object. Creates nested structures
4
+ * (arrays or objects) as needed to set the value at the correct location. Supports
5
+ * deleting the key if the value is `undefined` and the `deleteIfUndefined` flag is set to `true`.
6
+ * @template T - The type of the object to modify.
7
+ * @template K - The type of the value to set.
8
+ * @param object - The object to modify. The object is mutated directly.
9
+ * @param path - The path (dot or bracket notation) where the value should be set.
10
+ * @param value - The value to set at the specified path.
11
+ * @param deleteIfUndefined - A boolean indicating whether to delete the key if the value is `undefined` (default: `false`).
12
+ * @returns The modified object.
13
+ */
14
+ function set(object, path, value, deleteIfUndefined = false) {
15
+ if (object == null) return object;
16
+ const segments = path.split(/[.[\]]/g);
17
+ if (segments[0] === "") segments.shift();
18
+ if (segments.at(-1) === "") segments.pop();
19
+ const apply = (node) => {
20
+ if (segments.length > 1) {
21
+ const key = segments.shift();
22
+ const nextIsNumber = !Number.isNaN(Number.parseInt(segments[0], 10));
23
+ if (node[key] === void 0) node[key] = nextIsNumber ? [] : {};
24
+ apply(node[key]);
25
+ } else {
26
+ if (deleteIfUndefined && value === void 0) {
27
+ delete node[segments[0]];
28
+ return;
29
+ }
30
+ node[segments[0]] = value;
31
+ }
32
+ };
33
+ apply(object);
34
+ return object;
102
35
  }
103
- export {
104
- createIndex as default
105
- };
36
+ //#endregion
37
+ export { set as default };
@@ -0,0 +1,35 @@
1
+ const require_get = require("./index11.cjs.js");
2
+ const require_set = require("./index21.cjs.js");
3
+ //#region src/utils/project.ts
4
+ /**
5
+ * Projects the fields of an object based on a specified fields configuration.
6
+ * Supports inclusion (`1`) and exclusion (`0`) of specific fields. Creates a new object
7
+ * with the desired fields included or excluded, based on the configuration.
8
+ * @template T - The type of the object being projected.
9
+ * @param item - The original object to project fields from.
10
+ * @param fields - An object defining the fields to include (`1`) or exclude (`0`).
11
+ * - Keys are the field names, and values are either `1` (include) or `0` (exclude).
12
+ * @returns A new object with the specified fields included or excluded.
13
+ * - If all fields are set to `0`, the excluded fields are removed from the result.
14
+ * - If fields are set to `1`, only the included fields are retained.
15
+ */
16
+ function project(item, fields) {
17
+ if (Object.values(fields).every((value) => value === 0)) {
18
+ const result = { ...item };
19
+ Object.keys(fields).forEach((key) => {
20
+ if (require_get.default(item, key) === void 0) return;
21
+ require_set.default(result, key, void 0, true);
22
+ });
23
+ return result;
24
+ }
25
+ const result = {};
26
+ Object.entries(fields).forEach(([key, value]) => {
27
+ const fieldValue = require_get.default(item, key);
28
+ if (fieldValue === void 0) return;
29
+ if (fieldValue == null && value !== 1) return;
30
+ require_set.default(result, key, value === 1 ? fieldValue : void 0);
31
+ });
32
+ return result;
33
+ }
34
+ //#endregion
35
+ exports.default = project;