@wxn0brp/db-core 0.10.3 → 0.11.0-alpha.0
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/types/options.d.ts +5 -0
- package/dist/types/query.d.ts +0 -20
- package/dist/types/searchOpts.d.ts +8 -4
- package/dist/utils/action.d.ts +1 -1
- package/dist/utils/action.js +68 -23
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/dist/types/options.d.ts
CHANGED
|
@@ -12,6 +12,11 @@ export interface DbFindOpts<T = any> {
|
|
|
12
12
|
offset?: number;
|
|
13
13
|
sortBy?: KeysMatching<T, any>;
|
|
14
14
|
sortAsc?: boolean;
|
|
15
|
+
min?: Record<string, KeysMatching<T, number>>;
|
|
16
|
+
max?: Record<string, KeysMatching<T, number>>;
|
|
17
|
+
avg?: Record<string, KeysMatching<T, number>>;
|
|
18
|
+
groupBy?: KeysMatching<T, any> | KeysMatching<T, any>[];
|
|
19
|
+
count?: Record<string, string>;
|
|
15
20
|
}
|
|
16
21
|
export interface FindOpts<T = any> {
|
|
17
22
|
select?: KeysMatching<T, any>[];
|
package/dist/types/query.d.ts
CHANGED
|
@@ -44,23 +44,3 @@ export declare namespace VQueryT {
|
|
|
44
44
|
type: "added" | "removed";
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
|
-
/** @deprecated */
|
|
48
|
-
export type QueryBase<T = Data> = VQueryT.QueryBase<T>;
|
|
49
|
-
/** @deprecated */
|
|
50
|
-
export type AddQuery<T = Data> = VQueryT.Add<T>;
|
|
51
|
-
/** @deprecated */
|
|
52
|
-
export type FindQuery<T = Data> = VQueryT.Find<T>;
|
|
53
|
-
/** @deprecated */
|
|
54
|
-
export type FindOneQuery<T = Data> = VQueryT.FindOne<T>;
|
|
55
|
-
/** @deprecated */
|
|
56
|
-
export type UpdateQuery<T = Data> = VQueryT.Update<T>;
|
|
57
|
-
/** @deprecated */
|
|
58
|
-
export type RemoveQuery<T = Data> = VQueryT.Remove<T>;
|
|
59
|
-
/** @deprecated */
|
|
60
|
-
export type UpdateOneOrAddQuery<T = Data> = VQueryT.UpdateOneOrAdd<T>;
|
|
61
|
-
/** @deprecated */
|
|
62
|
-
export type ToggleOneQuery<T = Data> = VQueryT.ToggleOne<T>;
|
|
63
|
-
/** @deprecated */
|
|
64
|
-
export type UpdateOneOrAddResult<T> = VQueryT.UpdateOneOrAddResult<T>;
|
|
65
|
-
/** @deprecated */
|
|
66
|
-
export type ToggleOneResult<T> = VQueryT.ToggleOneResult<T>;
|
|
@@ -31,11 +31,11 @@ export type ComparisonOperators<T = any> = {
|
|
|
31
31
|
/** 5 between [min, max] */
|
|
32
32
|
$between?: NestedValue<T, [number, number], number>;
|
|
33
33
|
/** 2 in [1, 2, 3] */
|
|
34
|
-
$in?:
|
|
34
|
+
$in?: {
|
|
35
35
|
[K in keyof T]?: T[K] extends any[] ? T[K] : T[K][];
|
|
36
36
|
};
|
|
37
37
|
/** 5 not in [1, 2, 3] */
|
|
38
|
-
$nin?:
|
|
38
|
+
$nin?: {
|
|
39
39
|
[K in keyof T]?: T[K] extends any[] ? T[K] : T[K][];
|
|
40
40
|
};
|
|
41
41
|
/** id > 4 */
|
|
@@ -57,9 +57,13 @@ export type TypeAndExistenceOperators<T = any> = {
|
|
|
57
57
|
/** Array Operators with nested support */
|
|
58
58
|
export type ArrayOperators<T = any> = {
|
|
59
59
|
/** [1, 2, 3] includes 2 */
|
|
60
|
-
$arrinc?:
|
|
60
|
+
$arrinc?: {
|
|
61
|
+
[K in keyof T]?: T[K] extends any[] ? T[K][number][] : T[K][];
|
|
62
|
+
};
|
|
61
63
|
/** [1, 2, 3] array includes all elements e.g. [1, 2] */
|
|
62
|
-
$arrincall?:
|
|
64
|
+
$arrincall?: {
|
|
65
|
+
[K in keyof T]?: T[K] extends any[] ? T[K][number][] : T[K][];
|
|
66
|
+
};
|
|
63
67
|
/** [1, 2, 3] has size 3 */
|
|
64
68
|
$size?: NestedValue<T, number>;
|
|
65
69
|
};
|
package/dist/utils/action.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { Data } from "../types/data.js";
|
|
2
2
|
import { FileCpu } from "../types/fileCpu.js";
|
|
3
3
|
import { VQueryT } from "../types/query.js";
|
|
4
|
-
export declare function findUtil(query: VQueryT.Find,
|
|
4
|
+
export declare function findUtil(query: VQueryT.Find, fileCpuOrData: FileCpu | Object[], files: string[]): Promise<Data[]>;
|
package/dist/utils/action.js
CHANGED
|
@@ -1,43 +1,86 @@
|
|
|
1
1
|
import { compareSafe } from "./compare.js";
|
|
2
|
-
export async function findUtil(query,
|
|
2
|
+
export async function findUtil(query, fileCpuOrData, files) {
|
|
3
3
|
const { dbFindOpts = {} } = query;
|
|
4
|
-
const { reverse = false, offset = 0, limit = -1, sortBy, sortAsc = true } = dbFindOpts;
|
|
5
|
-
|
|
6
|
-
files.reverse();
|
|
4
|
+
const { reverse = false, offset = 0, limit = -1, sortBy, sortAsc = true, min, max, avg, groupBy, count, } = dbFindOpts;
|
|
5
|
+
const needsAllData = min || max || avg || groupBy || count || sortBy;
|
|
7
6
|
let datas = [];
|
|
8
|
-
let totalEntries = 0;
|
|
9
7
|
let skippedEntries = 0;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
if (Array.isArray(fileCpuOrData)) {
|
|
9
|
+
datas = [...fileCpuOrData];
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
const filesToProcess = reverse && !sortBy ? [...files].reverse() : files;
|
|
13
|
+
for (const f of filesToProcess) {
|
|
14
|
+
let entries = await fileCpuOrData.find(f, query);
|
|
15
|
+
if (reverse && !sortBy)
|
|
16
|
+
entries.reverse();
|
|
17
|
+
if (needsAllData) {
|
|
18
|
+
datas.push(...entries);
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
15
21
|
if (offset > skippedEntries) {
|
|
16
|
-
const
|
|
17
|
-
if (entries.length <=
|
|
22
|
+
const toSkip = offset - skippedEntries;
|
|
23
|
+
if (entries.length <= toSkip) {
|
|
18
24
|
skippedEntries += entries.length;
|
|
19
25
|
continue;
|
|
20
26
|
}
|
|
21
|
-
entries = entries.slice(
|
|
27
|
+
entries = entries.slice(toSkip);
|
|
22
28
|
skippedEntries = offset;
|
|
23
29
|
}
|
|
24
30
|
if (limit !== -1) {
|
|
25
|
-
|
|
26
|
-
|
|
31
|
+
const remaining = limit - datas.length;
|
|
32
|
+
if (entries.length > remaining)
|
|
27
33
|
entries = entries.slice(0, remaining);
|
|
28
|
-
totalEntries = limit;
|
|
29
|
-
}
|
|
30
|
-
else {
|
|
31
|
-
totalEntries += entries.length;
|
|
32
|
-
}
|
|
33
34
|
}
|
|
34
35
|
datas.push(...entries);
|
|
35
|
-
if (limit !== -1 &&
|
|
36
|
-
|
|
36
|
+
if (limit !== -1 && datas.length >= limit)
|
|
37
|
+
return datas;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (!needsAllData)
|
|
41
|
+
return datas;
|
|
42
|
+
if (min || max || avg || groupBy || count) {
|
|
43
|
+
const groups = new Map();
|
|
44
|
+
const groupKeys = Array.isArray(groupBy) ? groupBy : groupBy ? [groupBy] : [];
|
|
45
|
+
if (groupKeys.length) {
|
|
46
|
+
for (const data of datas) {
|
|
47
|
+
const key = groupKeys.map(k => String(data[k] ?? "")).join("|");
|
|
48
|
+
if (!groups.has(key))
|
|
49
|
+
groups.set(key, []);
|
|
50
|
+
groups.get(key).push(data);
|
|
51
|
+
}
|
|
37
52
|
}
|
|
38
53
|
else {
|
|
39
|
-
|
|
54
|
+
groups.set("all", [...datas]);
|
|
40
55
|
}
|
|
56
|
+
const aggregated = [];
|
|
57
|
+
for (const [, groupItems] of groups) {
|
|
58
|
+
const result = {};
|
|
59
|
+
if (groupKeys.length) {
|
|
60
|
+
const sample = groupItems[0];
|
|
61
|
+
for (const k of groupKeys)
|
|
62
|
+
result[k] = sample[k];
|
|
63
|
+
}
|
|
64
|
+
if (count) {
|
|
65
|
+
for (const [outKey, srcKey] of Object.entries(count)) {
|
|
66
|
+
result[outKey] = groupItems.filter(d => d[srcKey] !== undefined && d[srcKey] !== null).length;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
for (const [outKey, srcField] of Object.entries(min ?? {})) {
|
|
70
|
+
const nums = groupItems.map(d => Number(d[srcField])).filter(n => !isNaN(n));
|
|
71
|
+
result[outKey] = nums.length ? Math.min(...nums) : null;
|
|
72
|
+
}
|
|
73
|
+
for (const [outKey, srcField] of Object.entries(max ?? {})) {
|
|
74
|
+
const nums = groupItems.map(d => Number(d[srcField])).filter(n => !isNaN(n));
|
|
75
|
+
result[outKey] = nums.length ? Math.max(...nums) : null;
|
|
76
|
+
}
|
|
77
|
+
for (const [outKey, srcField] of Object.entries(avg ?? {})) {
|
|
78
|
+
const nums = groupItems.map(d => Number(d[srcField])).filter(n => !isNaN(n));
|
|
79
|
+
result[outKey] = nums.length ? nums.reduce((a, b) => a + b, 0) / nums.length : null;
|
|
80
|
+
}
|
|
81
|
+
aggregated.push(result);
|
|
82
|
+
}
|
|
83
|
+
datas = aggregated;
|
|
41
84
|
}
|
|
42
85
|
if (sortBy) {
|
|
43
86
|
if (sortBy === "random()") {
|
|
@@ -47,6 +90,8 @@ export async function findUtil(query, fileCpu, files) {
|
|
|
47
90
|
const dir = sortAsc ? 1 : -1;
|
|
48
91
|
datas.sort((a, b) => compareSafe(a[sortBy], b[sortBy]) * dir);
|
|
49
92
|
}
|
|
93
|
+
}
|
|
94
|
+
if (offset > 0 || limit !== -1) {
|
|
50
95
|
const start = offset;
|
|
51
96
|
const end = limit !== -1 ? offset + limit : undefined;
|
|
52
97
|
datas = datas.slice(start, end);
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = "0.
|
|
1
|
+
export const version = "0.11.0-alpha.0";
|