@signaldb/core 2.0.0-beta.21 → 2.0.0-beta.22
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/README.md +3 -3
- package/dist/.vite/manifest.json +61 -59
- package/dist/AsyncDataAdapter.d.ts +15 -2
- package/dist/AutoFetchDataAdapter.d.ts +13 -2
- package/dist/Collection/Observer.d.ts +1 -1
- package/dist/Collection/index.d.ts +11 -12
- package/dist/DataAdapter.d.ts +19 -3
- package/dist/WorkerDataAdapterHost.d.ts +19 -6
- package/dist/index.cjs.js +4 -4
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +4 -4
- package/dist/index12.cjs.js +1 -1
- package/dist/index12.mjs +1 -1
- package/dist/index15.cjs.js +6 -2
- package/dist/index15.mjs +6 -2
- package/dist/index17.cjs.js +4 -2
- package/dist/index17.mjs +4 -3
- package/dist/index18.cjs.js +1 -0
- package/dist/index18.mjs +1 -1
- package/dist/index28.cjs.js +15 -5
- package/dist/index28.mjs +15 -5
- package/dist/index29.cjs.js +29 -16
- package/dist/index29.mjs +29 -16
- package/dist/index33.cjs.js +17 -49
- package/dist/index33.mjs +17 -49
- package/dist/index34.cjs.js +45 -445
- package/dist/index34.mjs +45 -445
- package/dist/index35.cjs.js +132 -65
- package/dist/index35.mjs +132 -65
- package/dist/index36.cjs.js +385 -531
- package/dist/index36.mjs +386 -533
- package/dist/index37.cjs.js +68 -17
- package/dist/index37.mjs +68 -17
- package/dist/index38.cjs.js +536 -319
- package/dist/index38.mjs +538 -321
- package/dist/index39.cjs.js +301 -466
- package/dist/index39.mjs +301 -466
- package/dist/index40.cjs.js +484 -0
- package/dist/index40.mjs +484 -0
- package/dist/index5.cjs.js +3 -3
- package/dist/index5.mjs +3 -3
- package/dist/index6.cjs.js +1 -0
- package/dist/index6.mjs +1 -1
- package/dist/types/StorageAdapter.d.ts +80 -0
- package/dist/utils/executeStorageQuery.d.ts +28 -0
- package/package.json +1 -1
package/dist/index34.cjs.js
CHANGED
|
@@ -1,453 +1,53 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
const require_getIndexInfo = require("./index17.cjs.js");
|
|
4
|
-
const require_deepClone = require("./index18.cjs.js");
|
|
5
|
-
const require_match = require("./index19.cjs.js");
|
|
6
|
-
const require_modify = require("./index20.cjs.js");
|
|
7
|
-
const require_projectItems = require("./index23.cjs.js");
|
|
8
|
-
const require_sortItems = require("./index24.cjs.js");
|
|
9
|
-
const require_incrementalQueryUpdate = require("./index25.cjs.js");
|
|
10
|
-
const require_queryId = require("./index26.cjs.js");
|
|
11
|
-
const require_idIndexQuery = require("./index27.cjs.js");
|
|
12
|
-
const require_storageIndexQuery = require("./index33.cjs.js");
|
|
13
|
-
//#region src/AsyncDataAdapter.ts
|
|
1
|
+
const require_getMatchingKeys = require("./index14.cjs.js");
|
|
2
|
+
//#region src/utils/storageIndexQuery.ts
|
|
14
3
|
/**
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* the default `console.error` hook close to useless.
|
|
18
|
-
*/
|
|
19
|
-
var QueryError = class extends Error {
|
|
20
|
-
collectionName;
|
|
21
|
-
selector;
|
|
22
|
-
options;
|
|
23
|
-
attempts;
|
|
24
|
-
constructor(collectionName, selector, options, attempts, cause) {
|
|
25
|
-
const reason = cause instanceof Error ? cause.message : String(cause);
|
|
26
|
-
super(`Query on "${collectionName}" failed after ${attempts} attempt(s): ${reason}`);
|
|
27
|
-
this.name = "QueryError";
|
|
28
|
-
this.collectionName = collectionName;
|
|
29
|
-
this.selector = selector;
|
|
30
|
-
this.options = options;
|
|
31
|
-
this.attempts = attempts;
|
|
32
|
-
this.cause = cause;
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
/**
|
|
36
|
-
* Turns the item states a write produced into the upsert/delete split a query update needs.
|
|
4
|
+
* Builds the index provider a data adapter uses to narrow a selector down through
|
|
5
|
+
* a storage adapter's index.
|
|
37
6
|
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
* @
|
|
45
|
-
* @
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
return {
|
|
50
|
-
upserts: modifiedItems,
|
|
51
|
-
deletes: previousItems.map((item) => item.id).filter((id) => !modifiedIds.has(id))
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
var DEFAULT_RETRY_ATTEMPTS = 3;
|
|
55
|
-
var defaultRetryDelay = (attempt) => 100 * 4 ** (attempt - 1);
|
|
56
|
-
var wait = (ms) => new Promise((resolve) => {
|
|
57
|
-
setTimeout(resolve, ms);
|
|
58
|
-
});
|
|
59
|
-
/**
|
|
60
|
-
* AsyncDataAdapter
|
|
61
|
-
* Combines WorkerDataAdapter + WorkerDataAdapterHost into a single, transport-free adapter.
|
|
62
|
-
* - Keeps the DataAdapter/CollectionBackend surface identical to the Worker version.
|
|
63
|
-
* - Executes queries and mutations directly against the provided StorageAdapter.
|
|
64
|
-
* - Preserves index-aware query optimization and push-style query updates to listeners.
|
|
7
|
+
* Every adapter that keeps its data in a `StorageAdapter` needs exactly this, and
|
|
8
|
+
* each of them used to carry its own copy — three transcriptions of one set of
|
|
9
|
+
* rules about null, `$exists`, inclusion and exclusion, which is how they drift
|
|
10
|
+
* apart without anyone noticing. The index is keyed by `serializeValue(value)`,
|
|
11
|
+
* which is what `getMatchingKeys` produces, so the two only agree while they stay
|
|
12
|
+
* in one place.
|
|
13
|
+
* @template T - The type of the items in the collection.
|
|
14
|
+
* @template I - The type of the unique identifier for the items.
|
|
15
|
+
* @param storage - The storage adapter holding the index.
|
|
16
|
+
* @param field - The indexed field this provider answers for.
|
|
17
|
+
* @returns A query function for `getIndexInfo`.
|
|
65
18
|
*/
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
this.retryAttempts = Math.max(1, options.retry?.attempts ?? DEFAULT_RETRY_ATTEMPTS);
|
|
83
|
-
this.retryDelay = options.retry?.delay ?? defaultRetryDelay;
|
|
84
|
-
}
|
|
85
|
-
createCollectionBackend(collection, indices) {
|
|
86
|
-
this.collectionIndices.set(collection.name, indices);
|
|
87
|
-
this.queries.set(collection.name, /* @__PURE__ */ new Map());
|
|
88
|
-
this.ensureStorageAdapter(collection.name);
|
|
89
|
-
const ready = (async () => {
|
|
90
|
-
try {
|
|
91
|
-
await this.setupStorage(collection.name, indices);
|
|
92
|
-
} catch (error) {
|
|
93
|
-
this.onError(error);
|
|
94
|
-
throw error;
|
|
95
|
-
}
|
|
96
|
-
})();
|
|
97
|
-
this.storageAdapterReady.set(collection.name, ready);
|
|
98
|
-
const registerQuery = (selector, options) => {
|
|
99
|
-
const qid = require_queryId.default(selector, options);
|
|
100
|
-
const registry = this.queries.get(collection.name);
|
|
101
|
-
if (!registry) throw new Error(`Collection ${collection.name} not initialized!`);
|
|
102
|
-
registry.set(qid, {
|
|
103
|
-
selector,
|
|
104
|
-
options,
|
|
105
|
-
items: [],
|
|
106
|
-
listeners: /* @__PURE__ */ new Set(),
|
|
107
|
-
...registry.get(qid),
|
|
108
|
-
state: "active",
|
|
109
|
-
error: null
|
|
110
|
-
});
|
|
111
|
-
this.fulfillQuery(collection.name, selector, options).catch(this.onError);
|
|
112
|
-
};
|
|
113
|
-
const unregisterQuery = (selector, options) => {
|
|
114
|
-
const qid = require_queryId.default(selector, options);
|
|
115
|
-
this.queries.get(collection.name)?.delete(qid);
|
|
116
|
-
};
|
|
117
|
-
const getQueryState = (selector, options) => {
|
|
118
|
-
return (this.queries.get(collection.name)?.get(require_queryId.default(selector, options)))?.state ?? "active";
|
|
119
|
-
};
|
|
120
|
-
const getQueryError = (selector, options) => {
|
|
121
|
-
return (this.queries.get(collection.name)?.get(require_queryId.default(selector, options)))?.error ?? null;
|
|
122
|
-
};
|
|
123
|
-
const getQueryResult = (selector, options) => {
|
|
124
|
-
return (this.queries.get(collection.name)?.get(require_queryId.default(selector, options)))?.items ?? [];
|
|
125
|
-
};
|
|
126
|
-
const retryQuery = (selector, options) => {
|
|
127
|
-
const qid = require_queryId.default(selector, options);
|
|
128
|
-
if (!this.queries.get(collection.name)?.get(qid)) return;
|
|
129
|
-
this.publishState(collection.name, qid, "active", null);
|
|
130
|
-
this.runQuery(collection.name, selector, options);
|
|
131
|
-
};
|
|
132
|
-
const onQueryStateChange = (selector, options, callback) => {
|
|
133
|
-
const qid = require_queryId.default(selector, options);
|
|
134
|
-
const registry = this.queries.get(collection.name);
|
|
135
|
-
if (!registry) throw new Error(`Collection ${collection.name} not initialized!`);
|
|
136
|
-
if (!registry.has(qid)) registry.set(qid, {
|
|
137
|
-
selector,
|
|
138
|
-
options,
|
|
139
|
-
state: "active",
|
|
140
|
-
error: null,
|
|
141
|
-
items: [],
|
|
142
|
-
listeners: /* @__PURE__ */ new Set()
|
|
143
|
-
});
|
|
144
|
-
registry.get(qid)?.listeners.add(callback);
|
|
145
|
-
if (registry.get(qid)?.state === "error") retryQuery(selector, options);
|
|
146
|
-
return () => registry.get(qid)?.listeners.delete(callback);
|
|
147
|
-
};
|
|
148
|
-
return {
|
|
149
|
-
insert: async (item) => {
|
|
150
|
-
await ready;
|
|
151
|
-
return await this.insert(collection.name, item);
|
|
152
|
-
},
|
|
153
|
-
updateOne: async (selector, modifier) => {
|
|
154
|
-
await ready;
|
|
155
|
-
return this.updateOne(collection.name, selector, modifier);
|
|
156
|
-
},
|
|
157
|
-
updateMany: async (selector, modifier) => {
|
|
158
|
-
await ready;
|
|
159
|
-
return this.updateMany(collection.name, selector, modifier);
|
|
160
|
-
},
|
|
161
|
-
replaceOne: async (selector, replacement) => {
|
|
162
|
-
await ready;
|
|
163
|
-
return this.replaceOne(collection.name, selector, replacement);
|
|
164
|
-
},
|
|
165
|
-
removeOne: async (selector) => {
|
|
166
|
-
await ready;
|
|
167
|
-
return this.removeOne(collection.name, selector);
|
|
168
|
-
},
|
|
169
|
-
removeMany: async (selector) => {
|
|
170
|
-
await ready;
|
|
171
|
-
return this.removeMany(collection.name, selector);
|
|
172
|
-
},
|
|
173
|
-
registerQuery,
|
|
174
|
-
unregisterQuery,
|
|
175
|
-
retryQuery,
|
|
176
|
-
getQueryState,
|
|
177
|
-
getQueryError,
|
|
178
|
-
getQueryResult,
|
|
179
|
-
onQueryStateChange,
|
|
180
|
-
executeQuery: async (selector, options) => {
|
|
181
|
-
await ready;
|
|
182
|
-
return this.executeQuery(collection.name, selector, options);
|
|
183
|
-
},
|
|
184
|
-
dispose: async () => {
|
|
185
|
-
this.storageAdapters.delete(collection.name);
|
|
186
|
-
this.queries.delete(collection.name);
|
|
187
|
-
this.collectionIndices.delete(collection.name);
|
|
188
|
-
this.storageAdapterReady.delete(collection.name);
|
|
189
|
-
},
|
|
190
|
-
isReady: async () => {
|
|
191
|
-
await ready;
|
|
192
|
-
}
|
|
193
|
-
};
|
|
194
|
-
}
|
|
195
|
-
async setupStorage(collectionName, indices) {
|
|
196
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
197
|
-
if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
198
|
-
await storage.setup();
|
|
199
|
-
await Promise.all(indices.map((index) => storage.createIndex(index)));
|
|
200
|
-
}
|
|
201
|
-
ensureStorageAdapter(name) {
|
|
202
|
-
if (this.storageAdapters.has(name)) return;
|
|
203
|
-
const adapter = this.options.storage(name);
|
|
204
|
-
if (!adapter) return;
|
|
205
|
-
this.storageAdapters.set(name, adapter);
|
|
206
|
-
}
|
|
207
|
-
/**
|
|
208
|
-
* Compute and publish the result for a specific query
|
|
209
|
-
* @param collectionName - name of the collection
|
|
210
|
-
* @param selector - query selector
|
|
211
|
-
* @param options - query options
|
|
212
|
-
*/
|
|
213
|
-
async fulfillQuery(collectionName, selector, options) {
|
|
214
|
-
const qid = require_queryId.default(selector, options);
|
|
215
|
-
const registry = this.queries.get(collectionName);
|
|
216
|
-
if (!registry) throw new Error(`Collection ${collectionName} not initialized!`);
|
|
217
|
-
if (!registry.get(qid)) return;
|
|
218
|
-
this.publishState(collectionName, qid, "active", null);
|
|
219
|
-
await this.runQuery(collectionName, selector, options);
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* Executes a query, retrying transient failures before giving up. The state
|
|
223
|
-
* stays `'active'` across retries — consumers should see "still loading",
|
|
224
|
-
* not "failed", until we actually stop trying. Only the final failure is
|
|
225
|
-
* published as `'error'` and reported through `onError`; previously that
|
|
226
|
-
* error was swallowed entirely (`fulfillQuery` caught it internally, so the
|
|
227
|
-
* `.catch(this.onError)` on its call site was unreachable) and the query
|
|
228
|
-
* stayed dead for the rest of the session.
|
|
229
|
-
* @param collectionName - name of the collection
|
|
230
|
-
* @param selector - query selector
|
|
231
|
-
* @param options - query options
|
|
232
|
-
*/
|
|
233
|
-
async runQuery(collectionName, selector, options) {
|
|
234
|
-
const qid = require_queryId.default(selector, options);
|
|
235
|
-
let lastError;
|
|
236
|
-
for (let attempt = 1; attempt <= this.retryAttempts; attempt += 1) {
|
|
237
|
-
if (!this.queries.get(collectionName)?.has(qid)) return;
|
|
238
|
-
try {
|
|
239
|
-
const items = await this.executeQuery(collectionName, selector, options);
|
|
240
|
-
const rec = this.queries.get(collectionName)?.get(qid);
|
|
241
|
-
const delta = rec?.answered ? require_queryDelta.diffQueryResults(rec.items, items) : void 0;
|
|
242
|
-
this.publishResult(collectionName, qid, items);
|
|
243
|
-
this.publishState(collectionName, qid, "complete", null, delta);
|
|
244
|
-
return;
|
|
245
|
-
} catch (error) {
|
|
246
|
-
lastError = error;
|
|
247
|
-
if (attempt < this.retryAttempts) await wait(this.retryDelay(attempt));
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
const queryError = new QueryError(collectionName, selector, options, this.retryAttempts, lastError);
|
|
251
|
-
this.publishState(collectionName, qid, "error", queryError);
|
|
252
|
-
this.onError(queryError);
|
|
253
|
-
}
|
|
254
|
-
/**
|
|
255
|
-
* Notify listeners about state changes and keep the cache updated
|
|
256
|
-
* @param collectionName - name of the collection
|
|
257
|
-
* @param qid - query id
|
|
258
|
-
* @param state - new state
|
|
259
|
-
* @param error - error if state is 'error', null otherwise
|
|
260
|
-
* @param delta - what changed about the result, when that is known
|
|
261
|
-
*/
|
|
262
|
-
publishState(collectionName, qid, state, error, delta) {
|
|
263
|
-
const rec = this.queries.get(collectionName)?.get(qid);
|
|
264
|
-
if (!rec) return;
|
|
265
|
-
rec.state = state;
|
|
266
|
-
rec.error = error;
|
|
267
|
-
const subscribers = [...rec.listeners];
|
|
268
|
-
for (const callback of subscribers) try {
|
|
269
|
-
require_queryDelta.callWithDelta(callback, state, delta);
|
|
270
|
-
} catch (error_) {
|
|
271
|
-
this.onError(error_);
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
publishResult(collectionName, qid, items) {
|
|
275
|
-
const rec = this.queries.get(collectionName)?.get(qid);
|
|
276
|
-
if (!rec) return;
|
|
277
|
-
rec.items = items;
|
|
278
|
-
rec.itemIds = void 0;
|
|
279
|
-
rec.answered = true;
|
|
280
|
-
}
|
|
281
|
-
queryItemIds(rec) {
|
|
282
|
-
if (!rec.itemIds) rec.itemIds = new Set(rec.items.map((item) => item.id));
|
|
283
|
-
return rec.itemIds;
|
|
284
|
-
}
|
|
285
|
-
async getIndexInfo(collectionName, selector) {
|
|
286
|
-
const storageAdapter = this.storageAdapters.get(collectionName);
|
|
287
|
-
if (!storageAdapter) throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
288
|
-
if (selector != null && Object.keys(selector).length === 1 && "id" in selector) {
|
|
289
|
-
const idResult = require_idIndexQuery.default(selector);
|
|
290
|
-
if (idResult.matched) return {
|
|
291
|
-
matched: true,
|
|
292
|
-
ids: idResult.ids.filter(Boolean),
|
|
293
|
-
optimizedSelector: {}
|
|
294
|
-
};
|
|
295
|
-
}
|
|
296
|
-
if (selector == null) return {
|
|
297
|
-
matched: false,
|
|
298
|
-
ids: [],
|
|
299
|
-
optimizedSelector: {}
|
|
300
|
-
};
|
|
301
|
-
return require_getIndexInfo.default((this.collectionIndices.get(collectionName) ?? []).map((field) => require_storageIndexQuery.default(storageAdapter, field)), selector);
|
|
302
|
-
}
|
|
303
|
-
async queryItems(collectionName, selector) {
|
|
304
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
305
|
-
if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
306
|
-
const index = await this.getIndexInfo(collectionName, selector);
|
|
307
|
-
const matchItems = (item) => {
|
|
308
|
-
if (index.optimizedSelector == null) return true;
|
|
309
|
-
if (Object.keys(index.optimizedSelector).length <= 0) return true;
|
|
310
|
-
return require_match.default(item, index.optimizedSelector);
|
|
311
|
-
};
|
|
312
|
-
if (index.matched) {
|
|
313
|
-
const items = await storage.readIds(index.ids);
|
|
314
|
-
if (require_isEqual.default(index.optimizedSelector, {})) return items;
|
|
315
|
-
return items.filter(matchItems);
|
|
316
|
-
} else {
|
|
317
|
-
const allItems = await storage.readAll();
|
|
318
|
-
if (require_isEqual.default(selector, {})) return allItems;
|
|
319
|
-
return allItems.filter(matchItems);
|
|
19
|
+
function storageIndexQuery(storage, field) {
|
|
20
|
+
return async (flatSelector) => {
|
|
21
|
+
if (!Object.hasOwnProperty.call(flatSelector, field)) return { matched: false };
|
|
22
|
+
const index = await storage.readIndex(field);
|
|
23
|
+
const fieldSelector = flatSelector[field];
|
|
24
|
+
const filtersForNull = fieldSelector == null || fieldSelector.$exists === false;
|
|
25
|
+
const keys = filtersForNull ? {
|
|
26
|
+
include: null,
|
|
27
|
+
exclude: [...index.keys()].filter((key) => key != null)
|
|
28
|
+
} : require_getMatchingKeys.default(field, flatSelector);
|
|
29
|
+
if (keys.include == null && keys.exclude == null) return { matched: false };
|
|
30
|
+
let includedIds = [];
|
|
31
|
+
if (keys.include == null) for (const set of index.values()) for (const id of set) includedIds.push(id);
|
|
32
|
+
else for (const key of keys.include) {
|
|
33
|
+
const idSet = index.get(key);
|
|
34
|
+
if (idSet) for (const id of idSet) includedIds.push(id);
|
|
320
35
|
}
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
const skipped = skip ? sorted.slice(skip) : sorted;
|
|
327
|
-
return require_projectItems.default(limit ? skipped.slice(0, limit) : skipped, fields);
|
|
328
|
-
}
|
|
329
|
-
/**
|
|
330
|
-
* After mutations, bring every affected active query up to date.
|
|
331
|
-
*
|
|
332
|
-
* A query whose previous result is enough to answer the change is brought up to date from that
|
|
333
|
-
* result alone — no round trip to the storage, and no detour through `'active'`, because there is
|
|
334
|
-
* no window in which the query is stale. Only a query the change cannot be reasoned about
|
|
335
|
-
* locally — a window onto a larger set, or one that has never been answered — goes back to the
|
|
336
|
-
* store, and that one gets the same retry and reporting behaviour a freshly registered query
|
|
337
|
-
* gets: a refresh that fails silently leaves exactly the same dead cursor.
|
|
338
|
-
* @param collectionName - name of the collection
|
|
339
|
-
* @param changes - the items the write created, updated or removed
|
|
340
|
-
*/
|
|
341
|
-
async checkQueryUpdates(collectionName, changes) {
|
|
342
|
-
const registry = this.queries.get(collectionName);
|
|
343
|
-
if (!registry) throw new Error(`Collection ${collectionName} not initialized!`);
|
|
344
|
-
if (registry.size === 0) return;
|
|
345
|
-
if (changes.upserts.length === 0 && changes.deletes.length === 0) return;
|
|
346
|
-
const affected = [...registry.values()].filter((rec) => {
|
|
347
|
-
const ids = this.queryItemIds(rec);
|
|
348
|
-
if (changes.deletes.some((id) => ids.has(id))) return true;
|
|
349
|
-
return changes.upserts.some((item) => ids.has(item.id) || require_match.default(item, rec.selector));
|
|
350
|
-
});
|
|
351
|
-
if (affected.length === 0) return;
|
|
352
|
-
const needsReExecution = [];
|
|
353
|
-
for (const rec of affected) {
|
|
354
|
-
const { selector, options } = rec;
|
|
355
|
-
const incremental = rec.answered ? require_incrementalQueryUpdate.default(rec.items, selector, options, changes) : null;
|
|
356
|
-
if (incremental == null) {
|
|
357
|
-
needsReExecution.push(rec);
|
|
358
|
-
continue;
|
|
36
|
+
if (keys.exclude != null) {
|
|
37
|
+
const excludedIds = /* @__PURE__ */ new Set();
|
|
38
|
+
for (const key of keys.exclude) {
|
|
39
|
+
const idSet = index.get(key);
|
|
40
|
+
if (idSet) for (const id of idSet) excludedIds.add(id);
|
|
359
41
|
}
|
|
360
|
-
|
|
361
|
-
const delta = require_queryDelta.diffQueryResults(rec.items, incremental);
|
|
362
|
-
if (require_queryDelta.isEmptyQueryDelta(delta)) continue;
|
|
363
|
-
this.publishResult(collectionName, qid, incremental);
|
|
364
|
-
this.publishState(collectionName, qid, "complete", null, delta);
|
|
365
|
-
}
|
|
366
|
-
for (const { selector, options } of needsReExecution) this.publishState(collectionName, require_queryId.default(selector, options), "active", null);
|
|
367
|
-
await Promise.all(needsReExecution.map(({ selector, options }) => this.runQuery(collectionName, selector, options)));
|
|
368
|
-
}
|
|
369
|
-
async insert(collectionName, newItem) {
|
|
370
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
371
|
-
if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
372
|
-
if ((await storage.readIds([newItem.id])).length > 0) throw new Error(`Item with id ${String(newItem.id)} already exists`);
|
|
373
|
-
await storage.insert([newItem]);
|
|
374
|
-
await this.checkQueryUpdates(collectionName, {
|
|
375
|
-
upserts: [newItem],
|
|
376
|
-
deletes: []
|
|
377
|
-
});
|
|
378
|
-
return newItem;
|
|
379
|
-
}
|
|
380
|
-
async updateOne(collectionName, selector, modifier) {
|
|
381
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
382
|
-
if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
383
|
-
const [item] = await this.executeQuery(collectionName, selector, { limit: 1 });
|
|
384
|
-
const { $setOnInsert, ...rest } = modifier;
|
|
385
|
-
if (item == null) return [];
|
|
386
|
-
const modified = require_modify.default(require_deepClone.default(item), rest);
|
|
387
|
-
if (item.id !== modified.id) {
|
|
388
|
-
if ((await storage.readIds([modified.id])).length > 0) throw new Error(`Item with id ${String(modified.id)} already exists`);
|
|
42
|
+
includedIds = includedIds.filter((id) => !excludedIds.has(id));
|
|
389
43
|
}
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
396
|
-
if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
397
|
-
const items = await this.executeQuery(collectionName, selector);
|
|
398
|
-
if (items.length === 0) return [];
|
|
399
|
-
const { $setOnInsert, ...rest } = modifier;
|
|
400
|
-
const changed = await Promise.all(items.map(async (item) => {
|
|
401
|
-
const modified = require_modify.default(require_deepClone.default(item), rest);
|
|
402
|
-
if (item.id !== modified.id) {
|
|
403
|
-
if ((await storage.readIds([modified.id])).length > 0) throw new Error(`Item with id ${String(modified.id)} already exists`);
|
|
404
|
-
}
|
|
405
|
-
return modified;
|
|
406
|
-
}));
|
|
407
|
-
await storage.replace(changed);
|
|
408
|
-
await this.checkQueryUpdates(collectionName, toChangeset(items, changed));
|
|
409
|
-
return changed;
|
|
410
|
-
}
|
|
411
|
-
async replaceOne(collectionName, selector, replacement) {
|
|
412
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
413
|
-
if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
414
|
-
const [item] = await this.executeQuery(collectionName, selector, { limit: 1 });
|
|
415
|
-
if (item == null) return [];
|
|
416
|
-
const modified = {
|
|
417
|
-
...replacement,
|
|
418
|
-
id: replacement.id ?? item.id
|
|
44
|
+
return {
|
|
45
|
+
matched: true,
|
|
46
|
+
ids: includedIds,
|
|
47
|
+
fields: [field],
|
|
48
|
+
keepSelector: filtersForNull
|
|
419
49
|
};
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
}
|
|
423
|
-
await storage.replace([modified]);
|
|
424
|
-
await this.checkQueryUpdates(collectionName, toChangeset([item], [modified]));
|
|
425
|
-
return [modified];
|
|
426
|
-
}
|
|
427
|
-
async removeOne(collectionName, selector) {
|
|
428
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
429
|
-
if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
430
|
-
const [item] = await this.executeQuery(collectionName, selector, { limit: 1 });
|
|
431
|
-
if (item == null) return [];
|
|
432
|
-
await storage.remove([item]);
|
|
433
|
-
await this.checkQueryUpdates(collectionName, {
|
|
434
|
-
upserts: [],
|
|
435
|
-
deletes: [item.id]
|
|
436
|
-
});
|
|
437
|
-
return [item];
|
|
438
|
-
}
|
|
439
|
-
async removeMany(collectionName, selector) {
|
|
440
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
441
|
-
if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
442
|
-
const items = await this.executeQuery(collectionName, selector);
|
|
443
|
-
if (items.length === 0) return [];
|
|
444
|
-
await storage.remove(items);
|
|
445
|
-
await this.checkQueryUpdates(collectionName, {
|
|
446
|
-
upserts: [],
|
|
447
|
-
deletes: items.map((item) => item.id)
|
|
448
|
-
});
|
|
449
|
-
return items;
|
|
450
|
-
}
|
|
451
|
-
};
|
|
50
|
+
};
|
|
51
|
+
}
|
|
452
52
|
//#endregion
|
|
453
|
-
exports.default =
|
|
53
|
+
exports.default = storageIndexQuery;
|