@wxn0brp/db-core 0.0.1 → 0.0.2
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/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/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = "0.0.
|
|
1
|
+
export const version = "0.0.2";
|
package/package.json
CHANGED
|
@@ -1,16 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wxn0brp/db-core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
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;
|
package/src/db/memory.ts
DELETED
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import ValtheraClass from "./valthera";
|
|
3
|
-
import CustomFileCpu from "../customFileCpu";
|
|
4
|
-
import genId from "../helpers/gen";
|
|
5
|
-
import dbActionBase from "../base/actions";
|
|
6
|
-
import Data from "../types/data";
|
|
7
|
-
import FileCpu from "../types/fileCpu";
|
|
8
|
-
import { DbOpts } from "../types/options";
|
|
9
|
-
import { VQuery } from "../types/query";
|
|
10
|
-
import { compareSafe } from "../utils/sort";
|
|
11
|
-
|
|
12
|
-
export class MemoryAction extends dbActionBase {
|
|
13
|
-
folder: string;
|
|
14
|
-
options: DbOpts;
|
|
15
|
-
fileCpu: FileCpu;
|
|
16
|
-
memory: Map<string, any[]>;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Creates a new instance of dbActionC.
|
|
20
|
-
* @constructor
|
|
21
|
-
* @param folder - The folder where database files are stored.
|
|
22
|
-
* @param options - The options object.
|
|
23
|
-
*/
|
|
24
|
-
constructor() {
|
|
25
|
-
super();
|
|
26
|
-
this.memory = new Map();
|
|
27
|
-
this.fileCpu = new CustomFileCpu(this._readMemory.bind(this), this._writeMemory.bind(this));
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
_readMemory(key: string) {
|
|
31
|
-
if (!this.memory.has(key)) return [];
|
|
32
|
-
return this.memory.get(key);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
_writeMemory(key: string, data: any[]) {
|
|
36
|
-
this.memory.set(key, data);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
_getCollectionPath(collection: string) {
|
|
40
|
-
return this.folder + "/" + collection + "/";
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Get a list of available databases in the specified folder.
|
|
45
|
-
*/
|
|
46
|
-
async getCollections() {
|
|
47
|
-
const collections = Array.from(this.memory.keys());
|
|
48
|
-
return collections;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Check and create the specified collection if it doesn't exist.
|
|
53
|
-
*/
|
|
54
|
-
async checkCollection({ collection }: VQuery) {
|
|
55
|
-
if (this.issetCollection(collection)) return;
|
|
56
|
-
this.memory.set(collection, []);
|
|
57
|
-
return true;
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* Check if a collection exists.
|
|
61
|
-
*/
|
|
62
|
-
async issetCollection({ collection }: VQuery) {
|
|
63
|
-
return this.memory.has(collection);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Add a new entry to the specified database.
|
|
68
|
-
*/
|
|
69
|
-
async add({ collection, data, id_gen = true }: VQuery) {
|
|
70
|
-
await this.checkCollection(arguments[0]);
|
|
71
|
-
|
|
72
|
-
if (id_gen) data._id = data._id || genId();
|
|
73
|
-
await this.fileCpu.add(collection, data);
|
|
74
|
-
return data;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Find entries in the specified database based on search criteria.
|
|
79
|
-
*/
|
|
80
|
-
async find({ collection, search, context = {}, dbFindOpts = {}, findOpts = {} }: VQuery) {
|
|
81
|
-
const {
|
|
82
|
-
reverse = false,
|
|
83
|
-
max = -1,
|
|
84
|
-
offset = 0,
|
|
85
|
-
sortBy,
|
|
86
|
-
sortAsc = true
|
|
87
|
-
} = dbFindOpts;
|
|
88
|
-
|
|
89
|
-
await this.checkCollection(arguments[0]);
|
|
90
|
-
|
|
91
|
-
let data = await this.fileCpu.find(collection, search, context, findOpts) as Data[];
|
|
92
|
-
|
|
93
|
-
if (reverse) data.reverse();
|
|
94
|
-
|
|
95
|
-
if (sortBy) {
|
|
96
|
-
const dir = sortAsc ? 1 : -1;
|
|
97
|
-
data.sort((a, b) => compareSafe(a[sortBy], b[sortBy]) * dir);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (offset > 0) {
|
|
101
|
-
if (data.length <= offset) return [];
|
|
102
|
-
data = data.slice(offset);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
if (max !== -1 && data.length > max) {
|
|
106
|
-
data = data.slice(0, max);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
return data;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Find the first matching entry in the specified database based on search criteria.
|
|
114
|
-
*/
|
|
115
|
-
async findOne({ collection, search, context = {}, findOpts = {} }: VQuery) {
|
|
116
|
-
await this.checkCollection(arguments[0]);
|
|
117
|
-
let data = await this.fileCpu.findOne(collection, search, context, findOpts) as Data;
|
|
118
|
-
return data || null;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Update entries in the specified database based on search criteria and an updater function or object.
|
|
123
|
-
*/
|
|
124
|
-
async update({ collection, search, updater, context = {} }: VQuery) {
|
|
125
|
-
await this.checkCollection(arguments[0]);
|
|
126
|
-
return await this.fileCpu.update(collection, false, search, updater, context);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Update the first matching entry in the specified database based on search criteria and an updater function or object.
|
|
131
|
-
*/
|
|
132
|
-
async updateOne({ collection, search, updater, context = {} }: VQuery) {
|
|
133
|
-
await this.checkCollection(arguments[0]);
|
|
134
|
-
return await this.fileCpu.update(collection, true, search, updater, context);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Remove entries from the specified database based on search criteria.
|
|
139
|
-
*/
|
|
140
|
-
async remove({ collection, search, context = {} }: VQuery) {
|
|
141
|
-
await this.checkCollection(arguments[0]);
|
|
142
|
-
return await this.fileCpu.remove(collection, false, search, context);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Remove the first matching entry from the specified database based on search criteria.
|
|
147
|
-
*/
|
|
148
|
-
async removeOne({ collection, search, context = {} }: VQuery) {
|
|
149
|
-
await this.checkCollection(arguments[0]);
|
|
150
|
-
return await this.fileCpu.remove(collection, true, search, context);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* Removes a database collection from the file system.
|
|
155
|
-
*/
|
|
156
|
-
async removeCollection({ collection }: VQuery) {
|
|
157
|
-
this.memory.delete(collection);
|
|
158
|
-
return true;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
export default class ValtheraMemory extends ValtheraClass {
|
|
163
|
-
constructor(...args: any[]) {
|
|
164
|
-
super({ dbAction: new MemoryAction() });
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
export function createMemoryValthera<T = { [key: string]: Data[] }>(data?: T) {
|
|
169
|
-
const db = new ValtheraMemory();
|
|
170
|
-
if (!data) return db;
|
|
171
|
-
|
|
172
|
-
for (const collection of Object.keys(data)) {
|
|
173
|
-
(db.dbAction as MemoryAction).memory.set(collection, data[collection]);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
return db;
|
|
177
|
-
}
|