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