@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/index26.mjs
CHANGED
|
@@ -1,30 +1,38 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
if (Array.isArray(fieldSelector.$in) && fieldSelector.$in.length > 0) {
|
|
16
|
-
result.include = fieldSelector.$in.map(serializeValue);
|
|
17
|
-
return result;
|
|
18
|
-
}
|
|
19
|
-
if (Array.isArray(fieldSelector.$nin) && fieldSelector.$nin.length > 0) {
|
|
20
|
-
result.exclude = fieldSelector.$nin.map(serializeValue);
|
|
21
|
-
return result;
|
|
22
|
-
}
|
|
23
|
-
return { include: null, exclude: null };
|
|
24
|
-
}
|
|
25
|
-
result.include = [serializeValue(fieldSelector)];
|
|
26
|
-
return result;
|
|
1
|
+
//#region src/utils/queryId.ts
|
|
2
|
+
/**
|
|
3
|
+
* Returns true when `options` is effectively "empty" for the purpose of queryId generation.
|
|
4
|
+
* Treats `undefined`/`null` as empty, and only considers plain objects with zero own enumerable keys as empty.
|
|
5
|
+
* Arrays and non-object values are not considered empty.
|
|
6
|
+
* @param options - Query options to test.
|
|
7
|
+
* @returns `true` if `options` is `null`/`undefined` or a plain object with no own keys; otherwise `false`.
|
|
8
|
+
*/
|
|
9
|
+
function isEmptyOptions(options) {
|
|
10
|
+
if (options == null) return true;
|
|
11
|
+
if (typeof options !== "object") return false;
|
|
12
|
+
if (Array.isArray(options)) return false;
|
|
13
|
+
return Object.keys(options).length === 0;
|
|
27
14
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
15
|
+
var noOptions = {};
|
|
16
|
+
var cache = /* @__PURE__ */ new WeakMap();
|
|
17
|
+
/**
|
|
18
|
+
* Generates a unique identifier for a query based on its selector and options.
|
|
19
|
+
* @param selector - The selector object.
|
|
20
|
+
* @param options - The query options object (optional).
|
|
21
|
+
* @returns A unique identifier string for the query.
|
|
22
|
+
*/
|
|
23
|
+
function queryId(selector, options) {
|
|
24
|
+
if (!(selector != null && typeof selector === "object" && (options == null || typeof options === "object"))) {
|
|
25
|
+
const optionsId = isEmptyOptions(options) ? -1 : JSON.stringify(options);
|
|
26
|
+
return `${JSON.stringify(selector)}:${optionsId}`;
|
|
27
|
+
}
|
|
28
|
+
const optionsKey = options ?? noOptions;
|
|
29
|
+
const cachedForSelector = cache.get(selector);
|
|
30
|
+
const cached = cachedForSelector?.get(optionsKey);
|
|
31
|
+
if (cached != null) return cached;
|
|
32
|
+
const id = `${JSON.stringify(selector)}:${isEmptyOptions(options) ? -1 : JSON.stringify(options)}`;
|
|
33
|
+
if (cachedForSelector) cachedForSelector.set(optionsKey, id);
|
|
34
|
+
else cache.set(selector, new WeakMap([[optionsKey, id]]));
|
|
35
|
+
return id;
|
|
36
|
+
}
|
|
37
|
+
//#endregion
|
|
38
|
+
export { queryId as default };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
const require_isFieldExpression = require("./index12.cjs.js");
|
|
2
|
+
//#region src/utils/idIndexQuery.ts
|
|
3
|
+
/**
|
|
4
|
+
* Resolves a selector on `id` into the ids it names, without consulting an index.
|
|
5
|
+
*
|
|
6
|
+
* `id` is the one field every storage adapter can look up directly — that is what
|
|
7
|
+
* `readIds` is — so a query on it never needs an index to be declared and never
|
|
8
|
+
* needs the whole collection to be read. This behaves like an index provider that
|
|
9
|
+
* happens to need no stored index, because the ids are already in the selector.
|
|
10
|
+
*
|
|
11
|
+
* Only inclusive forms can be answered this way. `$ne`/`$nin` describe everything
|
|
12
|
+
* except* something, which cannot be enumerated without knowing every id, so they
|
|
13
|
+
* report no match and take the ordinary path.
|
|
14
|
+
* @template T - The type of the items in the collection.
|
|
15
|
+
* @template I - The type of the unique identifier for the items.
|
|
16
|
+
* @param selector - The flat selector to resolve.
|
|
17
|
+
* @returns An index result naming the matched ids, or `{ matched: false }`.
|
|
18
|
+
*/
|
|
19
|
+
function idIndexQuery(selector) {
|
|
20
|
+
if (selector == null || !Object.hasOwnProperty.call(selector, "id")) return { matched: false };
|
|
21
|
+
const fieldSelector = selector.id;
|
|
22
|
+
if (fieldSelector == null || fieldSelector instanceof RegExp) return { matched: false };
|
|
23
|
+
if (require_isFieldExpression.default(fieldSelector)) {
|
|
24
|
+
const values = fieldSelector.$in;
|
|
25
|
+
if (!Array.isArray(values) || values.length <= 0) return { matched: false };
|
|
26
|
+
return {
|
|
27
|
+
matched: true,
|
|
28
|
+
ids: values,
|
|
29
|
+
fields: ["id"],
|
|
30
|
+
keepSelector: false
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
if (typeof fieldSelector === "object") return { matched: false };
|
|
34
|
+
return {
|
|
35
|
+
matched: true,
|
|
36
|
+
ids: [fieldSelector],
|
|
37
|
+
fields: ["id"],
|
|
38
|
+
keepSelector: false
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
42
|
+
exports.default = idIndexQuery;
|
package/dist/index27.mjs
CHANGED
|
@@ -1,47 +1,42 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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
|
-
return { enqueue, flush };
|
|
1
|
+
import isFieldExpression from "./index12.mjs";
|
|
2
|
+
//#region src/utils/idIndexQuery.ts
|
|
3
|
+
/**
|
|
4
|
+
* Resolves a selector on `id` into the ids it names, without consulting an index.
|
|
5
|
+
*
|
|
6
|
+
* `id` is the one field every storage adapter can look up directly — that is what
|
|
7
|
+
* `readIds` is — so a query on it never needs an index to be declared and never
|
|
8
|
+
* needs the whole collection to be read. This behaves like an index provider that
|
|
9
|
+
* happens to need no stored index, because the ids are already in the selector.
|
|
10
|
+
*
|
|
11
|
+
* Only inclusive forms can be answered this way. `$ne`/`$nin` describe everything
|
|
12
|
+
* except* something, which cannot be enumerated without knowing every id, so they
|
|
13
|
+
* report no match and take the ordinary path.
|
|
14
|
+
* @template T - The type of the items in the collection.
|
|
15
|
+
* @template I - The type of the unique identifier for the items.
|
|
16
|
+
* @param selector - The flat selector to resolve.
|
|
17
|
+
* @returns An index result naming the matched ids, or `{ matched: false }`.
|
|
18
|
+
*/
|
|
19
|
+
function idIndexQuery(selector) {
|
|
20
|
+
if (selector == null || !Object.hasOwnProperty.call(selector, "id")) return { matched: false };
|
|
21
|
+
const fieldSelector = selector.id;
|
|
22
|
+
if (fieldSelector == null || fieldSelector instanceof RegExp) return { matched: false };
|
|
23
|
+
if (isFieldExpression(fieldSelector)) {
|
|
24
|
+
const values = fieldSelector.$in;
|
|
25
|
+
if (!Array.isArray(values) || values.length <= 0) return { matched: false };
|
|
26
|
+
return {
|
|
27
|
+
matched: true,
|
|
28
|
+
ids: values,
|
|
29
|
+
fields: ["id"],
|
|
30
|
+
keepSelector: false
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
if (typeof fieldSelector === "object") return { matched: false };
|
|
34
|
+
return {
|
|
35
|
+
matched: true,
|
|
36
|
+
ids: [fieldSelector],
|
|
37
|
+
fields: ["id"],
|
|
38
|
+
keepSelector: false
|
|
39
|
+
};
|
|
44
40
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
};
|
|
41
|
+
//#endregion
|
|
42
|
+
export { idIndexQuery as default };
|
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
const require_isEqual = require("./index2.cjs.js");
|
|
2
|
+
const require_queryDelta = require("./index4.cjs.js");
|
|
3
|
+
const require_EventEmitter = require("./index7.cjs.js");
|
|
4
|
+
const require_serializeValue = require("./index13.cjs.js");
|
|
5
|
+
const require_createIndex = require("./index15.cjs.js");
|
|
6
|
+
const require_getIndexInfo = require("./index17.cjs.js");
|
|
7
|
+
const require_deepClone = require("./index18.cjs.js");
|
|
8
|
+
const require_match = require("./index19.cjs.js");
|
|
9
|
+
const require_modify = require("./index20.cjs.js");
|
|
10
|
+
const require_projectItems = require("./index23.cjs.js");
|
|
11
|
+
const require_sortItems = require("./index24.cjs.js");
|
|
12
|
+
const require_incrementalQueryUpdate = require("./index25.cjs.js");
|
|
13
|
+
const require_queryId = require("./index26.cjs.js");
|
|
14
|
+
const require_idIndexQuery = require("./index27.cjs.js");
|
|
15
|
+
//#region src/DefaultDataAdapter.ts
|
|
16
|
+
/**
|
|
17
|
+
* Checks if there are any pending updates in the given changeset.
|
|
18
|
+
* @template T - The type of the items in the changeset.
|
|
19
|
+
* @param pendingUpdates - The changeset to check for pending updates.
|
|
20
|
+
* @returns `true` if there are pending updates, otherwise `false`.
|
|
21
|
+
*/
|
|
22
|
+
function hasPendingUpdates(pendingUpdates) {
|
|
23
|
+
return pendingUpdates.added.length > 0 || pendingUpdates.modified.length > 0 || pendingUpdates.removed.length > 0;
|
|
24
|
+
}
|
|
25
|
+
var DefaultDataAdapter = class {
|
|
26
|
+
items = /* @__PURE__ */ new Map();
|
|
27
|
+
options;
|
|
28
|
+
storageAdapters = /* @__PURE__ */ new Map();
|
|
29
|
+
collections = /* @__PURE__ */ new Set();
|
|
30
|
+
indices = /* @__PURE__ */ new Map();
|
|
31
|
+
activeQueries = /* @__PURE__ */ new Map();
|
|
32
|
+
queryEmitters = /* @__PURE__ */ new Map();
|
|
33
|
+
queuedQueryUpdates = /* @__PURE__ */ new Map();
|
|
34
|
+
cachedQueryResults = /* @__PURE__ */ new Map();
|
|
35
|
+
constructor(options) {
|
|
36
|
+
this.options = options || {};
|
|
37
|
+
}
|
|
38
|
+
ensureStorageAdapter(name) {
|
|
39
|
+
if (this.storageAdapters.get(name)) return;
|
|
40
|
+
if (!this.options.storage) return;
|
|
41
|
+
const adapter = this.options.storage(name);
|
|
42
|
+
if (!adapter) return;
|
|
43
|
+
this.storageAdapters.set(name, adapter);
|
|
44
|
+
}
|
|
45
|
+
rebuildIndices(collection) {
|
|
46
|
+
const items = this.items.get(collection.name);
|
|
47
|
+
if (!items) throw new Error(`Items not found for collection ${collection.name}`);
|
|
48
|
+
const indices = this.indices.get(collection.name);
|
|
49
|
+
if (!indices) throw new Error(`Indices not found for collection ${collection.name}`);
|
|
50
|
+
indices.forEach((index) => index.rebuild([...items.values()]));
|
|
51
|
+
}
|
|
52
|
+
async setupStorageAdapter(collection) {
|
|
53
|
+
const storageAdapter = this.storageAdapters.get(collection.name);
|
|
54
|
+
if (!storageAdapter) return;
|
|
55
|
+
return storageAdapter.setup().then(async () => {
|
|
56
|
+
await this.fetchItemsFromStorage(collection);
|
|
57
|
+
}).catch((error) => {
|
|
58
|
+
if (!this.options.onError) {
|
|
59
|
+
console.error(`Error during data persistence operation in collection ${collection.name}`, error);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
this.options.onError(collection.name, error instanceof Error ? error : new Error(error));
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
getIndexInfo(collection, selector) {
|
|
66
|
+
if (selector != null && Object.keys(selector).length === 1 && "id" in selector) {
|
|
67
|
+
const idResult = require_idIndexQuery.default(selector);
|
|
68
|
+
if (idResult.matched) return {
|
|
69
|
+
matched: true,
|
|
70
|
+
ids: idResult.ids.map((id) => require_serializeValue.default(id)),
|
|
71
|
+
optimizedSelector: {}
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
if (selector == null) return {
|
|
75
|
+
matched: false,
|
|
76
|
+
ids: [],
|
|
77
|
+
optimizedSelector: {}
|
|
78
|
+
};
|
|
79
|
+
return require_getIndexInfo.default((this.indices.get(collection.name) ?? []).map((i) => i.query.bind(i)), selector);
|
|
80
|
+
}
|
|
81
|
+
applyIndexDeltas(collection, changes) {
|
|
82
|
+
const indices = this.indices.get(collection.name) ?? [];
|
|
83
|
+
for (const index of indices) {
|
|
84
|
+
if (changes.modified) index.update(changes.modified);
|
|
85
|
+
if (changes.added && changes.added.length > 0) index.insert(changes.added);
|
|
86
|
+
if (changes.removed && changes.removed.length > 0) index.remove(changes.removed);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
getItem(collection, selector) {
|
|
90
|
+
const memory = this.items.get(collection.name) ?? /* @__PURE__ */ new Map();
|
|
91
|
+
const indexInfo = this.getIndexInfo(collection, selector);
|
|
92
|
+
return (indexInfo.matched ? indexInfo.ids.map((id) => memory.get(require_serializeValue.default(id))).filter((i) => i != null) : [...memory.values()]).find((document) => require_match.default(document, selector));
|
|
93
|
+
}
|
|
94
|
+
queryItems(collection, selector) {
|
|
95
|
+
const indexInfo = this.getIndexInfo(collection, selector);
|
|
96
|
+
const matchItems = (item) => {
|
|
97
|
+
if (indexInfo.optimizedSelector == null) return true;
|
|
98
|
+
if (Object.keys(indexInfo.optimizedSelector).length <= 0) return true;
|
|
99
|
+
return require_match.default(item, indexInfo.optimizedSelector);
|
|
100
|
+
};
|
|
101
|
+
const items = this.items.get(collection.name);
|
|
102
|
+
if (!indexInfo.matched) {
|
|
103
|
+
if (require_isEqual.default(selector, {})) return [...items.values()];
|
|
104
|
+
return [...items.values()].filter(matchItems);
|
|
105
|
+
}
|
|
106
|
+
const foundItems = indexInfo.ids.map((ids) => items.get(require_serializeValue.default(ids))).filter((i) => i != null);
|
|
107
|
+
if (require_isEqual.default(indexInfo.optimizedSelector, {})) return foundItems;
|
|
108
|
+
return foundItems.filter(matchItems);
|
|
109
|
+
}
|
|
110
|
+
executeQuery(collection, selector, options) {
|
|
111
|
+
const items = this.queryItems(collection, selector || {});
|
|
112
|
+
const { sort, skip, limit, fields } = options || {};
|
|
113
|
+
const sorted = sort ? require_sortItems.default(items, sort) : items;
|
|
114
|
+
const skipped = skip ? sorted.slice(skip) : sorted;
|
|
115
|
+
return require_projectItems.default(limit ? skipped.slice(0, limit) : skipped, fields);
|
|
116
|
+
}
|
|
117
|
+
flushQueuedQueryUpdates(collection) {
|
|
118
|
+
if (!this.queuedQueryUpdates.get(collection.name)) return;
|
|
119
|
+
const changes = this.queuedQueryUpdates.get(collection.name);
|
|
120
|
+
if (!changes || !hasPendingUpdates(changes)) return;
|
|
121
|
+
this.queuedQueryUpdates.set(collection.name, {
|
|
122
|
+
added: [],
|
|
123
|
+
modified: [],
|
|
124
|
+
removed: []
|
|
125
|
+
});
|
|
126
|
+
const changeset = {
|
|
127
|
+
upserts: [...changes.added, ...changes.modified],
|
|
128
|
+
deletes: changes.removed.map((item) => item.id)
|
|
129
|
+
};
|
|
130
|
+
const flatItems = [
|
|
131
|
+
...changes.added,
|
|
132
|
+
...changes.modified,
|
|
133
|
+
...changes.removed
|
|
134
|
+
];
|
|
135
|
+
const itemIds = new Set(flatItems.map((i) => i.id));
|
|
136
|
+
[...this.activeQueries.get(collection.name)?.values() ?? []].filter(({ selector, options }) => {
|
|
137
|
+
if ((this.cachedQueryResults.get(collection.name)?.get(require_queryId.default(selector, options))?.map((i) => i.id) ?? []).some((id) => itemIds.has(id))) return true;
|
|
138
|
+
return flatItems.some((item) => require_match.default(item, selector));
|
|
139
|
+
}).forEach(({ selector, options }) => {
|
|
140
|
+
this.executeAndCacheQuery(collection, selector, options, changeset);
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
executeAndCacheQuery(collection, selector, options, changes) {
|
|
144
|
+
const cached = this.cachedQueryResults.get(collection.name)?.get(require_queryId.default(selector, options));
|
|
145
|
+
const result = (changes && cached ? require_incrementalQueryUpdate.default(cached, selector, options, changes) : null) ?? this.executeQuery(collection, selector, options);
|
|
146
|
+
const delta = cached ? require_queryDelta.diffQueryResults(cached, result) : void 0;
|
|
147
|
+
this.cachedQueryResults.set(collection.name, this.cachedQueryResults.get(collection.name) || /* @__PURE__ */ new Map());
|
|
148
|
+
this.cachedQueryResults.get(collection.name)?.set(require_queryId.default(selector, options), result);
|
|
149
|
+
const emitter = this.queryEmitters.get(collection.name);
|
|
150
|
+
if (!emitter) return;
|
|
151
|
+
if (delta && require_queryDelta.isEmptyQueryDelta(delta)) return;
|
|
152
|
+
emitter.emit("change", selector, options, "complete", delta);
|
|
153
|
+
}
|
|
154
|
+
updateQueries(collection, changes) {
|
|
155
|
+
this.queuedQueryUpdates.set(collection.name, this.queuedQueryUpdates.get(collection.name) || {
|
|
156
|
+
added: [],
|
|
157
|
+
modified: [],
|
|
158
|
+
removed: []
|
|
159
|
+
});
|
|
160
|
+
this.queuedQueryUpdates.get(collection.name)?.added.push(...changes.added);
|
|
161
|
+
this.queuedQueryUpdates.get(collection.name)?.modified.push(...changes.modified);
|
|
162
|
+
this.queuedQueryUpdates.get(collection.name)?.removed.push(...changes.removed);
|
|
163
|
+
this.flushQueuedQueryUpdates(collection);
|
|
164
|
+
}
|
|
165
|
+
createCollectionBackend(collection, indices) {
|
|
166
|
+
this.collections.add(collection);
|
|
167
|
+
this.ensureStorageAdapter(collection.name);
|
|
168
|
+
this.items.set(collection.name, this.items.get(collection.name) ?? /* @__PURE__ */ new Map());
|
|
169
|
+
this.queryEmitters.set(collection.name, this.queryEmitters.get(collection.name) ?? new require_EventEmitter.default());
|
|
170
|
+
this.queryEmitters.get(collection.name)?.setMaxListeners(Infinity);
|
|
171
|
+
this.activeQueries.set(collection.name, this.activeQueries.get(collection.name) || /* @__PURE__ */ new Map());
|
|
172
|
+
this.indices.set(collection.name, indices.map((field) => require_createIndex.default(field)));
|
|
173
|
+
this.rebuildIndices(collection);
|
|
174
|
+
const persistenceReadyPromise = this.setupStorageAdapter(collection);
|
|
175
|
+
const backend = {
|
|
176
|
+
insert: async (newItem) => {
|
|
177
|
+
const items = this.items.get(collection.name);
|
|
178
|
+
if (!items) throw new Error(`Items not found for collection ${collection.name}`);
|
|
179
|
+
if (items.has(require_serializeValue.default(newItem.id))) throw new Error(`Item with id '${newItem.id}' already exists`);
|
|
180
|
+
this.items.get(collection.name)?.set(require_serializeValue.default(newItem.id), newItem);
|
|
181
|
+
await this.storageAdapters.get(collection.name)?.insert([newItem]);
|
|
182
|
+
this.applyIndexDeltas(collection, { added: [newItem] });
|
|
183
|
+
this.updateQueries(collection, {
|
|
184
|
+
added: [],
|
|
185
|
+
modified: [newItem],
|
|
186
|
+
removed: []
|
|
187
|
+
});
|
|
188
|
+
return newItem;
|
|
189
|
+
},
|
|
190
|
+
updateOne: async (selector, modifier) => {
|
|
191
|
+
const { $setOnInsert, ...restModifier } = modifier;
|
|
192
|
+
const item = this.getItem(collection, selector);
|
|
193
|
+
if (item == null) return [];
|
|
194
|
+
const modifiedItem = require_modify.default(require_deepClone.default(item), restModifier);
|
|
195
|
+
if (item.id !== modifiedItem.id && this.getItem(collection, { id: modifiedItem.id }) != null) throw new Error(`Item with id '${modifiedItem.id}' already exists`);
|
|
196
|
+
this.items.get(collection.name)?.set(require_serializeValue.default(modifiedItem.id), modifiedItem);
|
|
197
|
+
await this.storageAdapters.get(collection.name)?.replace([modifiedItem]);
|
|
198
|
+
this.applyIndexDeltas(collection, { modified: [{
|
|
199
|
+
oldItem: item,
|
|
200
|
+
newItem: modifiedItem
|
|
201
|
+
}] });
|
|
202
|
+
this.updateQueries(collection, {
|
|
203
|
+
added: [],
|
|
204
|
+
modified: [modifiedItem],
|
|
205
|
+
removed: []
|
|
206
|
+
});
|
|
207
|
+
return [modifiedItem];
|
|
208
|
+
},
|
|
209
|
+
updateMany: async (selector, modifier) => {
|
|
210
|
+
const { $setOnInsert, ...restModifier } = modifier;
|
|
211
|
+
const items = this.executeQuery(collection, selector, {});
|
|
212
|
+
const changedItems = items.map((item) => {
|
|
213
|
+
const modifiedItem = require_modify.default(require_deepClone.default(item), restModifier);
|
|
214
|
+
if (item.id !== modifiedItem.id && this.getItem(collection, { id: modifiedItem.id }) != null) throw new Error(`Item with id '${modifiedItem.id}' already exists`);
|
|
215
|
+
return modifiedItem;
|
|
216
|
+
});
|
|
217
|
+
changedItems.forEach((item) => {
|
|
218
|
+
this.items.get(collection.name)?.set(require_serializeValue.default(item.id), item);
|
|
219
|
+
});
|
|
220
|
+
await this.storageAdapters.get(collection.name)?.replace(changedItems);
|
|
221
|
+
const pairs = items.map((oldItem, index) => ({
|
|
222
|
+
oldItem,
|
|
223
|
+
newItem: changedItems[index]
|
|
224
|
+
}));
|
|
225
|
+
this.applyIndexDeltas(collection, { modified: pairs });
|
|
226
|
+
this.updateQueries(collection, {
|
|
227
|
+
added: [],
|
|
228
|
+
modified: changedItems,
|
|
229
|
+
removed: []
|
|
230
|
+
});
|
|
231
|
+
return changedItems;
|
|
232
|
+
},
|
|
233
|
+
replaceOne: async (selector, replacement) => {
|
|
234
|
+
const item = this.getItem(collection, selector);
|
|
235
|
+
if (item == null) return [];
|
|
236
|
+
if (item.id !== replacement.id && replacement.id != null && this.getItem(collection, { id: replacement.id }) != null) throw new Error(`Item with id '${replacement.id}' already exists`);
|
|
237
|
+
const modifiedItem = {
|
|
238
|
+
id: item.id,
|
|
239
|
+
...replacement
|
|
240
|
+
};
|
|
241
|
+
this.items.get(collection.name)?.set(require_serializeValue.default(modifiedItem.id), modifiedItem);
|
|
242
|
+
await this.storageAdapters.get(collection.name)?.replace([modifiedItem]);
|
|
243
|
+
this.applyIndexDeltas(collection, { modified: [{
|
|
244
|
+
oldItem: item,
|
|
245
|
+
newItem: modifiedItem
|
|
246
|
+
}] });
|
|
247
|
+
this.updateQueries(collection, {
|
|
248
|
+
added: [],
|
|
249
|
+
modified: [modifiedItem],
|
|
250
|
+
removed: []
|
|
251
|
+
});
|
|
252
|
+
return [modifiedItem];
|
|
253
|
+
},
|
|
254
|
+
removeOne: async (selector) => {
|
|
255
|
+
const item = this.getItem(collection, selector);
|
|
256
|
+
if (item == null) return [];
|
|
257
|
+
this.items.get(collection.name)?.delete(require_serializeValue.default(item.id));
|
|
258
|
+
await this.storageAdapters.get(collection.name)?.remove([item]);
|
|
259
|
+
this.applyIndexDeltas(collection, { removed: [item] });
|
|
260
|
+
this.updateQueries(collection, {
|
|
261
|
+
added: [],
|
|
262
|
+
modified: [],
|
|
263
|
+
removed: [item]
|
|
264
|
+
});
|
|
265
|
+
return [item];
|
|
266
|
+
},
|
|
267
|
+
removeMany: async (selector) => {
|
|
268
|
+
const items = backend.getQueryResult(selector, {});
|
|
269
|
+
items.forEach((item) => {
|
|
270
|
+
this.items.get(collection.name)?.delete(require_serializeValue.default(item.id));
|
|
271
|
+
});
|
|
272
|
+
await this.storageAdapters.get(collection.name)?.remove(items);
|
|
273
|
+
this.applyIndexDeltas(collection, { removed: items });
|
|
274
|
+
this.updateQueries(collection, {
|
|
275
|
+
added: [],
|
|
276
|
+
modified: [],
|
|
277
|
+
removed: items
|
|
278
|
+
});
|
|
279
|
+
return items;
|
|
280
|
+
},
|
|
281
|
+
registerQuery: (selector, options) => {
|
|
282
|
+
this.activeQueries.set(collection.name, this.activeQueries.get(collection.name) || /* @__PURE__ */ new Map());
|
|
283
|
+
this.activeQueries.get(collection.name)?.set(require_queryId.default(selector, options), {
|
|
284
|
+
selector,
|
|
285
|
+
options
|
|
286
|
+
});
|
|
287
|
+
this.executeAndCacheQuery(collection, selector, options);
|
|
288
|
+
},
|
|
289
|
+
unregisterQuery: (selector, options) => {
|
|
290
|
+
if (!this.activeQueries.get(collection.name)) return;
|
|
291
|
+
this.activeQueries.get(collection.name)?.delete(require_queryId.default(selector, options));
|
|
292
|
+
},
|
|
293
|
+
getQueryState: () => "complete",
|
|
294
|
+
onQueryStateChange: (selector, options, callback) => {
|
|
295
|
+
const emitter = this.queryEmitters.get(collection.name);
|
|
296
|
+
if (!emitter) throw new Error(`Query emitter not found for collection ${collection.name}`);
|
|
297
|
+
const handler = (querySelector, queryOptions, state, delta) => {
|
|
298
|
+
if (require_queryId.default(querySelector, queryOptions) !== require_queryId.default(selector, options)) return;
|
|
299
|
+
require_queryDelta.callWithDelta(callback, state, delta);
|
|
300
|
+
};
|
|
301
|
+
emitter.on("change", handler);
|
|
302
|
+
return () => {
|
|
303
|
+
emitter.off("change", handler);
|
|
304
|
+
};
|
|
305
|
+
},
|
|
306
|
+
getQueryError: () => null,
|
|
307
|
+
getQueryResult: (selector, options) => {
|
|
308
|
+
if (this.activeQueries.get(collection.name)?.has(require_queryId.default(selector, options))) {
|
|
309
|
+
const results = this.cachedQueryResults.get(collection.name)?.get(require_queryId.default(selector, options));
|
|
310
|
+
if (!results) throw new Error("Cached query results are not defined!");
|
|
311
|
+
return results;
|
|
312
|
+
}
|
|
313
|
+
return this.executeQuery(collection, selector, options);
|
|
314
|
+
},
|
|
315
|
+
executeQuery: (selector, options) => Promise.resolve(this.executeQuery(collection, selector, options)),
|
|
316
|
+
dispose: async () => {
|
|
317
|
+
const adapter = this.storageAdapters.get(collection.name);
|
|
318
|
+
if (adapter?.teardown) await adapter.teardown();
|
|
319
|
+
this.storageAdapters.delete(collection.name);
|
|
320
|
+
this.items.delete(collection.name);
|
|
321
|
+
this.indices.delete(collection.name);
|
|
322
|
+
this.activeQueries.delete(collection.name);
|
|
323
|
+
this.queryEmitters.delete(collection.name);
|
|
324
|
+
this.queuedQueryUpdates.delete(collection.name);
|
|
325
|
+
this.cachedQueryResults.delete(collection.name);
|
|
326
|
+
},
|
|
327
|
+
isReady: () => persistenceReadyPromise
|
|
328
|
+
};
|
|
329
|
+
return backend;
|
|
330
|
+
}
|
|
331
|
+
async fetchItemsFromStorage(collection) {
|
|
332
|
+
if (!collection) {
|
|
333
|
+
await Promise.all([...this.collections].map((currentCollection) => this.fetchItemsFromStorage(currentCollection)));
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
const storageAdapter = this.storageAdapters.get(collection.name);
|
|
337
|
+
if (!storageAdapter) return;
|
|
338
|
+
const items = await storageAdapter.readAll();
|
|
339
|
+
this.items.set(collection.name, items.reduce((map, item) => {
|
|
340
|
+
map.set(require_serializeValue.default(item.id), item);
|
|
341
|
+
return map;
|
|
342
|
+
}, /* @__PURE__ */ new Map()));
|
|
343
|
+
this.rebuildIndices(collection);
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
//#endregion
|
|
347
|
+
exports.default = DefaultDataAdapter;
|