@wxn0brp/db 0.3.2 → 0.4.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.
package/dist/database.js CHANGED
@@ -12,8 +12,8 @@ class DataBase {
12
12
  constructor(folder, options = {}, fileCpu) {
13
13
  if (!fileCpu)
14
14
  fileCpu = vFileCpu;
15
- this.dbAction = new dbActionC(folder, options, fileCpu);
16
- this.executor = new executorC();
15
+ this.dbAction = options.dbAction || new dbActionC(folder, options, fileCpu);
16
+ this.executor = options.executor || new executorC();
17
17
  }
18
18
  /**
19
19
  * Create a new instance of a CollectionManager class.
@@ -1,16 +1,15 @@
1
- import { Context } from "vm";
1
+ import { Context } from "../types/types.js";
2
2
  import { Search, Updater } from "../types/arg.js";
3
3
  import Data from "../types/data.js";
4
4
  import FileCpu from "../types/fileCpu.js";
5
5
  import { FindOpts } from "../types/options.js";
6
- type WriteFile = (file: string, data: any[]) => Promise<void>;
7
- type ReadFile = (file: string) => Promise<any[]>;
6
+ export type WriteFile = (file: string, data: any[]) => Promise<void>;
7
+ export type ReadFile = (file: string) => Promise<any[]>;
8
8
  declare class CustomFileCpu implements FileCpu {
9
9
  _readFile: ReadFile;
10
10
  _writeFile: WriteFile;
11
11
  constructor(readFile: ReadFile, writeFile: WriteFile);
12
12
  add(file: string, data: Data): Promise<void>;
13
- addMany(file: string, data: Data[]): Promise<void>;
14
13
  find(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<any[] | false>;
15
14
  findOne(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<any | false>;
16
15
  remove(file: string, one: boolean, arg: Search, context?: Context): Promise<boolean>;
@@ -14,17 +14,11 @@ class CustomFileCpu {
14
14
  entries.push(data);
15
15
  await this._writeFile(file, entries);
16
16
  }
17
- async addMany(file, data) {
18
- file = pathRepair(file);
19
- let entries = await this._readFile(file);
20
- entries = entries.concat(data);
21
- await this._writeFile(file, entries);
22
- }
23
17
  async find(file, arg, context = {}, findOpts = {}) {
24
18
  file = pathRepair(file);
25
19
  const entries = await this._readFile(file);
26
20
  const results = entries.filter(entry => typeof arg === "function" ? arg(entry, context) : hasFieldsAdvanced(entry, arg));
27
- return results.length ? results.map(res => updateFindObject(res, findOpts)) : false;
21
+ return results.length ? results.map(res => updateFindObject(res, findOpts)) : [];
28
22
  }
29
23
  async findOne(file, arg, context = {}, findOpts = {}) {
30
24
  file = pathRepair(file);
package/dist/index.d.ts CHANGED
@@ -5,7 +5,8 @@ import GraphRemote from "./client/graph.js";
5
5
  import genId from "./gen.js";
6
6
  import Relation from "./relation.js";
7
7
  import CustomFileCpu from "./file/customFileCpu.js";
8
- export { DataBase, Graph, DataBaseRemote, GraphRemote, Relation, genId, CustomFileCpu, };
8
+ import ValtheraMemory from "./memory.js";
9
+ export { DataBase, DataBase as Valthera, Graph, DataBaseRemote, DataBaseRemote as ValtheraRemote, GraphRemote, Relation, genId, CustomFileCpu, ValtheraMemory, };
9
10
  import type Id from "./types/Id.js";
10
11
  import type { Arg, Search, Updater } from "./types/arg.js";
11
12
  import type { DbFindOpts, FindOpts, DbOpts } from "./types/options.js";
package/dist/index.js CHANGED
@@ -5,4 +5,5 @@ import GraphRemote from "./client/graph.js";
5
5
  import genId from "./gen.js";
6
6
  import Relation from "./relation.js";
7
7
  import CustomFileCpu from "./file/customFileCpu.js";
8
- export { DataBase, Graph, DataBaseRemote, GraphRemote, Relation, genId, CustomFileCpu, };
8
+ import ValtheraMemory from "./memory.js";
9
+ export { DataBase, DataBase as Valthera, Graph, DataBaseRemote, DataBaseRemote as ValtheraRemote, GraphRemote, Relation, genId, CustomFileCpu, ValtheraMemory, };
@@ -0,0 +1,4 @@
1
+ import DataBase from "./database.js";
2
+ export default class ValtheraMemory extends DataBase {
3
+ constructor(...args: any[]);
4
+ }
package/dist/memory.js ADDED
@@ -0,0 +1,122 @@
1
+ import DataBase from "./database.js";
2
+ import CustomFileCpu from "./file/customFileCpu.js";
3
+ import genId from "./gen.js";
4
+ class MemoryAction {
5
+ folder;
6
+ options;
7
+ fileCpu;
8
+ memory;
9
+ /**
10
+ * Creates a new instance of dbActionC.
11
+ * @constructor
12
+ * @param folder - The folder where database files are stored.
13
+ * @param options - The options object.
14
+ */
15
+ constructor() {
16
+ this.memory = new Map();
17
+ this.fileCpu = new CustomFileCpu(this._readMemory.bind(this), this._writeMemory.bind(this));
18
+ }
19
+ _readMemory(key) {
20
+ if (!this.memory.has(key))
21
+ return [];
22
+ return this.memory.get(key);
23
+ }
24
+ _writeMemory(key, data) {
25
+ this.memory.set(key, data);
26
+ }
27
+ _getCollectionPath(collection) {
28
+ return this.folder + "/" + collection + "/";
29
+ }
30
+ /**
31
+ * Get a list of available databases in the specified folder.
32
+ */
33
+ async getCollections() {
34
+ const collections = Array.from(this.memory.keys());
35
+ return collections;
36
+ }
37
+ /**
38
+ * Check and create the specified collection if it doesn't exist.
39
+ */
40
+ async checkCollection(collection) {
41
+ if (this.issetCollection(collection))
42
+ return;
43
+ this.memory.set(collection, []);
44
+ }
45
+ /**
46
+ * Check if a collection exists.
47
+ */
48
+ async issetCollection(collection) {
49
+ return this.memory.has(collection);
50
+ }
51
+ /**
52
+ * Add a new entry to the specified database.
53
+ */
54
+ async add(collection, arg, id_gen = true) {
55
+ await this.checkCollection(collection);
56
+ if (id_gen)
57
+ arg._id = arg._id || genId();
58
+ await this.fileCpu.add(collection, arg);
59
+ return arg;
60
+ }
61
+ /**
62
+ * Find entries in the specified database based on search criteria.
63
+ */
64
+ async find(collection, arg, context = {}, options = {}, findOpts = {}) {
65
+ options.reverse = options.reverse || false;
66
+ options.max = options.max || -1;
67
+ await this.checkCollection(collection);
68
+ let data = await this.fileCpu.find(collection, arg, context, findOpts);
69
+ if (options.reverse)
70
+ data.reverse();
71
+ if (options.max !== -1 && data.length > options.max)
72
+ data = data.slice(0, options.max);
73
+ return data;
74
+ }
75
+ /**
76
+ * Find the first matching entry in the specified database based on search criteria.
77
+ */
78
+ async findOne(collection, arg, context = {}, findOpts = {}) {
79
+ await this.checkCollection(collection);
80
+ let data = await this.fileCpu.findOne(collection, arg, context, findOpts);
81
+ return data || null;
82
+ }
83
+ /**
84
+ * Update entries in the specified database based on search criteria and an updater function or object.
85
+ */
86
+ async update(collection, arg, updater, context = {}) {
87
+ await this.checkCollection(collection);
88
+ return await this.fileCpu.update(collection, false, arg, updater, context);
89
+ }
90
+ /**
91
+ * Update the first matching entry in the specified database based on search criteria and an updater function or object.
92
+ */
93
+ async updateOne(collection, arg, updater, context = {}) {
94
+ await this.checkCollection(collection);
95
+ return await this.fileCpu.update(collection, true, arg, updater, context);
96
+ }
97
+ /**
98
+ * Remove entries from the specified database based on search criteria.
99
+ */
100
+ async remove(collection, arg, context = {}) {
101
+ await this.checkCollection(collection);
102
+ return await this.fileCpu.remove(collection, false, arg, context);
103
+ }
104
+ /**
105
+ * Remove the first matching entry from the specified database based on search criteria.
106
+ */
107
+ async removeOne(collection, arg, context = {}) {
108
+ await this.checkCollection(collection);
109
+ return await this.fileCpu.remove(collection, true, arg, context);
110
+ }
111
+ /**
112
+ * Removes a database collection from the file system.
113
+ */
114
+ async removeCollection(collection) {
115
+ this.memory.delete(collection);
116
+ }
117
+ }
118
+ export default class ValtheraMemory extends DataBase {
119
+ constructor(...args) {
120
+ super("", { dbAction: new MemoryAction() });
121
+ }
122
+ }
@@ -1,5 +1,9 @@
1
+ import dbActionC from "../action.js";
2
+ import executorC from "../executor.js";
1
3
  export interface DbOpts {
2
4
  maxFileSize?: number;
5
+ dbAction?: dbActionC;
6
+ executor?: executorC;
3
7
  }
4
8
  export interface DbFindOpts {
5
9
  reverse?: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db",
3
- "version": "0.3.2",
3
+ "version": "0.4.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "description": "A simple file-based database management system with support for CRUD operations, custom queries, and graph structures.",
@@ -31,7 +31,7 @@
31
31
  },
32
32
  "scripts": {
33
33
  "build": "tsc && tsc-alias",
34
- "prepare": "npm run build",
34
+ "prepare": "rm -rf dist && npm run build",
35
35
  "postversion": "git push && git push --tags",
36
36
  "build:cdn": "npm run build && cd cdn && node index.js"
37
37
  },