@signaldb/core 1.7.0 → 2.0.0-beta.0

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 (88) hide show
  1. package/dist/.vite/manifest.json +125 -82
  2. package/dist/AsyncDataAdapter.d.ts +64 -0
  3. package/dist/AutoFetchDataAdapter.d.ts +113 -0
  4. package/dist/Collection/Cursor.d.ts +9 -9
  5. package/dist/Collection/Observer.d.ts +3 -2
  6. package/dist/Collection/index.d.ts +52 -46
  7. package/dist/Collection/types.d.ts +4 -9
  8. package/dist/DataAdapter.d.ts +33 -0
  9. package/dist/DefaultDataAdapter.d.ts +32 -0
  10. package/dist/WorkerDataAdapter.d.ts +22 -0
  11. package/dist/WorkerDataAdapterHost.d.ts +59 -0
  12. package/dist/createIndex.d.ts +7 -0
  13. package/dist/createStorageAdapter.d.ts +9 -0
  14. package/dist/{Collection/getIndexInfo.d.ts → getIndexInfo.d.ts} +17 -19
  15. package/dist/index.cjs.js +21 -17
  16. package/dist/index.cjs10.js +15 -26
  17. package/dist/index.cjs11.js +13 -10
  18. package/dist/index.cjs12.js +337 -3
  19. package/dist/index.cjs13.js +384 -115
  20. package/dist/index.cjs14.js +183 -67
  21. package/dist/index.cjs15.js +339 -8
  22. package/dist/index.cjs16.js +563 -20
  23. package/dist/index.cjs17.js +16 -12
  24. package/dist/index.cjs18.js +24 -5
  25. package/dist/index.cjs2.js +27 -36
  26. package/dist/index.cjs20.js +102 -13
  27. package/dist/index.cjs21.js +124 -21
  28. package/dist/index.cjs22.js +5 -68
  29. package/dist/index.cjs23.js +22 -82
  30. package/dist/index.cjs24.js +5 -16
  31. package/dist/index.cjs25.js +8 -27
  32. package/dist/index.cjs26.js +27 -7
  33. package/dist/index.cjs27.js +3 -5
  34. package/dist/index.cjs28.js +5 -27
  35. package/dist/index.cjs29.js +27 -40
  36. package/dist/index.cjs3.js +242 -497
  37. package/dist/index.cjs30.js +42 -0
  38. package/dist/index.cjs31.js +9 -0
  39. package/dist/index.cjs4.js +3 -170
  40. package/dist/index.cjs5.js +3 -67
  41. package/dist/index.cjs6.js +27 -3
  42. package/dist/index.cjs7.js +10 -3
  43. package/dist/index.cjs8.js +3 -3
  44. package/dist/index.cjs9.js +131 -3
  45. package/dist/index.d.ts +10 -8
  46. package/dist/index.mjs +15 -11
  47. package/dist/index10.mjs +15 -26
  48. package/dist/index11.mjs +13 -10
  49. package/dist/index12.mjs +337 -3
  50. package/dist/index13.mjs +384 -115
  51. package/dist/index14.mjs +183 -66
  52. package/dist/index15.mjs +339 -8
  53. package/dist/index16.mjs +563 -20
  54. package/dist/index17.mjs +16 -12
  55. package/dist/index18.mjs +24 -5
  56. package/dist/index2.mjs +27 -36
  57. package/dist/index20.mjs +102 -13
  58. package/dist/index21.mjs +123 -21
  59. package/dist/index22.mjs +5 -67
  60. package/dist/index23.mjs +22 -81
  61. package/dist/index24.mjs +5 -16
  62. package/dist/index25.mjs +8 -27
  63. package/dist/index26.mjs +27 -7
  64. package/dist/index27.mjs +3 -5
  65. package/dist/index28.mjs +5 -27
  66. package/dist/index29.mjs +27 -40
  67. package/dist/index3.mjs +241 -497
  68. package/dist/index30.mjs +43 -0
  69. package/dist/index31.mjs +10 -0
  70. package/dist/index4.mjs +3 -170
  71. package/dist/index5.mjs +3 -66
  72. package/dist/index6.mjs +27 -3
  73. package/dist/index7.mjs +10 -3
  74. package/dist/index8.mjs +3 -3
  75. package/dist/index9.mjs +131 -3
  76. package/dist/types/IndexProvider.d.ts +12 -7
  77. package/dist/types/StorageAdapter.d.ts +20 -0
  78. package/dist/utils/objectId.d.ts +8 -0
  79. package/dist/utils/queryId.d.ts +9 -0
  80. package/package.json +2 -2
  81. package/dist/AutoFetchCollection.d.ts +0 -60
  82. package/dist/Collection/createIndex.d.ts +0 -15
  83. package/dist/ReplicatedCollection.d.ts +0 -60
  84. package/dist/createMemoryAdapter.d.ts +0 -7
  85. package/dist/persistence/combinePersistenceAdapters.d.ts +0 -32
  86. package/dist/persistence/createPersistenceAdapter.d.ts +0 -9
  87. package/dist/types/MemoryAdapter.d.ts +0 -15
  88. package/dist/types/PersistenceAdapter.d.ts +0 -20
@@ -1,10 +1,5 @@
1
1
  "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
4
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
2
  Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
6
- const sortItems = require("./index.cjs15.js");
7
- const project = require("./index.cjs16.js");
8
3
  const Observer = require("./index.cjs17.js");
9
4
  function isInReactiveScope(reactivity) {
10
5
  if (!reactivity)
@@ -14,6 +9,10 @@ function isInReactiveScope(reactivity) {
14
9
  return reactivity.isInScope();
15
10
  }
16
11
  class Cursor {
12
+ observer;
13
+ getItems;
14
+ options;
15
+ onCleanupCallbacks = [];
17
16
  /**
18
17
  * Creates a new instance of the `Cursor` class.
19
18
  * Provides utilities for querying, observing, and transforming items from a collection.
@@ -31,11 +30,7 @@ class Cursor {
31
30
  * @param options.fieldTracking - A boolean to enable fine-grained field tracking for reactivity.
32
31
  */
33
32
  constructor(getItems, options) {
34
- __publicField(this, "observer");
35
- __publicField(this, "getFilteredItems");
36
- __publicField(this, "options");
37
- __publicField(this, "onCleanupCallbacks", []);
38
- this.getFilteredItems = getItems;
33
+ this.getItems = getItems;
39
34
  this.options = options || {};
40
35
  }
41
36
  addGetters(item) {
@@ -66,22 +61,6 @@ class Cursor {
66
61
  return item;
67
62
  return this.options.transform(item);
68
63
  }
69
- getItems() {
70
- const items = this.getFilteredItems();
71
- const { sort, skip, limit } = this.options;
72
- const sorted = sort ? sortItems(items, sort) : items;
73
- const skipped = skip ? sorted.slice(skip) : sorted;
74
- const limited = limit ? skipped.slice(0, limit) : skipped;
75
- const idExcluded = this.options.fields && this.options.fields.id === 0;
76
- return limited.map((item) => {
77
- if (!this.options.fields)
78
- return item;
79
- return {
80
- ...idExcluded ? {} : { id: item.id },
81
- ...project(item, this.options.fields)
82
- };
83
- });
84
- }
85
64
  depend(changeEvents) {
86
65
  if (!this.options.reactive)
87
66
  return;
@@ -119,7 +98,7 @@ class Cursor {
119
98
  if (!this.observer) {
120
99
  const observer = new Observer(() => {
121
100
  const requery = () => {
122
- observer.runChecks(this.getItems());
101
+ observer.runChecks(this.getItems);
123
102
  };
124
103
  const cleanup = this.options.bindEvents && this.options.bindEvents(requery);
125
104
  return () => {
@@ -135,7 +114,7 @@ class Cursor {
135
114
  observeRawChanges(callbacks, skipInitial = false) {
136
115
  const observer = this.ensureObserver();
137
116
  observer.addCallbacks(callbacks, skipInitial);
138
- observer.runChecks(this.getItems());
117
+ observer.runChecks(this.getItems);
139
118
  return () => {
140
119
  observer.removeCallbacks(callbacks);
141
120
  if (!observer.isEmpty())
@@ -169,18 +148,27 @@ class Cursor {
169
148
  * ⚡️ this function is reactive!
170
149
  * @param callback - A function to execute for each item in the result set.
171
150
  * @param callback.item - The transformed item.
151
+ * @returns A promise that resolves when all items have been processed, or void if not in async mode.
172
152
  */
173
153
  forEach(callback) {
174
- const items = this.getItems();
175
154
  this.depend({
176
155
  addedBefore: true,
177
156
  removed: true,
178
157
  movedBefore: true,
179
158
  ...this.options.fieldTracking ? {} : { changed: true }
180
159
  });
181
- items.forEach((item) => {
182
- callback(this.transform(item));
183
- });
160
+ const executeForEach = (items) => {
161
+ items.forEach((item) => {
162
+ callback(this.transform(item));
163
+ });
164
+ };
165
+ const result = this.getItems();
166
+ if (result instanceof Promise) {
167
+ return result.then(executeForEach);
168
+ } else {
169
+ executeForEach(result);
170
+ return void 0;
171
+ }
184
172
  }
185
173
  /**
186
174
  * Creates a new array populated with the results of applying the provided callback
@@ -193,9 +181,12 @@ class Cursor {
193
181
  */
194
182
  map(callback) {
195
183
  const results = [];
196
- this.forEach((item) => {
184
+ const maybePromise = this.forEach((item) => {
197
185
  results.push(callback(item));
198
186
  });
187
+ if (maybePromise instanceof Promise) {
188
+ return maybePromise.then(() => results);
189
+ }
199
190
  return results;
200
191
  }
201
192
  /**
@@ -214,12 +205,12 @@ class Cursor {
214
205
  * @returns The total number of items in the result set.
215
206
  */
216
207
  count() {
217
- const items = this.getItems();
218
208
  this.depend({
219
209
  added: true,
220
210
  removed: true
221
211
  });
222
- return items.length;
212
+ const maybePromise = this.getItems();
213
+ return maybePromise instanceof Promise ? maybePromise.then((items) => items.length) : maybePromise.length;
223
214
  }
224
215
  /**
225
216
  * Observes changes to the cursor's result set and triggers the specified callbacks
@@ -257,7 +248,7 @@ class Cursor {
257
248
  requery() {
258
249
  if (!this.observer)
259
250
  return;
260
- this.observer.runChecks(this.getItems());
251
+ this.observer.runChecks(this.getItems);
261
252
  }
262
253
  }
263
254
  exports.default = Cursor;
@@ -1,15 +1,104 @@
1
1
  "use strict";
2
- function serializeValue(value) {
3
- if (value == null)
4
- return null;
5
- if (typeof value === "string")
6
- return value;
7
- if (typeof value === "number")
8
- return value.toString();
9
- if (typeof value === "boolean")
10
- return value.toString();
11
- if (value instanceof Date)
12
- return value.toISOString();
13
- return JSON.stringify(value);
2
+ const createIndexProvider = require("./index.cjs27.js");
3
+ const get = require("./index.cjs10.js");
4
+ const getMatchingKeys = require("./index.cjs26.js");
5
+ const serializeValue = require("./index.cjs11.js");
6
+ function createIndex(field) {
7
+ const index = /* @__PURE__ */ new Map();
8
+ const ensureSet = (key) => {
9
+ let set = index.get(key);
10
+ if (!set) {
11
+ set = /* @__PURE__ */ new Set();
12
+ index.set(key, set);
13
+ }
14
+ return set;
15
+ };
16
+ return createIndexProvider({
17
+ query(selector) {
18
+ if (!Object.hasOwnProperty.call(selector, field)) {
19
+ return { matched: false };
20
+ }
21
+ const fieldSelector = selector[field];
22
+ const filteresForNull = fieldSelector == null || fieldSelector.$exists === false;
23
+ const keys = filteresForNull ? { include: null, exclude: [...index.keys()].filter((key) => key != null) } : getMatchingKeys(field, selector);
24
+ if (keys.include == null && keys.exclude == null)
25
+ return { matched: false };
26
+ let includedIds = [];
27
+ if (keys.include == null) {
28
+ for (const set of index.values()) {
29
+ for (const pos of set) {
30
+ includedIds.push(pos);
31
+ }
32
+ }
33
+ } else {
34
+ for (const key of keys.include) {
35
+ const posSet = index.get(key);
36
+ if (posSet) {
37
+ for (const pos of posSet) {
38
+ includedIds.push(pos);
39
+ }
40
+ }
41
+ }
42
+ }
43
+ if (keys.exclude != null) {
44
+ const excludeIds = /* @__PURE__ */ new Set();
45
+ for (const key of keys.exclude) {
46
+ const posSet = index.get(key);
47
+ if (posSet) {
48
+ for (const pos of posSet) {
49
+ excludeIds.add(pos);
50
+ }
51
+ }
52
+ }
53
+ includedIds = includedIds.filter((pos) => !excludeIds.has(pos));
54
+ }
55
+ return {
56
+ matched: true,
57
+ ids: includedIds,
58
+ fields: [field],
59
+ keepSelector: filteresForNull
60
+ };
61
+ },
62
+ rebuild(items) {
63
+ index.clear();
64
+ items.forEach((item) => {
65
+ const value = serializeValue(get(item, field));
66
+ ensureSet(value).add(item.id);
67
+ });
68
+ },
69
+ // NEW: delta methods
70
+ insert(items) {
71
+ for (const item of items) {
72
+ const value = serializeValue(get(item, field));
73
+ ensureSet(value).add(item.id);
74
+ }
75
+ },
76
+ remove(items) {
77
+ for (const item of items) {
78
+ const value = serializeValue(get(item, field));
79
+ const set = index.get(value);
80
+ if (!set)
81
+ continue;
82
+ set.delete(item.id);
83
+ if (set.size === 0)
84
+ index.delete(value);
85
+ }
86
+ },
87
+ update(pairs) {
88
+ for (const { oldItem, newItem } of pairs) {
89
+ const oldValue = serializeValue(get(oldItem, field));
90
+ const newValue = serializeValue(get(newItem, field));
91
+ if (oldValue === newValue)
92
+ continue;
93
+ const oldSet = index.get(oldValue);
94
+ if (oldSet) {
95
+ oldSet.delete(oldItem.id);
96
+ if (oldSet.size === 0)
97
+ index.delete(oldValue);
98
+ }
99
+ ensureSet(newValue).add(newItem.id);
100
+ }
101
+ }
102
+ });
14
103
  }
15
- module.exports = serializeValue;
104
+ module.exports = createIndex;
@@ -1,26 +1,129 @@
1
1
  "use strict";
2
- function createSignal(reactivityAdapter, initialValue, isEqual = Object.is) {
3
- let value = initialValue;
4
- const dependency = reactivityAdapter == null ? void 0 : reactivityAdapter.create();
5
- const isInReactiveScope = () => {
6
- if (!(reactivityAdapter == null ? void 0 : reactivityAdapter.isInScope))
7
- return true;
8
- return reactivityAdapter.isInScope();
9
- };
10
- const signal = {
11
- get() {
12
- if (dependency && isInReactiveScope())
13
- dependency.depend();
14
- return value;
15
- },
16
- set(newValue) {
17
- if (isEqual(value, newValue))
2
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
3
+ const intersection = require("./index.cjs28.js");
4
+ function getMergedIndexInfo(queryFunctions, selector) {
5
+ return queryFunctions.reduce((memoOrPromise, queryFunction) => {
6
+ const resultOrPromise = queryFunction(selector);
7
+ const processResult = (memo2, result) => {
8
+ if (!result.matched)
9
+ return memo2;
10
+ const optimizedSelector = result.keepSelector ? memo2.optimizedSelector : Object.fromEntries(Object.entries(memo2.optimizedSelector).filter(([key]) => !result.fields.includes(key)));
11
+ return {
12
+ matched: true,
13
+ ids: [...new Set(memo2.matched ? intersection(memo2.ids, result.ids) : result.ids)],
14
+ optimizedSelector
15
+ };
16
+ };
17
+ if (resultOrPromise instanceof Promise) {
18
+ return resultOrPromise.then(async (result) => {
19
+ const memo2 = memoOrPromise instanceof Promise ? await memoOrPromise : memoOrPromise;
20
+ return processResult(memo2, result);
21
+ });
22
+ }
23
+ const memo = memoOrPromise;
24
+ if (memo instanceof Promise)
25
+ throw new Error("Mixing async and sync index providers is not supported");
26
+ return processResult(memo, resultOrPromise);
27
+ }, {
28
+ matched: false,
29
+ ids: [],
30
+ optimizedSelector: { ...selector }
31
+ });
32
+ }
33
+ function optimizeLogicGate(queryFunctions, logicGate, idsCallback) {
34
+ return logicGate.reduce((memoOrPromise, sel) => {
35
+ const getSelector = (indexInfo) => {
36
+ const { matched: selMatched, ids: selIds, optimizedSelector: optimizedSelector2 } = indexInfo;
37
+ if (selMatched) {
38
+ idsCallback(true, selIds);
39
+ if (Object.keys(optimizedSelector2).length > 0) {
40
+ return optimizedSelector2;
41
+ }
42
+ } else {
43
+ idsCallback(false, []);
44
+ return sel;
45
+ }
46
+ };
47
+ const indexInfoOrPromise = getIndexInfo(queryFunctions, sel);
48
+ if (indexInfoOrPromise instanceof Promise) {
49
+ return indexInfoOrPromise.then(async (indexInfo) => {
50
+ const memo2 = memoOrPromise instanceof Promise ? await memoOrPromise : memoOrPromise;
51
+ const optimizedSelector2 = getSelector(indexInfo);
52
+ if (optimizedSelector2)
53
+ memo2.push(optimizedSelector2);
54
+ return memo2;
55
+ });
56
+ }
57
+ const memo = memoOrPromise;
58
+ if (memo instanceof Promise)
59
+ throw new Error("Mixing async and sync index providers is not supported");
60
+ const optimizedSelector = getSelector(indexInfoOrPromise);
61
+ if (optimizedSelector)
62
+ memo.push(optimizedSelector);
63
+ return memo;
64
+ }, []);
65
+ }
66
+ function getIndexInfo(queryFunctions, selector) {
67
+ if (selector == null || Object.keys(selector).length <= 0) {
68
+ return {
69
+ matched: false,
70
+ ids: [],
71
+ optimizedSelector: selector
72
+ };
73
+ }
74
+ const { $and, $or, ...rest } = selector;
75
+ const flatInfoOrPromise = getMergedIndexInfo(queryFunctions, rest);
76
+ const processFlatInfo = (flatInfo) => {
77
+ let { matched, ids } = flatInfo;
78
+ const newSelector = flatInfo.optimizedSelector;
79
+ const $andNewOrPromise = Array.isArray($and) ? optimizeLogicGate(queryFunctions, $and, (match, selIds) => {
80
+ if (!match)
18
81
  return;
19
- value = newValue;
20
- if (dependency)
21
- dependency.notify();
82
+ ids = matched ? intersection(ids, selIds) : selIds;
83
+ matched = true;
84
+ }) : void 0;
85
+ const process$and = ($andNew) => {
86
+ if ($andNew && $andNew.length > 0)
87
+ newSelector.$and = $andNew;
88
+ let hasNonIndexField = false;
89
+ const matchedBefore = matched;
90
+ const idsBefore = ids;
91
+ const process$or = ($orNew) => {
92
+ if ($orNew && $orNew.length > 0)
93
+ newSelector.$or = $orNew;
94
+ if (hasNonIndexField) {
95
+ newSelector.$or = $or;
96
+ matched = matchedBefore;
97
+ ids = idsBefore;
98
+ }
99
+ return {
100
+ matched,
101
+ ids: ids || [],
102
+ optimizedSelector: newSelector
103
+ };
104
+ };
105
+ const $orNewOrPromise = Array.isArray($or) ? optimizeLogicGate(queryFunctions, $or, (match, selIds) => {
106
+ if (match) {
107
+ ids = [.../* @__PURE__ */ new Set([...ids, ...selIds])];
108
+ matched = true;
109
+ } else {
110
+ hasNonIndexField = true;
111
+ }
112
+ }) : void 0;
113
+ if ($orNewOrPromise instanceof Promise) {
114
+ return $orNewOrPromise.then(($orNew) => process$or($orNew));
115
+ }
116
+ return process$or($orNewOrPromise);
117
+ };
118
+ if ($andNewOrPromise instanceof Promise) {
119
+ return $andNewOrPromise.then(($andNew) => process$and($andNew));
22
120
  }
121
+ return process$and($andNewOrPromise);
23
122
  };
24
- return signal;
123
+ if (flatInfoOrPromise instanceof Promise) {
124
+ return flatInfoOrPromise.then(processFlatInfo);
125
+ }
126
+ return processFlatInfo(flatInfoOrPromise);
25
127
  }
26
- module.exports = createSignal;
128
+ exports.default = getIndexInfo;
129
+ exports.getMergedIndexInfo = getMergedIndexInfo;
@@ -1,70 +1,7 @@
1
1
  "use strict";
2
- Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
3
- const intersection = require("./index.cjs27.js");
4
- function getMergedIndexInfo(indexProviders, selector) {
5
- return indexProviders.reduce((memo, indexProvider) => {
6
- const info = indexProvider.query(selector);
7
- if (!info.matched)
8
- return memo;
9
- const optimizedSelector = info.keepSelector ? memo.optimizedSelector : Object.fromEntries(Object.entries(memo.optimizedSelector).filter(([key]) => !info.fields.includes(key)));
10
- return {
11
- matched: true,
12
- positions: [...new Set(memo.matched ? intersection(memo.positions, info.positions) : info.positions)],
13
- optimizedSelector
14
- };
15
- }, {
16
- matched: false,
17
- positions: [],
18
- optimizedSelector: { ...selector }
19
- });
2
+ const mingo = require("mingo");
3
+ function match(item, selector) {
4
+ const query = new mingo.Query(selector);
5
+ return query.test(item);
20
6
  }
21
- function getIndexInfo(indexProviders, selector) {
22
- if (selector == null || Object.keys(selector).length <= 0) {
23
- return { matched: false, positions: [], optimizedSelector: selector };
24
- }
25
- const { $and, $or, ...rest } = selector;
26
- const flatInfo = getMergedIndexInfo(indexProviders, rest);
27
- let { matched, positions } = flatInfo;
28
- const newSelector = flatInfo.optimizedSelector;
29
- if (Array.isArray($and)) {
30
- const $andNew = [];
31
- for (const sel of $and) {
32
- const { matched: selMatched, positions: selPositions, optimizedSelector } = getIndexInfo(indexProviders, sel);
33
- if (selMatched) {
34
- positions = matched ? intersection(positions, selPositions) : selPositions;
35
- matched = true;
36
- if (Object.keys(optimizedSelector).length > 0) {
37
- $andNew.push(optimizedSelector);
38
- }
39
- } else {
40
- $andNew.push(sel);
41
- }
42
- }
43
- if ($andNew.length > 0)
44
- newSelector.$and = $andNew;
45
- }
46
- if (Array.isArray($or)) {
47
- const $orNew = [];
48
- for (const sel of $or) {
49
- const { matched: selMatched, positions: selPositions, optimizedSelector } = getIndexInfo(indexProviders, sel);
50
- if (selMatched) {
51
- positions = [.../* @__PURE__ */ new Set([...positions, ...selPositions])];
52
- matched = true;
53
- if (Object.keys(optimizedSelector).length > 0) {
54
- $orNew.push(optimizedSelector);
55
- }
56
- } else {
57
- $orNew.push(sel);
58
- }
59
- }
60
- if ($orNew.length > 0)
61
- newSelector.$or = $orNew;
62
- }
63
- return {
64
- matched,
65
- positions: positions || [],
66
- optimizedSelector: newSelector
67
- };
68
- }
69
- exports.default = getIndexInfo;
70
- exports.getMergedIndexInfo = getMergedIndexInfo;
7
+ module.exports = match;
@@ -1,87 +1,27 @@
1
1
  "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
4
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
- Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
6
- const index = require("./index.cjs3.js");
7
- const combinePersistenceAdapters = require("./index.cjs5.js");
8
- const createPersistenceAdapter = require("./index.cjs8.js");
9
- const createSignal = require("./index.cjs21.js");
10
- function createReplicationAdapter(options) {
11
- return createPersistenceAdapter({
12
- async register(onChange) {
13
- if (!options.registerRemoteChange)
2
+ const get = require("./index.cjs10.js");
3
+ const set = require("./index.cjs29.js");
4
+ function project(item, fields) {
5
+ const allFieldsDeactivated = Object.values(fields).every((value) => value === 0);
6
+ if (allFieldsDeactivated) {
7
+ const result2 = { ...item };
8
+ Object.keys(fields).forEach((key) => {
9
+ const fieldValue = get(item, key);
10
+ if (fieldValue === void 0)
14
11
  return;
15
- await options.registerRemoteChange(onChange);
16
- },
17
- load: () => options.pull(),
18
- save: (items, changes) => {
19
- if (!options.push)
20
- throw new Error("Pushing is not configured for this collection. Try to pass a `push` function to the collection options.");
21
- return options.push(changes, items);
22
- }
23
- });
24
- }
25
- class ReplicatedCollection extends index.default {
26
- /**
27
- * Creates a new instance of the `ReplicatedCollection` class.
28
- * Sets up the replication adapter, combining it with an optional persistence adapter, and
29
- * initializes signals for tracking remote pull and push operations.
30
- * @param options - The configuration options for the replicated collection.
31
- * @param options.pull - A function to fetch data from the remote source.
32
- * @param options.push - An optional function to send changes and items to the remote source.
33
- * @param options.registerRemoteChange - An optional function to register a listener for remote changes.
34
- * @param options.persistence - An optional persistence adapter to combine with replication.
35
- * @param options.reactivity - A reactivity adapter for observing changes in the collection.
36
- * @param options.transform - A transformation function to apply to items when retrieving them.
37
- * @param options.indices - An array of index providers for optimized querying.
38
- * @param options.enableDebugMode - A boolean to enable or disable debug mode.
39
- */
40
- constructor(options) {
41
- const replicationAdapter = createReplicationAdapter({
42
- registerRemoteChange: options.registerRemoteChange,
43
- pull: async () => {
44
- this.isPullingRemoteSignal.set(true);
45
- try {
46
- return await options.pull();
47
- } finally {
48
- this.isPullingRemoteSignal.set(false);
49
- }
50
- },
51
- push: options.push ? async (changes, items) => {
52
- if (!options.push)
53
- throw new Error("Pushing is not configured for this collection. Try to pass a `push` function to the collection options.");
54
- this.isPushingRemoteSignal.set(true);
55
- try {
56
- await options.push(changes, items);
57
- } finally {
58
- this.isPushingRemoteSignal.set(false);
59
- }
60
- } : void 0
61
- });
62
- const persistenceAdapter = (options == null ? void 0 : options.persistence) ? combinePersistenceAdapters.default(replicationAdapter, options.persistence) : replicationAdapter;
63
- super({
64
- ...options,
65
- persistence: persistenceAdapter
12
+ set(result2, key, void 0, true);
66
13
  });
67
- __publicField(this, "isPullingRemoteSignal");
68
- __publicField(this, "isPushingRemoteSignal");
69
- this.isPullingRemoteSignal = createSignal(options.reactivity, false);
70
- this.isPushingRemoteSignal = createSignal(options.reactivity, false);
71
- }
72
- /**
73
- * Checks whether the collection is currently performing any loading operation,
74
- * including pulling or pushing data from/to the remote source, or standard
75
- * persistence adapter operations.
76
- * ⚡️ this function is reactive!
77
- * @returns A boolean indicating if the collection is currently loading or synchronizing.
78
- */
79
- isLoading() {
80
- const isPullingRemote = this.isPullingRemoteSignal.get();
81
- const isPushingRemote = this.isPushingRemoteSignal.get();
82
- const isLoading = super.isLoading();
83
- return isPullingRemote || isPushingRemote || isLoading;
14
+ return result2;
84
15
  }
16
+ const result = {};
17
+ Object.entries(fields).forEach(([key, value]) => {
18
+ const fieldValue = get(item, key);
19
+ if (fieldValue === void 0)
20
+ return;
21
+ if (fieldValue == null && value !== 1)
22
+ return;
23
+ set(result, key, value === 1 ? fieldValue : void 0);
24
+ });
25
+ return result;
85
26
  }
86
- exports.createReplicationAdapter = createReplicationAdapter;
87
- exports.default = ReplicatedCollection;
27
+ module.exports = project;
@@ -1,18 +1,7 @@
1
1
  "use strict";
2
- function get(value, path) {
3
- const normalized = path.replaceAll(/\[(\w+)\]/g, ".$1");
4
- if (normalized.includes("..") || normalized.startsWith(".") || normalized.endsWith(".")) {
5
- return;
6
- }
7
- const segments = normalized.split(".");
8
- let current = value;
9
- for (const key of segments) {
10
- if (current == null)
11
- return;
12
- current = current[key];
13
- }
14
- if (current === void 0)
15
- return;
16
- return current;
2
+ function queryId(selector, options) {
3
+ const selectorId = JSON.stringify(selector);
4
+ const optionsId = options == null ? -1 : JSON.stringify(options);
5
+ return `${selectorId}:${optionsId}`;
17
6
  }
18
- module.exports = get;
7
+ module.exports = queryId;
@@ -1,29 +1,10 @@
1
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;
2
+ const fastSort = require("fast-sort");
3
+ const get = require("./index.cjs10.js");
4
+ function sortItems(items, sortFields) {
5
+ return fastSort.sort(items).by(Object.entries(sortFields).map(([key, value]) => {
6
+ const order = value === 1 ? "asc" : "desc";
7
+ return { [order]: (i) => get(i, key) };
8
+ }));
28
9
  }
29
- module.exports = set;
10
+ module.exports = sortItems;