@wxn0brp/db 0.1.0 → 0.1.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.
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # @wxn0brp/db
1
+ # ValtheraDB (@wxn0brp/db)
2
2
 
3
3
  A lightweight file-based database management system that supports CRUD operations, custom queries, and graph structures.
4
4
 
@@ -20,7 +20,7 @@ import { DataBase, Graph, DataBaseRemote, GraphRemote, Relation, genId } from "@
20
20
 
21
21
  ## Documentation
22
22
 
23
- Website: [https://wxn0brp.github.io/database/](https://wxn0brp.github.io/database/)
23
+ Website: [https://wxn0brp.github.io/ValtheraDB/](https://wxn0brp.github.io/ValtheraDB/)
24
24
 
25
25
  For detailed information, refer to the following resources:
26
26
 
@@ -3,6 +3,7 @@ import { DbFindOpts, DbOpts, FindOpts } from "./types/options.js";
3
3
  import { Context } from "./types/types.js";
4
4
  import { SearchOptions } from "./types/searchOpts.js";
5
5
  import Data from "./types/data.js";
6
+ import FileCpu from "./types/fileCpu.js";
6
7
  /**
7
8
  * A class representing database actions on files.
8
9
  * @class
@@ -10,13 +11,14 @@ import Data from "./types/data.js";
10
11
  declare class dbActionC {
11
12
  folder: string;
12
13
  options: DbOpts;
14
+ fileCpu: FileCpu;
13
15
  /**
14
16
  * Creates a new instance of dbActionC.
15
17
  * @constructor
16
18
  * @param folder - The folder where database files are stored.
17
19
  * @param options - The options object.
18
20
  */
19
- constructor(folder: string, options: DbOpts);
21
+ constructor(folder: string, options: DbOpts, fileCpu: FileCpu);
20
22
  _getCollectionPath(collection: string): string;
21
23
  /**
22
24
  * Get a list of available databases in the specified folder.
@@ -4,9 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const gen_1 = __importDefault(require("./gen.js"));
7
- const format_1 = require("./format.js");
8
7
  const fs_1 = require("fs");
9
- const index_1 = require("./file/index.js");
10
8
  /**
11
9
  * A class representing database actions on files.
12
10
  * @class
@@ -14,18 +12,20 @@ const index_1 = require("./file/index.js");
14
12
  class dbActionC {
15
13
  folder;
16
14
  options;
15
+ fileCpu;
17
16
  /**
18
17
  * Creates a new instance of dbActionC.
19
18
  * @constructor
20
19
  * @param folder - The folder where database files are stored.
21
20
  * @param options - The options object.
22
21
  */
23
- constructor(folder, options) {
22
+ constructor(folder, options, fileCpu) {
24
23
  this.folder = folder;
25
24
  this.options = {
26
25
  maxFileSize: 2 * 1024 * 1024, //2 MB
27
26
  ...options,
28
27
  };
28
+ this.fileCpu = fileCpu;
29
29
  if (!(0, fs_1.existsSync)(folder))
30
30
  (0, fs_1.mkdirSync)(folder, { recursive: true });
31
31
  }
@@ -69,8 +69,7 @@ class dbActionC {
69
69
  const file = cpath + getLastFile(cpath, this.options.maxFileSize);
70
70
  if (id_gen)
71
71
  arg._id = arg._id || (0, gen_1.default)();
72
- const data = (0, format_1.stringify)(arg);
73
- (0, fs_1.appendFileSync)(file, data + "\n");
72
+ await this.fileCpu.add(file, arg);
74
73
  return arg;
75
74
  }
76
75
  /**
@@ -87,7 +86,7 @@ class dbActionC {
87
86
  let datas = [];
88
87
  let totalEntries = 0;
89
88
  for (let f of files) {
90
- let data = await (0, index_1.find)(cpath + f, arg, context, findOpts);
89
+ let data = await this.fileCpu.find(cpath + f, arg, context, findOpts);
91
90
  if (options.reverse)
92
91
  data.reverse();
93
92
  if (options.max !== -1) {
@@ -114,7 +113,7 @@ class dbActionC {
114
113
  const cpath = this._getCollectionPath(collection);
115
114
  const files = getSortedFiles(cpath).map(f => f.f);
116
115
  for (let f of files) {
117
- let data = await (0, index_1.findOne)(cpath + f, arg, context, findOpts);
116
+ let data = await this.fileCpu.findOne(cpath + f, arg, context, findOpts);
118
117
  if (data)
119
118
  return data;
120
119
  }
@@ -125,28 +124,28 @@ class dbActionC {
125
124
  */
126
125
  async update(collection, arg, updater, context = {}) {
127
126
  this.checkCollection(collection);
128
- return await (0, index_1.update)(this._getCollectionPath(collection), arg, updater, context);
127
+ return await this.fileCpu.update(this._getCollectionPath(collection), arg, updater, context);
129
128
  }
130
129
  /**
131
130
  * Update the first matching entry in the specified database based on search criteria and an updater function or object.
132
131
  */
133
132
  async updateOne(collection, arg, updater, context = {}) {
134
133
  this.checkCollection(collection);
135
- return await (0, index_1.update)(this._getCollectionPath(collection), arg, updater, context, true);
134
+ return await this.fileCpu.update(this._getCollectionPath(collection), arg, updater, context, true);
136
135
  }
137
136
  /**
138
137
  * Remove entries from the specified database based on search criteria.
139
138
  */
140
139
  async remove(collection, arg, context = {}) {
141
140
  this.checkCollection(collection);
142
- return await (0, index_1.remove)(this._getCollectionPath(collection), arg, context);
141
+ return await this.fileCpu.remove(this._getCollectionPath(collection), arg, context);
143
142
  }
144
143
  /**
145
144
  * Remove the first matching entry from the specified database based on search criteria.
146
145
  */
147
146
  async removeOne(collection, arg, context = {}) {
148
147
  this.checkCollection(collection);
149
- return await (0, index_1.remove)(this._getCollectionPath(collection), arg, context, true);
148
+ return await this.fileCpu.remove(this._getCollectionPath(collection), arg, context, true);
150
149
  }
151
150
  /**
152
151
  * Removes a database collection from the file system.
@@ -5,6 +5,7 @@ import { DbFindOpts, DbOpts, FindOpts } from "./types/options.js";
5
5
  import { Arg, Search, Updater } from "./types/arg.js";
6
6
  import Data from "./types/data.js";
7
7
  import { Context } from "./types/types.js";
8
+ import FileCpu from "./types/fileCpu.js";
8
9
  /**
9
10
  * Represents a database management class for performing CRUD operations.
10
11
  * @class
@@ -12,7 +13,7 @@ import { Context } from "./types/types.js";
12
13
  declare class DataBase {
13
14
  dbAction: dbActionC;
14
15
  executor: executorC;
15
- constructor(folder: string, options?: DbOpts);
16
+ constructor(folder: string, options?: DbOpts, fileCpu?: FileCpu);
16
17
  /**
17
18
  * Create a new instance of a CollectionManager class.
18
19
  */
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const action_1 = __importDefault(require("./action.js"));
7
7
  const executor_1 = __importDefault(require("./executor.js"));
8
8
  const CollectionManager_1 = __importDefault(require("./CollectionManager.js"));
9
+ const file_1 = __importDefault(require("./file/index.js"));
9
10
  /**
10
11
  * Represents a database management class for performing CRUD operations.
11
12
  * @class
@@ -13,8 +14,10 @@ const CollectionManager_1 = __importDefault(require("./CollectionManager.js"));
13
14
  class DataBase {
14
15
  dbAction;
15
16
  executor;
16
- constructor(folder, options = {}) {
17
- this.dbAction = new action_1.default(folder, options);
17
+ constructor(folder, options = {}, fileCpu) {
18
+ if (!fileCpu)
19
+ fileCpu = file_1.default;
20
+ this.dbAction = new action_1.default(folder, options, fileCpu);
18
21
  this.executor = new executor_1.default();
19
22
  }
20
23
  /**
@@ -0,0 +1,19 @@
1
+ import { Context } from "vm";
2
+ import { Search, Updater } from "../types/arg.js";
3
+ import Data from "../types/data.js";
4
+ import FileCpu from "../types/fileCpu.js";
5
+ import { FindOpts } from "../types/options.js";
6
+ type WriteFile = (file: string, data: any[]) => Promise<void>;
7
+ type ReadFile = (file: string) => Promise<any[]>;
8
+ declare class CustomFileCpu implements FileCpu {
9
+ _readFile: ReadFile;
10
+ _writeFile: WriteFile;
11
+ constructor(readFile: ReadFile, writeFile: WriteFile);
12
+ add(file: string, data: Data): Promise<void>;
13
+ addMany(file: string, data: Data[]): Promise<void>;
14
+ find(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<any[] | false>;
15
+ findOne(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<any | false>;
16
+ remove(cpath: string, arg: Search, context?: Context, one?: boolean): Promise<boolean>;
17
+ update(cpath: string, arg: Search, updater: Updater, context?: Context, one?: boolean): Promise<boolean>;
18
+ }
19
+ export default CustomFileCpu;
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const hasFieldsAdvanced_1 = __importDefault(require("../utils/hasFieldsAdvanced.js"));
7
+ const updateFindObject_1 = __importDefault(require("../utils/updateFindObject.js"));
8
+ const utils_1 = require("./utils.js");
9
+ class CustomFileCpu {
10
+ _readFile;
11
+ _writeFile;
12
+ constructor(readFile, writeFile) {
13
+ this._readFile = readFile;
14
+ this._writeFile = writeFile;
15
+ }
16
+ async add(file, data) {
17
+ file = (0, utils_1.pathRepair)(file);
18
+ let entries = await this._readFile(file);
19
+ entries.push(data);
20
+ await this._writeFile(file, entries);
21
+ }
22
+ async addMany(file, data) {
23
+ file = (0, utils_1.pathRepair)(file);
24
+ let entries = await this._readFile(file);
25
+ entries = entries.concat(data);
26
+ await this._writeFile(file, entries);
27
+ }
28
+ async find(file, arg, context = {}, findOpts = {}) {
29
+ file = (0, utils_1.pathRepair)(file);
30
+ const entries = await this._readFile(file);
31
+ const results = entries.filter(entry => typeof arg === "function" ? arg(entry, context) : (0, hasFieldsAdvanced_1.default)(entry, arg));
32
+ return results.length ? results.map(res => (0, updateFindObject_1.default)(res, findOpts)) : false;
33
+ }
34
+ async findOne(file, arg, context = {}, findOpts = {}) {
35
+ file = (0, utils_1.pathRepair)(file);
36
+ const entries = await this._readFile(file);
37
+ const result = entries.find(entry => typeof arg === "function" ? arg(entry, context) : (0, hasFieldsAdvanced_1.default)(entry, arg));
38
+ return result ? (0, updateFindObject_1.default)(result, findOpts) : false;
39
+ }
40
+ async remove(cpath, arg, context = {}, one = false) {
41
+ const file = (0, utils_1.pathRepair)(cpath);
42
+ let entries = await this._readFile(file);
43
+ let removed = false;
44
+ entries = entries.filter(entry => {
45
+ if (removed && one)
46
+ return true;
47
+ let match = typeof arg === "function" ? arg(entry, context) : (0, hasFieldsAdvanced_1.default)(entry, arg);
48
+ if (match) {
49
+ removed = true;
50
+ return false;
51
+ }
52
+ return true;
53
+ });
54
+ if (!removed)
55
+ return false;
56
+ await this._writeFile(file, entries);
57
+ return true;
58
+ }
59
+ async update(cpath, arg, updater, context = {}, one = false) {
60
+ const file = (0, utils_1.pathRepair)(cpath);
61
+ let entries = await this._readFile(file);
62
+ let updated = false;
63
+ entries = entries.map(entry => {
64
+ if (updated && one)
65
+ return entry;
66
+ let match = typeof arg === "function" ? arg(entry, context) : (0, hasFieldsAdvanced_1.default)(entry, arg);
67
+ if (match) {
68
+ updated = true;
69
+ return typeof updater === "function" ? updater(entry, context) : { ...entry, ...updater };
70
+ }
71
+ return entry;
72
+ });
73
+ if (!updated)
74
+ return false;
75
+ await this._writeFile(file, entries);
76
+ return true;
77
+ }
78
+ }
79
+ exports.default = CustomFileCpu;
@@ -4,8 +4,8 @@ import { FindOpts } from "../types/options.js";
4
4
  /**
5
5
  * Asynchronously finds entries in a file based on search criteria.
6
6
  */
7
- export declare function find(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<unknown>;
7
+ export declare function find(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<any[] | false>;
8
8
  /**
9
9
  * Asynchronously finds one entry in a file based on search criteria.
10
10
  */
11
- export declare function findOne(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<unknown>;
11
+ export declare function findOne(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<any | false>;
@@ -1,3 +1,3 @@
1
- export { default as update } from "./update.js";
2
- export { default as remove } from "./remove.js";
3
- export * from "./find.js";
1
+ import FileCpu from "../types/fileCpu.js";
2
+ declare const vFileCpu: FileCpu;
3
+ export default vFileCpu;
@@ -1,25 +1,21 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
17
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
18
4
  };
19
5
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.remove = exports.update = void 0;
21
- var update_1 = require("./update.js");
22
- Object.defineProperty(exports, "update", { enumerable: true, get: function () { return __importDefault(update_1).default; } });
23
- var remove_1 = require("./remove.js");
24
- Object.defineProperty(exports, "remove", { enumerable: true, get: function () { return __importDefault(remove_1).default; } });
25
- __exportStar(require("./find.js"), exports);
6
+ const update_1 = __importDefault(require("./update.js"));
7
+ const remove_1 = __importDefault(require("./remove.js"));
8
+ const find_1 = require("./find.js");
9
+ const fs_1 = require("fs");
10
+ const format_1 = require("../format.js");
11
+ const vFileCpu = {
12
+ add: async (file, data) => {
13
+ const dataString = (0, format_1.stringify)(data);
14
+ (0, fs_1.appendFileSync)(file, dataString + "\n");
15
+ },
16
+ find: find_1.find,
17
+ findOne: find_1.findOne,
18
+ update: update_1.default,
19
+ remove: remove_1.default
20
+ };
21
+ exports.default = vFileCpu;
@@ -4,10 +4,11 @@ import DataBaseRemote from "./client/database.js";
4
4
  import GraphRemote from "./client/graph.js";
5
5
  import genId from "./gen.js";
6
6
  import Relation from "./relation.js";
7
- export { DataBase, Graph, DataBaseRemote, GraphRemote, Relation, genId };
7
+ import CustomFileCpu from "./file/customFileCpu.js";
8
+ export { DataBase, Graph, DataBaseRemote, GraphRemote, Relation, genId, CustomFileCpu, };
8
9
  import type Id from "./types/Id.js";
9
10
  import type { Arg, Search, Updater } from "./types/arg.js";
10
11
  import type { DbFindOpts, FindOpts, DbOpts } from "./types/options.js";
11
12
  import type Data from "./types/data.js";
12
13
  import type { SearchOptions } from "./types/searchOpts.js";
13
- export type { Id, Arg, Search, Updater, DbFindOpts, FindOpts, DbOpts, Data, SearchOptions };
14
+ export type { Id, Arg, Search, Updater, DbFindOpts, FindOpts, DbOpts, Data, SearchOptions, };
package/dist/cjs/index.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.genId = exports.Relation = exports.GraphRemote = exports.DataBaseRemote = exports.Graph = exports.DataBase = void 0;
6
+ exports.CustomFileCpu = exports.genId = exports.Relation = exports.GraphRemote = exports.DataBaseRemote = exports.Graph = exports.DataBase = void 0;
7
7
  const database_1 = __importDefault(require("./database.js"));
8
8
  exports.DataBase = database_1.default;
9
9
  const graph_1 = __importDefault(require("./graph.js"));
@@ -16,3 +16,5 @@ const gen_1 = __importDefault(require("./gen.js"));
16
16
  exports.genId = gen_1.default;
17
17
  const relation_1 = __importDefault(require("./relation.js"));
18
18
  exports.Relation = relation_1.default;
19
+ const customFileCpu_1 = __importDefault(require("./file/customFileCpu.js"));
20
+ exports.CustomFileCpu = customFileCpu_1.default;
@@ -0,0 +1,51 @@
1
+ import { Search, Updater } from "./arg.js";
2
+ import Data from "./data.js";
3
+ import { FindOpts } from "./options.js";
4
+ import { Context } from "./types.js";
5
+ interface FileCpu {
6
+ /**
7
+ * Asynchronously adds an entry to a file.
8
+ * @param file The path to the file.
9
+ * @param data The data to add.
10
+ * @returns A promise resolving to `void`.
11
+ */
12
+ add(file: string, data: Data): Promise<void>;
13
+ /**
14
+ * Asynchronously finds multiple entries in a file based on search criteria.
15
+ * @param file The path to the file.
16
+ * @param arg The search criteria.
17
+ * @param context Additional context for the search.
18
+ * @param findOpts Additional options for searching.
19
+ * @returns A promise resolving to an array of found entries, or `false` if the file does not exist.
20
+ */
21
+ find(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<any[] | false>;
22
+ /**
23
+ * Asynchronously finds one entry in a file based on search criteria.
24
+ * @param file The path to the file.
25
+ * @param arg The search criteria.
26
+ * @param context Additional context for the search.
27
+ * @param findOpts Additional options for searching.
28
+ * @returns A promise resolving to the found entry or `false` if not found.
29
+ */
30
+ findOne(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<any | false>;
31
+ /**
32
+ * Asynchronously removes entries from a file based on search criteria.
33
+ * @param cpath The path to the directory containing the files.
34
+ * @param arg The search criteria.
35
+ * @param context Additional context for the operation.
36
+ * @param one If `true`, removes only the first matching entry.
37
+ * @returns A promise resolving to `true` if at least one entry was removed, otherwise `false`.
38
+ */
39
+ remove(cpath: string, arg: Search, context?: Context, one?: boolean): Promise<boolean>;
40
+ /**
41
+ * Asynchronously updates entries in a file based on search criteria and an updater function or object.
42
+ * @param cpath The path to the directory containing the files.
43
+ * @param arg The search criteria.
44
+ * @param updater The updater function or object.
45
+ * @param context Additional context for the operation.
46
+ * @param one If `true`, updates only the first matching entry.
47
+ * @returns A promise resolving to `true` if at least one entry was updated, otherwise `false`.
48
+ */
49
+ update(cpath: string, arg: Search, updater: Updater, context?: Context, one?: boolean): Promise<boolean>;
50
+ }
51
+ export default FileCpu;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -3,6 +3,7 @@ import { DbFindOpts, DbOpts, FindOpts } from "./types/options.js";
3
3
  import { Context } from "./types/types.js";
4
4
  import { SearchOptions } from "./types/searchOpts.js";
5
5
  import Data from "./types/data.js";
6
+ import FileCpu from "./types/fileCpu.js";
6
7
  /**
7
8
  * A class representing database actions on files.
8
9
  * @class
@@ -10,13 +11,14 @@ import Data from "./types/data.js";
10
11
  declare class dbActionC {
11
12
  folder: string;
12
13
  options: DbOpts;
14
+ fileCpu: FileCpu;
13
15
  /**
14
16
  * Creates a new instance of dbActionC.
15
17
  * @constructor
16
18
  * @param folder - The folder where database files are stored.
17
19
  * @param options - The options object.
18
20
  */
19
- constructor(folder: string, options: DbOpts);
21
+ constructor(folder: string, options: DbOpts, fileCpu: FileCpu);
20
22
  _getCollectionPath(collection: string): string;
21
23
  /**
22
24
  * Get a list of available databases in the specified folder.
@@ -1,7 +1,5 @@
1
1
  import gen from "./gen.js";
2
- import { stringify } from "./format.js";
3
- import { existsSync, mkdirSync, readdirSync, appendFileSync, rmSync, writeFileSync, statSync } from "fs";
4
- import { find as _find, findOne as _findOne, update as _update, remove as _remove } from "./file/index.js";
2
+ import { existsSync, mkdirSync, readdirSync, rmSync, writeFileSync, statSync } from "fs";
5
3
  /**
6
4
  * A class representing database actions on files.
7
5
  * @class
@@ -9,18 +7,20 @@ import { find as _find, findOne as _findOne, update as _update, remove as _remov
9
7
  class dbActionC {
10
8
  folder;
11
9
  options;
10
+ fileCpu;
12
11
  /**
13
12
  * Creates a new instance of dbActionC.
14
13
  * @constructor
15
14
  * @param folder - The folder where database files are stored.
16
15
  * @param options - The options object.
17
16
  */
18
- constructor(folder, options) {
17
+ constructor(folder, options, fileCpu) {
19
18
  this.folder = folder;
20
19
  this.options = {
21
20
  maxFileSize: 2 * 1024 * 1024, //2 MB
22
21
  ...options,
23
22
  };
23
+ this.fileCpu = fileCpu;
24
24
  if (!existsSync(folder))
25
25
  mkdirSync(folder, { recursive: true });
26
26
  }
@@ -64,8 +64,7 @@ class dbActionC {
64
64
  const file = cpath + getLastFile(cpath, this.options.maxFileSize);
65
65
  if (id_gen)
66
66
  arg._id = arg._id || gen();
67
- const data = stringify(arg);
68
- appendFileSync(file, data + "\n");
67
+ await this.fileCpu.add(file, arg);
69
68
  return arg;
70
69
  }
71
70
  /**
@@ -82,7 +81,7 @@ class dbActionC {
82
81
  let datas = [];
83
82
  let totalEntries = 0;
84
83
  for (let f of files) {
85
- let data = await _find(cpath + f, arg, context, findOpts);
84
+ let data = await this.fileCpu.find(cpath + f, arg, context, findOpts);
86
85
  if (options.reverse)
87
86
  data.reverse();
88
87
  if (options.max !== -1) {
@@ -109,7 +108,7 @@ class dbActionC {
109
108
  const cpath = this._getCollectionPath(collection);
110
109
  const files = getSortedFiles(cpath).map(f => f.f);
111
110
  for (let f of files) {
112
- let data = await _findOne(cpath + f, arg, context, findOpts);
111
+ let data = await this.fileCpu.findOne(cpath + f, arg, context, findOpts);
113
112
  if (data)
114
113
  return data;
115
114
  }
@@ -120,28 +119,28 @@ class dbActionC {
120
119
  */
121
120
  async update(collection, arg, updater, context = {}) {
122
121
  this.checkCollection(collection);
123
- return await _update(this._getCollectionPath(collection), arg, updater, context);
122
+ return await this.fileCpu.update(this._getCollectionPath(collection), arg, updater, context);
124
123
  }
125
124
  /**
126
125
  * Update the first matching entry in the specified database based on search criteria and an updater function or object.
127
126
  */
128
127
  async updateOne(collection, arg, updater, context = {}) {
129
128
  this.checkCollection(collection);
130
- return await _update(this._getCollectionPath(collection), arg, updater, context, true);
129
+ return await this.fileCpu.update(this._getCollectionPath(collection), arg, updater, context, true);
131
130
  }
132
131
  /**
133
132
  * Remove entries from the specified database based on search criteria.
134
133
  */
135
134
  async remove(collection, arg, context = {}) {
136
135
  this.checkCollection(collection);
137
- return await _remove(this._getCollectionPath(collection), arg, context);
136
+ return await this.fileCpu.remove(this._getCollectionPath(collection), arg, context);
138
137
  }
139
138
  /**
140
139
  * Remove the first matching entry from the specified database based on search criteria.
141
140
  */
142
141
  async removeOne(collection, arg, context = {}) {
143
142
  this.checkCollection(collection);
144
- return await _remove(this._getCollectionPath(collection), arg, context, true);
143
+ return await this.fileCpu.remove(this._getCollectionPath(collection), arg, context, true);
145
144
  }
146
145
  /**
147
146
  * Removes a database collection from the file system.
@@ -5,6 +5,7 @@ import { DbFindOpts, DbOpts, FindOpts } from "./types/options.js";
5
5
  import { Arg, Search, Updater } from "./types/arg.js";
6
6
  import Data from "./types/data.js";
7
7
  import { Context } from "./types/types.js";
8
+ import FileCpu from "./types/fileCpu.js";
8
9
  /**
9
10
  * Represents a database management class for performing CRUD operations.
10
11
  * @class
@@ -12,7 +13,7 @@ import { Context } from "./types/types.js";
12
13
  declare class DataBase {
13
14
  dbAction: dbActionC;
14
15
  executor: executorC;
15
- constructor(folder: string, options?: DbOpts);
16
+ constructor(folder: string, options?: DbOpts, fileCpu?: FileCpu);
16
17
  /**
17
18
  * Create a new instance of a CollectionManager class.
18
19
  */
@@ -1,6 +1,7 @@
1
1
  import dbActionC from "./action.js";
2
2
  import executorC from "./executor.js";
3
3
  import CollectionManager from "./CollectionManager.js";
4
+ import vFileCpu from "./file/index.js";
4
5
  /**
5
6
  * Represents a database management class for performing CRUD operations.
6
7
  * @class
@@ -8,8 +9,10 @@ import CollectionManager from "./CollectionManager.js";
8
9
  class DataBase {
9
10
  dbAction;
10
11
  executor;
11
- constructor(folder, options = {}) {
12
- this.dbAction = new dbActionC(folder, options);
12
+ constructor(folder, options = {}, fileCpu) {
13
+ if (!fileCpu)
14
+ fileCpu = vFileCpu;
15
+ this.dbAction = new dbActionC(folder, options, fileCpu);
13
16
  this.executor = new executorC();
14
17
  }
15
18
  /**
@@ -0,0 +1,19 @@
1
+ import { Context } from "vm";
2
+ import { Search, Updater } from "../types/arg.js";
3
+ import Data from "../types/data.js";
4
+ import FileCpu from "../types/fileCpu.js";
5
+ import { FindOpts } from "../types/options.js";
6
+ type WriteFile = (file: string, data: any[]) => Promise<void>;
7
+ type ReadFile = (file: string) => Promise<any[]>;
8
+ declare class CustomFileCpu implements FileCpu {
9
+ _readFile: ReadFile;
10
+ _writeFile: WriteFile;
11
+ constructor(readFile: ReadFile, writeFile: WriteFile);
12
+ add(file: string, data: Data): Promise<void>;
13
+ addMany(file: string, data: Data[]): Promise<void>;
14
+ find(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<any[] | false>;
15
+ findOne(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<any | false>;
16
+ remove(cpath: string, arg: Search, context?: Context, one?: boolean): Promise<boolean>;
17
+ update(cpath: string, arg: Search, updater: Updater, context?: Context, one?: boolean): Promise<boolean>;
18
+ }
19
+ export default CustomFileCpu;
@@ -0,0 +1,74 @@
1
+ import hasFieldsAdvanced from "../utils/hasFieldsAdvanced.js";
2
+ import updateFindObject from "../utils/updateFindObject.js";
3
+ import { pathRepair } from "./utils.js";
4
+ class CustomFileCpu {
5
+ _readFile;
6
+ _writeFile;
7
+ constructor(readFile, writeFile) {
8
+ this._readFile = readFile;
9
+ this._writeFile = writeFile;
10
+ }
11
+ async add(file, data) {
12
+ file = pathRepair(file);
13
+ let entries = await this._readFile(file);
14
+ entries.push(data);
15
+ await this._writeFile(file, entries);
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
+ async find(file, arg, context = {}, findOpts = {}) {
24
+ file = pathRepair(file);
25
+ const entries = await this._readFile(file);
26
+ 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;
28
+ }
29
+ async findOne(file, arg, context = {}, findOpts = {}) {
30
+ file = pathRepair(file);
31
+ const entries = await this._readFile(file);
32
+ const result = entries.find(entry => typeof arg === "function" ? arg(entry, context) : hasFieldsAdvanced(entry, arg));
33
+ return result ? updateFindObject(result, findOpts) : false;
34
+ }
35
+ async remove(cpath, arg, context = {}, one = false) {
36
+ const file = pathRepair(cpath);
37
+ let entries = await this._readFile(file);
38
+ let removed = false;
39
+ entries = entries.filter(entry => {
40
+ if (removed && one)
41
+ return true;
42
+ let match = typeof arg === "function" ? arg(entry, context) : hasFieldsAdvanced(entry, arg);
43
+ if (match) {
44
+ removed = true;
45
+ return false;
46
+ }
47
+ return true;
48
+ });
49
+ if (!removed)
50
+ return false;
51
+ await this._writeFile(file, entries);
52
+ return true;
53
+ }
54
+ async update(cpath, arg, updater, context = {}, one = false) {
55
+ const file = pathRepair(cpath);
56
+ let entries = await this._readFile(file);
57
+ let updated = false;
58
+ entries = entries.map(entry => {
59
+ if (updated && one)
60
+ return entry;
61
+ let match = typeof arg === "function" ? arg(entry, context) : hasFieldsAdvanced(entry, arg);
62
+ if (match) {
63
+ updated = true;
64
+ return typeof updater === "function" ? updater(entry, context) : { ...entry, ...updater };
65
+ }
66
+ return entry;
67
+ });
68
+ if (!updated)
69
+ return false;
70
+ await this._writeFile(file, entries);
71
+ return true;
72
+ }
73
+ }
74
+ export default CustomFileCpu;
@@ -4,8 +4,8 @@ import { FindOpts } from "../types/options.js";
4
4
  /**
5
5
  * Asynchronously finds entries in a file based on search criteria.
6
6
  */
7
- export declare function find(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<unknown>;
7
+ export declare function find(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<any[] | false>;
8
8
  /**
9
9
  * Asynchronously finds one entry in a file based on search criteria.
10
10
  */
11
- export declare function findOne(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<unknown>;
11
+ export declare function findOne(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<any | false>;
@@ -1,3 +1,3 @@
1
- export { default as update } from "./update.js";
2
- export { default as remove } from "./remove.js";
3
- export * from "./find.js";
1
+ import FileCpu from "../types/fileCpu.js";
2
+ declare const vFileCpu: FileCpu;
3
+ export default vFileCpu;
@@ -1,3 +1,16 @@
1
- export { default as update } from "./update.js";
2
- export { default as remove } from "./remove.js";
3
- export * from "./find.js";
1
+ import update from "./update.js";
2
+ import remove from "./remove.js";
3
+ import { find, findOne } from "./find.js";
4
+ import { appendFileSync } from "fs";
5
+ import { stringify } from "../format.js";
6
+ const vFileCpu = {
7
+ add: async (file, data) => {
8
+ const dataString = stringify(data);
9
+ appendFileSync(file, dataString + "\n");
10
+ },
11
+ find,
12
+ findOne,
13
+ update,
14
+ remove
15
+ };
16
+ export default vFileCpu;
@@ -4,10 +4,11 @@ import DataBaseRemote from "./client/database.js";
4
4
  import GraphRemote from "./client/graph.js";
5
5
  import genId from "./gen.js";
6
6
  import Relation from "./relation.js";
7
- export { DataBase, Graph, DataBaseRemote, GraphRemote, Relation, genId };
7
+ import CustomFileCpu from "./file/customFileCpu.js";
8
+ export { DataBase, Graph, DataBaseRemote, GraphRemote, Relation, genId, CustomFileCpu, };
8
9
  import type Id from "./types/Id.js";
9
10
  import type { Arg, Search, Updater } from "./types/arg.js";
10
11
  import type { DbFindOpts, FindOpts, DbOpts } from "./types/options.js";
11
12
  import type Data from "./types/data.js";
12
13
  import type { SearchOptions } from "./types/searchOpts.js";
13
- export type { Id, Arg, Search, Updater, DbFindOpts, FindOpts, DbOpts, Data, SearchOptions };
14
+ export type { Id, Arg, Search, Updater, DbFindOpts, FindOpts, DbOpts, Data, SearchOptions, };
package/dist/esm/index.js CHANGED
@@ -4,4 +4,5 @@ import DataBaseRemote from "./client/database.js";
4
4
  import GraphRemote from "./client/graph.js";
5
5
  import genId from "./gen.js";
6
6
  import Relation from "./relation.js";
7
- export { DataBase, Graph, DataBaseRemote, GraphRemote, Relation, genId };
7
+ import CustomFileCpu from "./file/customFileCpu.js";
8
+ export { DataBase, Graph, DataBaseRemote, GraphRemote, Relation, genId, CustomFileCpu, };
@@ -0,0 +1,51 @@
1
+ import { Search, Updater } from "./arg.js";
2
+ import Data from "./data.js";
3
+ import { FindOpts } from "./options.js";
4
+ import { Context } from "./types.js";
5
+ interface FileCpu {
6
+ /**
7
+ * Asynchronously adds an entry to a file.
8
+ * @param file The path to the file.
9
+ * @param data The data to add.
10
+ * @returns A promise resolving to `void`.
11
+ */
12
+ add(file: string, data: Data): Promise<void>;
13
+ /**
14
+ * Asynchronously finds multiple entries in a file based on search criteria.
15
+ * @param file The path to the file.
16
+ * @param arg The search criteria.
17
+ * @param context Additional context for the search.
18
+ * @param findOpts Additional options for searching.
19
+ * @returns A promise resolving to an array of found entries, or `false` if the file does not exist.
20
+ */
21
+ find(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<any[] | false>;
22
+ /**
23
+ * Asynchronously finds one entry in a file based on search criteria.
24
+ * @param file The path to the file.
25
+ * @param arg The search criteria.
26
+ * @param context Additional context for the search.
27
+ * @param findOpts Additional options for searching.
28
+ * @returns A promise resolving to the found entry or `false` if not found.
29
+ */
30
+ findOne(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<any | false>;
31
+ /**
32
+ * Asynchronously removes entries from a file based on search criteria.
33
+ * @param cpath The path to the directory containing the files.
34
+ * @param arg The search criteria.
35
+ * @param context Additional context for the operation.
36
+ * @param one If `true`, removes only the first matching entry.
37
+ * @returns A promise resolving to `true` if at least one entry was removed, otherwise `false`.
38
+ */
39
+ remove(cpath: string, arg: Search, context?: Context, one?: boolean): Promise<boolean>;
40
+ /**
41
+ * Asynchronously updates entries in a file based on search criteria and an updater function or object.
42
+ * @param cpath The path to the directory containing the files.
43
+ * @param arg The search criteria.
44
+ * @param updater The updater function or object.
45
+ * @param context Additional context for the operation.
46
+ * @param one If `true`, updates only the first matching entry.
47
+ * @returns A promise resolving to `true` if at least one entry was updated, otherwise `false`.
48
+ */
49
+ update(cpath: string, arg: Search, updater: Updater, context?: Context, one?: boolean): Promise<boolean>;
50
+ }
51
+ export default FileCpu;
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@wxn0brp/db",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "main": "dist/esm/index.js",
5
5
  "types": "dist/esm/index.d.ts",
6
6
  "description": "A simple file-based database management system with support for CRUD operations, custom queries, and graph structures.",
7
- "homepage": "https://github.com/wxn0brP/database",
7
+ "homepage": "https://github.com/wxn0brP/ValtheraDB",
8
8
  "repository": {
9
9
  "type": "git",
10
- "url": "https://github.com/wxn0brP/database.git"
10
+ "url": "https://github.com/wxn0brP/ValtheraDB.git"
11
11
  },
12
12
  "keywords": [
13
13
  "database",