@wxn0brp/db-core 0.2.4 → 0.2.6-alpha.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/README.md CHANGED
@@ -14,7 +14,11 @@ npm install @wxn0brp/db-core
14
14
 
15
15
  ## Documentation
16
16
 
17
- See the [documentation](docs/README.md) for more information about the API and usage.
17
+ See [typedoc](https://wxn0brp.github.io/ValtheraDB-core/).
18
+
19
+ ## Contributing
20
+
21
+ Contributions are welcome! Please submit pull requests or open issues on GitHub.
18
22
 
19
23
  ## License
20
24
 
@@ -2,7 +2,6 @@ import Data from "../types/data.js";
2
2
  import { VQuery } from "../types/query.js";
3
3
  declare class ActionsBase {
4
4
  _inited: boolean;
5
- numberId: boolean;
6
5
  init(...args: any[]): Promise<void>;
7
6
  getCollections(): Promise<string[]>;
8
7
  ensureCollection(config: VQuery): Promise<boolean>;
@@ -1,7 +1,6 @@
1
1
  import { setDataUsingUpdateOneOrAdd } from "../helpers/updateOneOrAdd.js";
2
2
  class ActionsBase {
3
3
  _inited = true;
4
- numberId = false;
5
4
  async init(...args) { }
6
5
  async getCollections() {
7
6
  throw new Error("Not implemented");
@@ -8,7 +8,7 @@ export declare class CustomActionsBase extends ActionsBase {
8
8
  /**
9
9
  * Add a new entry to the specified database.
10
10
  */
11
- add(query: VQuery): Promise<import("../types/arg.js").Arg>;
11
+ add({ collection, data, id_gen }: VQuery): Promise<import("../types/arg.js").Arg>;
12
12
  /**
13
13
  * Find entries in the specified database based on search criteria.
14
14
  */
@@ -1,4 +1,4 @@
1
- import { addId } from "../helpers/addId.js";
1
+ import genId from "../helpers/gen.js";
2
2
  import { findUtil } from "../utils/action.js";
3
3
  import ActionsBase from "./actions.js";
4
4
  export class CustomActionsBase extends ActionsBase {
@@ -9,10 +9,10 @@ export class CustomActionsBase extends ActionsBase {
9
9
  /**
10
10
  * Add a new entry to the specified database.
11
11
  */
12
- async add(query) {
13
- await this.ensureCollection(query);
14
- await addId(query, this);
15
- const { collection, data } = query;
12
+ async add({ collection, data, id_gen = true }) {
13
+ await this.ensureCollection(arguments[0]);
14
+ if (id_gen)
15
+ data._id = data._id || genId();
16
16
  await this.fileCpu.add(collection, data);
17
17
  return data;
18
18
  }
@@ -11,9 +11,9 @@ declare class CustomFileCpu implements FileCpu {
11
11
  _writeFile: WriteFile;
12
12
  constructor(readFile: ReadFile, writeFile: WriteFile);
13
13
  add(file: string, data: Data): Promise<void>;
14
- find(file: string, search: Search, context?: VContext, findOpts?: FindOpts): Promise<any[] | false>;
15
- findOne(file: string, search: Search, context?: VContext, findOpts?: FindOpts): Promise<any | false>;
16
- remove(file: string, one: boolean, search: Search, context?: VContext): Promise<boolean>;
17
- update(file: string, one: boolean, search: Search, updater: Updater, context?: VContext): Promise<boolean>;
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
18
  }
19
19
  export default CustomFileCpu;
@@ -1,6 +1,5 @@
1
1
  import hasFieldsAdvanced from "./utils/hasFieldsAdvanced.js";
2
2
  import updateFindObject from "./utils/updateFindObject.js";
3
- import updateObjectAdvanced from "./utils/updateObject.js";
4
3
  export function pathRepair(path) {
5
4
  return path.replaceAll("//", "/");
6
5
  }
@@ -17,26 +16,26 @@ class CustomFileCpu {
17
16
  entries.push(data);
18
17
  await this._writeFile(file, entries);
19
18
  }
20
- async find(file, search, context = {}, findOpts = {}) {
19
+ async find(file, arg, context = {}, findOpts = {}) {
21
20
  file = pathRepair(file);
22
21
  const entries = await this._readFile(file);
23
- const results = entries.filter(entry => typeof search === "function" ? search(entry, context) : hasFieldsAdvanced(entry, search));
22
+ const results = entries.filter(entry => typeof arg === "function" ? arg(entry, context) : hasFieldsAdvanced(entry, arg));
24
23
  return results.length ? results.map(res => updateFindObject(res, findOpts)) : [];
25
24
  }
26
- async findOne(file, search, context = {}, findOpts = {}) {
25
+ async findOne(file, arg, context = {}, findOpts = {}) {
27
26
  file = pathRepair(file);
28
27
  const entries = await this._readFile(file);
29
- const result = entries.find(entry => typeof search === "function" ? search(entry, context) : hasFieldsAdvanced(entry, search));
28
+ const result = entries.find(entry => typeof arg === "function" ? arg(entry, context) : hasFieldsAdvanced(entry, arg));
30
29
  return result ? updateFindObject(result, findOpts) : false;
31
30
  }
32
- async remove(file, one, search, context = {}) {
31
+ async remove(file, one, arg, context = {}) {
33
32
  file = pathRepair(file);
34
33
  let entries = await this._readFile(file);
35
34
  let removed = false;
36
35
  entries = entries.filter(entry => {
37
36
  if (removed && one)
38
37
  return true;
39
- let match = typeof search === "function" ? search(entry, context) : hasFieldsAdvanced(entry, search);
38
+ let match = typeof arg === "function" ? arg(entry, context) : hasFieldsAdvanced(entry, arg);
40
39
  if (match) {
41
40
  removed = true;
42
41
  return false;
@@ -48,17 +47,17 @@ class CustomFileCpu {
48
47
  await this._writeFile(file, entries);
49
48
  return true;
50
49
  }
51
- async update(file, one, search, updater, context = {}) {
50
+ async update(file, one, arg, updater, context = {}) {
52
51
  file = pathRepair(file);
53
52
  let entries = await this._readFile(file);
54
53
  let updated = false;
55
54
  entries = entries.map(entry => {
56
55
  if (updated && one)
57
56
  return entry;
58
- let match = typeof search === "function" ? search(entry, context) : hasFieldsAdvanced(entry, search);
57
+ let match = typeof arg === "function" ? arg(entry, context) : hasFieldsAdvanced(entry, arg);
59
58
  if (match) {
60
59
  updated = true;
61
- return typeof updater === "function" ? updater(entry, context) : updateObjectAdvanced(entry, updater);
60
+ return typeof updater === "function" ? updater(entry, context) : { ...entry, ...updater };
62
61
  }
63
62
  return entry;
64
63
  });
@@ -16,8 +16,6 @@ class ValtheraClass {
16
16
  this.dbAction = options.dbAction || new ActionsBase();
17
17
  this.executor = options.executor || new executorC();
18
18
  this.emiter = new VEE();
19
- if (options.numberId)
20
- this.dbAction.numberId = true;
21
19
  }
22
20
  async init(...args) {
23
21
  if (this.dbAction._inited)
@@ -8,7 +8,7 @@ interface Task {
8
8
  * A simple executor for queuing and executing asynchronous operations sequentially.
9
9
  * @class
10
10
  */
11
- declare class ExecutorC {
11
+ declare class executorC {
12
12
  quote: Task[];
13
13
  isExecuting: boolean;
14
14
  /**
@@ -25,5 +25,4 @@ declare class ExecutorC {
25
25
  */
26
26
  execute(): Promise<void>;
27
27
  }
28
- export default ExecutorC;
29
- export { ExecutorC as executorC };
28
+ export default executorC;
@@ -2,7 +2,7 @@
2
2
  * A simple executor for queuing and executing asynchronous operations sequentially.
3
3
  * @class
4
4
  */
5
- class ExecutorC {
5
+ class executorC {
6
6
  quote;
7
7
  isExecuting;
8
8
  /**
@@ -42,5 +42,4 @@ class ExecutorC {
42
42
  this.isExecuting = false;
43
43
  }
44
44
  }
45
- export default ExecutorC;
46
- export { ExecutorC as executorC };
45
+ export default executorC;
@@ -13,39 +13,39 @@ interface FileCpu {
13
13
  /**
14
14
  * Asynchronously finds multiple entries in a file based on search criteria.
15
15
  * @param file The path to the file.
16
- * @param search The search criteria.
16
+ * @param arg The search criteria.
17
17
  * @param context Additional context for the search.
18
18
  * @param findOpts Additional options for searching.
19
19
  * @returns A promise resolving to an array of found entries, or `false` if the file does not exist.
20
20
  */
21
- find(file: string, search: Search, context?: VContext, findOpts?: FindOpts): Promise<any[] | false>;
21
+ find(file: string, arg: Search, context?: VContext, findOpts?: FindOpts): Promise<any[] | false>;
22
22
  /**
23
23
  * Asynchronously finds one entry in a file based on search criteria.
24
24
  * @param file The path to the file.
25
- * @param search The search criteria.
25
+ * @param arg The search criteria.
26
26
  * @param context Additional context for the search.
27
27
  * @param findOpts Additional options for searching.
28
28
  * @returns A promise resolving to the found entry or `false` if not found.
29
29
  */
30
- findOne(file: string, search: Search, context?: VContext, findOpts?: FindOpts): Promise<any | false>;
30
+ findOne(file: string, arg: Search, context?: VContext, findOpts?: FindOpts): Promise<any | false>;
31
31
  /**
32
32
  * Asynchronously removes entries from a file based on search criteria.
33
33
  * @param file The path to the file.
34
- * @param search The search criteria.
34
+ * @param arg The search criteria.
35
35
  * @param context Additional context for the operation.
36
36
  * @param one If `true`, removes only the first matching entry.
37
37
  * @returns A promise resolving to `true` if at least one entry was removed, otherwise `false`.
38
38
  */
39
- remove(file: string, one: boolean, search: Search, context?: VContext): Promise<boolean>;
39
+ remove(file: string, one: boolean, arg: Search, context?: VContext): Promise<boolean>;
40
40
  /**
41
41
  * Asynchronously updates entries in a file based on search criteria and an updater function or object.
42
42
  * @param file The path to the file.
43
- * @param search The search criteria.
43
+ * @param arg The search criteria.
44
44
  * @param updater The updater function or object.
45
45
  * @param context Additional context for the operation.
46
46
  * @param one If `true`, updates only the first matching entry.
47
47
  * @returns A promise resolving to `true` if at least one entry was updated, otherwise `false`.
48
48
  */
49
- update(file: string, one: boolean, search: Search, updater: Updater, context?: VContext): Promise<boolean>;
49
+ update(file: string, one: boolean, arg: Search, updater: Updater, context?: VContext): Promise<boolean>;
50
50
  }
51
51
  export default FileCpu;
@@ -5,7 +5,6 @@ export interface DbOpts {
5
5
  maxFileSize?: number;
6
6
  dbAction?: ActionsBase;
7
7
  executor?: executorC;
8
- numberId?: boolean;
9
8
  }
10
9
  export interface DbFindOpts<T = any> {
11
10
  reverse?: boolean;
@@ -9,6 +9,7 @@ export interface VQuery {
9
9
  findOpts?: FindOpts;
10
10
  data?: Arg;
11
11
  id_gen?: boolean;
12
+ limit?: number;
12
13
  add_arg?: Arg;
13
14
  updater?: Updater;
14
15
  }
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = "0.2.4";
1
+ export const version = "0.2.3";
package/package.json CHANGED
@@ -1,38 +1,41 @@
1
1
  {
2
- "name": "@wxn0brp/db-core",
3
- "version": "0.2.4",
4
- "types": "dist/index.d.ts",
5
- "main": "dist/index.js",
6
- "author": "wxn0brP",
7
- "license": "MIT",
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",
15
- "dependencies": {
16
- "@wxn0brp/event-emitter": "^0.0.2"
17
- },
18
- "devDependencies": {
19
- "@types/node": "^24.2.0",
20
- "tsc-alias": "^1.8.10",
21
- "typescript": "^5.9.2"
22
- },
23
- "files": [
24
- "dist"
25
- ],
26
- "exports": {
27
- ".": {
28
- "types": "./dist/index.d.ts",
29
- "import": "./dist/index.js",
30
- "default": "./dist/index.js"
2
+ "name": "@wxn0brp/db-core",
3
+ "version": "0.2.6-alpha.0",
4
+ "types": "dist/index.d.ts",
5
+ "main": "dist/index.js",
6
+ "author": "wxn0brP",
7
+ "license": "MIT",
8
+ "type": "module",
9
+ "description": "ValtheraDB Core",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/wxn0brP/ValtheraDB-core.git"
31
13
  },
32
- "./*": {
33
- "types": "./dist/*.d.ts",
34
- "import": "./dist/*.js",
35
- "default": "./dist/*.js"
14
+ "homepage": "https://github.com/wxn0brP/ValtheraDB",
15
+ "scripts": {
16
+ "build": "tsc && tsc-alias"
17
+ },
18
+ "dependencies": {
19
+ "@wxn0brp/event-emitter": "^0.0.3"
20
+ },
21
+ "devDependencies": {
22
+ "@types/node": "*",
23
+ "tsc-alias": "*",
24
+ "typescript": "*"
25
+ },
26
+ "files": [
27
+ "dist"
28
+ ],
29
+ "exports": {
30
+ ".": {
31
+ "types": "./dist/index.d.ts",
32
+ "import": "./dist/index.js",
33
+ "default": "./dist/index.js"
34
+ },
35
+ "./*": {
36
+ "types": "./dist/*.d.ts",
37
+ "import": "./dist/*.js",
38
+ "default": "./dist/*.js"
39
+ }
36
40
  }
37
- }
38
41
  }
@@ -1,3 +0,0 @@
1
- import ActionsBase from "../base/actions.js";
2
- import { VQuery } from "../types/query.js";
3
- export declare function addId(query: VQuery, actions: ActionsBase, defaultGen?: boolean): Promise<import("../types/arg.js").Arg>;
@@ -1,24 +0,0 @@
1
- import genId from "./gen.js";
2
- export async function addId(query, actions, defaultGen = true) {
3
- const { collection, data } = query;
4
- const id_gen = query.id_gen ?? defaultGen;
5
- if (!id_gen)
6
- return data;
7
- if (data._id)
8
- return data;
9
- const { numberId } = actions;
10
- if (!numberId) {
11
- data._id = genId();
12
- return;
13
- }
14
- const find = await actions.findOne({ collection: "__vdb_id", search: { c: collection } });
15
- data._id = find ? find.i + 1 : 1;
16
- await actions.updateOneOrAdd({
17
- collection: "__vdb_id",
18
- search: { c: collection },
19
- updater: {
20
- $inc: { i: 1 }
21
- },
22
- id_gen: false,
23
- });
24
- }