@wxn0brp/db-core 0.1.4 → 0.1.6

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/README.md CHANGED
@@ -12,6 +12,10 @@ It is **not recommended** to install this module separately unless you plan to c
12
12
  npm install @wxn0brp/db-core
13
13
  ```
14
14
 
15
+ ## Documentation
16
+
17
+ See the [documentation](docs/README.md) for more information about the API and usage.
18
+
15
19
  ## License
16
20
 
17
21
  This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
@@ -0,0 +1,27 @@
1
+ import ActionsBase from "../base/actions.js";
2
+ import { VQuery } from "../types/query.js";
3
+ export type MatchRule = string | RegExp | ((query: VQuery) => boolean);
4
+ export interface RouteEntry {
5
+ match: MatchRule;
6
+ backends: ActionsBase[];
7
+ }
8
+ export declare class RoutedStorage extends ActionsBase {
9
+ rules: RouteEntry[];
10
+ defaultBackend: ActionsBase[];
11
+ constructor(rules: RouteEntry[], defaultBackend: ActionsBase | ActionsBase[]);
12
+ _matchBackends(config: VQuery): ActionsBase[];
13
+ _withAll<T>(config: VQuery, fn: (b: ActionsBase) => Promise<T>, gather?: boolean): Promise<T>;
14
+ _withFirst<T>(config: VQuery, fn: (b: ActionsBase) => Promise<T>): Promise<T>;
15
+ init(...args: any[]): Promise<void>;
16
+ add(config: VQuery): Promise<import("../types/data.js").Data>;
17
+ find(config: VQuery): Promise<import("../types/data.js").Data[]>;
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>;
23
+ ensureCollection(config: VQuery): Promise<boolean>;
24
+ issetCollection(config: VQuery): Promise<boolean>;
25
+ removeCollection(config: VQuery): Promise<boolean>;
26
+ getCollections(): Promise<string[]>;
27
+ }
@@ -0,0 +1,82 @@
1
+ import ActionsBase from "../base/actions.js";
2
+ export class RoutedStorage extends ActionsBase {
3
+ rules;
4
+ defaultBackend;
5
+ constructor(rules, defaultBackend) {
6
+ super();
7
+ this.rules = rules;
8
+ this.defaultBackend = Array.isArray(defaultBackend) ? defaultBackend : [defaultBackend];
9
+ }
10
+ _matchBackends(config) {
11
+ const matched = [];
12
+ const { collection } = config;
13
+ for (const rule of this.rules) {
14
+ const { match, backends } = rule;
15
+ if ((typeof match === "string" && (match === collection) || match === "*") ||
16
+ (match instanceof RegExp && match.test(collection)) ||
17
+ (typeof match === "function" && match(config))) {
18
+ matched.push(...backends);
19
+ }
20
+ }
21
+ if (matched.length === 0)
22
+ matched.push(...this.defaultBackend);
23
+ return matched;
24
+ }
25
+ async _withAll(config, fn, gather = false) {
26
+ const backends = this._matchBackends(config);
27
+ if (gather) {
28
+ return await Promise.all(backends.map(fn)).then(res => res[0]);
29
+ }
30
+ else {
31
+ for (const b of backends)
32
+ await fn(b);
33
+ return undefined;
34
+ }
35
+ }
36
+ async _withFirst(config, fn) {
37
+ const [first] = this._matchBackends(config);
38
+ return await fn(first);
39
+ }
40
+ async init(...args) {
41
+ const all = new Set(this.rules.flatMap(r => r.backends));
42
+ await Promise.all(Array.from(all).map(b => b.init?.(...args)));
43
+ }
44
+ async add(config) {
45
+ const res = await this._withFirst(config, b => b.add(config));
46
+ await this._withAll({ ...config, data: res }, b => b.add({ ...config, data: res }));
47
+ return res;
48
+ }
49
+ async find(config) {
50
+ return this._withFirst(config, b => b.find(config));
51
+ }
52
+ async findOne(config) {
53
+ return this._withFirst(config, b => b.findOne(config));
54
+ }
55
+ async update(config) {
56
+ return this._withAll(config, b => b.update(config), true);
57
+ }
58
+ async updateOne(config) {
59
+ return this._withAll(config, b => b.updateOne(config), true);
60
+ }
61
+ async remove(config) {
62
+ return this._withAll(config, b => b.remove(config), true);
63
+ }
64
+ async removeOne(config) {
65
+ return this._withAll(config, b => b.removeOne(config), true);
66
+ }
67
+ async ensureCollection(config) {
68
+ return this._withAll(config, b => b.ensureCollection(config), true);
69
+ }
70
+ async issetCollection(config) {
71
+ return this._withFirst(config, b => b.issetCollection(config));
72
+ }
73
+ async removeCollection(config) {
74
+ return this._withAll(config, b => b.removeCollection(config), true);
75
+ }
76
+ async getCollections() {
77
+ const all = await Promise.all(Array.from(new Set(this.rules.flatMap(r => r.backends)))
78
+ .map(b => b.getCollections()));
79
+ const merged = new Set(all.flat());
80
+ return Array.from(merged);
81
+ }
82
+ }
@@ -1,4 +1,4 @@
1
- import { EventEmitter } from "@wxn0brp/event-emitter";
1
+ import { VEE } 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";
@@ -18,7 +18,7 @@ type DbActionsFns = keyof {
18
18
  declare class ValtheraClass implements ValtheraCompatible {
19
19
  dbAction: ActionsBase;
20
20
  executor: executorC;
21
- emiter: EventEmitter;
21
+ emiter: VEE;
22
22
  version: string;
23
23
  constructor(options?: DbOpts);
24
24
  init(...args: any[]): Promise<unknown>;
@@ -1,4 +1,4 @@
1
- import { EventEmitter } from "@wxn0brp/event-emitter";
1
+ import { VEE } 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";
@@ -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 EventEmitter();
18
+ this.emiter = new VEE();
19
19
  }
20
20
  async init(...args) {
21
21
  if (this.dbAction._inited)
package/dist/index.d.ts CHANGED
@@ -7,7 +7,7 @@ import { ValtheraTypes } from "./types/export.js";
7
7
  import { RelationTypes } from "./types/relation.js";
8
8
  import { ValtheraCompatible } from "./types/valthera.js";
9
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, };
10
+ import { sortByIds, convertIdToUnix } from "./utils/id.js";
11
+ export { createMemoryValthera, CustomFileCpu, forgeTypedValthera, forgeValthera, genId, Relation, ValtheraClass, ValtheraMemory, sortByIds, convertIdToUnix, };
12
12
  export type { RelationTypes, ValtheraCompatible, ValtheraTypes, };
13
13
  export type Id = import("./types/Id.js").Id;
package/dist/index.js CHANGED
@@ -4,5 +4,5 @@ import ValtheraClass from "./db/valthera.js";
4
4
  import genId from "./helpers/gen.js";
5
5
  import Relation from "./helpers/relation.js";
6
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, };
7
+ import { sortByIds, convertIdToUnix } from "./utils/id.js";
8
+ export { createMemoryValthera, CustomFileCpu, forgeTypedValthera, forgeValthera, genId, Relation, ValtheraClass, ValtheraMemory, sortByIds, convertIdToUnix, };
@@ -1,3 +1,4 @@
1
+ export declare function convertIdToUnix(id: string): number;
1
2
  export declare function sortByIds<T extends {
2
3
  _id: string;
3
4
  }>(objects: T[]): T[];
@@ -0,0 +1,8 @@
1
+ export function convertIdToUnix(id) {
2
+ return parseInt(id.split("-")[0], 36);
3
+ }
4
+ export function sortByIds(objects) {
5
+ return objects.slice().sort((a, b) => {
6
+ return convertIdToUnix(a._id) - convertIdToUnix(b._id) || a._id.localeCompare(b._id);
7
+ });
8
+ }
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = "0.1.4";
1
+ export const version = "0.1.6";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db-core",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "types": "dist/index.d.ts",
5
5
  "main": "dist/index.js",
6
6
  "author": "wxn0brP",
@@ -13,9 +13,9 @@
13
13
  },
14
14
  "homepage": "https://github.com/wxn0brP/ValtheraDB",
15
15
  "devDependencies": {
16
- "@types/node": "^22.12.0",
16
+ "@types/node": "^24.2.0",
17
17
  "tsc-alias": "^1.8.10",
18
- "typescript": "^5.7.3"
18
+ "typescript": "^5.9.2"
19
19
  },
20
20
  "files": [
21
21
  "dist"
@@ -33,6 +33,6 @@
33
33
  }
34
34
  },
35
35
  "dependencies": {
36
- "@wxn0brp/event-emitter": "^0.0.1"
36
+ "@wxn0brp/event-emitter": "^0.0.2"
37
37
  }
38
38
  }
@@ -1,7 +0,0 @@
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
- }