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