@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/index15.mjs
CHANGED
|
@@ -1,389 +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
|
-
const indices = this.collectionIndices.get(collectionName) ?? [];
|
|
85
|
-
return getIndexInfo(indices.map((field) => async (flatSelector) => {
|
|
86
|
-
if (!Object.hasOwnProperty.call(flatSelector, field)) {
|
|
87
|
-
return { matched: false };
|
|
88
|
-
}
|
|
89
|
-
const index = await storageAdapter.readIndex(field);
|
|
90
|
-
const fieldSelector = flatSelector[field];
|
|
91
|
-
const filteresForNull = fieldSelector == null || fieldSelector.$exists === false;
|
|
92
|
-
const keys = filteresForNull ? { include: null, exclude: [...index.keys()].filter((key) => key != null) } : getMatchingKeys(field, flatSelector);
|
|
93
|
-
if (keys.include == null && keys.exclude == null)
|
|
94
|
-
return { matched: false };
|
|
95
|
-
let includedIds = [];
|
|
96
|
-
if (keys.include == null) {
|
|
97
|
-
for (const set of index.values()) {
|
|
98
|
-
for (const pos of set) {
|
|
99
|
-
includedIds.push(pos);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
} else {
|
|
103
|
-
for (const key of keys.include) {
|
|
104
|
-
const idSet = index.get(key);
|
|
105
|
-
if (idSet) {
|
|
106
|
-
for (const id of idSet) {
|
|
107
|
-
includedIds.push(id);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
if (keys.exclude != null) {
|
|
113
|
-
const excludeIds = /* @__PURE__ */ new Set();
|
|
114
|
-
for (const key of keys.exclude) {
|
|
115
|
-
const idSet = index.get(key);
|
|
116
|
-
if (idSet) {
|
|
117
|
-
for (const id of idSet) {
|
|
118
|
-
excludeIds.add(id);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
includedIds = includedIds.filter((pos) => !excludeIds.has(pos));
|
|
123
|
-
}
|
|
124
|
-
return {
|
|
125
|
-
matched: true,
|
|
126
|
-
ids: includedIds,
|
|
127
|
-
fields: [field],
|
|
128
|
-
keepSelector: filteresForNull
|
|
129
|
-
};
|
|
130
|
-
}), selector);
|
|
131
|
-
}
|
|
132
|
-
async queryItems(collectionName, selector) {
|
|
133
|
-
const storageAdapter = this.storageAdapters.get(collectionName);
|
|
134
|
-
if (!storageAdapter)
|
|
135
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
136
|
-
const indexInfo = await this.getIndexInfo(collectionName, selector);
|
|
137
|
-
const matchItems = (item) => {
|
|
138
|
-
if (indexInfo.optimizedSelector == null)
|
|
139
|
-
return true;
|
|
140
|
-
if (Object.keys(indexInfo.optimizedSelector).length <= 0)
|
|
141
|
-
return true;
|
|
142
|
-
const matches = match(item, indexInfo.optimizedSelector);
|
|
143
|
-
return matches;
|
|
144
|
-
};
|
|
145
|
-
if (indexInfo.matched) {
|
|
146
|
-
const items = await storageAdapter.readIds(indexInfo.ids);
|
|
147
|
-
if (isEqual(indexInfo.optimizedSelector, {}))
|
|
148
|
-
return items;
|
|
149
|
-
return items.filter(matchItems);
|
|
150
|
-
} else {
|
|
151
|
-
const allItems = await storageAdapter.readAll();
|
|
152
|
-
if (isEqual(selector, {}))
|
|
153
|
-
return allItems;
|
|
154
|
-
return allItems.filter(matchItems);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
async executeQuery(collectionName, selector, options) {
|
|
158
|
-
if (selector === null)
|
|
159
|
-
return [];
|
|
160
|
-
const items = await this.queryItems(collectionName, selector || {});
|
|
161
|
-
const { sort, skip, limit, fields } = options || {};
|
|
162
|
-
const sorted = sort ? sortItems(items, sort) : items;
|
|
163
|
-
const skipped = skip ? sorted.slice(skip) : sorted;
|
|
164
|
-
const limited = limit ? skipped.slice(0, limit) : skipped;
|
|
165
|
-
const idExcluded = fields && fields.id === 0;
|
|
166
|
-
return limited.map((item) => {
|
|
167
|
-
if (!fields)
|
|
168
|
-
return item;
|
|
169
|
-
return {
|
|
170
|
-
...idExcluded ? {} : { id: item.id },
|
|
171
|
-
...project(item, fields)
|
|
172
|
-
};
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
ensureQuery(collectionName, selector, options) {
|
|
176
|
-
const id = queryId(selector, options);
|
|
177
|
-
if (!this.queries.get(collectionName)) {
|
|
178
|
-
throw new Error(`Collection ${collectionName} not initialized!`);
|
|
179
|
-
}
|
|
180
|
-
let query = this.queries.get(collectionName)?.get(id);
|
|
181
|
-
if (!query) {
|
|
182
|
-
query = { selector, options };
|
|
183
|
-
this.queries.get(collectionName)?.set(id, query);
|
|
184
|
-
}
|
|
185
|
-
return query;
|
|
186
|
-
}
|
|
187
|
-
emitQueryUpdate(collectionName, selector, options, state, error, items) {
|
|
188
|
-
const id = queryId(selector, options);
|
|
189
|
-
const collectionQueries = this.queries.get(collectionName);
|
|
190
|
-
if (!collectionQueries)
|
|
191
|
-
throw new Error(`Collection ${collectionName} not initialized!`);
|
|
192
|
-
this.respond(id, { collectionName, selector, options, state, error, items }, null, "queryUpdate");
|
|
193
|
-
}
|
|
194
|
-
ensureStorageAdapter(name) {
|
|
195
|
-
if (this.storageAdapters.has(name))
|
|
196
|
-
return;
|
|
197
|
-
const adapter = this.options.storage(name);
|
|
198
|
-
if (!adapter)
|
|
199
|
-
return;
|
|
200
|
-
this.storageAdapters.set(name, adapter);
|
|
201
|
-
}
|
|
202
|
-
async checkQueryUpdates(collectionName, items) {
|
|
203
|
-
const queries = this.queries.get(collectionName);
|
|
204
|
-
if (!queries)
|
|
205
|
-
throw new Error(`Collection ${collectionName} not initialized!`);
|
|
206
|
-
const affectedQueries = [...queries.values()].filter(({ selector }) => items.some((item) => match(item, selector))) ?? [];
|
|
207
|
-
if (affectedQueries.length === 0)
|
|
208
|
-
return;
|
|
209
|
-
affectedQueries.forEach(({ selector, options }) => {
|
|
210
|
-
this.emitQueryUpdate(collectionName, selector, options, "active", null);
|
|
211
|
-
});
|
|
212
|
-
await Promise.all(affectedQueries.map(async ({ selector, options }) => {
|
|
213
|
-
const queryItems = await this.executeQuery(collectionName, selector, options);
|
|
214
|
-
this.emitQueryUpdate(collectionName, selector, options, "complete", null, queryItems);
|
|
215
|
-
}));
|
|
216
|
-
}
|
|
217
|
-
registerCollection = async (collectionName, indices) => {
|
|
218
|
-
this.collectionIndices.set(collectionName, indices);
|
|
219
|
-
this.queries.set(collectionName, /* @__PURE__ */ new Map());
|
|
220
|
-
this.ensureStorageAdapter(collectionName);
|
|
221
|
-
const storageAdapter = this.storageAdapters.get(collectionName);
|
|
222
|
-
if (!storageAdapter)
|
|
223
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
224
|
-
const setupPromise = (async () => {
|
|
225
|
-
await Promise.all(indices.map((index) => storageAdapter.createIndex(index)));
|
|
226
|
-
await storageAdapter.setup();
|
|
227
|
-
})();
|
|
228
|
-
this.storageAdapterReady.set(collectionName, setupPromise);
|
|
229
|
-
await setupPromise;
|
|
230
|
-
};
|
|
231
|
-
unregisterCollection = async (collectionName) => {
|
|
232
|
-
this.storageAdapters.delete(collectionName);
|
|
233
|
-
this.queries.delete(collectionName);
|
|
234
|
-
};
|
|
235
|
-
registerQuery = async (collectionName, selector, options) => {
|
|
236
|
-
this.ensureQuery(collectionName, selector, options);
|
|
237
|
-
const queryItems = await this.executeQuery(collectionName, selector, options);
|
|
238
|
-
this.emitQueryUpdate(collectionName, selector, options, "complete", null, queryItems);
|
|
239
|
-
};
|
|
240
|
-
unregisterQuery = async (collectionName, selector, options) => {
|
|
241
|
-
const id = queryId(selector, options);
|
|
242
|
-
if (!this.queries.get(collectionName))
|
|
243
|
-
throw new Error(`Collection ${collectionName} not initialized!`);
|
|
244
|
-
this.queries.get(collectionName)?.delete(id);
|
|
245
|
-
};
|
|
246
|
-
insert = async (collectionName, input) => {
|
|
247
|
-
const storageAdapter = this.storageAdapters.get(collectionName);
|
|
248
|
-
if (!storageAdapter)
|
|
249
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
250
|
-
const existingItems = await this.executeQuery(collectionName, { id: { $in: input.map((i) => i[0].id) } });
|
|
251
|
-
const result = input.map(([item]) => {
|
|
252
|
-
if (item.id == null)
|
|
253
|
-
return new Error("Item must have an id");
|
|
254
|
-
if (existingItems.some((existing) => existing.id === item.id)) {
|
|
255
|
-
return new Error(`Item with id ${item.id} already exists`);
|
|
256
|
-
}
|
|
257
|
-
return item;
|
|
258
|
-
});
|
|
259
|
-
const newItems = result.filter((item) => !(item instanceof Error));
|
|
260
|
-
await storageAdapter.insert(newItems);
|
|
261
|
-
await this.checkQueryUpdates(collectionName, newItems);
|
|
262
|
-
return result;
|
|
263
|
-
};
|
|
264
|
-
updateOne = async (collectionName, parameters) => {
|
|
265
|
-
const storageAdapter = this.storageAdapters.get(collectionName);
|
|
266
|
-
if (!storageAdapter)
|
|
267
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
268
|
-
const previousItems = [];
|
|
269
|
-
const result = await Promise.all(parameters.map(async ([selector, modifier]) => {
|
|
270
|
-
const item = await this.executeQuery(collectionName, selector, { limit: 1 }).then((items) => items[0] ?? null);
|
|
271
|
-
const { $setOnInsert, ...restModifier } = modifier;
|
|
272
|
-
if (item == null)
|
|
273
|
-
return [];
|
|
274
|
-
previousItems.push(item);
|
|
275
|
-
const modifiedItem = modify(deepClone(item), restModifier);
|
|
276
|
-
if (item.id !== modifiedItem.id) {
|
|
277
|
-
const existingItems = await this.executeQuery(collectionName, { id: modifiedItem.id }, { limit: 1 });
|
|
278
|
-
if (existingItems.length > 0) {
|
|
279
|
-
return new Error(`Item with id ${modifiedItem.id} already exists`);
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
return [modifiedItem];
|
|
283
|
-
}));
|
|
284
|
-
const modifiedItems = compact(result.filter((item) => !(item instanceof Error)).flat());
|
|
285
|
-
if (modifiedItems.length > 0) {
|
|
286
|
-
await storageAdapter.replace(modifiedItems);
|
|
287
|
-
await this.checkQueryUpdates(collectionName, [...modifiedItems, ...previousItems]);
|
|
288
|
-
}
|
|
289
|
-
return result;
|
|
290
|
-
};
|
|
291
|
-
updateMany = async (collectionName, parameters) => {
|
|
292
|
-
const storageAdapter = this.storageAdapters.get(collectionName);
|
|
293
|
-
if (!storageAdapter)
|
|
294
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
295
|
-
const previousItems = [];
|
|
296
|
-
const result = await Promise.all(parameters.map(async ([selector, modifier]) => {
|
|
297
|
-
const items = await this.executeQuery(collectionName, selector);
|
|
298
|
-
if (items.length === 0)
|
|
299
|
-
return [];
|
|
300
|
-
const { $setOnInsert, ...restModifier } = modifier;
|
|
301
|
-
try {
|
|
302
|
-
const changedItems = await Promise.all(items.map(async (item) => {
|
|
303
|
-
const modifiedItem = modify(deepClone(item), restModifier);
|
|
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
|
-
previousItems.push(item);
|
|
311
|
-
return modifiedItem;
|
|
312
|
-
}));
|
|
313
|
-
return changedItems;
|
|
314
|
-
} catch (error) {
|
|
315
|
-
return error;
|
|
316
|
-
}
|
|
317
|
-
}));
|
|
318
|
-
const modifiedItems = compact(result.filter((item) => !(item instanceof Error)).flat());
|
|
319
|
-
if (modifiedItems.length > 0) {
|
|
320
|
-
await storageAdapter.replace(modifiedItems);
|
|
321
|
-
await this.checkQueryUpdates(collectionName, [...modifiedItems, ...previousItems]);
|
|
322
|
-
}
|
|
323
|
-
return result;
|
|
324
|
-
};
|
|
325
|
-
replaceOne = async (collectionName, parameters) => {
|
|
326
|
-
const storageAdapter = this.storageAdapters.get(collectionName);
|
|
327
|
-
if (!storageAdapter)
|
|
328
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
329
|
-
const previousItems = [];
|
|
330
|
-
const result = await Promise.all(parameters.map(async ([selector, replacement]) => {
|
|
331
|
-
const item = await this.executeQuery(collectionName, selector, { limit: 1 }).then((items) => items[0] ?? null);
|
|
332
|
-
if (item == null)
|
|
333
|
-
return [];
|
|
334
|
-
previousItems.push(item);
|
|
335
|
-
const modifiedItem = {
|
|
336
|
-
...replacement,
|
|
337
|
-
id: replacement.id ?? item.id
|
|
338
|
-
};
|
|
339
|
-
if (item.id !== modifiedItem.id) {
|
|
340
|
-
const existingItems = await this.executeQuery(collectionName, { id: modifiedItem.id }, { limit: 1 });
|
|
341
|
-
if (existingItems.length > 0) {
|
|
342
|
-
return new Error(`Item with id ${modifiedItem.id} already exists`);
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
return [modifiedItem];
|
|
346
|
-
}));
|
|
347
|
-
const modifiedItems = compact(result.filter((item) => !(item instanceof Error)).flat());
|
|
348
|
-
if (modifiedItems.length > 0) {
|
|
349
|
-
await storageAdapter.replace(modifiedItems);
|
|
350
|
-
await this.checkQueryUpdates(collectionName, [...modifiedItems, ...previousItems]);
|
|
351
|
-
}
|
|
352
|
-
return result;
|
|
353
|
-
};
|
|
354
|
-
removeOne = async (collectionName, selectors) => {
|
|
355
|
-
const storageAdapter = this.storageAdapters.get(collectionName);
|
|
356
|
-
if (!storageAdapter)
|
|
357
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
358
|
-
const result = await Promise.all(selectors.map(async ([selector]) => {
|
|
359
|
-
const item = await this.executeQuery(collectionName, selector, { limit: 1 }).then((items2) => items2[0] ?? null);
|
|
360
|
-
if (item == null)
|
|
361
|
-
return [];
|
|
362
|
-
return [item];
|
|
363
|
-
}));
|
|
364
|
-
const items = result.flat();
|
|
365
|
-
if (items.length > 0) {
|
|
366
|
-
await storageAdapter.remove(items);
|
|
367
|
-
await this.checkQueryUpdates(collectionName, items);
|
|
368
|
-
}
|
|
369
|
-
return result;
|
|
370
|
-
};
|
|
371
|
-
removeMany = async (collectionName, selectors) => {
|
|
372
|
-
const storageAdapter = this.storageAdapters.get(collectionName);
|
|
373
|
-
if (!storageAdapter)
|
|
374
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
375
|
-
const result = await Promise.all(selectors.map(async ([selector]) => this.executeQuery(collectionName, selector)));
|
|
376
|
-
const items = result.flat();
|
|
377
|
-
if (items.length > 0) {
|
|
378
|
-
await storageAdapter.remove(items);
|
|
379
|
-
await this.checkQueryUpdates(collectionName, items);
|
|
380
|
-
}
|
|
381
|
-
return result;
|
|
382
|
-
};
|
|
383
|
-
isReady = async (collectionName) => {
|
|
384
|
-
return this.storageAdapterReady.get(collectionName);
|
|
385
|
-
};
|
|
1
|
+
import createIndexProvider from "./index10.mjs";
|
|
2
|
+
import get from "./index11.mjs";
|
|
3
|
+
import serializeValue from "./index13.mjs";
|
|
4
|
+
import getMatchingKeys from "./index14.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
|
+
});
|
|
386
84
|
}
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
};
|
|
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;
|