@wxn0brp/db-core 0.0.1 → 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/README.md +15 -3
- 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 +10 -1
- package/.github/workflows/build-relase.yml +0 -25
- package/.github/workflows/build.yml +0 -20
- package/.github/workflows/types.yml +0 -89
- package/CHANGELOG.md +0 -9
- package/src/base/actions.ts +0 -71
- package/src/base/db.ts +0 -75
- package/src/customFileCpu.ts +0 -100
- package/src/db/memory.ts +0 -177
- package/src/db/valthera.ts +0 -132
- package/src/helpers/CollectionManager.ts +0 -67
- package/src/helpers/eventEmiter.ts +0 -73
- package/src/helpers/executor.ts +0 -55
- package/src/helpers/gen.ts +0 -154
- package/src/helpers/relation.ts +0 -192
- package/src/helpers/updateOneOrAdd.ts +0 -24
- package/src/index.ts +0 -25
- package/src/types/Id.ts +0 -3
- package/src/types/arg.ts +0 -15
- package/src/types/data.ts +0 -4
- package/src/types/export.ts +0 -13
- package/src/types/fileCpu.ts +0 -78
- package/src/types/options.ts +0 -22
- package/src/types/query.ts +0 -16
- package/src/types/relation.ts +0 -34
- package/src/types/searchOpts.ts +0 -78
- package/src/types/types.ts +0 -3
- package/src/types/updater.ts +0 -33
- package/src/types/valthera.ts +0 -38
- package/src/utils/hasFields.ts +0 -18
- package/src/utils/hasFieldsAdvanced.ts +0 -181
- package/src/utils/sort.ts +0 -25
- package/src/utils/updateFindObject.ts +0 -35
- package/src/utils/updateObject.ts +0 -156
- package/src/version.ts +0 -1
- package/suglite.json +0 -10
- package/tsconfig.json +0 -22
package/README.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @wxn0brp/db-core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Core library for [ValtheraDB](https://github.com/wxn0brp/ValtheraDB).
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## ⚠️ Disclaimer
|
|
6
|
+
|
|
7
|
+
It is **not recommended** to install this module separately unless you plan to create your own ValtheraDB adapter.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @wxn0brp/db-core
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## License
|
|
16
|
+
|
|
17
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
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";
|
package/package.json
CHANGED
|
@@ -1,16 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wxn0brp/db-core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"types": "dist/index.d.ts",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"author": "wxn0brP",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"type": "module",
|
|
9
|
+
"description": "ValtheraDB Core",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/wxn0brP/ValtheraDB-core.git"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://github.com/wxn0brP/ValtheraDB",
|
|
9
15
|
"devDependencies": {
|
|
10
16
|
"@types/node": "^22.12.0",
|
|
11
17
|
"tsc-alias": "^1.8.10",
|
|
12
18
|
"typescript": "^5.7.3"
|
|
13
19
|
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
14
23
|
"exports": {
|
|
15
24
|
".": {
|
|
16
25
|
"types": "./dist/index.d.ts",
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
name: Release
|
|
2
|
-
|
|
3
|
-
on:
|
|
4
|
-
push:
|
|
5
|
-
tags:
|
|
6
|
-
- '*'
|
|
7
|
-
workflow_dispatch:
|
|
8
|
-
|
|
9
|
-
concurrency:
|
|
10
|
-
group: build-release
|
|
11
|
-
cancel-in-progress: true
|
|
12
|
-
|
|
13
|
-
jobs:
|
|
14
|
-
build:
|
|
15
|
-
uses: wxn0brP/workflow-dist/.github/workflows/build-ts.yml@main
|
|
16
|
-
with:
|
|
17
|
-
files: "dist package.json README.md LICENSE"
|
|
18
|
-
publishToNpm: true
|
|
19
|
-
scriptsHandling: "remove-all"
|
|
20
|
-
customCommands: |
|
|
21
|
-
v=$(jq -r .version package.json)
|
|
22
|
-
echo "export const version = \"$v\";" > dist/version.js
|
|
23
|
-
|
|
24
|
-
secrets:
|
|
25
|
-
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
name: Nightly
|
|
2
|
-
|
|
3
|
-
on:
|
|
4
|
-
push:
|
|
5
|
-
branches:
|
|
6
|
-
- master
|
|
7
|
-
workflow_dispatch:
|
|
8
|
-
|
|
9
|
-
concurrency:
|
|
10
|
-
group: build-master
|
|
11
|
-
cancel-in-progress: true
|
|
12
|
-
|
|
13
|
-
jobs:
|
|
14
|
-
build:
|
|
15
|
-
uses: wxn0brP/workflow-dist/.github/workflows/build-ts.yml@main
|
|
16
|
-
with:
|
|
17
|
-
scriptsHandling: 'remove-all'
|
|
18
|
-
customCommands: |
|
|
19
|
-
v=$(jq -r .version package.json)
|
|
20
|
-
echo "export const version = \"$v\";" > dist/version.js
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
name: Types
|
|
2
|
-
|
|
3
|
-
on:
|
|
4
|
-
push:
|
|
5
|
-
branches:
|
|
6
|
-
- master
|
|
7
|
-
tags:
|
|
8
|
-
- '*'
|
|
9
|
-
|
|
10
|
-
pull_request:
|
|
11
|
-
branches:
|
|
12
|
-
- master
|
|
13
|
-
workflow_dispatch:
|
|
14
|
-
|
|
15
|
-
concurrency:
|
|
16
|
-
group: build-types
|
|
17
|
-
cancel-in-progress: true
|
|
18
|
-
|
|
19
|
-
jobs:
|
|
20
|
-
deploy-types:
|
|
21
|
-
runs-on: ubuntu-latest
|
|
22
|
-
permissions:
|
|
23
|
-
contents: write
|
|
24
|
-
|
|
25
|
-
steps:
|
|
26
|
-
- name: Checkout Repo
|
|
27
|
-
uses: actions/checkout@v3
|
|
28
|
-
|
|
29
|
-
- name: Setup Node.js
|
|
30
|
-
uses: actions/setup-node@v3
|
|
31
|
-
with:
|
|
32
|
-
node-version: 22
|
|
33
|
-
|
|
34
|
-
- name: Install Dependencies
|
|
35
|
-
run: |
|
|
36
|
-
npm i
|
|
37
|
-
npm install -g tsc-alias
|
|
38
|
-
|
|
39
|
-
- name: Build Declarations Only
|
|
40
|
-
run: |
|
|
41
|
-
npx tsc --emitDeclarationOnly --declaration --removeComments --outDir dist --project tsconfig.json
|
|
42
|
-
tsc-alias
|
|
43
|
-
|
|
44
|
-
- name: Prepare Clean package.json
|
|
45
|
-
run: |
|
|
46
|
-
jq 'del(
|
|
47
|
-
.scripts,
|
|
48
|
-
.dependencies,
|
|
49
|
-
.devDependencies,
|
|
50
|
-
.peerDependencies,
|
|
51
|
-
.optionalDependencies,
|
|
52
|
-
.bundledDependencies
|
|
53
|
-
)' package.json > dist/package.json
|
|
54
|
-
rm package.json
|
|
55
|
-
mv dist/package.json package.json
|
|
56
|
-
|
|
57
|
-
- name: Push to `types` Branch
|
|
58
|
-
run: |
|
|
59
|
-
git config --global user.name "github-actions[bot]"
|
|
60
|
-
git config --global user.email "github-actions[bot]@users.noreply.github.com"
|
|
61
|
-
|
|
62
|
-
git checkout --orphan types
|
|
63
|
-
git reset -q HEAD --
|
|
64
|
-
|
|
65
|
-
git add -f -A dist LICENSE package.json
|
|
66
|
-
git commit -m "Deploy type definitions only"
|
|
67
|
-
git push origin types --force
|
|
68
|
-
|
|
69
|
-
- name: Push to `types-only` Branch
|
|
70
|
-
run: |
|
|
71
|
-
git config --global user.name "github-actions[bot]"
|
|
72
|
-
git config --global user.email "github-actions[bot]@users.noreply.github.com"
|
|
73
|
-
|
|
74
|
-
git checkout --orphan types-only
|
|
75
|
-
git reset -q HEAD --
|
|
76
|
-
|
|
77
|
-
cat << EOF > dist/index.d.ts
|
|
78
|
-
import { ValtheraTypes } from "./types/export";
|
|
79
|
-
import { RelationTypes } from "./types/relation";
|
|
80
|
-
import { ValtheraCompatible } from "./types/valthera";
|
|
81
|
-
export type { RelationTypes, ValtheraCompatible, ValtheraTypes };
|
|
82
|
-
export type Id = import("./types/Id").Id;
|
|
83
|
-
EOF
|
|
84
|
-
|
|
85
|
-
echo "export {}" > dist/index.js
|
|
86
|
-
|
|
87
|
-
git add -f -A dist/types dist/index.d.ts dist/index.js LICENSE package.json
|
|
88
|
-
git commit -m "Deploy type definitions only"
|
|
89
|
-
git push origin types-only --force
|
package/CHANGELOG.md
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
3
|
-
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
|
-
|
|
5
|
-
### 0.0.1 (2025-07-12)
|
|
6
|
-
|
|
7
|
-
### [0.0.1](https://github.com/wxn0brP/ValtheraDB-core/compare/v0.0.2...v0.0.1) (2025-07-12)
|
|
8
|
-
|
|
9
|
-
### 0.0.2 (2025-07-12)
|
package/src/base/actions.ts
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { setDataUsingUpdateOneOrAdd } from "../helpers/updateOneOrAdd";
|
|
2
|
-
import Data from "../types/data";
|
|
3
|
-
import { VQuery } from "../types/query";
|
|
4
|
-
|
|
5
|
-
class dbActionBase {
|
|
6
|
-
async getCollections() {
|
|
7
|
-
throw new Error("Not implemented");
|
|
8
|
-
return [] as string[];
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
async checkCollection(config: VQuery) {
|
|
12
|
-
throw new Error("Not implemented");
|
|
13
|
-
return false;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
async issetCollection(config: VQuery) {
|
|
17
|
-
throw new Error("Not implemented");
|
|
18
|
-
return false;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
async add(config: VQuery) {
|
|
22
|
-
throw new Error("Not implemented");
|
|
23
|
-
return {} as Data;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
async find(config: VQuery) {
|
|
27
|
-
throw new Error("Not implemented");
|
|
28
|
-
return [] as Data[];
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
async findOne(config: VQuery): Promise<Data | null> {
|
|
32
|
-
throw new Error("Not implemented");
|
|
33
|
-
return {} as Data;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
async update(config: VQuery) {
|
|
37
|
-
throw new Error("Not implemented");
|
|
38
|
-
return false;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
async updateOne(config: VQuery) {
|
|
42
|
-
throw new Error("Not implemented");
|
|
43
|
-
return false;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
async remove(config: VQuery) {
|
|
47
|
-
throw new Error("Not implemented");
|
|
48
|
-
return false;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
async removeOne(config: VQuery) {
|
|
52
|
-
throw new Error("Not implemented");
|
|
53
|
-
return false;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
async removeCollection(config: VQuery) {
|
|
57
|
-
throw new Error("Not implemented");
|
|
58
|
-
return false;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
async updateOneOrAdd(config: VQuery) {
|
|
62
|
-
const res = await this.updateOne(config);
|
|
63
|
-
if (!res) {
|
|
64
|
-
setDataUsingUpdateOneOrAdd(config);
|
|
65
|
-
await this.add(config);
|
|
66
|
-
}
|
|
67
|
-
return res as boolean;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export default dbActionBase;
|
package/src/base/db.ts
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import CollectionManager from "../helpers/CollectionManager";
|
|
2
|
-
import Data from "../types/data";
|
|
3
|
-
import { VQuery } from "../types/query";
|
|
4
|
-
import { ValtheraCompatibleInternal } from "../types/valthera";
|
|
5
|
-
import { version } from "../version";
|
|
6
|
-
|
|
7
|
-
class ValtheraBase implements ValtheraCompatibleInternal {
|
|
8
|
-
version = version;
|
|
9
|
-
|
|
10
|
-
c(config: VQuery): CollectionManager {
|
|
11
|
-
throw new Error("Not implemented");
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
async getCollections() {
|
|
15
|
-
throw new Error("Not implemented");
|
|
16
|
-
return [];
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
async checkCollection(config: VQuery) {
|
|
20
|
-
throw new Error("Not implemented");
|
|
21
|
-
return false;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
async issetCollection(config: VQuery) {
|
|
25
|
-
throw new Error("Not implemented");
|
|
26
|
-
return false;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
async add<T = Data>(config: VQuery) {
|
|
30
|
-
throw new Error("Not implemented");
|
|
31
|
-
return {} as T;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
async find<T = Data>(config: VQuery) {
|
|
35
|
-
throw new Error("Not implemented");
|
|
36
|
-
return [] as T[];
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
async findOne<T = Data>(config: VQuery) {
|
|
40
|
-
throw new Error("Not implemented");
|
|
41
|
-
return {} as T;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
async remove(config: VQuery) {
|
|
45
|
-
throw new Error("Not implemented");
|
|
46
|
-
return false;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
async removeCollection(config: VQuery) {
|
|
50
|
-
throw new Error("Not implemented");
|
|
51
|
-
return false;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
async removeOne(config: VQuery) {
|
|
55
|
-
throw new Error("Not implemented");
|
|
56
|
-
return false;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
async update(config: VQuery) {
|
|
60
|
-
throw new Error("Not implemented");
|
|
61
|
-
return false;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
async updateOne(config: VQuery) {
|
|
65
|
-
throw new Error("Not implemented");
|
|
66
|
-
return false;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
async updateOneOrAdd(config: VQuery) {
|
|
70
|
-
throw new Error("Not implemented");
|
|
71
|
-
return false;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export default ValtheraBase;
|
package/src/customFileCpu.ts
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import { Search, Updater } from "./types/arg";
|
|
2
|
-
import Data from "./types/data";
|
|
3
|
-
import FileCpu from "./types/fileCpu";
|
|
4
|
-
import { FindOpts } from "./types/options";
|
|
5
|
-
import { VContext } from "./types/types";
|
|
6
|
-
import hasFieldsAdvanced from "./utils/hasFieldsAdvanced";
|
|
7
|
-
import updateFindObject from "./utils/updateFindObject";
|
|
8
|
-
|
|
9
|
-
export type WriteFile = (file: string, data: any[]) => Promise<void>
|
|
10
|
-
export type ReadFile = (file: string) => Promise<any[]>
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
export function pathRepair(path: string) {
|
|
14
|
-
return path.replaceAll("//", "/");
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
class CustomFileCpu implements FileCpu {
|
|
18
|
-
_readFile: ReadFile;
|
|
19
|
-
_writeFile: WriteFile;
|
|
20
|
-
|
|
21
|
-
constructor(readFile: ReadFile, writeFile: WriteFile) {
|
|
22
|
-
this._readFile = readFile;
|
|
23
|
-
this._writeFile = writeFile;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
async add(file: string, data: Data): Promise<void> {
|
|
27
|
-
file = pathRepair(file);
|
|
28
|
-
let entries = await this._readFile(file);
|
|
29
|
-
entries.push(data);
|
|
30
|
-
await this._writeFile(file, entries);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
async find(file: string, arg: Search, context: VContext = {}, findOpts: FindOpts = {}): Promise<any[] | false> {
|
|
34
|
-
file = pathRepair(file);
|
|
35
|
-
const entries = await this._readFile(file);
|
|
36
|
-
const results = entries.filter(entry =>
|
|
37
|
-
typeof arg === "function" ? arg(entry, context) : hasFieldsAdvanced(entry, arg)
|
|
38
|
-
);
|
|
39
|
-
return results.length ? results.map(res => updateFindObject(res, findOpts)) : [];
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
async findOne(file: string, arg: Search, context: VContext = {}, findOpts: FindOpts = {}): Promise<any | false> {
|
|
43
|
-
file = pathRepair(file);
|
|
44
|
-
const entries = await this._readFile(file);
|
|
45
|
-
const result = entries.find(entry =>
|
|
46
|
-
typeof arg === "function" ? arg(entry, context) : hasFieldsAdvanced(entry, arg)
|
|
47
|
-
);
|
|
48
|
-
return result ? updateFindObject(result, findOpts) : false;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
async remove(file: string, one: boolean, arg: Search, context: VContext = {}): Promise<boolean> {
|
|
52
|
-
file = pathRepair(file);
|
|
53
|
-
let entries = await this._readFile(file);
|
|
54
|
-
let removed = false;
|
|
55
|
-
|
|
56
|
-
entries = entries.filter(entry => {
|
|
57
|
-
if (removed && one) return true;
|
|
58
|
-
|
|
59
|
-
let match = typeof arg === "function" ? arg(entry, context) : hasFieldsAdvanced(entry, arg);
|
|
60
|
-
|
|
61
|
-
if (match) {
|
|
62
|
-
removed = true;
|
|
63
|
-
return false;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
return true;
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
if (!removed) return false;
|
|
70
|
-
|
|
71
|
-
await this._writeFile(file, entries);
|
|
72
|
-
return true;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
async update(file: string, one: boolean, arg: Search, updater: Updater, context: VContext = {}): Promise<boolean> {
|
|
76
|
-
file = pathRepair(file);
|
|
77
|
-
let entries = await this._readFile(file);
|
|
78
|
-
let updated = false;
|
|
79
|
-
|
|
80
|
-
entries = entries.map(entry => {
|
|
81
|
-
if (updated && one) return entry;
|
|
82
|
-
|
|
83
|
-
let match = typeof arg === "function" ? arg(entry, context) : hasFieldsAdvanced(entry, arg);
|
|
84
|
-
|
|
85
|
-
if (match) {
|
|
86
|
-
updated = true;
|
|
87
|
-
return typeof updater === "function" ? updater(entry, context) : { ...entry, ...updater };
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return entry;
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
if (!updated) return false;
|
|
94
|
-
|
|
95
|
-
await this._writeFile(file, entries);
|
|
96
|
-
return true;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export default CustomFileCpu;
|