@wxn0brp/db-core 0.0.1

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.
Files changed (99) hide show
  1. package/.github/workflows/build-relase.yml +25 -0
  2. package/.github/workflows/build.yml +20 -0
  3. package/.github/workflows/types.yml +89 -0
  4. package/CHANGELOG.md +9 -0
  5. package/LICENSE +21 -0
  6. package/README.md +5 -0
  7. package/dist/base/actions.d.ts +17 -0
  8. package/dist/base/actions.js +56 -0
  9. package/dist/base/db.d.ts +21 -0
  10. package/dist/base/db.js +56 -0
  11. package/dist/customFileCpu.d.ts +19 -0
  12. package/dist/customFileCpu.js +70 -0
  13. package/dist/db/memory.d.ts +72 -0
  14. package/dist/db/memory.js +145 -0
  15. package/dist/db/valthera.d.ts +74 -0
  16. package/dist/db/valthera.js +107 -0
  17. package/dist/helpers/CollectionManager.d.ts +43 -0
  18. package/dist/helpers/CollectionManager.js +57 -0
  19. package/dist/helpers/eventEmiter.d.ts +31 -0
  20. package/dist/helpers/eventEmiter.js +65 -0
  21. package/dist/helpers/executor.d.ts +28 -0
  22. package/dist/helpers/executor.js +45 -0
  23. package/dist/helpers/gen.d.ts +8 -0
  24. package/dist/helpers/gen.js +134 -0
  25. package/dist/helpers/relation.d.ts +10 -0
  26. package/dist/helpers/relation.js +155 -0
  27. package/dist/helpers/updateOneOrAdd.d.ts +2 -0
  28. package/dist/helpers/updateOneOrAdd.js +18 -0
  29. package/dist/index.d.ts +11 -0
  30. package/dist/index.js +6 -0
  31. package/dist/types/Id.d.ts +3 -0
  32. package/dist/types/Id.js +1 -0
  33. package/dist/types/arg.d.ts +12 -0
  34. package/dist/types/arg.js +1 -0
  35. package/dist/types/data.d.ts +4 -0
  36. package/dist/types/data.js +1 -0
  37. package/dist/types/export.d.ts +13 -0
  38. package/dist/types/export.js +1 -0
  39. package/dist/types/fileCpu.d.ts +51 -0
  40. package/dist/types/fileCpu.js +1 -0
  41. package/dist/types/options.d.ts +19 -0
  42. package/dist/types/options.js +1 -0
  43. package/dist/types/query.d.ts +15 -0
  44. package/dist/types/query.js +1 -0
  45. package/dist/types/relation.d.ts +28 -0
  46. package/dist/types/relation.js +1 -0
  47. package/dist/types/searchOpts.d.ts +62 -0
  48. package/dist/types/searchOpts.js +7 -0
  49. package/dist/types/types.d.ts +3 -0
  50. package/dist/types/types.js +1 -0
  51. package/dist/types/updater.d.ts +26 -0
  52. package/dist/types/updater.js +4 -0
  53. package/dist/types/valthera.d.ts +36 -0
  54. package/dist/types/valthera.js +1 -0
  55. package/dist/utils/hasFields.d.ts +7 -0
  56. package/dist/utils/hasFields.js +18 -0
  57. package/dist/utils/hasFieldsAdvanced.d.ts +5 -0
  58. package/dist/utils/hasFieldsAdvanced.js +176 -0
  59. package/dist/utils/sort.d.ts +1 -0
  60. package/dist/utils/sort.js +23 -0
  61. package/dist/utils/updateFindObject.d.ts +11 -0
  62. package/dist/utils/updateFindObject.js +28 -0
  63. package/dist/utils/updateObject.d.ts +7 -0
  64. package/dist/utils/updateObject.js +145 -0
  65. package/dist/version.d.ts +1 -0
  66. package/dist/version.js +1 -0
  67. package/package.json +26 -0
  68. package/src/base/actions.ts +71 -0
  69. package/src/base/db.ts +75 -0
  70. package/src/customFileCpu.ts +100 -0
  71. package/src/db/memory.ts +177 -0
  72. package/src/db/valthera.ts +132 -0
  73. package/src/helpers/CollectionManager.ts +67 -0
  74. package/src/helpers/eventEmiter.ts +73 -0
  75. package/src/helpers/executor.ts +55 -0
  76. package/src/helpers/gen.ts +154 -0
  77. package/src/helpers/relation.ts +192 -0
  78. package/src/helpers/updateOneOrAdd.ts +24 -0
  79. package/src/index.ts +25 -0
  80. package/src/types/Id.ts +3 -0
  81. package/src/types/arg.ts +15 -0
  82. package/src/types/data.ts +4 -0
  83. package/src/types/export.ts +13 -0
  84. package/src/types/fileCpu.ts +78 -0
  85. package/src/types/options.ts +22 -0
  86. package/src/types/query.ts +16 -0
  87. package/src/types/relation.ts +34 -0
  88. package/src/types/searchOpts.ts +78 -0
  89. package/src/types/types.ts +3 -0
  90. package/src/types/updater.ts +33 -0
  91. package/src/types/valthera.ts +38 -0
  92. package/src/utils/hasFields.ts +18 -0
  93. package/src/utils/hasFieldsAdvanced.ts +181 -0
  94. package/src/utils/sort.ts +25 -0
  95. package/src/utils/updateFindObject.ts +35 -0
  96. package/src/utils/updateObject.ts +156 -0
  97. package/src/version.ts +1 -0
  98. package/suglite.json +10 -0
  99. package/tsconfig.json +22 -0
@@ -0,0 +1,25 @@
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 }}
@@ -0,0 +1,20 @@
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
@@ -0,0 +1,89 @@
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 ADDED
@@ -0,0 +1,9 @@
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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 wxn0brP
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # ValtheraDB Core
2
+
3
+ This is the core library for [ValtheraDB](https://github.com/wxn0brp/ValtheraDB)
4
+
5
+ It is `not recommended` to install this module separately unless you plan to create your own ValtheraDB adapter
@@ -0,0 +1,17 @@
1
+ import Data from "../types/data.js";
2
+ import { VQuery } from "../types/query.js";
3
+ declare class dbActionBase {
4
+ getCollections(): Promise<string[]>;
5
+ checkCollection(config: VQuery): Promise<boolean>;
6
+ issetCollection(config: VQuery): Promise<boolean>;
7
+ add(config: VQuery): Promise<Data>;
8
+ find(config: VQuery): Promise<Data[]>;
9
+ findOne(config: VQuery): Promise<Data | null>;
10
+ update(config: VQuery): Promise<boolean>;
11
+ updateOne(config: VQuery): Promise<boolean>;
12
+ remove(config: VQuery): Promise<boolean>;
13
+ removeOne(config: VQuery): Promise<boolean>;
14
+ removeCollection(config: VQuery): Promise<boolean>;
15
+ updateOneOrAdd(config: VQuery): Promise<boolean>;
16
+ }
17
+ export default dbActionBase;
@@ -0,0 +1,56 @@
1
+ import { setDataUsingUpdateOneOrAdd } from "../helpers/updateOneOrAdd.js";
2
+ class dbActionBase {
3
+ async getCollections() {
4
+ throw new Error("Not implemented");
5
+ return [];
6
+ }
7
+ async checkCollection(config) {
8
+ throw new Error("Not implemented");
9
+ return false;
10
+ }
11
+ async issetCollection(config) {
12
+ throw new Error("Not implemented");
13
+ return false;
14
+ }
15
+ async add(config) {
16
+ throw new Error("Not implemented");
17
+ return {};
18
+ }
19
+ async find(config) {
20
+ throw new Error("Not implemented");
21
+ return [];
22
+ }
23
+ async findOne(config) {
24
+ throw new Error("Not implemented");
25
+ return {};
26
+ }
27
+ async update(config) {
28
+ throw new Error("Not implemented");
29
+ return false;
30
+ }
31
+ async updateOne(config) {
32
+ throw new Error("Not implemented");
33
+ return false;
34
+ }
35
+ async remove(config) {
36
+ throw new Error("Not implemented");
37
+ return false;
38
+ }
39
+ async removeOne(config) {
40
+ throw new Error("Not implemented");
41
+ return false;
42
+ }
43
+ async removeCollection(config) {
44
+ throw new Error("Not implemented");
45
+ return false;
46
+ }
47
+ async updateOneOrAdd(config) {
48
+ const res = await this.updateOne(config);
49
+ if (!res) {
50
+ setDataUsingUpdateOneOrAdd(config);
51
+ await this.add(config);
52
+ }
53
+ return res;
54
+ }
55
+ }
56
+ export default dbActionBase;
@@ -0,0 +1,21 @@
1
+ import CollectionManager from "../helpers/CollectionManager.js";
2
+ import Data from "../types/data.js";
3
+ import { VQuery } from "../types/query.js";
4
+ import { ValtheraCompatibleInternal } from "../types/valthera.js";
5
+ declare class ValtheraBase implements ValtheraCompatibleInternal {
6
+ version: string;
7
+ c(config: VQuery): CollectionManager;
8
+ getCollections(): Promise<any[]>;
9
+ checkCollection(config: VQuery): Promise<boolean>;
10
+ issetCollection(config: VQuery): Promise<boolean>;
11
+ add<T = Data>(config: VQuery): Promise<T>;
12
+ find<T = Data>(config: VQuery): Promise<T[]>;
13
+ findOne<T = Data>(config: VQuery): Promise<T>;
14
+ remove(config: VQuery): Promise<boolean>;
15
+ removeCollection(config: VQuery): Promise<boolean>;
16
+ removeOne(config: VQuery): Promise<boolean>;
17
+ update(config: VQuery): Promise<boolean>;
18
+ updateOne(config: VQuery): Promise<boolean>;
19
+ updateOneOrAdd(config: VQuery): Promise<boolean>;
20
+ }
21
+ export default ValtheraBase;
@@ -0,0 +1,56 @@
1
+ import { version } from "../version.js";
2
+ class ValtheraBase {
3
+ version = version;
4
+ c(config) {
5
+ throw new Error("Not implemented");
6
+ }
7
+ async getCollections() {
8
+ throw new Error("Not implemented");
9
+ return [];
10
+ }
11
+ async checkCollection(config) {
12
+ throw new Error("Not implemented");
13
+ return false;
14
+ }
15
+ async issetCollection(config) {
16
+ throw new Error("Not implemented");
17
+ return false;
18
+ }
19
+ async add(config) {
20
+ throw new Error("Not implemented");
21
+ return {};
22
+ }
23
+ async find(config) {
24
+ throw new Error("Not implemented");
25
+ return [];
26
+ }
27
+ async findOne(config) {
28
+ throw new Error("Not implemented");
29
+ return {};
30
+ }
31
+ async remove(config) {
32
+ throw new Error("Not implemented");
33
+ return false;
34
+ }
35
+ async removeCollection(config) {
36
+ throw new Error("Not implemented");
37
+ return false;
38
+ }
39
+ async removeOne(config) {
40
+ throw new Error("Not implemented");
41
+ return false;
42
+ }
43
+ async update(config) {
44
+ throw new Error("Not implemented");
45
+ return false;
46
+ }
47
+ async updateOne(config) {
48
+ throw new Error("Not implemented");
49
+ return false;
50
+ }
51
+ async updateOneOrAdd(config) {
52
+ throw new Error("Not implemented");
53
+ return false;
54
+ }
55
+ }
56
+ export default ValtheraBase;
@@ -0,0 +1,19 @@
1
+ import { Search, Updater } from "./types/arg.js";
2
+ import Data from "./types/data.js";
3
+ import FileCpu from "./types/fileCpu.js";
4
+ import { FindOpts } from "./types/options.js";
5
+ import { VContext } from "./types/types.js";
6
+ export type WriteFile = (file: string, data: any[]) => Promise<void>;
7
+ export type ReadFile = (file: string) => Promise<any[]>;
8
+ export declare function pathRepair(path: string): string;
9
+ declare class CustomFileCpu implements FileCpu {
10
+ _readFile: ReadFile;
11
+ _writeFile: WriteFile;
12
+ constructor(readFile: ReadFile, writeFile: WriteFile);
13
+ add(file: string, data: Data): Promise<void>;
14
+ find(file: string, arg: Search, context?: VContext, findOpts?: FindOpts): Promise<any[] | false>;
15
+ findOne(file: string, arg: Search, context?: VContext, findOpts?: FindOpts): Promise<any | false>;
16
+ remove(file: string, one: boolean, arg: Search, context?: VContext): Promise<boolean>;
17
+ update(file: string, one: boolean, arg: Search, updater: Updater, context?: VContext): Promise<boolean>;
18
+ }
19
+ export default CustomFileCpu;
@@ -0,0 +1,70 @@
1
+ import hasFieldsAdvanced from "./utils/hasFieldsAdvanced.js";
2
+ import updateFindObject from "./utils/updateFindObject.js";
3
+ export function pathRepair(path) {
4
+ return path.replaceAll("//", "/");
5
+ }
6
+ class CustomFileCpu {
7
+ _readFile;
8
+ _writeFile;
9
+ constructor(readFile, writeFile) {
10
+ this._readFile = readFile;
11
+ this._writeFile = writeFile;
12
+ }
13
+ async add(file, data) {
14
+ file = pathRepair(file);
15
+ let entries = await this._readFile(file);
16
+ entries.push(data);
17
+ await this._writeFile(file, entries);
18
+ }
19
+ async find(file, arg, context = {}, findOpts = {}) {
20
+ file = pathRepair(file);
21
+ const entries = await this._readFile(file);
22
+ const results = entries.filter(entry => typeof arg === "function" ? arg(entry, context) : hasFieldsAdvanced(entry, arg));
23
+ return results.length ? results.map(res => updateFindObject(res, findOpts)) : [];
24
+ }
25
+ async findOne(file, arg, context = {}, findOpts = {}) {
26
+ file = pathRepair(file);
27
+ const entries = await this._readFile(file);
28
+ const result = entries.find(entry => typeof arg === "function" ? arg(entry, context) : hasFieldsAdvanced(entry, arg));
29
+ return result ? updateFindObject(result, findOpts) : false;
30
+ }
31
+ async remove(file, one, arg, context = {}) {
32
+ file = pathRepair(file);
33
+ let entries = await this._readFile(file);
34
+ let removed = false;
35
+ entries = entries.filter(entry => {
36
+ if (removed && one)
37
+ return true;
38
+ let match = typeof arg === "function" ? arg(entry, context) : hasFieldsAdvanced(entry, arg);
39
+ if (match) {
40
+ removed = true;
41
+ return false;
42
+ }
43
+ return true;
44
+ });
45
+ if (!removed)
46
+ return false;
47
+ await this._writeFile(file, entries);
48
+ return true;
49
+ }
50
+ async update(file, one, arg, updater, context = {}) {
51
+ file = pathRepair(file);
52
+ let entries = await this._readFile(file);
53
+ let updated = false;
54
+ entries = entries.map(entry => {
55
+ if (updated && one)
56
+ return entry;
57
+ let match = typeof arg === "function" ? arg(entry, context) : hasFieldsAdvanced(entry, arg);
58
+ if (match) {
59
+ updated = true;
60
+ return typeof updater === "function" ? updater(entry, context) : { ...entry, ...updater };
61
+ }
62
+ return entry;
63
+ });
64
+ if (!updated)
65
+ return false;
66
+ await this._writeFile(file, entries);
67
+ return true;
68
+ }
69
+ }
70
+ export default CustomFileCpu;
@@ -0,0 +1,72 @@
1
+ import ValtheraClass from "./valthera.js";
2
+ import dbActionBase from "../base/actions.js";
3
+ import Data from "../types/data.js";
4
+ import FileCpu from "../types/fileCpu.js";
5
+ import { DbOpts } from "../types/options.js";
6
+ import { VQuery } from "../types/query.js";
7
+ export declare class MemoryAction extends dbActionBase {
8
+ folder: string;
9
+ options: DbOpts;
10
+ fileCpu: FileCpu;
11
+ memory: Map<string, any[]>;
12
+ /**
13
+ * Creates a new instance of dbActionC.
14
+ * @constructor
15
+ * @param folder - The folder where database files are stored.
16
+ * @param options - The options object.
17
+ */
18
+ constructor();
19
+ _readMemory(key: string): any[];
20
+ _writeMemory(key: string, data: any[]): void;
21
+ _getCollectionPath(collection: string): string;
22
+ /**
23
+ * Get a list of available databases in the specified folder.
24
+ */
25
+ getCollections(): Promise<string[]>;
26
+ /**
27
+ * Check and create the specified collection if it doesn't exist.
28
+ */
29
+ checkCollection({ collection }: VQuery): Promise<boolean>;
30
+ /**
31
+ * Check if a collection exists.
32
+ */
33
+ issetCollection({ collection }: VQuery): Promise<boolean>;
34
+ /**
35
+ * Add a new entry to the specified database.
36
+ */
37
+ add({ collection, data, id_gen }: VQuery): Promise<import("../types/arg.js").Arg>;
38
+ /**
39
+ * Find entries in the specified database based on search criteria.
40
+ */
41
+ find({ collection, search, context, dbFindOpts, findOpts }: VQuery): Promise<Data[]>;
42
+ /**
43
+ * Find the first matching entry in the specified database based on search criteria.
44
+ */
45
+ findOne({ collection, search, context, findOpts }: VQuery): Promise<Data>;
46
+ /**
47
+ * Update entries in the specified database based on search criteria and an updater function or object.
48
+ */
49
+ update({ collection, search, updater, context }: VQuery): Promise<boolean>;
50
+ /**
51
+ * Update the first matching entry in the specified database based on search criteria and an updater function or object.
52
+ */
53
+ updateOne({ collection, search, updater, context }: VQuery): Promise<boolean>;
54
+ /**
55
+ * Remove entries from the specified database based on search criteria.
56
+ */
57
+ remove({ collection, search, context }: VQuery): Promise<boolean>;
58
+ /**
59
+ * Remove the first matching entry from the specified database based on search criteria.
60
+ */
61
+ removeOne({ collection, search, context }: VQuery): Promise<boolean>;
62
+ /**
63
+ * Removes a database collection from the file system.
64
+ */
65
+ removeCollection({ collection }: VQuery): Promise<boolean>;
66
+ }
67
+ export default class ValtheraMemory extends ValtheraClass {
68
+ constructor(...args: any[]);
69
+ }
70
+ export declare function createMemoryValthera<T = {
71
+ [key: string]: Data[];
72
+ }>(data?: T): ValtheraMemory;
@@ -0,0 +1,145 @@
1
+ import ValtheraClass from "./valthera.js";
2
+ import CustomFileCpu from "../customFileCpu.js";
3
+ import genId from "../helpers/gen.js";
4
+ import dbActionBase from "../base/actions.js";
5
+ import { compareSafe } from "../utils/sort.js";
6
+ export class MemoryAction extends dbActionBase {
7
+ folder;
8
+ options;
9
+ fileCpu;
10
+ memory;
11
+ /**
12
+ * Creates a new instance of dbActionC.
13
+ * @constructor
14
+ * @param folder - The folder where database files are stored.
15
+ * @param options - The options object.
16
+ */
17
+ constructor() {
18
+ super();
19
+ this.memory = new Map();
20
+ this.fileCpu = new CustomFileCpu(this._readMemory.bind(this), this._writeMemory.bind(this));
21
+ }
22
+ _readMemory(key) {
23
+ if (!this.memory.has(key))
24
+ return [];
25
+ return this.memory.get(key);
26
+ }
27
+ _writeMemory(key, data) {
28
+ this.memory.set(key, data);
29
+ }
30
+ _getCollectionPath(collection) {
31
+ return this.folder + "/" + collection + "/";
32
+ }
33
+ /**
34
+ * Get a list of available databases in the specified folder.
35
+ */
36
+ async getCollections() {
37
+ const collections = Array.from(this.memory.keys());
38
+ return collections;
39
+ }
40
+ /**
41
+ * Check and create the specified collection if it doesn't exist.
42
+ */
43
+ async checkCollection({ collection }) {
44
+ if (this.issetCollection(collection))
45
+ return;
46
+ this.memory.set(collection, []);
47
+ return true;
48
+ }
49
+ /**
50
+ * Check if a collection exists.
51
+ */
52
+ async issetCollection({ collection }) {
53
+ return this.memory.has(collection);
54
+ }
55
+ /**
56
+ * Add a new entry to the specified database.
57
+ */
58
+ async add({ collection, data, id_gen = true }) {
59
+ await this.checkCollection(arguments[0]);
60
+ if (id_gen)
61
+ data._id = data._id || genId();
62
+ await this.fileCpu.add(collection, data);
63
+ return data;
64
+ }
65
+ /**
66
+ * Find entries in the specified database based on search criteria.
67
+ */
68
+ async find({ collection, search, context = {}, dbFindOpts = {}, findOpts = {} }) {
69
+ const { reverse = false, max = -1, offset = 0, sortBy, sortAsc = true } = dbFindOpts;
70
+ await this.checkCollection(arguments[0]);
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
+ }
86
+ return data;
87
+ }
88
+ /**
89
+ * Find the first matching entry in the specified database based on search criteria.
90
+ */
91
+ async findOne({ collection, search, context = {}, findOpts = {} }) {
92
+ await this.checkCollection(arguments[0]);
93
+ let data = await this.fileCpu.findOne(collection, search, context, findOpts);
94
+ return data || null;
95
+ }
96
+ /**
97
+ * Update entries in the specified database based on search criteria and an updater function or object.
98
+ */
99
+ async update({ collection, search, updater, context = {} }) {
100
+ await this.checkCollection(arguments[0]);
101
+ return await this.fileCpu.update(collection, false, search, updater, context);
102
+ }
103
+ /**
104
+ * Update the first matching entry in the specified database based on search criteria and an updater function or object.
105
+ */
106
+ async updateOne({ collection, search, updater, context = {} }) {
107
+ await this.checkCollection(arguments[0]);
108
+ return await this.fileCpu.update(collection, true, search, updater, context);
109
+ }
110
+ /**
111
+ * Remove entries from the specified database based on search criteria.
112
+ */
113
+ async remove({ collection, search, context = {} }) {
114
+ await this.checkCollection(arguments[0]);
115
+ return await this.fileCpu.remove(collection, false, search, context);
116
+ }
117
+ /**
118
+ * Remove the first matching entry from the specified database based on search criteria.
119
+ */
120
+ async removeOne({ collection, search, context = {} }) {
121
+ await this.checkCollection(arguments[0]);
122
+ return await this.fileCpu.remove(collection, true, search, context);
123
+ }
124
+ /**
125
+ * Removes a database collection from the file system.
126
+ */
127
+ async removeCollection({ collection }) {
128
+ this.memory.delete(collection);
129
+ return true;
130
+ }
131
+ }
132
+ export default class ValtheraMemory extends ValtheraClass {
133
+ constructor(...args) {
134
+ super({ dbAction: new MemoryAction() });
135
+ }
136
+ }
137
+ export function createMemoryValthera(data) {
138
+ const db = new ValtheraMemory();
139
+ if (!data)
140
+ return db;
141
+ for (const collection of Object.keys(data)) {
142
+ db.dbAction.memory.set(collection, data[collection]);
143
+ }
144
+ return db;
145
+ }