@wxn0brp/db-core 0.1.2 → 0.1.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.
package/dist/db/memory.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { CustomActionsBase } from "../base/custom.js";
2
2
  import CustomFileCpu from "../customFileCpu.js";
3
- import { forgeValthera } from "../utils/forge.js";
3
+ import { forgeValthera } from "../helpers/forge.js";
4
4
  import ValtheraClass from "./valthera.js";
5
5
  export class MemoryAction extends CustomActionsBase {
6
6
  memory;
@@ -1,12 +1,16 @@
1
- import { UniversalEventEmitter } from "../helpers/eventEmiter.js";
1
+ import { EventEmitter } from "@wxn0brp/event-emitter";
2
2
  import ActionsBase from "../base/actions.js";
3
3
  import CollectionManager from "../helpers/CollectionManager.js";
4
4
  import executorC from "../helpers/executor.js";
5
5
  import { Arg, Search, Updater } from "../types/arg.js";
6
6
  import Data from "../types/data.js";
7
- import { DbOpts, DbFindOpts, FindOpts } from "../types/options.js";
7
+ import { DbFindOpts, DbOpts, FindOpts } from "../types/options.js";
8
+ import { VQuery } from "../types/query.js";
8
9
  import { VContext } from "../types/types.js";
9
10
  import { ValtheraCompatible } from "../types/valthera.js";
11
+ type DbActionsFns = keyof {
12
+ [K in keyof ActionsBase as ActionsBase[K] extends (...args: any[]) => any ? K : never]: any;
13
+ };
10
14
  /**
11
15
  * Represents a database management class for performing CRUD operations.
12
16
  * @class
@@ -14,11 +18,11 @@ import { ValtheraCompatible } from "../types/valthera.js";
14
18
  declare class ValtheraClass implements ValtheraCompatible {
15
19
  dbAction: ActionsBase;
16
20
  executor: executorC;
17
- emiter: UniversalEventEmitter;
21
+ emiter: EventEmitter;
18
22
  version: string;
19
23
  constructor(options?: DbOpts);
20
24
  init(...args: any[]): Promise<unknown>;
21
- private execute;
25
+ execute<T>(name: DbActionsFns, query: VQuery): Promise<T>;
22
26
  /**
23
27
  * Create a new instance of a CollectionManager class.
24
28
  */
@@ -38,7 +42,10 @@ declare class ValtheraClass implements ValtheraCompatible {
38
42
  /**
39
43
  * Add data to a database.
40
44
  */
41
- add<T = Data>(collection: string, data: Arg<T>, id_gen?: boolean): Promise<T>;
45
+ add<T extends object>(collection: string, data: T, id_gen?: true): Promise<T & {
46
+ _id: string;
47
+ }>;
48
+ add<T extends object>(collection: string, data: T, id_gen: false): Promise<T>;
42
49
  /**
43
50
  * Find data in a database.
44
51
  */
@@ -1,8 +1,8 @@
1
- import { version } from "../version.js";
2
- import { UniversalEventEmitter } from "../helpers/eventEmiter.js";
1
+ import { EventEmitter } from "@wxn0brp/event-emitter";
3
2
  import ActionsBase from "../base/actions.js";
4
3
  import CollectionManager from "../helpers/CollectionManager.js";
5
4
  import executorC from "../helpers/executor.js";
5
+ import { version } from "../version.js";
6
6
  /**
7
7
  * Represents a database management class for performing CRUD operations.
8
8
  * @class
@@ -15,7 +15,7 @@ class ValtheraClass {
15
15
  constructor(options = {}) {
16
16
  this.dbAction = options.dbAction || new ActionsBase();
17
17
  this.executor = options.executor || new executorC();
18
- this.emiter = new UniversalEventEmitter();
18
+ this.emiter = new EventEmitter();
19
19
  }
20
20
  async init(...args) {
21
21
  if (this.dbAction._inited)
@@ -28,11 +28,12 @@ class ValtheraClass {
28
28
  self.dbAction._inited = true;
29
29
  });
30
30
  }
31
- async execute(name, ...args) {
31
+ async execute(name, query) {
32
32
  await this.init();
33
- const result = await this.executor.addOp(this.dbAction[name].bind(this.dbAction), ...args);
34
- this.emiter.emit(name, args, result);
35
- this.emiter.emit("*", name, args, result);
33
+ const result = await this.executor.addOp(this.dbAction[name].bind(this.dbAction), query);
34
+ const emitArgs = Array.from(arguments).slice(1);
35
+ this.emiter.emit(name, emitArgs, result);
36
+ this.emiter.emit("*", name, emitArgs, result);
36
37
  return result;
37
38
  }
38
39
  /**
@@ -45,7 +46,7 @@ class ValtheraClass {
45
46
  * Get the names of all available databases.
46
47
  */
47
48
  async getCollections() {
48
- return await this.execute("getCollections");
49
+ return await this.execute("getCollections", {});
49
50
  }
50
51
  /**
51
52
  * Check and create the specified collection if it doesn't exist.
@@ -59,9 +60,6 @@ class ValtheraClass {
59
60
  async issetCollection(collection) {
60
61
  return await this.execute("issetCollection", { collection });
61
62
  }
62
- /**
63
- * Add data to a database.
64
- */
65
63
  async add(collection, data, id_gen = true) {
66
64
  return await this.execute("add", { collection, data, id_gen });
67
65
  }
@@ -1,4 +1,4 @@
1
- import CollectionManager from "../helpers/CollectionManager.js";
1
+ import CollectionManager from "./CollectionManager.js";
2
2
  import Data from "../types/data.js";
3
3
  import { ValtheraCompatible } from "../types/valthera.js";
4
4
  export declare function forgeValthera<T extends string>(target: ValtheraCompatible): ValtheraCompatible & { [K in T]: CollectionManager; };
@@ -1,4 +1,4 @@
1
- import CollectionManager from "../helpers/CollectionManager.js";
1
+ import CollectionManager from "./CollectionManager.js";
2
2
  export function forgeValthera(target) {
3
3
  return new Proxy(target, {
4
4
  get(target, prop, receiver) {
package/dist/index.d.ts CHANGED
@@ -6,7 +6,8 @@ import Relation from "./helpers/relation.js";
6
6
  import { ValtheraTypes } from "./types/export.js";
7
7
  import { RelationTypes } from "./types/relation.js";
8
8
  import { ValtheraCompatible } from "./types/valthera.js";
9
- import { forgeTypedValthera, forgeValthera } from "./utils/forge.js";
10
- export { createMemoryValthera, CustomFileCpu, forgeTypedValthera, forgeValthera, genId, Relation, ValtheraClass, ValtheraMemory, };
9
+ import { forgeTypedValthera, forgeValthera } from "./helpers/forge.js";
10
+ import { sortByIds } from "./utils/sortId.js";
11
+ export { createMemoryValthera, CustomFileCpu, forgeTypedValthera, forgeValthera, genId, Relation, ValtheraClass, ValtheraMemory, sortByIds, };
11
12
  export type { RelationTypes, ValtheraCompatible, ValtheraTypes, };
12
13
  export type Id = import("./types/Id.js").Id;
package/dist/index.js CHANGED
@@ -3,5 +3,6 @@ import ValtheraMemory, { createMemoryValthera } from "./db/memory.js";
3
3
  import ValtheraClass from "./db/valthera.js";
4
4
  import genId from "./helpers/gen.js";
5
5
  import Relation from "./helpers/relation.js";
6
- import { forgeTypedValthera, forgeValthera } from "./utils/forge.js";
7
- export { createMemoryValthera, CustomFileCpu, forgeTypedValthera, forgeValthera, genId, Relation, ValtheraClass, ValtheraMemory, };
6
+ import { forgeTypedValthera, forgeValthera } from "./helpers/forge.js";
7
+ import { sortByIds } from "./utils/sortId.js";
8
+ export { createMemoryValthera, CustomFileCpu, forgeTypedValthera, forgeValthera, genId, Relation, ValtheraClass, ValtheraMemory, sortByIds, };
@@ -0,0 +1,3 @@
1
+ export declare function sortByIds<T extends {
2
+ _id: string;
3
+ }>(objects: T[]): T[];
@@ -0,0 +1,7 @@
1
+ export function sortByIds(objects) {
2
+ return objects.slice().sort((a, b) => {
3
+ const [timestampA] = a._id.split("-");
4
+ const [timestampB] = b._id.split("-");
5
+ return timestampA.localeCompare(timestampB) || a._id.localeCompare(b._id);
6
+ });
7
+ }
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = "0.1.2";
1
+ export const version = "0.1.4";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db-core",
3
- "version": "0.1.2",
3
+ "version": "0.1.4",
4
4
  "types": "dist/index.d.ts",
5
5
  "main": "dist/index.js",
6
6
  "author": "wxn0brP",
@@ -31,5 +31,8 @@
31
31
  "import": "./dist/*.js",
32
32
  "default": "./dist/*.js"
33
33
  }
34
+ },
35
+ "dependencies": {
36
+ "@wxn0brp/event-emitter": "^0.0.1"
34
37
  }
35
38
  }
@@ -1,31 +0,0 @@
1
- export declare class UniversalEventEmitter {
2
- private _events;
3
- /**
4
- * Registers an event listener
5
- * @param {string} event - event name
6
- * @param {Function} listener - function to be called when event occurs
7
- */
8
- on(event: string, listener: Function): void;
9
- /**
10
- * Registers a one-time event listener
11
- * @param {string} event - event name
12
- * @param {Function} listener - function to be called once
13
- */
14
- once(event: string, listener: Function): void;
15
- /**
16
- * Removes an event listener
17
- * @param {string} event - event name
18
- * @param {Function} [listener] - optional: specific listener to remove
19
- */
20
- off(event?: string, listener?: Function): void;
21
- /**
22
- * Emits an event
23
- * @param {string} event - event name
24
- * @param {...any} args - arguments to be passed to listeners
25
- */
26
- emit(event: string, ...args: any[]): void;
27
- /**
28
- * Returns the number of listeners for the given event
29
- */
30
- listenerCount(event: string): number;
31
- }
@@ -1,65 +0,0 @@
1
- export class UniversalEventEmitter {
2
- _events = {};
3
- /**
4
- * Registers an event listener
5
- * @param {string} event - event name
6
- * @param {Function} listener - function to be called when event occurs
7
- */
8
- on(event, listener) {
9
- if (!this._events[event])
10
- this._events[event] = [];
11
- this._events[event].push(listener);
12
- }
13
- /**
14
- * Registers a one-time event listener
15
- * @param {string} event - event name
16
- * @param {Function} listener - function to be called once
17
- */
18
- once(event, listener) {
19
- const onceListener = (...args) => {
20
- this.off(event, onceListener);
21
- listener(...args);
22
- };
23
- this.on(event, onceListener);
24
- }
25
- /**
26
- * Removes an event listener
27
- * @param {string} event - event name
28
- * @param {Function} [listener] - optional: specific listener to remove
29
- */
30
- off(event, listener) {
31
- if (!event) {
32
- // Remove all events
33
- this._events = {};
34
- return;
35
- }
36
- if (!listener) {
37
- // Remove all listeners for the given event
38
- delete this._events[event];
39
- return;
40
- }
41
- // Remove specific listener
42
- if (this._events[event]) {
43
- this._events[event] = this._events[event].filter(l => l !== listener);
44
- }
45
- }
46
- /**
47
- * Emits an event
48
- * @param {string} event - event name
49
- * @param {...any} args - arguments to be passed to listeners
50
- */
51
- emit(event, ...args) {
52
- const listeners = this._events[event];
53
- if (listeners && listeners.length > 0) {
54
- listeners.forEach(listener => {
55
- listener(...args);
56
- });
57
- }
58
- }
59
- /**
60
- * Returns the number of listeners for the given event
61
- */
62
- listenerCount(event) {
63
- return this._events[event]?.length || 0;
64
- }
65
- }