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