@signaldb/core 2.0.0-beta.13 → 2.0.0-beta.14
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 +72 -46
- package/dist/AsyncDataAdapter.d.ts +11 -2
- package/dist/Collection/Cursor.d.ts +11 -1
- package/dist/Collection/Observer.d.ts +31 -0
- package/dist/DataAdapter.d.ts +15 -2
- package/dist/WorkerDataAdapter.d.ts +25 -2
- package/dist/WorkerDataAdapterHost.d.ts +2 -0
- package/dist/index.cjs.js +15 -15
- package/dist/index.d.ts +2 -0
- package/dist/index.mjs +15 -15
- package/dist/index10.cjs.js +7 -19
- package/dist/index10.mjs +7 -19
- package/dist/index11.cjs.js +19 -41
- package/dist/index11.mjs +19 -41
- package/dist/index12.cjs.js +41 -18
- package/dist/index12.mjs +41 -18
- package/dist/index13.cjs.js +18 -41
- package/dist/index13.mjs +18 -41
- package/dist/index14.cjs.js +39 -80
- package/dist/index14.mjs +39 -80
- package/dist/index15.cjs.js +82 -11
- package/dist/index15.mjs +82 -11
- package/dist/index16.cjs.js +11 -132
- package/dist/index16.mjs +11 -132
- package/dist/index17.cjs.js +127 -38
- package/dist/index17.mjs +127 -38
- package/dist/index18.cjs.js +43 -11
- package/dist/index18.mjs +43 -11
- package/dist/index19.cjs.js +10 -18
- package/dist/index19.mjs +11 -19
- package/dist/index20.cjs.js +19 -33
- package/dist/index20.mjs +19 -33
- package/dist/index21.cjs.js +33 -31
- package/dist/index21.mjs +33 -31
- package/dist/index22.cjs.js +31 -21
- package/dist/index22.mjs +31 -21
- package/dist/index23.cjs.js +16 -14
- package/dist/index23.mjs +16 -14
- package/dist/index24.cjs.js +15 -338
- package/dist/index24.mjs +15 -338
- package/dist/index25.cjs.js +122 -554
- package/dist/index25.mjs +121 -554
- package/dist/index26.cjs.js +34 -7
- package/dist/index26.mjs +34 -7
- package/dist/index27.cjs.js +339 -7
- package/dist/index27.mjs +339 -7
- package/dist/index28.cjs.js +550 -83
- package/dist/index28.mjs +550 -82
- package/dist/index29.cjs.js +8 -422
- package/dist/index29.mjs +8 -422
- package/dist/index30.cjs.js +7 -68
- package/dist/index30.mjs +7 -68
- package/dist/index31.cjs.js +86 -28
- package/dist/index31.mjs +85 -28
- package/dist/index32.cjs.js +446 -278
- package/dist/index32.mjs +446 -278
- package/dist/index33.cjs.js +68 -17
- package/dist/index33.mjs +68 -17
- package/dist/index34.cjs.js +460 -304
- package/dist/index34.mjs +460 -304
- package/dist/index35.cjs.js +14 -532
- package/dist/index35.mjs +14 -532
- package/dist/index36.cjs.js +389 -0
- package/dist/index36.mjs +389 -0
- package/dist/index37.cjs.js +539 -0
- package/dist/index37.mjs +539 -0
- package/dist/index4.cjs.js +199 -140
- package/dist/index4.mjs +195 -140
- package/dist/index5.cjs.js +152 -253
- package/dist/index5.mjs +152 -253
- package/dist/index6.cjs.js +267 -89
- package/dist/index6.mjs +267 -89
- package/dist/index7.cjs.js +121 -29
- package/dist/index7.mjs +121 -29
- package/dist/index8.cjs.js +29 -8
- package/dist/index8.mjs +29 -8
- package/dist/index9.cjs.js +8 -7
- package/dist/index9.mjs +8 -7
- package/dist/utils/incrementalQueryUpdate.d.ts +60 -0
- package/dist/utils/projectItems.d.ts +12 -0
- package/dist/utils/queryDelta.d.ts +83 -0
- package/package.json +1 -1
package/dist/index29.cjs.js
CHANGED
|
@@ -1,425 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
const require_getMatchingKeys = require("./index13.cjs.js");
|
|
3
|
-
const require_getIndexInfo = require("./index16.cjs.js");
|
|
4
|
-
const require_deepClone = require("./index17.cjs.js");
|
|
5
|
-
const require_match = require("./index18.cjs.js");
|
|
6
|
-
const require_modify = require("./index19.cjs.js");
|
|
7
|
-
const require_project = require("./index21.cjs.js");
|
|
8
|
-
const require_queryId = require("./index22.cjs.js");
|
|
9
|
-
const require_sortItems = require("./index23.cjs.js");
|
|
10
|
-
//#region src/AsyncDataAdapter.ts
|
|
1
|
+
//#region src/createStorageAdapter.ts
|
|
11
2
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
3
|
+
* Creates an StorageAdapter based on the given definition.
|
|
4
|
+
* @param definition - The definition of the StorageAdapter.
|
|
5
|
+
* @returns The created StorageAdapter.
|
|
15
6
|
*/
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
options;
|
|
20
|
-
attempts;
|
|
21
|
-
constructor(collectionName, selector, options, attempts, cause) {
|
|
22
|
-
const reason = cause instanceof Error ? cause.message : String(cause);
|
|
23
|
-
super(`Query on "${collectionName}" failed after ${attempts} attempt(s): ${reason}`);
|
|
24
|
-
this.name = "QueryError";
|
|
25
|
-
this.collectionName = collectionName;
|
|
26
|
-
this.selector = selector;
|
|
27
|
-
this.options = options;
|
|
28
|
-
this.attempts = attempts;
|
|
29
|
-
this.cause = cause;
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
var DEFAULT_RETRY_ATTEMPTS = 3;
|
|
33
|
-
var defaultRetryDelay = (attempt) => 100 * 4 ** (attempt - 1);
|
|
34
|
-
var wait = (ms) => new Promise((resolve) => {
|
|
35
|
-
setTimeout(resolve, ms);
|
|
36
|
-
});
|
|
37
|
-
/**
|
|
38
|
-
* AsyncDataAdapter
|
|
39
|
-
* Combines WorkerDataAdapter + WorkerDataAdapterHost into a single, transport-free adapter.
|
|
40
|
-
* - Keeps the DataAdapter/CollectionBackend surface identical to the Worker version.
|
|
41
|
-
* - Executes queries and mutations directly against the provided StorageAdapter.
|
|
42
|
-
* - Preserves index-aware query optimization and push-style query updates to listeners.
|
|
43
|
-
*/
|
|
44
|
-
var AsyncDataAdapter = class {
|
|
45
|
-
options;
|
|
46
|
-
id;
|
|
47
|
-
onError;
|
|
48
|
-
retryAttempts;
|
|
49
|
-
retryDelay;
|
|
50
|
-
storageAdapters = /* @__PURE__ */ new Map();
|
|
51
|
-
storageAdapterReady = /* @__PURE__ */ new Map();
|
|
52
|
-
collectionIndices = /* @__PURE__ */ new Map();
|
|
53
|
-
queries = /* @__PURE__ */ new Map();
|
|
54
|
-
constructor(options) {
|
|
55
|
-
this.options = options;
|
|
56
|
-
this.id = options.id || "async-data-adapter";
|
|
57
|
-
this.onError = options.onError ?? ((error) => {
|
|
58
|
-
console.error(error);
|
|
59
|
-
});
|
|
60
|
-
this.retryAttempts = Math.max(1, options.retry?.attempts ?? DEFAULT_RETRY_ATTEMPTS);
|
|
61
|
-
this.retryDelay = options.retry?.delay ?? defaultRetryDelay;
|
|
62
|
-
}
|
|
63
|
-
createCollectionBackend(collection, indices) {
|
|
64
|
-
this.collectionIndices.set(collection.name, indices);
|
|
65
|
-
this.queries.set(collection.name, /* @__PURE__ */ new Map());
|
|
66
|
-
this.ensureStorageAdapter(collection.name);
|
|
67
|
-
const ready = (async () => {
|
|
68
|
-
try {
|
|
69
|
-
await this.setupStorage(collection.name, indices);
|
|
70
|
-
} catch (error) {
|
|
71
|
-
this.onError(error);
|
|
72
|
-
throw error;
|
|
73
|
-
}
|
|
74
|
-
})();
|
|
75
|
-
this.storageAdapterReady.set(collection.name, ready);
|
|
76
|
-
const registerQuery = (selector, options) => {
|
|
77
|
-
const qid = require_queryId.default(selector, options);
|
|
78
|
-
const registry = this.queries.get(collection.name);
|
|
79
|
-
if (!registry) throw new Error(`Collection ${collection.name} not initialized!`);
|
|
80
|
-
registry.set(qid, {
|
|
81
|
-
selector,
|
|
82
|
-
options,
|
|
83
|
-
items: [],
|
|
84
|
-
listeners: /* @__PURE__ */ new Set(),
|
|
85
|
-
...registry.get(qid),
|
|
86
|
-
state: "active",
|
|
87
|
-
error: null
|
|
88
|
-
});
|
|
89
|
-
this.fulfillQuery(collection.name, selector, options).catch(this.onError);
|
|
90
|
-
};
|
|
91
|
-
const unregisterQuery = (selector, options) => {
|
|
92
|
-
const qid = require_queryId.default(selector, options);
|
|
93
|
-
this.queries.get(collection.name)?.delete(qid);
|
|
94
|
-
};
|
|
95
|
-
const getQueryState = (selector, options) => {
|
|
96
|
-
return (this.queries.get(collection.name)?.get(require_queryId.default(selector, options)))?.state ?? "active";
|
|
97
|
-
};
|
|
98
|
-
const getQueryError = (selector, options) => {
|
|
99
|
-
return (this.queries.get(collection.name)?.get(require_queryId.default(selector, options)))?.error ?? null;
|
|
100
|
-
};
|
|
101
|
-
const getQueryResult = (selector, options) => {
|
|
102
|
-
return (this.queries.get(collection.name)?.get(require_queryId.default(selector, options)))?.items ?? [];
|
|
103
|
-
};
|
|
104
|
-
const retryQuery = (selector, options) => {
|
|
105
|
-
const qid = require_queryId.default(selector, options);
|
|
106
|
-
if (!this.queries.get(collection.name)?.get(qid)) return;
|
|
107
|
-
this.publishState(collection.name, qid, "active", null);
|
|
108
|
-
this.runQuery(collection.name, selector, options);
|
|
109
|
-
};
|
|
110
|
-
const onQueryStateChange = (selector, options, callback) => {
|
|
111
|
-
const qid = require_queryId.default(selector, options);
|
|
112
|
-
const registry = this.queries.get(collection.name);
|
|
113
|
-
if (!registry) throw new Error(`Collection ${collection.name} not initialized!`);
|
|
114
|
-
if (!registry.has(qid)) registry.set(qid, {
|
|
115
|
-
selector,
|
|
116
|
-
options,
|
|
117
|
-
state: "active",
|
|
118
|
-
error: null,
|
|
119
|
-
items: [],
|
|
120
|
-
listeners: /* @__PURE__ */ new Set()
|
|
121
|
-
});
|
|
122
|
-
registry.get(qid)?.listeners.add(callback);
|
|
123
|
-
if (registry.get(qid)?.state === "error") retryQuery(selector, options);
|
|
124
|
-
return () => registry.get(qid)?.listeners.delete(callback);
|
|
125
|
-
};
|
|
126
|
-
return {
|
|
127
|
-
insert: async (item) => {
|
|
128
|
-
await ready;
|
|
129
|
-
return await this.insert(collection.name, item);
|
|
130
|
-
},
|
|
131
|
-
updateOne: async (selector, modifier) => {
|
|
132
|
-
await ready;
|
|
133
|
-
return this.updateOne(collection.name, selector, modifier);
|
|
134
|
-
},
|
|
135
|
-
updateMany: async (selector, modifier) => {
|
|
136
|
-
await ready;
|
|
137
|
-
return this.updateMany(collection.name, selector, modifier);
|
|
138
|
-
},
|
|
139
|
-
replaceOne: async (selector, replacement) => {
|
|
140
|
-
await ready;
|
|
141
|
-
return this.replaceOne(collection.name, selector, replacement);
|
|
142
|
-
},
|
|
143
|
-
removeOne: async (selector) => {
|
|
144
|
-
await ready;
|
|
145
|
-
return this.removeOne(collection.name, selector);
|
|
146
|
-
},
|
|
147
|
-
removeMany: async (selector) => {
|
|
148
|
-
await ready;
|
|
149
|
-
return this.removeMany(collection.name, selector);
|
|
150
|
-
},
|
|
151
|
-
registerQuery,
|
|
152
|
-
unregisterQuery,
|
|
153
|
-
retryQuery,
|
|
154
|
-
getQueryState,
|
|
155
|
-
getQueryError,
|
|
156
|
-
getQueryResult,
|
|
157
|
-
onQueryStateChange,
|
|
158
|
-
executeQuery: async (selector, options) => {
|
|
159
|
-
await ready;
|
|
160
|
-
return this.executeQuery(collection.name, selector, options);
|
|
161
|
-
},
|
|
162
|
-
dispose: async () => {
|
|
163
|
-
this.storageAdapters.delete(collection.name);
|
|
164
|
-
this.queries.delete(collection.name);
|
|
165
|
-
this.collectionIndices.delete(collection.name);
|
|
166
|
-
this.storageAdapterReady.delete(collection.name);
|
|
167
|
-
},
|
|
168
|
-
isReady: async () => {
|
|
169
|
-
await ready;
|
|
170
|
-
}
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
async setupStorage(collectionName, indices) {
|
|
174
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
175
|
-
if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
176
|
-
await storage.setup();
|
|
177
|
-
await Promise.all(indices.map((index) => storage.createIndex(index)));
|
|
178
|
-
}
|
|
179
|
-
ensureStorageAdapter(name) {
|
|
180
|
-
if (this.storageAdapters.has(name)) return;
|
|
181
|
-
const adapter = this.options.storage(name);
|
|
182
|
-
if (!adapter) return;
|
|
183
|
-
this.storageAdapters.set(name, adapter);
|
|
184
|
-
}
|
|
185
|
-
/**
|
|
186
|
-
* Compute and publish the result for a specific query
|
|
187
|
-
* @param collectionName - name of the collection
|
|
188
|
-
* @param selector - query selector
|
|
189
|
-
* @param options - query options
|
|
190
|
-
*/
|
|
191
|
-
async fulfillQuery(collectionName, selector, options) {
|
|
192
|
-
const qid = require_queryId.default(selector, options);
|
|
193
|
-
const registry = this.queries.get(collectionName);
|
|
194
|
-
if (!registry) throw new Error(`Collection ${collectionName} not initialized!`);
|
|
195
|
-
if (!registry.get(qid)) return;
|
|
196
|
-
this.publishState(collectionName, qid, "active", null);
|
|
197
|
-
await this.runQuery(collectionName, selector, options);
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* Executes a query, retrying transient failures before giving up. The state
|
|
201
|
-
* stays `'active'` across retries — consumers should see "still loading",
|
|
202
|
-
* not "failed", until we actually stop trying. Only the final failure is
|
|
203
|
-
* published as `'error'` and reported through `onError`; previously that
|
|
204
|
-
* error was swallowed entirely (`fulfillQuery` caught it internally, so the
|
|
205
|
-
* `.catch(this.onError)` on its call site was unreachable) and the query
|
|
206
|
-
* stayed dead for the rest of the session.
|
|
207
|
-
* @param collectionName - name of the collection
|
|
208
|
-
* @param selector - query selector
|
|
209
|
-
* @param options - query options
|
|
210
|
-
*/
|
|
211
|
-
async runQuery(collectionName, selector, options) {
|
|
212
|
-
const qid = require_queryId.default(selector, options);
|
|
213
|
-
let lastError;
|
|
214
|
-
for (let attempt = 1; attempt <= this.retryAttempts; attempt += 1) {
|
|
215
|
-
if (!this.queries.get(collectionName)?.has(qid)) return;
|
|
216
|
-
try {
|
|
217
|
-
const items = await this.executeQuery(collectionName, selector, options);
|
|
218
|
-
this.publishResult(collectionName, qid, items);
|
|
219
|
-
this.publishState(collectionName, qid, "complete", null);
|
|
220
|
-
return;
|
|
221
|
-
} catch (error) {
|
|
222
|
-
lastError = error;
|
|
223
|
-
if (attempt < this.retryAttempts) await wait(this.retryDelay(attempt));
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
const queryError = new QueryError(collectionName, selector, options, this.retryAttempts, lastError);
|
|
227
|
-
this.publishState(collectionName, qid, "error", queryError);
|
|
228
|
-
this.onError(queryError);
|
|
229
|
-
}
|
|
230
|
-
/**
|
|
231
|
-
* Notify listeners about state changes and keep the cache updated
|
|
232
|
-
* @param collectionName - name of the collection
|
|
233
|
-
* @param qid - query id
|
|
234
|
-
* @param state - new state
|
|
235
|
-
* @param error - error if state is 'error', null otherwise
|
|
236
|
-
*/
|
|
237
|
-
publishState(collectionName, qid, state, error) {
|
|
238
|
-
const rec = this.queries.get(collectionName)?.get(qid);
|
|
239
|
-
if (!rec) return;
|
|
240
|
-
rec.state = state;
|
|
241
|
-
rec.error = error;
|
|
242
|
-
const subscribers = [...rec.listeners];
|
|
243
|
-
for (const callback of subscribers) try {
|
|
244
|
-
callback(state);
|
|
245
|
-
} catch (error_) {
|
|
246
|
-
this.onError(error_);
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
publishResult(collectionName, qid, items) {
|
|
250
|
-
const rec = this.queries.get(collectionName)?.get(qid);
|
|
251
|
-
if (!rec) return;
|
|
252
|
-
rec.items = items;
|
|
253
|
-
}
|
|
254
|
-
async getIndexInfo(collectionName, selector) {
|
|
255
|
-
const storageAdapter = this.storageAdapters.get(collectionName);
|
|
256
|
-
if (!storageAdapter) throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
257
|
-
if (selector != null && Object.keys(selector).length === 1 && "id" in selector && typeof selector.id !== "object") return {
|
|
258
|
-
matched: true,
|
|
259
|
-
ids: [selector.id].filter(Boolean),
|
|
260
|
-
optimizedSelector: {}
|
|
261
|
-
};
|
|
262
|
-
if (selector == null) return {
|
|
263
|
-
matched: false,
|
|
264
|
-
ids: [],
|
|
265
|
-
optimizedSelector: {}
|
|
266
|
-
};
|
|
267
|
-
return require_getIndexInfo.default((this.collectionIndices.get(collectionName) ?? []).map((field) => async (flatSelector) => {
|
|
268
|
-
if (!Object.hasOwnProperty.call(flatSelector, field)) return { matched: false };
|
|
269
|
-
const index = await storageAdapter.readIndex(field);
|
|
270
|
-
const fieldSelector = flatSelector[field];
|
|
271
|
-
const filtersForNull = fieldSelector == null || fieldSelector.$exists === false;
|
|
272
|
-
const keys = filtersForNull ? {
|
|
273
|
-
include: null,
|
|
274
|
-
exclude: [...index.keys()].filter((key) => key != null)
|
|
275
|
-
} : require_getMatchingKeys.default(field, flatSelector);
|
|
276
|
-
if (keys.include == null && keys.exclude == null) return { matched: false };
|
|
277
|
-
let includedIds = [];
|
|
278
|
-
if (keys.include == null) for (const set of index.values()) for (const pos of set) includedIds.push(pos);
|
|
279
|
-
else for (const key of keys.include) {
|
|
280
|
-
const idSet = index.get(key);
|
|
281
|
-
if (idSet) for (const id of idSet) includedIds.push(id);
|
|
282
|
-
}
|
|
283
|
-
if (keys.exclude != null) {
|
|
284
|
-
const excludeIds = /* @__PURE__ */ new Set();
|
|
285
|
-
for (const key of keys.exclude) {
|
|
286
|
-
const idSet = index.get(key);
|
|
287
|
-
if (idSet) for (const id of idSet) excludeIds.add(id);
|
|
288
|
-
}
|
|
289
|
-
includedIds = includedIds.filter((pos) => !excludeIds.has(pos));
|
|
290
|
-
}
|
|
291
|
-
return {
|
|
292
|
-
matched: true,
|
|
293
|
-
ids: includedIds,
|
|
294
|
-
fields: [field],
|
|
295
|
-
keepSelector: filtersForNull
|
|
296
|
-
};
|
|
297
|
-
}), selector);
|
|
298
|
-
}
|
|
299
|
-
async queryItems(collectionName, selector) {
|
|
300
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
301
|
-
if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
302
|
-
const index = await this.getIndexInfo(collectionName, selector);
|
|
303
|
-
const matchItems = (item) => {
|
|
304
|
-
if (index.optimizedSelector == null) return true;
|
|
305
|
-
if (Object.keys(index.optimizedSelector).length <= 0) return true;
|
|
306
|
-
return require_match.default(item, index.optimizedSelector);
|
|
307
|
-
};
|
|
308
|
-
if (index.matched) {
|
|
309
|
-
const items = await storage.readIds(index.ids);
|
|
310
|
-
if (require_isEqual.default(index.optimizedSelector, {})) return items;
|
|
311
|
-
return items.filter(matchItems);
|
|
312
|
-
} else {
|
|
313
|
-
const allItems = await storage.readAll();
|
|
314
|
-
if (require_isEqual.default(selector, {})) return allItems;
|
|
315
|
-
return allItems.filter(matchItems);
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
async executeQuery(collectionName, selector, options) {
|
|
319
|
-
const items = await this.queryItems(collectionName, selector || {});
|
|
320
|
-
const { sort, skip, limit, fields } = options || {};
|
|
321
|
-
const sorted = sort ? require_sortItems.default(items, sort) : items;
|
|
322
|
-
const skipped = skip ? sorted.slice(skip) : sorted;
|
|
323
|
-
const limited = limit ? skipped.slice(0, limit) : skipped;
|
|
324
|
-
const idExcluded = fields && fields.id === 0;
|
|
325
|
-
return limited.map((item) => {
|
|
326
|
-
if (!fields) return item;
|
|
327
|
-
return {
|
|
328
|
-
...idExcluded ? {} : { id: item.id },
|
|
329
|
-
...require_project.default(item, fields)
|
|
330
|
-
};
|
|
331
|
-
});
|
|
332
|
-
}
|
|
333
|
-
/**
|
|
334
|
-
* After mutations, recompute and push updates for affected active queries
|
|
335
|
-
* @param collectionName - name of the collection
|
|
336
|
-
* @param affectedItems - item states before and/or after the mutation
|
|
337
|
-
*/
|
|
338
|
-
async checkQueryUpdates(collectionName, affectedItems) {
|
|
339
|
-
const registry = this.queries.get(collectionName);
|
|
340
|
-
if (!registry) throw new Error(`Collection ${collectionName} not initialized!`);
|
|
341
|
-
if (registry.size === 0) return;
|
|
342
|
-
const affected = [...registry.values()].filter(({ selector }) => affectedItems.some((item) => require_match.default(item, selector)));
|
|
343
|
-
if (affected.length === 0) return;
|
|
344
|
-
for (const { selector, options } of affected) {
|
|
345
|
-
const qid = require_queryId.default(selector, options);
|
|
346
|
-
this.publishState(collectionName, qid, "active", null);
|
|
347
|
-
}
|
|
348
|
-
await Promise.all(affected.map(({ selector, options }) => this.runQuery(collectionName, selector, options)));
|
|
349
|
-
}
|
|
350
|
-
async insert(collectionName, newItem) {
|
|
351
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
352
|
-
if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
353
|
-
if ((await storage.readIds([newItem.id])).length > 0) throw new Error(`Item with id ${String(newItem.id)} already exists`);
|
|
354
|
-
await storage.insert([newItem]);
|
|
355
|
-
await this.checkQueryUpdates(collectionName, [newItem]);
|
|
356
|
-
return newItem;
|
|
357
|
-
}
|
|
358
|
-
async updateOne(collectionName, selector, modifier) {
|
|
359
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
360
|
-
if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
361
|
-
const [item] = await this.executeQuery(collectionName, selector, { limit: 1 });
|
|
362
|
-
const { $setOnInsert, ...rest } = modifier;
|
|
363
|
-
if (item == null) return [];
|
|
364
|
-
const modified = require_modify.default(require_deepClone.default(item), rest);
|
|
365
|
-
if (item.id !== modified.id) {
|
|
366
|
-
if ((await storage.readIds([modified.id])).length > 0) throw new Error(`Item with id ${String(modified.id)} already exists`);
|
|
367
|
-
}
|
|
368
|
-
await storage.replace([modified]);
|
|
369
|
-
await this.checkQueryUpdates(collectionName, [item, modified]);
|
|
370
|
-
return [modified];
|
|
371
|
-
}
|
|
372
|
-
async updateMany(collectionName, selector, modifier) {
|
|
373
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
374
|
-
if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
375
|
-
const items = await this.executeQuery(collectionName, selector);
|
|
376
|
-
if (items.length === 0) return [];
|
|
377
|
-
const { $setOnInsert, ...rest } = modifier;
|
|
378
|
-
const changed = await Promise.all(items.map(async (item) => {
|
|
379
|
-
const modified = require_modify.default(require_deepClone.default(item), rest);
|
|
380
|
-
if (item.id !== modified.id) {
|
|
381
|
-
if ((await storage.readIds([modified.id])).length > 0) throw new Error(`Item with id ${String(modified.id)} already exists`);
|
|
382
|
-
}
|
|
383
|
-
return modified;
|
|
384
|
-
}));
|
|
385
|
-
await storage.replace(changed);
|
|
386
|
-
await this.checkQueryUpdates(collectionName, [...items, ...changed]);
|
|
387
|
-
return changed;
|
|
388
|
-
}
|
|
389
|
-
async replaceOne(collectionName, selector, replacement) {
|
|
390
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
391
|
-
if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
392
|
-
const [item] = await this.executeQuery(collectionName, selector, { limit: 1 });
|
|
393
|
-
if (item == null) return [];
|
|
394
|
-
const modified = {
|
|
395
|
-
...replacement,
|
|
396
|
-
id: replacement.id ?? item.id
|
|
397
|
-
};
|
|
398
|
-
if (item.id !== modified.id) {
|
|
399
|
-
if ((await storage.readIds([modified.id])).length > 0) throw new Error(`Item with id ${String(modified.id)} already exists`);
|
|
400
|
-
}
|
|
401
|
-
await storage.replace([modified]);
|
|
402
|
-
await this.checkQueryUpdates(collectionName, [item, modified]);
|
|
403
|
-
return [modified];
|
|
404
|
-
}
|
|
405
|
-
async removeOne(collectionName, selector) {
|
|
406
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
407
|
-
if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
408
|
-
const [item] = await this.executeQuery(collectionName, selector, { limit: 1 });
|
|
409
|
-
if (item == null) return [];
|
|
410
|
-
await storage.remove([item]);
|
|
411
|
-
await this.checkQueryUpdates(collectionName, [item]);
|
|
412
|
-
return [item];
|
|
413
|
-
}
|
|
414
|
-
async removeMany(collectionName, selector) {
|
|
415
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
416
|
-
if (!storage) throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
417
|
-
const items = await this.executeQuery(collectionName, selector);
|
|
418
|
-
if (items.length === 0) return [];
|
|
419
|
-
await storage.remove(items);
|
|
420
|
-
await this.checkQueryUpdates(collectionName, items);
|
|
421
|
-
return items;
|
|
422
|
-
}
|
|
423
|
-
};
|
|
7
|
+
function createStorageAdapter(definition) {
|
|
8
|
+
return definition;
|
|
9
|
+
}
|
|
424
10
|
//#endregion
|
|
425
|
-
exports.default =
|
|
11
|
+
exports.default = createStorageAdapter;
|