@wxn0brp/db-core 0.11.0-alpha.0 → 0.11.0-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.
@@ -4,6 +4,7 @@ import { VQueryT } from "../types/query.js";
4
4
  export declare abstract class ActionsBase implements ActionsBaseInterface {
5
5
  _inited: boolean;
6
6
  numberId: boolean;
7
+ smartExecutor: boolean;
7
8
  init(...args: any[]): Promise<void>;
8
9
  abstract getCollections(): Promise<string[]>;
9
10
  abstract ensureCollection(collection: string): Promise<boolean>;
@@ -2,6 +2,7 @@ import { setDataForToggleOne, setDataForUpdateOneOrAdd } from "../helpers/assign
2
2
  export class ActionsBase {
3
3
  _inited = true;
4
4
  numberId = false;
5
+ smartExecutor = false;
5
6
  async init(...args) { }
6
7
  async updateOneOrAdd(config) {
7
8
  const res = await this.updateOne(config);
@@ -4,6 +4,7 @@ import { Data } from "../types/data.js";
4
4
  import { ValtheraClass } from "./valthera.js";
5
5
  export declare class MemoryAction extends CustomActionsBase {
6
6
  memory: Map<string, any[]>;
7
+ smartExecutor: boolean;
7
8
  constructor();
8
9
  _readMemory(key: string): any[];
9
10
  _writeMemory(key: string, data: any[]): void;
package/dist/db/memory.js CHANGED
@@ -4,6 +4,7 @@ import { forgeValthera } from "../helpers/forge.js";
4
4
  import { ValtheraClass } from "./valthera.js";
5
5
  export class MemoryAction extends CustomActionsBase {
6
6
  memory;
7
+ smartExecutor = true;
7
8
  constructor() {
8
9
  super();
9
10
  this.fileCpu = new CustomFileCpu(this._readMemory.bind(this), this._writeMemory.bind(this));
@@ -1,10 +1,10 @@
1
1
  import { VEE } from "@wxn0brp/event-emitter";
2
2
  import { ActionsBase } from "../base/actions.js";
3
3
  import { Collection } from "../helpers/collection.js";
4
- import { Executor } from "../helpers/executor.js";
4
+ import { ExecutorInterface } from "../helpers/executor.js";
5
5
  import { Data } from "../types/data.js";
6
6
  import { DbOpts } from "../types/options.js";
7
- import { VQueryT, VQuery } from "../types/query.js";
7
+ import { VQuery, VQueryT } from "../types/query.js";
8
8
  import { ValtheraCompatible } from "../types/valthera.js";
9
9
  /**
10
10
  * Represents a database management class for performing CRUD operations.
@@ -12,7 +12,7 @@ import { ValtheraCompatible } from "../types/valthera.js";
12
12
  */
13
13
  export declare class ValtheraClass implements ValtheraCompatible {
14
14
  dbAction: ActionsBase;
15
- executor: Executor;
15
+ executor: ExecutorInterface;
16
16
  emiter: VEE<{
17
17
  [K in keyof ValtheraCompatible]: (query: VQuery, result: Awaited<ReturnType<ValtheraCompatible[K]>>) => void;
18
18
  } & {
@@ -20,7 +20,7 @@ export declare class ValtheraClass implements ValtheraCompatible {
20
20
  }>;
21
21
  version: string;
22
22
  constructor(options: DbOpts);
23
- init(...args: any[]): Promise<unknown>;
23
+ init(...args: any[]): Promise<any>;
24
24
  execute<T>(name: keyof ValtheraCompatible, query: VQuery<any> | string): Promise<T>;
25
25
  /**
26
26
  * Create a new instance of a Collection class.
@@ -1,6 +1,6 @@
1
1
  import { VEE } from "@wxn0brp/event-emitter";
2
2
  import { Collection } from "../helpers/collection.js";
3
- import { Executor } from "../helpers/executor.js";
3
+ import { Executor, 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.
@@ -13,7 +13,7 @@ export class ValtheraClass {
13
13
  version = version;
14
14
  constructor(options) {
15
15
  this.dbAction = options.dbAction;
16
- this.executor = options.executor || new Executor();
16
+ this.executor = options.executor || new (this.dbAction.smartExecutor ? SmartExecutor : Executor)();
17
17
  if (options.numberId)
18
18
  this.dbAction.numberId = true;
19
19
  }
@@ -30,7 +30,7 @@ export class ValtheraClass {
30
30
  }
31
31
  async execute(name, query) {
32
32
  await this.init();
33
- const result = await this.executor.addOp(this.dbAction[name].bind(this.dbAction), query);
33
+ const result = await this.executor.addOp(this.dbAction[name].bind(this.dbAction), query, typeof query === "string" ? query : query.collection);
34
34
  this.emiter.emit(name, query, result);
35
35
  return result;
36
36
  }
@@ -4,18 +4,35 @@ export interface Task {
4
4
  resolve: Function;
5
5
  reject: Function;
6
6
  }
7
+ export interface ExecutorInterface {
8
+ addOp(func: Function, query?: any, collection?: string): Promise<any>;
9
+ }
10
+ export interface SmartExecutorEntry {
11
+ executor: Executor;
12
+ interval: ReturnType<typeof setTimeout> | null;
13
+ }
7
14
  /**
8
15
  * A simple executor for queuing and executing asynchronous operations sequentially.
9
16
  */
10
- export declare class Executor {
11
- quote: Task[];
17
+ export declare class Executor implements ExecutorInterface {
18
+ queue: Task[];
12
19
  isExecuting: boolean;
13
20
  /**
14
21
  * Add an asynchronous operation to the execution queue.
15
22
  */
16
- addOp(func: Function, ...param: any[]): Promise<unknown>;
23
+ addOp(func: Function, query: any): Promise<any>;
17
24
  /**
18
25
  * Execute the queued asynchronous operations sequentially.
19
26
  */
20
27
  execute(): Promise<void>;
21
28
  }
29
+ /**
30
+ * A smart executor for queuing and executing asynchronous operations with a TTL.
31
+ */
32
+ export declare class SmartExecutor implements ExecutorInterface {
33
+ ttl: number;
34
+ collections: Map<string, SmartExecutorEntry>;
35
+ constructor(ttl?: number);
36
+ addOp(func: Function, query: any, collection: string): Promise<any>;
37
+ scheduleCleanup(key: string, entry: SmartExecutorEntry): number;
38
+ }
@@ -2,16 +2,16 @@
2
2
  * A simple executor for queuing and executing asynchronous operations sequentially.
3
3
  */
4
4
  export class Executor {
5
- quote = [];
5
+ queue = [];
6
6
  isExecuting = false;
7
7
  /**
8
8
  * Add an asynchronous operation to the execution queue.
9
9
  */
10
- async addOp(func, ...param) {
10
+ async addOp(func, query) {
11
11
  return await new Promise((resolve, reject) => {
12
- this.quote.push({
12
+ this.queue.push({
13
13
  func,
14
- param,
14
+ param: [query],
15
15
  resolve,
16
16
  reject
17
17
  });
@@ -25,11 +25,52 @@ export class Executor {
25
25
  if (this.isExecuting)
26
26
  return;
27
27
  this.isExecuting = true;
28
- while (this.quote.length > 0) {
29
- let q = this.quote.shift();
28
+ while (this.queue.length > 0) {
29
+ let q = this.queue.shift();
30
30
  let res = await q.func(...q.param);
31
31
  q.resolve(res);
32
32
  }
33
33
  this.isExecuting = false;
34
34
  }
35
35
  }
36
+ /**
37
+ * A smart executor for queuing and executing asynchronous operations with a TTL.
38
+ */
39
+ export class SmartExecutor {
40
+ ttl;
41
+ collections = new Map();
42
+ constructor(ttl = 5 * 60 * 1000) {
43
+ this.ttl = ttl;
44
+ }
45
+ async addOp(func, query, collection) {
46
+ const key = collection ?? "__default__";
47
+ let entry = this.collections.get(key);
48
+ if (!entry) {
49
+ entry = {
50
+ executor: new Executor(),
51
+ interval: null
52
+ };
53
+ this.collections.set(key, entry);
54
+ }
55
+ else if (entry.interval)
56
+ clearTimeout(entry.interval);
57
+ entry.interval = this.scheduleCleanup(key, entry);
58
+ return entry.executor.addOp(func, query);
59
+ }
60
+ scheduleCleanup(key, entry) {
61
+ const interval = setTimeout(() => {
62
+ const current = this.collections.get(key);
63
+ if (!current || current !== entry)
64
+ return;
65
+ if (current.executor.isExecuting) {
66
+ current.interval = this.scheduleCleanup(key, current);
67
+ }
68
+ else {
69
+ this.collections.delete(key);
70
+ }
71
+ }, this.ttl);
72
+ if (typeof interval === "object" && "unref" in interval)
73
+ interval.unref();
74
+ return interval;
75
+ }
76
+ }
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = "0.11.0-alpha.0";
1
+ export const version = "0.11.0-alpha.1";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db-core",
3
- "version": "0.11.0-alpha.0",
3
+ "version": "0.11.0-alpha.1",
4
4
  "types": "dist/index.d.ts",
5
5
  "main": "dist/index.js",
6
6
  "author": "wxn0brP",