@signaldb/core 2.0.0-beta.6 → 2.0.0-beta.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/.vite/manifest.json +118 -84
- package/dist/AsyncDataAdapter.d.ts +1 -1
- package/dist/index.cjs.js +33 -34
- package/dist/index.mjs +17 -35
- package/dist/index10.cjs.js +23 -0
- package/dist/index10.mjs +21 -17
- package/dist/index11.cjs.js +45 -0
- package/dist/index11.mjs +44 -15
- package/dist/index12.cjs.js +22 -0
- package/dist/index12.mjs +21 -47
- package/dist/index13.cjs.js +45 -0
- package/dist/index13.mjs +44 -365
- package/dist/index14.cjs.js +86 -0
- package/dist/index14.mjs +85 -416
- package/dist/index15.cjs.js +15 -0
- package/dist/index15.mjs +14 -206
- package/dist/index16.cjs.js +136 -0
- package/dist/index16.mjs +135 -388
- package/dist/index17.cjs.js +47 -0
- package/dist/index17.mjs +45 -578
- package/dist/index18.cjs.js +15 -0
- package/dist/index18.mjs +14 -157
- package/dist/index19.cjs.js +23 -0
- package/dist/index19.mjs +22 -26
- package/dist/index2.cjs.js +33 -0
- package/dist/index2.mjs +32 -258
- package/dist/index20.cjs.js +37 -0
- package/dist/index20.mjs +36 -41
- package/dist/index21.cjs.js +35 -0
- package/dist/index21.mjs +34 -16
- package/dist/index22.cjs.js +25 -0
- package/dist/index22.mjs +24 -104
- package/dist/index23.cjs.js +19 -0
- package/dist/index23.mjs +18 -128
- package/dist/index24.cjs.js +342 -0
- package/dist/index24.mjs +340 -6
- package/dist/index25.cjs.js +519 -0
- package/dist/index25.mjs +518 -27
- package/dist/index26.cjs.js +11 -0
- package/dist/index26.mjs +10 -10
- package/dist/index27.cjs.js +11 -0
- package/dist/index27.mjs +10 -29
- package/dist/index28.cjs.js +90 -0
- package/dist/index28.mjs +88 -46
- package/dist/index29.cjs.js +370 -0
- package/dist/index29.mjs +369 -8
- package/dist/index3.cjs.js +19 -0
- package/dist/index3.mjs +18 -539
- package/dist/index30.cjs.js +72 -0
- package/dist/index30.mjs +71 -9
- package/dist/index31.cjs.js +191 -0
- package/dist/index31.mjs +190 -7
- package/dist/index32.cjs.js +21 -0
- package/dist/index32.mjs +20 -42
- package/dist/index33.cjs.js +336 -0
- package/dist/index33.mjs +335 -29
- package/dist/index34.cjs.js +539 -0
- package/dist/index34.mjs +537 -4
- package/dist/index4.cjs.js +150 -0
- package/dist/index4.mjs +149 -5
- package/dist/index5.cjs.js +254 -0
- package/dist/index5.mjs +252 -4
- package/dist/index6.cjs.js +125 -0
- package/dist/index6.mjs +124 -29
- package/dist/index7.cjs.js +33 -0
- package/dist/index7.mjs +32 -12
- package/dist/index8.cjs.js +12 -0
- package/dist/index8.mjs +10 -4
- package/dist/index9.cjs.js +11 -0
- package/dist/index9.mjs +10 -133
- package/package.json +1 -1
- package/dist/index.cjs10.js +0 -18
- package/dist/index.cjs11.js +0 -15
- package/dist/index.cjs12.js +0 -48
- package/dist/index.cjs13.js +0 -365
- package/dist/index.cjs14.js +0 -416
- package/dist/index.cjs15.js +0 -206
- package/dist/index.cjs16.js +0 -388
- package/dist/index.cjs17.js +0 -579
- package/dist/index.cjs18.js +0 -157
- package/dist/index.cjs19.js +0 -26
- package/dist/index.cjs2.js +0 -259
- package/dist/index.cjs20.js +0 -42
- package/dist/index.cjs21.js +0 -16
- package/dist/index.cjs22.js +0 -104
- package/dist/index.cjs23.js +0 -129
- package/dist/index.cjs24.js +0 -7
- package/dist/index.cjs25.js +0 -27
- package/dist/index.cjs26.js +0 -10
- package/dist/index.cjs27.js +0 -29
- package/dist/index.cjs28.js +0 -46
- package/dist/index.cjs29.js +0 -8
- package/dist/index.cjs3.js +0 -540
- package/dist/index.cjs30.js +0 -9
- package/dist/index.cjs31.js +0 -7
- package/dist/index.cjs32.js +0 -42
- package/dist/index.cjs33.js +0 -29
- package/dist/index.cjs34.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/index17.mjs
CHANGED
|
@@ -1,580 +1,47 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
function
|
|
11
|
-
|
|
1
|
+
//#region src/utils/deepClone.ts
|
|
2
|
+
/**
|
|
3
|
+
* Performs a deep clone of a value, supporting various types including arrays, objects,
|
|
4
|
+
* Maps, Sets, Dates, and RegExps. Functions are not supported and will throw an error.
|
|
5
|
+
* @template T - The type of the value to clone.
|
|
6
|
+
* @param value - The value to deep clone.
|
|
7
|
+
* @returns A deep copy of the provided value.
|
|
8
|
+
* @throws {Error} An error if the value is a function, as cloning functions is not supported.
|
|
9
|
+
*/
|
|
10
|
+
function clone(value) {
|
|
11
|
+
if (typeof value === "function") throw new Error("Cloning functions is not supported");
|
|
12
|
+
if (value === null || typeof value !== "object") return value;
|
|
13
|
+
if (value instanceof Date) return new Date(value);
|
|
14
|
+
if (Array.isArray(value)) return value.map((item) => clone(item));
|
|
15
|
+
if (value instanceof Map) {
|
|
16
|
+
const result = /* @__PURE__ */ new Map();
|
|
17
|
+
value.forEach((currentValue, key) => {
|
|
18
|
+
result.set(key, clone(currentValue));
|
|
19
|
+
});
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
if (value instanceof Set) {
|
|
23
|
+
const result = /* @__PURE__ */ new Set();
|
|
24
|
+
value.forEach((currentValue) => {
|
|
25
|
+
result.add(clone(currentValue));
|
|
26
|
+
});
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
if (value instanceof RegExp) return new RegExp(value);
|
|
30
|
+
const result = {};
|
|
31
|
+
for (const key in value) if (Object.hasOwnProperty.call(value, key)) result[key] = clone(value[key]);
|
|
32
|
+
return result;
|
|
12
33
|
}
|
|
13
|
-
|
|
14
|
-
|
|
34
|
+
/**
|
|
35
|
+
* Creates a deep clone of an object. Uses the `structuredClone` function if available,
|
|
36
|
+
* otherwise falls back to a manual deep clone implementation.
|
|
37
|
+
* @template T - The type of the object to clone.
|
|
38
|
+
* @param object - The object to deep clone.
|
|
39
|
+
* @returns A deep copy of the provided object.
|
|
40
|
+
*/
|
|
41
|
+
function deepClone(object) {
|
|
42
|
+
if (typeof structuredClone === "function") return structuredClone(object);
|
|
43
|
+
/* istanbul ignore next -- @preserve */
|
|
44
|
+
return clone(object);
|
|
15
45
|
}
|
|
16
|
-
|
|
17
|
-
|
|
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 storage.setup();
|
|
285
|
-
await Promise.all(indices.map((field) => storage.createIndex(field)));
|
|
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
|
-
};
|
|
46
|
+
//#endregion
|
|
47
|
+
export { deepClone as default };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
let mingo = require("mingo");
|
|
2
|
+
//#region src/utils/match.ts
|
|
3
|
+
/**
|
|
4
|
+
* Tests whether a given item matches a specified selector.
|
|
5
|
+
* Uses the `mingo` library to evaluate the query.
|
|
6
|
+
* @template T - The type of the item being tested.
|
|
7
|
+
* @param item - The item to test against the selector.
|
|
8
|
+
* @param selector - The query selector used to match the item.
|
|
9
|
+
* @returns A boolean indicating whether the item matches the selector.
|
|
10
|
+
*/
|
|
11
|
+
function match(item, selector) {
|
|
12
|
+
return new mingo.Query(selector).test(item);
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
exports.default = match;
|