@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 +4 -0
- package/dist/db/routedStorage.d.ts +27 -0
- package/dist/db/routedStorage.js +82 -0
- package/dist/db/valthera.d.ts +2 -2
- package/dist/db/valthera.js +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/utils/{sortId.d.ts → id.d.ts} +1 -0
- package/dist/utils/id.js +8 -0
- package/dist/version.js +1 -1
- package/package.json +4 -4
- package/dist/utils/sortId.js +0 -7
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
|
+
}
|
package/dist/db/valthera.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
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:
|
|
21
|
+
emiter: VEE;
|
|
22
22
|
version: string;
|
|
23
23
|
constructor(options?: DbOpts);
|
|
24
24
|
init(...args: any[]): Promise<unknown>;
|
package/dist/db/valthera.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
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
|
|
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/
|
|
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/
|
|
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, };
|
package/dist/utils/id.js
ADDED
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = "0.1.
|
|
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.
|
|
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": "^
|
|
16
|
+
"@types/node": "^24.2.0",
|
|
17
17
|
"tsc-alias": "^1.8.10",
|
|
18
|
-
"typescript": "^5.
|
|
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.
|
|
36
|
+
"@wxn0brp/event-emitter": "^0.0.2"
|
|
37
37
|
}
|
|
38
38
|
}
|
package/dist/utils/sortId.js
DELETED