@wxn0brp/db-core 0.10.4 → 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.
@@ -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>[];
@@ -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>;
@@ -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, fileCpu: FileCpu, files: string[]): Promise<Data[]>;
4
+ export declare function findUtil(query: VQueryT.Find, fileCpuOrData: FileCpu | Object[], files: string[]): Promise<Data[]>;
@@ -1,43 +1,86 @@
1
1
  import { compareSafe } from "./compare.js";
2
- export async function findUtil(query, fileCpu, files) {
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
- if (reverse && !sortBy)
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
- for (const f of files) {
11
- let entries = await fileCpu.find(f, query);
12
- if (reverse && !sortBy)
13
- entries.reverse();
14
- if (!sortBy) {
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 remainingSkip = offset - skippedEntries;
17
- if (entries.length <= remainingSkip) {
22
+ const toSkip = offset - skippedEntries;
23
+ if (entries.length <= toSkip) {
18
24
  skippedEntries += entries.length;
19
25
  continue;
20
26
  }
21
- entries = entries.slice(remainingSkip);
27
+ entries = entries.slice(toSkip);
22
28
  skippedEntries = offset;
23
29
  }
24
30
  if (limit !== -1) {
25
- if (totalEntries + entries.length > limit) {
26
- const remaining = limit - totalEntries;
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 && totalEntries >= limit)
36
- break;
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
- datas.push(...entries);
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.10.4";
1
+ export const version = "0.11.0-alpha.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db-core",
3
- "version": "0.10.4",
3
+ "version": "0.11.0-alpha.0",
4
4
  "types": "dist/index.d.ts",
5
5
  "main": "dist/index.js",
6
6
  "author": "wxn0brP",