@signaldb/core 2.0.0-beta.5 → 2.0.0-beta.7
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.
- package/dist/.vite/manifest.json +118 -84
- package/dist/AsyncDataAdapter.d.ts +1 -1
- package/dist/index.cjs.js +33 -34
- package/dist/index.mjs +17 -35
- package/dist/index10.cjs.js +23 -0
- package/dist/index10.mjs +21 -17
- package/dist/index11.cjs.js +45 -0
- package/dist/index11.mjs +44 -15
- package/dist/index12.cjs.js +22 -0
- package/dist/index12.mjs +21 -47
- package/dist/index13.cjs.js +45 -0
- package/dist/index13.mjs +44 -365
- package/dist/index14.cjs.js +86 -0
- package/dist/index14.mjs +85 -416
- package/dist/index15.cjs.js +15 -0
- package/dist/index15.mjs +14 -206
- package/dist/index16.cjs.js +136 -0
- package/dist/index16.mjs +135 -388
- package/dist/index17.cjs.js +47 -0
- package/dist/index17.mjs +45 -578
- package/dist/index18.cjs.js +15 -0
- package/dist/index18.mjs +14 -157
- package/dist/index19.cjs.js +23 -0
- package/dist/index19.mjs +22 -26
- package/dist/index2.cjs.js +33 -0
- package/dist/index2.mjs +32 -258
- package/dist/index20.cjs.js +37 -0
- package/dist/index20.mjs +36 -41
- package/dist/index21.cjs.js +35 -0
- package/dist/index21.mjs +34 -16
- package/dist/index22.cjs.js +25 -0
- package/dist/index22.mjs +24 -104
- package/dist/index23.cjs.js +19 -0
- package/dist/index23.mjs +18 -128
- package/dist/index24.cjs.js +342 -0
- package/dist/index24.mjs +340 -6
- package/dist/index25.cjs.js +519 -0
- package/dist/index25.mjs +518 -27
- package/dist/index26.cjs.js +11 -0
- package/dist/index26.mjs +10 -10
- package/dist/index27.cjs.js +11 -0
- package/dist/index27.mjs +10 -29
- package/dist/index28.cjs.js +90 -0
- package/dist/index28.mjs +88 -46
- package/dist/index29.cjs.js +370 -0
- package/dist/index29.mjs +369 -8
- package/dist/index3.cjs.js +19 -0
- package/dist/index3.mjs +18 -539
- package/dist/index30.cjs.js +72 -0
- package/dist/index30.mjs +71 -7
- package/dist/index31.cjs.js +191 -0
- package/dist/index31.mjs +190 -42
- package/dist/index32.cjs.js +21 -0
- package/dist/index32.mjs +20 -29
- package/dist/index33.cjs.js +336 -0
- package/dist/index33.mjs +335 -5
- package/dist/index34.cjs.js +539 -0
- package/dist/index34.mjs +537 -8
- package/dist/index4.cjs.js +150 -0
- package/dist/index4.mjs +149 -5
- package/dist/index5.cjs.js +254 -0
- package/dist/index5.mjs +252 -4
- package/dist/index6.cjs.js +125 -0
- package/dist/index6.mjs +124 -29
- package/dist/index7.cjs.js +33 -0
- package/dist/index7.mjs +32 -12
- package/dist/index8.cjs.js +12 -0
- package/dist/index8.mjs +10 -4
- package/dist/index9.cjs.js +11 -0
- package/dist/index9.mjs +10 -133
- package/package.json +1 -1
- package/dist/index.cjs10.js +0 -18
- package/dist/index.cjs11.js +0 -15
- package/dist/index.cjs12.js +0 -48
- package/dist/index.cjs13.js +0 -365
- package/dist/index.cjs14.js +0 -416
- package/dist/index.cjs15.js +0 -206
- package/dist/index.cjs16.js +0 -388
- package/dist/index.cjs17.js +0 -579
- package/dist/index.cjs18.js +0 -157
- package/dist/index.cjs19.js +0 -26
- package/dist/index.cjs2.js +0 -259
- package/dist/index.cjs20.js +0 -42
- package/dist/index.cjs21.js +0 -16
- package/dist/index.cjs22.js +0 -104
- package/dist/index.cjs23.js +0 -129
- package/dist/index.cjs24.js +0 -7
- package/dist/index.cjs25.js +0 -27
- package/dist/index.cjs26.js +0 -10
- package/dist/index.cjs27.js +0 -29
- package/dist/index.cjs28.js +0 -46
- package/dist/index.cjs29.js +0 -8
- package/dist/index.cjs3.js +0 -540
- package/dist/index.cjs30.js +0 -7
- package/dist/index.cjs31.js +0 -42
- package/dist/index.cjs32.js +0 -29
- package/dist/index.cjs33.js +0 -5
- package/dist/index.cjs34.js +0 -9
- package/dist/index.cjs4.js +0 -5
- package/dist/index.cjs5.js +0 -5
- package/dist/index.cjs6.js +0 -29
- package/dist/index.cjs7.js +0 -12
- package/dist/index.cjs8.js +0 -5
- package/dist/index.cjs9.js +0 -133
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
const require_isEqual = require("./index2.cjs.js");
|
|
2
|
+
const require_EventEmitter = require("./index6.cjs.js");
|
|
3
|
+
const require_serializeValue = require("./index12.cjs.js");
|
|
4
|
+
const require_createIndex = require("./index14.cjs.js");
|
|
5
|
+
const require_getIndexInfo = require("./index16.cjs.js");
|
|
6
|
+
const require_deepClone = require("./index17.cjs.js");
|
|
7
|
+
const require_match = require("./index18.cjs.js");
|
|
8
|
+
const require_modify = require("./index19.cjs.js");
|
|
9
|
+
const require_project = require("./index21.cjs.js");
|
|
10
|
+
const require_queryId = require("./index22.cjs.js");
|
|
11
|
+
const require_sortItems = require("./index23.cjs.js");
|
|
12
|
+
//#region src/DefaultDataAdapter.ts
|
|
13
|
+
/**
|
|
14
|
+
* Checks if there are any pending updates in the given changeset.
|
|
15
|
+
* @template T - The type of the items in the changeset.
|
|
16
|
+
* @param pendingUpdates - The changeset to check for pending updates.
|
|
17
|
+
* @returns `true` if there are pending updates, otherwise `false`.
|
|
18
|
+
*/
|
|
19
|
+
function hasPendingUpdates(pendingUpdates) {
|
|
20
|
+
return pendingUpdates.added.length > 0 || pendingUpdates.modified.length > 0 || pendingUpdates.removed.length > 0;
|
|
21
|
+
}
|
|
22
|
+
var DefaultDataAdapter = class {
|
|
23
|
+
items = /* @__PURE__ */ new Map();
|
|
24
|
+
options;
|
|
25
|
+
storageAdapters = /* @__PURE__ */ new Map();
|
|
26
|
+
collections = /* @__PURE__ */ new Set();
|
|
27
|
+
indices = /* @__PURE__ */ new Map();
|
|
28
|
+
activeQueries = /* @__PURE__ */ new Map();
|
|
29
|
+
queryEmitters = /* @__PURE__ */ new Map();
|
|
30
|
+
queuedQueryUpdates = /* @__PURE__ */ new Map();
|
|
31
|
+
cachedQueryResults = /* @__PURE__ */ new Map();
|
|
32
|
+
constructor(options) {
|
|
33
|
+
this.options = options || {};
|
|
34
|
+
}
|
|
35
|
+
ensureStorageAdapter(name) {
|
|
36
|
+
if (this.storageAdapters.get(name)) return;
|
|
37
|
+
if (!this.options.storage) return;
|
|
38
|
+
const adapter = this.options.storage(name);
|
|
39
|
+
if (!adapter) return;
|
|
40
|
+
this.storageAdapters.set(name, adapter);
|
|
41
|
+
}
|
|
42
|
+
rebuildIndices(collection) {
|
|
43
|
+
const items = this.items.get(collection.name);
|
|
44
|
+
if (!items) throw new Error(`Items not found for collection ${collection.name}`);
|
|
45
|
+
const indices = this.indices.get(collection.name);
|
|
46
|
+
if (!indices) throw new Error(`Indices not found for collection ${collection.name}`);
|
|
47
|
+
indices.forEach((index) => index.rebuild([...items.values()]));
|
|
48
|
+
}
|
|
49
|
+
async setupStorageAdapter(collection) {
|
|
50
|
+
const storageAdapter = this.storageAdapters.get(collection.name);
|
|
51
|
+
if (!storageAdapter) return;
|
|
52
|
+
return storageAdapter.setup().then(async () => {
|
|
53
|
+
await this.fetchItemsFromStorage(collection);
|
|
54
|
+
}).catch((error) => {
|
|
55
|
+
if (!this.options.onError) {
|
|
56
|
+
console.error(`Error during data persistence operation in collection ${collection.name}`, error);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
this.options.onError(collection.name, error instanceof Error ? error : new Error(error));
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
getIndexInfo(collection, selector) {
|
|
63
|
+
if (selector != null && Object.keys(selector).length === 1 && "id" in selector && typeof selector.id !== "object") return {
|
|
64
|
+
matched: true,
|
|
65
|
+
ids: [require_serializeValue.default(selector.id)],
|
|
66
|
+
optimizedSelector: {}
|
|
67
|
+
};
|
|
68
|
+
if (selector == null) return {
|
|
69
|
+
matched: false,
|
|
70
|
+
ids: [],
|
|
71
|
+
optimizedSelector: {}
|
|
72
|
+
};
|
|
73
|
+
return require_getIndexInfo.default((this.indices.get(collection.name) ?? []).map((i) => i.query.bind(i)), selector);
|
|
74
|
+
}
|
|
75
|
+
applyIndexDeltas(collection, changes) {
|
|
76
|
+
const indices = this.indices.get(collection.name) ?? [];
|
|
77
|
+
for (const index of indices) {
|
|
78
|
+
if (changes.modified) index.update(changes.modified);
|
|
79
|
+
if (changes.added && changes.added.length > 0) index.insert(changes.added);
|
|
80
|
+
if (changes.removed && changes.removed.length > 0) index.remove(changes.removed);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
getItem(collection, selector) {
|
|
84
|
+
const memory = this.items.get(collection.name) ?? /* @__PURE__ */ new Map();
|
|
85
|
+
const indexInfo = this.getIndexInfo(collection, selector);
|
|
86
|
+
return (indexInfo.matched ? indexInfo.ids.map((id) => memory.get(require_serializeValue.default(id))).filter((i) => i != null) : [...memory.values()]).find((document) => require_match.default(document, selector));
|
|
87
|
+
}
|
|
88
|
+
queryItems(collection, selector) {
|
|
89
|
+
const indexInfo = this.getIndexInfo(collection, selector);
|
|
90
|
+
const matchItems = (item) => {
|
|
91
|
+
if (indexInfo.optimizedSelector == null) return true;
|
|
92
|
+
if (Object.keys(indexInfo.optimizedSelector).length <= 0) return true;
|
|
93
|
+
return require_match.default(item, indexInfo.optimizedSelector);
|
|
94
|
+
};
|
|
95
|
+
const items = this.items.get(collection.name);
|
|
96
|
+
if (!indexInfo.matched) {
|
|
97
|
+
if (require_isEqual.default(selector, {})) return [...items.values()];
|
|
98
|
+
return [...items.values()].filter(matchItems);
|
|
99
|
+
}
|
|
100
|
+
const foundItems = indexInfo.ids.map((ids) => items.get(require_serializeValue.default(ids))).filter((i) => i != null);
|
|
101
|
+
if (require_isEqual.default(indexInfo.optimizedSelector, {})) return foundItems;
|
|
102
|
+
return foundItems.filter(matchItems);
|
|
103
|
+
}
|
|
104
|
+
executeQuery(collection, selector, options) {
|
|
105
|
+
const items = this.queryItems(collection, selector || {});
|
|
106
|
+
const { sort, skip, limit, fields } = options || {};
|
|
107
|
+
const sorted = sort ? require_sortItems.default(items, sort) : items;
|
|
108
|
+
const skipped = skip ? sorted.slice(skip) : sorted;
|
|
109
|
+
const limited = limit ? skipped.slice(0, limit) : skipped;
|
|
110
|
+
const idExcluded = fields && fields.id === 0;
|
|
111
|
+
return limited.map((item) => {
|
|
112
|
+
if (!fields) return item;
|
|
113
|
+
return {
|
|
114
|
+
...idExcluded ? {} : { id: item.id },
|
|
115
|
+
...require_project.default(item, fields)
|
|
116
|
+
};
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
flushQueuedQueryUpdates(collection) {
|
|
120
|
+
if (!this.queuedQueryUpdates.get(collection.name)) return;
|
|
121
|
+
const changes = this.queuedQueryUpdates.get(collection.name);
|
|
122
|
+
if (!changes || !hasPendingUpdates(changes)) return;
|
|
123
|
+
this.queuedQueryUpdates.set(collection.name, {
|
|
124
|
+
added: [],
|
|
125
|
+
modified: [],
|
|
126
|
+
removed: []
|
|
127
|
+
});
|
|
128
|
+
const flatItems = [
|
|
129
|
+
...changes.added,
|
|
130
|
+
...changes.modified,
|
|
131
|
+
...changes.removed
|
|
132
|
+
];
|
|
133
|
+
const itemIds = new Set(flatItems.map((i) => i.id));
|
|
134
|
+
[...this.activeQueries.get(collection.name)?.values() ?? []].filter(({ selector, options }) => {
|
|
135
|
+
if ((this.cachedQueryResults.get(collection.name)?.get(require_queryId.default(selector, options))?.map((i) => i.id) ?? []).some((id) => itemIds.has(id))) return true;
|
|
136
|
+
return flatItems.some((item) => require_match.default(item, selector));
|
|
137
|
+
}).forEach(({ selector, options }) => {
|
|
138
|
+
this.executeAndCacheQuery(collection, selector, options);
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
executeAndCacheQuery(collection, selector, options) {
|
|
142
|
+
const result = this.executeQuery(collection, selector, options);
|
|
143
|
+
this.cachedQueryResults.set(collection.name, this.cachedQueryResults.get(collection.name) || /* @__PURE__ */ new Map());
|
|
144
|
+
this.cachedQueryResults.get(collection.name)?.set(require_queryId.default(selector, options), result);
|
|
145
|
+
const emitter = this.queryEmitters.get(collection.name);
|
|
146
|
+
if (!emitter) return;
|
|
147
|
+
emitter.emit("change", selector, options, "complete");
|
|
148
|
+
}
|
|
149
|
+
updateQueries(collection, changes) {
|
|
150
|
+
this.queuedQueryUpdates.set(collection.name, this.queuedQueryUpdates.get(collection.name) || {
|
|
151
|
+
added: [],
|
|
152
|
+
modified: [],
|
|
153
|
+
removed: []
|
|
154
|
+
});
|
|
155
|
+
this.queuedQueryUpdates.get(collection.name)?.added.push(...changes.added);
|
|
156
|
+
this.queuedQueryUpdates.get(collection.name)?.modified.push(...changes.modified);
|
|
157
|
+
this.queuedQueryUpdates.get(collection.name)?.removed.push(...changes.removed);
|
|
158
|
+
this.flushQueuedQueryUpdates(collection);
|
|
159
|
+
}
|
|
160
|
+
createCollectionBackend(collection, indices) {
|
|
161
|
+
this.collections.add(collection);
|
|
162
|
+
this.ensureStorageAdapter(collection.name);
|
|
163
|
+
this.items.set(collection.name, this.items.get(collection.name) ?? /* @__PURE__ */ new Map());
|
|
164
|
+
this.queryEmitters.set(collection.name, this.queryEmitters.get(collection.name) ?? new require_EventEmitter.default());
|
|
165
|
+
this.queryEmitters.get(collection.name)?.setMaxListeners(Infinity);
|
|
166
|
+
this.activeQueries.set(collection.name, this.activeQueries.get(collection.name) || /* @__PURE__ */ new Map());
|
|
167
|
+
this.indices.set(collection.name, indices.map((field) => require_createIndex.default(field)));
|
|
168
|
+
this.rebuildIndices(collection);
|
|
169
|
+
const persistenceReadyPromise = this.setupStorageAdapter(collection);
|
|
170
|
+
const backend = {
|
|
171
|
+
insert: async (newItem) => {
|
|
172
|
+
const items = this.items.get(collection.name);
|
|
173
|
+
if (!items) throw new Error(`Items not found for collection ${collection.name}`);
|
|
174
|
+
if (items.has(require_serializeValue.default(newItem.id))) throw new Error(`Item with id '${newItem.id}' already exists`);
|
|
175
|
+
this.items.get(collection.name)?.set(require_serializeValue.default(newItem.id), newItem);
|
|
176
|
+
await this.storageAdapters.get(collection.name)?.insert([newItem]);
|
|
177
|
+
this.applyIndexDeltas(collection, { added: [newItem] });
|
|
178
|
+
this.updateQueries(collection, {
|
|
179
|
+
added: [],
|
|
180
|
+
modified: [newItem],
|
|
181
|
+
removed: []
|
|
182
|
+
});
|
|
183
|
+
return newItem;
|
|
184
|
+
},
|
|
185
|
+
updateOne: async (selector, modifier) => {
|
|
186
|
+
const { $setOnInsert, ...restModifier } = modifier;
|
|
187
|
+
const item = this.getItem(collection, selector);
|
|
188
|
+
if (item == null) return [];
|
|
189
|
+
const modifiedItem = require_modify.default(require_deepClone.default(item), restModifier);
|
|
190
|
+
if (item.id !== modifiedItem.id && this.getItem(collection, { id: modifiedItem.id }) != null) throw new Error(`Item with id '${modifiedItem.id}' already exists`);
|
|
191
|
+
this.items.get(collection.name)?.set(require_serializeValue.default(modifiedItem.id), modifiedItem);
|
|
192
|
+
await this.storageAdapters.get(collection.name)?.replace([modifiedItem]);
|
|
193
|
+
this.applyIndexDeltas(collection, { modified: [{
|
|
194
|
+
oldItem: item,
|
|
195
|
+
newItem: modifiedItem
|
|
196
|
+
}] });
|
|
197
|
+
this.updateQueries(collection, {
|
|
198
|
+
added: [],
|
|
199
|
+
modified: [modifiedItem],
|
|
200
|
+
removed: []
|
|
201
|
+
});
|
|
202
|
+
return [modifiedItem];
|
|
203
|
+
},
|
|
204
|
+
updateMany: async (selector, modifier) => {
|
|
205
|
+
const { $setOnInsert, ...restModifier } = modifier;
|
|
206
|
+
const items = this.executeQuery(collection, selector, {});
|
|
207
|
+
const changedItems = items.map((item) => {
|
|
208
|
+
const modifiedItem = require_modify.default(require_deepClone.default(item), restModifier);
|
|
209
|
+
if (item.id !== modifiedItem.id && this.getItem(collection, { id: modifiedItem.id }) != null) throw new Error(`Item with id '${modifiedItem.id}' already exists`);
|
|
210
|
+
return modifiedItem;
|
|
211
|
+
});
|
|
212
|
+
changedItems.forEach((item) => {
|
|
213
|
+
this.items.get(collection.name)?.set(require_serializeValue.default(item.id), item);
|
|
214
|
+
});
|
|
215
|
+
await this.storageAdapters.get(collection.name)?.replace(changedItems);
|
|
216
|
+
const pairs = items.map((oldItem, index) => ({
|
|
217
|
+
oldItem,
|
|
218
|
+
newItem: changedItems[index]
|
|
219
|
+
}));
|
|
220
|
+
this.applyIndexDeltas(collection, { modified: pairs });
|
|
221
|
+
this.updateQueries(collection, {
|
|
222
|
+
added: [],
|
|
223
|
+
modified: changedItems,
|
|
224
|
+
removed: []
|
|
225
|
+
});
|
|
226
|
+
return changedItems;
|
|
227
|
+
},
|
|
228
|
+
replaceOne: async (selector, replacement) => {
|
|
229
|
+
const item = this.getItem(collection, selector);
|
|
230
|
+
if (item == null) return [];
|
|
231
|
+
if (item.id !== replacement.id && replacement.id != null && this.getItem(collection, { id: replacement.id }) != null) throw new Error(`Item with id '${replacement.id}' already exists`);
|
|
232
|
+
const modifiedItem = {
|
|
233
|
+
id: item.id,
|
|
234
|
+
...replacement
|
|
235
|
+
};
|
|
236
|
+
this.items.get(collection.name)?.set(require_serializeValue.default(modifiedItem.id), modifiedItem);
|
|
237
|
+
await this.storageAdapters.get(collection.name)?.replace([modifiedItem]);
|
|
238
|
+
this.applyIndexDeltas(collection, { modified: [{
|
|
239
|
+
oldItem: item,
|
|
240
|
+
newItem: modifiedItem
|
|
241
|
+
}] });
|
|
242
|
+
this.updateQueries(collection, {
|
|
243
|
+
added: [],
|
|
244
|
+
modified: [modifiedItem],
|
|
245
|
+
removed: []
|
|
246
|
+
});
|
|
247
|
+
return [modifiedItem];
|
|
248
|
+
},
|
|
249
|
+
removeOne: async (selector) => {
|
|
250
|
+
const item = this.getItem(collection, selector);
|
|
251
|
+
if (item == null) return [];
|
|
252
|
+
this.items.get(collection.name)?.delete(require_serializeValue.default(item.id));
|
|
253
|
+
await this.storageAdapters.get(collection.name)?.remove([item]);
|
|
254
|
+
this.applyIndexDeltas(collection, { removed: [item] });
|
|
255
|
+
this.updateQueries(collection, {
|
|
256
|
+
added: [],
|
|
257
|
+
modified: [],
|
|
258
|
+
removed: [item]
|
|
259
|
+
});
|
|
260
|
+
return [item];
|
|
261
|
+
},
|
|
262
|
+
removeMany: async (selector) => {
|
|
263
|
+
const items = backend.getQueryResult(selector, {});
|
|
264
|
+
items.forEach((item) => {
|
|
265
|
+
this.items.get(collection.name)?.delete(require_serializeValue.default(item.id));
|
|
266
|
+
});
|
|
267
|
+
await this.storageAdapters.get(collection.name)?.remove(items);
|
|
268
|
+
this.applyIndexDeltas(collection, { removed: items });
|
|
269
|
+
this.updateQueries(collection, {
|
|
270
|
+
added: [],
|
|
271
|
+
modified: [],
|
|
272
|
+
removed: items
|
|
273
|
+
});
|
|
274
|
+
return items;
|
|
275
|
+
},
|
|
276
|
+
registerQuery: (selector, options) => {
|
|
277
|
+
this.activeQueries.set(collection.name, this.activeQueries.get(collection.name) || /* @__PURE__ */ new Map());
|
|
278
|
+
this.activeQueries.get(collection.name)?.set(require_queryId.default(selector, options), {
|
|
279
|
+
selector,
|
|
280
|
+
options
|
|
281
|
+
});
|
|
282
|
+
this.executeAndCacheQuery(collection, selector, options);
|
|
283
|
+
},
|
|
284
|
+
unregisterQuery: (selector, options) => {
|
|
285
|
+
if (!this.activeQueries.get(collection.name)) return;
|
|
286
|
+
this.activeQueries.get(collection.name)?.delete(require_queryId.default(selector, options));
|
|
287
|
+
},
|
|
288
|
+
getQueryState: () => "complete",
|
|
289
|
+
onQueryStateChange: (selector, options, callback) => {
|
|
290
|
+
const emitter = this.queryEmitters.get(collection.name);
|
|
291
|
+
if (!emitter) throw new Error(`Query emitter not found for collection ${collection.name}`);
|
|
292
|
+
const handler = (querySelector, queryOptions, state) => {
|
|
293
|
+
if (require_queryId.default(querySelector, queryOptions) !== require_queryId.default(selector, options)) return;
|
|
294
|
+
callback(state);
|
|
295
|
+
};
|
|
296
|
+
emitter.on("change", handler);
|
|
297
|
+
return () => {
|
|
298
|
+
emitter.off("change", handler);
|
|
299
|
+
};
|
|
300
|
+
},
|
|
301
|
+
getQueryError: () => null,
|
|
302
|
+
getQueryResult: (selector, options) => {
|
|
303
|
+
if (this.activeQueries.get(collection.name)?.has(require_queryId.default(selector, options))) {
|
|
304
|
+
const results = this.cachedQueryResults.get(collection.name)?.get(require_queryId.default(selector, options));
|
|
305
|
+
if (!results) throw new Error("Cached query results are not defined!");
|
|
306
|
+
return results;
|
|
307
|
+
}
|
|
308
|
+
return this.executeQuery(collection, selector, options);
|
|
309
|
+
},
|
|
310
|
+
executeQuery: (selector, options) => Promise.resolve(this.executeQuery(collection, selector, options)),
|
|
311
|
+
dispose: async () => {
|
|
312
|
+
const adapter = this.storageAdapters.get(collection.name);
|
|
313
|
+
if (adapter?.teardown) await adapter.teardown();
|
|
314
|
+
this.storageAdapters.delete(collection.name);
|
|
315
|
+
this.items.delete(collection.name);
|
|
316
|
+
this.indices.delete(collection.name);
|
|
317
|
+
this.activeQueries.delete(collection.name);
|
|
318
|
+
this.queryEmitters.delete(collection.name);
|
|
319
|
+
this.queuedQueryUpdates.delete(collection.name);
|
|
320
|
+
this.cachedQueryResults.delete(collection.name);
|
|
321
|
+
},
|
|
322
|
+
isReady: () => persistenceReadyPromise
|
|
323
|
+
};
|
|
324
|
+
return backend;
|
|
325
|
+
}
|
|
326
|
+
async fetchItemsFromStorage(collection) {
|
|
327
|
+
if (!collection) {
|
|
328
|
+
await Promise.all([...this.collections].map((currentCollection) => this.fetchItemsFromStorage(currentCollection)));
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
const storageAdapter = this.storageAdapters.get(collection.name);
|
|
332
|
+
if (!storageAdapter) return;
|
|
333
|
+
const items = await storageAdapter.readAll();
|
|
334
|
+
this.items.set(collection.name, items.reduce((map, item) => {
|
|
335
|
+
map.set(require_serializeValue.default(item.id), item);
|
|
336
|
+
return map;
|
|
337
|
+
}, /* @__PURE__ */ new Map()));
|
|
338
|
+
this.rebuildIndices(collection);
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
//#endregion
|
|
342
|
+
exports.default = DefaultDataAdapter;
|
package/dist/index24.mjs
CHANGED
|
@@ -1,8 +1,342 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import isEqual from "./index2.mjs";
|
|
2
|
+
import EventEmitter from "./index6.mjs";
|
|
3
|
+
import serializeValue from "./index12.mjs";
|
|
4
|
+
import createIndex from "./index14.mjs";
|
|
5
|
+
import getIndexInfo from "./index16.mjs";
|
|
6
|
+
import deepClone from "./index17.mjs";
|
|
7
|
+
import match from "./index18.mjs";
|
|
8
|
+
import modify from "./index19.mjs";
|
|
9
|
+
import project from "./index21.mjs";
|
|
10
|
+
import queryId from "./index22.mjs";
|
|
11
|
+
import sortItems from "./index23.mjs";
|
|
12
|
+
//#region src/DefaultDataAdapter.ts
|
|
13
|
+
/**
|
|
14
|
+
* Checks if there are any pending updates in the given changeset.
|
|
15
|
+
* @template T - The type of the items in the changeset.
|
|
16
|
+
* @param pendingUpdates - The changeset to check for pending updates.
|
|
17
|
+
* @returns `true` if there are pending updates, otherwise `false`.
|
|
18
|
+
*/
|
|
19
|
+
function hasPendingUpdates(pendingUpdates) {
|
|
20
|
+
return pendingUpdates.added.length > 0 || pendingUpdates.modified.length > 0 || pendingUpdates.removed.length > 0;
|
|
5
21
|
}
|
|
6
|
-
|
|
7
|
-
|
|
22
|
+
var DefaultDataAdapter = class {
|
|
23
|
+
items = /* @__PURE__ */ new Map();
|
|
24
|
+
options;
|
|
25
|
+
storageAdapters = /* @__PURE__ */ new Map();
|
|
26
|
+
collections = /* @__PURE__ */ new Set();
|
|
27
|
+
indices = /* @__PURE__ */ new Map();
|
|
28
|
+
activeQueries = /* @__PURE__ */ new Map();
|
|
29
|
+
queryEmitters = /* @__PURE__ */ new Map();
|
|
30
|
+
queuedQueryUpdates = /* @__PURE__ */ new Map();
|
|
31
|
+
cachedQueryResults = /* @__PURE__ */ new Map();
|
|
32
|
+
constructor(options) {
|
|
33
|
+
this.options = options || {};
|
|
34
|
+
}
|
|
35
|
+
ensureStorageAdapter(name) {
|
|
36
|
+
if (this.storageAdapters.get(name)) return;
|
|
37
|
+
if (!this.options.storage) return;
|
|
38
|
+
const adapter = this.options.storage(name);
|
|
39
|
+
if (!adapter) return;
|
|
40
|
+
this.storageAdapters.set(name, adapter);
|
|
41
|
+
}
|
|
42
|
+
rebuildIndices(collection) {
|
|
43
|
+
const items = this.items.get(collection.name);
|
|
44
|
+
if (!items) throw new Error(`Items not found for collection ${collection.name}`);
|
|
45
|
+
const indices = this.indices.get(collection.name);
|
|
46
|
+
if (!indices) throw new Error(`Indices not found for collection ${collection.name}`);
|
|
47
|
+
indices.forEach((index) => index.rebuild([...items.values()]));
|
|
48
|
+
}
|
|
49
|
+
async setupStorageAdapter(collection) {
|
|
50
|
+
const storageAdapter = this.storageAdapters.get(collection.name);
|
|
51
|
+
if (!storageAdapter) return;
|
|
52
|
+
return storageAdapter.setup().then(async () => {
|
|
53
|
+
await this.fetchItemsFromStorage(collection);
|
|
54
|
+
}).catch((error) => {
|
|
55
|
+
if (!this.options.onError) {
|
|
56
|
+
console.error(`Error during data persistence operation in collection ${collection.name}`, error);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
this.options.onError(collection.name, error instanceof Error ? error : new Error(error));
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
getIndexInfo(collection, selector) {
|
|
63
|
+
if (selector != null && Object.keys(selector).length === 1 && "id" in selector && typeof selector.id !== "object") return {
|
|
64
|
+
matched: true,
|
|
65
|
+
ids: [serializeValue(selector.id)],
|
|
66
|
+
optimizedSelector: {}
|
|
67
|
+
};
|
|
68
|
+
if (selector == null) return {
|
|
69
|
+
matched: false,
|
|
70
|
+
ids: [],
|
|
71
|
+
optimizedSelector: {}
|
|
72
|
+
};
|
|
73
|
+
return getIndexInfo((this.indices.get(collection.name) ?? []).map((i) => i.query.bind(i)), selector);
|
|
74
|
+
}
|
|
75
|
+
applyIndexDeltas(collection, changes) {
|
|
76
|
+
const indices = this.indices.get(collection.name) ?? [];
|
|
77
|
+
for (const index of indices) {
|
|
78
|
+
if (changes.modified) index.update(changes.modified);
|
|
79
|
+
if (changes.added && changes.added.length > 0) index.insert(changes.added);
|
|
80
|
+
if (changes.removed && changes.removed.length > 0) index.remove(changes.removed);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
getItem(collection, selector) {
|
|
84
|
+
const memory = this.items.get(collection.name) ?? /* @__PURE__ */ new Map();
|
|
85
|
+
const indexInfo = this.getIndexInfo(collection, selector);
|
|
86
|
+
return (indexInfo.matched ? indexInfo.ids.map((id) => memory.get(serializeValue(id))).filter((i) => i != null) : [...memory.values()]).find((document) => match(document, selector));
|
|
87
|
+
}
|
|
88
|
+
queryItems(collection, selector) {
|
|
89
|
+
const indexInfo = this.getIndexInfo(collection, selector);
|
|
90
|
+
const matchItems = (item) => {
|
|
91
|
+
if (indexInfo.optimizedSelector == null) return true;
|
|
92
|
+
if (Object.keys(indexInfo.optimizedSelector).length <= 0) return true;
|
|
93
|
+
return match(item, indexInfo.optimizedSelector);
|
|
94
|
+
};
|
|
95
|
+
const items = this.items.get(collection.name);
|
|
96
|
+
if (!indexInfo.matched) {
|
|
97
|
+
if (isEqual(selector, {})) return [...items.values()];
|
|
98
|
+
return [...items.values()].filter(matchItems);
|
|
99
|
+
}
|
|
100
|
+
const foundItems = indexInfo.ids.map((ids) => items.get(serializeValue(ids))).filter((i) => i != null);
|
|
101
|
+
if (isEqual(indexInfo.optimizedSelector, {})) return foundItems;
|
|
102
|
+
return foundItems.filter(matchItems);
|
|
103
|
+
}
|
|
104
|
+
executeQuery(collection, selector, options) {
|
|
105
|
+
const items = this.queryItems(collection, selector || {});
|
|
106
|
+
const { sort, skip, limit, fields } = options || {};
|
|
107
|
+
const sorted = sort ? sortItems(items, sort) : items;
|
|
108
|
+
const skipped = skip ? sorted.slice(skip) : sorted;
|
|
109
|
+
const limited = limit ? skipped.slice(0, limit) : skipped;
|
|
110
|
+
const idExcluded = fields && fields.id === 0;
|
|
111
|
+
return limited.map((item) => {
|
|
112
|
+
if (!fields) return item;
|
|
113
|
+
return {
|
|
114
|
+
...idExcluded ? {} : { id: item.id },
|
|
115
|
+
...project(item, fields)
|
|
116
|
+
};
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
flushQueuedQueryUpdates(collection) {
|
|
120
|
+
if (!this.queuedQueryUpdates.get(collection.name)) return;
|
|
121
|
+
const changes = this.queuedQueryUpdates.get(collection.name);
|
|
122
|
+
if (!changes || !hasPendingUpdates(changes)) return;
|
|
123
|
+
this.queuedQueryUpdates.set(collection.name, {
|
|
124
|
+
added: [],
|
|
125
|
+
modified: [],
|
|
126
|
+
removed: []
|
|
127
|
+
});
|
|
128
|
+
const flatItems = [
|
|
129
|
+
...changes.added,
|
|
130
|
+
...changes.modified,
|
|
131
|
+
...changes.removed
|
|
132
|
+
];
|
|
133
|
+
const itemIds = new Set(flatItems.map((i) => i.id));
|
|
134
|
+
[...this.activeQueries.get(collection.name)?.values() ?? []].filter(({ selector, options }) => {
|
|
135
|
+
if ((this.cachedQueryResults.get(collection.name)?.get(queryId(selector, options))?.map((i) => i.id) ?? []).some((id) => itemIds.has(id))) return true;
|
|
136
|
+
return flatItems.some((item) => match(item, selector));
|
|
137
|
+
}).forEach(({ selector, options }) => {
|
|
138
|
+
this.executeAndCacheQuery(collection, selector, options);
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
executeAndCacheQuery(collection, selector, options) {
|
|
142
|
+
const result = this.executeQuery(collection, selector, options);
|
|
143
|
+
this.cachedQueryResults.set(collection.name, this.cachedQueryResults.get(collection.name) || /* @__PURE__ */ new Map());
|
|
144
|
+
this.cachedQueryResults.get(collection.name)?.set(queryId(selector, options), result);
|
|
145
|
+
const emitter = this.queryEmitters.get(collection.name);
|
|
146
|
+
if (!emitter) return;
|
|
147
|
+
emitter.emit("change", selector, options, "complete");
|
|
148
|
+
}
|
|
149
|
+
updateQueries(collection, changes) {
|
|
150
|
+
this.queuedQueryUpdates.set(collection.name, this.queuedQueryUpdates.get(collection.name) || {
|
|
151
|
+
added: [],
|
|
152
|
+
modified: [],
|
|
153
|
+
removed: []
|
|
154
|
+
});
|
|
155
|
+
this.queuedQueryUpdates.get(collection.name)?.added.push(...changes.added);
|
|
156
|
+
this.queuedQueryUpdates.get(collection.name)?.modified.push(...changes.modified);
|
|
157
|
+
this.queuedQueryUpdates.get(collection.name)?.removed.push(...changes.removed);
|
|
158
|
+
this.flushQueuedQueryUpdates(collection);
|
|
159
|
+
}
|
|
160
|
+
createCollectionBackend(collection, indices) {
|
|
161
|
+
this.collections.add(collection);
|
|
162
|
+
this.ensureStorageAdapter(collection.name);
|
|
163
|
+
this.items.set(collection.name, this.items.get(collection.name) ?? /* @__PURE__ */ new Map());
|
|
164
|
+
this.queryEmitters.set(collection.name, this.queryEmitters.get(collection.name) ?? new EventEmitter());
|
|
165
|
+
this.queryEmitters.get(collection.name)?.setMaxListeners(Infinity);
|
|
166
|
+
this.activeQueries.set(collection.name, this.activeQueries.get(collection.name) || /* @__PURE__ */ new Map());
|
|
167
|
+
this.indices.set(collection.name, indices.map((field) => createIndex(field)));
|
|
168
|
+
this.rebuildIndices(collection);
|
|
169
|
+
const persistenceReadyPromise = this.setupStorageAdapter(collection);
|
|
170
|
+
const backend = {
|
|
171
|
+
insert: async (newItem) => {
|
|
172
|
+
const items = this.items.get(collection.name);
|
|
173
|
+
if (!items) throw new Error(`Items not found for collection ${collection.name}`);
|
|
174
|
+
if (items.has(serializeValue(newItem.id))) throw new Error(`Item with id '${newItem.id}' already exists`);
|
|
175
|
+
this.items.get(collection.name)?.set(serializeValue(newItem.id), newItem);
|
|
176
|
+
await this.storageAdapters.get(collection.name)?.insert([newItem]);
|
|
177
|
+
this.applyIndexDeltas(collection, { added: [newItem] });
|
|
178
|
+
this.updateQueries(collection, {
|
|
179
|
+
added: [],
|
|
180
|
+
modified: [newItem],
|
|
181
|
+
removed: []
|
|
182
|
+
});
|
|
183
|
+
return newItem;
|
|
184
|
+
},
|
|
185
|
+
updateOne: async (selector, modifier) => {
|
|
186
|
+
const { $setOnInsert, ...restModifier } = modifier;
|
|
187
|
+
const item = this.getItem(collection, selector);
|
|
188
|
+
if (item == null) return [];
|
|
189
|
+
const modifiedItem = modify(deepClone(item), restModifier);
|
|
190
|
+
if (item.id !== modifiedItem.id && this.getItem(collection, { id: modifiedItem.id }) != null) throw new Error(`Item with id '${modifiedItem.id}' already exists`);
|
|
191
|
+
this.items.get(collection.name)?.set(serializeValue(modifiedItem.id), modifiedItem);
|
|
192
|
+
await this.storageAdapters.get(collection.name)?.replace([modifiedItem]);
|
|
193
|
+
this.applyIndexDeltas(collection, { modified: [{
|
|
194
|
+
oldItem: item,
|
|
195
|
+
newItem: modifiedItem
|
|
196
|
+
}] });
|
|
197
|
+
this.updateQueries(collection, {
|
|
198
|
+
added: [],
|
|
199
|
+
modified: [modifiedItem],
|
|
200
|
+
removed: []
|
|
201
|
+
});
|
|
202
|
+
return [modifiedItem];
|
|
203
|
+
},
|
|
204
|
+
updateMany: async (selector, modifier) => {
|
|
205
|
+
const { $setOnInsert, ...restModifier } = modifier;
|
|
206
|
+
const items = this.executeQuery(collection, selector, {});
|
|
207
|
+
const changedItems = items.map((item) => {
|
|
208
|
+
const modifiedItem = modify(deepClone(item), restModifier);
|
|
209
|
+
if (item.id !== modifiedItem.id && this.getItem(collection, { id: modifiedItem.id }) != null) throw new Error(`Item with id '${modifiedItem.id}' already exists`);
|
|
210
|
+
return modifiedItem;
|
|
211
|
+
});
|
|
212
|
+
changedItems.forEach((item) => {
|
|
213
|
+
this.items.get(collection.name)?.set(serializeValue(item.id), item);
|
|
214
|
+
});
|
|
215
|
+
await this.storageAdapters.get(collection.name)?.replace(changedItems);
|
|
216
|
+
const pairs = items.map((oldItem, index) => ({
|
|
217
|
+
oldItem,
|
|
218
|
+
newItem: changedItems[index]
|
|
219
|
+
}));
|
|
220
|
+
this.applyIndexDeltas(collection, { modified: pairs });
|
|
221
|
+
this.updateQueries(collection, {
|
|
222
|
+
added: [],
|
|
223
|
+
modified: changedItems,
|
|
224
|
+
removed: []
|
|
225
|
+
});
|
|
226
|
+
return changedItems;
|
|
227
|
+
},
|
|
228
|
+
replaceOne: async (selector, replacement) => {
|
|
229
|
+
const item = this.getItem(collection, selector);
|
|
230
|
+
if (item == null) return [];
|
|
231
|
+
if (item.id !== replacement.id && replacement.id != null && this.getItem(collection, { id: replacement.id }) != null) throw new Error(`Item with id '${replacement.id}' already exists`);
|
|
232
|
+
const modifiedItem = {
|
|
233
|
+
id: item.id,
|
|
234
|
+
...replacement
|
|
235
|
+
};
|
|
236
|
+
this.items.get(collection.name)?.set(serializeValue(modifiedItem.id), modifiedItem);
|
|
237
|
+
await this.storageAdapters.get(collection.name)?.replace([modifiedItem]);
|
|
238
|
+
this.applyIndexDeltas(collection, { modified: [{
|
|
239
|
+
oldItem: item,
|
|
240
|
+
newItem: modifiedItem
|
|
241
|
+
}] });
|
|
242
|
+
this.updateQueries(collection, {
|
|
243
|
+
added: [],
|
|
244
|
+
modified: [modifiedItem],
|
|
245
|
+
removed: []
|
|
246
|
+
});
|
|
247
|
+
return [modifiedItem];
|
|
248
|
+
},
|
|
249
|
+
removeOne: async (selector) => {
|
|
250
|
+
const item = this.getItem(collection, selector);
|
|
251
|
+
if (item == null) return [];
|
|
252
|
+
this.items.get(collection.name)?.delete(serializeValue(item.id));
|
|
253
|
+
await this.storageAdapters.get(collection.name)?.remove([item]);
|
|
254
|
+
this.applyIndexDeltas(collection, { removed: [item] });
|
|
255
|
+
this.updateQueries(collection, {
|
|
256
|
+
added: [],
|
|
257
|
+
modified: [],
|
|
258
|
+
removed: [item]
|
|
259
|
+
});
|
|
260
|
+
return [item];
|
|
261
|
+
},
|
|
262
|
+
removeMany: async (selector) => {
|
|
263
|
+
const items = backend.getQueryResult(selector, {});
|
|
264
|
+
items.forEach((item) => {
|
|
265
|
+
this.items.get(collection.name)?.delete(serializeValue(item.id));
|
|
266
|
+
});
|
|
267
|
+
await this.storageAdapters.get(collection.name)?.remove(items);
|
|
268
|
+
this.applyIndexDeltas(collection, { removed: items });
|
|
269
|
+
this.updateQueries(collection, {
|
|
270
|
+
added: [],
|
|
271
|
+
modified: [],
|
|
272
|
+
removed: items
|
|
273
|
+
});
|
|
274
|
+
return items;
|
|
275
|
+
},
|
|
276
|
+
registerQuery: (selector, options) => {
|
|
277
|
+
this.activeQueries.set(collection.name, this.activeQueries.get(collection.name) || /* @__PURE__ */ new Map());
|
|
278
|
+
this.activeQueries.get(collection.name)?.set(queryId(selector, options), {
|
|
279
|
+
selector,
|
|
280
|
+
options
|
|
281
|
+
});
|
|
282
|
+
this.executeAndCacheQuery(collection, selector, options);
|
|
283
|
+
},
|
|
284
|
+
unregisterQuery: (selector, options) => {
|
|
285
|
+
if (!this.activeQueries.get(collection.name)) return;
|
|
286
|
+
this.activeQueries.get(collection.name)?.delete(queryId(selector, options));
|
|
287
|
+
},
|
|
288
|
+
getQueryState: () => "complete",
|
|
289
|
+
onQueryStateChange: (selector, options, callback) => {
|
|
290
|
+
const emitter = this.queryEmitters.get(collection.name);
|
|
291
|
+
if (!emitter) throw new Error(`Query emitter not found for collection ${collection.name}`);
|
|
292
|
+
const handler = (querySelector, queryOptions, state) => {
|
|
293
|
+
if (queryId(querySelector, queryOptions) !== queryId(selector, options)) return;
|
|
294
|
+
callback(state);
|
|
295
|
+
};
|
|
296
|
+
emitter.on("change", handler);
|
|
297
|
+
return () => {
|
|
298
|
+
emitter.off("change", handler);
|
|
299
|
+
};
|
|
300
|
+
},
|
|
301
|
+
getQueryError: () => null,
|
|
302
|
+
getQueryResult: (selector, options) => {
|
|
303
|
+
if (this.activeQueries.get(collection.name)?.has(queryId(selector, options))) {
|
|
304
|
+
const results = this.cachedQueryResults.get(collection.name)?.get(queryId(selector, options));
|
|
305
|
+
if (!results) throw new Error("Cached query results are not defined!");
|
|
306
|
+
return results;
|
|
307
|
+
}
|
|
308
|
+
return this.executeQuery(collection, selector, options);
|
|
309
|
+
},
|
|
310
|
+
executeQuery: (selector, options) => Promise.resolve(this.executeQuery(collection, selector, options)),
|
|
311
|
+
dispose: async () => {
|
|
312
|
+
const adapter = this.storageAdapters.get(collection.name);
|
|
313
|
+
if (adapter?.teardown) await adapter.teardown();
|
|
314
|
+
this.storageAdapters.delete(collection.name);
|
|
315
|
+
this.items.delete(collection.name);
|
|
316
|
+
this.indices.delete(collection.name);
|
|
317
|
+
this.activeQueries.delete(collection.name);
|
|
318
|
+
this.queryEmitters.delete(collection.name);
|
|
319
|
+
this.queuedQueryUpdates.delete(collection.name);
|
|
320
|
+
this.cachedQueryResults.delete(collection.name);
|
|
321
|
+
},
|
|
322
|
+
isReady: () => persistenceReadyPromise
|
|
323
|
+
};
|
|
324
|
+
return backend;
|
|
325
|
+
}
|
|
326
|
+
async fetchItemsFromStorage(collection) {
|
|
327
|
+
if (!collection) {
|
|
328
|
+
await Promise.all([...this.collections].map((currentCollection) => this.fetchItemsFromStorage(currentCollection)));
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
const storageAdapter = this.storageAdapters.get(collection.name);
|
|
332
|
+
if (!storageAdapter) return;
|
|
333
|
+
const items = await storageAdapter.readAll();
|
|
334
|
+
this.items.set(collection.name, items.reduce((map, item) => {
|
|
335
|
+
map.set(serializeValue(item.id), item);
|
|
336
|
+
return map;
|
|
337
|
+
}, /* @__PURE__ */ new Map()));
|
|
338
|
+
this.rebuildIndices(collection);
|
|
339
|
+
}
|
|
8
340
|
};
|
|
341
|
+
//#endregion
|
|
342
|
+
export { DefaultDataAdapter as default };
|