@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
@@ -1,7 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
3
- const sortItems = require("./index.cjs15.js");
4
- const project = require("./index.cjs16.js");
5
3
  const Observer = require("./index.cjs17.js");
6
4
  function isInReactiveScope(reactivity) {
7
5
  if (!reactivity)
@@ -12,7 +10,7 @@ function isInReactiveScope(reactivity) {
12
10
  }
13
11
  class Cursor {
14
12
  observer;
15
- getFilteredItems;
13
+ getItems;
16
14
  options;
17
15
  onCleanupCallbacks = [];
18
16
  /**
@@ -32,7 +30,7 @@ class Cursor {
32
30
  * @param options.fieldTracking - A boolean to enable fine-grained field tracking for reactivity.
33
31
  */
34
32
  constructor(getItems, options) {
35
- this.getFilteredItems = getItems;
33
+ this.getItems = getItems;
36
34
  this.options = options || {};
37
35
  }
38
36
  addGetters(item) {
@@ -63,26 +61,13 @@ class Cursor {
63
61
  return item;
64
62
  return this.options.transform(item);
65
63
  }
66
- getItems() {
67
- const items = this.getFilteredItems();
68
- const { sort, skip, limit } = this.options;
69
- const sorted = sort ? sortItems(items, sort) : items;
70
- const skipped = skip ? sorted.slice(skip) : sorted;
71
- const limited = limit ? skipped.slice(0, limit) : skipped;
72
- const idExcluded = this.options.fields && this.options.fields.id === 0;
73
- return limited.map((item) => {
74
- if (!this.options.fields)
75
- return item;
76
- return {
77
- ...idExcluded ? {} : { id: item.id },
78
- ...project(item, this.options.fields)
79
- };
80
- });
81
- }
82
64
  depend(changeEvents) {
83
- if (!this.options.reactive)
65
+ if (this.options?.async)
84
66
  return;
85
- if (!isInReactiveScope(this.options.reactive))
67
+ if (!isInReactiveScope(this.options.reactive)) {
68
+ console.warn("Cursor.depend() called outside of a reactive scope without async option; consider using { async: true } or wrapping in a reactive scope");
69
+ }
70
+ if (!this.options.reactive)
86
71
  return;
87
72
  const signal = this.options.reactive.create();
88
73
  signal.depend();
@@ -116,7 +101,7 @@ class Cursor {
116
101
  if (!this.observer) {
117
102
  const observer = new Observer(() => {
118
103
  const requery = () => {
119
- observer.runChecks(this.getItems());
104
+ observer.runChecks(this.getItems);
120
105
  };
121
106
  const cleanup = this.options.bindEvents && this.options.bindEvents(requery);
122
107
  return () => {
@@ -132,7 +117,7 @@ class Cursor {
132
117
  observeRawChanges(callbacks, skipInitial = false) {
133
118
  const observer = this.ensureObserver();
134
119
  observer.addCallbacks(callbacks, skipInitial);
135
- observer.runChecks(this.getItems());
120
+ observer.runChecks(this.getItems);
136
121
  return () => {
137
122
  observer.removeCallbacks(callbacks);
138
123
  if (!observer.isEmpty())
@@ -166,18 +151,27 @@ class Cursor {
166
151
  * ⚡️ this function is reactive!
167
152
  * @param callback - A function to execute for each item in the result set.
168
153
  * @param callback.item - The transformed item.
154
+ * @returns A promise that resolves when all items have been processed, or void if not in async mode.
169
155
  */
170
156
  forEach(callback) {
171
- const items = this.getItems();
172
157
  this.depend({
173
158
  addedBefore: true,
174
159
  removed: true,
175
160
  movedBefore: true,
176
161
  ...this.options.fieldTracking ? {} : { changed: true }
177
162
  });
178
- items.forEach((item) => {
179
- callback(this.transform(item));
180
- });
163
+ const executeForEach = (items) => {
164
+ items.forEach((item) => {
165
+ callback(this.transform(item));
166
+ });
167
+ };
168
+ const result = this.getItems();
169
+ if (result instanceof Promise) {
170
+ return result.then(executeForEach);
171
+ } else {
172
+ executeForEach(result);
173
+ return void 0;
174
+ }
181
175
  }
182
176
  /**
183
177
  * Creates a new array populated with the results of applying the provided callback
@@ -190,9 +184,12 @@ class Cursor {
190
184
  */
191
185
  map(callback) {
192
186
  const results = [];
193
- this.forEach((item) => {
187
+ const maybePromise = this.forEach((item) => {
194
188
  results.push(callback(item));
195
189
  });
190
+ if (maybePromise instanceof Promise) {
191
+ return maybePromise.then(() => results);
192
+ }
196
193
  return results;
197
194
  }
198
195
  /**
@@ -211,12 +208,12 @@ class Cursor {
211
208
  * @returns The total number of items in the result set.
212
209
  */
213
210
  count() {
214
- const items = this.getItems();
215
211
  this.depend({
216
212
  added: true,
217
213
  removed: true
218
214
  });
219
- return items.length;
215
+ const maybePromise = this.getItems();
216
+ return maybePromise instanceof Promise ? maybePromise.then((items) => items.length) : maybePromise.length;
220
217
  }
221
218
  /**
222
219
  * Observes changes to the cursor's result set and triggers the specified callbacks
@@ -254,7 +251,7 @@ class Cursor {
254
251
  requery() {
255
252
  if (!this.observer)
256
253
  return;
257
- this.observer.runChecks(this.getItems());
254
+ this.observer.runChecks(this.getItems);
258
255
  }
259
256
  }
260
257
  exports.default = Cursor;
@@ -1,15 +1,7 @@
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
+ function queryId(selector, options) {
3
+ const selectorId = JSON.stringify(selector);
4
+ const optionsId = options == null ? -1 : JSON.stringify(options);
5
+ return `${selectorId}:${optionsId}`;
14
6
  }
15
- module.exports = serializeValue;
7
+ module.exports = queryId;
@@ -1,26 +1,104 @@
1
1
  "use strict";
2
- function createSignal(reactivityAdapter, initialValue, isEqual = Object.is) {
3
- let value = initialValue;
4
- const dependency = reactivityAdapter?.create();
5
- const isInReactiveScope = () => {
6
- if (!reactivityAdapter?.isInScope)
7
- return true;
8
- return reactivityAdapter.isInScope();
2
+ const createIndexProvider = require("./index.cjs30.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;
9
15
  };
10
- const signal = {
11
- get() {
12
- if (dependency && isInReactiveScope())
13
- dependency.depend();
14
- return value;
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
+ }
15
75
  },
16
- set(newValue) {
17
- if (isEqual(value, newValue))
18
- return;
19
- value = newValue;
20
- if (dependency)
21
- dependency.notify();
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
+ }
22
101
  }
23
- };
24
- return signal;
102
+ });
25
103
  }
26
- module.exports = createSignal;
104
+ module.exports = createIndex;
@@ -1,79 +1,129 @@
1
1
  "use strict";
2
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
3
+ const intersection = require("./index.cjs31.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
+ };
14
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);
15
27
  }, {
16
28
  matched: false,
17
- positions: [],
29
+ ids: [],
18
30
  optimizedSelector: { ...selector }
19
31
  });
20
32
  }
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
+ function optimizeLogicGate(queryFunctions, logicGate, idsCallback) {
34
+ return logicGate.reduce((memoOrPromise, sel) => {
35
+ const getSelector = (indexInfo) => {
36
+ const { matched: selMatched, ids: selIds, optimizedSelector: optimizedSelector2 } = indexInfo;
33
37
  if (selMatched) {
34
- positions = matched ? intersection(positions, selPositions) : selPositions;
35
- matched = true;
36
- if (Object.keys(optimizedSelector).length > 0) {
37
- $andNew.push(optimizedSelector);
38
+ idsCallback(true, selIds);
39
+ if (Object.keys(optimizedSelector2).length > 0) {
40
+ return optimizedSelector2;
38
41
  }
39
42
  } else {
40
- $andNew.push(sel);
43
+ idsCallback(false, []);
44
+ return sel;
41
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
+ });
42
56
  }
43
- if ($andNew.length > 0)
44
- newSelector.$and = $andNew;
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
+ };
45
73
  }
46
- if (Array.isArray($or)) {
47
- const $orNew = [];
48
- const matchedBefore = matched;
49
- const positionsBefore = positions;
50
- let hasNonIndexField = false;
51
- for (const sel of $or) {
52
- const { matched: selMatched, positions: selPositions, optimizedSelector } = getIndexInfo(indexProviders, sel);
53
- if (selMatched) {
54
- positions = [.../* @__PURE__ */ new Set([...positions, ...selPositions])];
55
- matched = true;
56
- if (Object.keys(optimizedSelector).length > 0) {
57
- $orNew.push(optimizedSelector);
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)
81
+ return;
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;
58
98
  }
59
- } else {
60
- $orNew.push(sel);
61
- hasNonIndexField = true;
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));
62
115
  }
116
+ return process$or($orNewOrPromise);
117
+ };
118
+ if ($andNewOrPromise instanceof Promise) {
119
+ return $andNewOrPromise.then(($andNew) => process$and($andNew));
63
120
  }
64
- if ($orNew.length > 0)
65
- newSelector.$or = $orNew;
66
- if (hasNonIndexField) {
67
- newSelector.$or = $or;
68
- matched = matchedBefore;
69
- positions = positionsBefore;
70
- }
71
- }
72
- return {
73
- matched,
74
- positions: positions || [],
75
- optimizedSelector: newSelector
121
+ return process$and($andNewOrPromise);
76
122
  };
123
+ if (flatInfoOrPromise instanceof Promise) {
124
+ return flatInfoOrPromise.then(processFlatInfo);
125
+ }
126
+ return processFlatInfo(flatInfoOrPromise);
77
127
  }
78
128
  exports.default = getIndexInfo;
79
129
  exports.getMergedIndexInfo = getMergedIndexInfo;
@@ -1,84 +1,7 @@
1
1
  "use strict";
2
- Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
3
- const index = require("./index.cjs3.js");
4
- const combinePersistenceAdapters = require("./index.cjs5.js");
5
- const createPersistenceAdapter = require("./index.cjs8.js");
6
- const createSignal = require("./index.cjs21.js");
7
- function createReplicationAdapter(options) {
8
- return createPersistenceAdapter({
9
- async register(onChange) {
10
- if (!options.registerRemoteChange)
11
- return;
12
- await options.registerRemoteChange(onChange);
13
- },
14
- load: () => options.pull(),
15
- save: (items, changes) => {
16
- if (!options.push)
17
- throw new Error("Pushing is not configured for this collection. Try to pass a `push` function to the collection options.");
18
- return options.push(changes, items);
19
- }
20
- });
2
+ const mingo = require("mingo");
3
+ function match(item, selector) {
4
+ const query = new mingo.Query(selector);
5
+ return query.test(item);
21
6
  }
22
- class ReplicatedCollection extends index.default {
23
- isPullingRemoteSignal;
24
- isPushingRemoteSignal;
25
- /**
26
- * Creates a new instance of the `ReplicatedCollection` class.
27
- * Sets up the replication adapter, combining it with an optional persistence adapter, and
28
- * initializes signals for tracking remote pull and push operations.
29
- * @param options - The configuration options for the replicated collection.
30
- * @param options.pull - A function to fetch data from the remote source.
31
- * @param options.push - An optional function to send changes and items to the remote source.
32
- * @param options.registerRemoteChange - An optional function to register a listener for remote changes.
33
- * @param options.persistence - An optional persistence adapter to combine with replication.
34
- * @param options.reactivity - A reactivity adapter for observing changes in the collection.
35
- * @param options.transform - A transformation function to apply to items when retrieving them.
36
- * @param options.indices - An array of index providers for optimized querying.
37
- * @param options.enableDebugMode - A boolean to enable or disable debug mode.
38
- */
39
- constructor(options) {
40
- const replicationAdapter = createReplicationAdapter({
41
- registerRemoteChange: options.registerRemoteChange,
42
- pull: async () => {
43
- this.isPullingRemoteSignal.set(true);
44
- try {
45
- return await options.pull();
46
- } finally {
47
- this.isPullingRemoteSignal.set(false);
48
- }
49
- },
50
- push: options.push ? async (changes, items) => {
51
- if (!options.push)
52
- throw new Error("Pushing is not configured for this collection. Try to pass a `push` function to the collection options.");
53
- this.isPushingRemoteSignal.set(true);
54
- try {
55
- await options.push(changes, items);
56
- } finally {
57
- this.isPushingRemoteSignal.set(false);
58
- }
59
- } : void 0
60
- });
61
- const persistenceAdapter = options?.persistence ? combinePersistenceAdapters.default(replicationAdapter, options.persistence) : replicationAdapter;
62
- super({
63
- ...options,
64
- persistence: persistenceAdapter
65
- });
66
- this.isPullingRemoteSignal = createSignal(options.reactivity, false);
67
- this.isPushingRemoteSignal = createSignal(options.reactivity, false);
68
- }
69
- /**
70
- * Checks whether the collection is currently performing any loading operation,
71
- * including pulling or pushing data from/to the remote source, or standard
72
- * persistence adapter operations.
73
- * ⚡️ this function is reactive!
74
- * @returns A boolean indicating if the collection is currently loading or synchronizing.
75
- */
76
- isLoading() {
77
- const isPullingRemote = this.isPullingRemoteSignal.get();
78
- const isPushingRemote = this.isPushingRemoteSignal.get();
79
- const isLoading = super.isLoading();
80
- return isPullingRemote || isPushingRemote || isLoading;
81
- }
82
- }
83
- exports.createReplicationAdapter = createReplicationAdapter;
84
- exports.default = ReplicatedCollection;
7
+ module.exports = match;
@@ -1,18 +1,27 @@
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;
2
+ const get = require("./index.cjs10.js");
3
+ const set = require("./index.cjs32.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)
11
+ return;
12
+ set(result2, key, void 0, true);
13
+ });
14
+ return result2;
6
15
  }
7
- const segments = normalized.split(".");
8
- let current = value;
9
- for (const key of segments) {
10
- if (current == null)
16
+ const result = {};
17
+ Object.entries(fields).forEach(([key, value]) => {
18
+ const fieldValue = get(item, key);
19
+ if (fieldValue === void 0)
11
20
  return;
12
- current = current[key];
13
- }
14
- if (current === void 0)
15
- return;
16
- return current;
21
+ if (fieldValue == null && value !== 1)
22
+ return;
23
+ set(result, key, value === 1 ? fieldValue : void 0);
24
+ });
25
+ return result;
17
26
  }
18
- module.exports = get;
27
+ module.exports = project;
@@ -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;