@signaldb/core 1.0.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 (102) hide show
  1. package/README.md +68 -0
  2. package/dist/.vite/manifest.json +222 -0
  3. package/dist/AutoFetchCollection.d.ts +60 -0
  4. package/dist/Collection/Cursor.d.ts +112 -0
  5. package/dist/Collection/Observer.d.ts +61 -0
  6. package/dist/Collection/createIndex.d.ts +15 -0
  7. package/dist/Collection/getIndexInfo.d.ts +41 -0
  8. package/dist/Collection/index.d.ts +235 -0
  9. package/dist/Collection/types.d.ts +25 -0
  10. package/dist/ReplicatedCollection.d.ts +60 -0
  11. package/dist/createIndexProvider.d.ts +8 -0
  12. package/dist/createMemoryAdapter.d.ts +7 -0
  13. package/dist/createReactivityAdapter.d.ts +8 -0
  14. package/dist/index.cjs.js +24 -0
  15. package/dist/index.cjs10.js +8 -0
  16. package/dist/index.cjs11.js +5 -0
  17. package/dist/index.cjs12.js +37 -0
  18. package/dist/index.cjs13.js +26 -0
  19. package/dist/index.cjs14.js +7 -0
  20. package/dist/index.cjs15.js +43 -0
  21. package/dist/index.cjs16.js +13 -0
  22. package/dist/index.cjs17.js +20 -0
  23. package/dist/index.cjs18.js +247 -0
  24. package/dist/index.cjs19.js +71 -0
  25. package/dist/index.cjs2.js +631 -0
  26. package/dist/index.cjs20.js +80 -0
  27. package/dist/index.cjs21.js +10 -0
  28. package/dist/index.cjs22.js +27 -0
  29. package/dist/index.cjs23.js +137 -0
  30. package/dist/index.cjs24.js +18 -0
  31. package/dist/index.cjs25.js +7 -0
  32. package/dist/index.cjs26.js +19 -0
  33. package/dist/index.cjs27.js +24 -0
  34. package/dist/index.cjs28.js +42 -0
  35. package/dist/index.cjs29.js +29 -0
  36. package/dist/index.cjs3.js +165 -0
  37. package/dist/index.cjs30.js +9 -0
  38. package/dist/index.cjs4.js +70 -0
  39. package/dist/index.cjs5.js +5 -0
  40. package/dist/index.cjs6.js +5 -0
  41. package/dist/index.cjs7.js +5 -0
  42. package/dist/index.cjs8.js +5 -0
  43. package/dist/index.cjs9.js +30 -0
  44. package/dist/index.d.ts +16 -0
  45. package/dist/index.mjs +24 -0
  46. package/dist/index10.mjs +9 -0
  47. package/dist/index11.mjs +6 -0
  48. package/dist/index12.mjs +37 -0
  49. package/dist/index13.mjs +27 -0
  50. package/dist/index14.mjs +8 -0
  51. package/dist/index15.mjs +43 -0
  52. package/dist/index16.mjs +14 -0
  53. package/dist/index17.mjs +21 -0
  54. package/dist/index18.mjs +247 -0
  55. package/dist/index19.mjs +71 -0
  56. package/dist/index2.mjs +632 -0
  57. package/dist/index20.mjs +80 -0
  58. package/dist/index21.mjs +11 -0
  59. package/dist/index22.mjs +28 -0
  60. package/dist/index23.mjs +138 -0
  61. package/dist/index24.mjs +18 -0
  62. package/dist/index25.mjs +8 -0
  63. package/dist/index26.mjs +20 -0
  64. package/dist/index27.mjs +25 -0
  65. package/dist/index28.mjs +43 -0
  66. package/dist/index29.mjs +30 -0
  67. package/dist/index3.mjs +166 -0
  68. package/dist/index30.mjs +10 -0
  69. package/dist/index4.mjs +70 -0
  70. package/dist/index5.mjs +6 -0
  71. package/dist/index6.mjs +6 -0
  72. package/dist/index7.mjs +6 -0
  73. package/dist/index8.mjs +6 -0
  74. package/dist/index9.mjs +31 -0
  75. package/dist/persistence/combinePersistenceAdapters.d.ts +32 -0
  76. package/dist/persistence/createPersistenceAdapter.d.ts +9 -0
  77. package/dist/types/Dependency.d.ts +4 -0
  78. package/dist/types/EventEmitter.d.ts +25 -0
  79. package/dist/types/IndexProvider.d.ts +19 -0
  80. package/dist/types/MemoryAdapter.d.ts +15 -0
  81. package/dist/types/Modifier.d.ts +39 -0
  82. package/dist/types/PersistenceAdapter.d.ts +20 -0
  83. package/dist/types/ReactivityAdapter.d.ts +6 -0
  84. package/dist/types/Selector.d.ts +47 -0
  85. package/dist/types/Signal.d.ts +4 -0
  86. package/dist/utils/compact.d.ts +9 -0
  87. package/dist/utils/createSignal.d.ts +14 -0
  88. package/dist/utils/deepClone.d.ts +17 -0
  89. package/dist/utils/get.d.ts +9 -0
  90. package/dist/utils/getMatchingKeys.d.ts +14 -0
  91. package/dist/utils/intersection.d.ts +9 -0
  92. package/dist/utils/isEqual.d.ts +14 -0
  93. package/dist/utils/isFieldExpression.d.ts +11 -0
  94. package/dist/utils/match.d.ts +12 -0
  95. package/dist/utils/modify.d.ts +14 -0
  96. package/dist/utils/project.d.ts +15 -0
  97. package/dist/utils/randomId.d.ts +7 -0
  98. package/dist/utils/serializeValue.d.ts +12 -0
  99. package/dist/utils/set.d.ts +13 -0
  100. package/dist/utils/sortItems.d.ts +12 -0
  101. package/dist/utils/uniqueBy.d.ts +10 -0
  102. package/package.json +58 -0
@@ -0,0 +1,247 @@
1
+ "use strict";
2
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
3
+ const sortItems = require("./index.cjs21.js");
4
+ const project = require("./index.cjs22.js");
5
+ const Observer = require("./index.cjs23.js");
6
+ function isInReactiveScope(reactivity) {
7
+ if (!reactivity)
8
+ return false;
9
+ if (!reactivity.isInScope)
10
+ return true;
11
+ return reactivity.isInScope();
12
+ }
13
+ class Cursor {
14
+ /**
15
+ * Creates a new instance of the `Cursor` class.
16
+ * Provides utilities for querying, observing, and transforming items from a collection.
17
+ * @template T - The type of the items in the collection.
18
+ * @template U - The transformed item type after applying transformations (default is T).
19
+ * @param getItems - A function that retrieves the filtered list of items.
20
+ * @param options - Optional configuration for the cursor.
21
+ * @param options.transform - A transformation function to apply to each item when retrieving them.
22
+ * @param options.bindEvents - A function to bind reactivity events for the cursor, which should return a cleanup function.
23
+ * @param options.fields - A projection object defining which fields of the item should be included or excluded.
24
+ * @param options.sort - A sort specifier to determine the order of the items.
25
+ * @param options.skip - The number of items to skip from the beginning of the result set.
26
+ * @param options.limit - The maximum number of items to return in the result set.
27
+ * @param options.reactive - A reactivity adapter to enable observing changes in the cursor's result set.
28
+ * @param options.fieldTracking - A boolean to enable fine-grained field tracking for reactivity.
29
+ */
30
+ constructor(getItems, options) {
31
+ this.onCleanupCallbacks = [];
32
+ this.getFilteredItems = getItems;
33
+ this.options = options || {};
34
+ }
35
+ addGetters(item) {
36
+ if (!isInReactiveScope(this.options.reactive))
37
+ return item;
38
+ const depend = this.depend.bind(this);
39
+ return Object.entries(item).reduce((memo, [key, value]) => {
40
+ Object.defineProperty(memo, key, {
41
+ get() {
42
+ depend({
43
+ changedField: (notify) => (changedItem, changedFieldName) => {
44
+ if (changedFieldName !== key || changedItem.id !== item.id)
45
+ return;
46
+ notify();
47
+ }
48
+ });
49
+ return value;
50
+ },
51
+ enumerable: true,
52
+ configurable: true
53
+ });
54
+ return memo;
55
+ }, {});
56
+ }
57
+ transform(rawItem) {
58
+ const item = this.options.fieldTracking ? this.addGetters(rawItem) : rawItem;
59
+ if (!this.options.transform)
60
+ return item;
61
+ return this.options.transform(item);
62
+ }
63
+ getItems() {
64
+ const items = this.getFilteredItems();
65
+ const { sort, skip, limit } = this.options;
66
+ const sorted = sort ? sortItems(items, sort) : items;
67
+ const skipped = skip ? sorted.slice(skip) : sorted;
68
+ const limited = limit ? skipped.slice(0, limit) : skipped;
69
+ const idExcluded = this.options.fields && this.options.fields.id === 0;
70
+ return limited.map((item) => {
71
+ if (!this.options.fields)
72
+ return item;
73
+ return Object.assign(Object.assign({}, idExcluded ? {} : { id: item.id }), project(item, this.options.fields));
74
+ });
75
+ }
76
+ depend(changeEvents) {
77
+ if (!this.options.reactive)
78
+ return;
79
+ if (!isInReactiveScope(this.options.reactive))
80
+ return;
81
+ const signal = this.options.reactive.create();
82
+ signal.depend();
83
+ const notify = () => signal.notify();
84
+ function buildNotifier(event) {
85
+ const eventHandler = changeEvents[event];
86
+ return (...args) => {
87
+ if (eventHandler === true) {
88
+ notify();
89
+ return;
90
+ }
91
+ if (typeof eventHandler !== "function")
92
+ return;
93
+ eventHandler(notify)(...args);
94
+ };
95
+ }
96
+ const stop = this.observeRawChanges({
97
+ added: buildNotifier("added"),
98
+ addedBefore: buildNotifier("addedBefore"),
99
+ changed: buildNotifier("changed"),
100
+ changedField: buildNotifier("changedField"),
101
+ movedBefore: buildNotifier("movedBefore"),
102
+ removed: buildNotifier("removed")
103
+ }, true);
104
+ if (this.options.reactive.onDispose) {
105
+ this.options.reactive.onDispose(() => stop(), signal);
106
+ }
107
+ this.onCleanup(stop);
108
+ }
109
+ ensureObserver() {
110
+ if (!this.observer) {
111
+ const observer = new Observer(() => {
112
+ const requery = () => {
113
+ observer.runChecks(this.getItems());
114
+ };
115
+ const cleanup = this.options.bindEvents && this.options.bindEvents(requery);
116
+ return () => {
117
+ if (cleanup)
118
+ cleanup();
119
+ };
120
+ });
121
+ this.onCleanup(() => observer.stop());
122
+ this.observer = observer;
123
+ }
124
+ return this.observer;
125
+ }
126
+ observeRawChanges(callbacks, skipInitial = false) {
127
+ const observer = this.ensureObserver();
128
+ observer.addCallbacks(callbacks, skipInitial);
129
+ observer.runChecks(this.getItems());
130
+ return () => {
131
+ observer.removeCallbacks(callbacks);
132
+ if (!observer.isEmpty())
133
+ return;
134
+ observer.stop();
135
+ this.observer = void 0;
136
+ };
137
+ }
138
+ /**
139
+ * Cleans up all resources associated with the cursor, such as reactive bindings
140
+ * and event listeners. This method should be called when the cursor is no longer needed
141
+ * to prevent memory leaks.
142
+ */
143
+ cleanup() {
144
+ this.onCleanupCallbacks.forEach((callback) => {
145
+ callback();
146
+ });
147
+ this.onCleanupCallbacks = [];
148
+ }
149
+ /**
150
+ * Registers a cleanup callback to be executed when the `cleanup` method is called.
151
+ * Useful for managing resources and ensuring proper cleanup of bindings or listeners.
152
+ * @param callback - A function to be executed during cleanup.
153
+ */
154
+ onCleanup(callback) {
155
+ this.onCleanupCallbacks.push(callback);
156
+ }
157
+ /**
158
+ * Iterates over each item in the cursor's result set, applying the provided callback
159
+ * function to each transformed item.
160
+ * ⚡️ this function is reactive!
161
+ * @param callback - A function to execute for each item in the result set.
162
+ * @param callback.item - The transformed item.
163
+ */
164
+ forEach(callback) {
165
+ const items = this.getItems();
166
+ this.depend(Object.assign({ addedBefore: true, removed: true, movedBefore: true }, this.options.fieldTracking ? {} : { changed: true }));
167
+ items.forEach((item) => {
168
+ callback(this.transform(item));
169
+ });
170
+ }
171
+ /**
172
+ * Creates a new array populated with the results of applying the provided callback
173
+ * function to each transformed item in the cursor's result set.
174
+ * ⚡️ this function is reactive!
175
+ * @template V - The type of the items in the resulting array.
176
+ * @param callback - A function to execute for each item in the result set.
177
+ * @param callback.item - The transformed item.
178
+ * @returns An array of results after applying the callback to each item.
179
+ */
180
+ map(callback) {
181
+ const results = [];
182
+ this.forEach((item) => {
183
+ results.push(callback(item));
184
+ });
185
+ return results;
186
+ }
187
+ /**
188
+ * Fetches all transformed items from the cursor's result set as an array.
189
+ * Automatically applies filtering, sorting, and limiting as per the cursor's options.
190
+ * ⚡️ this function is reactive!
191
+ * @returns An array of transformed items in the result set.
192
+ */
193
+ fetch() {
194
+ return this.map((item) => item);
195
+ }
196
+ /**
197
+ * Counts the total number of items in the cursor's result set after applying
198
+ * filtering and other criteria.
199
+ * ⚡️ this function is reactive!
200
+ * @returns The total number of items in the result set.
201
+ */
202
+ count() {
203
+ const items = this.getItems();
204
+ this.depend({
205
+ added: true,
206
+ removed: true
207
+ });
208
+ return items.length;
209
+ }
210
+ /**
211
+ * Observes changes to the cursor's result set and triggers the specified callbacks
212
+ * when items are added, removed, or updated. Supports reactivity and transformation.
213
+ * @param callbacks - An object containing the callback functions to handle different change events.
214
+ * @param callbacks.added - Triggered when an item is added to the result set.
215
+ * @param callbacks.removed - Triggered when an item is removed from the result set.
216
+ * @param callbacks.changed - Triggered when an item in the result set is modified.
217
+ * @param callbacks.addedBefore - Triggered when an item is added before another item in the result set.
218
+ * @param callbacks.movedBefore - Triggered when an item is moved before another item in the result set.
219
+ * @param callbacks.changedField - Triggered when a specific field of an item changes.
220
+ * @param skipInitial - A boolean indicating whether to skip the initial notification of the current result set.
221
+ * @returns A function to stop observing changes.
222
+ */
223
+ observeChanges(callbacks, skipInitial = false) {
224
+ return this.observeRawChanges(Object.entries(callbacks).reduce((memo, [callbackName, callback]) => {
225
+ if (!callback)
226
+ return memo;
227
+ return Object.assign(Object.assign({}, memo), { [callbackName]: (item, before) => {
228
+ const transformedValue = this.transform(item);
229
+ const hasBeforeParam = before !== void 0;
230
+ const transformedBeforeValue = hasBeforeParam && before ? this.transform(before) : null;
231
+ return callback(transformedValue, ...hasBeforeParam ? [transformedBeforeValue] : []);
232
+ } });
233
+ }, {}), skipInitial);
234
+ }
235
+ /**
236
+ * Forces the cursor to re-evaluate its result set by re-fetching items
237
+ * from the collection. This is useful when the underlying data or query
238
+ * criteria have changed, and you want to ensure the cursor reflects the latest state.
239
+ */
240
+ requery() {
241
+ if (!this.observer)
242
+ return;
243
+ this.observer.runChecks(this.getItems());
244
+ }
245
+ }
246
+ exports.default = Cursor;
247
+ exports.isInReactiveScope = isInReactiveScope;
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
3
+ const tslib_es6 = require("./index.cjs24.js");
4
+ const intersection = require("./index.cjs25.js");
5
+ function getMergedIndexInfo(indexProviders, selector) {
6
+ return indexProviders.reduce((memo, indexProvider) => {
7
+ const info = indexProvider.query(selector);
8
+ if (!info.matched)
9
+ return memo;
10
+ const optimizedSelector = Object.fromEntries(Object.entries(memo.optimizedSelector).filter(([key]) => !info.fields.includes(key)));
11
+ return {
12
+ matched: true,
13
+ positions: [...new Set(memo.matched ? intersection(memo.positions, info.positions) : info.positions)],
14
+ optimizedSelector
15
+ };
16
+ }, {
17
+ matched: false,
18
+ positions: [],
19
+ optimizedSelector: Object.assign({}, selector)
20
+ });
21
+ }
22
+ function getIndexInfo(indexProviders, selector) {
23
+ if (selector == null || Object.keys(selector).length <= 0) {
24
+ return { matched: false, positions: [], optimizedSelector: selector };
25
+ }
26
+ const { $and, $or } = selector, rest = tslib_es6.__rest(selector, ["$and", "$or"]);
27
+ const flatInfo = getMergedIndexInfo(indexProviders, rest);
28
+ let { matched, positions } = flatInfo;
29
+ const newSelector = flatInfo.optimizedSelector;
30
+ if (Array.isArray($and)) {
31
+ const $andNew = [];
32
+ for (const sel of $and) {
33
+ const { matched: selMatched, positions: selPositions, optimizedSelector } = getIndexInfo(indexProviders, sel);
34
+ if (selMatched) {
35
+ positions = matched ? intersection(positions, selPositions) : selPositions;
36
+ matched = true;
37
+ if (Object.keys(optimizedSelector).length > 0) {
38
+ $andNew.push(optimizedSelector);
39
+ }
40
+ } else {
41
+ $andNew.push(sel);
42
+ }
43
+ }
44
+ if ($andNew.length > 0)
45
+ newSelector.$and = $andNew;
46
+ }
47
+ if (Array.isArray($or)) {
48
+ const $orNew = [];
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
+ }
60
+ }
61
+ if ($orNew.length > 0)
62
+ newSelector.$or = $orNew;
63
+ }
64
+ return {
65
+ matched,
66
+ positions: positions || [],
67
+ optimizedSelector: newSelector
68
+ };
69
+ }
70
+ exports.default = getIndexInfo;
71
+ exports.getMergedIndexInfo = getMergedIndexInfo;