@wxn0brp/db-core 0.0.2 → 0.0.3
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/db/memory.d.ts +2 -4
- package/dist/db/memory.js +4 -19
- package/dist/utils/action.d.ts +4 -0
- package/dist/utils/action.js +50 -0
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/dist/db/memory.d.ts
CHANGED
|
@@ -38,7 +38,7 @@ export declare class MemoryAction extends dbActionBase {
|
|
|
38
38
|
/**
|
|
39
39
|
* Find entries in the specified database based on search criteria.
|
|
40
40
|
*/
|
|
41
|
-
find(
|
|
41
|
+
find(query: VQuery): Promise<Data[]>;
|
|
42
42
|
/**
|
|
43
43
|
* Find the first matching entry in the specified database based on search criteria.
|
|
44
44
|
*/
|
|
@@ -67,6 +67,4 @@ export declare class MemoryAction extends dbActionBase {
|
|
|
67
67
|
export default class ValtheraMemory extends ValtheraClass {
|
|
68
68
|
constructor(...args: any[]);
|
|
69
69
|
}
|
|
70
|
-
export declare function createMemoryValthera<T
|
|
71
|
-
[key: string]: Data[];
|
|
72
|
-
}>(data?: T): ValtheraMemory;
|
|
70
|
+
export declare function createMemoryValthera<T extends Record<string, Data[]>>(data?: T): ValtheraMemory;
|
package/dist/db/memory.js
CHANGED
|
@@ -2,7 +2,7 @@ import ValtheraClass from "./valthera.js";
|
|
|
2
2
|
import CustomFileCpu from "../customFileCpu.js";
|
|
3
3
|
import genId from "../helpers/gen.js";
|
|
4
4
|
import dbActionBase from "../base/actions.js";
|
|
5
|
-
import {
|
|
5
|
+
import { findUtil } from "../utils/action.js";
|
|
6
6
|
export class MemoryAction extends dbActionBase {
|
|
7
7
|
folder;
|
|
8
8
|
options;
|
|
@@ -65,24 +65,9 @@ export class MemoryAction extends dbActionBase {
|
|
|
65
65
|
/**
|
|
66
66
|
* Find entries in the specified database based on search criteria.
|
|
67
67
|
*/
|
|
68
|
-
async find(
|
|
69
|
-
|
|
70
|
-
await this.
|
|
71
|
-
let data = await this.fileCpu.find(collection, search, context, findOpts);
|
|
72
|
-
if (reverse)
|
|
73
|
-
data.reverse();
|
|
74
|
-
if (sortBy) {
|
|
75
|
-
const dir = sortAsc ? 1 : -1;
|
|
76
|
-
data.sort((a, b) => compareSafe(a[sortBy], b[sortBy]) * dir);
|
|
77
|
-
}
|
|
78
|
-
if (offset > 0) {
|
|
79
|
-
if (data.length <= offset)
|
|
80
|
-
return [];
|
|
81
|
-
data = data.slice(offset);
|
|
82
|
-
}
|
|
83
|
-
if (max !== -1 && data.length > max) {
|
|
84
|
-
data = data.slice(0, max);
|
|
85
|
-
}
|
|
68
|
+
async find(query) {
|
|
69
|
+
await this.checkCollection(query);
|
|
70
|
+
const data = await findUtil(query, this.fileCpu, [query.collection]);
|
|
86
71
|
return data;
|
|
87
72
|
}
|
|
88
73
|
/**
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { compareSafe } from "./sort.js";
|
|
2
|
+
export async function findUtil(query, fileCpu, files) {
|
|
3
|
+
const { search, context = {}, dbFindOpts = {}, findOpts = {} } = query;
|
|
4
|
+
const { reverse = false, max = -1, offset = 0, sortBy, sortAsc = true } = dbFindOpts;
|
|
5
|
+
if (reverse && !sortBy)
|
|
6
|
+
files.reverse();
|
|
7
|
+
let datas = [];
|
|
8
|
+
let totalEntries = 0;
|
|
9
|
+
let skippedEntries = 0;
|
|
10
|
+
for (const f of files) {
|
|
11
|
+
let entries = await fileCpu.find(f, search, context, findOpts);
|
|
12
|
+
if (reverse && !sortBy)
|
|
13
|
+
entries.reverse();
|
|
14
|
+
if (!sortBy) {
|
|
15
|
+
if (offset > skippedEntries) {
|
|
16
|
+
const remainingSkip = offset - skippedEntries;
|
|
17
|
+
if (entries.length <= remainingSkip) {
|
|
18
|
+
skippedEntries += entries.length;
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
entries = entries.slice(remainingSkip);
|
|
22
|
+
skippedEntries = offset;
|
|
23
|
+
}
|
|
24
|
+
if (max !== -1) {
|
|
25
|
+
if (totalEntries + entries.length > max) {
|
|
26
|
+
const remaining = max - totalEntries;
|
|
27
|
+
entries = entries.slice(0, remaining);
|
|
28
|
+
totalEntries = max;
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
totalEntries += entries.length;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
datas.push(...entries);
|
|
35
|
+
if (max !== -1 && totalEntries >= max)
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
datas.push(...entries);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (sortBy) {
|
|
43
|
+
const dir = sortAsc ? 1 : -1;
|
|
44
|
+
datas.sort((a, b) => compareSafe(a[sortBy], b[sortBy]) * dir);
|
|
45
|
+
const start = offset;
|
|
46
|
+
const end = max !== -1 ? offset + max : undefined;
|
|
47
|
+
datas = datas.slice(start, end);
|
|
48
|
+
}
|
|
49
|
+
return datas;
|
|
50
|
+
}
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = "0.0.
|
|
1
|
+
export const version = "0.0.3";
|