@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.
- package/dist/.vite/manifest.json +183 -80
- package/dist/AsyncDataAdapter.d.ts +53 -7
- package/dist/AutoFetchDataAdapter.d.ts +4 -6
- package/dist/Collection/Cursor.d.ts +56 -4
- package/dist/Collection/Observer.d.ts +33 -0
- package/dist/Collection/index.d.ts +66 -19
- package/dist/Collection/types.d.ts +9 -2
- package/dist/DataAdapter.d.ts +24 -6
- package/dist/DefaultDataAdapter.d.ts +5 -5
- package/dist/WorkerDataAdapter.d.ts +57 -5
- package/dist/WorkerDataAdapterHost.d.ts +9 -7
- package/dist/createIndex.d.ts +2 -2
- package/dist/createIndexProvider.d.ts +2 -2
- package/dist/createReactivityAdapter.d.ts +2 -2
- package/dist/createStorageAdapter.d.ts +1 -1
- package/dist/getIndexInfo.d.ts +3 -4
- package/dist/index.cjs.js +33 -31
- package/dist/index.d.ts +6 -1
- package/dist/index.mjs +17 -32
- package/dist/index10.cjs.js +11 -0
- package/dist/index10.mjs +10 -18
- package/dist/index11.cjs.js +23 -0
- package/dist/index11.mjs +22 -15
- package/dist/index12.cjs.js +45 -0
- package/dist/index12.mjs +44 -353
- package/dist/index13.cjs.js +22 -0
- package/dist/index13.mjs +21 -419
- package/dist/index14.cjs.js +45 -0
- package/dist/index14.mjs +44 -206
- package/dist/index15.cjs.js +86 -0
- package/dist/index15.mjs +85 -388
- package/dist/index16.cjs.js +15 -0
- package/dist/index16.mjs +14 -579
- package/dist/index17.cjs.js +141 -0
- package/dist/index17.mjs +140 -152
- package/dist/index18.cjs.js +47 -0
- package/dist/index18.mjs +46 -26
- package/dist/index19.cjs.js +15 -0
- package/dist/index19.mjs +14 -41
- package/dist/index2.cjs.js +33 -0
- package/dist/index2.mjs +32 -258
- package/dist/index20.cjs.js +23 -0
- package/dist/index20.mjs +22 -16
- package/dist/index21.cjs.js +37 -0
- package/dist/index21.mjs +36 -104
- package/dist/index22.cjs.js +35 -0
- package/dist/index22.mjs +34 -128
- package/dist/index23.cjs.js +21 -0
- package/dist/index23.mjs +20 -7
- package/dist/index24.cjs.js +19 -0
- package/dist/index24.mjs +18 -27
- package/dist/index25.cjs.js +157 -0
- package/dist/index25.mjs +156 -11
- package/dist/index26.cjs.js +38 -0
- package/dist/index26.mjs +37 -29
- package/dist/index27.cjs.js +42 -0
- package/dist/index27.mjs +41 -46
- package/dist/index28.cjs.js +347 -0
- package/dist/index28.mjs +345 -7
- package/dist/index29.cjs.js +602 -0
- package/dist/index29.mjs +601 -7
- package/dist/index3.cjs.js +19 -0
- package/dist/index3.mjs +18 -556
- package/dist/index30.cjs.js +11 -0
- package/dist/index30.mjs +10 -42
- package/dist/index31.cjs.js +11 -0
- package/dist/index31.mjs +10 -29
- package/dist/index32.cjs.js +90 -0
- package/dist/index32.mjs +89 -10
- package/dist/index33.cjs.js +53 -0
- package/dist/index33.mjs +52 -5
- package/dist/index34.cjs.js +453 -0
- package/dist/index34.mjs +453 -0
- package/dist/index35.cjs.js +72 -0
- package/dist/index35.mjs +72 -0
- package/dist/index36.cjs.js +580 -0
- package/dist/index36.mjs +580 -0
- package/dist/index37.cjs.js +21 -0
- package/dist/index37.mjs +21 -0
- package/dist/index38.cjs.js +363 -0
- package/dist/index38.mjs +363 -0
- package/dist/index39.cjs.js +513 -0
- package/dist/index39.mjs +513 -0
- package/dist/index4.cjs.js +209 -0
- package/dist/index4.mjs +204 -5
- package/dist/index5.cjs.js +187 -0
- package/dist/index5.mjs +186 -5
- package/dist/index6.cjs.js +303 -0
- package/dist/index6.mjs +301 -28
- package/dist/index7.cjs.js +125 -0
- package/dist/index7.mjs +124 -12
- package/dist/index8.cjs.js +33 -0
- package/dist/index8.mjs +32 -5
- package/dist/index9.cjs.js +12 -0
- package/dist/index9.mjs +11 -133
- package/dist/types/IndexProvider.d.ts +2 -2
- package/dist/types/Modifier.d.ts +1 -1
- package/dist/types/ReactivityAdapter.d.ts +1 -1
- package/dist/types/StorageAdapter.d.ts +9 -1
- package/dist/utils/applyQueryOptions.d.ts +15 -0
- package/dist/utils/createSignal.d.ts +2 -2
- package/dist/utils/getMatchingKeys.d.ts +2 -2
- package/dist/utils/idIndexQuery.d.ts +20 -0
- package/dist/utils/incrementalQueryUpdate.d.ts +62 -0
- package/dist/utils/isFieldExpression.d.ts +1 -1
- package/dist/utils/match.d.ts +1 -1
- package/dist/utils/modify.d.ts +1 -1
- package/dist/utils/projectItems.d.ts +12 -0
- package/dist/utils/queryDelta.d.ts +83 -0
- package/dist/utils/queryId.d.ts +2 -2
- package/dist/utils/reactiveOrAsync.d.ts +59 -0
- package/dist/utils/storageIndexQuery.d.ts +20 -0
- package/package.json +1 -1
- package/dist/index.cjs10.js +0 -18
- package/dist/index.cjs11.js +0 -15
- package/dist/index.cjs12.js +0 -353
- package/dist/index.cjs13.js +0 -419
- package/dist/index.cjs14.js +0 -206
- package/dist/index.cjs15.js +0 -388
- package/dist/index.cjs16.js +0 -579
- package/dist/index.cjs17.js +0 -152
- package/dist/index.cjs18.js +0 -26
- package/dist/index.cjs19.js +0 -42
- package/dist/index.cjs2.js +0 -259
- package/dist/index.cjs20.js +0 -16
- package/dist/index.cjs21.js +0 -104
- package/dist/index.cjs22.js +0 -129
- package/dist/index.cjs23.js +0 -7
- package/dist/index.cjs24.js +0 -27
- package/dist/index.cjs25.js +0 -10
- package/dist/index.cjs26.js +0 -29
- package/dist/index.cjs27.js +0 -46
- package/dist/index.cjs28.js +0 -8
- package/dist/index.cjs29.js +0 -7
- package/dist/index.cjs3.js +0 -557
- package/dist/index.cjs30.js +0 -42
- package/dist/index.cjs31.js +0 -29
- package/dist/index.cjs32.js +0 -9
- package/dist/index.cjs33.js +0 -5
- package/dist/index.cjs4.js +0 -5
- package/dist/index.cjs5.js +0 -5
- package/dist/index.cjs6.js +0 -29
- package/dist/index.cjs7.js +0 -12
- package/dist/index.cjs8.js +0 -5
- package/dist/index.cjs9.js +0 -133
package/dist/index.cjs3.js
DELETED
|
@@ -1,557 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
-
const EventEmitter = require("./index.cjs9.js");
|
|
4
|
-
const createSignal = require("./index.cjs18.js");
|
|
5
|
-
const randomId = require("./index.cjs8.js");
|
|
6
|
-
const DefaultDataAdapter = require("./index.cjs12.js");
|
|
7
|
-
const modify = require("./index.cjs7.js");
|
|
8
|
-
const deepClone = require("./index.cjs19.js");
|
|
9
|
-
const queryId = require("./index.cjs20.js");
|
|
10
|
-
const Cursor = require("./index.cjs2.js");
|
|
11
|
-
class Collection extends EventEmitter {
|
|
12
|
-
static collections = [];
|
|
13
|
-
static debugMode = false;
|
|
14
|
-
static batchOperationInProgress = false;
|
|
15
|
-
static fieldTracking = false;
|
|
16
|
-
static onCreationCallbacks = [];
|
|
17
|
-
static onDisposeCallbacks = [];
|
|
18
|
-
static getCollections() {
|
|
19
|
-
return Collection.collections;
|
|
20
|
-
}
|
|
21
|
-
static onCreation(callback) {
|
|
22
|
-
Collection.onCreationCallbacks.push(callback);
|
|
23
|
-
}
|
|
24
|
-
static onDispose(callback) {
|
|
25
|
-
Collection.onDisposeCallbacks.push(callback);
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Enables debug mode for all collections.
|
|
29
|
-
*/
|
|
30
|
-
static enableDebugMode = () => {
|
|
31
|
-
Collection.debugMode = true;
|
|
32
|
-
Collection.collections.forEach((collection) => {
|
|
33
|
-
collection.setDebugMode(true);
|
|
34
|
-
});
|
|
35
|
-
};
|
|
36
|
-
/**
|
|
37
|
-
* Enables field tracking for all collections.
|
|
38
|
-
* @param enable - A boolean indicating whether to enable field tracking.
|
|
39
|
-
*/
|
|
40
|
-
static setFieldTracking = (enable) => {
|
|
41
|
-
Collection.fieldTracking = enable;
|
|
42
|
-
Collection.collections.forEach((collection) => {
|
|
43
|
-
collection.setFieldTracking(enable);
|
|
44
|
-
});
|
|
45
|
-
};
|
|
46
|
-
static batch(callback) {
|
|
47
|
-
Collection.batchOperationInProgress = true;
|
|
48
|
-
const execute = () => Collection.collections.reduce((memo, collection) => () => {
|
|
49
|
-
return collection.batch(memo);
|
|
50
|
-
}, callback)();
|
|
51
|
-
const maybePromise = execute();
|
|
52
|
-
const afterBatch = () => {
|
|
53
|
-
Collection.batchOperationInProgress = false;
|
|
54
|
-
};
|
|
55
|
-
if (maybePromise && typeof maybePromise.then === "function") {
|
|
56
|
-
return maybePromise.then(() => afterBatch());
|
|
57
|
-
} else {
|
|
58
|
-
afterBatch();
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
name;
|
|
62
|
-
backend;
|
|
63
|
-
options;
|
|
64
|
-
isPullingSignal;
|
|
65
|
-
isPushingSignal;
|
|
66
|
-
readySignal;
|
|
67
|
-
debugMode;
|
|
68
|
-
batchOperationInProgress = false;
|
|
69
|
-
isDisposed = false;
|
|
70
|
-
postBatchCallbacks = /* @__PURE__ */ new Set();
|
|
71
|
-
fieldTracking = false;
|
|
72
|
-
queryListenersMap = /* @__PURE__ */ new Map();
|
|
73
|
-
constructor(nameOrOptions, maybeDataAdapter, maybeOptions) {
|
|
74
|
-
super();
|
|
75
|
-
const name = typeof nameOrOptions === "string" ? nameOrOptions : nameOrOptions?.name || `${this.constructor.name}-${randomId()}`;
|
|
76
|
-
const options = typeof nameOrOptions === "string" ? maybeOptions || {} : nameOrOptions || {};
|
|
77
|
-
const persistence = options.persistence;
|
|
78
|
-
const dataAdapter = maybeDataAdapter || new DefaultDataAdapter({
|
|
79
|
-
...persistence ? { storage: () => persistence } : {}
|
|
80
|
-
});
|
|
81
|
-
Collection.collections.push(this);
|
|
82
|
-
this.name = name;
|
|
83
|
-
this.options = { ...options };
|
|
84
|
-
this.fieldTracking = this.options.fieldTracking ?? Collection.fieldTracking;
|
|
85
|
-
this.debugMode = this.options.enableDebugMode ?? Collection.debugMode;
|
|
86
|
-
this.isPullingSignal = createSignal(this.options.reactivity, false);
|
|
87
|
-
this.isPushingSignal = createSignal(this.options.reactivity, false);
|
|
88
|
-
this.readySignal = createSignal(this.options.reactivity, false);
|
|
89
|
-
this.backend = dataAdapter.createCollectionBackend(this, this.options.indices ?? []);
|
|
90
|
-
void this.backend.isReady().then(() => {
|
|
91
|
-
this.readySignal.set(true);
|
|
92
|
-
}).catch(() => {
|
|
93
|
-
});
|
|
94
|
-
Collection.onCreationCallbacks.forEach((callback) => callback(this));
|
|
95
|
-
}
|
|
96
|
-
isBatchOperationInProgress() {
|
|
97
|
-
return Collection.batchOperationInProgress || this.batchOperationInProgress;
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* Checks whether the collection is currently performing a pull operation
|
|
101
|
-
* ⚡️ this function is reactive!
|
|
102
|
-
* (loading data from the persistence adapter).
|
|
103
|
-
* @returns A boolean indicating if the collection is in the process of pulling data.
|
|
104
|
-
*/
|
|
105
|
-
isPulling() {
|
|
106
|
-
return this.isPullingSignal.get() ?? false;
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* Checks whether the collection is currently performing a push operation
|
|
110
|
-
* ⚡️ this function is reactive!
|
|
111
|
-
* (saving data to the persistence adapter).
|
|
112
|
-
* @returns A boolean indicating if the collection is in the process of pushing data.
|
|
113
|
-
*/
|
|
114
|
-
isPushing() {
|
|
115
|
-
return this.isPushingSignal.get() ?? false;
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Checks whether the collection is currently performing either a pull or push operation,
|
|
119
|
-
* ⚡️ this function is reactive!
|
|
120
|
-
* indicating that it is loading or saving data.
|
|
121
|
-
* @returns A boolean indicating if the collection is in the process of loading or saving data.
|
|
122
|
-
*/
|
|
123
|
-
isLoading() {
|
|
124
|
-
const isPulling = this.isPulling();
|
|
125
|
-
const isPushing = this.isPushing();
|
|
126
|
-
return isPulling || isPushing;
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Retrieves the current debug mode status of the collection.
|
|
130
|
-
* @returns A boolean indicating whether debug mode is enabled for the collection.
|
|
131
|
-
*/
|
|
132
|
-
getDebugMode() {
|
|
133
|
-
return this.debugMode;
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Enables or disables debug mode for the collection.
|
|
137
|
-
* When debug mode is enabled, additional debugging information and events are emitted.
|
|
138
|
-
* @param enable - A boolean indicating whether to enable (`true`) or disable (`false`) debug mode.
|
|
139
|
-
*/
|
|
140
|
-
setDebugMode(enable) {
|
|
141
|
-
this.debugMode = enable;
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Enables or disables field tracking for the collection.
|
|
145
|
-
* @param enable - A boolean indicating whether to enable (`true`) or disable (`false`) field tracking.
|
|
146
|
-
*/
|
|
147
|
-
setFieldTracking(enable) {
|
|
148
|
-
this.fieldTracking = enable;
|
|
149
|
-
}
|
|
150
|
-
/**
|
|
151
|
-
* Resolves when the persistence adapter finished initializing
|
|
152
|
-
* and the collection is ready to be used.
|
|
153
|
-
* @returns A promise that resolves when the collection is ready.
|
|
154
|
-
* @example
|
|
155
|
-
* ```ts
|
|
156
|
-
* const collection = new Collection({
|
|
157
|
-
* persistence: // ...
|
|
158
|
-
* })
|
|
159
|
-
* await collection.isReady()
|
|
160
|
-
*
|
|
161
|
-
* collection.insert({ name: 'Item 1' })
|
|
162
|
-
*/
|
|
163
|
-
async ready() {
|
|
164
|
-
return this.backend.isReady();
|
|
165
|
-
}
|
|
166
|
-
/**
|
|
167
|
-
* Checks if the collection is ready.
|
|
168
|
-
* ⚡️ this function is reactive!
|
|
169
|
-
* @returns A boolean indicating whether the collection is ready.
|
|
170
|
-
*/
|
|
171
|
-
isReady() {
|
|
172
|
-
return this.readySignal.get() ?? false;
|
|
173
|
-
}
|
|
174
|
-
profile(fn, measureFunction) {
|
|
175
|
-
if (!this.debugMode)
|
|
176
|
-
return fn();
|
|
177
|
-
const startTime = performance.now();
|
|
178
|
-
const handleProfileEnd = (result) => {
|
|
179
|
-
const endTime = performance.now();
|
|
180
|
-
measureFunction(endTime - startTime);
|
|
181
|
-
return result;
|
|
182
|
-
};
|
|
183
|
-
const maybePromise = fn();
|
|
184
|
-
return maybePromise instanceof Promise ? maybePromise.then(handleProfileEnd) : handleProfileEnd(maybePromise);
|
|
185
|
-
}
|
|
186
|
-
executeInDebugMode(fn) {
|
|
187
|
-
if (!this.debugMode)
|
|
188
|
-
return;
|
|
189
|
-
const callstack = new Error().stack || "";
|
|
190
|
-
fn(callstack);
|
|
191
|
-
}
|
|
192
|
-
transform(item) {
|
|
193
|
-
if (!this.options.transform)
|
|
194
|
-
return item;
|
|
195
|
-
return this.options.transform(item);
|
|
196
|
-
}
|
|
197
|
-
transformAll(items, fields) {
|
|
198
|
-
if (!this.options.transformAll)
|
|
199
|
-
return items;
|
|
200
|
-
return this.options.transformAll(deepClone.default(items), fields);
|
|
201
|
-
}
|
|
202
|
-
getItem(selector, options) {
|
|
203
|
-
const itemsOrPromise = this.getItems(selector, { ...options, limit: 1 });
|
|
204
|
-
if (itemsOrPromise instanceof Promise) {
|
|
205
|
-
return itemsOrPromise.then((items) => {
|
|
206
|
-
return items[0] || void 0;
|
|
207
|
-
});
|
|
208
|
-
}
|
|
209
|
-
return itemsOrPromise[0];
|
|
210
|
-
}
|
|
211
|
-
getItems(selector, options) {
|
|
212
|
-
this.emit("getItems", selector);
|
|
213
|
-
return this.profile(() => {
|
|
214
|
-
if (!options?.async)
|
|
215
|
-
return this.backend.getQueryResult(selector, options);
|
|
216
|
-
this.isPullingSignal.set(true);
|
|
217
|
-
return this.backend.executeQuery(selector, options).finally(() => {
|
|
218
|
-
this.isPullingSignal.set(false);
|
|
219
|
-
});
|
|
220
|
-
}, (measuredTime) => this.executeInDebugMode((callstack) => this.emit("_debug.getItems", callstack, selector, measuredTime)));
|
|
221
|
-
}
|
|
222
|
-
async withPushState(asyncFunction) {
|
|
223
|
-
this.isPushingSignal.set(true);
|
|
224
|
-
try {
|
|
225
|
-
return await asyncFunction();
|
|
226
|
-
} finally {
|
|
227
|
-
this.isPushingSignal.set(false);
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
queryListeners(query, listeners) {
|
|
231
|
-
const id = queryId(query.selector, query.options);
|
|
232
|
-
if (listeners != null) {
|
|
233
|
-
return this.queryListenersMap.set(id, listeners);
|
|
234
|
-
}
|
|
235
|
-
return this.queryListenersMap.get(id) ?? 0;
|
|
236
|
-
}
|
|
237
|
-
/**
|
|
238
|
-
* Disposes the collection, unregisters persistence adapters, clears memory, and
|
|
239
|
-
* cleans up all resources used by the collection.
|
|
240
|
-
* @returns A promise that resolves when the collection is disposed.
|
|
241
|
-
*/
|
|
242
|
-
async dispose() {
|
|
243
|
-
await this.backend.dispose();
|
|
244
|
-
this.isDisposed = true;
|
|
245
|
-
this.removeAllListeners();
|
|
246
|
-
Collection.collections = Collection.collections.filter((collection) => collection !== this);
|
|
247
|
-
Collection.onDisposeCallbacks.forEach((callback) => callback(this));
|
|
248
|
-
}
|
|
249
|
-
/**
|
|
250
|
-
* Finds multiple items in the collection based on a selector and optional options.
|
|
251
|
-
* Returns a cursor for reactive data queries.
|
|
252
|
-
* @template O - The options type for the find operation.
|
|
253
|
-
* @param [selector] - The criteria to select items.
|
|
254
|
-
* @param [options] - Options for the find operation, such as limit and sort.
|
|
255
|
-
* @returns A cursor to fetch and observe the matching items.
|
|
256
|
-
*/
|
|
257
|
-
find(selector = {}, options) {
|
|
258
|
-
if (this.isDisposed)
|
|
259
|
-
throw new Error("Collection is disposed");
|
|
260
|
-
if (selector !== void 0 && (!selector || typeof selector !== "object"))
|
|
261
|
-
throw new Error("Invalid selector");
|
|
262
|
-
const getTransformedItems = () => {
|
|
263
|
-
const itemsOrPromise = this.getItems(selector, options || {});
|
|
264
|
-
if (itemsOrPromise instanceof Promise) {
|
|
265
|
-
return itemsOrPromise.then((items2) => {
|
|
266
|
-
return this.transformAll(items2, options?.fields);
|
|
267
|
-
});
|
|
268
|
-
}
|
|
269
|
-
const items = itemsOrPromise;
|
|
270
|
-
return this.transformAll(items, options?.fields);
|
|
271
|
-
};
|
|
272
|
-
const cursor = new Cursor.default(getTransformedItems, {
|
|
273
|
-
reactive: this.options.reactivity,
|
|
274
|
-
fieldTracking: this.fieldTracking,
|
|
275
|
-
...options,
|
|
276
|
-
transform: this.transform.bind(this),
|
|
277
|
-
bindEvents: (requery) => {
|
|
278
|
-
const handleRequery = () => {
|
|
279
|
-
if (this.batchOperationInProgress) {
|
|
280
|
-
this.postBatchCallbacks.add(requery);
|
|
281
|
-
return;
|
|
282
|
-
}
|
|
283
|
-
requery();
|
|
284
|
-
};
|
|
285
|
-
const listeners = this.queryListeners({ selector, options });
|
|
286
|
-
if (listeners === 0)
|
|
287
|
-
this.backend.registerQuery(selector, options || {});
|
|
288
|
-
this.queryListeners({ selector, options }, listeners + 1);
|
|
289
|
-
const queryStateChangeCleanup = this.backend.onQueryStateChange(selector, options || {}, (state) => {
|
|
290
|
-
if (state !== "complete")
|
|
291
|
-
return;
|
|
292
|
-
handleRequery();
|
|
293
|
-
});
|
|
294
|
-
this.emit("observer.created", selector, options);
|
|
295
|
-
return () => {
|
|
296
|
-
setTimeout(() => {
|
|
297
|
-
const newListeners = Math.max(0, this.queryListeners({ selector, options }) - 1);
|
|
298
|
-
if (newListeners === 0)
|
|
299
|
-
this.backend.unregisterQuery(selector, options || {});
|
|
300
|
-
this.queryListeners({ selector, options }, newListeners);
|
|
301
|
-
queryStateChangeCleanup();
|
|
302
|
-
this.emit("observer.disposed", selector, options);
|
|
303
|
-
}, 0);
|
|
304
|
-
};
|
|
305
|
-
}
|
|
306
|
-
});
|
|
307
|
-
this.emit("find", selector, options, cursor);
|
|
308
|
-
this.executeInDebugMode((callstack) => this.emit("_debug.find", callstack, selector, options, cursor));
|
|
309
|
-
return cursor;
|
|
310
|
-
}
|
|
311
|
-
/**
|
|
312
|
-
* Finds a single item in the collection based on a selector and optional options.
|
|
313
|
-
* ⚡️ this function is reactive!
|
|
314
|
-
* Returns the found item or undefined if no item matches.
|
|
315
|
-
* @template Async - Whether to perform the operation asynchronously.
|
|
316
|
-
* @template O - The options type for the find operation.
|
|
317
|
-
* @param selector - The criteria to select the item.
|
|
318
|
-
* @param [options] - Options for the find operation, such as projection.
|
|
319
|
-
* @returns The found item or `undefined`.
|
|
320
|
-
*/
|
|
321
|
-
findOne(selector, options) {
|
|
322
|
-
if (this.isDisposed)
|
|
323
|
-
throw new Error("Collection is disposed");
|
|
324
|
-
const cursor = this.find(selector, {
|
|
325
|
-
limit: 1,
|
|
326
|
-
...options
|
|
327
|
-
});
|
|
328
|
-
const handleItems = (items) => {
|
|
329
|
-
const returnValue = items[0] || void 0;
|
|
330
|
-
this.emit("findOne", selector, options, returnValue);
|
|
331
|
-
this.executeInDebugMode((callstack) => this.emit("_debug.findOne", callstack, selector, options, returnValue));
|
|
332
|
-
return returnValue;
|
|
333
|
-
};
|
|
334
|
-
const maybePromise = cursor.fetch();
|
|
335
|
-
return maybePromise instanceof Promise ? maybePromise.then(handleItems) : handleItems(maybePromise);
|
|
336
|
-
}
|
|
337
|
-
batch(callback) {
|
|
338
|
-
if (this.batchOperationInProgress)
|
|
339
|
-
return callback();
|
|
340
|
-
this.batchOperationInProgress = true;
|
|
341
|
-
const maybePromise = callback();
|
|
342
|
-
const afterBatch = () => {
|
|
343
|
-
this.batchOperationInProgress = false;
|
|
344
|
-
this.postBatchCallbacks.forEach((callback_) => callback_());
|
|
345
|
-
this.postBatchCallbacks.clear();
|
|
346
|
-
};
|
|
347
|
-
if (maybePromise && typeof maybePromise.then === "function") {
|
|
348
|
-
return maybePromise.then(() => afterBatch());
|
|
349
|
-
} else {
|
|
350
|
-
afterBatch();
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
onPostBatch(callback) {
|
|
354
|
-
if (this.isDisposed)
|
|
355
|
-
throw new Error("Collection is disposed");
|
|
356
|
-
if (this.batchOperationInProgress) {
|
|
357
|
-
this.postBatchCallbacks.add(callback);
|
|
358
|
-
return;
|
|
359
|
-
}
|
|
360
|
-
return callback();
|
|
361
|
-
}
|
|
362
|
-
/**
|
|
363
|
-
* Inserts a single item into the collection. Generates a unique ID if not provided.
|
|
364
|
-
* @param item - The item to insert.
|
|
365
|
-
* @returns The ID of the inserted item.
|
|
366
|
-
* @throws {Error} If the collection is disposed or the item has an invalid ID.
|
|
367
|
-
*/
|
|
368
|
-
async insert(item) {
|
|
369
|
-
if (this.isDisposed)
|
|
370
|
-
throw new Error("Collection is disposed");
|
|
371
|
-
if (!item)
|
|
372
|
-
throw new Error("Invalid item");
|
|
373
|
-
const primaryKeyGenerator = this.options.primaryKeyGenerator ?? randomId;
|
|
374
|
-
const itemWithId = {
|
|
375
|
-
id: primaryKeyGenerator(item),
|
|
376
|
-
...item
|
|
377
|
-
};
|
|
378
|
-
this.emit("validate", itemWithId);
|
|
379
|
-
const newItem = await this.withPushState(() => this.backend.insert(itemWithId));
|
|
380
|
-
this.emit("added", newItem);
|
|
381
|
-
this.emit("insert", newItem);
|
|
382
|
-
this.executeInDebugMode((callstack) => this.emit("_debug.insert", callstack, newItem));
|
|
383
|
-
return newItem.id;
|
|
384
|
-
}
|
|
385
|
-
/**
|
|
386
|
-
* Inserts multiple items into the collection. Generates unique IDs for items if not provided.
|
|
387
|
-
* @param items - The items to insert.
|
|
388
|
-
* @returns An array of IDs of the inserted items.
|
|
389
|
-
* @throws {Error} If the collection is disposed or the items are invalid.
|
|
390
|
-
*/
|
|
391
|
-
async insertMany(items) {
|
|
392
|
-
if (this.isDisposed)
|
|
393
|
-
throw new Error("Collection is disposed");
|
|
394
|
-
if (!items)
|
|
395
|
-
throw new Error("Invalid items");
|
|
396
|
-
if (items.length === 0)
|
|
397
|
-
return [];
|
|
398
|
-
const ids = [];
|
|
399
|
-
await this.batch(async () => {
|
|
400
|
-
await Promise.all(items.map(async (item) => {
|
|
401
|
-
ids.push(await this.insert(item));
|
|
402
|
-
}));
|
|
403
|
-
});
|
|
404
|
-
return ids;
|
|
405
|
-
}
|
|
406
|
-
/**
|
|
407
|
-
* Updates a single item in the collection that matches the given selector.
|
|
408
|
-
* @param selector - The criteria to select the item to update.
|
|
409
|
-
* @param modifier - The modifications to apply to the item.
|
|
410
|
-
* @param [options] - Optional settings for the update operation.
|
|
411
|
-
* @param [options.upsert] - If `true`, creates a new item if no item matches the selector.
|
|
412
|
-
* @returns The number of items updated (0 or 1).
|
|
413
|
-
* @throws {Error} If the collection is disposed or invalid arguments are provided.
|
|
414
|
-
*/
|
|
415
|
-
async updateOne(selector, modifier, options) {
|
|
416
|
-
if (this.isDisposed)
|
|
417
|
-
throw new Error("Collection is disposed");
|
|
418
|
-
if (!selector)
|
|
419
|
-
throw new Error("Invalid selector");
|
|
420
|
-
if (!modifier)
|
|
421
|
-
throw new Error("Invalid modifier");
|
|
422
|
-
const { $setOnInsert, ...restModifier } = modifier;
|
|
423
|
-
const item = await this.getItem(selector, { async: true });
|
|
424
|
-
if (item == null) {
|
|
425
|
-
if (options?.upsert) {
|
|
426
|
-
const newItem = modify({}, {
|
|
427
|
-
...restModifier,
|
|
428
|
-
$set: {
|
|
429
|
-
...$setOnInsert,
|
|
430
|
-
...restModifier.$set
|
|
431
|
-
}
|
|
432
|
-
});
|
|
433
|
-
await this.insert(newItem);
|
|
434
|
-
return 1;
|
|
435
|
-
}
|
|
436
|
-
return 0;
|
|
437
|
-
}
|
|
438
|
-
const modifiedItem = modify(deepClone.default(item), restModifier);
|
|
439
|
-
this.emit("validate", modifiedItem);
|
|
440
|
-
const changes = await this.withPushState(() => this.backend.updateOne(selector, modifier));
|
|
441
|
-
this.emit("changed", modifiedItem, restModifier);
|
|
442
|
-
this.emit("updateOne", selector, modifier);
|
|
443
|
-
this.executeInDebugMode((callstack) => this.emit("_debug.updateOne", callstack, selector, modifier));
|
|
444
|
-
return changes.length;
|
|
445
|
-
}
|
|
446
|
-
/**
|
|
447
|
-
* Updates multiple items in the collection that match the given selector.
|
|
448
|
-
* @param selector - The criteria to select the items to update.
|
|
449
|
-
* @param modifier - The modifications to apply to the items.
|
|
450
|
-
* @param [options] - Optional settings for the update operation.
|
|
451
|
-
* @param [options.upsert] - If `true`, creates new items if no items match the selector.
|
|
452
|
-
* @returns The number of items updated.
|
|
453
|
-
* @throws {Error} If the collection is disposed or invalid arguments are provided.
|
|
454
|
-
*/
|
|
455
|
-
async updateMany(selector, modifier, options) {
|
|
456
|
-
if (this.isDisposed)
|
|
457
|
-
throw new Error("Collection is disposed");
|
|
458
|
-
if (!selector)
|
|
459
|
-
throw new Error("Invalid selector");
|
|
460
|
-
if (!modifier)
|
|
461
|
-
throw new Error("Invalid modifier");
|
|
462
|
-
const { $setOnInsert, ...restModifier } = modifier;
|
|
463
|
-
const items = await this.getItems(selector, { async: true });
|
|
464
|
-
if (items.length === 0) {
|
|
465
|
-
if (options?.upsert) {
|
|
466
|
-
const newItem = modify({}, {
|
|
467
|
-
...restModifier,
|
|
468
|
-
$set: {
|
|
469
|
-
...$setOnInsert,
|
|
470
|
-
...restModifier.$set
|
|
471
|
-
}
|
|
472
|
-
});
|
|
473
|
-
await this.insert(newItem);
|
|
474
|
-
return 1;
|
|
475
|
-
}
|
|
476
|
-
return 0;
|
|
477
|
-
}
|
|
478
|
-
items.forEach((item) => {
|
|
479
|
-
const modifiedItem = modify(deepClone.default(item), restModifier);
|
|
480
|
-
this.emit("validate", modifiedItem);
|
|
481
|
-
});
|
|
482
|
-
const changes = await this.withPushState(() => this.backend.updateMany(selector, modifier));
|
|
483
|
-
changes.forEach((item) => {
|
|
484
|
-
this.emit("changed", item, restModifier);
|
|
485
|
-
});
|
|
486
|
-
this.emit("updateMany", selector, modifier);
|
|
487
|
-
this.executeInDebugMode((callstack) => this.emit("_debug.updateMany", callstack, selector, modifier));
|
|
488
|
-
return changes.length;
|
|
489
|
-
}
|
|
490
|
-
/**
|
|
491
|
-
* Replaces a single item in the collection that matches the given selector.
|
|
492
|
-
* @param selector - The criteria to select the item to replace.
|
|
493
|
-
* @param replacement - The item to replace the selected item with.
|
|
494
|
-
* @param [options] - Optional settings for the replace operation.
|
|
495
|
-
* @param [options.upsert] - If `true`, creates a new item if no item matches the selector.
|
|
496
|
-
* @returns The number of items replaced (0 or 1).
|
|
497
|
-
* @throws {Error} If the collection is disposed or invalid arguments are provided.
|
|
498
|
-
*/
|
|
499
|
-
async replaceOne(selector, replacement, options) {
|
|
500
|
-
if (this.isDisposed)
|
|
501
|
-
throw new Error("Collection is disposed");
|
|
502
|
-
if (!selector)
|
|
503
|
-
throw new Error("Invalid selector");
|
|
504
|
-
const item = await this.getItem(selector, { async: true });
|
|
505
|
-
if (item == null) {
|
|
506
|
-
if (options?.upsert) {
|
|
507
|
-
await this.insert(replacement);
|
|
508
|
-
return 1;
|
|
509
|
-
}
|
|
510
|
-
return 0;
|
|
511
|
-
}
|
|
512
|
-
const modifiedItem = { id: item.id, ...replacement };
|
|
513
|
-
this.emit("validate", modifiedItem);
|
|
514
|
-
const changes = await this.withPushState(() => this.backend.replaceOne(selector, replacement));
|
|
515
|
-
this.emit("changed", modifiedItem, replacement);
|
|
516
|
-
this.emit("replaceOne", selector, replacement);
|
|
517
|
-
this.executeInDebugMode((callstack) => this.emit("_debug.replaceOne", callstack, selector, replacement));
|
|
518
|
-
return changes.length;
|
|
519
|
-
}
|
|
520
|
-
/**
|
|
521
|
-
* Removes a single item from the collection that matches the given selector.
|
|
522
|
-
* @param selector - The criteria to select the item to remove.
|
|
523
|
-
* @returns The number of items removed (0 or 1).
|
|
524
|
-
* @throws {Error} If the collection is disposed or invalid arguments are provided.
|
|
525
|
-
*/
|
|
526
|
-
async removeOne(selector) {
|
|
527
|
-
if (this.isDisposed)
|
|
528
|
-
throw new Error("Collection is disposed");
|
|
529
|
-
if (!selector)
|
|
530
|
-
throw new Error("Invalid selector");
|
|
531
|
-
const removedItems = await this.withPushState(() => this.backend.removeOne(selector));
|
|
532
|
-
this.emit("removed", removedItems[0]);
|
|
533
|
-
this.emit("removeOne", selector);
|
|
534
|
-
this.executeInDebugMode((callstack) => this.emit("_debug.removeOne", callstack, selector));
|
|
535
|
-
return removedItems.length;
|
|
536
|
-
}
|
|
537
|
-
/**
|
|
538
|
-
* Removes multiple items from the collection that match the given selector.
|
|
539
|
-
* @param selector - The criteria to select the items to remove.
|
|
540
|
-
* @returns The number of items removed.
|
|
541
|
-
* @throws {Error} If the collection is disposed or invalid arguments are provided.
|
|
542
|
-
*/
|
|
543
|
-
async removeMany(selector) {
|
|
544
|
-
if (this.isDisposed)
|
|
545
|
-
throw new Error("Collection is disposed");
|
|
546
|
-
if (!selector)
|
|
547
|
-
throw new Error("Invalid selector");
|
|
548
|
-
const removedItems = await this.withPushState(() => this.backend.removeMany(selector));
|
|
549
|
-
removedItems.forEach((item) => {
|
|
550
|
-
this.emit("removed", item);
|
|
551
|
-
});
|
|
552
|
-
this.emit("removeMany", selector);
|
|
553
|
-
this.executeInDebugMode((callstack) => this.emit("_debug.removeMany", callstack, selector));
|
|
554
|
-
return removedItems.length;
|
|
555
|
-
}
|
|
556
|
-
}
|
|
557
|
-
exports.default = Collection;
|
package/dist/index.cjs30.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
const expressionKeys = /* @__PURE__ */ 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
|
-
function isFieldExpression(expression) {
|
|
29
|
-
if (typeof expression !== "object" || expression == null) {
|
|
30
|
-
return false;
|
|
31
|
-
}
|
|
32
|
-
const keys = Object.keys(expression);
|
|
33
|
-
if (keys.length === 0) {
|
|
34
|
-
return false;
|
|
35
|
-
}
|
|
36
|
-
const hasInvalidKeys = keys.some((key) => !expressionKeys.has(key));
|
|
37
|
-
if (hasInvalidKeys)
|
|
38
|
-
return false;
|
|
39
|
-
const hasValidKeys = keys.every((key) => expressionKeys.has(key));
|
|
40
|
-
return hasValidKeys;
|
|
41
|
-
}
|
|
42
|
-
module.exports = isFieldExpression;
|
package/dist/index.cjs31.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
function set(object, path, value, deleteIfUndefined = false) {
|
|
3
|
-
if (object == null)
|
|
4
|
-
return object;
|
|
5
|
-
const segments = path.split(/[.[\]]/g);
|
|
6
|
-
if (segments[0] === "")
|
|
7
|
-
segments.shift();
|
|
8
|
-
if (segments.at(-1) === "")
|
|
9
|
-
segments.pop();
|
|
10
|
-
const apply = (node) => {
|
|
11
|
-
if (segments.length > 1) {
|
|
12
|
-
const key = segments.shift();
|
|
13
|
-
const nextIsNumber = !Number.isNaN(Number.parseInt(segments[0], 10));
|
|
14
|
-
if (node[key] === void 0) {
|
|
15
|
-
node[key] = nextIsNumber ? [] : {};
|
|
16
|
-
}
|
|
17
|
-
apply(node[key]);
|
|
18
|
-
} else {
|
|
19
|
-
if (deleteIfUndefined && value === void 0) {
|
|
20
|
-
delete node[segments[0]];
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
node[segments[0]] = value;
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
apply(object);
|
|
27
|
-
return object;
|
|
28
|
-
}
|
|
29
|
-
module.exports = set;
|
package/dist/index.cjs32.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
function uniqueBy(array, fn) {
|
|
3
|
-
const set = /* @__PURE__ */ new Set();
|
|
4
|
-
return array.filter((element) => {
|
|
5
|
-
const value = typeof fn === "function" ? fn(element) : element[fn];
|
|
6
|
-
return !set.has(value) && set.add(value);
|
|
7
|
-
});
|
|
8
|
-
}
|
|
9
|
-
module.exports = uniqueBy;
|
package/dist/index.cjs33.js
DELETED
package/dist/index.cjs4.js
DELETED
package/dist/index.cjs5.js
DELETED
package/dist/index.cjs6.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
function isEqual(a, b) {
|
|
3
|
-
if (Object.is(a, b))
|
|
4
|
-
return true;
|
|
5
|
-
if (a instanceof RegExp && b instanceof RegExp)
|
|
6
|
-
return a.toString() === b.toString();
|
|
7
|
-
if (a instanceof Date && b instanceof Date)
|
|
8
|
-
return a.getTime() === b.getTime();
|
|
9
|
-
if (typeof a !== "object")
|
|
10
|
-
return false;
|
|
11
|
-
if (typeof b !== "object")
|
|
12
|
-
return false;
|
|
13
|
-
if (a === null)
|
|
14
|
-
return false;
|
|
15
|
-
if (b === null)
|
|
16
|
-
return false;
|
|
17
|
-
const aKeys = Object.keys(a);
|
|
18
|
-
const bKeys = Object.keys(b);
|
|
19
|
-
if (aKeys.length !== bKeys.length)
|
|
20
|
-
return false;
|
|
21
|
-
for (const key of aKeys) {
|
|
22
|
-
if (!bKeys.includes(key))
|
|
23
|
-
return false;
|
|
24
|
-
if (!isEqual(a[key], b[key]))
|
|
25
|
-
return false;
|
|
26
|
-
}
|
|
27
|
-
return true;
|
|
28
|
-
}
|
|
29
|
-
module.exports = isEqual;
|