@signaldb/core 1.7.1 → 2.0.0-beta.1

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 (93) hide show
  1. package/dist/.vite/manifest.json +138 -82
  2. package/dist/AsyncDataAdapter.d.ts +64 -0
  3. package/dist/AutoFetchDataAdapter.d.ts +112 -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 +54 -46
  7. package/dist/Collection/types.d.ts +4 -9
  8. package/dist/DataAdapter.d.ts +34 -0
  9. package/dist/DefaultDataAdapter.d.ts +33 -0
  10. package/dist/WorkerDataAdapter.d.ts +25 -0
  11. package/dist/WorkerDataAdapterHost.d.ts +62 -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 +348 -3
  19. package/dist/index.cjs13.js +396 -110
  20. package/dist/index.cjs14.js +178 -68
  21. package/dist/index.cjs15.js +384 -8
  22. package/dist/index.cjs16.js +572 -20
  23. package/dist/index.cjs17.js +12 -4
  24. package/dist/index.cjs18.js +24 -5
  25. package/dist/index.cjs2.js +29 -32
  26. package/dist/index.cjs20.js +5 -13
  27. package/dist/index.cjs21.js +99 -21
  28. package/dist/index.cjs22.js +108 -58
  29. package/dist/index.cjs23.js +5 -82
  30. package/dist/index.cjs24.js +23 -14
  31. package/dist/index.cjs25.js +8 -27
  32. package/dist/index.cjs26.js +27 -7
  33. package/dist/index.cjs27.js +44 -5
  34. package/dist/index.cjs28.js +6 -27
  35. package/dist/index.cjs29.js +7 -40
  36. package/dist/index.cjs3.js +198 -444
  37. package/dist/index.cjs30.js +5 -0
  38. package/dist/index.cjs31.js +7 -0
  39. package/dist/index.cjs32.js +29 -0
  40. package/dist/index.cjs33.js +42 -0
  41. package/dist/index.cjs4.js +3 -165
  42. package/dist/index.cjs5.js +3 -66
  43. package/dist/index.cjs6.js +27 -3
  44. package/dist/index.cjs7.js +10 -3
  45. package/dist/index.cjs8.js +3 -3
  46. package/dist/index.cjs9.js +131 -3
  47. package/dist/index.d.ts +11 -8
  48. package/dist/index.mjs +15 -11
  49. package/dist/index10.mjs +15 -26
  50. package/dist/index11.mjs +13 -10
  51. package/dist/index12.mjs +348 -3
  52. package/dist/index13.mjs +396 -110
  53. package/dist/index14.mjs +178 -67
  54. package/dist/index15.mjs +384 -8
  55. package/dist/index16.mjs +572 -20
  56. package/dist/index17.mjs +12 -4
  57. package/dist/index18.mjs +24 -5
  58. package/dist/index2.mjs +29 -32
  59. package/dist/index20.mjs +5 -13
  60. package/dist/index21.mjs +99 -21
  61. package/dist/index22.mjs +108 -58
  62. package/dist/index23.mjs +5 -81
  63. package/dist/index24.mjs +23 -14
  64. package/dist/index25.mjs +8 -27
  65. package/dist/index26.mjs +27 -7
  66. package/dist/index27.mjs +44 -5
  67. package/dist/index28.mjs +6 -27
  68. package/dist/index29.mjs +7 -40
  69. package/dist/index3.mjs +198 -445
  70. package/dist/index30.mjs +6 -0
  71. package/dist/index31.mjs +8 -0
  72. package/dist/index32.mjs +30 -0
  73. package/dist/index33.mjs +43 -0
  74. package/dist/index4.mjs +3 -165
  75. package/dist/index5.mjs +3 -65
  76. package/dist/index6.mjs +27 -3
  77. package/dist/index7.mjs +10 -3
  78. package/dist/index8.mjs +3 -3
  79. package/dist/index9.mjs +131 -3
  80. package/dist/types/IndexProvider.d.ts +12 -7
  81. package/dist/types/StorageAdapter.d.ts +20 -0
  82. package/dist/utils/batchOnNextTick.d.ts +16 -0
  83. package/dist/utils/objectId.d.ts +8 -0
  84. package/dist/utils/queryId.d.ts +9 -0
  85. package/package.json +1 -1
  86. package/dist/AutoFetchCollection.d.ts +0 -60
  87. package/dist/Collection/createIndex.d.ts +0 -15
  88. package/dist/ReplicatedCollection.d.ts +0 -60
  89. package/dist/createMemoryAdapter.d.ts +0 -7
  90. package/dist/persistence/combinePersistenceAdapters.d.ts +0 -32
  91. package/dist/persistence/createPersistenceAdapter.d.ts +0 -9
  92. package/dist/types/MemoryAdapter.d.ts +0 -15
  93. 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,26 +59,13 @@ 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
- if (!this.options.reactive)
63
+ if (this.options?.async)
82
64
  return;
83
- if (!isInReactiveScope(this.options.reactive))
65
+ if (!isInReactiveScope(this.options.reactive)) {
66
+ console.warn("Cursor.depend() called outside of a reactive scope without async option; consider using { async: true } or wrapping in a reactive scope");
67
+ }
68
+ if (!this.options.reactive)
84
69
  return;
85
70
  const signal = this.options.reactive.create();
86
71
  signal.depend();
@@ -114,7 +99,7 @@ class Cursor {
114
99
  if (!this.observer) {
115
100
  const observer = new Observer(() => {
116
101
  const requery = () => {
117
- observer.runChecks(this.getItems());
102
+ observer.runChecks(this.getItems);
118
103
  };
119
104
  const cleanup = this.options.bindEvents && this.options.bindEvents(requery);
120
105
  return () => {
@@ -130,7 +115,7 @@ class Cursor {
130
115
  observeRawChanges(callbacks, skipInitial = false) {
131
116
  const observer = this.ensureObserver();
132
117
  observer.addCallbacks(callbacks, skipInitial);
133
- observer.runChecks(this.getItems());
118
+ observer.runChecks(this.getItems);
134
119
  return () => {
135
120
  observer.removeCallbacks(callbacks);
136
121
  if (!observer.isEmpty())
@@ -164,18 +149,27 @@ class Cursor {
164
149
  * ⚡️ this function is reactive!
165
150
  * @param callback - A function to execute for each item in the result set.
166
151
  * @param callback.item - The transformed item.
152
+ * @returns A promise that resolves when all items have been processed, or void if not in async mode.
167
153
  */
168
154
  forEach(callback) {
169
- const items = this.getItems();
170
155
  this.depend({
171
156
  addedBefore: true,
172
157
  removed: true,
173
158
  movedBefore: true,
174
159
  ...this.options.fieldTracking ? {} : { changed: true }
175
160
  });
176
- items.forEach((item) => {
177
- callback(this.transform(item));
178
- });
161
+ const executeForEach = (items) => {
162
+ items.forEach((item) => {
163
+ callback(this.transform(item));
164
+ });
165
+ };
166
+ const result = this.getItems();
167
+ if (result instanceof Promise) {
168
+ return result.then(executeForEach);
169
+ } else {
170
+ executeForEach(result);
171
+ return void 0;
172
+ }
179
173
  }
180
174
  /**
181
175
  * Creates a new array populated with the results of applying the provided callback
@@ -188,9 +182,12 @@ class Cursor {
188
182
  */
189
183
  map(callback) {
190
184
  const results = [];
191
- this.forEach((item) => {
185
+ const maybePromise = this.forEach((item) => {
192
186
  results.push(callback(item));
193
187
  });
188
+ if (maybePromise instanceof Promise) {
189
+ return maybePromise.then(() => results);
190
+ }
194
191
  return results;
195
192
  }
196
193
  /**
@@ -209,12 +206,12 @@ class Cursor {
209
206
  * @returns The total number of items in the result set.
210
207
  */
211
208
  count() {
212
- const items = this.getItems();
213
209
  this.depend({
214
210
  added: true,
215
211
  removed: true
216
212
  });
217
- return items.length;
213
+ const maybePromise = this.getItems();
214
+ return maybePromise instanceof Promise ? maybePromise.then((items) => items.length) : maybePromise.length;
218
215
  }
219
216
  /**
220
217
  * Observes changes to the cursor's result set and triggers the specified callbacks
@@ -252,7 +249,7 @@ class Cursor {
252
249
  requery() {
253
250
  if (!this.observer)
254
251
  return;
255
- this.observer.runChecks(this.getItems());
252
+ this.observer.runChecks(this.getItems);
256
253
  }
257
254
  }
258
255
  export {
package/dist/index20.mjs CHANGED
@@ -1,16 +1,8 @@
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
+ function queryId(selector, options) {
2
+ const selectorId = JSON.stringify(selector);
3
+ const optionsId = options == null ? -1 : JSON.stringify(options);
4
+ return `${selectorId}:${optionsId}`;
13
5
  }
14
6
  export {
15
- serializeValue as default
7
+ queryId as default
16
8
  };
package/dist/index21.mjs CHANGED
@@ -1,27 +1,105 @@
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();
1
+ import createIndexProvider from "./index30.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;
8
14
  };
9
- const signal = {
10
- get() {
11
- if (dependency && isInReactiveScope())
12
- dependency.depend();
13
- return value;
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
+ }
14
74
  },
15
- set(newValue) {
16
- if (isEqual(value, newValue))
17
- return;
18
- value = newValue;
19
- if (dependency)
20
- dependency.notify();
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
+ }
21
100
  }
22
- };
23
- return signal;
101
+ });
24
102
  }
25
103
  export {
26
- createSignal as default
104
+ createIndex as default
27
105
  };
package/dist/index22.mjs CHANGED
@@ -1,77 +1,127 @@
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
1
+ import intersection from "./index31.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
+ };
12
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);
13
25
  }, {
14
26
  matched: false,
15
- positions: [],
27
+ ids: [],
16
28
  optimizedSelector: { ...selector }
17
29
  });
18
30
  }
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
+ function optimizeLogicGate(queryFunctions, logicGate, idsCallback) {
32
+ return logicGate.reduce((memoOrPromise, sel) => {
33
+ const getSelector = (indexInfo) => {
34
+ const { matched: selMatched, ids: selIds, optimizedSelector: optimizedSelector2 } = indexInfo;
31
35
  if (selMatched) {
32
- positions = matched ? intersection(positions, selPositions) : selPositions;
33
- matched = true;
34
- if (Object.keys(optimizedSelector).length > 0) {
35
- $andNew.push(optimizedSelector);
36
+ idsCallback(true, selIds);
37
+ if (Object.keys(optimizedSelector2).length > 0) {
38
+ return optimizedSelector2;
36
39
  }
37
40
  } else {
38
- $andNew.push(sel);
41
+ idsCallback(false, []);
42
+ return sel;
39
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
+ });
40
54
  }
41
- if ($andNew.length > 0)
42
- newSelector.$and = $andNew;
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
+ };
43
71
  }
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);
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)
79
+ return;
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;
56
96
  }
57
- } else {
58
- $orNew.push(sel);
59
- hasNonIndexField = true;
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));
60
113
  }
114
+ return process$or($orNewOrPromise);
115
+ };
116
+ if ($andNewOrPromise instanceof Promise) {
117
+ return $andNewOrPromise.then(($andNew) => process$and($andNew));
61
118
  }
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
119
+ return process$and($andNewOrPromise);
74
120
  };
121
+ if (flatInfoOrPromise instanceof Promise) {
122
+ return flatInfoOrPromise.then(processFlatInfo);
123
+ }
124
+ return processFlatInfo(flatInfoOrPromise);
75
125
  }
76
126
  export {
77
127
  getIndexInfo as default,
package/dist/index23.mjs CHANGED
@@ -1,84 +1,8 @@
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)
9
- 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
63
- });
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;
79
- }
1
+ import { Query } from "mingo";
2
+ function match(item, selector) {
3
+ const query = new Query(selector);
4
+ return query.test(item);
80
5
  }
81
6
  export {
82
- createReplicationAdapter,
83
- ReplicatedCollection as default
7
+ match as default
84
8
  };
package/dist/index24.mjs CHANGED
@@ -1,19 +1,28 @@
1
- function get(value, path) {
2
- const normalized = path.replaceAll(/\[(\w+)\]/g, ".$1");
3
- if (normalized.includes("..") || normalized.startsWith(".") || normalized.endsWith(".")) {
4
- return;
1
+ import get from "./index10.mjs";
2
+ import set from "./index32.mjs";
3
+ function project(item, fields) {
4
+ const allFieldsDeactivated = Object.values(fields).every((value) => value === 0);
5
+ if (allFieldsDeactivated) {
6
+ const result2 = { ...item };
7
+ Object.keys(fields).forEach((key) => {
8
+ const fieldValue = get(item, key);
9
+ if (fieldValue === void 0)
10
+ return;
11
+ set(result2, key, void 0, true);
12
+ });
13
+ return result2;
5
14
  }
6
- const segments = normalized.split(".");
7
- let current = value;
8
- for (const key of segments) {
9
- if (current == null)
15
+ const result = {};
16
+ Object.entries(fields).forEach(([key, value]) => {
17
+ const fieldValue = get(item, key);
18
+ if (fieldValue === void 0)
10
19
  return;
11
- current = current[key];
12
- }
13
- if (current === void 0)
14
- return;
15
- return current;
20
+ if (fieldValue == null && value !== 1)
21
+ return;
22
+ set(result, key, value === 1 ? fieldValue : void 0);
23
+ });
24
+ return result;
16
25
  }
17
26
  export {
18
- get as default
27
+ project as default
19
28
  };
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
  };