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