@wxn0brp/db-core 0.11.4 → 0.11.5-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.
- package/dist/db/valthera.d.ts +3 -0
- package/dist/db/valthera.js +31 -3
- package/dist/index.d.ts +1 -0
- package/dist/types/options.d.ts +3 -2
- package/dist/types/plugin.d.ts +6 -0
- package/dist/types/plugin.js +1 -0
- package/dist/utils/updateFindObject.d.ts +2 -2
- package/dist/utils/updateFindObject.js +65 -11
- package/dist/version.js +1 -1
- package/package.json +2 -2
package/dist/db/valthera.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { Collection } from "../helpers/collection.js";
|
|
|
4
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 { ValtheraPlugin } from "../types/plugin.js";
|
|
7
8
|
import { VQuery, VQueryT } from "../types/query.js";
|
|
8
9
|
import { ValtheraCompatible } from "../types/valthera.js";
|
|
9
10
|
/**
|
|
@@ -39,6 +40,8 @@ export declare class ValtheraClass implements ValtheraCompatible {
|
|
|
39
40
|
"*": (name: keyof ValtheraCompatible, query: VQuery, result: any) => void;
|
|
40
41
|
}>;
|
|
41
42
|
version: string;
|
|
43
|
+
_plugins: ValtheraPlugin[];
|
|
44
|
+
plugin(p: ValtheraPlugin): () => void;
|
|
42
45
|
/** @deprecated use `adapter` */
|
|
43
46
|
get dbAction(): ActionsBase;
|
|
44
47
|
/** @deprecated use `adapter` */
|
package/dist/db/valthera.js
CHANGED
|
@@ -14,6 +14,16 @@ export class ValtheraClass {
|
|
|
14
14
|
/** @deprecated typo */
|
|
15
15
|
emiter = this.emitter;
|
|
16
16
|
version = version;
|
|
17
|
+
_plugins = [];
|
|
18
|
+
plugin(p) {
|
|
19
|
+
p.init?.(this);
|
|
20
|
+
this._plugins.push(p);
|
|
21
|
+
return () => {
|
|
22
|
+
const i = this._plugins.indexOf(p);
|
|
23
|
+
if (i !== -1)
|
|
24
|
+
this._plugins.splice(i, 1);
|
|
25
|
+
};
|
|
26
|
+
}
|
|
17
27
|
/** @deprecated use `adapter` */
|
|
18
28
|
get dbAction() {
|
|
19
29
|
return this.adapter;
|
|
@@ -63,9 +73,27 @@ export class ValtheraClass {
|
|
|
63
73
|
}
|
|
64
74
|
async execute(name, query) {
|
|
65
75
|
await this.init();
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
76
|
+
const plugins = this._plugins;
|
|
77
|
+
const self = this;
|
|
78
|
+
const exe = () => self.executor.addOp(self.adapter[name].bind(self.adapter), query, typeof query === "string" ? query : query.collection);
|
|
79
|
+
if (plugins.length === 0) {
|
|
80
|
+
const result = await exe();
|
|
81
|
+
this.emitter.emit(name, query, result);
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
let q = query;
|
|
85
|
+
let idx = 0;
|
|
86
|
+
const op = name;
|
|
87
|
+
const next = async (modifiedQuery) => {
|
|
88
|
+
if (modifiedQuery !== undefined)
|
|
89
|
+
q = modifiedQuery;
|
|
90
|
+
if (idx < plugins.length)
|
|
91
|
+
return plugins[idx++].execute(op, q, next);
|
|
92
|
+
return exe();
|
|
93
|
+
};
|
|
94
|
+
const r = await next();
|
|
95
|
+
this.emitter.emit(name, q, r);
|
|
96
|
+
return r;
|
|
69
97
|
}
|
|
70
98
|
/**
|
|
71
99
|
* Create a new instance of a Collection class.
|
package/dist/index.d.ts
CHANGED
|
@@ -7,5 +7,6 @@ export { Relation } from "./helpers/relation.js";
|
|
|
7
7
|
export { convertIdToUnix, sortByIds } from "./utils/id.js";
|
|
8
8
|
export type { ValtheraTypes } from "./types/export.js";
|
|
9
9
|
export type { Id } from "./types/Id.js";
|
|
10
|
+
export type { ValtheraPlugin } from "./types/plugin.js";
|
|
10
11
|
export type { RelationTypes } from "./types/relation.js";
|
|
11
12
|
export type { ValtheraCompatible } from "./types/valthera.js";
|
package/dist/types/options.d.ts
CHANGED
|
@@ -15,6 +15,7 @@ export type DbOpts = AdapterOption & {
|
|
|
15
15
|
executor?: Executor;
|
|
16
16
|
numberId?: boolean;
|
|
17
17
|
};
|
|
18
|
+
export type FieldPath<T = any> = KeysMatching<T, any> | (string & {}) | string[];
|
|
18
19
|
export interface DbFindOpts<T = any> {
|
|
19
20
|
reverse?: boolean;
|
|
20
21
|
limit?: number;
|
|
@@ -28,8 +29,8 @@ export interface DbFindOpts<T = any> {
|
|
|
28
29
|
count?: Record<string, string>;
|
|
29
30
|
}
|
|
30
31
|
export interface FindOpts<T = any> {
|
|
31
|
-
select?:
|
|
32
|
-
exclude?:
|
|
32
|
+
select?: FieldPath<T>[];
|
|
33
|
+
exclude?: FieldPath<T>[];
|
|
33
34
|
transform?: Function;
|
|
34
35
|
}
|
|
35
36
|
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -4,8 +4,8 @@ import { FindOpts } from "../types/options.js";
|
|
|
4
4
|
* @param obj - The object to update.
|
|
5
5
|
* @param findOpts - An object containing options to update the target object.
|
|
6
6
|
* @param [findOpts.transform] - A function to transform the object before applying the other options.
|
|
7
|
-
* @param [findOpts.select] - An array of fields to select from the target object.
|
|
8
|
-
* @param [findOpts.exclude] - An array of fields to exclude from the target object.
|
|
7
|
+
* @param [findOpts.select] - An array of fields to select from the target object. Supports dotted paths ("a.b.c") and array paths (["a","b","c"]).
|
|
8
|
+
* @param [findOpts.exclude] - An array of fields to exclude from the target object. Supports dotted paths and array paths.
|
|
9
9
|
* @returns The updated object.
|
|
10
10
|
*/
|
|
11
11
|
export declare function updateFindObject(obj: Object, findOpts: FindOpts): Object;
|
|
@@ -1,10 +1,61 @@
|
|
|
1
|
+
function resolvePath(path) {
|
|
2
|
+
return Array.isArray(path) ? path : path.split(".");
|
|
3
|
+
}
|
|
4
|
+
function pathExists(obj, path) {
|
|
5
|
+
const segs = resolvePath(path);
|
|
6
|
+
if (segs.length === 0)
|
|
7
|
+
return false;
|
|
8
|
+
let cur = obj;
|
|
9
|
+
for (const s of segs) {
|
|
10
|
+
if (cur == null || typeof cur !== "object" || !(s in cur))
|
|
11
|
+
return false;
|
|
12
|
+
cur = cur[s];
|
|
13
|
+
}
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
function getValueAt(obj, path) {
|
|
17
|
+
const segs = resolvePath(path);
|
|
18
|
+
let cur = obj;
|
|
19
|
+
for (const s of segs) {
|
|
20
|
+
if (cur == null || typeof cur !== "object")
|
|
21
|
+
return undefined;
|
|
22
|
+
cur = cur[s];
|
|
23
|
+
}
|
|
24
|
+
return cur;
|
|
25
|
+
}
|
|
26
|
+
function setNested(obj, path, value) {
|
|
27
|
+
const segs = resolvePath(path);
|
|
28
|
+
let cur = obj;
|
|
29
|
+
for (let i = 0; i < segs.length - 1; i++) {
|
|
30
|
+
const s = segs[i];
|
|
31
|
+
if (!(s in cur) || typeof cur[s] !== "object" || Array.isArray(cur[s])) {
|
|
32
|
+
cur[s] = {};
|
|
33
|
+
}
|
|
34
|
+
cur = cur[s];
|
|
35
|
+
}
|
|
36
|
+
cur[segs[segs.length - 1]] = value;
|
|
37
|
+
}
|
|
38
|
+
function deleteNested(obj, path) {
|
|
39
|
+
const segs = resolvePath(path);
|
|
40
|
+
if (segs.length === 0)
|
|
41
|
+
return;
|
|
42
|
+
let cur = obj;
|
|
43
|
+
for (let i = 0; i < segs.length - 1; i++) {
|
|
44
|
+
if (cur == null || typeof cur !== "object")
|
|
45
|
+
return;
|
|
46
|
+
cur = cur[segs[i]];
|
|
47
|
+
}
|
|
48
|
+
if (cur != null && typeof cur === "object") {
|
|
49
|
+
delete cur[segs[segs.length - 1]];
|
|
50
|
+
}
|
|
51
|
+
}
|
|
1
52
|
/**
|
|
2
53
|
* Updates an object with new values from a findOpts object.
|
|
3
54
|
* @param obj - The object to update.
|
|
4
55
|
* @param findOpts - An object containing options to update the target object.
|
|
5
56
|
* @param [findOpts.transform] - A function to transform the object before applying the other options.
|
|
6
|
-
* @param [findOpts.select] - An array of fields to select from the target object.
|
|
7
|
-
* @param [findOpts.exclude] - An array of fields to exclude from the target object.
|
|
57
|
+
* @param [findOpts.select] - An array of fields to select from the target object. Supports dotted paths ("a.b.c") and array paths (["a","b","c"]).
|
|
58
|
+
* @param [findOpts.exclude] - An array of fields to exclude from the target object. Supports dotted paths and array paths.
|
|
8
59
|
* @returns The updated object.
|
|
9
60
|
*/
|
|
10
61
|
export function updateFindObject(obj, findOpts) {
|
|
@@ -12,17 +63,20 @@ export function updateFindObject(obj, findOpts) {
|
|
|
12
63
|
if (typeof transform === "function")
|
|
13
64
|
obj = transform(obj);
|
|
14
65
|
if (Array.isArray(exclude)) {
|
|
15
|
-
|
|
16
|
-
if (
|
|
17
|
-
|
|
18
|
-
|
|
66
|
+
for (const field of exclude) {
|
|
67
|
+
if (typeof field === "string" || Array.isArray(field)) {
|
|
68
|
+
deleteNested(obj, field);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
19
71
|
}
|
|
20
72
|
if (Array.isArray(select)) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
73
|
+
const result = {};
|
|
74
|
+
for (const field of select) {
|
|
75
|
+
if ((typeof field === "string" || Array.isArray(field)) && pathExists(obj, field)) {
|
|
76
|
+
setNested(result, field, getValueAt(obj, field));
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
obj = result;
|
|
26
80
|
}
|
|
27
81
|
return obj;
|
|
28
82
|
}
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = "0.11.
|
|
1
|
+
export const version = "0.11.5-alpha.1";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wxn0brp/db-core",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.5-alpha.1",
|
|
4
4
|
"types": "dist/index.d.ts",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"author": "wxn0brP",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"@types/bun": "*",
|
|
20
20
|
"@types/node": "*",
|
|
21
21
|
"tsc-alias": "^1",
|
|
22
|
-
"typescript": "^
|
|
22
|
+
"typescript": "^7"
|
|
23
23
|
},
|
|
24
24
|
"files": [
|
|
25
25
|
"dist"
|