@wxn0brp/db-core 0.3.2 → 0.5.0-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.
Files changed (48) hide show
  1. package/dist/base/actions.d.ts +6 -7
  2. package/dist/base/actions.js +5 -6
  3. package/dist/base/custom.d.ts +7 -7
  4. package/dist/base/custom.js +5 -3
  5. package/dist/customFileCpu.d.ts +7 -8
  6. package/dist/customFileCpu.js +18 -20
  7. package/dist/db/memory.d.ts +5 -5
  8. package/dist/db/memory.js +3 -3
  9. package/dist/db/multiStorage.d.ts +5 -5
  10. package/dist/db/multiStorage.js +9 -11
  11. package/dist/db/routedStorage.d.ts +5 -5
  12. package/dist/db/routedStorage.js +1 -1
  13. package/dist/db/valthera.d.ts +19 -24
  14. package/dist/db/valthera.js +52 -27
  15. package/dist/helpers/addId.d.ts +1 -1
  16. package/dist/helpers/addId.js +1 -1
  17. package/dist/helpers/collection.d.ts +49 -0
  18. package/dist/helpers/collection.js +59 -0
  19. package/dist/helpers/executor.d.ts +2 -4
  20. package/dist/helpers/executor.js +1 -3
  21. package/dist/helpers/forge.d.ts +4 -4
  22. package/dist/helpers/forge.js +2 -2
  23. package/dist/helpers/gen.d.ts +18 -2
  24. package/dist/helpers/gen.js +36 -30
  25. package/dist/helpers/relation.d.ts +5 -2
  26. package/dist/helpers/relation.js +44 -33
  27. package/dist/index.d.ts +11 -13
  28. package/dist/index.js +7 -8
  29. package/dist/types/Id.d.ts +1 -2
  30. package/dist/types/data.d.ts +0 -1
  31. package/dist/types/fileCpu.d.ts +6 -7
  32. package/dist/types/options.d.ts +3 -3
  33. package/dist/types/query.d.ts +12 -0
  34. package/dist/types/relation.d.ts +1 -1
  35. package/dist/types/valthera.d.ts +21 -30
  36. package/dist/utils/action.d.ts +2 -2
  37. package/dist/utils/hasFields.d.ts +1 -1
  38. package/dist/utils/hasFields.js +1 -1
  39. package/dist/utils/hasFieldsAdvanced.d.ts +1 -1
  40. package/dist/utils/hasFieldsAdvanced.js +2 -2
  41. package/dist/utils/updateFindObject.d.ts +1 -1
  42. package/dist/utils/updateFindObject.js +1 -1
  43. package/dist/utils/updateObject.d.ts +1 -1
  44. package/dist/utils/updateObject.js +1 -1
  45. package/dist/version.js +1 -1
  46. package/package.json +1 -1
  47. package/dist/helpers/CollectionManager.d.ts +0 -47
  48. package/dist/helpers/CollectionManager.js +0 -63
@@ -1,6 +1,6 @@
1
- import Data from "../types/data.js";
1
+ import { Data } from "../types/data.js";
2
2
  import { VQuery } from "../types/query.js";
3
- declare class ActionsBase {
3
+ export declare class ActionsBase {
4
4
  _inited: boolean;
5
5
  numberId: boolean;
6
6
  init(...args: any[]): Promise<void>;
@@ -10,12 +10,11 @@ declare class ActionsBase {
10
10
  add(config: VQuery): Promise<Data>;
11
11
  find(config: VQuery): Promise<Data[]>;
12
12
  findOne(config: VQuery): Promise<Data | null>;
13
- update(config: VQuery): Promise<boolean>;
14
- updateOne(config: VQuery): Promise<boolean>;
15
- remove(config: VQuery): Promise<boolean>;
16
- removeOne(config: VQuery): Promise<boolean>;
13
+ update(config: VQuery): Promise<Data>;
14
+ updateOne(config: VQuery): Promise<Data>;
15
+ remove(config: VQuery): Promise<Data>;
16
+ removeOne(config: VQuery): Promise<Data>;
17
17
  removeCollection(config: VQuery): Promise<boolean>;
18
18
  updateOneOrAdd(config: VQuery): Promise<boolean>;
19
19
  toggleOne(config: VQuery): Promise<boolean>;
20
20
  }
21
- export default ActionsBase;
@@ -1,5 +1,5 @@
1
1
  import { setDataForToggleOne, setDataForUpdateOneOrAdd } from "../helpers/assignDataPush.js";
2
- class ActionsBase {
2
+ export class ActionsBase {
3
3
  _inited = true;
4
4
  numberId = false;
5
5
  async init(...args) { }
@@ -29,19 +29,19 @@ class ActionsBase {
29
29
  }
30
30
  async update(config) {
31
31
  throw new Error("Not implemented");
32
- return false;
32
+ return [];
33
33
  }
34
34
  async updateOne(config) {
35
35
  throw new Error("Not implemented");
36
- return false;
36
+ return {};
37
37
  }
38
38
  async remove(config) {
39
39
  throw new Error("Not implemented");
40
- return false;
40
+ return [];
41
41
  }
42
42
  async removeOne(config) {
43
43
  throw new Error("Not implemented");
44
- return false;
44
+ return {};
45
45
  }
46
46
  async removeCollection(config) {
47
47
  throw new Error("Not implemented");
@@ -64,4 +64,3 @@ class ActionsBase {
64
64
  return false;
65
65
  }
66
66
  }
67
- export default ActionsBase;
@@ -1,7 +1,7 @@
1
- import CustomFileCpu from "../customFileCpu.js";
2
- import Data from "../types/data.js";
1
+ import { CustomFileCpu } from "../customFileCpu.js";
2
+ import { Data } from "../types/data.js";
3
3
  import { VQuery } from "../types/query.js";
4
- import ActionsBase from "./actions.js";
4
+ import { ActionsBase } from "./actions.js";
5
5
  export declare class CustomActionsBase extends ActionsBase {
6
6
  fileCpu: CustomFileCpu;
7
7
  constructor();
@@ -20,17 +20,17 @@ export declare class CustomActionsBase extends ActionsBase {
20
20
  /**
21
21
  * Update entries in the specified database based on search criteria and an updater function or object.
22
22
  */
23
- update({ collection, search, updater, context }: VQuery): Promise<boolean>;
23
+ update({ collection, search, updater, context }: VQuery): Promise<Data[]>;
24
24
  /**
25
25
  * Update the first matching entry in the specified database based on search criteria and an updater function or object.
26
26
  */
27
- updateOne({ collection, search, updater, context }: VQuery): Promise<boolean>;
27
+ updateOne({ collection, search, updater, context }: VQuery): Promise<Data>;
28
28
  /**
29
29
  * Remove entries from the specified database based on search criteria.
30
30
  */
31
- remove({ collection, search, context }: VQuery): Promise<boolean>;
31
+ remove({ collection, search, context }: VQuery): Promise<Data[]>;
32
32
  /**
33
33
  * Remove the first matching entry from the specified database based on search criteria.
34
34
  */
35
- removeOne({ collection, search, context }: VQuery): Promise<boolean>;
35
+ removeOne({ collection, search, context }: VQuery): Promise<Data>;
36
36
  }
@@ -1,6 +1,6 @@
1
1
  import { addId } from "../helpers/addId.js";
2
2
  import { findUtil } from "../utils/action.js";
3
- import ActionsBase from "./actions.js";
3
+ import { ActionsBase } from "./actions.js";
4
4
  export class CustomActionsBase extends ActionsBase {
5
5
  fileCpu;
6
6
  constructor() {
@@ -44,7 +44,8 @@ export class CustomActionsBase extends ActionsBase {
44
44
  */
45
45
  async updateOne({ collection, search, updater, context = {} }) {
46
46
  await this.ensureCollection(arguments[0]);
47
- return await this.fileCpu.update(collection, true, search, updater, context);
47
+ const res = await this.fileCpu.update(collection, true, search, updater, context);
48
+ return res.length ? res[0] : null;
48
49
  }
49
50
  /**
50
51
  * Remove entries from the specified database based on search criteria.
@@ -58,6 +59,7 @@ export class CustomActionsBase extends ActionsBase {
58
59
  */
59
60
  async removeOne({ collection, search, context = {} }) {
60
61
  await this.ensureCollection(arguments[0]);
61
- return await this.fileCpu.remove(collection, true, search, context);
62
+ const res = await this.fileCpu.remove(collection, true, search, context);
63
+ return res.length ? res[0] : null;
62
64
  }
63
65
  }
@@ -1,19 +1,18 @@
1
1
  import { Search, Updater } from "./types/arg.js";
2
- import Data from "./types/data.js";
3
- import FileCpu from "./types/fileCpu.js";
2
+ import { Data } from "./types/data.js";
3
+ import { FileCpu } from "./types/fileCpu.js";
4
4
  import { FindOpts } from "./types/options.js";
5
5
  import { VContext } from "./types/types.js";
6
6
  export type WriteFile = (file: string, data: any[]) => Promise<void>;
7
7
  export type ReadFile = (file: string) => Promise<any[]>;
8
8
  export declare function pathRepair(path: string): string;
9
- declare class CustomFileCpu implements FileCpu {
9
+ export declare class CustomFileCpu implements FileCpu {
10
10
  _readFile: ReadFile;
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, search: Search, context?: VContext, findOpts?: FindOpts): Promise<Data[]>;
15
+ findOne(file: string, search: Search, context?: VContext, findOpts?: FindOpts): Promise<Data | false>;
16
+ remove(file: string, one: boolean, search: Search, context?: VContext): Promise<Data[]>;
17
+ update(file: string, one: boolean, search: Search, updater: Updater, context?: VContext): Promise<Data[]>;
18
18
  }
19
- export default CustomFileCpu;
@@ -1,10 +1,10 @@
1
- import hasFieldsAdvanced from "./utils/hasFieldsAdvanced.js";
2
- import updateFindObject from "./utils/updateFindObject.js";
3
- import updateObjectAdvanced from "./utils/updateObject.js";
1
+ import { hasFieldsAdvanced } from "./utils/hasFieldsAdvanced.js";
2
+ import { updateFindObject } from "./utils/updateFindObject.js";
3
+ import { updateObjectAdvanced } from "./utils/updateObject.js";
4
4
  export function pathRepair(path) {
5
5
  return path.replaceAll("//", "/");
6
6
  }
7
- class CustomFileCpu {
7
+ export class CustomFileCpu {
8
8
  _readFile;
9
9
  _writeFile;
10
10
  constructor(readFile, writeFile) {
@@ -32,40 +32,38 @@ class CustomFileCpu {
32
32
  async remove(file, one, search, context = {}) {
33
33
  file = pathRepair(file);
34
34
  let entries = await this._readFile(file);
35
- let removed = false;
35
+ const removed = [];
36
36
  entries = entries.filter(entry => {
37
- if (removed && one)
37
+ if (removed.length && one)
38
38
  return true;
39
39
  let match = typeof search === "function" ? search(entry, context) : hasFieldsAdvanced(entry, search);
40
40
  if (match) {
41
- removed = true;
41
+ removed.push(entry);
42
42
  return false;
43
43
  }
44
44
  return true;
45
45
  });
46
- if (!removed)
47
- return false;
48
- await this._writeFile(file, entries);
49
- return true;
46
+ if (removed.length)
47
+ await this._writeFile(file, entries);
48
+ return removed;
50
49
  }
51
50
  async update(file, one, search, updater, context = {}) {
52
51
  file = pathRepair(file);
53
52
  let entries = await this._readFile(file);
54
- let updated = false;
53
+ const updated = [];
55
54
  entries = entries.map(entry => {
56
- if (updated && one)
55
+ if (updated.length && one)
57
56
  return entry;
58
57
  let match = typeof search === "function" ? search(entry, context) : hasFieldsAdvanced(entry, search);
59
58
  if (match) {
60
- updated = true;
61
- return typeof updater === "function" ? updater(entry, context) : updateObjectAdvanced(entry, updater);
59
+ const updatedEntry = typeof updater === "function" ? updater(entry, context) : updateObjectAdvanced(entry, updater);
60
+ updated.push(updatedEntry);
61
+ return updatedEntry;
62
62
  }
63
63
  return entry;
64
64
  });
65
- if (!updated)
66
- return false;
67
- await this._writeFile(file, entries);
68
- return true;
65
+ if (updated.length)
66
+ await this._writeFile(file, entries);
67
+ return updated;
69
68
  }
70
69
  }
71
- export default CustomFileCpu;
@@ -1,8 +1,8 @@
1
1
  import { CustomActionsBase } from "../base/custom.js";
2
- import CollectionManager from "../helpers/CollectionManager.js";
3
- import Data from "../types/data.js";
2
+ import { Collection } from "../helpers/collection.js";
3
+ import { Data } from "../types/data.js";
4
4
  import { VQuery } from "../types/query.js";
5
- import ValtheraClass from "./valthera.js";
5
+ import { ValtheraClass } from "./valthera.js";
6
6
  export declare class MemoryAction extends CustomActionsBase {
7
7
  memory: Map<string, any[]>;
8
8
  constructor();
@@ -13,9 +13,9 @@ export declare class MemoryAction extends CustomActionsBase {
13
13
  issetCollection({ collection }: VQuery): Promise<boolean>;
14
14
  removeCollection({ collection }: VQuery): Promise<boolean>;
15
15
  }
16
- export default class ValtheraMemory extends ValtheraClass {
16
+ export declare class ValtheraMemory extends ValtheraClass {
17
17
  constructor(...args: any[]);
18
18
  }
19
19
  export declare function createMemoryValthera<T extends Record<string, Data[]>>(data?: T): ValtheraMemory & {
20
- [K in keyof T]: CollectionManager<T[K][number]>;
20
+ [K in keyof T]: Collection<T[K][number]>;
21
21
  };
package/dist/db/memory.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { CustomActionsBase } from "../base/custom.js";
2
- import CustomFileCpu from "../customFileCpu.js";
2
+ import { CustomFileCpu } from "../customFileCpu.js";
3
3
  import { forgeValthera } from "../helpers/forge.js";
4
- import ValtheraClass from "./valthera.js";
4
+ import { ValtheraClass } from "./valthera.js";
5
5
  export class MemoryAction extends CustomActionsBase {
6
6
  memory;
7
7
  constructor() {
@@ -35,7 +35,7 @@ export class MemoryAction extends CustomActionsBase {
35
35
  return true;
36
36
  }
37
37
  }
38
- export default class ValtheraMemory extends ValtheraClass {
38
+ export class ValtheraMemory extends ValtheraClass {
39
39
  constructor(...args) {
40
40
  super({ dbAction: new MemoryAction() });
41
41
  }
@@ -1,4 +1,4 @@
1
- import ActionsBase from "../base/actions.js";
1
+ import { ActionsBase } from "../base/actions.js";
2
2
  import { VQuery } from "../types/query.js";
3
3
  export declare class MultiBackend extends ActionsBase {
4
4
  backends: ActionsBase[];
@@ -8,10 +8,10 @@ export declare class MultiBackend extends ActionsBase {
8
8
  add(config: VQuery): Promise<import("../types/data.js").Data>;
9
9
  find(config: VQuery): Promise<import("../types/data.js").Data[]>;
10
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>;
11
+ update(config: VQuery): Promise<import("../types/data.js").Data[]>;
12
+ updateOne(config: VQuery): Promise<import("../types/data.js").Data>;
13
+ remove(config: VQuery): Promise<import("../types/data.js").Data[]>;
14
+ removeOne(config: VQuery): Promise<import("../types/data.js").Data>;
15
15
  ensureCollection(config: VQuery): Promise<boolean>;
16
16
  issetCollection(config: VQuery): Promise<boolean>;
17
17
  removeCollection(config: VQuery): Promise<boolean>;
@@ -1,4 +1,4 @@
1
- import ActionsBase from "../base/actions.js";
1
+ import { ActionsBase } from "../base/actions.js";
2
2
  export class MultiBackend extends ActionsBase {
3
3
  backends;
4
4
  primaryBackend;
@@ -33,39 +33,37 @@ export class MultiBackend extends ActionsBase {
33
33
  }
34
34
  async update(config) {
35
35
  const results = await Promise.all(this.backends.map(b => b.update({ ...config })));
36
- return results.some(Boolean);
36
+ return results.flat();
37
37
  }
38
38
  async updateOne(config) {
39
39
  for (const backend of this.backends) {
40
40
  try {
41
41
  const result = await backend.updateOne({ ...config });
42
- if (result === true) {
43
- return true;
44
- }
42
+ if (result)
43
+ return result;
45
44
  }
46
45
  catch (error) {
47
46
  continue;
48
47
  }
49
48
  }
50
- return false;
49
+ return null;
51
50
  }
52
51
  async remove(config) {
53
52
  const results = await Promise.all(this.backends.map(b => b.remove({ ...config })));
54
- return results.some(Boolean);
53
+ return results.flat();
55
54
  }
56
55
  async removeOne(config) {
57
56
  for (const backend of this.backends) {
58
57
  try {
59
58
  const result = await backend.removeOne({ ...config });
60
- if (result === true) {
61
- return true;
62
- }
59
+ if (result)
60
+ return result;
63
61
  }
64
62
  catch (error) {
65
63
  continue;
66
64
  }
67
65
  }
68
- return false;
66
+ return null;
69
67
  }
70
68
  async ensureCollection(config) {
71
69
  const results = await Promise.all(this.backends.map(b => b.ensureCollection({ ...config })));
@@ -1,4 +1,4 @@
1
- import ActionsBase from "../base/actions.js";
1
+ import { ActionsBase } from "../base/actions.js";
2
2
  import { VQuery } from "../types/query.js";
3
3
  export type MatchRule = string | RegExp | ((query: VQuery) => boolean);
4
4
  export interface RouteEntry {
@@ -16,10 +16,10 @@ export declare class RoutedStorage extends ActionsBase {
16
16
  add(config: VQuery): Promise<import("../types/data.js").Data>;
17
17
  find(config: VQuery): Promise<import("../types/data.js").Data[]>;
18
18
  findOne(config: VQuery): Promise<import("../types/data.js").Data>;
19
- update(config: VQuery): Promise<boolean>;
20
- updateOne(config: VQuery): Promise<boolean>;
21
- remove(config: VQuery): Promise<boolean>;
22
- removeOne(config: VQuery): Promise<boolean>;
19
+ update(config: VQuery): Promise<import("../types/data.js").Data>;
20
+ updateOne(config: VQuery): Promise<import("../types/data.js").Data>;
21
+ remove(config: VQuery): Promise<import("../types/data.js").Data>;
22
+ removeOne(config: VQuery): Promise<import("../types/data.js").Data>;
23
23
  ensureCollection(config: VQuery): Promise<boolean>;
24
24
  issetCollection(config: VQuery): Promise<boolean>;
25
25
  removeCollection(config: VQuery): Promise<boolean>;
@@ -1,4 +1,4 @@
1
- import ActionsBase from "../base/actions.js";
1
+ import { ActionsBase } from "../base/actions.js";
2
2
  export class RoutedStorage extends ActionsBase {
3
3
  rules;
4
4
  defaultBackend;
@@ -1,13 +1,11 @@
1
1
  import { VEE } from "@wxn0brp/event-emitter";
2
- import ActionsBase from "../base/actions.js";
3
- import CollectionManager from "../helpers/CollectionManager.js";
4
- import executorC from "../helpers/executor.js";
5
- import { Arg, Search, Updater } from "../types/arg.js";
6
- import Data from "../types/data.js";
7
- import { DbFindOpts, DbOpts, FindOpts } from "../types/options.js";
2
+ import { ActionsBase } from "../base/actions.js";
3
+ import { Collection } from "../helpers/collection.js";
4
+ import { Executor } from "../helpers/executor.js";
5
+ import { Data } from "../types/data.js";
6
+ import { DbOpts } from "../types/options.js";
8
7
  import { VQuery } from "../types/query.js";
9
- import { VContext } from "../types/types.js";
10
- import { UpdateOneOrAdd, ValtheraCompatible } from "../types/valthera.js";
8
+ import { AddQuery, FindOneQuery, FindQuery, RemoveQuery, ToggleOneQuery, UpdateOneOrAddQuery, UpdateQuery, ValtheraCompatible } from "../types/valthera.js";
11
9
  type DbActionsFns = keyof {
12
10
  [K in keyof ActionsBase as ActionsBase[K] extends (...args: any[]) => any ? K : never]: any;
13
11
  };
@@ -15,9 +13,9 @@ type DbActionsFns = keyof {
15
13
  * Represents a database management class for performing CRUD operations.
16
14
  * @class
17
15
  */
18
- declare class ValtheraClass implements ValtheraCompatible {
16
+ export declare class ValtheraClass implements ValtheraCompatible {
19
17
  dbAction: ActionsBase;
20
- executor: executorC;
18
+ executor: Executor;
21
19
  emiter: VEE<{
22
20
  [K in Exclude<DbActionsFns, "init">]: (query: VQuery, result: Awaited<ReturnType<ValtheraCompatible[K]>>) => void;
23
21
  } & {
@@ -30,7 +28,7 @@ declare class ValtheraClass implements ValtheraCompatible {
30
28
  /**
31
29
  * Create a new instance of a CollectionManager class.
32
30
  */
33
- c<T = Data>(collection: string): CollectionManager<T>;
31
+ c<T = Data>(collection: string): Collection<T>;
34
32
  /**
35
33
  * Get the names of all available databases.
36
34
  */
@@ -46,45 +44,42 @@ declare class ValtheraClass implements ValtheraCompatible {
46
44
  /**
47
45
  * Add data to a database.
48
46
  */
49
- add<T extends object>(collection: string, data: T, id_gen?: true): Promise<T & {
50
- _id: string;
51
- }>;
52
- add<T extends object>(collection: string, data: T, id_gen: false): Promise<T>;
47
+ add<T = Data>(query: AddQuery): Promise<T>;
53
48
  /**
54
49
  * Find data in a database.
55
50
  */
56
- find<T = Data>(collection: string, search?: Search<T>, dbFindOpts?: DbFindOpts<T>, findOpts?: FindOpts<T>, context?: VContext): Promise<T[]>;
51
+ find<T = Data>(query: FindQuery): Promise<T[]>;
57
52
  /**
58
53
  * Find one data entry in a database.
59
54
  */
60
- findOne<T = Data>(collection: string, search?: Search<T>, findOpts?: FindOpts<T>, context?: VContext): Promise<T>;
55
+ findOne<T = Data>(query: FindOneQuery): Promise<T>;
61
56
  /**
62
57
  * Update data in a database.
63
58
  */
64
- update<T = Data>(collection: string, search: Search<T>, updater: Updater<T>, context?: {}): Promise<boolean>;
59
+ update<T = Data>(query: UpdateQuery): Promise<T[]>;
65
60
  /**
66
61
  * Update one data entry in a database.
67
62
  */
68
- updateOne<T = Data>(collection: string, search: Search<T>, updater: Updater<T>, context?: VContext): Promise<boolean>;
63
+ updateOne<T = Data>(query: UpdateQuery): Promise<T>;
69
64
  /**
70
65
  * Remove data from a database.
71
66
  */
72
- remove<T = Data>(collection: string, search: Search<T>, context?: VContext): Promise<boolean>;
67
+ remove<T = Data>(query: RemoveQuery): Promise<T[]>;
73
68
  /**
74
69
  * Remove one data entry from a database.
75
70
  */
76
- removeOne<T = Data>(collection: string, search: Search<T>, context?: VContext): Promise<boolean>;
71
+ removeOne<T = Data>(query: RemoveQuery): Promise<T>;
77
72
  /**
78
73
  * Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
79
74
  */
80
- updateOneOrAdd<T = Data>(collection: string, search: Search<T>, updater: Updater<T>, { add_arg, context, id_gen }?: UpdateOneOrAdd<T>): Promise<boolean>;
75
+ updateOneOrAdd<T = Data>(query: UpdateOneOrAddQuery): Promise<T>;
81
76
  /**
82
77
  * Asynchronously removes one entry in a database or adds a new one if it doesn't exist. Usage e.g. for toggling a flag.
83
78
  */
84
- toggleOne<T = Data>(collection: string, search: Search<T>, data?: Arg<T>, context?: VContext): Promise<boolean>;
79
+ toggleOne<T = Data>(query: ToggleOneQuery): Promise<T>;
85
80
  /**
86
81
  * Removes a database collection from the file system.
87
82
  */
88
83
  removeCollection(collection: string): Promise<boolean>;
89
84
  }
90
- export default ValtheraClass;
85
+ export {};
@@ -1,20 +1,20 @@
1
1
  import { VEE } from "@wxn0brp/event-emitter";
2
- import ActionsBase from "../base/actions.js";
3
- import CollectionManager from "../helpers/CollectionManager.js";
4
- import executorC from "../helpers/executor.js";
2
+ import { ActionsBase } from "../base/actions.js";
3
+ import { Collection } from "../helpers/collection.js";
4
+ import { Executor } from "../helpers/executor.js";
5
5
  import { version } from "../version.js";
6
6
  /**
7
7
  * Represents a database management class for performing CRUD operations.
8
8
  * @class
9
9
  */
10
- class ValtheraClass {
10
+ export class ValtheraClass {
11
11
  dbAction;
12
12
  executor;
13
13
  emiter;
14
14
  version = version;
15
15
  constructor(options = {}) {
16
16
  this.dbAction = options.dbAction || new ActionsBase();
17
- this.executor = options.executor || new executorC();
17
+ this.executor = options.executor || new Executor();
18
18
  this.emiter = new VEE();
19
19
  if (options.numberId)
20
20
  this.dbAction.numberId = true;
@@ -41,7 +41,7 @@ class ValtheraClass {
41
41
  * Create a new instance of a CollectionManager class.
42
42
  */
43
43
  c(collection) {
44
- return new CollectionManager(this, collection);
44
+ return new Collection(this, collection);
45
45
  }
46
46
  /**
47
47
  * Get the names of all available databases.
@@ -61,62 +61,87 @@ class ValtheraClass {
61
61
  async issetCollection(collection) {
62
62
  return await this.execute("issetCollection", { collection });
63
63
  }
64
- async add(collection, data, id_gen = true) {
65
- return await this.execute("add", { collection, data, id_gen });
64
+ /**
65
+ * Add data to a database.
66
+ */
67
+ add(query) {
68
+ query.control ||= {};
69
+ return this.execute("add", query);
66
70
  }
67
71
  /**
68
72
  * Find data in a database.
69
73
  */
70
- async find(collection, search = {}, dbFindOpts = {}, findOpts = {}, context = {}) {
71
- return await this.execute("find", { collection, search, context, dbFindOpts, findOpts });
74
+ find(query) {
75
+ query.dbFindOpts ||= {};
76
+ query.findOpts ||= {};
77
+ query.context ||= {};
78
+ query.control ||= {};
79
+ return this.execute("find", query);
72
80
  }
73
81
  /**
74
82
  * Find one data entry in a database.
75
83
  */
76
- async findOne(collection, search = {}, findOpts = {}, context = {}) {
77
- return await this.execute("findOne", { collection, search, context, findOpts });
84
+ findOne(query) {
85
+ query.findOpts ||= {};
86
+ query.context ||= {};
87
+ query.control ||= {};
88
+ return this.execute("findOne", query);
78
89
  }
79
90
  /**
80
91
  * Update data in a database.
81
92
  */
82
- async update(collection, search, updater, context = {}) {
83
- return await this.execute("update", { collection, search, updater, context });
93
+ update(query) {
94
+ query.context ||= {};
95
+ query.control ||= {};
96
+ return this.execute("update", query);
84
97
  }
85
98
  /**
86
99
  * Update one data entry in a database.
87
100
  */
88
- async updateOne(collection, search, updater, context = {}) {
89
- return await this.execute("updateOne", { collection, search, updater, context });
101
+ updateOne(query) {
102
+ query.context ||= {};
103
+ query.control ||= {};
104
+ return this.execute("updateOne", query);
90
105
  }
91
106
  /**
92
107
  * Remove data from a database.
93
108
  */
94
- async remove(collection, search, context = {}) {
95
- return await this.execute("remove", { collection, search, context });
109
+ remove(query) {
110
+ query.context ||= {};
111
+ query.control ||= {};
112
+ return this.execute("remove", query);
96
113
  }
97
114
  /**
98
115
  * Remove one data entry from a database.
99
116
  */
100
- async removeOne(collection, search, context = {}) {
101
- return await this.execute("removeOne", { collection, search, context });
117
+ removeOne(query) {
118
+ query.context ||= {};
119
+ query.control ||= {};
120
+ return this.execute("removeOne", query);
102
121
  }
103
122
  /**
104
123
  * Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
105
124
  */
106
- async updateOneOrAdd(collection, search, updater, { add_arg = {}, context = {}, id_gen = true } = {}) {
107
- return await this.execute("updateOneOrAdd", { collection, search, updater, add_arg, context, id_gen });
125
+ updateOneOrAdd(query) {
126
+ query.context ||= {};
127
+ query.add_arg ||= {};
128
+ query.control ||= {};
129
+ query.id_gen ||= true;
130
+ return this.execute("updateOneOrAdd", query);
108
131
  }
109
132
  /**
110
133
  * Asynchronously removes one entry in a database or adds a new one if it doesn't exist. Usage e.g. for toggling a flag.
111
134
  */
112
- async toggleOne(collection, search, data = {}, context = {}) {
113
- return await this.execute("toggleOne", { collection, search, data, context, id_gen: false });
135
+ toggleOne(query) {
136
+ query.data ||= {};
137
+ query.context ||= {};
138
+ query.control ||= {};
139
+ return this.execute("toggleOne", query);
114
140
  }
115
141
  /**
116
142
  * Removes a database collection from the file system.
117
143
  */
118
- async removeCollection(collection) {
119
- return await this.execute("removeCollection", { collection });
144
+ removeCollection(collection) {
145
+ return this.execute("removeCollection", { collection });
120
146
  }
121
147
  }
122
- export default ValtheraClass;
@@ -1,3 +1,3 @@
1
- import ActionsBase from "../base/actions.js";
1
+ import { ActionsBase } from "../base/actions.js";
2
2
  import { VQuery } from "../types/query.js";
3
3
  export declare function addId(query: VQuery, actions: ActionsBase, defaultGen?: boolean): Promise<import("../types/arg.js").Arg>;
@@ -1,4 +1,4 @@
1
- import genId from "./gen.js";
1
+ import { genId } from "./gen.js";
2
2
  export async function addId(query, actions, defaultGen = true) {
3
3
  const { collection, data } = query;
4
4
  const id_gen = query.id_gen ?? defaultGen;