@wxn0brp/db-core 0.2.2 → 0.2.4

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.
@@ -2,6 +2,7 @@ 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;
5
6
  init(...args: any[]): Promise<void>;
6
7
  getCollections(): Promise<string[]>;
7
8
  ensureCollection(config: VQuery): Promise<boolean>;
@@ -1,6 +1,7 @@
1
1
  import { setDataUsingUpdateOneOrAdd } from "../helpers/updateOneOrAdd.js";
2
2
  class ActionsBase {
3
3
  _inited = true;
4
+ numberId = false;
4
5
  async init(...args) { }
5
6
  async getCollections() {
6
7
  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({ collection, data, id_gen }: VQuery): Promise<import("../types/arg.js").Arg>;
11
+ add(query: 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 genId from "../helpers/gen.js";
1
+ import { addId } from "../helpers/addId.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({ collection, data, id_gen = true }) {
13
- await this.ensureCollection(arguments[0]);
14
- if (id_gen)
15
- data._id = data._id || genId();
12
+ async add(query) {
13
+ await this.ensureCollection(query);
14
+ await addId(query, this);
15
+ const { collection, data } = query;
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, 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>;
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>;
18
18
  }
19
19
  export default CustomFileCpu;
@@ -1,5 +1,6 @@
1
1
  import hasFieldsAdvanced from "./utils/hasFieldsAdvanced.js";
2
2
  import updateFindObject from "./utils/updateFindObject.js";
3
+ import updateObjectAdvanced from "./utils/updateObject.js";
3
4
  export function pathRepair(path) {
4
5
  return path.replaceAll("//", "/");
5
6
  }
@@ -16,26 +17,26 @@ class CustomFileCpu {
16
17
  entries.push(data);
17
18
  await this._writeFile(file, entries);
18
19
  }
19
- async find(file, arg, context = {}, findOpts = {}) {
20
+ async find(file, search, context = {}, findOpts = {}) {
20
21
  file = pathRepair(file);
21
22
  const entries = await this._readFile(file);
22
- const results = entries.filter(entry => typeof arg === "function" ? arg(entry, context) : hasFieldsAdvanced(entry, arg));
23
+ const results = entries.filter(entry => typeof search === "function" ? search(entry, context) : hasFieldsAdvanced(entry, search));
23
24
  return results.length ? results.map(res => updateFindObject(res, findOpts)) : [];
24
25
  }
25
- async findOne(file, arg, context = {}, findOpts = {}) {
26
+ async findOne(file, search, context = {}, findOpts = {}) {
26
27
  file = pathRepair(file);
27
28
  const entries = await this._readFile(file);
28
- const result = entries.find(entry => typeof arg === "function" ? arg(entry, context) : hasFieldsAdvanced(entry, arg));
29
+ const result = entries.find(entry => typeof search === "function" ? search(entry, context) : hasFieldsAdvanced(entry, search));
29
30
  return result ? updateFindObject(result, findOpts) : false;
30
31
  }
31
- async remove(file, one, arg, context = {}) {
32
+ async remove(file, one, search, context = {}) {
32
33
  file = pathRepair(file);
33
34
  let entries = await this._readFile(file);
34
35
  let removed = false;
35
36
  entries = entries.filter(entry => {
36
37
  if (removed && one)
37
38
  return true;
38
- let match = typeof arg === "function" ? arg(entry, context) : hasFieldsAdvanced(entry, arg);
39
+ let match = typeof search === "function" ? search(entry, context) : hasFieldsAdvanced(entry, search);
39
40
  if (match) {
40
41
  removed = true;
41
42
  return false;
@@ -47,17 +48,17 @@ class CustomFileCpu {
47
48
  await this._writeFile(file, entries);
48
49
  return true;
49
50
  }
50
- async update(file, one, arg, updater, context = {}) {
51
+ async update(file, one, search, updater, context = {}) {
51
52
  file = pathRepair(file);
52
53
  let entries = await this._readFile(file);
53
54
  let updated = false;
54
55
  entries = entries.map(entry => {
55
56
  if (updated && one)
56
57
  return entry;
57
- let match = typeof arg === "function" ? arg(entry, context) : hasFieldsAdvanced(entry, arg);
58
+ let match = typeof search === "function" ? search(entry, context) : hasFieldsAdvanced(entry, search);
58
59
  if (match) {
59
60
  updated = true;
60
- return typeof updater === "function" ? updater(entry, context) : { ...entry, ...updater };
61
+ return typeof updater === "function" ? updater(entry, context) : updateObjectAdvanced(entry, updater);
61
62
  }
62
63
  return entry;
63
64
  });
@@ -0,0 +1,19 @@
1
+ import ActionsBase from "../base/actions.js";
2
+ import { VQuery } from "../types/query.js";
3
+ export declare class MultiBackend extends ActionsBase {
4
+ backends: ActionsBase[];
5
+ primaryBackend: ActionsBase;
6
+ constructor(backends: ActionsBase[], primaryIndex?: number);
7
+ init(...args: any[]): Promise<void>;
8
+ add(config: VQuery): Promise<import("../types/data.js").Data>;
9
+ find(config: VQuery): Promise<import("../types/data.js").Data[]>;
10
+ findOne(config: VQuery): Promise<import("../types/data.js").Data>;
11
+ update(config: VQuery): Promise<boolean>;
12
+ updateOne(config: VQuery): Promise<boolean>;
13
+ remove(config: VQuery): Promise<boolean>;
14
+ removeOne(config: VQuery): Promise<boolean>;
15
+ ensureCollection(config: VQuery): Promise<boolean>;
16
+ issetCollection(config: VQuery): Promise<boolean>;
17
+ removeCollection(config: VQuery): Promise<boolean>;
18
+ getCollections(): Promise<string[]>;
19
+ }
@@ -0,0 +1,94 @@
1
+ import ActionsBase from "../base/actions.js";
2
+ export class MultiBackend extends ActionsBase {
3
+ backends;
4
+ primaryBackend;
5
+ constructor(backends, primaryIndex = 0) {
6
+ super();
7
+ this.backends = backends;
8
+ this.primaryBackend = backends[primaryIndex] || backends[0];
9
+ }
10
+ async init(...args) {
11
+ await Promise.all(this.backends.map(b => b.init?.(...args)));
12
+ }
13
+ async add(config) {
14
+ return await this.primaryBackend.add({ ...config });
15
+ }
16
+ async find(config) {
17
+ const results = await Promise.all(this.backends.map(b => b.find({ ...config })));
18
+ return results.flat();
19
+ }
20
+ async findOne(config) {
21
+ for (const backend of this.backends) {
22
+ try {
23
+ const result = await backend.findOne({ ...config });
24
+ if (result !== null && result !== undefined) {
25
+ return result;
26
+ }
27
+ }
28
+ catch {
29
+ continue;
30
+ }
31
+ }
32
+ return null;
33
+ }
34
+ async update(config) {
35
+ const results = await Promise.all(this.backends.map(b => b.update({ ...config })));
36
+ return results.some(Boolean);
37
+ }
38
+ async updateOne(config) {
39
+ for (const backend of this.backends) {
40
+ try {
41
+ const result = await backend.updateOne({ ...config });
42
+ if (result === true) {
43
+ return true;
44
+ }
45
+ }
46
+ catch (error) {
47
+ continue;
48
+ }
49
+ }
50
+ return false;
51
+ }
52
+ async remove(config) {
53
+ const results = await Promise.all(this.backends.map(b => b.remove({ ...config })));
54
+ return results.some(Boolean);
55
+ }
56
+ async removeOne(config) {
57
+ for (const backend of this.backends) {
58
+ try {
59
+ const result = await backend.removeOne({ ...config });
60
+ if (result === true) {
61
+ return true;
62
+ }
63
+ }
64
+ catch (error) {
65
+ continue;
66
+ }
67
+ }
68
+ return false;
69
+ }
70
+ async ensureCollection(config) {
71
+ const results = await Promise.all(this.backends.map(b => b.ensureCollection({ ...config })));
72
+ return results.some(Boolean);
73
+ }
74
+ async issetCollection(config) {
75
+ for (const backend of this.backends) {
76
+ if (await backend.issetCollection({ ...config })) {
77
+ return true;
78
+ }
79
+ }
80
+ return false;
81
+ }
82
+ async removeCollection(config) {
83
+ const results = await Promise.all(this.backends.map(b => b.removeCollection({ ...config })));
84
+ return results.some(Boolean);
85
+ }
86
+ async getCollections() {
87
+ const allCollections = await Promise.all(this.backends.map(b => b.getCollections()));
88
+ const uniqueCollections = new Set();
89
+ allCollections.forEach(collections => {
90
+ collections.forEach(collection => uniqueCollections.add(collection));
91
+ });
92
+ return Array.from(uniqueCollections);
93
+ }
94
+ }
@@ -16,6 +16,8 @@ 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;
19
21
  }
20
22
  async init(...args) {
21
23
  if (this.dbAction._inited)
@@ -0,0 +1,3 @@
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>;
@@ -0,0 +1,24 @@
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
+ }
@@ -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,4 +25,5 @@ declare class executorC {
25
25
  */
26
26
  execute(): Promise<void>;
27
27
  }
28
- export default executorC;
28
+ export default ExecutorC;
29
+ export { ExecutorC as 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,4 +42,5 @@ class executorC {
42
42
  this.isExecuting = false;
43
43
  }
44
44
  }
45
- export default executorC;
45
+ export default ExecutorC;
46
+ export { ExecutorC as 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 arg The search criteria.
16
+ * @param search 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, arg: Search, context?: VContext, findOpts?: FindOpts): Promise<any[] | false>;
21
+ find(file: string, search: 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 arg The search criteria.
25
+ * @param search 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, arg: Search, context?: VContext, findOpts?: FindOpts): Promise<any | false>;
30
+ findOne(file: string, search: 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 arg The search criteria.
34
+ * @param search 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, arg: Search, context?: VContext): Promise<boolean>;
39
+ remove(file: string, one: boolean, search: 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 arg The search criteria.
43
+ * @param search 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, arg: Search, updater: Updater, context?: VContext): Promise<boolean>;
49
+ update(file: string, one: boolean, search: Search, updater: Updater, context?: VContext): Promise<boolean>;
50
50
  }
51
51
  export default FileCpu;
@@ -5,6 +5,7 @@ export interface DbOpts {
5
5
  maxFileSize?: number;
6
6
  dbAction?: ActionsBase;
7
7
  executor?: executorC;
8
+ numberId?: boolean;
8
9
  }
9
10
  export interface DbFindOpts<T = any> {
10
11
  reverse?: boolean;
@@ -9,7 +9,6 @@ export interface VQuery {
9
9
  findOpts?: FindOpts;
10
10
  data?: Arg;
11
11
  id_gen?: boolean;
12
- limit?: number;
13
12
  add_arg?: Arg;
14
13
  updater?: Updater;
15
14
  }
@@ -47,7 +47,7 @@ export type ArrayOperators<T = any> = {
47
47
  };
48
48
  /** String Operators */
49
49
  export type StringOperators<T = any> = {
50
- $regex?: PartialOfType<T, RegExp, string>;
50
+ $regex?: PartialOfType<T, RegExp | string, string>;
51
51
  $startsWith?: PartialOfType<T, string>;
52
52
  $endsWith?: PartialOfType<T, string>;
53
53
  };
@@ -104,7 +104,8 @@ function checkType(obj, fields) {
104
104
  }
105
105
  function checkRegex(obj, fields) {
106
106
  if ("$regex" in fields) {
107
- for (const [key, regex] of Object.entries(fields["$regex"])) {
107
+ for (const [key, regexData] of Object.entries(fields["$regex"])) {
108
+ const regex = typeof regexData === "string" ? new RegExp(regexData) : regexData;
108
109
  if (!regex.test(obj[key]))
109
110
  return false;
110
111
  }
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = "0.2.2";
1
+ export const version = "0.2.4";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db-core",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "types": "dist/index.d.ts",
5
5
  "main": "dist/index.js",
6
6
  "author": "wxn0brP",
@@ -35,4 +35,4 @@
35
35
  "default": "./dist/*.js"
36
36
  }
37
37
  }
38
- }
38
+ }