@signaldb/core 2.0.0-beta.5 → 2.0.0-beta.7
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 +118 -84
- package/dist/AsyncDataAdapter.d.ts +1 -1
- package/dist/index.cjs.js +33 -34
- package/dist/index.mjs +17 -35
- 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 -47
- package/dist/index13.cjs.js +45 -0
- package/dist/index13.mjs +44 -365
- package/dist/index14.cjs.js +86 -0
- package/dist/index14.mjs +85 -416
- package/dist/index15.cjs.js +15 -0
- package/dist/index15.mjs +14 -206
- package/dist/index16.cjs.js +136 -0
- package/dist/index16.mjs +135 -388
- package/dist/index17.cjs.js +47 -0
- package/dist/index17.mjs +45 -578
- package/dist/index18.cjs.js +15 -0
- package/dist/index18.mjs +14 -157
- package/dist/index19.cjs.js +23 -0
- package/dist/index19.mjs +22 -26
- package/dist/index2.cjs.js +33 -0
- package/dist/index2.mjs +32 -258
- package/dist/index20.cjs.js +37 -0
- package/dist/index20.mjs +36 -41
- package/dist/index21.cjs.js +35 -0
- package/dist/index21.mjs +34 -16
- package/dist/index22.cjs.js +25 -0
- package/dist/index22.mjs +24 -104
- package/dist/index23.cjs.js +19 -0
- package/dist/index23.mjs +18 -128
- package/dist/index24.cjs.js +342 -0
- package/dist/index24.mjs +340 -6
- package/dist/index25.cjs.js +519 -0
- package/dist/index25.mjs +518 -27
- package/dist/index26.cjs.js +11 -0
- package/dist/index26.mjs +10 -10
- package/dist/index27.cjs.js +11 -0
- package/dist/index27.mjs +10 -29
- package/dist/index28.cjs.js +90 -0
- package/dist/index28.mjs +88 -46
- package/dist/index29.cjs.js +370 -0
- package/dist/index29.mjs +369 -8
- package/dist/index3.cjs.js +19 -0
- package/dist/index3.mjs +18 -539
- package/dist/index30.cjs.js +72 -0
- package/dist/index30.mjs +71 -7
- package/dist/index31.cjs.js +191 -0
- package/dist/index31.mjs +190 -42
- package/dist/index32.cjs.js +21 -0
- package/dist/index32.mjs +20 -29
- package/dist/index33.cjs.js +336 -0
- package/dist/index33.mjs +335 -5
- package/dist/index34.cjs.js +539 -0
- package/dist/index34.mjs +537 -8
- 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/package.json +1 -1
- package/dist/index.cjs10.js +0 -18
- package/dist/index.cjs11.js +0 -15
- package/dist/index.cjs12.js +0 -48
- package/dist/index.cjs13.js +0 -365
- package/dist/index.cjs14.js +0 -416
- package/dist/index.cjs15.js +0 -206
- package/dist/index.cjs16.js +0 -388
- package/dist/index.cjs17.js +0 -579
- package/dist/index.cjs18.js +0 -157
- package/dist/index.cjs19.js +0 -26
- package/dist/index.cjs2.js +0 -259
- package/dist/index.cjs20.js +0 -42
- package/dist/index.cjs21.js +0 -16
- package/dist/index.cjs22.js +0 -104
- package/dist/index.cjs23.js +0 -129
- package/dist/index.cjs24.js +0 -7
- package/dist/index.cjs25.js +0 -27
- package/dist/index.cjs26.js +0 -10
- package/dist/index.cjs27.js +0 -29
- package/dist/index.cjs28.js +0 -46
- package/dist/index.cjs29.js +0 -8
- package/dist/index.cjs3.js +0 -540
- package/dist/index.cjs30.js +0 -7
- package/dist/index.cjs31.js +0 -42
- package/dist/index.cjs32.js +0 -29
- package/dist/index.cjs33.js +0 -5
- package/dist/index.cjs34.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
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
const require_createIndexProvider = require("./index9.cjs.js");
|
|
2
|
+
const require_get = require("./index10.cjs.js");
|
|
3
|
+
const require_serializeValue = require("./index12.cjs.js");
|
|
4
|
+
const require_getMatchingKeys = require("./index13.cjs.js");
|
|
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 require_createIndexProvider.default({
|
|
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
|
+
} : require_getMatchingKeys.default(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(require_serializeValue.default(require_get.default(item, field))).add(item.id);
|
|
56
|
+
});
|
|
57
|
+
},
|
|
58
|
+
insert(items) {
|
|
59
|
+
for (const item of items) ensureSet(require_serializeValue.default(require_get.default(item, field))).add(item.id);
|
|
60
|
+
},
|
|
61
|
+
remove(items) {
|
|
62
|
+
for (const item of items) {
|
|
63
|
+
const value = require_serializeValue.default(require_get.default(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 = require_serializeValue.default(require_get.default(oldItem, field));
|
|
73
|
+
const newValue = require_serializeValue.default(require_get.default(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
|
+
});
|
|
84
|
+
}
|
|
85
|
+
//#endregion
|
|
86
|
+
exports.default = createIndex;
|
package/dist/index14.mjs
CHANGED
|
@@ -1,417 +1,86 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
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
|
-
listeners: /* @__PURE__ */ new Set()
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
registry.get(qid)?.listeners.add(callback);
|
|
88
|
-
return () => registry.get(qid)?.listeners.delete(callback);
|
|
89
|
-
};
|
|
90
|
-
return {
|
|
91
|
-
insert: async (item) => {
|
|
92
|
-
await ready;
|
|
93
|
-
const inserted = await this.insert(collection.name, item);
|
|
94
|
-
return inserted;
|
|
95
|
-
},
|
|
96
|
-
updateOne: async (selector, modifier) => {
|
|
97
|
-
await ready;
|
|
98
|
-
return this.updateOne(collection.name, selector, modifier);
|
|
99
|
-
},
|
|
100
|
-
updateMany: async (selector, modifier) => {
|
|
101
|
-
await ready;
|
|
102
|
-
return this.updateMany(collection.name, selector, modifier);
|
|
103
|
-
},
|
|
104
|
-
replaceOne: async (selector, replacement) => {
|
|
105
|
-
await ready;
|
|
106
|
-
return this.replaceOne(collection.name, selector, replacement);
|
|
107
|
-
},
|
|
108
|
-
removeOne: async (selector) => {
|
|
109
|
-
await ready;
|
|
110
|
-
return this.removeOne(collection.name, selector);
|
|
111
|
-
},
|
|
112
|
-
removeMany: async (selector) => {
|
|
113
|
-
await ready;
|
|
114
|
-
return this.removeMany(collection.name, selector);
|
|
115
|
-
},
|
|
116
|
-
registerQuery,
|
|
117
|
-
unregisterQuery,
|
|
118
|
-
getQueryState,
|
|
119
|
-
getQueryError,
|
|
120
|
-
getQueryResult,
|
|
121
|
-
onQueryStateChange,
|
|
122
|
-
executeQuery: async (selector, options) => {
|
|
123
|
-
await ready;
|
|
124
|
-
return this.executeQuery(collection.name, selector, options);
|
|
125
|
-
},
|
|
126
|
-
dispose: async () => {
|
|
127
|
-
this.storageAdapters.delete(collection.name);
|
|
128
|
-
this.queries.delete(collection.name);
|
|
129
|
-
this.collectionIndices.delete(collection.name);
|
|
130
|
-
this.storageAdapterReady.delete(collection.name);
|
|
131
|
-
},
|
|
132
|
-
isReady: async () => {
|
|
133
|
-
await ready;
|
|
134
|
-
}
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
async setupStorage(collectionName, indices) {
|
|
138
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
139
|
-
if (!storage)
|
|
140
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
141
|
-
await storage.setup();
|
|
142
|
-
await Promise.all(indices.map((index) => storage.createIndex(index)));
|
|
143
|
-
}
|
|
144
|
-
ensureStorageAdapter(name) {
|
|
145
|
-
if (this.storageAdapters.has(name))
|
|
146
|
-
return;
|
|
147
|
-
const adapter = this.options.storage(name);
|
|
148
|
-
if (!adapter)
|
|
149
|
-
return;
|
|
150
|
-
this.storageAdapters.set(name, adapter);
|
|
151
|
-
}
|
|
152
|
-
/**
|
|
153
|
-
* Compute and publish the result for a specific query
|
|
154
|
-
* @param collectionName - name of the collection
|
|
155
|
-
* @param selector - query selector
|
|
156
|
-
* @param options - query options
|
|
157
|
-
*/
|
|
158
|
-
async fulfillQuery(collectionName, selector, options) {
|
|
159
|
-
const qid = queryId(selector, options);
|
|
160
|
-
const registry = this.queries.get(collectionName);
|
|
161
|
-
if (!registry)
|
|
162
|
-
throw new Error(`Collection ${collectionName} not initialized!`);
|
|
163
|
-
const rec = registry.get(qid);
|
|
164
|
-
if (!rec)
|
|
165
|
-
return;
|
|
166
|
-
this.publishState(collectionName, qid, "active", null);
|
|
167
|
-
try {
|
|
168
|
-
const items = await this.executeQuery(collectionName, selector, options);
|
|
169
|
-
this.publishResult(collectionName, qid, items);
|
|
170
|
-
this.publishState(collectionName, qid, "complete", null);
|
|
171
|
-
} catch (error) {
|
|
172
|
-
this.publishState(collectionName, qid, "error", error);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* Notify listeners about state changes and keep the cache updated
|
|
177
|
-
* @param collectionName - name of the collection
|
|
178
|
-
* @param qid - query id
|
|
179
|
-
* @param state - new state
|
|
180
|
-
* @param error - error if state is 'error', null otherwise
|
|
181
|
-
*/
|
|
182
|
-
publishState(collectionName, qid, state, error) {
|
|
183
|
-
const rec = this.queries.get(collectionName)?.get(qid);
|
|
184
|
-
if (!rec)
|
|
185
|
-
return;
|
|
186
|
-
rec.state = state;
|
|
187
|
-
rec.error = error;
|
|
188
|
-
for (const callback of rec.listeners) {
|
|
189
|
-
try {
|
|
190
|
-
callback(state);
|
|
191
|
-
} catch (error_) {
|
|
192
|
-
this.onError(error_);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
publishResult(collectionName, qid, items) {
|
|
197
|
-
const rec = this.queries.get(collectionName)?.get(qid);
|
|
198
|
-
if (!rec)
|
|
199
|
-
return;
|
|
200
|
-
rec.items = items;
|
|
201
|
-
}
|
|
202
|
-
async getIndexInfo(collectionName, selector) {
|
|
203
|
-
const storageAdapter = this.storageAdapters.get(collectionName);
|
|
204
|
-
if (!storageAdapter)
|
|
205
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
206
|
-
if (selector != null && Object.keys(selector).length === 1 && "id" in selector && typeof selector.id !== "object") {
|
|
207
|
-
return {
|
|
208
|
-
matched: true,
|
|
209
|
-
ids: [selector.id].filter(Boolean),
|
|
210
|
-
optimizedSelector: {}
|
|
211
|
-
};
|
|
212
|
-
}
|
|
213
|
-
if (selector == null) {
|
|
214
|
-
return {
|
|
215
|
-
matched: false,
|
|
216
|
-
ids: [],
|
|
217
|
-
optimizedSelector: {}
|
|
218
|
-
};
|
|
219
|
-
}
|
|
220
|
-
const indices = this.collectionIndices.get(collectionName) ?? [];
|
|
221
|
-
return getIndexInfo(indices.map((field) => async (flatSelector) => {
|
|
222
|
-
if (!Object.hasOwnProperty.call(flatSelector, field))
|
|
223
|
-
return { matched: false };
|
|
224
|
-
const index = await storageAdapter.readIndex(field);
|
|
225
|
-
const fieldSelector = flatSelector[field];
|
|
226
|
-
const filtersForNull = fieldSelector == null || fieldSelector.$exists === false;
|
|
227
|
-
const keys = filtersForNull ? { include: null, exclude: [...index.keys()].filter((key) => key != null) } : getMatchingKeys(field, flatSelector);
|
|
228
|
-
if (keys.include == null && keys.exclude == null)
|
|
229
|
-
return { matched: false };
|
|
230
|
-
let includedIds = [];
|
|
231
|
-
if (keys.include == null) {
|
|
232
|
-
for (const set of index.values())
|
|
233
|
-
for (const pos of set)
|
|
234
|
-
includedIds.push(pos);
|
|
235
|
-
} else {
|
|
236
|
-
for (const key of keys.include) {
|
|
237
|
-
const idSet = index.get(key);
|
|
238
|
-
if (idSet)
|
|
239
|
-
for (const id of idSet)
|
|
240
|
-
includedIds.push(id);
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
if (keys.exclude != null) {
|
|
244
|
-
const excludeIds = /* @__PURE__ */ new Set();
|
|
245
|
-
for (const key of keys.exclude) {
|
|
246
|
-
const idSet = index.get(key);
|
|
247
|
-
if (idSet)
|
|
248
|
-
for (const id of idSet)
|
|
249
|
-
excludeIds.add(id);
|
|
250
|
-
}
|
|
251
|
-
includedIds = includedIds.filter((pos) => !excludeIds.has(pos));
|
|
252
|
-
}
|
|
253
|
-
return { matched: true, ids: includedIds, fields: [field], keepSelector: filtersForNull };
|
|
254
|
-
}), selector);
|
|
255
|
-
}
|
|
256
|
-
async queryItems(collectionName, selector) {
|
|
257
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
258
|
-
if (!storage)
|
|
259
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
260
|
-
const index = await this.getIndexInfo(collectionName, selector);
|
|
261
|
-
const matchItems = (item) => {
|
|
262
|
-
if (index.optimizedSelector == null)
|
|
263
|
-
return true;
|
|
264
|
-
if (Object.keys(index.optimizedSelector).length <= 0)
|
|
265
|
-
return true;
|
|
266
|
-
return match(item, index.optimizedSelector);
|
|
267
|
-
};
|
|
268
|
-
if (index.matched) {
|
|
269
|
-
const items = await storage.readIds(index.ids);
|
|
270
|
-
if (isEqual(index.optimizedSelector, {}))
|
|
271
|
-
return items;
|
|
272
|
-
return items.filter(matchItems);
|
|
273
|
-
} else {
|
|
274
|
-
const allItems = await storage.readAll();
|
|
275
|
-
if (isEqual(selector, {}))
|
|
276
|
-
return allItems;
|
|
277
|
-
return allItems.filter(matchItems);
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
async executeQuery(collectionName, selector, options) {
|
|
281
|
-
const items = await this.queryItems(collectionName, selector || {});
|
|
282
|
-
const { sort, skip, limit, fields } = options || {};
|
|
283
|
-
const sorted = sort ? sortItems(items, sort) : items;
|
|
284
|
-
const skipped = skip ? sorted.slice(skip) : sorted;
|
|
285
|
-
const limited = limit ? skipped.slice(0, limit) : skipped;
|
|
286
|
-
const idExcluded = fields && fields.id === 0;
|
|
287
|
-
return limited.map((item) => {
|
|
288
|
-
if (!fields)
|
|
289
|
-
return item;
|
|
290
|
-
return { ...idExcluded ? {} : { id: item.id }, ...project(item, fields) };
|
|
291
|
-
});
|
|
292
|
-
}
|
|
293
|
-
/**
|
|
294
|
-
* After mutations, recompute and push updates for affected active queries
|
|
295
|
-
* @param collectionName - name of the collection
|
|
296
|
-
* @param changedItems - items that were inserted/updated/replaced/removed
|
|
297
|
-
*/
|
|
298
|
-
async checkQueryUpdates(collectionName, changedItems) {
|
|
299
|
-
const registry = this.queries.get(collectionName);
|
|
300
|
-
if (!registry)
|
|
301
|
-
throw new Error(`Collection ${collectionName} not initialized!`);
|
|
302
|
-
if (registry.size === 0)
|
|
303
|
-
return;
|
|
304
|
-
const affected = [...registry.values()].filter(({ selector }) => changedItems.some((item) => match(item, selector)));
|
|
305
|
-
if (affected.length === 0)
|
|
306
|
-
return;
|
|
307
|
-
for (const { selector, options } of affected) {
|
|
308
|
-
const qid = queryId(selector, options);
|
|
309
|
-
this.publishState(collectionName, qid, "active", null);
|
|
310
|
-
}
|
|
311
|
-
await Promise.all(affected.map(async ({ selector, options }) => {
|
|
312
|
-
const qid = queryId(selector, options);
|
|
313
|
-
try {
|
|
314
|
-
const items = await this.executeQuery(collectionName, selector, options);
|
|
315
|
-
this.publishResult(collectionName, qid, items);
|
|
316
|
-
this.publishState(collectionName, qid, "complete", null);
|
|
317
|
-
} catch (error) {
|
|
318
|
-
this.publishState(collectionName, qid, "error", error);
|
|
319
|
-
}
|
|
320
|
-
}));
|
|
321
|
-
}
|
|
322
|
-
async insert(collectionName, newItem) {
|
|
323
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
324
|
-
if (!storage)
|
|
325
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
326
|
-
const existingItems = await storage.readIds([newItem.id]);
|
|
327
|
-
if (existingItems.length > 0)
|
|
328
|
-
throw new Error(`Item with id ${String(newItem.id)} already exists`);
|
|
329
|
-
await storage.insert([newItem]);
|
|
330
|
-
await this.checkQueryUpdates(collectionName, [newItem]);
|
|
331
|
-
return newItem;
|
|
332
|
-
}
|
|
333
|
-
async updateOne(collectionName, selector, modifier) {
|
|
334
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
335
|
-
if (!storage)
|
|
336
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
337
|
-
const items = await this.executeQuery(collectionName, selector, { limit: 1 });
|
|
338
|
-
const [item] = items;
|
|
339
|
-
const { $setOnInsert, ...rest } = modifier;
|
|
340
|
-
if (item == null)
|
|
341
|
-
return [];
|
|
342
|
-
const modified = modify(deepClone(item), rest);
|
|
343
|
-
if (item.id !== modified.id) {
|
|
344
|
-
const existing = await storage.readIds([modified.id]);
|
|
345
|
-
if (existing.length > 0)
|
|
346
|
-
throw new Error(`Item with id ${String(modified.id)} already exists`);
|
|
347
|
-
}
|
|
348
|
-
await storage.replace([modified]);
|
|
349
|
-
await this.checkQueryUpdates(collectionName, [modified]);
|
|
350
|
-
return [modified];
|
|
351
|
-
}
|
|
352
|
-
async updateMany(collectionName, selector, modifier) {
|
|
353
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
354
|
-
if (!storage)
|
|
355
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
356
|
-
const items = await this.executeQuery(collectionName, selector);
|
|
357
|
-
if (items.length === 0)
|
|
358
|
-
return [];
|
|
359
|
-
const { $setOnInsert, ...rest } = modifier;
|
|
360
|
-
const changed = await Promise.all(items.map(async (item) => {
|
|
361
|
-
const modified = modify(deepClone(item), rest);
|
|
362
|
-
if (item.id !== modified.id) {
|
|
363
|
-
const existing = await storage.readIds([modified.id]);
|
|
364
|
-
if (existing.length > 0)
|
|
365
|
-
throw new Error(`Item with id ${String(modified.id)} already exists`);
|
|
366
|
-
}
|
|
367
|
-
return modified;
|
|
368
|
-
}));
|
|
369
|
-
await storage.replace(changed);
|
|
370
|
-
await this.checkQueryUpdates(collectionName, changed);
|
|
371
|
-
return changed;
|
|
372
|
-
}
|
|
373
|
-
async replaceOne(collectionName, selector, replacement) {
|
|
374
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
375
|
-
if (!storage)
|
|
376
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
377
|
-
const items = await this.executeQuery(collectionName, selector, { limit: 1 });
|
|
378
|
-
const [item] = items;
|
|
379
|
-
if (item == null)
|
|
380
|
-
return [];
|
|
381
|
-
const modified = { ...replacement, id: replacement.id ?? item.id };
|
|
382
|
-
if (item.id !== modified.id) {
|
|
383
|
-
const existing = await storage.readIds([modified.id]);
|
|
384
|
-
if (existing.length > 0)
|
|
385
|
-
throw new Error(`Item with id ${String(modified.id)} already exists`);
|
|
386
|
-
}
|
|
387
|
-
await storage.replace([modified]);
|
|
388
|
-
await this.checkQueryUpdates(collectionName, [modified]);
|
|
389
|
-
return [modified];
|
|
390
|
-
}
|
|
391
|
-
async removeOne(collectionName, selector) {
|
|
392
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
393
|
-
if (!storage)
|
|
394
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
395
|
-
const items = await this.executeQuery(collectionName, selector, { limit: 1 });
|
|
396
|
-
const [item] = items;
|
|
397
|
-
if (item == null)
|
|
398
|
-
return [];
|
|
399
|
-
await storage.remove([item]);
|
|
400
|
-
await this.checkQueryUpdates(collectionName, [item]);
|
|
401
|
-
return [item];
|
|
402
|
-
}
|
|
403
|
-
async removeMany(collectionName, selector) {
|
|
404
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
405
|
-
if (!storage)
|
|
406
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
407
|
-
const items = await this.executeQuery(collectionName, selector);
|
|
408
|
-
if (items.length === 0)
|
|
409
|
-
return [];
|
|
410
|
-
await storage.remove(items);
|
|
411
|
-
await this.checkQueryUpdates(collectionName, items);
|
|
412
|
-
return items;
|
|
413
|
-
}
|
|
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
|
+
});
|
|
414
84
|
}
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
};
|
|
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;
|