@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
package/dist/index2.mjs CHANGED
@@ -1,8 +1,3 @@
1
- var __defProp = Object.defineProperty;
2
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
- import sortItems from "./index15.mjs";
5
- import project from "./index16.mjs";
6
1
  import Observer from "./index17.mjs";
7
2
  function isInReactiveScope(reactivity) {
8
3
  if (!reactivity)
@@ -12,6 +7,10 @@ function isInReactiveScope(reactivity) {
12
7
  return reactivity.isInScope();
13
8
  }
14
9
  class Cursor {
10
+ observer;
11
+ getItems;
12
+ options;
13
+ onCleanupCallbacks = [];
15
14
  /**
16
15
  * Creates a new instance of the `Cursor` class.
17
16
  * Provides utilities for querying, observing, and transforming items from a collection.
@@ -29,11 +28,7 @@ class Cursor {
29
28
  * @param options.fieldTracking - A boolean to enable fine-grained field tracking for reactivity.
30
29
  */
31
30
  constructor(getItems, options) {
32
- __publicField(this, "observer");
33
- __publicField(this, "getFilteredItems");
34
- __publicField(this, "options");
35
- __publicField(this, "onCleanupCallbacks", []);
36
- this.getFilteredItems = getItems;
31
+ this.getItems = getItems;
37
32
  this.options = options || {};
38
33
  }
39
34
  addGetters(item) {
@@ -64,22 +59,6 @@ class Cursor {
64
59
  return item;
65
60
  return this.options.transform(item);
66
61
  }
67
- getItems() {
68
- const items = this.getFilteredItems();
69
- const { sort, skip, limit } = this.options;
70
- const sorted = sort ? sortItems(items, sort) : items;
71
- const skipped = skip ? sorted.slice(skip) : sorted;
72
- const limited = limit ? skipped.slice(0, limit) : skipped;
73
- const idExcluded = this.options.fields && this.options.fields.id === 0;
74
- return limited.map((item) => {
75
- if (!this.options.fields)
76
- return item;
77
- return {
78
- ...idExcluded ? {} : { id: item.id },
79
- ...project(item, this.options.fields)
80
- };
81
- });
82
- }
83
62
  depend(changeEvents) {
84
63
  if (!this.options.reactive)
85
64
  return;
@@ -117,7 +96,7 @@ class Cursor {
117
96
  if (!this.observer) {
118
97
  const observer = new Observer(() => {
119
98
  const requery = () => {
120
- observer.runChecks(this.getItems());
99
+ observer.runChecks(this.getItems);
121
100
  };
122
101
  const cleanup = this.options.bindEvents && this.options.bindEvents(requery);
123
102
  return () => {
@@ -133,7 +112,7 @@ class Cursor {
133
112
  observeRawChanges(callbacks, skipInitial = false) {
134
113
  const observer = this.ensureObserver();
135
114
  observer.addCallbacks(callbacks, skipInitial);
136
- observer.runChecks(this.getItems());
115
+ observer.runChecks(this.getItems);
137
116
  return () => {
138
117
  observer.removeCallbacks(callbacks);
139
118
  if (!observer.isEmpty())
@@ -167,18 +146,27 @@ class Cursor {
167
146
  * ⚡️ this function is reactive!
168
147
  * @param callback - A function to execute for each item in the result set.
169
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.
170
150
  */
171
151
  forEach(callback) {
172
- const items = this.getItems();
173
152
  this.depend({
174
153
  addedBefore: true,
175
154
  removed: true,
176
155
  movedBefore: true,
177
156
  ...this.options.fieldTracking ? {} : { changed: true }
178
157
  });
179
- items.forEach((item) => {
180
- callback(this.transform(item));
181
- });
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
+ }
182
170
  }
183
171
  /**
184
172
  * Creates a new array populated with the results of applying the provided callback
@@ -191,9 +179,12 @@ class Cursor {
191
179
  */
192
180
  map(callback) {
193
181
  const results = [];
194
- this.forEach((item) => {
182
+ const maybePromise = this.forEach((item) => {
195
183
  results.push(callback(item));
196
184
  });
185
+ if (maybePromise instanceof Promise) {
186
+ return maybePromise.then(() => results);
187
+ }
197
188
  return results;
198
189
  }
199
190
  /**
@@ -212,12 +203,12 @@ class Cursor {
212
203
  * @returns The total number of items in the result set.
213
204
  */
214
205
  count() {
215
- const items = this.getItems();
216
206
  this.depend({
217
207
  added: true,
218
208
  removed: true
219
209
  });
220
- return items.length;
210
+ const maybePromise = this.getItems();
211
+ return maybePromise instanceof Promise ? maybePromise.then((items) => items.length) : maybePromise.length;
221
212
  }
222
213
  /**
223
214
  * Observes changes to the cursor's result set and triggers the specified callbacks
@@ -255,7 +246,7 @@ class Cursor {
255
246
  requery() {
256
247
  if (!this.observer)
257
248
  return;
258
- this.observer.runChecks(this.getItems());
249
+ this.observer.runChecks(this.getItems);
259
250
  }
260
251
  }
261
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 == null ? void 0 : reactivityAdapter.create();
4
- const isInReactiveScope = () => {
5
- if (!(reactivityAdapter == null ? void 0 : 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,70 +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
- for (const sel of $or) {
47
- const { matched: selMatched, positions: selPositions, optimizedSelector } = getIndexInfo(indexProviders, sel);
48
- if (selMatched) {
49
- positions = [.../* @__PURE__ */ new Set([...positions, ...selPositions])];
50
- matched = true;
51
- if (Object.keys(optimizedSelector).length > 0) {
52
- $orNew.push(optimizedSelector);
53
- }
54
- } else {
55
- $orNew.push(sel);
56
- }
57
- }
58
- if ($orNew.length > 0)
59
- newSelector.$or = $orNew;
60
- }
61
- return {
62
- matched,
63
- positions: positions || [],
64
- optimizedSelector: newSelector
65
- };
1
+ import { Query } from "mingo";
2
+ function match(item, selector) {
3
+ const query = new Query(selector);
4
+ return query.test(item);
66
5
  }
67
6
  export {
68
- getIndexInfo as default,
69
- getMergedIndexInfo
7
+ match as default
70
8
  };
package/dist/index23.mjs CHANGED
@@ -1,87 +1,28 @@
1
- var __defProp = Object.defineProperty;
2
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
- import Collection from "./index3.mjs";
5
- import combinePersistenceAdapters from "./index5.mjs";
6
- import createPersistenceAdapter from "./index8.mjs";
7
- import createSignal from "./index21.mjs";
8
- function createReplicationAdapter(options) {
9
- return createPersistenceAdapter({
10
- async register(onChange) {
11
- 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)
12
10
  return;
13
- await options.registerRemoteChange(onChange);
14
- },
15
- load: () => options.pull(),
16
- save: (items, changes) => {
17
- if (!options.push)
18
- throw new Error("Pushing is not configured for this collection. Try to pass a `push` function to the collection options.");
19
- return options.push(changes, items);
20
- }
21
- });
22
- }
23
- class ReplicatedCollection extends Collection {
24
- /**
25
- * Creates a new instance of the `ReplicatedCollection` class.
26
- * Sets up the replication adapter, combining it with an optional persistence adapter, and
27
- * initializes signals for tracking remote pull and push operations.
28
- * @param options - The configuration options for the replicated collection.
29
- * @param options.pull - A function to fetch data from the remote source.
30
- * @param options.push - An optional function to send changes and items to the remote source.
31
- * @param options.registerRemoteChange - An optional function to register a listener for remote changes.
32
- * @param options.persistence - An optional persistence adapter to combine with replication.
33
- * @param options.reactivity - A reactivity adapter for observing changes in the collection.
34
- * @param options.transform - A transformation function to apply to items when retrieving them.
35
- * @param options.indices - An array of index providers for optimized querying.
36
- * @param options.enableDebugMode - A boolean to enable or disable debug mode.
37
- */
38
- constructor(options) {
39
- const replicationAdapter = createReplicationAdapter({
40
- registerRemoteChange: options.registerRemoteChange,
41
- pull: async () => {
42
- this.isPullingRemoteSignal.set(true);
43
- try {
44
- return await options.pull();
45
- } finally {
46
- this.isPullingRemoteSignal.set(false);
47
- }
48
- },
49
- push: options.push ? async (changes, items) => {
50
- if (!options.push)
51
- throw new Error("Pushing is not configured for this collection. Try to pass a `push` function to the collection options.");
52
- this.isPushingRemoteSignal.set(true);
53
- try {
54
- await options.push(changes, items);
55
- } finally {
56
- this.isPushingRemoteSignal.set(false);
57
- }
58
- } : void 0
59
- });
60
- const persistenceAdapter = (options == null ? void 0 : options.persistence) ? combinePersistenceAdapters(replicationAdapter, options.persistence) : replicationAdapter;
61
- super({
62
- ...options,
63
- persistence: persistenceAdapter
11
+ set(result2, key, void 0, true);
64
12
  });
65
- __publicField(this, "isPullingRemoteSignal");
66
- __publicField(this, "isPushingRemoteSignal");
67
- this.isPullingRemoteSignal = createSignal(options.reactivity, false);
68
- this.isPushingRemoteSignal = createSignal(options.reactivity, false);
69
- }
70
- /**
71
- * Checks whether the collection is currently performing any loading operation,
72
- * including pulling or pushing data from/to the remote source, or standard
73
- * persistence adapter operations.
74
- * ⚡️ this function is reactive!
75
- * @returns A boolean indicating if the collection is currently loading or synchronizing.
76
- */
77
- isLoading() {
78
- const isPullingRemote = this.isPullingRemoteSignal.get();
79
- const isPushingRemote = this.isPushingRemoteSignal.get();
80
- const isLoading = super.isLoading();
81
- return isPullingRemote || isPushingRemote || isLoading;
13
+ return result2;
82
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;
83
25
  }
84
26
  export {
85
- createReplicationAdapter,
86
- ReplicatedCollection as default
27
+ project as default
87
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
  };