@wxn0brp/db-core 0.11.3 → 0.11.4-alpha.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.
@@ -11,6 +11,7 @@ import { ValtheraCompatible } from "../types/valthera.js";
11
11
  * @class
12
12
  */
13
13
  export declare class ValtheraClass implements ValtheraCompatible {
14
+ options: DbOpts;
14
15
  dbAction: ActionsBase;
15
16
  executor: ExecutorInterface;
16
17
  emitter: VEE<{
@@ -1,12 +1,13 @@
1
1
  import { VEE } from "@wxn0brp/event-emitter";
2
2
  import { Collection } from "../helpers/collection.js";
3
- import { Executor, SmartExecutor } from "../helpers/executor.js";
3
+ import { SmartExecutor } from "../helpers/executor.js";
4
4
  import { version } from "../version.js";
5
5
  /**
6
6
  * Represents a database management class for performing CRUD operations.
7
7
  * @class
8
8
  */
9
9
  export class ValtheraClass {
10
+ options;
10
11
  dbAction;
11
12
  executor;
12
13
  emitter = new VEE();
@@ -14,18 +15,27 @@ export class ValtheraClass {
14
15
  emiter = this.emitter;
15
16
  version = version;
16
17
  constructor(options) {
17
- this.dbAction = options.dbAction;
18
- this.executor = options.executor || new (this.dbAction.smartExecutor ? SmartExecutor : Executor)();
19
- if (options.numberId)
20
- this.dbAction.numberId = true;
18
+ this.options = options;
19
+ this.executor = options.executor || new SmartExecutor(undefined, false);
20
+ if (typeof options.dbAction === "function")
21
+ return;
22
+ else
23
+ this.dbAction = options.dbAction;
21
24
  }
22
25
  async init(...args) {
23
- if (this.dbAction._inited)
26
+ if (this.dbAction?._inited)
24
27
  return;
25
28
  const self = this;
26
29
  return await this.executor.addOp(async () => {
27
- if (self.dbAction._inited)
30
+ if (self.dbAction?._inited)
28
31
  return;
32
+ if (typeof self.options.dbAction === "function")
33
+ self.dbAction = await self.options.dbAction();
34
+ // if the executor is not set, and the action wants a smart executor
35
+ if (!self.options.executor && self.dbAction.smartExecutor && self.executor instanceof SmartExecutor)
36
+ self.executor.aware = true;
37
+ if (self.options.numberId)
38
+ self.dbAction.numberId = true;
29
39
  await self.dbAction.init(...args);
30
40
  self.dbAction._inited = true;
31
41
  });
@@ -31,8 +31,9 @@ export declare class Executor implements ExecutorInterface {
31
31
  */
32
32
  export declare class SmartExecutor implements ExecutorInterface {
33
33
  ttl: number;
34
+ aware: boolean;
34
35
  collections: Map<string, SmartExecutorEntry>;
35
- constructor(ttl?: number);
36
+ constructor(ttl?: number, aware?: boolean);
36
37
  addOp(func: Function, query: any, collection: string): Promise<any>;
37
38
  scheduleCleanup(key: string, entry: SmartExecutorEntry): number;
38
39
  }
@@ -1,3 +1,4 @@
1
+ const defaultCollection = "__default__";
1
2
  /**
2
3
  * A simple executor for queuing and executing asynchronous operations sequentially.
3
4
  */
@@ -38,12 +39,16 @@ export class Executor {
38
39
  */
39
40
  export class SmartExecutor {
40
41
  ttl;
42
+ aware;
41
43
  collections = new Map();
42
- constructor(ttl = 5 * 60 * 1000) {
44
+ constructor(ttl = 5 * 60 * 1000, aware = true) {
43
45
  this.ttl = ttl;
46
+ this.aware = aware;
44
47
  }
45
48
  async addOp(func, query, collection) {
46
- const key = collection ?? "__default__";
49
+ const key = this.aware ?
50
+ collection ?? defaultCollection :
51
+ defaultCollection;
47
52
  let entry = this.collections.get(key);
48
53
  if (!entry) {
49
54
  entry = {
@@ -2,7 +2,7 @@ import { ActionsBase } from "../base/actions.js";
2
2
  import { Executor } from "../helpers/executor.js";
3
3
  import { KeysMatching } from "./utils.js";
4
4
  export interface DbOpts {
5
- dbAction: ActionsBase;
5
+ dbAction: ActionsBase | (() => Promise<ActionsBase>);
6
6
  executor?: Executor;
7
7
  numberId?: boolean;
8
8
  }
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = "0.11.3";
1
+ export const version = "0.11.4-alpha.1";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db-core",
3
- "version": "0.11.3",
3
+ "version": "0.11.4-alpha.1",
4
4
  "types": "dist/index.d.ts",
5
5
  "main": "dist/index.js",
6
6
  "author": "wxn0brP",