@signaldb/core 2.0.0-beta.6 → 2.0.0-beta.7

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