@signaldb/core 2.0.0-beta.0 → 2.0.0-beta.10
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 +128 -75
- package/dist/AsyncDataAdapter.d.ts +44 -7
- package/dist/AutoFetchDataAdapter.d.ts +5 -8
- package/dist/Collection/Cursor.d.ts +6 -5
- package/dist/Collection/Observer.d.ts +2 -0
- package/dist/Collection/index.d.ts +36 -25
- package/dist/Collection/types.d.ts +10 -2
- package/dist/DataAdapter.d.ts +18 -11
- package/dist/DefaultDataAdapter.d.ts +7 -6
- package/dist/WorkerDataAdapter.d.ts +7 -6
- package/dist/WorkerDataAdapterHost.d.ts +14 -11
- 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 +3 -1
- package/dist/index.mjs +17 -32
- 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 -339
- package/dist/index13.cjs.js +45 -0
- package/dist/index13.mjs +44 -407
- package/dist/index14.cjs.js +86 -0
- package/dist/index14.mjs +85 -190
- package/dist/index15.cjs.js +15 -0
- package/dist/index15.mjs +14 -341
- package/dist/index16.cjs.js +136 -0
- package/dist/index16.mjs +134 -569
- package/dist/index17.cjs.js +47 -0
- package/dist/index17.mjs +46 -150
- package/dist/index18.cjs.js +15 -0
- package/dist/index18.mjs +14 -26
- package/dist/index19.cjs.js +23 -0
- package/dist/index19.mjs +22 -42
- package/dist/index2.cjs.js +33 -0
- package/dist/index2.mjs +32 -254
- package/dist/index20.cjs.js +37 -0
- package/dist/index20.mjs +36 -104
- package/dist/index21.cjs.js +35 -0
- package/dist/index21.mjs +34 -128
- package/dist/index22.cjs.js +25 -0
- package/dist/index22.mjs +24 -7
- package/dist/index23.cjs.js +19 -0
- package/dist/index23.mjs +17 -26
- package/dist/index24.cjs.js +342 -0
- package/dist/index24.mjs +340 -6
- package/dist/index25.cjs.js +542 -0
- package/dist/index25.mjs +541 -10
- package/dist/index26.cjs.js +11 -0
- package/dist/index26.mjs +10 -29
- package/dist/index27.cjs.js +11 -0
- package/dist/index27.mjs +10 -5
- package/dist/index28.cjs.js +90 -0
- package/dist/index28.mjs +89 -8
- package/dist/index29.cjs.js +424 -0
- package/dist/index29.mjs +423 -29
- package/dist/index3.cjs.js +19 -0
- package/dist/index3.mjs +18 -537
- package/dist/index30.cjs.js +72 -0
- package/dist/index30.mjs +71 -42
- package/dist/index31.cjs.js +191 -0
- package/dist/index31.mjs +190 -9
- package/dist/index32.cjs.js +21 -0
- package/dist/index32.mjs +21 -0
- package/dist/index33.cjs.js +336 -0
- package/dist/index33.mjs +336 -0
- package/dist/index34.cjs.js +539 -0
- package/dist/index34.mjs +539 -0
- 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/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/utils/batchOnNextTick.d.ts +16 -0
- package/dist/utils/createSignal.d.ts +2 -2
- package/dist/utils/deepClone.d.ts +1 -1
- package/dist/utils/getMatchingKeys.d.ts +2 -2
- 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/queryId.d.ts +2 -2
- package/dist/utils/reactiveOrAsync.d.ts +59 -0
- package/package.json +3 -3
- package/dist/index.cjs10.js +0 -18
- package/dist/index.cjs11.js +0 -15
- package/dist/index.cjs12.js +0 -339
- package/dist/index.cjs13.js +0 -407
- package/dist/index.cjs14.js +0 -190
- package/dist/index.cjs15.js +0 -341
- package/dist/index.cjs16.js +0 -570
- package/dist/index.cjs17.js +0 -150
- package/dist/index.cjs18.js +0 -26
- package/dist/index.cjs19.js +0 -43
- package/dist/index.cjs2.js +0 -255
- package/dist/index.cjs20.js +0 -104
- package/dist/index.cjs21.js +0 -129
- package/dist/index.cjs22.js +0 -7
- package/dist/index.cjs23.js +0 -27
- package/dist/index.cjs24.js +0 -7
- package/dist/index.cjs25.js +0 -10
- package/dist/index.cjs26.js +0 -29
- package/dist/index.cjs27.js +0 -5
- package/dist/index.cjs28.js +0 -7
- package/dist/index.cjs29.js +0 -29
- package/dist/index.cjs3.js +0 -538
- package/dist/index.cjs30.js +0 -42
- package/dist/index.cjs31.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
package/dist/index16.mjs
CHANGED
|
@@ -1,571 +1,136 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
import intersection from "./index15.mjs";
|
|
2
|
+
//#region src/getIndexInfo.ts
|
|
3
|
+
/**
|
|
4
|
+
* Retrieves merged index information for a given flat selector by querying multiple
|
|
5
|
+
* index providers. Combines results from all index providers to determine matched ids
|
|
6
|
+
* and an optimized selector.
|
|
7
|
+
* @template T - The type of the items in the collection.
|
|
8
|
+
* @template I - The type of the unique identifier for the items.
|
|
9
|
+
* @param queryFunctions - An array of index providers to query.
|
|
10
|
+
* @param selector - The flat selector used to filter items.
|
|
11
|
+
* @returns An object containing:
|
|
12
|
+
* - `matched`: A boolean indicating if the selector matched any items.
|
|
13
|
+
* - `ids`: An array of matched item ids.
|
|
14
|
+
* - `optimizedSelector`: A flat selector optimized based on the index results.
|
|
15
|
+
*/
|
|
16
|
+
function getMergedIndexInfo(queryFunctions, selector) {
|
|
17
|
+
return queryFunctions.reduce((memoOrPromise, queryFunction) => {
|
|
18
|
+
const resultOrPromise = queryFunction(selector);
|
|
19
|
+
const processResult = (memo, result) => {
|
|
20
|
+
if (!result.matched) return memo;
|
|
21
|
+
const optimizedSelector = result.keepSelector ? memo.optimizedSelector : Object.fromEntries(Object.entries(memo.optimizedSelector).filter(([key]) => !result.fields.includes(key)));
|
|
22
|
+
return {
|
|
23
|
+
matched: true,
|
|
24
|
+
ids: [...new Set(memo.matched ? intersection(memo.ids, result.ids) : result.ids)],
|
|
25
|
+
optimizedSelector
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
if (resultOrPromise instanceof Promise) return resultOrPromise.then(async (result) => {
|
|
29
|
+
return processResult(memoOrPromise instanceof Promise ? await memoOrPromise : memoOrPromise, result);
|
|
30
|
+
});
|
|
31
|
+
const memo = memoOrPromise;
|
|
32
|
+
if (memo instanceof Promise) throw new Error("Mixing async and sync index providers is not supported");
|
|
33
|
+
return processResult(memo, resultOrPromise);
|
|
34
|
+
}, {
|
|
35
|
+
matched: false,
|
|
36
|
+
ids: [],
|
|
37
|
+
optimizedSelector: { ...selector }
|
|
38
|
+
});
|
|
12
39
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if (options.registerRemoteChange) {
|
|
46
|
-
void options.registerRemoteChange(async () => {
|
|
47
|
-
await this.forceRefetchAll();
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
createCollectionBackend(collection, indices) {
|
|
52
|
-
this.collectionIndices.set(collection.name, indices);
|
|
53
|
-
this.queries.set(collection.name, /* @__PURE__ */ new Map());
|
|
54
|
-
this.ensureStorageAdapter(collection.name);
|
|
55
|
-
this.activeObservers.set(collection.name, /* @__PURE__ */ new Map());
|
|
56
|
-
this.observerTimeouts.set(collection.name, /* @__PURE__ */ new Map());
|
|
57
|
-
this.selectorIds.set(collection.name, /* @__PURE__ */ new Map());
|
|
58
|
-
this.idRefCounts.set(collection.name, /* @__PURE__ */ new Map());
|
|
59
|
-
this.autoloadIds.set(collection.name, /* @__PURE__ */ new Set());
|
|
60
|
-
const ready = this.setupStorage(collection.name, indices);
|
|
61
|
-
this.storageAdapterReady.set(collection.name, ready);
|
|
62
|
-
const registerQuery = (selector, options) => {
|
|
63
|
-
const qid = queryId(selector, options);
|
|
64
|
-
const registry = this.queries.get(collection.name);
|
|
65
|
-
if (!registry)
|
|
66
|
-
throw new Error(`Collection ${collection.name} not initialized!`);
|
|
67
|
-
registry.set(qid, {
|
|
68
|
-
selector,
|
|
69
|
-
options,
|
|
70
|
-
items: [],
|
|
71
|
-
listeners: /* @__PURE__ */ new Set(),
|
|
72
|
-
...registry.get(qid),
|
|
73
|
-
state: "active",
|
|
74
|
-
error: null
|
|
75
|
-
});
|
|
76
|
-
const key = this.selectorKey(selector);
|
|
77
|
-
const perColObservers = this.activeObservers.get(collection.name);
|
|
78
|
-
const current = perColObservers?.get(key)?.count ?? 0;
|
|
79
|
-
perColObservers?.set(key, { selector, count: current + 1 });
|
|
80
|
-
const timeouts = this.observerTimeouts.get(collection.name);
|
|
81
|
-
const t = timeouts?.get(key);
|
|
82
|
-
if (t)
|
|
83
|
-
clearTimeout(t);
|
|
84
|
-
if (current === 0) {
|
|
85
|
-
void this.fetchAndIngest(collection.name, selector).catch(this.onError);
|
|
86
|
-
}
|
|
87
|
-
void this.fulfillQuery(collection.name, selector, options).catch(this.onError);
|
|
88
|
-
};
|
|
89
|
-
const unregisterQuery = (selector, options) => {
|
|
90
|
-
const qid = queryId(selector, options);
|
|
91
|
-
const registry = this.queries.get(collection.name);
|
|
92
|
-
registry?.delete(qid);
|
|
93
|
-
const key = this.selectorKey(selector);
|
|
94
|
-
const perColObservers = this.activeObservers.get(collection.name);
|
|
95
|
-
const current = perColObservers?.get(key)?.count ?? 0;
|
|
96
|
-
const remaining = Math.max(0, current - 1);
|
|
97
|
-
if (remaining > 0) {
|
|
98
|
-
perColObservers?.set(key, { selector, count: remaining });
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
101
|
-
const doPurge = () => {
|
|
102
|
-
perColObservers?.delete(key);
|
|
103
|
-
void this.purgeSelector(collection.name, selector).catch(this.onError);
|
|
104
|
-
};
|
|
105
|
-
if (this.purgeDelay === 0) {
|
|
106
|
-
doPurge();
|
|
107
|
-
} else {
|
|
108
|
-
const timeouts = this.observerTimeouts.get(collection.name);
|
|
109
|
-
const t = timeouts?.get(key);
|
|
110
|
-
if (t)
|
|
111
|
-
clearTimeout(t);
|
|
112
|
-
timeouts?.set(key, setTimeout(doPurge, this.purgeDelay));
|
|
113
|
-
}
|
|
114
|
-
};
|
|
115
|
-
const getQueryState = (selector, options) => {
|
|
116
|
-
const q = this.queries.get(collection.name)?.get(queryId(selector, options));
|
|
117
|
-
return q?.state ?? "active";
|
|
118
|
-
};
|
|
119
|
-
const getQueryError = (selector, options) => {
|
|
120
|
-
const q = this.queries.get(collection.name)?.get(queryId(selector, options));
|
|
121
|
-
return q?.error ?? null;
|
|
122
|
-
};
|
|
123
|
-
const getQueryResult = (selector, options) => {
|
|
124
|
-
const q = this.queries.get(collection.name)?.get(queryId(selector, options));
|
|
125
|
-
return q?.items ?? [];
|
|
126
|
-
};
|
|
127
|
-
const onQueryStateChange = (selector, options, callback) => {
|
|
128
|
-
const qid = queryId(selector, options);
|
|
129
|
-
const registry = this.queries.get(collection.name);
|
|
130
|
-
if (!registry)
|
|
131
|
-
throw new Error(`Collection ${collection.name} not initialized!`);
|
|
132
|
-
if (!registry.has(qid)) {
|
|
133
|
-
registry.set(qid, {
|
|
134
|
-
selector,
|
|
135
|
-
options,
|
|
136
|
-
state: "active",
|
|
137
|
-
error: null,
|
|
138
|
-
items: [],
|
|
139
|
-
listeners: /* @__PURE__ */ new Set()
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
registry.get(qid)?.listeners.add(callback);
|
|
143
|
-
return () => registry.get(qid)?.listeners.delete(callback);
|
|
144
|
-
};
|
|
145
|
-
return {
|
|
146
|
-
insert: async (item) => {
|
|
147
|
-
await ready;
|
|
148
|
-
const inserted = await this.insert(collection.name, item);
|
|
149
|
-
return inserted;
|
|
150
|
-
},
|
|
151
|
-
updateOne: async (selector, modifier) => {
|
|
152
|
-
await ready;
|
|
153
|
-
return this.updateOne(collection.name, selector, modifier);
|
|
154
|
-
},
|
|
155
|
-
updateMany: async (selector, modifier) => {
|
|
156
|
-
await ready;
|
|
157
|
-
return this.updateMany(collection.name, selector, modifier);
|
|
158
|
-
},
|
|
159
|
-
replaceOne: async (selector, replacement) => {
|
|
160
|
-
await ready;
|
|
161
|
-
return this.replaceOne(collection.name, selector, replacement);
|
|
162
|
-
},
|
|
163
|
-
removeOne: async (selector) => {
|
|
164
|
-
await ready;
|
|
165
|
-
return this.removeOne(collection.name, selector);
|
|
166
|
-
},
|
|
167
|
-
removeMany: async (selector) => {
|
|
168
|
-
await ready;
|
|
169
|
-
return this.removeMany(collection.name, selector);
|
|
170
|
-
},
|
|
171
|
-
registerQuery,
|
|
172
|
-
unregisterQuery,
|
|
173
|
-
getQueryState,
|
|
174
|
-
getQueryError,
|
|
175
|
-
getQueryResult,
|
|
176
|
-
onQueryStateChange,
|
|
177
|
-
dispose: async () => {
|
|
178
|
-
this.storageAdapters.delete(collection.name);
|
|
179
|
-
this.queries.delete(collection.name);
|
|
180
|
-
this.collectionIndices.delete(collection.name);
|
|
181
|
-
this.storageAdapterReady.delete(collection.name);
|
|
182
|
-
this.activeObservers.delete(collection.name);
|
|
183
|
-
this.observerTimeouts.delete(collection.name);
|
|
184
|
-
this.selectorIds.delete(collection.name);
|
|
185
|
-
this.idRefCounts.delete(collection.name);
|
|
186
|
-
this.autoloadIds.delete(collection.name);
|
|
187
|
-
},
|
|
188
|
-
isReady: async () => {
|
|
189
|
-
await ready;
|
|
190
|
-
}
|
|
191
|
-
};
|
|
192
|
-
}
|
|
193
|
-
// ===== Auto-fetch mechanics =====
|
|
194
|
-
selectorKey(selector) {
|
|
195
|
-
return JSON.stringify(selector ?? {});
|
|
196
|
-
}
|
|
197
|
-
async forceRefetchAll() {
|
|
198
|
-
const tasks = [];
|
|
199
|
-
for (const [collectionName, observers] of this.activeObservers.entries()) {
|
|
200
|
-
for (const { selector, count } of observers.values()) {
|
|
201
|
-
if (count > 0)
|
|
202
|
-
tasks.push(this.fetchAndIngest(collectionName, selector));
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
await Promise.all(tasks);
|
|
206
|
-
}
|
|
207
|
-
async fetchAndIngest(collectionName, selector) {
|
|
208
|
-
this.publishForSelector(collectionName, selector, "active", null);
|
|
209
|
-
try {
|
|
210
|
-
const items = await this.fetchQueryItems(collectionName, selector);
|
|
211
|
-
if (!items || !Array.isArray(items)) {
|
|
212
|
-
throw new Error("AutoFetchDataAdapter: fetchQueryItems must resolve to { items: T[] }");
|
|
213
|
-
}
|
|
214
|
-
const ids = items.map((i) => i.id);
|
|
215
|
-
const selectorKey = this.selectorKey(selector);
|
|
216
|
-
const selMap = this.selectorIds.get(collectionName);
|
|
217
|
-
const previous = selMap?.get(selectorKey) ?? /* @__PURE__ */ new Set();
|
|
218
|
-
ids.forEach((id) => previous.add(id));
|
|
219
|
-
selMap?.set(selectorKey, previous);
|
|
220
|
-
await this.upsertMerged(collectionName, items);
|
|
221
|
-
await this.checkQueryUpdates(collectionName, items);
|
|
222
|
-
} catch (error) {
|
|
223
|
-
this.publishForSelector(collectionName, selector, "error", error);
|
|
224
|
-
this.onError(error);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
async purgeSelector(collectionName, selector) {
|
|
228
|
-
const selectorKey = this.selectorKey(selector);
|
|
229
|
-
const selMap = this.selectorIds.get(collectionName);
|
|
230
|
-
const ids = selMap?.get(selectorKey);
|
|
231
|
-
selMap?.delete(selectorKey);
|
|
232
|
-
if (!ids || ids.size === 0)
|
|
233
|
-
return;
|
|
234
|
-
const referenceMap = this.idRefCounts.get(collectionName);
|
|
235
|
-
const autoload = this.autoloadIds.get(collectionName);
|
|
236
|
-
const toRemove = [];
|
|
237
|
-
for (const id of ids) {
|
|
238
|
-
const current = referenceMap?.get(id) ?? 0;
|
|
239
|
-
const next = Math.max(0, current - 1);
|
|
240
|
-
if (next === 0) {
|
|
241
|
-
referenceMap?.delete(id);
|
|
242
|
-
if (autoload?.has(id))
|
|
243
|
-
toRemove.push(id);
|
|
244
|
-
} else {
|
|
245
|
-
referenceMap?.set(id, next);
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
if (toRemove.length === 0)
|
|
249
|
-
return;
|
|
250
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
251
|
-
if (!storage)
|
|
252
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
253
|
-
const items = await storage.readIds(toRemove);
|
|
254
|
-
if (items.length > 0) {
|
|
255
|
-
await storage.remove(items);
|
|
256
|
-
await this.checkQueryUpdates(collectionName, items);
|
|
257
|
-
}
|
|
258
|
-
toRemove.forEach((id) => autoload?.delete(id));
|
|
259
|
-
}
|
|
260
|
-
// ===== Core query + CRUD plumbing (index-aware, same semantics as AsyncDataAdapter) =====
|
|
261
|
-
async setupStorage(collectionName, indices) {
|
|
262
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
263
|
-
if (!storage)
|
|
264
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
265
|
-
await Promise.all([
|
|
266
|
-
storage.createIndex("id"),
|
|
267
|
-
...indices.map((field) => storage.createIndex(field))
|
|
268
|
-
]);
|
|
269
|
-
await storage.setup();
|
|
270
|
-
}
|
|
271
|
-
ensureStorageAdapter(name) {
|
|
272
|
-
if (this.storageAdapters.has(name))
|
|
273
|
-
return;
|
|
274
|
-
const adapter = this.options.storage && this.options.storage(name);
|
|
275
|
-
if (!adapter)
|
|
276
|
-
return;
|
|
277
|
-
this.storageAdapters.set(name, adapter);
|
|
278
|
-
}
|
|
279
|
-
publishForSelector(collectionName, selector, state, error) {
|
|
280
|
-
const registry = this.queries.get(collectionName);
|
|
281
|
-
if (!registry)
|
|
282
|
-
return;
|
|
283
|
-
for (const query of registry.values()) {
|
|
284
|
-
if (!isEqual(query.selector, selector))
|
|
285
|
-
continue;
|
|
286
|
-
query.state = state;
|
|
287
|
-
query.error = error;
|
|
288
|
-
for (const callback of query.listeners) {
|
|
289
|
-
try {
|
|
290
|
-
callback(state);
|
|
291
|
-
} catch (error_) {
|
|
292
|
-
this.onError(error_);
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
publishState(collectionName, qid, state, error) {
|
|
298
|
-
const rec = this.queries.get(collectionName)?.get(qid);
|
|
299
|
-
if (!rec)
|
|
300
|
-
return;
|
|
301
|
-
rec.state = state;
|
|
302
|
-
rec.error = error;
|
|
303
|
-
for (const callback of rec.listeners) {
|
|
304
|
-
try {
|
|
305
|
-
callback(state);
|
|
306
|
-
} catch (error_) {
|
|
307
|
-
this.onError(error_);
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
publishResult(collectionName, qid, items) {
|
|
312
|
-
const rec = this.queries.get(collectionName)?.get(qid);
|
|
313
|
-
if (!rec)
|
|
314
|
-
return;
|
|
315
|
-
rec.items = items;
|
|
316
|
-
}
|
|
317
|
-
async fulfillQuery(collectionName, selector, options) {
|
|
318
|
-
const qid = queryId(selector, options);
|
|
319
|
-
const registry = this.queries.get(collectionName);
|
|
320
|
-
if (!registry)
|
|
321
|
-
throw new Error(`Collection ${collectionName} not initialized!`);
|
|
322
|
-
const rec = registry.get(qid);
|
|
323
|
-
if (!rec)
|
|
324
|
-
return;
|
|
325
|
-
this.publishState(collectionName, qid, "active", null);
|
|
326
|
-
try {
|
|
327
|
-
const items = await this.executeQuery(collectionName, selector, options);
|
|
328
|
-
this.publishResult(collectionName, qid, items);
|
|
329
|
-
this.publishState(collectionName, qid, "complete", null);
|
|
330
|
-
} catch (error) {
|
|
331
|
-
this.publishState(collectionName, qid, "error", error);
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
async getIndexInfo(collectionName, selector) {
|
|
335
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
336
|
-
if (!storage)
|
|
337
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
338
|
-
if (selector != null && Object.keys(selector).length === 1 && "id" in selector && typeof selector.id !== "object") {
|
|
339
|
-
return { matched: true, ids: [selector.id], optimizedSelector: {} };
|
|
340
|
-
}
|
|
341
|
-
if (selector == null) {
|
|
342
|
-
return { matched: false, ids: [], optimizedSelector: {} };
|
|
343
|
-
}
|
|
344
|
-
const indices = this.collectionIndices.get(collectionName) ?? [];
|
|
345
|
-
return getIndexInfo(indices.map((field) => async (flatSelector) => {
|
|
346
|
-
if (!Object.hasOwnProperty.call(flatSelector, field))
|
|
347
|
-
return { matched: false };
|
|
348
|
-
const index = await storage.readIndex(field);
|
|
349
|
-
const fieldSelector = flatSelector[field];
|
|
350
|
-
const filtersForNull = fieldSelector == null || fieldSelector.$exists === false;
|
|
351
|
-
const keys = filtersForNull ? { include: null, exclude: [...index.keys()].filter((key) => key != null) } : getMatchingKeys(field, flatSelector);
|
|
352
|
-
if (keys.include == null && keys.exclude == null)
|
|
353
|
-
return { matched: false };
|
|
354
|
-
let includedIds = [];
|
|
355
|
-
if (keys.include == null) {
|
|
356
|
-
for (const set of index.values())
|
|
357
|
-
for (const pos of set)
|
|
358
|
-
includedIds.push(pos);
|
|
359
|
-
} else {
|
|
360
|
-
for (const key of keys.include) {
|
|
361
|
-
const idSet = index.get(key);
|
|
362
|
-
if (idSet)
|
|
363
|
-
for (const id of idSet)
|
|
364
|
-
includedIds.push(id);
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
if (keys.exclude != null) {
|
|
368
|
-
const excludeIds = /* @__PURE__ */ new Set();
|
|
369
|
-
for (const key of keys.exclude) {
|
|
370
|
-
const idSet = index.get(key);
|
|
371
|
-
if (idSet)
|
|
372
|
-
for (const id of idSet)
|
|
373
|
-
excludeIds.add(id);
|
|
374
|
-
}
|
|
375
|
-
includedIds = includedIds.filter((pos) => !excludeIds.has(pos));
|
|
376
|
-
}
|
|
377
|
-
return {
|
|
378
|
-
matched: true,
|
|
379
|
-
ids: includedIds,
|
|
380
|
-
fields: [field],
|
|
381
|
-
keepSelector: filtersForNull
|
|
382
|
-
};
|
|
383
|
-
}), selector);
|
|
384
|
-
}
|
|
385
|
-
async queryItems(collectionName, selector) {
|
|
386
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
387
|
-
if (!storage)
|
|
388
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
389
|
-
const index = await this.getIndexInfo(collectionName, selector);
|
|
390
|
-
const matchItems = (item) => {
|
|
391
|
-
if (index.optimizedSelector == null)
|
|
392
|
-
return true;
|
|
393
|
-
if (Object.keys(index.optimizedSelector).length <= 0)
|
|
394
|
-
return true;
|
|
395
|
-
return match(item, index.optimizedSelector);
|
|
396
|
-
};
|
|
397
|
-
if (index.matched) {
|
|
398
|
-
const items = await storage.readIds(index.ids);
|
|
399
|
-
if (isEqual(index.optimizedSelector, {}))
|
|
400
|
-
return items;
|
|
401
|
-
return items.filter(matchItems);
|
|
402
|
-
} else {
|
|
403
|
-
const allItems = await storage.readAll();
|
|
404
|
-
if (isEqual(selector, {}))
|
|
405
|
-
return allItems;
|
|
406
|
-
return allItems.filter(matchItems);
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
async executeQuery(collectionName, selector, options) {
|
|
410
|
-
const items = await this.queryItems(collectionName, selector || {});
|
|
411
|
-
const { sort, skip, limit, fields } = options || {};
|
|
412
|
-
const sorted = sort ? sortItems(items, sort) : items;
|
|
413
|
-
const skipped = skip ? sorted.slice(skip) : sorted;
|
|
414
|
-
const limited = limit ? skipped.slice(0, limit) : skipped;
|
|
415
|
-
const idExcluded = fields && fields.id === 0;
|
|
416
|
-
return limited.map((item) => {
|
|
417
|
-
if (!fields)
|
|
418
|
-
return item;
|
|
419
|
-
return { ...idExcluded ? {} : { id: item.id }, ...project(item, fields) };
|
|
420
|
-
});
|
|
421
|
-
}
|
|
422
|
-
async checkQueryUpdates(collectionName, changedItems) {
|
|
423
|
-
const registry = this.queries.get(collectionName);
|
|
424
|
-
if (!registry)
|
|
425
|
-
throw new Error(`Collection ${collectionName} not initialized!`);
|
|
426
|
-
if (registry.size === 0)
|
|
427
|
-
return;
|
|
428
|
-
const affected = [...registry.values()].filter(({ selector }) => changedItems.some((item) => match(item, selector)));
|
|
429
|
-
if (affected.length === 0)
|
|
430
|
-
return;
|
|
431
|
-
for (const { selector, options } of affected) {
|
|
432
|
-
const qid = queryId(selector, options);
|
|
433
|
-
this.publishState(collectionName, qid, "active", null);
|
|
434
|
-
}
|
|
435
|
-
await Promise.all(affected.map(async ({ selector, options }) => {
|
|
436
|
-
const qid = queryId(selector, options);
|
|
437
|
-
try {
|
|
438
|
-
const items = await this.executeQuery(collectionName, selector, options);
|
|
439
|
-
this.publishResult(collectionName, qid, items);
|
|
440
|
-
this.publishState(collectionName, qid, "complete", null);
|
|
441
|
-
} catch (error) {
|
|
442
|
-
this.publishState(collectionName, qid, "error", error);
|
|
443
|
-
}
|
|
444
|
-
}));
|
|
445
|
-
}
|
|
446
|
-
// ===== CRUD with push-style query updates =====
|
|
447
|
-
async insert(collectionName, newItem) {
|
|
448
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
449
|
-
if (!storage)
|
|
450
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
451
|
-
const existing = await this.executeQuery(collectionName, { id: newItem.id }, { limit: 1 });
|
|
452
|
-
if (existing.length > 0)
|
|
453
|
-
throw new Error(`Item with id ${String(newItem.id)} already exists`);
|
|
454
|
-
await storage.insert([newItem]);
|
|
455
|
-
await this.checkQueryUpdates(collectionName, [newItem]);
|
|
456
|
-
return newItem;
|
|
457
|
-
}
|
|
458
|
-
async updateOne(collectionName, selector, modifier) {
|
|
459
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
460
|
-
if (!storage)
|
|
461
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
462
|
-
const items = await this.executeQuery(collectionName, selector, { limit: 1 });
|
|
463
|
-
const [item] = items;
|
|
464
|
-
const { $setOnInsert, ...rest } = modifier;
|
|
465
|
-
if (item == null)
|
|
466
|
-
return [];
|
|
467
|
-
const modified = modify(deepClone(item), rest);
|
|
468
|
-
if (item.id !== modified.id) {
|
|
469
|
-
const existing = await this.executeQuery(collectionName, { id: modified.id }, { limit: 1 });
|
|
470
|
-
if (existing.length > 0)
|
|
471
|
-
throw new Error(`Item with id ${String(modified.id)} already exists`);
|
|
472
|
-
}
|
|
473
|
-
await storage.replace([modified]);
|
|
474
|
-
await this.checkQueryUpdates(collectionName, [modified]);
|
|
475
|
-
return [modified];
|
|
476
|
-
}
|
|
477
|
-
async updateMany(collectionName, selector, modifier) {
|
|
478
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
479
|
-
if (!storage)
|
|
480
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
481
|
-
const items = await this.executeQuery(collectionName, selector);
|
|
482
|
-
if (items.length === 0)
|
|
483
|
-
return [];
|
|
484
|
-
const { $setOnInsert, ...rest } = modifier;
|
|
485
|
-
const changed = await Promise.all(items.map(async (item) => {
|
|
486
|
-
const modified = modify(deepClone(item), rest);
|
|
487
|
-
if (item.id !== modified.id) {
|
|
488
|
-
const existing = await this.executeQuery(collectionName, { id: modified.id }, { limit: 1 });
|
|
489
|
-
if (existing.length > 0)
|
|
490
|
-
throw new Error(`Item with id ${String(modified.id)} already exists`);
|
|
491
|
-
}
|
|
492
|
-
return modified;
|
|
493
|
-
}));
|
|
494
|
-
await storage.replace(changed);
|
|
495
|
-
await this.checkQueryUpdates(collectionName, changed);
|
|
496
|
-
return changed;
|
|
497
|
-
}
|
|
498
|
-
async replaceOne(collectionName, selector, replacement) {
|
|
499
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
500
|
-
if (!storage)
|
|
501
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
502
|
-
const items = await this.executeQuery(collectionName, selector, { limit: 1 });
|
|
503
|
-
const [item] = items;
|
|
504
|
-
if (item == null)
|
|
505
|
-
return [];
|
|
506
|
-
const modified = { ...replacement, id: replacement.id ?? item.id };
|
|
507
|
-
if (item.id !== modified.id) {
|
|
508
|
-
const existing = await this.executeQuery(collectionName, { id: modified.id }, { limit: 1 });
|
|
509
|
-
if (existing.length > 0)
|
|
510
|
-
throw new Error(`Item with id ${String(modified.id)} already exists`);
|
|
511
|
-
}
|
|
512
|
-
await storage.replace([modified]);
|
|
513
|
-
await this.checkQueryUpdates(collectionName, [modified]);
|
|
514
|
-
return [modified];
|
|
515
|
-
}
|
|
516
|
-
async removeOne(collectionName, selector) {
|
|
517
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
518
|
-
if (!storage)
|
|
519
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
520
|
-
const items = await this.executeQuery(collectionName, selector, { limit: 1 });
|
|
521
|
-
const [item] = items;
|
|
522
|
-
if (item == null)
|
|
523
|
-
return [];
|
|
524
|
-
await storage.remove([item]);
|
|
525
|
-
await this.checkQueryUpdates(collectionName, [item]);
|
|
526
|
-
return [item];
|
|
527
|
-
}
|
|
528
|
-
async removeMany(collectionName, selector) {
|
|
529
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
530
|
-
if (!storage)
|
|
531
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
532
|
-
const items = await this.executeQuery(collectionName, selector);
|
|
533
|
-
if (items.length === 0)
|
|
534
|
-
return [];
|
|
535
|
-
await storage.remove(items);
|
|
536
|
-
await this.checkQueryUpdates(collectionName, items);
|
|
537
|
-
return items;
|
|
538
|
-
}
|
|
539
|
-
// ===== Upsert/merge helper for ingesting fetched items =====
|
|
540
|
-
async upsertMerged(collectionName, incoming) {
|
|
541
|
-
if (incoming.length === 0)
|
|
542
|
-
return;
|
|
543
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
544
|
-
if (!storage)
|
|
545
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
546
|
-
const ids = incoming.map((i) => i.id);
|
|
547
|
-
const existing = await storage.readIds(ids);
|
|
548
|
-
const existingById = new Map(existing.map((it) => [it.id, it]));
|
|
549
|
-
const toInsert = [];
|
|
550
|
-
const toReplace = [];
|
|
551
|
-
const referenceMap = this.idRefCounts.get(collectionName);
|
|
552
|
-
const autoload = this.autoloadIds.get(collectionName);
|
|
553
|
-
for (const item of incoming) {
|
|
554
|
-
const previous = existingById.get(item.id);
|
|
555
|
-
if (previous) {
|
|
556
|
-
toReplace.push(this.mergeItems(previous, item));
|
|
557
|
-
} else {
|
|
558
|
-
toInsert.push(item);
|
|
559
|
-
}
|
|
560
|
-
autoload?.add(item.id);
|
|
561
|
-
referenceMap?.set(item.id, (referenceMap?.get(item.id) ?? 0) + 1);
|
|
562
|
-
}
|
|
563
|
-
if (toInsert.length > 0)
|
|
564
|
-
await storage.insert(toInsert);
|
|
565
|
-
if (toReplace.length > 0)
|
|
566
|
-
await storage.replace(toReplace);
|
|
567
|
-
}
|
|
40
|
+
/**
|
|
41
|
+
* Optimizes a logic gate ($and/$or) of selectors by querying multiple index providers.
|
|
42
|
+
* @param queryFunctions - An array of index providers to query.
|
|
43
|
+
* @param logicGate - An array of selectors representing the logic gate.
|
|
44
|
+
* @param idsCallback - A callback function called with matched ids.
|
|
45
|
+
* @returns An array of optimized selectors or a promise that resolves to such an array.
|
|
46
|
+
*/
|
|
47
|
+
function optimizeLogicGate(queryFunctions, logicGate, idsCallback) {
|
|
48
|
+
return logicGate.reduce((memoOrPromise, sel) => {
|
|
49
|
+
const getSelector = (indexInfo) => {
|
|
50
|
+
const { matched: selMatched, ids: selIds, optimizedSelector } = indexInfo;
|
|
51
|
+
if (selMatched) {
|
|
52
|
+
idsCallback(true, selIds);
|
|
53
|
+
if (Object.keys(optimizedSelector).length > 0) return optimizedSelector;
|
|
54
|
+
} else {
|
|
55
|
+
idsCallback(false, []);
|
|
56
|
+
return sel;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
const indexInfoOrPromise = getIndexInfo(queryFunctions, sel);
|
|
60
|
+
if (indexInfoOrPromise instanceof Promise) return indexInfoOrPromise.then(async (indexInfo) => {
|
|
61
|
+
const memo = memoOrPromise instanceof Promise ? await memoOrPromise : memoOrPromise;
|
|
62
|
+
const optimizedSelector = getSelector(indexInfo);
|
|
63
|
+
if (optimizedSelector) memo.push(optimizedSelector);
|
|
64
|
+
return memo;
|
|
65
|
+
});
|
|
66
|
+
const memo = memoOrPromise;
|
|
67
|
+
if (memo instanceof Promise) throw new Error("Mixing async and sync index providers is not supported");
|
|
68
|
+
const optimizedSelector = getSelector(indexInfoOrPromise);
|
|
69
|
+
if (optimizedSelector) memo.push(optimizedSelector);
|
|
70
|
+
return memo;
|
|
71
|
+
}, []);
|
|
568
72
|
}
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
73
|
+
/**
|
|
74
|
+
* Retrieves index information for a given complex selector by querying multiple
|
|
75
|
+
* index providers. Handles nested `$and` and `$or` conditions in the selector and
|
|
76
|
+
* optimizes the selector to minimize processing overhead.
|
|
77
|
+
* @template T - The type of the items in the collection.
|
|
78
|
+
* @template I - The type of the unique identifier for the items.
|
|
79
|
+
* @param queryFunctions - An array of index providers to query.
|
|
80
|
+
* @param selector - The complex selector used to filter items.
|
|
81
|
+
* @returns An object containing:
|
|
82
|
+
* - `matched`: A boolean indicating if the selector matched any items.
|
|
83
|
+
* - `ids`: An array of matched item ids.
|
|
84
|
+
* - `optimizedSelector`: A selector optimized based on the index results, with unused
|
|
85
|
+
* conditions removed.
|
|
86
|
+
*/
|
|
87
|
+
function getIndexInfo(queryFunctions, selector) {
|
|
88
|
+
if (selector == null || Object.keys(selector).length <= 0) return {
|
|
89
|
+
matched: false,
|
|
90
|
+
ids: [],
|
|
91
|
+
optimizedSelector: selector
|
|
92
|
+
};
|
|
93
|
+
const { $and, $or, ...rest } = selector;
|
|
94
|
+
const flatInfoOrPromise = getMergedIndexInfo(queryFunctions, rest);
|
|
95
|
+
const processFlatInfo = (flatInfo) => {
|
|
96
|
+
let { matched, ids } = flatInfo;
|
|
97
|
+
const newSelector = flatInfo.optimizedSelector;
|
|
98
|
+
const $andNewOrPromise = Array.isArray($and) ? optimizeLogicGate(queryFunctions, $and, (match, selIds) => {
|
|
99
|
+
if (!match) return;
|
|
100
|
+
ids = matched ? intersection(ids, selIds) : selIds;
|
|
101
|
+
matched = true;
|
|
102
|
+
}) : void 0;
|
|
103
|
+
const process$and = ($andNew) => {
|
|
104
|
+
if ($andNew && $andNew.length > 0) newSelector.$and = $andNew;
|
|
105
|
+
let hasNonIndexField = false;
|
|
106
|
+
const matchedBefore = matched;
|
|
107
|
+
const idsBefore = ids;
|
|
108
|
+
const process$or = ($orNew) => {
|
|
109
|
+
if (hasNonIndexField || $orNew && $orNew.length > 0) {
|
|
110
|
+
newSelector.$or = $or;
|
|
111
|
+
matched = matchedBefore;
|
|
112
|
+
ids = idsBefore;
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
matched,
|
|
116
|
+
ids: ids || [],
|
|
117
|
+
optimizedSelector: newSelector
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
const $orNewOrPromise = Array.isArray($or) ? optimizeLogicGate(queryFunctions, $or, (match, selIds) => {
|
|
121
|
+
if (match) {
|
|
122
|
+
ids = [...new Set([...ids, ...selIds])];
|
|
123
|
+
matched = true;
|
|
124
|
+
} else hasNonIndexField = true;
|
|
125
|
+
}) : void 0;
|
|
126
|
+
if ($orNewOrPromise instanceof Promise) return $orNewOrPromise.then(($orNew) => process$or($orNew));
|
|
127
|
+
return process$or($orNewOrPromise);
|
|
128
|
+
};
|
|
129
|
+
if ($andNewOrPromise instanceof Promise) return $andNewOrPromise.then(($andNew) => process$and($andNew));
|
|
130
|
+
return process$and($andNewOrPromise);
|
|
131
|
+
};
|
|
132
|
+
if (flatInfoOrPromise instanceof Promise) return flatInfoOrPromise.then(processFlatInfo);
|
|
133
|
+
return processFlatInfo(flatInfoOrPromise);
|
|
134
|
+
}
|
|
135
|
+
//#endregion
|
|
136
|
+
export { getIndexInfo as default };
|