@signaldb/svelte 1.1.1 → 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/base/core/src/AsyncDataAdapter.d.ts +103 -0
- package/dist/base/core/src/AsyncDataAdapter.js +518 -0
- package/dist/base/core/src/AutoFetchDataAdapter.d.ts +112 -0
- package/dist/base/core/src/AutoFetchDataAdapter.js +652 -0
- package/dist/base/core/src/Collection/Cursor.d.ts +113 -0
- package/dist/base/core/src/Collection/Cursor.js +293 -0
- package/dist/base/core/src/Collection/Observer.d.ts +64 -0
- package/dist/base/core/src/Collection/Observer.js +181 -0
- package/dist/base/core/src/Collection/index.d.ts +300 -0
- package/dist/base/core/src/Collection/index.js +625 -0
- package/dist/base/core/src/Collection/types.d.ts +28 -0
- package/dist/base/core/src/Collection/types.js +2 -0
- package/dist/base/core/src/DataAdapter.d.ts +41 -0
- package/dist/base/core/src/DataAdapter.js +2 -0
- package/dist/base/core/src/DefaultDataAdapter.d.ts +35 -0
- package/dist/base/core/src/DefaultDataAdapter.js +398 -0
- package/dist/base/core/src/WorkerDataAdapter.d.ts +25 -0
- package/dist/base/core/src/WorkerDataAdapter.js +211 -0
- package/dist/base/core/src/WorkerDataAdapterHost.d.ts +62 -0
- package/dist/base/core/src/WorkerDataAdapterHost.js +409 -0
- package/dist/base/core/src/createIndex.d.ts +7 -0
- package/dist/base/core/src/createIndex.js +120 -0
- package/dist/base/core/src/createIndexProvider.d.ts +8 -0
- package/dist/base/core/src/createIndexProvider.js +11 -0
- package/dist/base/core/src/createReactivityAdapter.d.ts +8 -0
- package/dist/base/core/src/createReactivityAdapter.js +11 -0
- package/dist/base/core/src/createStorageAdapter.d.ts +9 -0
- package/dist/base/core/src/createStorageAdapter.js +11 -0
- package/dist/base/core/src/getIndexInfo.d.ts +39 -0
- package/dist/base/core/src/getIndexInfo.js +176 -0
- package/dist/base/core/src/index.d.ts +22 -0
- package/dist/base/core/src/index.js +39 -0
- package/dist/base/core/src/types/Dependency.d.ts +4 -0
- package/dist/base/core/src/types/Dependency.js +2 -0
- package/dist/base/core/src/types/IndexProvider.d.ts +26 -0
- package/dist/base/core/src/types/IndexProvider.js +2 -0
- package/dist/base/core/src/types/Modifier.d.ts +46 -0
- package/dist/base/core/src/types/Modifier.js +3 -0
- package/dist/base/core/src/types/ReactivityAdapter.d.ts +6 -0
- package/dist/base/core/src/types/ReactivityAdapter.js +2 -0
- package/dist/base/core/src/types/Selector.d.ts +46 -0
- package/dist/base/core/src/types/Selector.js +3 -0
- package/dist/base/core/src/types/Signal.d.ts +4 -0
- package/dist/base/core/src/types/Signal.js +2 -0
- package/dist/base/core/src/types/StorageAdapter.d.ts +20 -0
- package/dist/base/core/src/types/StorageAdapter.js +2 -0
- package/dist/base/core/src/utils/EventEmitter.d.ts +71 -0
- package/dist/base/core/src/utils/EventEmitter.js +144 -0
- package/dist/base/core/src/utils/batchOnNextTick.d.ts +16 -0
- package/dist/base/core/src/utils/batchOnNextTick.js +72 -0
- package/dist/base/core/src/utils/compact.d.ts +9 -0
- package/dist/base/core/src/utils/compact.js +21 -0
- package/dist/base/core/src/utils/createSignal.d.ts +14 -0
- package/dist/base/core/src/utils/createSignal.js +38 -0
- package/dist/base/core/src/utils/deepClone.d.ts +17 -0
- package/dist/base/core/src/utils/deepClone.js +68 -0
- package/dist/base/core/src/utils/get.d.ts +9 -0
- package/dist/base/core/src/utils/get.js +27 -0
- package/dist/base/core/src/utils/getMatchingKeys.d.ts +19 -0
- package/dist/base/core/src/utils/getMatchingKeys.js +49 -0
- package/dist/base/core/src/utils/intersection.d.ts +9 -0
- package/dist/base/core/src/utils/intersection.js +16 -0
- package/dist/base/core/src/utils/isEqual.d.ts +14 -0
- package/dist/base/core/src/utils/isEqual.js +43 -0
- package/dist/base/core/src/utils/isFieldExpression.d.ts +11 -0
- package/dist/base/core/src/utils/isFieldExpression.js +52 -0
- package/dist/base/core/src/utils/match.d.ts +12 -0
- package/dist/base/core/src/utils/match.js +16 -0
- package/dist/base/core/src/utils/modify.d.ts +14 -0
- package/dist/base/core/src/utils/modify.js +29 -0
- package/dist/base/core/src/utils/project.d.ts +15 -0
- package/dist/base/core/src/utils/project.js +43 -0
- package/dist/base/core/src/utils/queryId.d.ts +9 -0
- package/dist/base/core/src/utils/queryId.js +30 -0
- package/dist/base/core/src/utils/randomId.d.ts +7 -0
- package/dist/base/core/src/utils/randomId.js +12 -0
- package/dist/base/core/src/utils/reactiveOrAsync.d.ts +59 -0
- package/dist/base/core/src/utils/reactiveOrAsync.js +93 -0
- package/dist/base/core/src/utils/serializeValue.d.ts +12 -0
- package/dist/base/core/src/utils/serializeValue.js +27 -0
- package/dist/base/core/src/utils/set.d.ts +13 -0
- package/dist/base/core/src/utils/set.js +43 -0
- package/dist/base/core/src/utils/sortItems.d.ts +12 -0
- package/dist/base/core/src/utils/sortItems.js +23 -0
- package/dist/base/core/src/utils/uniqueBy.d.ts +10 -0
- package/dist/base/core/src/utils/uniqueBy.js +19 -0
- package/dist/index.cjs +38 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.mjs +38 -0
- package/dist/index.mjs.map +1 -0
- package/dist/index.svelte.d.ts +7 -3
- package/dist/index.svelte.js +30 -28
- package/dist/reactivity-adapters/svelte/index.svelte.d.ts +10 -0
- package/dist/reactivity-adapters/svelte/index.svelte.js +36 -0
- package/package.json +4 -4
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { BaseItem } from './Collection/types';
|
|
2
|
+
/**
|
|
3
|
+
* creates an index for a specific field
|
|
4
|
+
* @param field name of the field
|
|
5
|
+
* @returns an index provider to pass to the `indices` option of the collection constructor
|
|
6
|
+
*/
|
|
7
|
+
export default function createIndex<T extends BaseItem<I> = BaseItem, I = any>(field: string): import("./types/IndexProvider").default<T, I>;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.default = createIndex;
|
|
7
|
+
const createIndexProvider_1 = __importDefault(require("./createIndexProvider"));
|
|
8
|
+
const get_1 = __importDefault(require("./utils/get"));
|
|
9
|
+
const getMatchingKeys_1 = __importDefault(require("./utils/getMatchingKeys"));
|
|
10
|
+
const serializeValue_1 = __importDefault(require("./utils/serializeValue"));
|
|
11
|
+
/**
|
|
12
|
+
* creates an index for a specific field
|
|
13
|
+
* @param field name of the field
|
|
14
|
+
* @returns an index provider to pass to the `indices` option of the collection constructor
|
|
15
|
+
*/
|
|
16
|
+
function createIndex(field) {
|
|
17
|
+
const index = new Map();
|
|
18
|
+
const ensureSet = (key) => {
|
|
19
|
+
let set = index.get(key);
|
|
20
|
+
if (!set) {
|
|
21
|
+
set = new Set();
|
|
22
|
+
index.set(key, set);
|
|
23
|
+
}
|
|
24
|
+
return set;
|
|
25
|
+
};
|
|
26
|
+
return (0, createIndexProvider_1.default)({
|
|
27
|
+
query(selector) {
|
|
28
|
+
if (!Object.hasOwnProperty.call(selector, field)) {
|
|
29
|
+
// If the field is not present in the selector, we can't optimize
|
|
30
|
+
return { matched: false };
|
|
31
|
+
}
|
|
32
|
+
const fieldSelector = selector[field];
|
|
33
|
+
const filteresForNull = fieldSelector == null || fieldSelector.$exists === false;
|
|
34
|
+
const keys = filteresForNull
|
|
35
|
+
? { include: null, exclude: [...index.keys()].filter(key => key != null) }
|
|
36
|
+
: (0, getMatchingKeys_1.default)(field, selector);
|
|
37
|
+
if (keys.include == null && keys.exclude == null)
|
|
38
|
+
return { matched: false };
|
|
39
|
+
// Accumulate included positions
|
|
40
|
+
let includedIds = [];
|
|
41
|
+
if (keys.include == null) {
|
|
42
|
+
for (const set of index.values()) {
|
|
43
|
+
for (const pos of set) {
|
|
44
|
+
includedIds.push(pos);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
for (const key of keys.include) {
|
|
50
|
+
const posSet = index.get(key);
|
|
51
|
+
if (posSet) {
|
|
52
|
+
for (const pos of posSet) {
|
|
53
|
+
includedIds.push(pos);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
// If exclusion is specified, build a single set of all positions to exclude.
|
|
59
|
+
if (keys.exclude != null) {
|
|
60
|
+
const excludeIds = new Set();
|
|
61
|
+
for (const key of keys.exclude) {
|
|
62
|
+
const posSet = index.get(key);
|
|
63
|
+
if (posSet) {
|
|
64
|
+
for (const pos of posSet) {
|
|
65
|
+
excludeIds.add(pos);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
// Filter out any position that exists in the exclude set.
|
|
70
|
+
includedIds = includedIds.filter(pos => !excludeIds.has(pos));
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
matched: true,
|
|
74
|
+
ids: includedIds,
|
|
75
|
+
fields: [field],
|
|
76
|
+
keepSelector: filteresForNull,
|
|
77
|
+
};
|
|
78
|
+
},
|
|
79
|
+
rebuild(items) {
|
|
80
|
+
index.clear();
|
|
81
|
+
items.forEach((item) => {
|
|
82
|
+
const value = (0, serializeValue_1.default)((0, get_1.default)(item, field));
|
|
83
|
+
ensureSet(value).add(item.id);
|
|
84
|
+
});
|
|
85
|
+
},
|
|
86
|
+
// NEW: delta methods
|
|
87
|
+
insert(items) {
|
|
88
|
+
for (const item of items) {
|
|
89
|
+
const value = (0, serializeValue_1.default)((0, get_1.default)(item, field));
|
|
90
|
+
ensureSet(value).add(item.id);
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
remove(items) {
|
|
94
|
+
for (const item of items) {
|
|
95
|
+
const value = (0, serializeValue_1.default)((0, get_1.default)(item, field));
|
|
96
|
+
const set = index.get(value);
|
|
97
|
+
if (!set)
|
|
98
|
+
continue;
|
|
99
|
+
set.delete(item.id);
|
|
100
|
+
if (set.size === 0)
|
|
101
|
+
index.delete(value);
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
update(pairs) {
|
|
105
|
+
for (const { oldItem, newItem } of pairs) {
|
|
106
|
+
const oldValue = (0, serializeValue_1.default)((0, get_1.default)(oldItem, field));
|
|
107
|
+
const newValue = (0, serializeValue_1.default)((0, get_1.default)(newItem, field));
|
|
108
|
+
if (oldValue === newValue)
|
|
109
|
+
continue;
|
|
110
|
+
const oldSet = index.get(oldValue);
|
|
111
|
+
if (oldSet) {
|
|
112
|
+
oldSet.delete(oldItem.id);
|
|
113
|
+
if (oldSet.size === 0)
|
|
114
|
+
index.delete(oldValue);
|
|
115
|
+
}
|
|
116
|
+
ensureSet(newValue).add(newItem.id);
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { BaseItem } from './Collection';
|
|
2
|
+
import type IndexProvider from './types/IndexProvider';
|
|
3
|
+
/**
|
|
4
|
+
* Creates an IndexProvider based on the given definition.
|
|
5
|
+
* @param definition - The definition of the IndexProvider.
|
|
6
|
+
* @returns The created IndexProvider.
|
|
7
|
+
*/
|
|
8
|
+
export default function createIndexProvider<T extends BaseItem<I> = BaseItem, I = any>(definition: IndexProvider<T, I>): IndexProvider<T, I>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = createIndexProvider;
|
|
4
|
+
/**
|
|
5
|
+
* Creates an IndexProvider based on the given definition.
|
|
6
|
+
* @param definition - The definition of the IndexProvider.
|
|
7
|
+
* @returns The created IndexProvider.
|
|
8
|
+
*/
|
|
9
|
+
function createIndexProvider(definition) {
|
|
10
|
+
return definition;
|
|
11
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type Dependency from './types/Dependency';
|
|
2
|
+
import type ReactivityAdapter from './types/ReactivityAdapter';
|
|
3
|
+
/**
|
|
4
|
+
* Creates an ReactivityAdapter based on the given definition.
|
|
5
|
+
* @param definition - The definition of the ReactivityAdapter.
|
|
6
|
+
* @returns The created ReactivityAdapter.
|
|
7
|
+
*/
|
|
8
|
+
export default function createReactivityAdapter<T extends Dependency = Dependency>(definition: ReactivityAdapter<T>): ReactivityAdapter<T>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = createReactivityAdapter;
|
|
4
|
+
/**
|
|
5
|
+
* Creates an ReactivityAdapter based on the given definition.
|
|
6
|
+
* @param definition - The definition of the ReactivityAdapter.
|
|
7
|
+
* @returns The created ReactivityAdapter.
|
|
8
|
+
*/
|
|
9
|
+
function createReactivityAdapter(definition) {
|
|
10
|
+
return definition;
|
|
11
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type StorageAdapter from './types/StorageAdapter';
|
|
2
|
+
/**
|
|
3
|
+
* Creates an StorageAdapter based on the given definition.
|
|
4
|
+
* @param definition - The definition of the StorageAdapter.
|
|
5
|
+
* @returns The created StorageAdapter.
|
|
6
|
+
*/
|
|
7
|
+
export default function createStorageAdapter<T extends {
|
|
8
|
+
id: I;
|
|
9
|
+
} & Record<string, any>, I>(definition: StorageAdapter<T, I>): StorageAdapter<T, I>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = createStorageAdapter;
|
|
4
|
+
/**
|
|
5
|
+
* Creates an StorageAdapter based on the given definition.
|
|
6
|
+
* @param definition - The definition of the StorageAdapter.
|
|
7
|
+
* @returns The created StorageAdapter.
|
|
8
|
+
*/
|
|
9
|
+
function createStorageAdapter(definition) {
|
|
10
|
+
return definition;
|
|
11
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { AsynchronousQueryFunction, SynchronousQueryFunction } from './types/IndexProvider';
|
|
2
|
+
import type { FlatSelector } from './types/Selector';
|
|
3
|
+
import type Selector from './types/Selector';
|
|
4
|
+
import type { BaseItem } from './Collection/types';
|
|
5
|
+
type IndexInfo<T extends BaseItem<I> = BaseItem, I = any> = {
|
|
6
|
+
matched: boolean;
|
|
7
|
+
ids: I[];
|
|
8
|
+
optimizedSelector: FlatSelector<T>;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Retrieves merged index information for a given flat selector by querying multiple
|
|
12
|
+
* index providers. Combines results from all index providers to determine matched ids
|
|
13
|
+
* and an optimized selector.
|
|
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 queryFunctions - An array of index providers to query.
|
|
17
|
+
* @param selector - The flat selector used to filter items.
|
|
18
|
+
* @returns An object containing:
|
|
19
|
+
* - `matched`: A boolean indicating if the selector matched any items.
|
|
20
|
+
* - `ids`: An array of matched item ids.
|
|
21
|
+
* - `optimizedSelector`: A flat selector optimized based on the index results.
|
|
22
|
+
*/
|
|
23
|
+
export declare function getMergedIndexInfo<T extends BaseItem<I> = BaseItem, I = any>(queryFunctions: (SynchronousQueryFunction<T, I> | AsynchronousQueryFunction<T, I>)[], selector: FlatSelector<T>): IndexInfo<T, I> | Promise<IndexInfo<T, I>>;
|
|
24
|
+
/**
|
|
25
|
+
* Retrieves index information for a given complex selector by querying multiple
|
|
26
|
+
* index providers. Handles nested `$and` and `$or` conditions in the selector and
|
|
27
|
+
* optimizes the selector to minimize processing overhead.
|
|
28
|
+
* @template T - The type of the items in the collection.
|
|
29
|
+
* @template I - The type of the unique identifier for the items.
|
|
30
|
+
* @param queryFunctions - An array of index providers to query.
|
|
31
|
+
* @param selector - The complex selector used to filter items.
|
|
32
|
+
* @returns An object containing:
|
|
33
|
+
* - `matched`: A boolean indicating if the selector matched any items.
|
|
34
|
+
* - `ids`: An array of matched item ids.
|
|
35
|
+
* - `optimizedSelector`: A selector optimized based on the index results, with unused
|
|
36
|
+
* conditions removed.
|
|
37
|
+
*/
|
|
38
|
+
export default function getIndexInfo<QueryFunction extends SynchronousQueryFunction<T, I> | AsynchronousQueryFunction<T, I>, T extends BaseItem<I> = BaseItem, I = any>(queryFunctions: QueryFunction[], selector: Selector<T>): QueryFunction extends AsynchronousQueryFunction<T, I> ? Promise<IndexInfo<T, I>> : IndexInfo<T, I>;
|
|
39
|
+
export {};
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getMergedIndexInfo = getMergedIndexInfo;
|
|
7
|
+
exports.default = getIndexInfo;
|
|
8
|
+
const intersection_1 = __importDefault(require("./utils/intersection"));
|
|
9
|
+
/**
|
|
10
|
+
* Retrieves merged index information for a given flat selector by querying multiple
|
|
11
|
+
* index providers. Combines results from all index providers to determine matched ids
|
|
12
|
+
* and an optimized selector.
|
|
13
|
+
* @template T - The type of the items in the collection.
|
|
14
|
+
* @template I - The type of the unique identifier for the items.
|
|
15
|
+
* @param queryFunctions - An array of index providers to query.
|
|
16
|
+
* @param selector - The flat selector used to filter items.
|
|
17
|
+
* @returns An object containing:
|
|
18
|
+
* - `matched`: A boolean indicating if the selector matched any items.
|
|
19
|
+
* - `ids`: An array of matched item ids.
|
|
20
|
+
* - `optimizedSelector`: A flat selector optimized based on the index results.
|
|
21
|
+
*/
|
|
22
|
+
function getMergedIndexInfo(queryFunctions, selector) {
|
|
23
|
+
return queryFunctions.reduce((memoOrPromise, queryFunction) => {
|
|
24
|
+
const resultOrPromise = queryFunction(selector);
|
|
25
|
+
const processResult = (memo, result) => {
|
|
26
|
+
if (!result.matched)
|
|
27
|
+
return memo;
|
|
28
|
+
const optimizedSelector = result.keepSelector
|
|
29
|
+
? memo.optimizedSelector
|
|
30
|
+
: Object.fromEntries(Object.entries(memo.optimizedSelector)
|
|
31
|
+
.filter(([key]) => !result.fields.includes(key)));
|
|
32
|
+
return {
|
|
33
|
+
matched: true,
|
|
34
|
+
ids: [...new Set(memo.matched
|
|
35
|
+
? (0, intersection_1.default)(memo.ids, result.ids)
|
|
36
|
+
: result.ids)],
|
|
37
|
+
optimizedSelector,
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
if (resultOrPromise instanceof Promise) {
|
|
41
|
+
return resultOrPromise.then(async (result) => {
|
|
42
|
+
const memo = memoOrPromise instanceof Promise ? await memoOrPromise : memoOrPromise;
|
|
43
|
+
return processResult(memo, result);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
const memo = memoOrPromise;
|
|
47
|
+
if (memo instanceof Promise)
|
|
48
|
+
throw new Error('Mixing async and sync index providers is not supported');
|
|
49
|
+
return processResult(memo, resultOrPromise);
|
|
50
|
+
}, {
|
|
51
|
+
matched: false,
|
|
52
|
+
ids: [],
|
|
53
|
+
optimizedSelector: { ...selector },
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Optimizes a logic gate ($and/$or) of selectors by querying multiple index providers.
|
|
58
|
+
* @param queryFunctions - An array of index providers to query.
|
|
59
|
+
* @param logicGate - An array of selectors representing the logic gate.
|
|
60
|
+
* @param idsCallback - A callback function called with matched ids.
|
|
61
|
+
* @returns An array of optimized selectors or a promise that resolves to such an array.
|
|
62
|
+
*/
|
|
63
|
+
function optimizeLogicGate(queryFunctions, logicGate, idsCallback) {
|
|
64
|
+
return logicGate.reduce((memoOrPromise, sel) => {
|
|
65
|
+
const getSelector = (indexInfo) => {
|
|
66
|
+
const { matched: selMatched, ids: selIds, optimizedSelector, } = indexInfo;
|
|
67
|
+
if (selMatched) {
|
|
68
|
+
idsCallback(true, selIds);
|
|
69
|
+
if (Object.keys(optimizedSelector).length > 0) {
|
|
70
|
+
return optimizedSelector;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
idsCallback(false, []);
|
|
75
|
+
return sel;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
const indexInfoOrPromise = getIndexInfo(queryFunctions, sel);
|
|
79
|
+
if (indexInfoOrPromise instanceof Promise) {
|
|
80
|
+
return indexInfoOrPromise.then(async (indexInfo) => {
|
|
81
|
+
const memo = memoOrPromise instanceof Promise ? await memoOrPromise : memoOrPromise;
|
|
82
|
+
const optimizedSelector = getSelector(indexInfo);
|
|
83
|
+
if (optimizedSelector)
|
|
84
|
+
memo.push(optimizedSelector);
|
|
85
|
+
return memo;
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
const memo = memoOrPromise;
|
|
89
|
+
if (memo instanceof Promise)
|
|
90
|
+
throw new Error('Mixing async and sync index providers is not supported');
|
|
91
|
+
const optimizedSelector = getSelector(indexInfoOrPromise);
|
|
92
|
+
if (optimizedSelector)
|
|
93
|
+
memo.push(optimizedSelector);
|
|
94
|
+
return memo;
|
|
95
|
+
}, []);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Retrieves index information for a given complex selector by querying multiple
|
|
99
|
+
* index providers. Handles nested `$and` and `$or` conditions in the selector and
|
|
100
|
+
* optimizes the selector to minimize processing overhead.
|
|
101
|
+
* @template T - The type of the items in the collection.
|
|
102
|
+
* @template I - The type of the unique identifier for the items.
|
|
103
|
+
* @param queryFunctions - An array of index providers to query.
|
|
104
|
+
* @param selector - The complex selector used to filter items.
|
|
105
|
+
* @returns An object containing:
|
|
106
|
+
* - `matched`: A boolean indicating if the selector matched any items.
|
|
107
|
+
* - `ids`: An array of matched item ids.
|
|
108
|
+
* - `optimizedSelector`: A selector optimized based on the index results, with unused
|
|
109
|
+
* conditions removed.
|
|
110
|
+
*/
|
|
111
|
+
function getIndexInfo(queryFunctions, selector) {
|
|
112
|
+
if (selector == null || Object.keys(selector).length <= 0) {
|
|
113
|
+
return {
|
|
114
|
+
matched: false,
|
|
115
|
+
ids: [],
|
|
116
|
+
optimizedSelector: selector,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
const { $and, $or, ...rest } = selector;
|
|
120
|
+
const flatInfoOrPromise = getMergedIndexInfo(queryFunctions, rest);
|
|
121
|
+
const processFlatInfo = (flatInfo) => {
|
|
122
|
+
let { matched, ids } = flatInfo;
|
|
123
|
+
const newSelector = flatInfo.optimizedSelector;
|
|
124
|
+
const $andNewOrPromise = Array.isArray($and)
|
|
125
|
+
? optimizeLogicGate(queryFunctions, $and, (match, selIds) => {
|
|
126
|
+
if (!match)
|
|
127
|
+
return;
|
|
128
|
+
ids = matched ? (0, intersection_1.default)(ids, selIds) : selIds;
|
|
129
|
+
matched = true;
|
|
130
|
+
})
|
|
131
|
+
: undefined;
|
|
132
|
+
const process$and = ($andNew) => {
|
|
133
|
+
if ($andNew && $andNew.length > 0)
|
|
134
|
+
newSelector.$and = $andNew;
|
|
135
|
+
let hasNonIndexField = false;
|
|
136
|
+
const matchedBefore = matched;
|
|
137
|
+
const idsBefore = ids;
|
|
138
|
+
const process$or = ($orNew) => {
|
|
139
|
+
if (hasNonIndexField || ($orNew && $orNew.length > 0)) {
|
|
140
|
+
newSelector.$or = $or;
|
|
141
|
+
matched = matchedBefore;
|
|
142
|
+
ids = idsBefore;
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
matched,
|
|
146
|
+
ids: ids || [],
|
|
147
|
+
optimizedSelector: newSelector,
|
|
148
|
+
};
|
|
149
|
+
};
|
|
150
|
+
const $orNewOrPromise = Array.isArray($or)
|
|
151
|
+
? optimizeLogicGate(queryFunctions, $or, (match, selIds) => {
|
|
152
|
+
if (match) {
|
|
153
|
+
ids = [...new Set([...ids, ...selIds])];
|
|
154
|
+
matched = true;
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
hasNonIndexField = true;
|
|
158
|
+
}
|
|
159
|
+
})
|
|
160
|
+
: undefined;
|
|
161
|
+
if ($orNewOrPromise instanceof Promise) {
|
|
162
|
+
return $orNewOrPromise.then($orNew => process$or($orNew));
|
|
163
|
+
}
|
|
164
|
+
return process$or($orNewOrPromise);
|
|
165
|
+
};
|
|
166
|
+
if ($andNewOrPromise instanceof Promise) {
|
|
167
|
+
return $andNewOrPromise.then($andNew => process$and($andNew));
|
|
168
|
+
}
|
|
169
|
+
return process$and($andNewOrPromise);
|
|
170
|
+
};
|
|
171
|
+
if (flatInfoOrPromise instanceof Promise) {
|
|
172
|
+
return flatInfoOrPromise
|
|
173
|
+
.then(processFlatInfo);
|
|
174
|
+
}
|
|
175
|
+
return processFlatInfo(flatInfoOrPromise);
|
|
176
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export type { default as ReactivityAdapter } from './types/ReactivityAdapter';
|
|
2
|
+
export type { default as StorageAdapter, Changeset, } from './types/StorageAdapter';
|
|
3
|
+
export type { default as Selector } from './types/Selector';
|
|
4
|
+
export type { default as Modifier } from './types/Modifier';
|
|
5
|
+
export type { BaseItem, ObserveCallbacks, CursorOptions, Transform, TransformAll, SortSpecifier, FieldSpecifier, AnyFindOptions, AsyncFindOptions, FindOptions, SyncFindOptions, CollectionOptions, } from './Collection';
|
|
6
|
+
export type { default as DataAdapter } from './DataAdapter';
|
|
7
|
+
export { default as Cursor } from './Collection/Cursor';
|
|
8
|
+
export { default as Collection } from './Collection';
|
|
9
|
+
export { default as createStorageAdapter } from './createStorageAdapter';
|
|
10
|
+
export { default as createReactivityAdapter } from './createReactivityAdapter';
|
|
11
|
+
export { default as isEqual } from './utils/isEqual';
|
|
12
|
+
export { default as modify } from './utils/modify';
|
|
13
|
+
export { default as randomId } from './utils/randomId';
|
|
14
|
+
export { default as EventEmitter } from './utils/EventEmitter';
|
|
15
|
+
export { default as get } from './utils/get';
|
|
16
|
+
export { default as serializeValue } from './utils/serializeValue';
|
|
17
|
+
export { default as reactiveOrAsync, unwrap } from './utils/reactiveOrAsync';
|
|
18
|
+
export { default as DefaultDataAdapter } from './DefaultDataAdapter';
|
|
19
|
+
export { default as AsyncDataAdapter } from './AsyncDataAdapter';
|
|
20
|
+
export { default as WorkerDataAdapter } from './WorkerDataAdapter';
|
|
21
|
+
export { default as WorkerDataAdapterHost } from './WorkerDataAdapterHost';
|
|
22
|
+
export { default as AutoFetchDataAdapter } from './AutoFetchDataAdapter';
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.AutoFetchDataAdapter = exports.WorkerDataAdapterHost = exports.WorkerDataAdapter = exports.AsyncDataAdapter = exports.DefaultDataAdapter = exports.unwrap = exports.reactiveOrAsync = exports.serializeValue = exports.get = exports.EventEmitter = exports.randomId = exports.modify = exports.isEqual = exports.createReactivityAdapter = exports.createStorageAdapter = exports.Collection = exports.Cursor = void 0;
|
|
7
|
+
var Cursor_1 = require("./Collection/Cursor");
|
|
8
|
+
Object.defineProperty(exports, "Cursor", { enumerable: true, get: function () { return __importDefault(Cursor_1).default; } });
|
|
9
|
+
var Collection_1 = require("./Collection");
|
|
10
|
+
Object.defineProperty(exports, "Collection", { enumerable: true, get: function () { return __importDefault(Collection_1).default; } });
|
|
11
|
+
var createStorageAdapter_1 = require("./createStorageAdapter");
|
|
12
|
+
Object.defineProperty(exports, "createStorageAdapter", { enumerable: true, get: function () { return __importDefault(createStorageAdapter_1).default; } });
|
|
13
|
+
var createReactivityAdapter_1 = require("./createReactivityAdapter");
|
|
14
|
+
Object.defineProperty(exports, "createReactivityAdapter", { enumerable: true, get: function () { return __importDefault(createReactivityAdapter_1).default; } });
|
|
15
|
+
var isEqual_1 = require("./utils/isEqual");
|
|
16
|
+
Object.defineProperty(exports, "isEqual", { enumerable: true, get: function () { return __importDefault(isEqual_1).default; } });
|
|
17
|
+
var modify_1 = require("./utils/modify");
|
|
18
|
+
Object.defineProperty(exports, "modify", { enumerable: true, get: function () { return __importDefault(modify_1).default; } });
|
|
19
|
+
var randomId_1 = require("./utils/randomId");
|
|
20
|
+
Object.defineProperty(exports, "randomId", { enumerable: true, get: function () { return __importDefault(randomId_1).default; } });
|
|
21
|
+
var EventEmitter_1 = require("./utils/EventEmitter");
|
|
22
|
+
Object.defineProperty(exports, "EventEmitter", { enumerable: true, get: function () { return __importDefault(EventEmitter_1).default; } });
|
|
23
|
+
var get_1 = require("./utils/get");
|
|
24
|
+
Object.defineProperty(exports, "get", { enumerable: true, get: function () { return __importDefault(get_1).default; } });
|
|
25
|
+
var serializeValue_1 = require("./utils/serializeValue");
|
|
26
|
+
Object.defineProperty(exports, "serializeValue", { enumerable: true, get: function () { return __importDefault(serializeValue_1).default; } });
|
|
27
|
+
var reactiveOrAsync_1 = require("./utils/reactiveOrAsync");
|
|
28
|
+
Object.defineProperty(exports, "reactiveOrAsync", { enumerable: true, get: function () { return __importDefault(reactiveOrAsync_1).default; } });
|
|
29
|
+
Object.defineProperty(exports, "unwrap", { enumerable: true, get: function () { return reactiveOrAsync_1.unwrap; } });
|
|
30
|
+
var DefaultDataAdapter_1 = require("./DefaultDataAdapter");
|
|
31
|
+
Object.defineProperty(exports, "DefaultDataAdapter", { enumerable: true, get: function () { return __importDefault(DefaultDataAdapter_1).default; } });
|
|
32
|
+
var AsyncDataAdapter_1 = require("./AsyncDataAdapter");
|
|
33
|
+
Object.defineProperty(exports, "AsyncDataAdapter", { enumerable: true, get: function () { return __importDefault(AsyncDataAdapter_1).default; } });
|
|
34
|
+
var WorkerDataAdapter_1 = require("./WorkerDataAdapter");
|
|
35
|
+
Object.defineProperty(exports, "WorkerDataAdapter", { enumerable: true, get: function () { return __importDefault(WorkerDataAdapter_1).default; } });
|
|
36
|
+
var WorkerDataAdapterHost_1 = require("./WorkerDataAdapterHost");
|
|
37
|
+
Object.defineProperty(exports, "WorkerDataAdapterHost", { enumerable: true, get: function () { return __importDefault(WorkerDataAdapterHost_1).default; } });
|
|
38
|
+
var AutoFetchDataAdapter_1 = require("./AutoFetchDataAdapter");
|
|
39
|
+
Object.defineProperty(exports, "AutoFetchDataAdapter", { enumerable: true, get: function () { return __importDefault(AutoFetchDataAdapter_1).default; } });
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { BaseItem } from '../Collection';
|
|
2
|
+
import type { FlatSelector } from './Selector';
|
|
3
|
+
export type IndexResult<IdType> = {
|
|
4
|
+
ids: IdType[];
|
|
5
|
+
fields: string[];
|
|
6
|
+
keepSelector?: boolean;
|
|
7
|
+
matched: true;
|
|
8
|
+
} | {
|
|
9
|
+
ids?: never;
|
|
10
|
+
fields?: never;
|
|
11
|
+
keepSelector?: never;
|
|
12
|
+
matched: false;
|
|
13
|
+
};
|
|
14
|
+
export type SynchronousQueryFunction<T extends BaseItem<I> = BaseItem, I = any> = (selector: FlatSelector<T>) => IndexResult<I>;
|
|
15
|
+
export type AsynchronousQueryFunction<T extends BaseItem<I> = BaseItem, I = any> = (selector: FlatSelector<T>) => Promise<IndexResult<I>>;
|
|
16
|
+
interface IndexProvider<T extends BaseItem<I> = BaseItem, I = any> {
|
|
17
|
+
query: SynchronousQueryFunction<T, I>;
|
|
18
|
+
rebuild(items: T[]): void;
|
|
19
|
+
insert(items: T[]): void;
|
|
20
|
+
remove(items: T[]): void;
|
|
21
|
+
update(pairs: {
|
|
22
|
+
oldItem: T;
|
|
23
|
+
newItem: T;
|
|
24
|
+
}[]): void;
|
|
25
|
+
}
|
|
26
|
+
export default IndexProvider;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { DotNotation, GetType } from './Selector';
|
|
2
|
+
type Dictionary<T> = Record<string, T>;
|
|
3
|
+
type PartialMapTo<T, M> = Partial<Record<DotNotation<T>, M>> & Dictionary<M>;
|
|
4
|
+
type OnlyElementsOfArrays<T> = T extends any[] ? Partial<T[0]> : never;
|
|
5
|
+
type ElementsOf<T> = {
|
|
6
|
+
[P in DotNotation<T>]?: OnlyElementsOfArrays<GetType<T, P>>;
|
|
7
|
+
};
|
|
8
|
+
type PushModifier<T> = {
|
|
9
|
+
[P in DotNotation<T>]?: OnlyElementsOfArrays<GetType<T, P>> | {
|
|
10
|
+
$each?: GetType<T, P> | undefined;
|
|
11
|
+
$position?: number | undefined;
|
|
12
|
+
$slice?: number | undefined;
|
|
13
|
+
$sort?: 1 | -1 | Dictionary<number> | undefined;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
type ArraysOrEach<T> = {
|
|
17
|
+
[P in DotNotation<T>]?: OnlyElementsOfArrays<GetType<T, P>> | {
|
|
18
|
+
$each: GetType<T, P>;
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
type CurrentDateModifier = {
|
|
22
|
+
$type: 'timestamp' | 'date';
|
|
23
|
+
} | true;
|
|
24
|
+
type Modifier<T extends Dictionary<any> = Dictionary<any>> = {
|
|
25
|
+
$currentDate?: (Partial<Record<DotNotation<T>, CurrentDateModifier>> & Dictionary<CurrentDateModifier>) | undefined;
|
|
26
|
+
$inc?: (PartialMapTo<T, number> & Dictionary<number>) | undefined;
|
|
27
|
+
$min?: (PartialMapTo<T, Date | number> & Dictionary<Date | number>) | undefined;
|
|
28
|
+
$max?: (PartialMapTo<T, Date | number> & Dictionary<Date | number>) | undefined;
|
|
29
|
+
$mul?: (PartialMapTo<T, number> & Dictionary<number>) | undefined;
|
|
30
|
+
$rename?: (PartialMapTo<T, string> & Dictionary<string>) | undefined;
|
|
31
|
+
$set?: ({
|
|
32
|
+
[P in DotNotation<T>]?: GetType<T, P>;
|
|
33
|
+
} & Dictionary<any>) | undefined;
|
|
34
|
+
$setOnInsert?: ({
|
|
35
|
+
[P in DotNotation<T>]?: GetType<T, P>;
|
|
36
|
+
} & Dictionary<any>) | undefined;
|
|
37
|
+
$unset?: (PartialMapTo<T, string | boolean | 1 | 0> & Dictionary<any>) | undefined;
|
|
38
|
+
$addToSet?: (ArraysOrEach<T> & Dictionary<any>) | undefined;
|
|
39
|
+
$push?: (PushModifier<T> & Dictionary<any>) | undefined;
|
|
40
|
+
$pull?: (ElementsOf<T> & Dictionary<any>) | undefined;
|
|
41
|
+
$pullAll?: ({
|
|
42
|
+
[P in DotNotation<T>]?: GetType<T, P>;
|
|
43
|
+
} & Dictionary<any>) | undefined;
|
|
44
|
+
$pop?: (PartialMapTo<T, 1 | -1> & Dictionary<1 | -1>) | undefined;
|
|
45
|
+
};
|
|
46
|
+
export default Modifier;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export interface FieldExpression<T> {
|
|
2
|
+
$eq?: T;
|
|
3
|
+
$gt?: T;
|
|
4
|
+
$gte?: T;
|
|
5
|
+
$lt?: T;
|
|
6
|
+
$lte?: T;
|
|
7
|
+
$in?: T[] | undefined;
|
|
8
|
+
$nin?: T[];
|
|
9
|
+
$ne?: T;
|
|
10
|
+
$exists?: boolean;
|
|
11
|
+
$not?: FieldExpression<T>;
|
|
12
|
+
$expr?: FieldExpression<T>;
|
|
13
|
+
$jsonSchema?: any;
|
|
14
|
+
$mod?: number[];
|
|
15
|
+
$regex?: RegExp | string;
|
|
16
|
+
$options?: string;
|
|
17
|
+
$where?: string | ((this: T) => boolean);
|
|
18
|
+
$all?: T[];
|
|
19
|
+
$elemMatch?: T extends object ? Query<T> : FieldExpression<T>;
|
|
20
|
+
$size?: number;
|
|
21
|
+
$bitsAllClear?: any;
|
|
22
|
+
$bitsAllSet?: any;
|
|
23
|
+
$bitsAnyClear?: any;
|
|
24
|
+
$bitsAnySet?: any;
|
|
25
|
+
}
|
|
26
|
+
export type DotNotation<T> = {
|
|
27
|
+
[K in keyof T & string]: T[K] extends Array<infer U> ? `${K}` | `${K}.$` | `${K}.${DotNotation<U>}` : T[K] extends object ? `${K}` | `${K}.${DotNotation<T[K]>}` : `${K}`;
|
|
28
|
+
}[keyof T & string];
|
|
29
|
+
export type GetType<T, P extends string> = P extends `${infer H}.${infer R}` ? H extends keyof T ? T[H] extends Array<infer U> ? GetType<U, R> : GetType<T[H], R> : H extends '$' ? T extends Array<infer U> ? GetType<U, R> : never : never : P extends keyof T ? T[P] : never;
|
|
30
|
+
type FlatQuery<T> = {
|
|
31
|
+
[P in DotNotation<T>]?: FlatQueryValue<T, P>;
|
|
32
|
+
};
|
|
33
|
+
type FieldValue<U> = U extends string ? string | RegExp | FieldExpression<string> : U | FieldExpression<U>;
|
|
34
|
+
type FlatQueryValue<T, P extends string> = GetType<T, P> extends never ? never : GetType<T, P> extends Array<infer U> ? FieldValue<U> | FieldValue<U[]> : FieldValue<GetType<T, P>>;
|
|
35
|
+
type Query<T> = FlatQuery<T> & {
|
|
36
|
+
$or?: Query<T>[];
|
|
37
|
+
$and?: Query<T>[];
|
|
38
|
+
$nor?: Query<T>[];
|
|
39
|
+
};
|
|
40
|
+
export type FlatSelector<T extends Record<string, any>> = FlatQuery<T> & {
|
|
41
|
+
$or?: never;
|
|
42
|
+
$and?: never;
|
|
43
|
+
$nor?: never;
|
|
44
|
+
};
|
|
45
|
+
type Selector<T extends Record<string, any>> = Query<T>;
|
|
46
|
+
export default Selector;
|