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