@signaldb/core 1.7.0 → 2.0.0-beta.0
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 +125 -82
- package/dist/AsyncDataAdapter.d.ts +64 -0
- package/dist/AutoFetchDataAdapter.d.ts +113 -0
- package/dist/Collection/Cursor.d.ts +9 -9
- package/dist/Collection/Observer.d.ts +3 -2
- package/dist/Collection/index.d.ts +52 -46
- package/dist/Collection/types.d.ts +4 -9
- package/dist/DataAdapter.d.ts +33 -0
- package/dist/DefaultDataAdapter.d.ts +32 -0
- package/dist/WorkerDataAdapter.d.ts +22 -0
- package/dist/WorkerDataAdapterHost.d.ts +59 -0
- package/dist/createIndex.d.ts +7 -0
- package/dist/createStorageAdapter.d.ts +9 -0
- package/dist/{Collection/getIndexInfo.d.ts → getIndexInfo.d.ts} +17 -19
- package/dist/index.cjs.js +21 -17
- package/dist/index.cjs10.js +15 -26
- package/dist/index.cjs11.js +13 -10
- package/dist/index.cjs12.js +337 -3
- package/dist/index.cjs13.js +384 -115
- package/dist/index.cjs14.js +183 -67
- package/dist/index.cjs15.js +339 -8
- package/dist/index.cjs16.js +563 -20
- package/dist/index.cjs17.js +16 -12
- package/dist/index.cjs18.js +24 -5
- package/dist/index.cjs2.js +27 -36
- package/dist/index.cjs20.js +102 -13
- package/dist/index.cjs21.js +124 -21
- package/dist/index.cjs22.js +5 -68
- package/dist/index.cjs23.js +22 -82
- package/dist/index.cjs24.js +5 -16
- package/dist/index.cjs25.js +8 -27
- package/dist/index.cjs26.js +27 -7
- package/dist/index.cjs27.js +3 -5
- package/dist/index.cjs28.js +5 -27
- package/dist/index.cjs29.js +27 -40
- package/dist/index.cjs3.js +242 -497
- package/dist/index.cjs30.js +42 -0
- package/dist/index.cjs31.js +9 -0
- package/dist/index.cjs4.js +3 -170
- package/dist/index.cjs5.js +3 -67
- package/dist/index.cjs6.js +27 -3
- package/dist/index.cjs7.js +10 -3
- package/dist/index.cjs8.js +3 -3
- package/dist/index.cjs9.js +131 -3
- package/dist/index.d.ts +10 -8
- package/dist/index.mjs +15 -11
- package/dist/index10.mjs +15 -26
- package/dist/index11.mjs +13 -10
- package/dist/index12.mjs +337 -3
- package/dist/index13.mjs +384 -115
- package/dist/index14.mjs +183 -66
- package/dist/index15.mjs +339 -8
- package/dist/index16.mjs +563 -20
- package/dist/index17.mjs +16 -12
- package/dist/index18.mjs +24 -5
- package/dist/index2.mjs +27 -36
- package/dist/index20.mjs +102 -13
- package/dist/index21.mjs +123 -21
- package/dist/index22.mjs +5 -67
- package/dist/index23.mjs +22 -81
- package/dist/index24.mjs +5 -16
- package/dist/index25.mjs +8 -27
- package/dist/index26.mjs +27 -7
- package/dist/index27.mjs +3 -5
- package/dist/index28.mjs +5 -27
- package/dist/index29.mjs +27 -40
- package/dist/index3.mjs +241 -497
- package/dist/index30.mjs +43 -0
- package/dist/index31.mjs +10 -0
- package/dist/index4.mjs +3 -170
- package/dist/index5.mjs +3 -66
- package/dist/index6.mjs +27 -3
- package/dist/index7.mjs +10 -3
- package/dist/index8.mjs +3 -3
- package/dist/index9.mjs +131 -3
- package/dist/types/IndexProvider.d.ts +12 -7
- package/dist/types/StorageAdapter.d.ts +20 -0
- package/dist/utils/objectId.d.ts +8 -0
- package/dist/utils/queryId.d.ts +9 -0
- package/package.json +2 -2
- package/dist/AutoFetchCollection.d.ts +0 -60
- package/dist/Collection/createIndex.d.ts +0 -15
- package/dist/ReplicatedCollection.d.ts +0 -60
- package/dist/createMemoryAdapter.d.ts +0 -7
- package/dist/persistence/combinePersistenceAdapters.d.ts +0 -32
- package/dist/persistence/createPersistenceAdapter.d.ts +0 -9
- package/dist/types/MemoryAdapter.d.ts +0 -15
- package/dist/types/PersistenceAdapter.d.ts +0 -20
package/dist/index.cjs14.js
CHANGED
|
@@ -1,74 +1,190 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if (posSet) {
|
|
29
|
-
for (const pos of posSet) {
|
|
30
|
-
includedPositions.push(pos);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
2
|
+
const queryId = require("./index.cjs24.js");
|
|
3
|
+
const randomId = require("./index.cjs8.js");
|
|
4
|
+
class WorkerDataAdapter {
|
|
5
|
+
worker;
|
|
6
|
+
options;
|
|
7
|
+
id;
|
|
8
|
+
isDisposed = false;
|
|
9
|
+
workerReady;
|
|
10
|
+
collectionReady = /* @__PURE__ */ new Map();
|
|
11
|
+
queries = {};
|
|
12
|
+
constructor(worker, options) {
|
|
13
|
+
this.worker = worker;
|
|
14
|
+
this.options = options;
|
|
15
|
+
this.id = this.options.id || "default-worker-data-adapter";
|
|
16
|
+
this.workerReady = new Promise((resolve, reject) => {
|
|
17
|
+
const timeoutId = setTimeout(() => {
|
|
18
|
+
reject(new Error("WorkerDataAdapter initialization timed out"));
|
|
19
|
+
}, 5e3);
|
|
20
|
+
const handleMessage = (event) => {
|
|
21
|
+
const { type, workerId } = event.data;
|
|
22
|
+
if (workerId !== this.id)
|
|
23
|
+
return;
|
|
24
|
+
if (type === "ready") {
|
|
25
|
+
resolve();
|
|
26
|
+
clearTimeout(timeoutId);
|
|
27
|
+
this.worker.removeEventListener("message", handleMessage);
|
|
33
28
|
}
|
|
34
|
-
}
|
|
35
|
-
if (keys.exclude != null) {
|
|
36
|
-
const excludeSet = /* @__PURE__ */ new Set();
|
|
37
|
-
for (const key of keys.exclude) {
|
|
38
|
-
const posSet = index.get(key);
|
|
39
|
-
if (posSet) {
|
|
40
|
-
for (const pos of posSet) {
|
|
41
|
-
excludeSet.add(pos);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
includedPositions = includedPositions.filter((pos) => !excludeSet.has(pos));
|
|
46
|
-
}
|
|
47
|
-
return {
|
|
48
|
-
matched: true,
|
|
49
|
-
positions: includedPositions,
|
|
50
|
-
fields: [field],
|
|
51
|
-
keepSelector: filteresForNull
|
|
52
29
|
};
|
|
53
|
-
|
|
54
|
-
|
|
30
|
+
this.worker.addEventListener("message", handleMessage);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
async exec(method, collectionName, ...args) {
|
|
34
|
+
await this.workerReady;
|
|
35
|
+
if (method !== "isReady") {
|
|
36
|
+
const collectionReady = this.collectionReady.get(collectionName);
|
|
37
|
+
if (!collectionReady)
|
|
38
|
+
throw new Error(`Collection "${collectionName}" is not registered in WorkerDataAdapter`);
|
|
39
|
+
await collectionReady;
|
|
55
40
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
41
|
+
if (this.isDisposed) {
|
|
42
|
+
throw new Error("WorkerDataAdapter is disposed");
|
|
43
|
+
}
|
|
44
|
+
return new Promise((resolve, reject) => {
|
|
45
|
+
const messageId = randomId();
|
|
46
|
+
const handleMessage = (event) => {
|
|
47
|
+
const { id, workerId, type, data, error } = event.data;
|
|
48
|
+
if (workerId !== this.id)
|
|
49
|
+
return;
|
|
50
|
+
if (type !== "response")
|
|
51
|
+
return;
|
|
52
|
+
if (id !== messageId)
|
|
53
|
+
return;
|
|
54
|
+
if (error) {
|
|
55
|
+
reject(error);
|
|
56
|
+
} else {
|
|
57
|
+
resolve(data);
|
|
58
|
+
}
|
|
59
|
+
this.worker.removeEventListener("message", handleMessage);
|
|
60
|
+
};
|
|
61
|
+
this.worker.addEventListener("message", handleMessage);
|
|
62
|
+
this.worker.postMessage({
|
|
63
|
+
id: messageId,
|
|
64
|
+
workerId: this.id,
|
|
65
|
+
method,
|
|
66
|
+
args: [collectionName, ...args]
|
|
69
67
|
});
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
queryListeners(collectionName, query, listeners) {
|
|
71
|
+
if (listeners != null) {
|
|
72
|
+
return this.updateQuery(collectionName, query, { listeners });
|
|
70
73
|
}
|
|
71
|
-
|
|
74
|
+
const id = queryId(query.selector, query.options);
|
|
75
|
+
const collectionQueries = this.queries[collectionName];
|
|
76
|
+
if (!collectionQueries)
|
|
77
|
+
return 0;
|
|
78
|
+
const existing = collectionQueries.get(id);
|
|
79
|
+
return existing?.listeners || 0;
|
|
80
|
+
}
|
|
81
|
+
updateQuery(collectionName, query, update) {
|
|
82
|
+
const id = queryId(query.selector, query.options);
|
|
83
|
+
const collectionQueries = this.queries[collectionName];
|
|
84
|
+
if (!collectionQueries)
|
|
85
|
+
return;
|
|
86
|
+
const existing = collectionQueries.get(id);
|
|
87
|
+
collectionQueries.set(id, {
|
|
88
|
+
listeners: 0,
|
|
89
|
+
state: "active",
|
|
90
|
+
error: null,
|
|
91
|
+
items: [],
|
|
92
|
+
...existing,
|
|
93
|
+
...update
|
|
94
|
+
});
|
|
95
|
+
this.queries[collectionName] = collectionQueries;
|
|
96
|
+
}
|
|
97
|
+
createCollectionBackend(collection, indices = []) {
|
|
98
|
+
this.queries[collection.name] = /* @__PURE__ */ new Map();
|
|
99
|
+
void this.exec("registerCollection", collection.name, indices);
|
|
100
|
+
this.collectionReady.set(collection.name, this.exec("isReady", collection.name));
|
|
101
|
+
return {
|
|
102
|
+
insert: async (item) => {
|
|
103
|
+
return this.exec("insert", collection.name, item);
|
|
104
|
+
},
|
|
105
|
+
updateOne: async (selector, modifier) => {
|
|
106
|
+
return this.exec("updateOne", collection.name, selector, modifier);
|
|
107
|
+
},
|
|
108
|
+
updateMany: async (selector, modifier) => {
|
|
109
|
+
return this.exec("updateMany", collection.name, selector, modifier);
|
|
110
|
+
},
|
|
111
|
+
replaceOne: async (selector, replacement) => {
|
|
112
|
+
return this.exec("replaceOne", collection.name, selector, replacement);
|
|
113
|
+
},
|
|
114
|
+
removeOne: async (selector) => {
|
|
115
|
+
return this.exec("removeOne", collection.name, selector);
|
|
116
|
+
},
|
|
117
|
+
removeMany: async (selector) => {
|
|
118
|
+
return this.exec("removeMany", collection.name, selector);
|
|
119
|
+
},
|
|
120
|
+
// methods for registering and unregistering queries that will be called from the collection during find/findOne
|
|
121
|
+
registerQuery: (selector, options) => {
|
|
122
|
+
const listeners = this.queryListeners(collection.name, { selector, options });
|
|
123
|
+
if (listeners === 0) {
|
|
124
|
+
this.updateQuery(collection.name, { selector, options }, { state: "active", error: null, items: [] });
|
|
125
|
+
void this.exec("registerQuery", collection.name, selector, options);
|
|
126
|
+
}
|
|
127
|
+
this.queryListeners(collection.name, { selector, options }, listeners + 1);
|
|
128
|
+
},
|
|
129
|
+
unregisterQuery: (selector, options) => {
|
|
130
|
+
setTimeout(() => {
|
|
131
|
+
const listeners = this.queryListeners(collection.name, { selector, options });
|
|
132
|
+
const newListeners = Math.max(0, listeners - 1);
|
|
133
|
+
if (newListeners === 0) {
|
|
134
|
+
this.queries[collection.name]?.delete(queryId(selector, options));
|
|
135
|
+
void this.exec("unregisterQuery", collection.name, selector, options);
|
|
136
|
+
}
|
|
137
|
+
}, 0);
|
|
138
|
+
},
|
|
139
|
+
getQueryState: (selector, options) => {
|
|
140
|
+
const query = this.queries[collection.name]?.get(queryId(selector, options));
|
|
141
|
+
return query?.state || "active";
|
|
142
|
+
},
|
|
143
|
+
getQueryError: (selector, options) => {
|
|
144
|
+
const query = this.queries[collection.name]?.get(queryId(selector, options));
|
|
145
|
+
return query?.error || null;
|
|
146
|
+
},
|
|
147
|
+
getQueryResult: (selector, options) => {
|
|
148
|
+
const query = this.queries[collection.name]?.get(queryId(selector, options));
|
|
149
|
+
return query?.items || [];
|
|
150
|
+
},
|
|
151
|
+
onQueryStateChange: (selector, options, callback) => {
|
|
152
|
+
const handler = (event) => {
|
|
153
|
+
const { type, data, workerId, error } = event.data;
|
|
154
|
+
if (type !== "queryUpdate")
|
|
155
|
+
return;
|
|
156
|
+
if (data == null)
|
|
157
|
+
return;
|
|
158
|
+
const { collectionName, selector: responseSelector, options: responseOptions, state, items } = data;
|
|
159
|
+
if (workerId !== this.id)
|
|
160
|
+
return;
|
|
161
|
+
if (collectionName !== collection.name)
|
|
162
|
+
return;
|
|
163
|
+
if (JSON.stringify(responseSelector) !== JSON.stringify(selector))
|
|
164
|
+
return;
|
|
165
|
+
if (JSON.stringify(responseOptions) !== JSON.stringify(options))
|
|
166
|
+
return;
|
|
167
|
+
this.updateQuery(collection.name, {
|
|
168
|
+
selector: responseSelector,
|
|
169
|
+
options: responseOptions
|
|
170
|
+
}, { state, error, items });
|
|
171
|
+
callback(state);
|
|
172
|
+
};
|
|
173
|
+
this.worker.addEventListener("message", handler);
|
|
174
|
+
return () => {
|
|
175
|
+
this.worker.removeEventListener("message", handler);
|
|
176
|
+
};
|
|
177
|
+
},
|
|
178
|
+
// lifecycle methods
|
|
179
|
+
dispose: async () => {
|
|
180
|
+
await this.exec("unregisterCollection", collection.name);
|
|
181
|
+
this.isDisposed = true;
|
|
182
|
+
this.worker.terminate();
|
|
183
|
+
},
|
|
184
|
+
isReady: async () => {
|
|
185
|
+
await this.exec("isReady", collection.name);
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
}
|
|
72
189
|
}
|
|
73
|
-
exports
|
|
74
|
-
exports.default = createIndex;
|
|
190
|
+
module.exports = WorkerDataAdapter;
|
package/dist/index.cjs15.js
CHANGED
|
@@ -1,10 +1,341 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
const
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
const deepClone = require("./index.cjs19.js");
|
|
3
|
+
const match = require("./index.cjs22.js");
|
|
4
|
+
const modify = require("./index.cjs7.js");
|
|
5
|
+
const queryId = require("./index.cjs24.js");
|
|
6
|
+
const isEqual = require("./index.cjs6.js");
|
|
7
|
+
const getIndexInfo = require("./index.cjs21.js");
|
|
8
|
+
const getMatchingKeys = require("./index.cjs26.js");
|
|
9
|
+
const sortItems = require("./index.cjs25.js");
|
|
10
|
+
const project = require("./index.cjs23.js");
|
|
11
|
+
class WorkerDataAdapterHost {
|
|
12
|
+
workerContext;
|
|
13
|
+
options;
|
|
14
|
+
id;
|
|
15
|
+
storageAdapters = /* @__PURE__ */ new Map();
|
|
16
|
+
storageAdapterReady = /* @__PURE__ */ new Map();
|
|
17
|
+
collectionIndices = /* @__PURE__ */ new Map();
|
|
18
|
+
queries = /* @__PURE__ */ new Map();
|
|
19
|
+
onError = (error) => {
|
|
20
|
+
console.error(error);
|
|
21
|
+
};
|
|
22
|
+
constructor(workerContext, options) {
|
|
23
|
+
this.workerContext = workerContext;
|
|
24
|
+
this.options = options;
|
|
25
|
+
this.id = this.options.id || "default-worker-data-adapter";
|
|
26
|
+
if (this.options.onError) {
|
|
27
|
+
this.onError = this.options.onError;
|
|
28
|
+
}
|
|
29
|
+
if (typeof addEventListener === "undefined" || typeof postMessage === "undefined") {
|
|
30
|
+
throw new TypeError("WorkerDataAdapterHost can only be used in a Web Worker context");
|
|
31
|
+
}
|
|
32
|
+
this.workerContext.addEventListener("message", async (event) => {
|
|
33
|
+
try {
|
|
34
|
+
const { workerId, id, method, args } = event.data;
|
|
35
|
+
await this.handleMessage(workerId, id, method, args);
|
|
36
|
+
} catch (error) {
|
|
37
|
+
this.onError(error);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
this.respond("ready", null, null, "ready");
|
|
41
|
+
}
|
|
42
|
+
respond(id, data, error = null, type = "response") {
|
|
43
|
+
this.workerContext.postMessage({ id, workerId: this.id, type, data, error });
|
|
44
|
+
}
|
|
45
|
+
async handleMessage(workerId, id, method, args) {
|
|
46
|
+
if (workerId !== this.id)
|
|
47
|
+
return;
|
|
48
|
+
const fn = this[method];
|
|
49
|
+
if (typeof fn !== "function") {
|
|
50
|
+
this.respond(id, null, new Error(`Method ${method} not found`));
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
const result = await fn.apply(this, args);
|
|
55
|
+
this.respond(id, result);
|
|
56
|
+
} catch (error) {
|
|
57
|
+
this.respond(id, null, error);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
async getIndexInfo(collectionName, selector) {
|
|
61
|
+
const storageAdapter = this.storageAdapters.get(collectionName);
|
|
62
|
+
if (!storageAdapter)
|
|
63
|
+
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
64
|
+
if (selector != null && Object.keys(selector).length === 1 && "id" in selector && typeof selector.id !== "object") {
|
|
65
|
+
return {
|
|
66
|
+
matched: true,
|
|
67
|
+
ids: [selector.id],
|
|
68
|
+
optimizedSelector: {}
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
if (selector == null) {
|
|
72
|
+
return {
|
|
73
|
+
matched: false,
|
|
74
|
+
ids: [],
|
|
75
|
+
optimizedSelector: {}
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
const indices = this.collectionIndices.get(collectionName) ?? [];
|
|
79
|
+
return getIndexInfo.default(indices.map((field) => async (flatSelector) => {
|
|
80
|
+
if (!Object.hasOwnProperty.call(flatSelector, field)) {
|
|
81
|
+
return { matched: false };
|
|
82
|
+
}
|
|
83
|
+
const index = await storageAdapter.readIndex(field);
|
|
84
|
+
const fieldSelector = flatSelector[field];
|
|
85
|
+
const filteresForNull = fieldSelector == null || fieldSelector.$exists === false;
|
|
86
|
+
const keys = filteresForNull ? { include: null, exclude: [...index.keys()].filter((key) => key != null) } : getMatchingKeys(field, flatSelector);
|
|
87
|
+
if (keys.include == null && keys.exclude == null)
|
|
88
|
+
return { matched: false };
|
|
89
|
+
let includedIds = [];
|
|
90
|
+
if (keys.include == null) {
|
|
91
|
+
for (const set of index.values()) {
|
|
92
|
+
for (const pos of set) {
|
|
93
|
+
includedIds.push(pos);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
} else {
|
|
97
|
+
for (const key of keys.include) {
|
|
98
|
+
const idSet = index.get(key);
|
|
99
|
+
if (idSet) {
|
|
100
|
+
for (const id of idSet) {
|
|
101
|
+
includedIds.push(id);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (keys.exclude != null) {
|
|
107
|
+
const excludeIds = /* @__PURE__ */ new Set();
|
|
108
|
+
for (const key of keys.exclude) {
|
|
109
|
+
const idSet = index.get(key);
|
|
110
|
+
if (idSet) {
|
|
111
|
+
for (const id of idSet) {
|
|
112
|
+
excludeIds.add(id);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
includedIds = includedIds.filter((pos) => !excludeIds.has(pos));
|
|
117
|
+
}
|
|
118
|
+
return {
|
|
119
|
+
matched: true,
|
|
120
|
+
ids: includedIds,
|
|
121
|
+
fields: [field],
|
|
122
|
+
keepSelector: filteresForNull
|
|
123
|
+
};
|
|
124
|
+
}), selector);
|
|
125
|
+
}
|
|
126
|
+
async queryItems(collectionName, selector) {
|
|
127
|
+
const storageAdapter = this.storageAdapters.get(collectionName);
|
|
128
|
+
if (!storageAdapter)
|
|
129
|
+
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
130
|
+
const indexInfo = await this.getIndexInfo(collectionName, selector);
|
|
131
|
+
const matchItems = (item) => {
|
|
132
|
+
if (indexInfo.optimizedSelector == null)
|
|
133
|
+
return true;
|
|
134
|
+
if (Object.keys(indexInfo.optimizedSelector).length <= 0)
|
|
135
|
+
return true;
|
|
136
|
+
const matches = match(item, indexInfo.optimizedSelector);
|
|
137
|
+
return matches;
|
|
138
|
+
};
|
|
139
|
+
if (indexInfo.matched) {
|
|
140
|
+
const items = await storageAdapter.readIds(indexInfo.ids);
|
|
141
|
+
if (isEqual(indexInfo.optimizedSelector, {}))
|
|
142
|
+
return items;
|
|
143
|
+
return items.filter(matchItems);
|
|
144
|
+
} else {
|
|
145
|
+
const allItems = await storageAdapter.readAll();
|
|
146
|
+
if (isEqual(selector, {}))
|
|
147
|
+
return allItems;
|
|
148
|
+
return allItems.filter(matchItems);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
async executeQuery(collectionName, selector, options) {
|
|
152
|
+
const items = await this.queryItems(collectionName, selector || {});
|
|
153
|
+
const { sort, skip, limit, fields } = options || {};
|
|
154
|
+
const sorted = sort ? sortItems(items, sort) : items;
|
|
155
|
+
const skipped = skip ? sorted.slice(skip) : sorted;
|
|
156
|
+
const limited = limit ? skipped.slice(0, limit) : skipped;
|
|
157
|
+
const idExcluded = fields && fields.id === 0;
|
|
158
|
+
return limited.map((item) => {
|
|
159
|
+
if (!fields)
|
|
160
|
+
return item;
|
|
161
|
+
return {
|
|
162
|
+
...idExcluded ? {} : { id: item.id },
|
|
163
|
+
...project(item, fields)
|
|
164
|
+
};
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
ensureQuery(collectionName, selector, options) {
|
|
168
|
+
const id = queryId(selector, options);
|
|
169
|
+
if (!this.queries.get(collectionName)) {
|
|
170
|
+
throw new Error(`Collection ${collectionName} not initialized!`);
|
|
171
|
+
}
|
|
172
|
+
let query = this.queries.get(collectionName)?.get(id);
|
|
173
|
+
if (!query) {
|
|
174
|
+
query = { selector, options };
|
|
175
|
+
this.queries.get(collectionName)?.set(id, query);
|
|
176
|
+
}
|
|
177
|
+
return query;
|
|
178
|
+
}
|
|
179
|
+
emitQueryUpdate(collectionName, selector, options, state, error, items) {
|
|
180
|
+
const id = queryId(selector, options);
|
|
181
|
+
const collectionQueries = this.queries.get(collectionName);
|
|
182
|
+
if (!collectionQueries)
|
|
183
|
+
throw new Error(`Collection ${collectionName} not initialized!`);
|
|
184
|
+
this.respond(id, { collectionName, selector, options, state, error, items }, null, "queryUpdate");
|
|
185
|
+
}
|
|
186
|
+
ensureStorageAdapter(name) {
|
|
187
|
+
if (this.storageAdapters.has(name))
|
|
188
|
+
return;
|
|
189
|
+
const adapter = this.options.storage(name);
|
|
190
|
+
if (!adapter)
|
|
191
|
+
return;
|
|
192
|
+
this.storageAdapters.set(name, adapter);
|
|
193
|
+
}
|
|
194
|
+
async checkQueryUpdates(collectionName, items) {
|
|
195
|
+
const queries = this.queries.get(collectionName);
|
|
196
|
+
if (!queries)
|
|
197
|
+
throw new Error(`Collection ${collectionName} not initialized!`);
|
|
198
|
+
const affectedQueries = [...queries.values()].filter(({ selector }) => items.some((item) => match(item, selector))) ?? [];
|
|
199
|
+
if (affectedQueries.length === 0)
|
|
200
|
+
return;
|
|
201
|
+
affectedQueries.forEach(({ selector, options }) => {
|
|
202
|
+
this.emitQueryUpdate(collectionName, selector, options, "active", null);
|
|
203
|
+
});
|
|
204
|
+
await Promise.all(affectedQueries.map(async ({ selector, options }) => {
|
|
205
|
+
const queryItems = await this.executeQuery(collectionName, selector, options);
|
|
206
|
+
this.emitQueryUpdate(collectionName, selector, options, "complete", null, queryItems);
|
|
207
|
+
}));
|
|
208
|
+
}
|
|
209
|
+
registerCollection = async (collectionName, indices) => {
|
|
210
|
+
this.collectionIndices.set(collectionName, indices);
|
|
211
|
+
this.queries.set(collectionName, /* @__PURE__ */ new Map());
|
|
212
|
+
this.ensureStorageAdapter(collectionName);
|
|
213
|
+
const storageAdapter = this.storageAdapters.get(collectionName);
|
|
214
|
+
if (!storageAdapter)
|
|
215
|
+
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
216
|
+
const setupPromise = (async () => {
|
|
217
|
+
await Promise.all([
|
|
218
|
+
storageAdapter.createIndex("id"),
|
|
219
|
+
...indices.map((index) => storageAdapter.createIndex(index))
|
|
220
|
+
]);
|
|
221
|
+
await storageAdapter.setup();
|
|
222
|
+
})();
|
|
223
|
+
this.storageAdapterReady.set(collectionName, setupPromise);
|
|
224
|
+
await setupPromise;
|
|
225
|
+
};
|
|
226
|
+
unregisterCollection = async (collectionName) => {
|
|
227
|
+
this.storageAdapters.delete(collectionName);
|
|
228
|
+
this.queries.delete(collectionName);
|
|
229
|
+
};
|
|
230
|
+
registerQuery = async (collectionName, selector, options) => {
|
|
231
|
+
this.ensureQuery(collectionName, selector, options);
|
|
232
|
+
const queryItems = await this.executeQuery(collectionName, selector, options);
|
|
233
|
+
this.emitQueryUpdate(collectionName, selector, options, "complete", null, queryItems);
|
|
234
|
+
};
|
|
235
|
+
unregisterQuery = async (collectionName, selector, options) => {
|
|
236
|
+
const id = queryId(selector, options);
|
|
237
|
+
if (!this.queries.get(collectionName))
|
|
238
|
+
throw new Error(`Collection ${collectionName} not initialized!`);
|
|
239
|
+
this.queries.get(collectionName)?.delete(id);
|
|
240
|
+
};
|
|
241
|
+
insert = async (collectionName, newItem) => {
|
|
242
|
+
const storageAdapter = this.storageAdapters.get(collectionName);
|
|
243
|
+
if (!storageAdapter)
|
|
244
|
+
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
245
|
+
const existingItems = await this.executeQuery(collectionName, { id: newItem.id }, { limit: 1 });
|
|
246
|
+
if (existingItems.length > 0) {
|
|
247
|
+
throw new Error(`Item with id ${newItem.id} already exists`);
|
|
248
|
+
}
|
|
249
|
+
await storageAdapter.insert([newItem]);
|
|
250
|
+
await this.checkQueryUpdates(collectionName, [newItem]);
|
|
251
|
+
return newItem;
|
|
252
|
+
};
|
|
253
|
+
updateOne = async (collectionName, selector, modifier) => {
|
|
254
|
+
const storageAdapter = this.storageAdapters.get(collectionName);
|
|
255
|
+
if (!storageAdapter)
|
|
256
|
+
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
257
|
+
const item = await this.executeQuery(collectionName, selector, { limit: 1 }).then((items) => items[0] ?? null);
|
|
258
|
+
const { $setOnInsert, ...restModifier } = modifier;
|
|
259
|
+
if (item == null)
|
|
260
|
+
return [];
|
|
261
|
+
const modifiedItem = modify(deepClone.default(item), restModifier);
|
|
262
|
+
if (item.id !== modifiedItem.id) {
|
|
263
|
+
const existingItems = await this.executeQuery(collectionName, { id: modifiedItem.id }, { limit: 1 });
|
|
264
|
+
if (existingItems.length > 0) {
|
|
265
|
+
throw new Error(`Item with id ${modifiedItem.id} already exists`);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
await storageAdapter.replace([modifiedItem]);
|
|
269
|
+
await this.checkQueryUpdates(collectionName, [modifiedItem]);
|
|
270
|
+
return [modifiedItem];
|
|
271
|
+
};
|
|
272
|
+
updateMany = async (collectionName, selector, modifier) => {
|
|
273
|
+
const storageAdapter = this.storageAdapters.get(collectionName);
|
|
274
|
+
if (!storageAdapter)
|
|
275
|
+
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
276
|
+
const items = await this.executeQuery(collectionName, selector);
|
|
277
|
+
if (items.length === 0)
|
|
278
|
+
return [];
|
|
279
|
+
const { $setOnInsert, ...restModifier } = modifier;
|
|
280
|
+
const changedItems = await Promise.all(items.map(async (item) => {
|
|
281
|
+
const modifiedItem = modify(deepClone.default(item), restModifier);
|
|
282
|
+
if (item.id !== modifiedItem.id) {
|
|
283
|
+
const existingItems = await this.executeQuery(collectionName, { id: modifiedItem.id }, { limit: 1 });
|
|
284
|
+
if (existingItems.length > 0) {
|
|
285
|
+
throw new Error(`Item with id ${modifiedItem.id} already exists`);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return modifiedItem;
|
|
289
|
+
}));
|
|
290
|
+
await storageAdapter.replace(changedItems);
|
|
291
|
+
await this.checkQueryUpdates(collectionName, changedItems);
|
|
292
|
+
return changedItems;
|
|
293
|
+
};
|
|
294
|
+
replaceOne = async (collectionName, selector, replacement) => {
|
|
295
|
+
const storageAdapter = this.storageAdapters.get(collectionName);
|
|
296
|
+
if (!storageAdapter)
|
|
297
|
+
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
298
|
+
const item = await this.executeQuery(collectionName, selector, { limit: 1 }).then((items) => items[0] ?? null);
|
|
299
|
+
if (item == null)
|
|
300
|
+
return [];
|
|
301
|
+
const modifiedItem = {
|
|
302
|
+
...replacement,
|
|
303
|
+
id: replacement.id ?? item.id
|
|
304
|
+
};
|
|
305
|
+
if (item.id !== modifiedItem.id) {
|
|
306
|
+
const existingItems = await this.executeQuery(collectionName, { id: modifiedItem.id }, { limit: 1 });
|
|
307
|
+
if (existingItems.length > 0) {
|
|
308
|
+
throw new Error(`Item with id ${modifiedItem.id} already exists`);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
await storageAdapter.replace([modifiedItem]);
|
|
312
|
+
await this.checkQueryUpdates(collectionName, [modifiedItem]);
|
|
313
|
+
return [modifiedItem];
|
|
314
|
+
};
|
|
315
|
+
removeOne = async (collectionName, selector) => {
|
|
316
|
+
const storageAdapter = this.storageAdapters.get(collectionName);
|
|
317
|
+
if (!storageAdapter)
|
|
318
|
+
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
319
|
+
const item = await this.executeQuery(collectionName, selector, { limit: 1 }).then((items) => items[0] ?? null);
|
|
320
|
+
if (item == null)
|
|
321
|
+
return [];
|
|
322
|
+
await storageAdapter.remove([item]);
|
|
323
|
+
await this.checkQueryUpdates(collectionName, [item]);
|
|
324
|
+
return [item];
|
|
325
|
+
};
|
|
326
|
+
removeMany = async (collectionName, selector) => {
|
|
327
|
+
const storageAdapter = this.storageAdapters.get(collectionName);
|
|
328
|
+
if (!storageAdapter)
|
|
329
|
+
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
330
|
+
const items = await this.executeQuery(collectionName, selector);
|
|
331
|
+
if (items.length === 0)
|
|
332
|
+
return [];
|
|
333
|
+
await storageAdapter.remove(items);
|
|
334
|
+
await this.checkQueryUpdates(collectionName, items);
|
|
335
|
+
return items;
|
|
336
|
+
};
|
|
337
|
+
isReady = async (collectionName) => {
|
|
338
|
+
return this.storageAdapterReady.get(collectionName);
|
|
339
|
+
};
|
|
9
340
|
}
|
|
10
|
-
module.exports =
|
|
341
|
+
module.exports = WorkerDataAdapterHost;
|