@signaldb/core 2.0.0-beta.2 → 2.0.0-beta.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/.vite/manifest.json +183 -80
- package/dist/AsyncDataAdapter.d.ts +53 -7
- package/dist/AutoFetchDataAdapter.d.ts +4 -6
- package/dist/Collection/Cursor.d.ts +56 -4
- package/dist/Collection/Observer.d.ts +33 -0
- package/dist/Collection/index.d.ts +66 -19
- package/dist/Collection/types.d.ts +9 -2
- package/dist/DataAdapter.d.ts +24 -6
- package/dist/DefaultDataAdapter.d.ts +5 -5
- package/dist/WorkerDataAdapter.d.ts +57 -5
- package/dist/WorkerDataAdapterHost.d.ts +9 -7
- package/dist/createIndex.d.ts +2 -2
- package/dist/createIndexProvider.d.ts +2 -2
- package/dist/createReactivityAdapter.d.ts +2 -2
- package/dist/createStorageAdapter.d.ts +1 -1
- package/dist/getIndexInfo.d.ts +3 -4
- package/dist/index.cjs.js +33 -31
- package/dist/index.d.ts +6 -1
- package/dist/index.mjs +17 -32
- package/dist/index10.cjs.js +11 -0
- package/dist/index10.mjs +10 -18
- package/dist/index11.cjs.js +23 -0
- package/dist/index11.mjs +22 -15
- package/dist/index12.cjs.js +45 -0
- package/dist/index12.mjs +44 -353
- package/dist/index13.cjs.js +22 -0
- package/dist/index13.mjs +21 -419
- package/dist/index14.cjs.js +45 -0
- package/dist/index14.mjs +44 -206
- package/dist/index15.cjs.js +86 -0
- package/dist/index15.mjs +85 -388
- package/dist/index16.cjs.js +15 -0
- package/dist/index16.mjs +14 -579
- package/dist/index17.cjs.js +141 -0
- package/dist/index17.mjs +140 -152
- package/dist/index18.cjs.js +47 -0
- package/dist/index18.mjs +46 -26
- package/dist/index19.cjs.js +15 -0
- package/dist/index19.mjs +14 -41
- package/dist/index2.cjs.js +33 -0
- package/dist/index2.mjs +32 -258
- package/dist/index20.cjs.js +23 -0
- package/dist/index20.mjs +22 -16
- package/dist/index21.cjs.js +37 -0
- package/dist/index21.mjs +36 -104
- package/dist/index22.cjs.js +35 -0
- package/dist/index22.mjs +34 -128
- package/dist/index23.cjs.js +21 -0
- package/dist/index23.mjs +20 -7
- package/dist/index24.cjs.js +19 -0
- package/dist/index24.mjs +18 -27
- package/dist/index25.cjs.js +157 -0
- package/dist/index25.mjs +156 -11
- package/dist/index26.cjs.js +38 -0
- package/dist/index26.mjs +37 -29
- package/dist/index27.cjs.js +42 -0
- package/dist/index27.mjs +41 -46
- package/dist/index28.cjs.js +347 -0
- package/dist/index28.mjs +345 -7
- package/dist/index29.cjs.js +602 -0
- package/dist/index29.mjs +601 -7
- package/dist/index3.cjs.js +19 -0
- package/dist/index3.mjs +18 -556
- package/dist/index30.cjs.js +11 -0
- package/dist/index30.mjs +10 -42
- package/dist/index31.cjs.js +11 -0
- package/dist/index31.mjs +10 -29
- package/dist/index32.cjs.js +90 -0
- package/dist/index32.mjs +89 -10
- package/dist/index33.cjs.js +53 -0
- package/dist/index33.mjs +52 -5
- package/dist/index34.cjs.js +453 -0
- package/dist/index34.mjs +453 -0
- package/dist/index35.cjs.js +72 -0
- package/dist/index35.mjs +72 -0
- package/dist/index36.cjs.js +580 -0
- package/dist/index36.mjs +580 -0
- package/dist/index37.cjs.js +21 -0
- package/dist/index37.mjs +21 -0
- package/dist/index38.cjs.js +363 -0
- package/dist/index38.mjs +363 -0
- package/dist/index39.cjs.js +513 -0
- package/dist/index39.mjs +513 -0
- package/dist/index4.cjs.js +209 -0
- package/dist/index4.mjs +204 -5
- package/dist/index5.cjs.js +187 -0
- package/dist/index5.mjs +186 -5
- package/dist/index6.cjs.js +303 -0
- package/dist/index6.mjs +301 -28
- package/dist/index7.cjs.js +125 -0
- package/dist/index7.mjs +124 -12
- package/dist/index8.cjs.js +33 -0
- package/dist/index8.mjs +32 -5
- package/dist/index9.cjs.js +12 -0
- package/dist/index9.mjs +11 -133
- package/dist/types/IndexProvider.d.ts +2 -2
- package/dist/types/Modifier.d.ts +1 -1
- package/dist/types/ReactivityAdapter.d.ts +1 -1
- package/dist/types/StorageAdapter.d.ts +9 -1
- package/dist/utils/applyQueryOptions.d.ts +15 -0
- package/dist/utils/createSignal.d.ts +2 -2
- package/dist/utils/getMatchingKeys.d.ts +2 -2
- package/dist/utils/idIndexQuery.d.ts +20 -0
- package/dist/utils/incrementalQueryUpdate.d.ts +62 -0
- package/dist/utils/isFieldExpression.d.ts +1 -1
- package/dist/utils/match.d.ts +1 -1
- package/dist/utils/modify.d.ts +1 -1
- package/dist/utils/projectItems.d.ts +12 -0
- package/dist/utils/queryDelta.d.ts +83 -0
- package/dist/utils/queryId.d.ts +2 -2
- package/dist/utils/reactiveOrAsync.d.ts +59 -0
- package/dist/utils/storageIndexQuery.d.ts +20 -0
- package/package.json +1 -1
- package/dist/index.cjs10.js +0 -18
- package/dist/index.cjs11.js +0 -15
- package/dist/index.cjs12.js +0 -353
- package/dist/index.cjs13.js +0 -419
- package/dist/index.cjs14.js +0 -206
- package/dist/index.cjs15.js +0 -388
- package/dist/index.cjs16.js +0 -579
- package/dist/index.cjs17.js +0 -152
- package/dist/index.cjs18.js +0 -26
- package/dist/index.cjs19.js +0 -42
- package/dist/index.cjs2.js +0 -259
- package/dist/index.cjs20.js +0 -16
- package/dist/index.cjs21.js +0 -104
- package/dist/index.cjs22.js +0 -129
- package/dist/index.cjs23.js +0 -7
- package/dist/index.cjs24.js +0 -27
- package/dist/index.cjs25.js +0 -10
- package/dist/index.cjs26.js +0 -29
- package/dist/index.cjs27.js +0 -46
- package/dist/index.cjs28.js +0 -8
- package/dist/index.cjs29.js +0 -7
- package/dist/index.cjs3.js +0 -557
- package/dist/index.cjs30.js +0 -42
- package/dist/index.cjs31.js +0 -29
- package/dist/index.cjs32.js +0 -9
- package/dist/index.cjs33.js +0 -5
- package/dist/index.cjs4.js +0 -5
- package/dist/index.cjs5.js +0 -5
- package/dist/index.cjs6.js +0 -29
- package/dist/index.cjs7.js +0 -12
- package/dist/index.cjs8.js +0 -5
- package/dist/index.cjs9.js +0 -133
package/dist/index.cjs13.js
DELETED
|
@@ -1,419 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
const deepClone = require("./index.cjs19.js");
|
|
3
|
-
const match = require("./index.cjs23.js");
|
|
4
|
-
const modify = require("./index.cjs7.js");
|
|
5
|
-
const queryId = require("./index.cjs20.js");
|
|
6
|
-
const isEqual = require("./index.cjs6.js");
|
|
7
|
-
const getIndexInfo = require("./index.cjs22.js");
|
|
8
|
-
const getMatchingKeys = require("./index.cjs26.js");
|
|
9
|
-
const sortItems = require("./index.cjs25.js");
|
|
10
|
-
const project = require("./index.cjs24.js");
|
|
11
|
-
class AsyncDataAdapter {
|
|
12
|
-
options;
|
|
13
|
-
id;
|
|
14
|
-
onError;
|
|
15
|
-
// Per-collection resources
|
|
16
|
-
storageAdapters = /* @__PURE__ */ new Map();
|
|
17
|
-
storageAdapterReady = /* @__PURE__ */ new Map();
|
|
18
|
-
collectionIndices = /* @__PURE__ */ new Map();
|
|
19
|
-
// Per-collection query registries
|
|
20
|
-
queries = /* @__PURE__ */ new Map();
|
|
21
|
-
constructor(options) {
|
|
22
|
-
this.options = options;
|
|
23
|
-
this.id = options.id || "async-data-adapter";
|
|
24
|
-
this.onError = options.onError ?? ((error) => {
|
|
25
|
-
console.error(error);
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
createCollectionBackend(collection, indices) {
|
|
29
|
-
this.collectionIndices.set(collection.name, indices);
|
|
30
|
-
this.queries.set(collection.name, /* @__PURE__ */ new Map());
|
|
31
|
-
this.ensureStorageAdapter(collection.name);
|
|
32
|
-
const ready = (async () => {
|
|
33
|
-
try {
|
|
34
|
-
await this.setupStorage(collection.name, indices);
|
|
35
|
-
} catch (error) {
|
|
36
|
-
this.onError(error);
|
|
37
|
-
throw error;
|
|
38
|
-
}
|
|
39
|
-
})();
|
|
40
|
-
this.storageAdapterReady.set(collection.name, ready);
|
|
41
|
-
const registerQuery = (selector, options) => {
|
|
42
|
-
const qid = queryId(selector, options);
|
|
43
|
-
const registry = this.queries.get(collection.name);
|
|
44
|
-
if (!registry)
|
|
45
|
-
throw new Error(`Collection ${collection.name} not initialized!`);
|
|
46
|
-
registry.set(qid, {
|
|
47
|
-
selector,
|
|
48
|
-
options,
|
|
49
|
-
items: [],
|
|
50
|
-
listeners: /* @__PURE__ */ new Set(),
|
|
51
|
-
...registry.get(qid),
|
|
52
|
-
state: "active",
|
|
53
|
-
error: null
|
|
54
|
-
});
|
|
55
|
-
void this.fulfillQuery(collection.name, selector, options).catch(this.onError);
|
|
56
|
-
};
|
|
57
|
-
const unregisterQuery = (selector, options) => {
|
|
58
|
-
const qid = queryId(selector, options);
|
|
59
|
-
this.queries.get(collection.name)?.delete(qid);
|
|
60
|
-
};
|
|
61
|
-
const getQueryState = (selector, options) => {
|
|
62
|
-
const q = this.queries.get(collection.name)?.get(queryId(selector, options));
|
|
63
|
-
return q?.state ?? "active";
|
|
64
|
-
};
|
|
65
|
-
const getQueryError = (selector, options) => {
|
|
66
|
-
const q = this.queries.get(collection.name)?.get(queryId(selector, options));
|
|
67
|
-
return q?.error ?? null;
|
|
68
|
-
};
|
|
69
|
-
const getQueryResult = (selector, options) => {
|
|
70
|
-
const q = this.queries.get(collection.name)?.get(queryId(selector, options));
|
|
71
|
-
return q?.items ?? [];
|
|
72
|
-
};
|
|
73
|
-
const onQueryStateChange = (selector, options, callback) => {
|
|
74
|
-
const qid = queryId(selector, options);
|
|
75
|
-
const registry = this.queries.get(collection.name);
|
|
76
|
-
if (!registry)
|
|
77
|
-
throw new Error(`Collection ${collection.name} not initialized!`);
|
|
78
|
-
if (!registry.has(qid)) {
|
|
79
|
-
registry.set(qid, {
|
|
80
|
-
selector,
|
|
81
|
-
options,
|
|
82
|
-
state: "active",
|
|
83
|
-
error: null,
|
|
84
|
-
items: [],
|
|
85
|
-
listeners: /* @__PURE__ */ new Set()
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
registry.get(qid)?.listeners.add(callback);
|
|
89
|
-
return () => registry.get(qid)?.listeners.delete(callback);
|
|
90
|
-
};
|
|
91
|
-
return {
|
|
92
|
-
insert: async (item) => {
|
|
93
|
-
await ready;
|
|
94
|
-
const inserted = await this.insert(collection.name, item);
|
|
95
|
-
return inserted;
|
|
96
|
-
},
|
|
97
|
-
updateOne: async (selector, modifier) => {
|
|
98
|
-
await ready;
|
|
99
|
-
return this.updateOne(collection.name, selector, modifier);
|
|
100
|
-
},
|
|
101
|
-
updateMany: async (selector, modifier) => {
|
|
102
|
-
await ready;
|
|
103
|
-
return this.updateMany(collection.name, selector, modifier);
|
|
104
|
-
},
|
|
105
|
-
replaceOne: async (selector, replacement) => {
|
|
106
|
-
await ready;
|
|
107
|
-
return this.replaceOne(collection.name, selector, replacement);
|
|
108
|
-
},
|
|
109
|
-
removeOne: async (selector) => {
|
|
110
|
-
await ready;
|
|
111
|
-
return this.removeOne(collection.name, selector);
|
|
112
|
-
},
|
|
113
|
-
removeMany: async (selector) => {
|
|
114
|
-
await ready;
|
|
115
|
-
return this.removeMany(collection.name, selector);
|
|
116
|
-
},
|
|
117
|
-
registerQuery,
|
|
118
|
-
unregisterQuery,
|
|
119
|
-
getQueryState,
|
|
120
|
-
getQueryError,
|
|
121
|
-
getQueryResult,
|
|
122
|
-
onQueryStateChange,
|
|
123
|
-
executeQuery: async (selector, options) => {
|
|
124
|
-
await ready;
|
|
125
|
-
registerQuery(selector, options);
|
|
126
|
-
const result = getQueryResult(selector, options);
|
|
127
|
-
unregisterQuery(selector, options);
|
|
128
|
-
return result;
|
|
129
|
-
},
|
|
130
|
-
dispose: async () => {
|
|
131
|
-
this.storageAdapters.delete(collection.name);
|
|
132
|
-
this.queries.delete(collection.name);
|
|
133
|
-
this.collectionIndices.delete(collection.name);
|
|
134
|
-
this.storageAdapterReady.delete(collection.name);
|
|
135
|
-
},
|
|
136
|
-
isReady: async () => {
|
|
137
|
-
await ready;
|
|
138
|
-
}
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
async setupStorage(collectionName, indices) {
|
|
142
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
143
|
-
if (!storage)
|
|
144
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
145
|
-
await storage.setup();
|
|
146
|
-
await Promise.all(indices.map((index) => storage.createIndex(index)));
|
|
147
|
-
}
|
|
148
|
-
ensureStorageAdapter(name) {
|
|
149
|
-
if (this.storageAdapters.has(name))
|
|
150
|
-
return;
|
|
151
|
-
const adapter = this.options.storage(name);
|
|
152
|
-
if (!adapter)
|
|
153
|
-
return;
|
|
154
|
-
this.storageAdapters.set(name, adapter);
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* Compute and publish the result for a specific query
|
|
158
|
-
* @param collectionName - name of the collection
|
|
159
|
-
* @param selector - query selector
|
|
160
|
-
* @param options - query options
|
|
161
|
-
*/
|
|
162
|
-
async fulfillQuery(collectionName, selector, options) {
|
|
163
|
-
const qid = queryId(selector, options);
|
|
164
|
-
const registry = this.queries.get(collectionName);
|
|
165
|
-
if (!registry)
|
|
166
|
-
throw new Error(`Collection ${collectionName} not initialized!`);
|
|
167
|
-
const rec = registry.get(qid);
|
|
168
|
-
if (!rec)
|
|
169
|
-
return;
|
|
170
|
-
this.publishState(collectionName, qid, "active", null);
|
|
171
|
-
try {
|
|
172
|
-
const items = await this.executeQuery(collectionName, selector, options);
|
|
173
|
-
this.publishResult(collectionName, qid, items);
|
|
174
|
-
this.publishState(collectionName, qid, "complete", null);
|
|
175
|
-
} catch (error) {
|
|
176
|
-
this.publishState(collectionName, qid, "error", error);
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
/**
|
|
180
|
-
* Notify listeners about state changes and keep the cache updated
|
|
181
|
-
* @param collectionName - name of the collection
|
|
182
|
-
* @param qid - query id
|
|
183
|
-
* @param state - new state
|
|
184
|
-
* @param error - error if state is 'error', null otherwise
|
|
185
|
-
*/
|
|
186
|
-
publishState(collectionName, qid, state, error) {
|
|
187
|
-
const rec = this.queries.get(collectionName)?.get(qid);
|
|
188
|
-
if (!rec)
|
|
189
|
-
return;
|
|
190
|
-
rec.state = state;
|
|
191
|
-
rec.error = error;
|
|
192
|
-
for (const callback of rec.listeners) {
|
|
193
|
-
try {
|
|
194
|
-
callback(state);
|
|
195
|
-
} catch (error_) {
|
|
196
|
-
this.onError(error_);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
publishResult(collectionName, qid, items) {
|
|
201
|
-
const rec = this.queries.get(collectionName)?.get(qid);
|
|
202
|
-
if (!rec)
|
|
203
|
-
return;
|
|
204
|
-
rec.items = items;
|
|
205
|
-
}
|
|
206
|
-
async getIndexInfo(collectionName, selector) {
|
|
207
|
-
const storageAdapter = this.storageAdapters.get(collectionName);
|
|
208
|
-
if (!storageAdapter)
|
|
209
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
210
|
-
if (selector != null && Object.keys(selector).length === 1 && "id" in selector && typeof selector.id !== "object") {
|
|
211
|
-
return {
|
|
212
|
-
matched: true,
|
|
213
|
-
ids: [selector.id].filter(Boolean),
|
|
214
|
-
optimizedSelector: {}
|
|
215
|
-
};
|
|
216
|
-
}
|
|
217
|
-
if (selector == null) {
|
|
218
|
-
return {
|
|
219
|
-
matched: false,
|
|
220
|
-
ids: [],
|
|
221
|
-
optimizedSelector: {}
|
|
222
|
-
};
|
|
223
|
-
}
|
|
224
|
-
const indices = this.collectionIndices.get(collectionName) ?? [];
|
|
225
|
-
return getIndexInfo.default(indices.map((field) => async (flatSelector) => {
|
|
226
|
-
if (!Object.hasOwnProperty.call(flatSelector, field))
|
|
227
|
-
return { matched: false };
|
|
228
|
-
const index = await storageAdapter.readIndex(field);
|
|
229
|
-
const fieldSelector = flatSelector[field];
|
|
230
|
-
const filtersForNull = fieldSelector == null || fieldSelector.$exists === false;
|
|
231
|
-
const keys = filtersForNull ? { include: null, exclude: [...index.keys()].filter((key) => key != null) } : getMatchingKeys(field, flatSelector);
|
|
232
|
-
if (keys.include == null && keys.exclude == null)
|
|
233
|
-
return { matched: false };
|
|
234
|
-
let includedIds = [];
|
|
235
|
-
if (keys.include == null) {
|
|
236
|
-
for (const set of index.values())
|
|
237
|
-
for (const pos of set)
|
|
238
|
-
includedIds.push(pos);
|
|
239
|
-
} else {
|
|
240
|
-
for (const key of keys.include) {
|
|
241
|
-
const idSet = index.get(key);
|
|
242
|
-
if (idSet)
|
|
243
|
-
for (const id of idSet)
|
|
244
|
-
includedIds.push(id);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
if (keys.exclude != null) {
|
|
248
|
-
const excludeIds = /* @__PURE__ */ new Set();
|
|
249
|
-
for (const key of keys.exclude) {
|
|
250
|
-
const idSet = index.get(key);
|
|
251
|
-
if (idSet)
|
|
252
|
-
for (const id of idSet)
|
|
253
|
-
excludeIds.add(id);
|
|
254
|
-
}
|
|
255
|
-
includedIds = includedIds.filter((pos) => !excludeIds.has(pos));
|
|
256
|
-
}
|
|
257
|
-
return { matched: true, ids: includedIds, fields: [field], keepSelector: filtersForNull };
|
|
258
|
-
}), selector);
|
|
259
|
-
}
|
|
260
|
-
async queryItems(collectionName, selector) {
|
|
261
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
262
|
-
if (!storage)
|
|
263
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
264
|
-
const index = await this.getIndexInfo(collectionName, selector);
|
|
265
|
-
const matchItems = (item) => {
|
|
266
|
-
if (index.optimizedSelector == null)
|
|
267
|
-
return true;
|
|
268
|
-
if (Object.keys(index.optimizedSelector).length <= 0)
|
|
269
|
-
return true;
|
|
270
|
-
return match(item, index.optimizedSelector);
|
|
271
|
-
};
|
|
272
|
-
if (index.matched) {
|
|
273
|
-
const items = await storage.readIds(index.ids);
|
|
274
|
-
if (isEqual(index.optimizedSelector, {}))
|
|
275
|
-
return items;
|
|
276
|
-
return items.filter(matchItems);
|
|
277
|
-
} else {
|
|
278
|
-
const allItems = await storage.readAll();
|
|
279
|
-
if (isEqual(selector, {}))
|
|
280
|
-
return allItems;
|
|
281
|
-
return allItems.filter(matchItems);
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
async executeQuery(collectionName, selector, options) {
|
|
285
|
-
const items = await this.queryItems(collectionName, selector || {});
|
|
286
|
-
const { sort, skip, limit, fields } = options || {};
|
|
287
|
-
const sorted = sort ? sortItems(items, sort) : items;
|
|
288
|
-
const skipped = skip ? sorted.slice(skip) : sorted;
|
|
289
|
-
const limited = limit ? skipped.slice(0, limit) : skipped;
|
|
290
|
-
const idExcluded = fields && fields.id === 0;
|
|
291
|
-
return limited.map((item) => {
|
|
292
|
-
if (!fields)
|
|
293
|
-
return item;
|
|
294
|
-
return { ...idExcluded ? {} : { id: item.id }, ...project(item, fields) };
|
|
295
|
-
});
|
|
296
|
-
}
|
|
297
|
-
/**
|
|
298
|
-
* After mutations, recompute and push updates for affected active queries
|
|
299
|
-
* @param collectionName - name of the collection
|
|
300
|
-
* @param changedItems - items that were inserted/updated/replaced/removed
|
|
301
|
-
*/
|
|
302
|
-
async checkQueryUpdates(collectionName, changedItems) {
|
|
303
|
-
const registry = this.queries.get(collectionName);
|
|
304
|
-
if (!registry)
|
|
305
|
-
throw new Error(`Collection ${collectionName} not initialized!`);
|
|
306
|
-
if (registry.size === 0)
|
|
307
|
-
return;
|
|
308
|
-
const affected = [...registry.values()].filter(({ selector }) => changedItems.some((item) => match(item, selector)));
|
|
309
|
-
if (affected.length === 0)
|
|
310
|
-
return;
|
|
311
|
-
for (const { selector, options } of affected) {
|
|
312
|
-
const qid = queryId(selector, options);
|
|
313
|
-
this.publishState(collectionName, qid, "active", null);
|
|
314
|
-
}
|
|
315
|
-
await Promise.all(affected.map(async ({ selector, options }) => {
|
|
316
|
-
const qid = queryId(selector, options);
|
|
317
|
-
try {
|
|
318
|
-
const items = await this.executeQuery(collectionName, selector, options);
|
|
319
|
-
this.publishResult(collectionName, qid, items);
|
|
320
|
-
this.publishState(collectionName, qid, "complete", null);
|
|
321
|
-
} catch (error) {
|
|
322
|
-
this.publishState(collectionName, qid, "error", error);
|
|
323
|
-
}
|
|
324
|
-
}));
|
|
325
|
-
}
|
|
326
|
-
async insert(collectionName, newItem) {
|
|
327
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
328
|
-
if (!storage)
|
|
329
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
330
|
-
const existingItems = await storage.readIds([newItem.id]);
|
|
331
|
-
if (existingItems.length > 0)
|
|
332
|
-
throw new Error(`Item with id ${String(newItem.id)} already exists`);
|
|
333
|
-
await storage.insert([newItem]);
|
|
334
|
-
await this.checkQueryUpdates(collectionName, [newItem]);
|
|
335
|
-
return newItem;
|
|
336
|
-
}
|
|
337
|
-
async updateOne(collectionName, selector, modifier) {
|
|
338
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
339
|
-
if (!storage)
|
|
340
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
341
|
-
const items = await this.executeQuery(collectionName, selector, { limit: 1 });
|
|
342
|
-
const [item] = items;
|
|
343
|
-
const { $setOnInsert, ...rest } = modifier;
|
|
344
|
-
if (item == null)
|
|
345
|
-
return [];
|
|
346
|
-
const modified = modify(deepClone.default(item), rest);
|
|
347
|
-
if (item.id !== modified.id) {
|
|
348
|
-
const existing = await this.executeQuery(collectionName, { id: modified.id }, { limit: 1 });
|
|
349
|
-
if (existing.length > 0)
|
|
350
|
-
throw new Error(`Item with id ${String(modified.id)} already exists`);
|
|
351
|
-
}
|
|
352
|
-
await storage.replace([modified]);
|
|
353
|
-
await this.checkQueryUpdates(collectionName, [modified]);
|
|
354
|
-
return [modified];
|
|
355
|
-
}
|
|
356
|
-
async updateMany(collectionName, selector, modifier) {
|
|
357
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
358
|
-
if (!storage)
|
|
359
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
360
|
-
const items = await this.executeQuery(collectionName, selector);
|
|
361
|
-
if (items.length === 0)
|
|
362
|
-
return [];
|
|
363
|
-
const { $setOnInsert, ...rest } = modifier;
|
|
364
|
-
const changed = await Promise.all(items.map(async (item) => {
|
|
365
|
-
const modified = modify(deepClone.default(item), rest);
|
|
366
|
-
if (item.id !== modified.id) {
|
|
367
|
-
const existing = await this.executeQuery(collectionName, { id: modified.id }, { limit: 1 });
|
|
368
|
-
if (existing.length > 0)
|
|
369
|
-
throw new Error(`Item with id ${String(modified.id)} already exists`);
|
|
370
|
-
}
|
|
371
|
-
return modified;
|
|
372
|
-
}));
|
|
373
|
-
await storage.replace(changed);
|
|
374
|
-
await this.checkQueryUpdates(collectionName, changed);
|
|
375
|
-
return changed;
|
|
376
|
-
}
|
|
377
|
-
async replaceOne(collectionName, selector, replacement) {
|
|
378
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
379
|
-
if (!storage)
|
|
380
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
381
|
-
const items = await this.executeQuery(collectionName, selector, { limit: 1 });
|
|
382
|
-
const [item] = items;
|
|
383
|
-
if (item == null)
|
|
384
|
-
return [];
|
|
385
|
-
const modified = { ...replacement, id: replacement.id ?? item.id };
|
|
386
|
-
if (item.id !== modified.id) {
|
|
387
|
-
const existing = await this.executeQuery(collectionName, { id: modified.id }, { limit: 1 });
|
|
388
|
-
if (existing.length > 0)
|
|
389
|
-
throw new Error(`Item with id ${String(modified.id)} already exists`);
|
|
390
|
-
}
|
|
391
|
-
await storage.replace([modified]);
|
|
392
|
-
await this.checkQueryUpdates(collectionName, [modified]);
|
|
393
|
-
return [modified];
|
|
394
|
-
}
|
|
395
|
-
async removeOne(collectionName, selector) {
|
|
396
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
397
|
-
if (!storage)
|
|
398
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
399
|
-
const items = await this.executeQuery(collectionName, selector, { limit: 1 });
|
|
400
|
-
const [item] = items;
|
|
401
|
-
if (item == null)
|
|
402
|
-
return [];
|
|
403
|
-
await storage.remove([item]);
|
|
404
|
-
await this.checkQueryUpdates(collectionName, [item]);
|
|
405
|
-
return [item];
|
|
406
|
-
}
|
|
407
|
-
async removeMany(collectionName, selector) {
|
|
408
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
409
|
-
if (!storage)
|
|
410
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
411
|
-
const items = await this.executeQuery(collectionName, selector);
|
|
412
|
-
if (items.length === 0)
|
|
413
|
-
return [];
|
|
414
|
-
await storage.remove(items);
|
|
415
|
-
await this.checkQueryUpdates(collectionName, items);
|
|
416
|
-
return items;
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
module.exports = AsyncDataAdapter;
|
package/dist/index.cjs14.js
DELETED
|
@@ -1,206 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
const queryId = require("./index.cjs20.js");
|
|
3
|
-
const randomId = require("./index.cjs8.js");
|
|
4
|
-
const batchOnNextTick = require("./index.cjs27.js");
|
|
5
|
-
class WorkerDataAdapter {
|
|
6
|
-
worker;
|
|
7
|
-
options;
|
|
8
|
-
id;
|
|
9
|
-
isDisposed = false;
|
|
10
|
-
workerReady;
|
|
11
|
-
log = () => {
|
|
12
|
-
};
|
|
13
|
-
collectionReady = /* @__PURE__ */ new Map();
|
|
14
|
-
batchExecutionHelpers = /* @__PURE__ */ new Map();
|
|
15
|
-
queries = {};
|
|
16
|
-
constructor(worker, options) {
|
|
17
|
-
this.worker = worker;
|
|
18
|
-
this.options = options;
|
|
19
|
-
this.id = this.options.id || "default-worker-data-adapter";
|
|
20
|
-
if (this.options.log)
|
|
21
|
-
this.log = this.options.log;
|
|
22
|
-
this.workerReady = new Promise((resolve, reject) => {
|
|
23
|
-
const timeoutId = setTimeout(() => {
|
|
24
|
-
reject(new Error("WorkerDataAdapter initialization timed out"));
|
|
25
|
-
}, 5e3);
|
|
26
|
-
const handleMessage = (event) => {
|
|
27
|
-
const { type, workerId } = event.data;
|
|
28
|
-
if (workerId !== this.id)
|
|
29
|
-
return;
|
|
30
|
-
if (type === "ready") {
|
|
31
|
-
resolve();
|
|
32
|
-
clearTimeout(timeoutId);
|
|
33
|
-
this.worker.removeEventListener("message", handleMessage);
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
this.worker.addEventListener("message", handleMessage);
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
async exec(method, collectionName, ...args) {
|
|
40
|
-
await this.workerReady;
|
|
41
|
-
if (method !== "isReady") {
|
|
42
|
-
const collectionReady = this.collectionReady.get(collectionName);
|
|
43
|
-
if (!collectionReady)
|
|
44
|
-
throw new Error(`Collection "${collectionName}" is not registered in WorkerDataAdapter`);
|
|
45
|
-
await collectionReady;
|
|
46
|
-
}
|
|
47
|
-
if (this.isDisposed) {
|
|
48
|
-
throw new Error("WorkerDataAdapter is disposed");
|
|
49
|
-
}
|
|
50
|
-
return new Promise((resolve, reject) => {
|
|
51
|
-
const messageId = randomId();
|
|
52
|
-
const handleMessage = (event) => {
|
|
53
|
-
const { id, workerId, type, data, error } = event.data;
|
|
54
|
-
if (workerId !== this.id)
|
|
55
|
-
return;
|
|
56
|
-
if (type !== "response")
|
|
57
|
-
return;
|
|
58
|
-
if (id !== messageId)
|
|
59
|
-
return;
|
|
60
|
-
this.log(method, "result", data ?? error);
|
|
61
|
-
if (error) {
|
|
62
|
-
reject(error);
|
|
63
|
-
} else {
|
|
64
|
-
resolve(data);
|
|
65
|
-
}
|
|
66
|
-
this.worker.removeEventListener("message", handleMessage);
|
|
67
|
-
};
|
|
68
|
-
this.worker.addEventListener("message", handleMessage);
|
|
69
|
-
this.worker.postMessage({
|
|
70
|
-
id: messageId,
|
|
71
|
-
workerId: this.id,
|
|
72
|
-
method,
|
|
73
|
-
args: [collectionName, ...args]
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
enqueueBatched(collectionName, method, args) {
|
|
78
|
-
const helper = this.batchExecutionHelpers.get(collectionName);
|
|
79
|
-
if (!helper)
|
|
80
|
-
throw new Error(`Collection "${collectionName}" is not registered in WorkerDataAdapter`);
|
|
81
|
-
return helper.enqueue(method, args);
|
|
82
|
-
}
|
|
83
|
-
// ---------- end batching integration ----------
|
|
84
|
-
updateQuery(collectionName, query, update) {
|
|
85
|
-
const id = queryId(query.selector, query.options);
|
|
86
|
-
const collectionQueries = this.queries[collectionName];
|
|
87
|
-
if (!collectionQueries)
|
|
88
|
-
return;
|
|
89
|
-
const existing = collectionQueries.get(id);
|
|
90
|
-
const newState = {
|
|
91
|
-
state: "active",
|
|
92
|
-
error: null,
|
|
93
|
-
items: [],
|
|
94
|
-
stateChangeCallbacks: [],
|
|
95
|
-
eventHandler: existing?.eventHandler,
|
|
96
|
-
...existing,
|
|
97
|
-
...update
|
|
98
|
-
};
|
|
99
|
-
collectionQueries.set(id, newState);
|
|
100
|
-
this.queries[collectionName] = collectionQueries;
|
|
101
|
-
}
|
|
102
|
-
createCollectionBackend(collection, indices) {
|
|
103
|
-
this.queries[collection.name] = /* @__PURE__ */ new Map();
|
|
104
|
-
void this.exec("registerCollection", collection.name, indices);
|
|
105
|
-
this.collectionReady.set(collection.name, this.exec("isReady", collection.name));
|
|
106
|
-
this.batchExecutionHelpers.set(collection.name, batchOnNextTick(async (method, args) => this.exec(method, collection.name, args)));
|
|
107
|
-
return {
|
|
108
|
-
insert: async (item) => {
|
|
109
|
-
return this.enqueueBatched(collection.name, "insert", [item]);
|
|
110
|
-
},
|
|
111
|
-
updateOne: async (selector, modifier) => {
|
|
112
|
-
return this.enqueueBatched(collection.name, "updateOne", [selector, modifier]);
|
|
113
|
-
},
|
|
114
|
-
updateMany: async (selector, modifier) => {
|
|
115
|
-
return this.enqueueBatched(collection.name, "updateMany", [selector, modifier]);
|
|
116
|
-
},
|
|
117
|
-
replaceOne: async (selector, replacement) => {
|
|
118
|
-
return this.enqueueBatched(collection.name, "replaceOne", [selector, replacement]);
|
|
119
|
-
},
|
|
120
|
-
removeOne: async (selector) => {
|
|
121
|
-
return this.enqueueBatched(collection.name, "removeOne", [selector]);
|
|
122
|
-
},
|
|
123
|
-
removeMany: async (selector) => {
|
|
124
|
-
return this.enqueueBatched(collection.name, "removeMany", [selector]);
|
|
125
|
-
},
|
|
126
|
-
// methods for registering and unregistering queries that will be called from the collection during find/findOne
|
|
127
|
-
registerQuery: (selector, options) => {
|
|
128
|
-
this.updateQuery(collection.name, { selector, options }, { state: "active", error: null, items: [] });
|
|
129
|
-
void this.exec("registerQuery", collection.name, selector, options);
|
|
130
|
-
const handler = (event) => {
|
|
131
|
-
const { type, data, workerId, error } = event.data;
|
|
132
|
-
if (type !== "queryUpdate")
|
|
133
|
-
return;
|
|
134
|
-
if (data == null)
|
|
135
|
-
return;
|
|
136
|
-
const { collectionName, selector: responseSelector, options: responseOptions, state, items } = data;
|
|
137
|
-
if (workerId !== this.id)
|
|
138
|
-
return;
|
|
139
|
-
if (collectionName !== collection.name)
|
|
140
|
-
return;
|
|
141
|
-
if (queryId(responseSelector, responseOptions) !== queryId(selector, options))
|
|
142
|
-
return;
|
|
143
|
-
this.log("queryUpdate", responseSelector, responseOptions, state, data ?? error);
|
|
144
|
-
this.updateQuery(collection.name, {
|
|
145
|
-
selector: responseSelector,
|
|
146
|
-
options: responseOptions
|
|
147
|
-
}, { state, error, items });
|
|
148
|
-
const query = this.queries[collection.name]?.get(queryId(selector, options));
|
|
149
|
-
if (!query)
|
|
150
|
-
return;
|
|
151
|
-
query.stateChangeCallbacks.forEach((callback) => callback(state));
|
|
152
|
-
};
|
|
153
|
-
this.worker.addEventListener("message", handler);
|
|
154
|
-
this.updateQuery(collection.name, { selector, options }, { eventHandler: handler });
|
|
155
|
-
},
|
|
156
|
-
unregisterQuery: (selector, options) => {
|
|
157
|
-
const qid = queryId(selector, options);
|
|
158
|
-
const query = this.queries[collection.name]?.get(qid);
|
|
159
|
-
if (query?.eventHandler) {
|
|
160
|
-
this.worker.removeEventListener("message", query.eventHandler);
|
|
161
|
-
}
|
|
162
|
-
this.queries[collection.name]?.delete(qid);
|
|
163
|
-
void this.exec("unregisterQuery", collection.name, selector, options);
|
|
164
|
-
},
|
|
165
|
-
getQueryState: (selector, options) => {
|
|
166
|
-
const query = this.queries[collection.name]?.get(queryId(selector, options));
|
|
167
|
-
return query?.state || "active";
|
|
168
|
-
},
|
|
169
|
-
getQueryError: (selector, options) => {
|
|
170
|
-
const query = this.queries[collection.name]?.get(queryId(selector, options));
|
|
171
|
-
return query?.error || null;
|
|
172
|
-
},
|
|
173
|
-
getQueryResult: (selector, options) => {
|
|
174
|
-
const query = this.queries[collection.name]?.get(queryId(selector, options));
|
|
175
|
-
return query?.items || [];
|
|
176
|
-
},
|
|
177
|
-
onQueryStateChange: (selector, options, callback) => {
|
|
178
|
-
this.updateQuery(collection.name, { selector, options }, {
|
|
179
|
-
stateChangeCallbacks: [
|
|
180
|
-
...this.queries[collection.name]?.get(queryId(selector, options))?.stateChangeCallbacks || [],
|
|
181
|
-
callback
|
|
182
|
-
]
|
|
183
|
-
});
|
|
184
|
-
return () => {
|
|
185
|
-
const currentCallbacks = this.queries[collection.name]?.get(queryId(selector, options))?.stateChangeCallbacks;
|
|
186
|
-
if (!currentCallbacks)
|
|
187
|
-
throw new Error("State change callbacks are not defined!");
|
|
188
|
-
this.updateQuery(collection.name, { selector, options }, {
|
|
189
|
-
stateChangeCallbacks: currentCallbacks.filter((existingCallback) => existingCallback !== callback)
|
|
190
|
-
});
|
|
191
|
-
};
|
|
192
|
-
},
|
|
193
|
-
executeQuery: (selector, options) => this.exec("executeQuery", collection.name, selector, options),
|
|
194
|
-
// lifecycle methods
|
|
195
|
-
dispose: async () => {
|
|
196
|
-
await this.exec("unregisterCollection", collection.name);
|
|
197
|
-
this.isDisposed = true;
|
|
198
|
-
this.worker.terminate();
|
|
199
|
-
},
|
|
200
|
-
isReady: async () => {
|
|
201
|
-
await this.exec("isReady", collection.name);
|
|
202
|
-
}
|
|
203
|
-
};
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
module.exports = WorkerDataAdapter;
|