@signaldb/core 2.0.0-beta.18 → 2.0.0-beta.19
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 +39 -17
- package/dist/index.cjs.js +9 -9
- package/dist/index.mjs +9 -9
- package/dist/index17.cjs.js +7 -2
- package/dist/index17.mjs +7 -2
- package/dist/index27.cjs.js +36 -337
- package/dist/index27.mjs +36 -337
- package/dist/index28.cjs.js +318 -573
- package/dist/index28.mjs +318 -573
- package/dist/index29.cjs.js +599 -8
- package/dist/index29.mjs +599 -8
- package/dist/index30.cjs.js +6 -6
- package/dist/index30.mjs +6 -6
- package/dist/index31.cjs.js +7 -86
- package/dist/index31.mjs +7 -85
- package/dist/index32.cjs.js +76 -465
- package/dist/index32.mjs +75 -465
- package/dist/index33.cjs.js +45 -64
- package/dist/index33.mjs +45 -64
- package/dist/index34.cjs.js +403 -530
- package/dist/index34.mjs +405 -532
- package/dist/index35.cjs.js +68 -17
- package/dist/index35.mjs +68 -17
- package/dist/index36.cjs.js +535 -344
- package/dist/index36.mjs +537 -346
- package/dist/index37.cjs.js +14 -532
- package/dist/index37.mjs +14 -532
- 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/types/StorageAdapter.d.ts +9 -1
- package/dist/utils/idIndexQuery.d.ts +20 -0
- package/dist/utils/storageIndexQuery.d.ts +20 -0
- package/package.json +1 -1
package/dist/index32.cjs.js
CHANGED
|
@@ -1,479 +1,90 @@
|
|
|
1
|
-
|
|
2
|
-
const require_queryDelta = require("./index4.cjs.js");
|
|
3
|
-
const require_getMatchingKeys = require("./index14.cjs.js");
|
|
4
|
-
const require_getIndexInfo = require("./index17.cjs.js");
|
|
5
|
-
const require_deepClone = require("./index18.cjs.js");
|
|
6
|
-
const require_match = require("./index19.cjs.js");
|
|
7
|
-
const require_modify = require("./index20.cjs.js");
|
|
8
|
-
const require_projectItems = require("./index23.cjs.js");
|
|
9
|
-
const require_sortItems = require("./index24.cjs.js");
|
|
10
|
-
const require_incrementalQueryUpdate = require("./index25.cjs.js");
|
|
11
|
-
const require_queryId = require("./index26.cjs.js");
|
|
12
|
-
//#region src/AsyncDataAdapter.ts
|
|
1
|
+
//#region src/utils/reactiveOrAsync.ts
|
|
13
2
|
/**
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
3
|
+
* A generator helper that makes TypeScript infer the “synchronous value type” for maybe-async expressions.
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* const doc = yield* unwrap(Collection.findOne(...))
|
|
7
|
+
* const list = yield* unwrap(Collection.find(...).fetch())
|
|
8
|
+
*
|
|
9
|
+
* Runtime note:
|
|
10
|
+
* This does not “unwrap” Promises by itself. It yields the value/Promise to the runner and returns the
|
|
11
|
+
* value that the runner feeds back via `.next(...)`.
|
|
12
|
+
* @param value The value (or Promise of a value) to yield to the runner.
|
|
13
|
+
* @returns A generator that yields `value` and resolves to the runner-supplied unwrapped `T`.
|
|
17
14
|
*/
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
constructor(collectionName, selector, options, attempts, cause) {
|
|
24
|
-
const reason = cause instanceof Error ? cause.message : String(cause);
|
|
25
|
-
super(`Query on "${collectionName}" failed after ${attempts} attempt(s): ${reason}`);
|
|
26
|
-
this.name = "QueryError";
|
|
27
|
-
this.collectionName = collectionName;
|
|
28
|
-
this.selector = selector;
|
|
29
|
-
this.options = options;
|
|
30
|
-
this.attempts = attempts;
|
|
31
|
-
this.cause = cause;
|
|
32
|
-
}
|
|
33
|
-
};
|
|
15
|
+
function unwrap(value) {
|
|
16
|
+
return (function* () {
|
|
17
|
+
return yield value;
|
|
18
|
+
})();
|
|
19
|
+
}
|
|
34
20
|
/**
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
* new state, while an item that is gone — removed, or given a new id — is described by the id it
|
|
39
|
-
* used to have and nothing else. Mixing the states from before and after a write into one list
|
|
40
|
-
* loses exactly that distinction.
|
|
41
|
-
* @template T - The type of the items.
|
|
42
|
-
* @param previousItems - The items as they were before the write.
|
|
43
|
-
* @param modifiedItems - The items as they are after it.
|
|
44
|
-
* @returns The changeset describing the write.
|
|
21
|
+
* Internal: checks for thenables (Promise-like).
|
|
22
|
+
* @param value The value to test.
|
|
23
|
+
* @returns `true` if `value` looks like a Promise/thenable.
|
|
45
24
|
*/
|
|
46
|
-
function
|
|
47
|
-
|
|
48
|
-
return {
|
|
49
|
-
upserts: modifiedItems,
|
|
50
|
-
deletes: previousItems.map((item) => item.id).filter((id) => !modifiedIds.has(id))
|
|
51
|
-
};
|
|
25
|
+
function isThenable(value) {
|
|
26
|
+
return typeof value === "object" && value !== null && typeof value.then === "function";
|
|
52
27
|
}
|
|
53
|
-
var DEFAULT_RETRY_ATTEMPTS = 3;
|
|
54
|
-
var defaultRetryDelay = (attempt) => 100 * 4 ** (attempt - 1);
|
|
55
|
-
var wait = (ms) => new Promise((resolve) => {
|
|
56
|
-
setTimeout(resolve, ms);
|
|
57
|
-
});
|
|
58
28
|
/**
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
* -
|
|
62
|
-
* -
|
|
63
|
-
*
|
|
29
|
+
* Internal runner: executes a generator either synchronously (reactive) or asynchronously (imperative).
|
|
30
|
+
*
|
|
31
|
+
* - In sync mode, yielding a Promise is a programming error and throws.
|
|
32
|
+
* - In async mode, yielded Promises are awaited.
|
|
33
|
+
* @param thisArgument The `this` value to bind when invoking `gen`.
|
|
34
|
+
* @param mode Execution mode options.
|
|
35
|
+
* @param gen The generator workflow to run.
|
|
36
|
+
* @returns The workflow result (a plain value in sync mode, or a Promise in async mode).
|
|
64
37
|
*/
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
queries = /* @__PURE__ */ new Map();
|
|
75
|
-
constructor(options) {
|
|
76
|
-
this.options = options;
|
|
77
|
-
this.id = options.id || "async-data-adapter";
|
|
78
|
-
this.onError = options.onError ?? ((error) => {
|
|
79
|
-
console.error(error);
|
|
80
|
-
});
|
|
81
|
-
this.retryAttempts = Math.max(1, options.retry?.attempts ?? DEFAULT_RETRY_ATTEMPTS);
|
|
82
|
-
this.retryDelay = options.retry?.delay ?? defaultRetryDelay;
|
|
83
|
-
}
|
|
84
|
-
createCollectionBackend(collection, indices) {
|
|
85
|
-
this.collectionIndices.set(collection.name, indices);
|
|
86
|
-
this.queries.set(collection.name, /* @__PURE__ */ new Map());
|
|
87
|
-
this.ensureStorageAdapter(collection.name);
|
|
88
|
-
const ready = (async () => {
|
|
89
|
-
try {
|
|
90
|
-
await this.setupStorage(collection.name, indices);
|
|
91
|
-
} catch (error) {
|
|
92
|
-
this.onError(error);
|
|
93
|
-
throw error;
|
|
94
|
-
}
|
|
95
|
-
})();
|
|
96
|
-
this.storageAdapterReady.set(collection.name, ready);
|
|
97
|
-
const registerQuery = (selector, options) => {
|
|
98
|
-
const qid = require_queryId.default(selector, options);
|
|
99
|
-
const registry = this.queries.get(collection.name);
|
|
100
|
-
if (!registry) throw new Error(`Collection ${collection.name} not initialized!`);
|
|
101
|
-
registry.set(qid, {
|
|
102
|
-
selector,
|
|
103
|
-
options,
|
|
104
|
-
items: [],
|
|
105
|
-
listeners: /* @__PURE__ */ new Set(),
|
|
106
|
-
...registry.get(qid),
|
|
107
|
-
state: "active",
|
|
108
|
-
error: null
|
|
109
|
-
});
|
|
110
|
-
this.fulfillQuery(collection.name, selector, options).catch(this.onError);
|
|
111
|
-
};
|
|
112
|
-
const unregisterQuery = (selector, options) => {
|
|
113
|
-
const qid = require_queryId.default(selector, options);
|
|
114
|
-
this.queries.get(collection.name)?.delete(qid);
|
|
115
|
-
};
|
|
116
|
-
const getQueryState = (selector, options) => {
|
|
117
|
-
return (this.queries.get(collection.name)?.get(require_queryId.default(selector, options)))?.state ?? "active";
|
|
118
|
-
};
|
|
119
|
-
const getQueryError = (selector, options) => {
|
|
120
|
-
return (this.queries.get(collection.name)?.get(require_queryId.default(selector, options)))?.error ?? null;
|
|
121
|
-
};
|
|
122
|
-
const getQueryResult = (selector, options) => {
|
|
123
|
-
return (this.queries.get(collection.name)?.get(require_queryId.default(selector, options)))?.items ?? [];
|
|
124
|
-
};
|
|
125
|
-
const retryQuery = (selector, options) => {
|
|
126
|
-
const qid = require_queryId.default(selector, options);
|
|
127
|
-
if (!this.queries.get(collection.name)?.get(qid)) return;
|
|
128
|
-
this.publishState(collection.name, qid, "active", null);
|
|
129
|
-
this.runQuery(collection.name, selector, options);
|
|
130
|
-
};
|
|
131
|
-
const onQueryStateChange = (selector, options, callback) => {
|
|
132
|
-
const qid = require_queryId.default(selector, options);
|
|
133
|
-
const registry = this.queries.get(collection.name);
|
|
134
|
-
if (!registry) throw new Error(`Collection ${collection.name} not initialized!`);
|
|
135
|
-
if (!registry.has(qid)) registry.set(qid, {
|
|
136
|
-
selector,
|
|
137
|
-
options,
|
|
138
|
-
state: "active",
|
|
139
|
-
error: null,
|
|
140
|
-
items: [],
|
|
141
|
-
listeners: /* @__PURE__ */ new Set()
|
|
142
|
-
});
|
|
143
|
-
registry.get(qid)?.listeners.add(callback);
|
|
144
|
-
if (registry.get(qid)?.state === "error") retryQuery(selector, options);
|
|
145
|
-
return () => registry.get(qid)?.listeners.delete(callback);
|
|
146
|
-
};
|
|
147
|
-
return {
|
|
148
|
-
insert: async (item) => {
|
|
149
|
-
await ready;
|
|
150
|
-
return await this.insert(collection.name, item);
|
|
151
|
-
},
|
|
152
|
-
updateOne: async (selector, modifier) => {
|
|
153
|
-
await ready;
|
|
154
|
-
return this.updateOne(collection.name, selector, modifier);
|
|
155
|
-
},
|
|
156
|
-
updateMany: async (selector, modifier) => {
|
|
157
|
-
await ready;
|
|
158
|
-
return this.updateMany(collection.name, selector, modifier);
|
|
159
|
-
},
|
|
160
|
-
replaceOne: async (selector, replacement) => {
|
|
161
|
-
await ready;
|
|
162
|
-
return this.replaceOne(collection.name, selector, replacement);
|
|
163
|
-
},
|
|
164
|
-
removeOne: async (selector) => {
|
|
165
|
-
await ready;
|
|
166
|
-
return this.removeOne(collection.name, selector);
|
|
167
|
-
},
|
|
168
|
-
removeMany: async (selector) => {
|
|
169
|
-
await ready;
|
|
170
|
-
return this.removeMany(collection.name, selector);
|
|
171
|
-
},
|
|
172
|
-
registerQuery,
|
|
173
|
-
unregisterQuery,
|
|
174
|
-
retryQuery,
|
|
175
|
-
getQueryState,
|
|
176
|
-
getQueryError,
|
|
177
|
-
getQueryResult,
|
|
178
|
-
onQueryStateChange,
|
|
179
|
-
executeQuery: async (selector, options) => {
|
|
180
|
-
await ready;
|
|
181
|
-
return this.executeQuery(collection.name, selector, options);
|
|
182
|
-
},
|
|
183
|
-
dispose: async () => {
|
|
184
|
-
this.storageAdapters.delete(collection.name);
|
|
185
|
-
this.queries.delete(collection.name);
|
|
186
|
-
this.collectionIndices.delete(collection.name);
|
|
187
|
-
this.storageAdapterReady.delete(collection.name);
|
|
188
|
-
},
|
|
189
|
-
isReady: async () => {
|
|
190
|
-
await ready;
|
|
191
|
-
}
|
|
192
|
-
};
|
|
193
|
-
}
|
|
194
|
-
async setupStorage(collectionName, indices) {
|
|
195
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
196
|
-
if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
197
|
-
await storage.setup();
|
|
198
|
-
await Promise.all(indices.map((index) => storage.createIndex(index)));
|
|
199
|
-
}
|
|
200
|
-
ensureStorageAdapter(name) {
|
|
201
|
-
if (this.storageAdapters.has(name)) return;
|
|
202
|
-
const adapter = this.options.storage(name);
|
|
203
|
-
if (!adapter) return;
|
|
204
|
-
this.storageAdapters.set(name, adapter);
|
|
205
|
-
}
|
|
206
|
-
/**
|
|
207
|
-
* Compute and publish the result for a specific query
|
|
208
|
-
* @param collectionName - name of the collection
|
|
209
|
-
* @param selector - query selector
|
|
210
|
-
* @param options - query options
|
|
211
|
-
*/
|
|
212
|
-
async fulfillQuery(collectionName, selector, options) {
|
|
213
|
-
const qid = require_queryId.default(selector, options);
|
|
214
|
-
const registry = this.queries.get(collectionName);
|
|
215
|
-
if (!registry) throw new Error(`Collection ${collectionName} not initialized!`);
|
|
216
|
-
if (!registry.get(qid)) return;
|
|
217
|
-
this.publishState(collectionName, qid, "active", null);
|
|
218
|
-
await this.runQuery(collectionName, selector, options);
|
|
219
|
-
}
|
|
220
|
-
/**
|
|
221
|
-
* Executes a query, retrying transient failures before giving up. The state
|
|
222
|
-
* stays `'active'` across retries — consumers should see "still loading",
|
|
223
|
-
* not "failed", until we actually stop trying. Only the final failure is
|
|
224
|
-
* published as `'error'` and reported through `onError`; previously that
|
|
225
|
-
* error was swallowed entirely (`fulfillQuery` caught it internally, so the
|
|
226
|
-
* `.catch(this.onError)` on its call site was unreachable) and the query
|
|
227
|
-
* stayed dead for the rest of the session.
|
|
228
|
-
* @param collectionName - name of the collection
|
|
229
|
-
* @param selector - query selector
|
|
230
|
-
* @param options - query options
|
|
231
|
-
*/
|
|
232
|
-
async runQuery(collectionName, selector, options) {
|
|
233
|
-
const qid = require_queryId.default(selector, options);
|
|
234
|
-
let lastError;
|
|
235
|
-
for (let attempt = 1; attempt <= this.retryAttempts; attempt += 1) {
|
|
236
|
-
if (!this.queries.get(collectionName)?.has(qid)) return;
|
|
237
|
-
try {
|
|
238
|
-
const items = await this.executeQuery(collectionName, selector, options);
|
|
239
|
-
const rec = this.queries.get(collectionName)?.get(qid);
|
|
240
|
-
const delta = rec?.answered ? require_queryDelta.diffQueryResults(rec.items, items) : void 0;
|
|
241
|
-
this.publishResult(collectionName, qid, items);
|
|
242
|
-
this.publishState(collectionName, qid, "complete", null, delta);
|
|
243
|
-
return;
|
|
244
|
-
} catch (error) {
|
|
245
|
-
lastError = error;
|
|
246
|
-
if (attempt < this.retryAttempts) await wait(this.retryDelay(attempt));
|
|
247
|
-
}
|
|
38
|
+
function runReactiveOrAsync(thisArgument, mode, gen) {
|
|
39
|
+
const a = !!mode?.async;
|
|
40
|
+
const it = gen.call(thisArgument, a);
|
|
41
|
+
if (!a) {
|
|
42
|
+
let step = it.next();
|
|
43
|
+
while (!step.done) {
|
|
44
|
+
const y = step.value;
|
|
45
|
+
if (isThenable(y)) throw new Error("Promise yielded in sync flow");
|
|
46
|
+
step = it.next(y);
|
|
248
47
|
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
* @param state - new state
|
|
258
|
-
* @param error - error if state is 'error', null otherwise
|
|
259
|
-
* @param delta - what changed about the result, when that is known
|
|
260
|
-
*/
|
|
261
|
-
publishState(collectionName, qid, state, error, delta) {
|
|
262
|
-
const rec = this.queries.get(collectionName)?.get(qid);
|
|
263
|
-
if (!rec) return;
|
|
264
|
-
rec.state = state;
|
|
265
|
-
rec.error = error;
|
|
266
|
-
const subscribers = [...rec.listeners];
|
|
267
|
-
for (const callback of subscribers) try {
|
|
268
|
-
require_queryDelta.callWithDelta(callback, state, delta);
|
|
269
|
-
} catch (error_) {
|
|
270
|
-
this.onError(error_);
|
|
48
|
+
return step.value;
|
|
49
|
+
}
|
|
50
|
+
return (async function() {
|
|
51
|
+
let step = it.next();
|
|
52
|
+
while (!step.done) {
|
|
53
|
+
const y = step.value;
|
|
54
|
+
const v = isThenable(y) ? await y : y;
|
|
55
|
+
step = it.next(v);
|
|
271
56
|
}
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
if (!storageAdapter) throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
287
|
-
if (selector != null && Object.keys(selector).length === 1 && "id" in selector && typeof selector.id !== "object") return {
|
|
288
|
-
matched: true,
|
|
289
|
-
ids: [selector.id].filter(Boolean),
|
|
290
|
-
optimizedSelector: {}
|
|
291
|
-
};
|
|
292
|
-
if (selector == null) return {
|
|
293
|
-
matched: false,
|
|
294
|
-
ids: [],
|
|
295
|
-
optimizedSelector: {}
|
|
296
|
-
};
|
|
297
|
-
return require_getIndexInfo.default((this.collectionIndices.get(collectionName) ?? []).map((field) => async (flatSelector) => {
|
|
298
|
-
if (!Object.hasOwnProperty.call(flatSelector, field)) return { matched: false };
|
|
299
|
-
const index = await storageAdapter.readIndex(field);
|
|
300
|
-
const fieldSelector = flatSelector[field];
|
|
301
|
-
const filtersForNull = fieldSelector == null || fieldSelector.$exists === false;
|
|
302
|
-
const keys = filtersForNull ? {
|
|
303
|
-
include: null,
|
|
304
|
-
exclude: [...index.keys()].filter((key) => key != null)
|
|
305
|
-
} : require_getMatchingKeys.default(field, flatSelector);
|
|
306
|
-
if (keys.include == null && keys.exclude == null) return { matched: false };
|
|
307
|
-
let includedIds = [];
|
|
308
|
-
if (keys.include == null) for (const set of index.values()) for (const pos of set) includedIds.push(pos);
|
|
309
|
-
else for (const key of keys.include) {
|
|
310
|
-
const idSet = index.get(key);
|
|
311
|
-
if (idSet) for (const id of idSet) includedIds.push(id);
|
|
312
|
-
}
|
|
313
|
-
if (keys.exclude != null) {
|
|
314
|
-
const excludeIds = /* @__PURE__ */ new Set();
|
|
315
|
-
for (const key of keys.exclude) {
|
|
316
|
-
const idSet = index.get(key);
|
|
317
|
-
if (idSet) for (const id of idSet) excludeIds.add(id);
|
|
318
|
-
}
|
|
319
|
-
includedIds = includedIds.filter((pos) => !excludeIds.has(pos));
|
|
320
|
-
}
|
|
321
|
-
return {
|
|
322
|
-
matched: true,
|
|
323
|
-
ids: includedIds,
|
|
324
|
-
fields: [field],
|
|
325
|
-
keepSelector: filtersForNull
|
|
326
|
-
};
|
|
327
|
-
}), selector);
|
|
328
|
-
}
|
|
329
|
-
async queryItems(collectionName, selector) {
|
|
330
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
331
|
-
if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
332
|
-
const index = await this.getIndexInfo(collectionName, selector);
|
|
333
|
-
const matchItems = (item) => {
|
|
334
|
-
if (index.optimizedSelector == null) return true;
|
|
335
|
-
if (Object.keys(index.optimizedSelector).length <= 0) return true;
|
|
336
|
-
return require_match.default(item, index.optimizedSelector);
|
|
337
|
-
};
|
|
338
|
-
if (index.matched) {
|
|
339
|
-
const items = await storage.readIds(index.ids);
|
|
340
|
-
if (require_isEqual.default(index.optimizedSelector, {})) return items;
|
|
341
|
-
return items.filter(matchItems);
|
|
342
|
-
} else {
|
|
343
|
-
const allItems = await storage.readAll();
|
|
344
|
-
if (require_isEqual.default(selector, {})) return allItems;
|
|
345
|
-
return allItems.filter(matchItems);
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
async executeQuery(collectionName, selector, options) {
|
|
349
|
-
const items = await this.queryItems(collectionName, selector || {});
|
|
350
|
-
const { sort, skip, limit, fields } = options || {};
|
|
351
|
-
const sorted = sort ? require_sortItems.default(items, sort) : items;
|
|
352
|
-
const skipped = skip ? sorted.slice(skip) : sorted;
|
|
353
|
-
return require_projectItems.default(limit ? skipped.slice(0, limit) : skipped, fields);
|
|
354
|
-
}
|
|
57
|
+
return step.value;
|
|
58
|
+
})();
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Factory that turns a generator workflow into a callable method that can run in sync (reactive) or async mode.
|
|
62
|
+
*
|
|
63
|
+
* Call style:
|
|
64
|
+
* fn(a, b) -> sync/reactive return
|
|
65
|
+
* await fn(a, b, { async: true }) -> async return
|
|
66
|
+
* @param gen Generator workflow. Receives `(a)` which indicates async mode and should `yield`/`yield* unwrap(...)`
|
|
67
|
+
* any values that may be Promises.
|
|
68
|
+
* @returns A callable method with overloads plus a `.generator` property for composition.
|
|
69
|
+
*/
|
|
70
|
+
function reactiveOrAsync(gen) {
|
|
355
71
|
/**
|
|
356
|
-
*
|
|
357
|
-
*
|
|
358
|
-
*
|
|
359
|
-
* result alone — no round trip to the storage, and no detour through `'active'`, because there is
|
|
360
|
-
* no window in which the query is stale. Only a query the change cannot be reasoned about
|
|
361
|
-
* locally — a window onto a larger set, or one that has never been answered — goes back to the
|
|
362
|
-
* store, and that one gets the same retry and reporting behaviour a freshly registered query
|
|
363
|
-
* gets: a refresh that fails silently leaves exactly the same dead cursor.
|
|
364
|
-
* @param collectionName - name of the collection
|
|
365
|
-
* @param changes - the items the write created, updated or removed
|
|
72
|
+
* The generated method wrapper.
|
|
73
|
+
* @param allArguments Method arguments, optionally ending with a `ModeOptions` object.
|
|
74
|
+
* @returns The workflow result (sync) or a Promise of the result (async).
|
|
366
75
|
*/
|
|
367
|
-
|
|
368
|
-
const
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
if (changes.deletes.some((id) => ids.has(id))) return true;
|
|
375
|
-
return changes.upserts.some((item) => ids.has(item.id) || require_match.default(item, rec.selector));
|
|
376
|
-
});
|
|
377
|
-
if (affected.length === 0) return;
|
|
378
|
-
const needsReExecution = [];
|
|
379
|
-
for (const rec of affected) {
|
|
380
|
-
const { selector, options } = rec;
|
|
381
|
-
const incremental = rec.answered ? require_incrementalQueryUpdate.default(rec.items, selector, options, changes) : null;
|
|
382
|
-
if (incremental == null) {
|
|
383
|
-
needsReExecution.push(rec);
|
|
384
|
-
continue;
|
|
385
|
-
}
|
|
386
|
-
const qid = require_queryId.default(selector, options);
|
|
387
|
-
const delta = require_queryDelta.diffQueryResults(rec.items, incremental);
|
|
388
|
-
if (require_queryDelta.isEmptyQueryDelta(delta)) continue;
|
|
389
|
-
this.publishResult(collectionName, qid, incremental);
|
|
390
|
-
this.publishState(collectionName, qid, "complete", null, delta);
|
|
391
|
-
}
|
|
392
|
-
for (const { selector, options } of needsReExecution) this.publishState(collectionName, require_queryId.default(selector, options), "active", null);
|
|
393
|
-
await Promise.all(needsReExecution.map(({ selector, options }) => this.runQuery(collectionName, selector, options)));
|
|
394
|
-
}
|
|
395
|
-
async insert(collectionName, newItem) {
|
|
396
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
397
|
-
if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
398
|
-
if ((await storage.readIds([newItem.id])).length > 0) throw new Error(`Item with id ${String(newItem.id)} already exists`);
|
|
399
|
-
await storage.insert([newItem]);
|
|
400
|
-
await this.checkQueryUpdates(collectionName, {
|
|
401
|
-
upserts: [newItem],
|
|
402
|
-
deletes: []
|
|
76
|
+
function method(...allArguments) {
|
|
77
|
+
const last = allArguments.length > 0 ? allArguments.at(-1) : void 0;
|
|
78
|
+
const hasMode = typeof last === "object" && last !== null && "async" in last;
|
|
79
|
+
const mode = hasMode ? last : void 0;
|
|
80
|
+
const parameters = hasMode ? allArguments.slice(0, -1) : allArguments;
|
|
81
|
+
return runReactiveOrAsync(this, mode, function* (a) {
|
|
82
|
+
return yield* gen.call(this, a, ...parameters);
|
|
403
83
|
});
|
|
404
|
-
return newItem;
|
|
405
84
|
}
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
const [item] = await this.executeQuery(collectionName, selector, { limit: 1 });
|
|
410
|
-
const { $setOnInsert, ...rest } = modifier;
|
|
411
|
-
if (item == null) return [];
|
|
412
|
-
const modified = require_modify.default(require_deepClone.default(item), rest);
|
|
413
|
-
if (item.id !== modified.id) {
|
|
414
|
-
if ((await storage.readIds([modified.id])).length > 0) throw new Error(`Item with id ${String(modified.id)} already exists`);
|
|
415
|
-
}
|
|
416
|
-
await storage.replace([modified]);
|
|
417
|
-
await this.checkQueryUpdates(collectionName, toChangeset([item], [modified]));
|
|
418
|
-
return [modified];
|
|
419
|
-
}
|
|
420
|
-
async updateMany(collectionName, selector, modifier) {
|
|
421
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
422
|
-
if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
423
|
-
const items = await this.executeQuery(collectionName, selector);
|
|
424
|
-
if (items.length === 0) return [];
|
|
425
|
-
const { $setOnInsert, ...rest } = modifier;
|
|
426
|
-
const changed = await Promise.all(items.map(async (item) => {
|
|
427
|
-
const modified = require_modify.default(require_deepClone.default(item), rest);
|
|
428
|
-
if (item.id !== modified.id) {
|
|
429
|
-
if ((await storage.readIds([modified.id])).length > 0) throw new Error(`Item with id ${String(modified.id)} already exists`);
|
|
430
|
-
}
|
|
431
|
-
return modified;
|
|
432
|
-
}));
|
|
433
|
-
await storage.replace(changed);
|
|
434
|
-
await this.checkQueryUpdates(collectionName, toChangeset(items, changed));
|
|
435
|
-
return changed;
|
|
436
|
-
}
|
|
437
|
-
async replaceOne(collectionName, selector, replacement) {
|
|
438
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
439
|
-
if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
440
|
-
const [item] = await this.executeQuery(collectionName, selector, { limit: 1 });
|
|
441
|
-
if (item == null) return [];
|
|
442
|
-
const modified = {
|
|
443
|
-
...replacement,
|
|
444
|
-
id: replacement.id ?? item.id
|
|
445
|
-
};
|
|
446
|
-
if (item.id !== modified.id) {
|
|
447
|
-
if ((await storage.readIds([modified.id])).length > 0) throw new Error(`Item with id ${String(modified.id)} already exists`);
|
|
448
|
-
}
|
|
449
|
-
await storage.replace([modified]);
|
|
450
|
-
await this.checkQueryUpdates(collectionName, toChangeset([item], [modified]));
|
|
451
|
-
return [modified];
|
|
452
|
-
}
|
|
453
|
-
async removeOne(collectionName, selector) {
|
|
454
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
455
|
-
if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
456
|
-
const [item] = await this.executeQuery(collectionName, selector, { limit: 1 });
|
|
457
|
-
if (item == null) return [];
|
|
458
|
-
await storage.remove([item]);
|
|
459
|
-
await this.checkQueryUpdates(collectionName, {
|
|
460
|
-
upserts: [],
|
|
461
|
-
deletes: [item.id]
|
|
462
|
-
});
|
|
463
|
-
return [item];
|
|
464
|
-
}
|
|
465
|
-
async removeMany(collectionName, selector) {
|
|
466
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
467
|
-
if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
468
|
-
const items = await this.executeQuery(collectionName, selector);
|
|
469
|
-
if (items.length === 0) return [];
|
|
470
|
-
await storage.remove(items);
|
|
471
|
-
await this.checkQueryUpdates(collectionName, {
|
|
472
|
-
upserts: [],
|
|
473
|
-
deletes: items.map((item) => item.id)
|
|
474
|
-
});
|
|
475
|
-
return items;
|
|
476
|
-
}
|
|
477
|
-
};
|
|
85
|
+
method.generator = gen;
|
|
86
|
+
return method;
|
|
87
|
+
}
|
|
478
88
|
//#endregion
|
|
479
|
-
exports.default =
|
|
89
|
+
exports.default = reactiveOrAsync;
|
|
90
|
+
exports.unwrap = unwrap;
|