@signaldb/core 1.7.2 → 1.8.1
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.
- package/dist/.vite/manifest.json +88 -59
- package/dist/AutoFetchCollection.d.ts +3 -3
- package/dist/Collection/Cursor.d.ts +10 -7
- package/dist/Collection/index.d.ts +27 -11
- package/dist/Collection/types.d.ts +1 -0
- package/dist/ReplicatedCollection.d.ts +4 -4
- package/dist/index.cjs.js +26 -27
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +14 -28
- package/dist/index10.cjs.js +265 -0
- package/dist/index10.mjs +263 -28
- package/dist/index11.cjs.js +125 -0
- package/dist/index11.mjs +124 -12
- package/dist/index12.cjs.js +15 -0
- package/dist/index12.mjs +14 -5
- package/dist/index13.cjs.js +23 -0
- package/dist/index13.mjs +22 -133
- package/dist/index14.cjs.js +12 -0
- package/dist/index14.mjs +11 -73
- package/dist/index15.cjs.js +22 -0
- package/dist/index15.mjs +21 -10
- package/dist/index16.cjs.js +33 -0
- package/dist/index16.mjs +32 -27
- package/dist/index17.cjs.js +15 -0
- package/dist/index17.mjs +14 -142
- package/dist/index18.cjs.js +98 -0
- package/dist/index18.mjs +97 -7
- package/dist/index19.cjs.js +11 -0
- package/dist/index19.mjs +10 -41
- package/dist/index2.cjs.js +23 -0
- package/dist/index2.mjs +22 -260
- package/dist/index20.cjs.js +45 -0
- package/dist/index20.mjs +44 -15
- package/dist/index21.cjs.js +45 -0
- package/dist/index21.mjs +44 -26
- package/dist/index22.cjs.js +69 -0
- package/dist/index22.mjs +66 -77
- package/dist/index23.cjs.js +778 -0
- package/dist/index23.mjs +775 -81
- package/dist/index24.cjs.js +11 -0
- package/dist/index24.mjs +10 -18
- package/dist/index25.cjs.js +81 -0
- package/dist/index25.mjs +80 -29
- package/dist/index26.cjs.js +98 -0
- package/dist/index26.mjs +96 -8
- package/dist/index27.cjs.js +154 -0
- package/dist/index27.mjs +153 -7
- package/dist/index28.cjs.js +11 -0
- package/dist/index28.mjs +10 -29
- package/dist/index29.cjs.js +11 -0
- package/dist/index29.mjs +10 -42
- package/dist/index3.cjs.js +19 -0
- package/dist/index3.mjs +18 -788
- package/dist/index4.cjs.js +37 -0
- package/dist/index4.mjs +36 -167
- package/dist/index5.cjs.js +35 -0
- package/dist/index5.mjs +34 -67
- package/dist/index6.cjs.js +47 -0
- package/dist/index6.mjs +46 -5
- package/dist/index7.cjs.js +33 -0
- package/dist/index7.mjs +32 -5
- package/dist/index8.cjs.js +19 -0
- package/dist/index8.mjs +18 -5
- package/dist/index9.cjs.js +139 -0
- package/dist/index9.mjs +138 -5
- package/dist/utils/deepClone.d.ts +1 -1
- package/package.json +3 -3
- package/dist/index.cjs10.js +0 -29
- package/dist/index.cjs11.js +0 -12
- package/dist/index.cjs12.js +0 -5
- package/dist/index.cjs13.js +0 -133
- package/dist/index.cjs14.js +0 -74
- package/dist/index.cjs15.js +0 -10
- package/dist/index.cjs16.js +0 -27
- package/dist/index.cjs17.js +0 -142
- package/dist/index.cjs18.js +0 -7
- package/dist/index.cjs19.js +0 -42
- package/dist/index.cjs2.js +0 -261
- package/dist/index.cjs20.js +0 -15
- package/dist/index.cjs21.js +0 -26
- package/dist/index.cjs22.js +0 -79
- package/dist/index.cjs23.js +0 -84
- package/dist/index.cjs24.js +0 -18
- package/dist/index.cjs25.js +0 -29
- package/dist/index.cjs26.js +0 -9
- package/dist/index.cjs27.js +0 -7
- package/dist/index.cjs28.js +0 -29
- package/dist/index.cjs29.js +0 -42
- package/dist/index.cjs3.js +0 -788
- package/dist/index.cjs4.js +0 -167
- package/dist/index.cjs5.js +0 -68
- package/dist/index.cjs6.js +0 -5
- package/dist/index.cjs7.js +0 -5
- package/dist/index.cjs8.js +0 -5
- package/dist/index.cjs9.js +0 -5
package/dist/index2.mjs
CHANGED
|
@@ -1,261 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
//#region src/utils/get.ts
|
|
2
|
+
/**
|
|
3
|
+
* Retrieves the value at a specified path within an object.
|
|
4
|
+
* Supports dot and bracket notation for navigating nested properties.
|
|
5
|
+
* @template T - The type of the object to retrieve the value from.
|
|
6
|
+
* @param value - The object to navigate.
|
|
7
|
+
* @param path - The path (dot or bracket notation) to the desired value.
|
|
8
|
+
* @returns The value at the specified path, or `undefined` if the path does not exist.
|
|
9
|
+
*/
|
|
10
|
+
function get(value, path) {
|
|
11
|
+
const normalized = path.replaceAll(/\[(\w+)\]/g, ".$1");
|
|
12
|
+
if (normalized.includes("..") || normalized.startsWith(".") || normalized.endsWith(".")) return;
|
|
13
|
+
const segments = normalized.split(".");
|
|
14
|
+
let current = value;
|
|
15
|
+
for (const key of segments) {
|
|
16
|
+
if (current == null) return;
|
|
17
|
+
current = current[key];
|
|
18
|
+
}
|
|
19
|
+
if (current === void 0) return;
|
|
20
|
+
return current;
|
|
10
21
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
getFilteredItems;
|
|
14
|
-
options;
|
|
15
|
-
onCleanupCallbacks = [];
|
|
16
|
-
/**
|
|
17
|
-
* Creates a new instance of the `Cursor` class.
|
|
18
|
-
* Provides utilities for querying, observing, and transforming items from a collection.
|
|
19
|
-
* @template T - The type of the items in the collection.
|
|
20
|
-
* @template U - The transformed item type after applying transformations (default is T).
|
|
21
|
-
* @param getItems - A function that retrieves the filtered list of items.
|
|
22
|
-
* @param options - Optional configuration for the cursor.
|
|
23
|
-
* @param options.transform - A transformation function to apply to each item when retrieving them.
|
|
24
|
-
* @param options.bindEvents - A function to bind reactivity events for the cursor, which should return a cleanup function.
|
|
25
|
-
* @param options.fields - A projection object defining which fields of the item should be included or excluded.
|
|
26
|
-
* @param options.sort - A sort specifier to determine the order of the items.
|
|
27
|
-
* @param options.skip - The number of items to skip from the beginning of the result set.
|
|
28
|
-
* @param options.limit - The maximum number of items to return in the result set.
|
|
29
|
-
* @param options.reactive - A reactivity adapter to enable observing changes in the cursor's result set.
|
|
30
|
-
* @param options.fieldTracking - A boolean to enable fine-grained field tracking for reactivity.
|
|
31
|
-
*/
|
|
32
|
-
constructor(getItems, options) {
|
|
33
|
-
this.getFilteredItems = getItems;
|
|
34
|
-
this.options = options || {};
|
|
35
|
-
}
|
|
36
|
-
addGetters(item) {
|
|
37
|
-
if (!isInReactiveScope(this.options.reactive))
|
|
38
|
-
return item;
|
|
39
|
-
const depend = this.depend.bind(this);
|
|
40
|
-
return Object.entries(item).reduce((memo, [key, value]) => {
|
|
41
|
-
Object.defineProperty(memo, key, {
|
|
42
|
-
get() {
|
|
43
|
-
depend({
|
|
44
|
-
changedField: (notify) => (changedItem, changedFieldName) => {
|
|
45
|
-
if (changedFieldName !== key || changedItem.id !== item.id)
|
|
46
|
-
return;
|
|
47
|
-
notify();
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
return value;
|
|
51
|
-
},
|
|
52
|
-
enumerable: true,
|
|
53
|
-
configurable: true
|
|
54
|
-
});
|
|
55
|
-
return memo;
|
|
56
|
-
}, {});
|
|
57
|
-
}
|
|
58
|
-
transform(rawItem) {
|
|
59
|
-
const item = this.options.fieldTracking ? this.addGetters(rawItem) : rawItem;
|
|
60
|
-
if (!this.options.transform)
|
|
61
|
-
return item;
|
|
62
|
-
return this.options.transform(item);
|
|
63
|
-
}
|
|
64
|
-
getItems() {
|
|
65
|
-
const items = this.getFilteredItems();
|
|
66
|
-
const { sort, skip, limit } = this.options;
|
|
67
|
-
const sorted = sort ? sortItems(items, sort) : items;
|
|
68
|
-
const skipped = skip ? sorted.slice(skip) : sorted;
|
|
69
|
-
const limited = limit ? skipped.slice(0, limit) : skipped;
|
|
70
|
-
const idExcluded = this.options.fields && this.options.fields.id === 0;
|
|
71
|
-
return limited.map((item) => {
|
|
72
|
-
if (!this.options.fields)
|
|
73
|
-
return item;
|
|
74
|
-
return {
|
|
75
|
-
...idExcluded ? {} : { id: item.id },
|
|
76
|
-
...project(item, this.options.fields)
|
|
77
|
-
};
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
depend(changeEvents) {
|
|
81
|
-
if (!this.options.reactive)
|
|
82
|
-
return;
|
|
83
|
-
if (!isInReactiveScope(this.options.reactive))
|
|
84
|
-
return;
|
|
85
|
-
const signal = this.options.reactive.create();
|
|
86
|
-
signal.depend();
|
|
87
|
-
const notify = () => signal.notify();
|
|
88
|
-
function buildNotifier(event) {
|
|
89
|
-
const eventHandler = changeEvents[event];
|
|
90
|
-
return (...args) => {
|
|
91
|
-
if (eventHandler === true) {
|
|
92
|
-
notify();
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
if (typeof eventHandler !== "function")
|
|
96
|
-
return;
|
|
97
|
-
eventHandler(notify)(...args);
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
const stop = this.observeRawChanges({
|
|
101
|
-
added: buildNotifier("added"),
|
|
102
|
-
addedBefore: buildNotifier("addedBefore"),
|
|
103
|
-
changed: buildNotifier("changed"),
|
|
104
|
-
changedField: buildNotifier("changedField"),
|
|
105
|
-
movedBefore: buildNotifier("movedBefore"),
|
|
106
|
-
removed: buildNotifier("removed")
|
|
107
|
-
}, true);
|
|
108
|
-
if (this.options.reactive.onDispose) {
|
|
109
|
-
this.options.reactive.onDispose(() => stop(), signal);
|
|
110
|
-
}
|
|
111
|
-
this.onCleanup(stop);
|
|
112
|
-
}
|
|
113
|
-
ensureObserver() {
|
|
114
|
-
if (!this.observer) {
|
|
115
|
-
const observer = new Observer(() => {
|
|
116
|
-
const requery = () => {
|
|
117
|
-
observer.runChecks(this.getItems());
|
|
118
|
-
};
|
|
119
|
-
const cleanup = this.options.bindEvents && this.options.bindEvents(requery);
|
|
120
|
-
return () => {
|
|
121
|
-
if (cleanup)
|
|
122
|
-
cleanup();
|
|
123
|
-
};
|
|
124
|
-
});
|
|
125
|
-
this.onCleanup(() => observer.stop());
|
|
126
|
-
this.observer = observer;
|
|
127
|
-
}
|
|
128
|
-
return this.observer;
|
|
129
|
-
}
|
|
130
|
-
observeRawChanges(callbacks, skipInitial = false) {
|
|
131
|
-
const observer = this.ensureObserver();
|
|
132
|
-
observer.addCallbacks(callbacks, skipInitial);
|
|
133
|
-
observer.runChecks(this.getItems());
|
|
134
|
-
return () => {
|
|
135
|
-
observer.removeCallbacks(callbacks);
|
|
136
|
-
if (!observer.isEmpty())
|
|
137
|
-
return;
|
|
138
|
-
observer.stop();
|
|
139
|
-
this.observer = void 0;
|
|
140
|
-
};
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* Cleans up all resources associated with the cursor, such as reactive bindings
|
|
144
|
-
* and event listeners. This method should be called when the cursor is no longer needed
|
|
145
|
-
* to prevent memory leaks.
|
|
146
|
-
*/
|
|
147
|
-
cleanup() {
|
|
148
|
-
this.onCleanupCallbacks.forEach((callback) => {
|
|
149
|
-
callback();
|
|
150
|
-
});
|
|
151
|
-
this.onCleanupCallbacks = [];
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* Registers a cleanup callback to be executed when the `cleanup` method is called.
|
|
155
|
-
* Useful for managing resources and ensuring proper cleanup of bindings or listeners.
|
|
156
|
-
* @param callback - A function to be executed during cleanup.
|
|
157
|
-
*/
|
|
158
|
-
onCleanup(callback) {
|
|
159
|
-
this.onCleanupCallbacks.push(callback);
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* Iterates over each item in the cursor's result set, applying the provided callback
|
|
163
|
-
* function to each transformed item.
|
|
164
|
-
* ⚡️ this function is reactive!
|
|
165
|
-
* @param callback - A function to execute for each item in the result set.
|
|
166
|
-
* @param callback.item - The transformed item.
|
|
167
|
-
*/
|
|
168
|
-
forEach(callback) {
|
|
169
|
-
const items = this.getItems();
|
|
170
|
-
this.depend({
|
|
171
|
-
addedBefore: true,
|
|
172
|
-
removed: true,
|
|
173
|
-
movedBefore: true,
|
|
174
|
-
...this.options.fieldTracking ? {} : { changed: true }
|
|
175
|
-
});
|
|
176
|
-
items.forEach((item) => {
|
|
177
|
-
callback(this.transform(item));
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Creates a new array populated with the results of applying the provided callback
|
|
182
|
-
* function to each transformed item in the cursor's result set.
|
|
183
|
-
* ⚡️ this function is reactive!
|
|
184
|
-
* @template V - The type of the items in the resulting array.
|
|
185
|
-
* @param callback - A function to execute for each item in the result set.
|
|
186
|
-
* @param callback.item - The transformed item.
|
|
187
|
-
* @returns An array of results after applying the callback to each item.
|
|
188
|
-
*/
|
|
189
|
-
map(callback) {
|
|
190
|
-
const results = [];
|
|
191
|
-
this.forEach((item) => {
|
|
192
|
-
results.push(callback(item));
|
|
193
|
-
});
|
|
194
|
-
return results;
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* Fetches all transformed items from the cursor's result set as an array.
|
|
198
|
-
* Automatically applies filtering, sorting, and limiting as per the cursor's options.
|
|
199
|
-
* ⚡️ this function is reactive!
|
|
200
|
-
* @returns An array of transformed items in the result set.
|
|
201
|
-
*/
|
|
202
|
-
fetch() {
|
|
203
|
-
return this.map((item) => item);
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* Counts the total number of items in the cursor's result set after applying
|
|
207
|
-
* filtering and other criteria.
|
|
208
|
-
* ⚡️ this function is reactive!
|
|
209
|
-
* @returns The total number of items in the result set.
|
|
210
|
-
*/
|
|
211
|
-
count() {
|
|
212
|
-
const items = this.getItems();
|
|
213
|
-
this.depend({
|
|
214
|
-
added: true,
|
|
215
|
-
removed: true
|
|
216
|
-
});
|
|
217
|
-
return items.length;
|
|
218
|
-
}
|
|
219
|
-
/**
|
|
220
|
-
* Observes changes to the cursor's result set and triggers the specified callbacks
|
|
221
|
-
* when items are added, removed, or updated. Supports reactivity and transformation.
|
|
222
|
-
* @param callbacks - An object containing the callback functions to handle different change events.
|
|
223
|
-
* @param callbacks.added - Triggered when an item is added to the result set.
|
|
224
|
-
* @param callbacks.removed - Triggered when an item is removed from the result set.
|
|
225
|
-
* @param callbacks.changed - Triggered when an item in the result set is modified.
|
|
226
|
-
* @param callbacks.addedBefore - Triggered when an item is added before another item in the result set.
|
|
227
|
-
* @param callbacks.movedBefore - Triggered when an item is moved before another item in the result set.
|
|
228
|
-
* @param callbacks.changedField - Triggered when a specific field of an item changes.
|
|
229
|
-
* @param skipInitial - A boolean indicating whether to skip the initial notification of the current result set.
|
|
230
|
-
* @returns A function to stop observing changes.
|
|
231
|
-
*/
|
|
232
|
-
observeChanges(callbacks, skipInitial = false) {
|
|
233
|
-
return this.observeRawChanges(Object.entries(callbacks).reduce((memo, [callbackName, callback]) => {
|
|
234
|
-
if (!callback)
|
|
235
|
-
return memo;
|
|
236
|
-
return {
|
|
237
|
-
...memo,
|
|
238
|
-
[callbackName]: (item, before) => {
|
|
239
|
-
const transformedValue = this.transform(item);
|
|
240
|
-
const hasBeforeParameter = before !== void 0;
|
|
241
|
-
const transformedBeforeValue = hasBeforeParameter && before ? this.transform(before) : null;
|
|
242
|
-
return callback(transformedValue, ...hasBeforeParameter ? [transformedBeforeValue] : []);
|
|
243
|
-
}
|
|
244
|
-
};
|
|
245
|
-
}, {}), skipInitial);
|
|
246
|
-
}
|
|
247
|
-
/**
|
|
248
|
-
* Forces the cursor to re-evaluate its result set by re-fetching items
|
|
249
|
-
* from the collection. This is useful when the underlying data or query
|
|
250
|
-
* criteria have changed, and you want to ensure the cursor reflects the latest state.
|
|
251
|
-
*/
|
|
252
|
-
requery() {
|
|
253
|
-
if (!this.observer)
|
|
254
|
-
return;
|
|
255
|
-
this.observer.runChecks(this.getItems());
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
export {
|
|
259
|
-
Cursor as default,
|
|
260
|
-
isInReactiveScope
|
|
261
|
-
};
|
|
22
|
+
//#endregion
|
|
23
|
+
export { get as default };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
//#region src/utils/isFieldExpression.ts
|
|
2
|
+
var expressionKeys = new Set([
|
|
3
|
+
"$eq",
|
|
4
|
+
"$gt",
|
|
5
|
+
"$gte",
|
|
6
|
+
"$lt",
|
|
7
|
+
"$lte",
|
|
8
|
+
"$in",
|
|
9
|
+
"$nin",
|
|
10
|
+
"$ne",
|
|
11
|
+
"$exists",
|
|
12
|
+
"$not",
|
|
13
|
+
"$expr",
|
|
14
|
+
"$jsonSchema",
|
|
15
|
+
"$mod",
|
|
16
|
+
"$regex",
|
|
17
|
+
"$options",
|
|
18
|
+
"$text",
|
|
19
|
+
"$where",
|
|
20
|
+
"$all",
|
|
21
|
+
"$elemMatch",
|
|
22
|
+
"$size",
|
|
23
|
+
"$bitsAllClear",
|
|
24
|
+
"$bitsAllSet",
|
|
25
|
+
"$bitsAnyClear",
|
|
26
|
+
"$bitsAnySet"
|
|
27
|
+
]);
|
|
28
|
+
/**
|
|
29
|
+
* Determines whether a given object is a valid field expression.
|
|
30
|
+
* A field expression is an object containing query operators supported by MongoDB-style queries.
|
|
31
|
+
* @template T - The type of the field expression.
|
|
32
|
+
* @param expression - The object to test.
|
|
33
|
+
* @returns A boolean indicating whether the object is a valid field expression.
|
|
34
|
+
* - `true` if the object contains only recognized query operators.
|
|
35
|
+
* - `false` otherwise.
|
|
36
|
+
*/
|
|
37
|
+
function isFieldExpression(expression) {
|
|
38
|
+
if (typeof expression !== "object" || expression == null) return false;
|
|
39
|
+
const keys = Object.keys(expression);
|
|
40
|
+
if (keys.length === 0) return false;
|
|
41
|
+
if (keys.some((key) => !expressionKeys.has(key))) return false;
|
|
42
|
+
return keys.every((key) => expressionKeys.has(key));
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
exports.default = isFieldExpression;
|
package/dist/index20.mjs
CHANGED
|
@@ -1,16 +1,45 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
//#region src/utils/isFieldExpression.ts
|
|
2
|
+
var expressionKeys = new Set([
|
|
3
|
+
"$eq",
|
|
4
|
+
"$gt",
|
|
5
|
+
"$gte",
|
|
6
|
+
"$lt",
|
|
7
|
+
"$lte",
|
|
8
|
+
"$in",
|
|
9
|
+
"$nin",
|
|
10
|
+
"$ne",
|
|
11
|
+
"$exists",
|
|
12
|
+
"$not",
|
|
13
|
+
"$expr",
|
|
14
|
+
"$jsonSchema",
|
|
15
|
+
"$mod",
|
|
16
|
+
"$regex",
|
|
17
|
+
"$options",
|
|
18
|
+
"$text",
|
|
19
|
+
"$where",
|
|
20
|
+
"$all",
|
|
21
|
+
"$elemMatch",
|
|
22
|
+
"$size",
|
|
23
|
+
"$bitsAllClear",
|
|
24
|
+
"$bitsAllSet",
|
|
25
|
+
"$bitsAnyClear",
|
|
26
|
+
"$bitsAnySet"
|
|
27
|
+
]);
|
|
28
|
+
/**
|
|
29
|
+
* Determines whether a given object is a valid field expression.
|
|
30
|
+
* A field expression is an object containing query operators supported by MongoDB-style queries.
|
|
31
|
+
* @template T - The type of the field expression.
|
|
32
|
+
* @param expression - The object to test.
|
|
33
|
+
* @returns A boolean indicating whether the object is a valid field expression.
|
|
34
|
+
* - `true` if the object contains only recognized query operators.
|
|
35
|
+
* - `false` otherwise.
|
|
36
|
+
*/
|
|
37
|
+
function isFieldExpression(expression) {
|
|
38
|
+
if (typeof expression !== "object" || expression == null) return false;
|
|
39
|
+
const keys = Object.keys(expression);
|
|
40
|
+
if (keys.length === 0) return false;
|
|
41
|
+
if (keys.some((key) => !expressionKeys.has(key))) return false;
|
|
42
|
+
return keys.every((key) => expressionKeys.has(key));
|
|
13
43
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
};
|
|
44
|
+
//#endregion
|
|
45
|
+
export { isFieldExpression as default };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const require_serializeValue = require("./index15.cjs.js");
|
|
2
|
+
const require_isFieldExpression = require("./index20.cjs.js");
|
|
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 (require_isFieldExpression.default(fieldSelector)) {
|
|
24
|
+
if (fieldSelector.$ne != null) {
|
|
25
|
+
result.exclude = [require_serializeValue.default(fieldSelector.$ne)];
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
if (Array.isArray(fieldSelector.$in) && fieldSelector.$in.length > 0) {
|
|
29
|
+
result.include = fieldSelector.$in.map(require_serializeValue.default);
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
32
|
+
if (Array.isArray(fieldSelector.$nin) && fieldSelector.$nin.length > 0) {
|
|
33
|
+
result.exclude = fieldSelector.$nin.map(require_serializeValue.default);
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
include: null,
|
|
38
|
+
exclude: null
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
result.include = [require_serializeValue.default(fieldSelector)];
|
|
42
|
+
return result;
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
exports.default = getMatchingKeys;
|
package/dist/index21.mjs
CHANGED
|
@@ -1,27 +1,45 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
1
|
+
import serializeValue from "./index15.mjs";
|
|
2
|
+
import isFieldExpression from "./index20.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;
|
|
24
43
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
};
|
|
44
|
+
//#endregion
|
|
45
|
+
export { getMatchingKeys as default };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
const require_get = require("./index2.cjs.js");
|
|
2
|
+
const require_serializeValue = require("./index15.cjs.js");
|
|
3
|
+
const require_createIndexProvider = require("./index19.cjs.js");
|
|
4
|
+
const require_getMatchingKeys = require("./index21.cjs.js");
|
|
5
|
+
//#region src/Collection/createIndex.ts
|
|
6
|
+
/**
|
|
7
|
+
* creates an index for a specific field but uses an external map to store the index
|
|
8
|
+
* @param field name of the field
|
|
9
|
+
* @param index the external map to use for the index
|
|
10
|
+
* @returns an index provider to pass to the `indices` option of the collection constructor
|
|
11
|
+
*/
|
|
12
|
+
function createExternalIndex(field, index) {
|
|
13
|
+
return require_createIndexProvider.default({
|
|
14
|
+
query(selector) {
|
|
15
|
+
if (!Object.hasOwnProperty.call(selector, field)) return { matched: false };
|
|
16
|
+
const fieldSelector = selector[field];
|
|
17
|
+
const filteresForNull = fieldSelector == null || fieldSelector.$exists === false;
|
|
18
|
+
const keys = filteresForNull ? {
|
|
19
|
+
include: null,
|
|
20
|
+
exclude: [...index.keys()].filter((key) => key != null)
|
|
21
|
+
} : require_getMatchingKeys.default(field, selector);
|
|
22
|
+
if (keys.include == null && keys.exclude == null) return { matched: false };
|
|
23
|
+
let includedPositions = [];
|
|
24
|
+
if (keys.include == null) for (const set of index.values()) for (const pos of set) includedPositions.push(pos);
|
|
25
|
+
else for (const key of keys.include) {
|
|
26
|
+
const posSet = index.get(key);
|
|
27
|
+
if (posSet) for (const pos of posSet) includedPositions.push(pos);
|
|
28
|
+
}
|
|
29
|
+
if (keys.exclude != null) {
|
|
30
|
+
const excludeSet = /* @__PURE__ */ new Set();
|
|
31
|
+
for (const key of keys.exclude) {
|
|
32
|
+
const posSet = index.get(key);
|
|
33
|
+
if (posSet) for (const pos of posSet) excludeSet.add(pos);
|
|
34
|
+
}
|
|
35
|
+
includedPositions = includedPositions.filter((pos) => !excludeSet.has(pos));
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
matched: true,
|
|
39
|
+
positions: includedPositions,
|
|
40
|
+
fields: [field],
|
|
41
|
+
keepSelector: filteresForNull
|
|
42
|
+
};
|
|
43
|
+
},
|
|
44
|
+
rebuild() {}
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* creates an index for a specific field
|
|
49
|
+
* @param field name of the field
|
|
50
|
+
* @returns an index provider to pass to the `indices` option of the collection constructor
|
|
51
|
+
*/
|
|
52
|
+
function createIndex(field) {
|
|
53
|
+
const index = /* @__PURE__ */ new Map();
|
|
54
|
+
return {
|
|
55
|
+
...createExternalIndex(field, index),
|
|
56
|
+
rebuild(items) {
|
|
57
|
+
index.clear();
|
|
58
|
+
items.forEach((item, i) => {
|
|
59
|
+
const value = require_serializeValue.default(require_get.default(item, field));
|
|
60
|
+
const current = index.get(value) || /* @__PURE__ */ new Set();
|
|
61
|
+
current.add(i);
|
|
62
|
+
index.set(value, current);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
//#endregion
|
|
68
|
+
exports.createExternalIndex = createExternalIndex;
|
|
69
|
+
exports.default = createIndex;
|