@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/index13.mjs
CHANGED
|
@@ -1,408 +1,45 @@
|
|
|
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
|
-
if (!registry)
|
|
44
|
-
throw new Error(`Collection ${collection.name} not initialized!`);
|
|
45
|
-
registry.set(qid, {
|
|
46
|
-
selector,
|
|
47
|
-
options,
|
|
48
|
-
items: [],
|
|
49
|
-
listeners: /* @__PURE__ */ new Set(),
|
|
50
|
-
...registry.get(qid),
|
|
51
|
-
state: "active",
|
|
52
|
-
error: null
|
|
53
|
-
});
|
|
54
|
-
void this.fulfillQuery(collection.name, selector, options).catch(this.onError);
|
|
55
|
-
};
|
|
56
|
-
const unregisterQuery = (selector, options) => {
|
|
57
|
-
const qid = queryId(selector, options);
|
|
58
|
-
this.queries.get(collection.name)?.delete(qid);
|
|
59
|
-
};
|
|
60
|
-
const getQueryState = (selector, options) => {
|
|
61
|
-
const q = this.queries.get(collection.name)?.get(queryId(selector, options));
|
|
62
|
-
return q?.state ?? "active";
|
|
63
|
-
};
|
|
64
|
-
const getQueryError = (selector, options) => {
|
|
65
|
-
const q = this.queries.get(collection.name)?.get(queryId(selector, options));
|
|
66
|
-
return q?.error ?? null;
|
|
67
|
-
};
|
|
68
|
-
const getQueryResult = (selector, options) => {
|
|
69
|
-
const q = this.queries.get(collection.name)?.get(queryId(selector, options));
|
|
70
|
-
return q?.items ?? [];
|
|
71
|
-
};
|
|
72
|
-
const onQueryStateChange = (selector, options, callback) => {
|
|
73
|
-
const qid = queryId(selector, options);
|
|
74
|
-
const registry = this.queries.get(collection.name);
|
|
75
|
-
if (!registry)
|
|
76
|
-
throw new Error(`Collection ${collection.name} not initialized!`);
|
|
77
|
-
if (!registry.has(qid)) {
|
|
78
|
-
registry.set(qid, {
|
|
79
|
-
selector,
|
|
80
|
-
options,
|
|
81
|
-
state: "active",
|
|
82
|
-
error: null,
|
|
83
|
-
items: [],
|
|
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
|
-
dispose: async () => {
|
|
123
|
-
this.storageAdapters.delete(collection.name);
|
|
124
|
-
this.queries.delete(collection.name);
|
|
125
|
-
this.collectionIndices.delete(collection.name);
|
|
126
|
-
this.storageAdapterReady.delete(collection.name);
|
|
127
|
-
},
|
|
128
|
-
isReady: async () => {
|
|
129
|
-
await ready;
|
|
130
|
-
}
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
async setupStorage(collectionName, indices) {
|
|
134
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
135
|
-
if (!storage)
|
|
136
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
137
|
-
await Promise.all([
|
|
138
|
-
storage.createIndex("id"),
|
|
139
|
-
...indices.map((index) => storage.createIndex(index))
|
|
140
|
-
]);
|
|
141
|
-
await storage.setup();
|
|
142
|
-
}
|
|
143
|
-
ensureStorageAdapter(name) {
|
|
144
|
-
if (this.storageAdapters.has(name))
|
|
145
|
-
return;
|
|
146
|
-
const adapter = this.options.storage(name);
|
|
147
|
-
if (!adapter)
|
|
148
|
-
return;
|
|
149
|
-
this.storageAdapters.set(name, adapter);
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Compute and publish the result for a specific query
|
|
153
|
-
* @param collectionName - name of the collection
|
|
154
|
-
* @param selector - query selector
|
|
155
|
-
* @param options - query options
|
|
156
|
-
*/
|
|
157
|
-
async fulfillQuery(collectionName, selector, options) {
|
|
158
|
-
const qid = queryId(selector, options);
|
|
159
|
-
const registry = this.queries.get(collectionName);
|
|
160
|
-
if (!registry)
|
|
161
|
-
throw new Error(`Collection ${collectionName} not initialized!`);
|
|
162
|
-
const rec = registry.get(qid);
|
|
163
|
-
if (!rec)
|
|
164
|
-
return;
|
|
165
|
-
this.publishState(collectionName, qid, "active", null);
|
|
166
|
-
try {
|
|
167
|
-
const items = await this.executeQuery(collectionName, selector, options);
|
|
168
|
-
this.publishResult(collectionName, qid, items);
|
|
169
|
-
this.publishState(collectionName, qid, "complete", null);
|
|
170
|
-
} catch (error) {
|
|
171
|
-
this.publishState(collectionName, qid, "error", error);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
/**
|
|
175
|
-
* Notify listeners about state changes and keep the cache updated
|
|
176
|
-
* @param collectionName - name of the collection
|
|
177
|
-
* @param qid - query id
|
|
178
|
-
* @param state - new state
|
|
179
|
-
* @param error - error if state is 'error', null otherwise
|
|
180
|
-
*/
|
|
181
|
-
publishState(collectionName, qid, state, error) {
|
|
182
|
-
const rec = this.queries.get(collectionName)?.get(qid);
|
|
183
|
-
if (!rec)
|
|
184
|
-
return;
|
|
185
|
-
rec.state = state;
|
|
186
|
-
rec.error = error;
|
|
187
|
-
for (const callback of rec.listeners) {
|
|
188
|
-
try {
|
|
189
|
-
callback(state);
|
|
190
|
-
} catch (error_) {
|
|
191
|
-
this.onError(error_);
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
publishResult(collectionName, qid, items) {
|
|
196
|
-
const rec = this.queries.get(collectionName)?.get(qid);
|
|
197
|
-
if (!rec)
|
|
198
|
-
return;
|
|
199
|
-
rec.items = items;
|
|
200
|
-
}
|
|
201
|
-
async getIndexInfo(collectionName, selector) {
|
|
202
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
203
|
-
if (!storage)
|
|
204
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
205
|
-
if (selector != null && Object.keys(selector).length === 1 && "id" in selector && typeof selector.id !== "object") {
|
|
206
|
-
return { matched: true, ids: [selector.id], optimizedSelector: {} };
|
|
207
|
-
}
|
|
208
|
-
if (selector == null) {
|
|
209
|
-
return { matched: false, ids: [], optimizedSelector: {} };
|
|
210
|
-
}
|
|
211
|
-
const indices = this.collectionIndices.get(collectionName) ?? [];
|
|
212
|
-
return getIndexInfo(indices.map((field) => async (flatSelector) => {
|
|
213
|
-
if (!Object.hasOwnProperty.call(flatSelector, field))
|
|
214
|
-
return { matched: false };
|
|
215
|
-
const index = await storage.readIndex(field);
|
|
216
|
-
const fieldSelector = flatSelector[field];
|
|
217
|
-
const filtersForNull = fieldSelector == null || fieldSelector.$exists === false;
|
|
218
|
-
const keys = filtersForNull ? { include: null, exclude: [...index.keys()].filter((key) => key != null) } : getMatchingKeys(field, flatSelector);
|
|
219
|
-
if (keys.include == null && keys.exclude == null)
|
|
220
|
-
return { matched: false };
|
|
221
|
-
let includedIds = [];
|
|
222
|
-
if (keys.include == null) {
|
|
223
|
-
for (const set of index.values())
|
|
224
|
-
for (const pos of set)
|
|
225
|
-
includedIds.push(pos);
|
|
226
|
-
} else {
|
|
227
|
-
for (const key of keys.include) {
|
|
228
|
-
const idSet = index.get(key);
|
|
229
|
-
if (idSet)
|
|
230
|
-
for (const id of idSet)
|
|
231
|
-
includedIds.push(id);
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
if (keys.exclude != null) {
|
|
235
|
-
const excludeIds = /* @__PURE__ */ new Set();
|
|
236
|
-
for (const key of keys.exclude) {
|
|
237
|
-
const idSet = index.get(key);
|
|
238
|
-
if (idSet)
|
|
239
|
-
for (const id of idSet)
|
|
240
|
-
excludeIds.add(id);
|
|
241
|
-
}
|
|
242
|
-
includedIds = includedIds.filter((pos) => !excludeIds.has(pos));
|
|
243
|
-
}
|
|
244
|
-
return { matched: true, ids: includedIds, fields: [field], keepSelector: filtersForNull };
|
|
245
|
-
}), selector);
|
|
246
|
-
}
|
|
247
|
-
async queryItems(collectionName, selector) {
|
|
248
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
249
|
-
if (!storage)
|
|
250
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
251
|
-
const index = await this.getIndexInfo(collectionName, selector);
|
|
252
|
-
const matchItems = (item) => {
|
|
253
|
-
if (index.optimizedSelector == null)
|
|
254
|
-
return true;
|
|
255
|
-
if (Object.keys(index.optimizedSelector).length <= 0)
|
|
256
|
-
return true;
|
|
257
|
-
return match(item, index.optimizedSelector);
|
|
258
|
-
};
|
|
259
|
-
if (index.matched) {
|
|
260
|
-
const items = await storage.readIds(index.ids);
|
|
261
|
-
if (isEqual(index.optimizedSelector, {}))
|
|
262
|
-
return items;
|
|
263
|
-
return items.filter(matchItems);
|
|
264
|
-
} else {
|
|
265
|
-
const allItems = await storage.readAll();
|
|
266
|
-
if (isEqual(selector, {}))
|
|
267
|
-
return allItems;
|
|
268
|
-
return allItems.filter(matchItems);
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
async executeQuery(collectionName, selector, options) {
|
|
272
|
-
const items = await this.queryItems(collectionName, selector || {});
|
|
273
|
-
const { sort, skip, limit, fields } = options || {};
|
|
274
|
-
const sorted = sort ? sortItems(items, sort) : items;
|
|
275
|
-
const skipped = skip ? sorted.slice(skip) : sorted;
|
|
276
|
-
const limited = limit ? skipped.slice(0, limit) : skipped;
|
|
277
|
-
const idExcluded = fields && fields.id === 0;
|
|
278
|
-
return limited.map((item) => {
|
|
279
|
-
if (!fields)
|
|
280
|
-
return item;
|
|
281
|
-
return { ...idExcluded ? {} : { id: item.id }, ...project(item, fields) };
|
|
282
|
-
});
|
|
283
|
-
}
|
|
284
|
-
/**
|
|
285
|
-
* After mutations, recompute and push updates for affected active queries
|
|
286
|
-
* @param collectionName - name of the collection
|
|
287
|
-
* @param changedItems - items that were inserted/updated/replaced/removed
|
|
288
|
-
*/
|
|
289
|
-
async checkQueryUpdates(collectionName, changedItems) {
|
|
290
|
-
const registry = this.queries.get(collectionName);
|
|
291
|
-
if (!registry)
|
|
292
|
-
throw new Error(`Collection ${collectionName} not initialized!`);
|
|
293
|
-
if (registry.size === 0)
|
|
294
|
-
return;
|
|
295
|
-
const affected = [...registry.values()].filter(({ selector }) => changedItems.some((item) => match(item, selector)));
|
|
296
|
-
if (affected.length === 0)
|
|
297
|
-
return;
|
|
298
|
-
for (const { selector, options } of affected) {
|
|
299
|
-
const qid = queryId(selector, options);
|
|
300
|
-
this.publishState(collectionName, qid, "active", null);
|
|
301
|
-
}
|
|
302
|
-
await Promise.all(affected.map(async ({ selector, options }) => {
|
|
303
|
-
const qid = queryId(selector, options);
|
|
304
|
-
try {
|
|
305
|
-
const items = await this.executeQuery(collectionName, selector, options);
|
|
306
|
-
this.publishResult(collectionName, qid, items);
|
|
307
|
-
this.publishState(collectionName, qid, "complete", null);
|
|
308
|
-
} catch (error) {
|
|
309
|
-
this.publishState(collectionName, qid, "error", error);
|
|
310
|
-
}
|
|
311
|
-
}));
|
|
312
|
-
}
|
|
313
|
-
async insert(collectionName, newItem) {
|
|
314
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
315
|
-
if (!storage)
|
|
316
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
317
|
-
const existing = await this.executeQuery(collectionName, { id: newItem.id }, { limit: 1 });
|
|
318
|
-
if (existing.length > 0)
|
|
319
|
-
throw new Error(`Item with id ${String(newItem.id)} already exists`);
|
|
320
|
-
await storage.insert([newItem]);
|
|
321
|
-
await this.checkQueryUpdates(collectionName, [newItem]);
|
|
322
|
-
return newItem;
|
|
323
|
-
}
|
|
324
|
-
async updateOne(collectionName, selector, modifier) {
|
|
325
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
326
|
-
if (!storage)
|
|
327
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
328
|
-
const items = await this.executeQuery(collectionName, selector, { limit: 1 });
|
|
329
|
-
const [item] = items;
|
|
330
|
-
const { $setOnInsert, ...rest } = modifier;
|
|
331
|
-
if (item == null)
|
|
332
|
-
return [];
|
|
333
|
-
const modified = modify(deepClone(item), rest);
|
|
334
|
-
if (item.id !== modified.id) {
|
|
335
|
-
const existing = await this.executeQuery(collectionName, { id: modified.id }, { limit: 1 });
|
|
336
|
-
if (existing.length > 0)
|
|
337
|
-
throw new Error(`Item with id ${String(modified.id)} already exists`);
|
|
338
|
-
}
|
|
339
|
-
await storage.replace([modified]);
|
|
340
|
-
await this.checkQueryUpdates(collectionName, [modified]);
|
|
341
|
-
return [modified];
|
|
342
|
-
}
|
|
343
|
-
async updateMany(collectionName, selector, modifier) {
|
|
344
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
345
|
-
if (!storage)
|
|
346
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
347
|
-
const items = await this.executeQuery(collectionName, selector);
|
|
348
|
-
if (items.length === 0)
|
|
349
|
-
return [];
|
|
350
|
-
const { $setOnInsert, ...rest } = modifier;
|
|
351
|
-
const changed = await Promise.all(items.map(async (item) => {
|
|
352
|
-
const modified = modify(deepClone(item), rest);
|
|
353
|
-
if (item.id !== modified.id) {
|
|
354
|
-
const existing = await this.executeQuery(collectionName, { id: modified.id }, { limit: 1 });
|
|
355
|
-
if (existing.length > 0)
|
|
356
|
-
throw new Error(`Item with id ${String(modified.id)} already exists`);
|
|
357
|
-
}
|
|
358
|
-
return modified;
|
|
359
|
-
}));
|
|
360
|
-
await storage.replace(changed);
|
|
361
|
-
await this.checkQueryUpdates(collectionName, changed);
|
|
362
|
-
return changed;
|
|
363
|
-
}
|
|
364
|
-
async replaceOne(collectionName, selector, replacement) {
|
|
365
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
366
|
-
if (!storage)
|
|
367
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
368
|
-
const items = await this.executeQuery(collectionName, selector, { limit: 1 });
|
|
369
|
-
const [item] = items;
|
|
370
|
-
if (item == null)
|
|
371
|
-
return [];
|
|
372
|
-
const modified = { ...replacement, id: replacement.id ?? item.id };
|
|
373
|
-
if (item.id !== modified.id) {
|
|
374
|
-
const existing = await this.executeQuery(collectionName, { id: modified.id }, { limit: 1 });
|
|
375
|
-
if (existing.length > 0)
|
|
376
|
-
throw new Error(`Item with id ${String(modified.id)} already exists`);
|
|
377
|
-
}
|
|
378
|
-
await storage.replace([modified]);
|
|
379
|
-
await this.checkQueryUpdates(collectionName, [modified]);
|
|
380
|
-
return [modified];
|
|
381
|
-
}
|
|
382
|
-
async removeOne(collectionName, selector) {
|
|
383
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
384
|
-
if (!storage)
|
|
385
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
386
|
-
const items = await this.executeQuery(collectionName, selector, { limit: 1 });
|
|
387
|
-
const [item] = items;
|
|
388
|
-
if (item == null)
|
|
389
|
-
return [];
|
|
390
|
-
await storage.remove([item]);
|
|
391
|
-
await this.checkQueryUpdates(collectionName, [item]);
|
|
392
|
-
return [item];
|
|
393
|
-
}
|
|
394
|
-
async removeMany(collectionName, selector) {
|
|
395
|
-
const storage = this.storageAdapters.get(collectionName);
|
|
396
|
-
if (!storage)
|
|
397
|
-
throw new Error(`No persistence adapter for collection ${collectionName}`);
|
|
398
|
-
const items = await this.executeQuery(collectionName, selector);
|
|
399
|
-
if (items.length === 0)
|
|
400
|
-
return [];
|
|
401
|
-
await storage.remove(items);
|
|
402
|
-
await this.checkQueryUpdates(collectionName, items);
|
|
403
|
-
return items;
|
|
404
|
-
}
|
|
1
|
+
import isFieldExpression from "./index11.mjs";
|
|
2
|
+
import serializeValue from "./index12.mjs";
|
|
3
|
+
//#region src/utils/getMatchingKeys.ts
|
|
4
|
+
/**
|
|
5
|
+
* Extracts the matching and excluded keys for a given field in a selector.
|
|
6
|
+
* Supports serialized values and `$in`/`$nin` field expressions for optimization.
|
|
7
|
+
* Returns `null` for include/exclude if the field cannot be optimized.
|
|
8
|
+
* @template T - The type of the items in the selector.
|
|
9
|
+
* @template I - The type of the unique identifier for the items.
|
|
10
|
+
* @param field - The name of the field to extract matching keys for.
|
|
11
|
+
* @param selector - The selector object containing query criteria.
|
|
12
|
+
* @returns An object containing arrays of serialized included and excluded keys,
|
|
13
|
+
* or `null` if the field cannot be optimized.
|
|
14
|
+
*/
|
|
15
|
+
function getMatchingKeys(field, selector) {
|
|
16
|
+
const result = {
|
|
17
|
+
include: null,
|
|
18
|
+
exclude: null
|
|
19
|
+
};
|
|
20
|
+
const fieldSelector = selector[field];
|
|
21
|
+
if (fieldSelector instanceof RegExp) return result;
|
|
22
|
+
if (fieldSelector == null) return result;
|
|
23
|
+
if (isFieldExpression(fieldSelector)) {
|
|
24
|
+
if (fieldSelector.$ne != null) {
|
|
25
|
+
result.exclude = [serializeValue(fieldSelector.$ne)];
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
if (Array.isArray(fieldSelector.$in) && fieldSelector.$in.length > 0) {
|
|
29
|
+
result.include = fieldSelector.$in.map(serializeValue);
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
32
|
+
if (Array.isArray(fieldSelector.$nin) && fieldSelector.$nin.length > 0) {
|
|
33
|
+
result.exclude = fieldSelector.$nin.map(serializeValue);
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
include: null,
|
|
38
|
+
exclude: null
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
result.include = [serializeValue(fieldSelector)];
|
|
42
|
+
return result;
|
|
405
43
|
}
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
};
|
|
44
|
+
//#endregion
|
|
45
|
+
export { getMatchingKeys as default };
|
|
@@ -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;
|