@wxn0brp/db-core 0.1.6 → 0.2.0
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 +5 -5
- package/dist/db/valthera.js +3 -3
- package/dist/helpers/CollectionManager.d.ts +4 -4
- package/dist/helpers/CollectionManager.js +6 -6
- package/dist/helpers/relation.d.ts +1 -1
- package/dist/helpers/relation.js +5 -5
- package/dist/types/valthera.d.ts +8 -3
- package/dist/version.js +1 -1
- package/package.json +4 -4
package/dist/db/valthera.d.ts
CHANGED
|
@@ -2,12 +2,12 @@ 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";
|
|
5
|
-
import {
|
|
5
|
+
import { Search, Updater } from "../types/arg.js";
|
|
6
6
|
import Data from "../types/data.js";
|
|
7
7
|
import { DbFindOpts, DbOpts, FindOpts } from "../types/options.js";
|
|
8
8
|
import { VQuery } from "../types/query.js";
|
|
9
9
|
import { VContext } from "../types/types.js";
|
|
10
|
-
import { ValtheraCompatible } from "../types/valthera.js";
|
|
10
|
+
import { UpdateOneOrAdd, ValtheraCompatible } from "../types/valthera.js";
|
|
11
11
|
type DbActionsFns = keyof {
|
|
12
12
|
[K in keyof ActionsBase as ActionsBase[K] extends (...args: any[]) => any ? K : never]: any;
|
|
13
13
|
};
|
|
@@ -49,11 +49,11 @@ declare class ValtheraClass implements ValtheraCompatible {
|
|
|
49
49
|
/**
|
|
50
50
|
* Find data in a database.
|
|
51
51
|
*/
|
|
52
|
-
find<T = Data>(collection: string, search?: Search<T>,
|
|
52
|
+
find<T = Data>(collection: string, search?: Search<T>, dbFindOpts?: DbFindOpts<T>, findOpts?: FindOpts<T>, context?: VContext): Promise<T[]>;
|
|
53
53
|
/**
|
|
54
54
|
* Find one data entry in a database.
|
|
55
55
|
*/
|
|
56
|
-
findOne<T = Data>(collection: string, search?: Search<T>,
|
|
56
|
+
findOne<T = Data>(collection: string, search?: Search<T>, findOpts?: FindOpts<T>, context?: VContext): Promise<T>;
|
|
57
57
|
/**
|
|
58
58
|
* Update data in a database.
|
|
59
59
|
*/
|
|
@@ -73,7 +73,7 @@ declare class ValtheraClass implements ValtheraCompatible {
|
|
|
73
73
|
/**
|
|
74
74
|
* Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
|
|
75
75
|
*/
|
|
76
|
-
updateOneOrAdd<T = Data>(collection: string, search: Search<T>, updater: Updater<T>, add_arg
|
|
76
|
+
updateOneOrAdd<T = Data>(collection: string, search: Search<T>, updater: Updater<T>, { add_arg, context, id_gen }: UpdateOneOrAdd<T>): Promise<boolean>;
|
|
77
77
|
/**
|
|
78
78
|
* Removes a database collection from the file system.
|
|
79
79
|
*/
|
package/dist/db/valthera.js
CHANGED
|
@@ -66,13 +66,13 @@ class ValtheraClass {
|
|
|
66
66
|
/**
|
|
67
67
|
* Find data in a database.
|
|
68
68
|
*/
|
|
69
|
-
async find(collection, search = {},
|
|
69
|
+
async find(collection, search = {}, dbFindOpts = {}, findOpts = {}, context = {}) {
|
|
70
70
|
return await this.execute("find", { collection, search, context, dbFindOpts, findOpts });
|
|
71
71
|
}
|
|
72
72
|
/**
|
|
73
73
|
* Find one data entry in a database.
|
|
74
74
|
*/
|
|
75
|
-
async findOne(collection, search = {},
|
|
75
|
+
async findOne(collection, search = {}, findOpts = {}, context = {}) {
|
|
76
76
|
return await this.execute("findOne", { collection, search, context, findOpts });
|
|
77
77
|
}
|
|
78
78
|
/**
|
|
@@ -102,7 +102,7 @@ class ValtheraClass {
|
|
|
102
102
|
/**
|
|
103
103
|
* Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
|
|
104
104
|
*/
|
|
105
|
-
async updateOneOrAdd(collection, search, updater, add_arg = {}, context = {}, id_gen = true) {
|
|
105
|
+
async updateOneOrAdd(collection, search, updater, { add_arg = {}, context = {}, id_gen = true }) {
|
|
106
106
|
return await this.execute("updateOneOrAdd", { collection, search, updater, add_arg, context, id_gen });
|
|
107
107
|
}
|
|
108
108
|
/**
|
|
@@ -2,7 +2,7 @@ import { Arg, Search, Updater } from "../types/arg.js";
|
|
|
2
2
|
import { DbFindOpts, FindOpts } from "../types/options.js";
|
|
3
3
|
import { VContext } from "../types/types.js";
|
|
4
4
|
import Data from "../types/data.js";
|
|
5
|
-
import { ValtheraCompatible } from "../types/valthera.js";
|
|
5
|
+
import { UpdateOneOrAdd, ValtheraCompatible } from "../types/valthera.js";
|
|
6
6
|
declare class CollectionManager<D = Data> {
|
|
7
7
|
private db;
|
|
8
8
|
private collection;
|
|
@@ -14,11 +14,11 @@ declare class CollectionManager<D = Data> {
|
|
|
14
14
|
/**
|
|
15
15
|
* Find data in a database.
|
|
16
16
|
*/
|
|
17
|
-
find<T = Data>(search?: Search<T & D>,
|
|
17
|
+
find<T = Data>(search?: Search<T & D>, options?: DbFindOpts<T & Data>, findOpts?: FindOpts<T & Data>, context?: VContext): Promise<T[]>;
|
|
18
18
|
/**
|
|
19
19
|
* Find one data entry in a database.
|
|
20
20
|
*/
|
|
21
|
-
findOne<T = Data>(search?: Search<T & Data>,
|
|
21
|
+
findOne<T = Data>(search?: Search<T & Data>, findOpts?: FindOpts<T & Data>, context?: VContext): Promise<T>;
|
|
22
22
|
/**
|
|
23
23
|
* Update data in a database.
|
|
24
24
|
*/
|
|
@@ -38,6 +38,6 @@ declare class CollectionManager<D = Data> {
|
|
|
38
38
|
/**
|
|
39
39
|
* Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
|
|
40
40
|
*/
|
|
41
|
-
updateOneOrAdd<T = Data>(search: Search<T & Data>, updater: Updater<T & Data>, add_arg
|
|
41
|
+
updateOneOrAdd<T = Data>(search: Search<T & Data>, updater: Updater<T & Data>, { add_arg, context, id_gen }: UpdateOneOrAdd<T & Data>): Promise<boolean>;
|
|
42
42
|
}
|
|
43
43
|
export default CollectionManager;
|
|
@@ -14,14 +14,14 @@ class CollectionManager {
|
|
|
14
14
|
/**
|
|
15
15
|
* Find data in a database.
|
|
16
16
|
*/
|
|
17
|
-
async find(search = {},
|
|
18
|
-
return await this.db.find(this.collection, search,
|
|
17
|
+
async find(search = {}, options = {}, findOpts = {}, context = {}) {
|
|
18
|
+
return await this.db.find(this.collection, search, options, findOpts, context);
|
|
19
19
|
}
|
|
20
20
|
/**
|
|
21
21
|
* Find one data entry in a database.
|
|
22
22
|
*/
|
|
23
|
-
async findOne(search = {},
|
|
24
|
-
return await this.db.findOne(this.collection, search,
|
|
23
|
+
async findOne(search = {}, findOpts = {}, context = {}) {
|
|
24
|
+
return await this.db.findOne(this.collection, search, findOpts, context);
|
|
25
25
|
}
|
|
26
26
|
/**
|
|
27
27
|
* Update data in a database.
|
|
@@ -50,8 +50,8 @@ class CollectionManager {
|
|
|
50
50
|
/**
|
|
51
51
|
* Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
|
|
52
52
|
*/
|
|
53
|
-
async updateOneOrAdd(search, updater, add_arg = {}, context = {}, id_gen = true) {
|
|
54
|
-
return await this.db.updateOneOrAdd(this.collection, search, updater, add_arg, context, id_gen);
|
|
53
|
+
async updateOneOrAdd(search, updater, { add_arg = {}, context = {}, id_gen = true }) {
|
|
54
|
+
return await this.db.updateOneOrAdd(this.collection, search, updater, { add_arg, context, id_gen });
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
export default CollectionManager;
|
|
@@ -5,6 +5,6 @@ declare class Relation {
|
|
|
5
5
|
dbs: RelationTypes.DBS;
|
|
6
6
|
constructor(dbs: RelationTypes.DBS);
|
|
7
7
|
findOne(path: RelationTypes.Path, search: Search, relations: RelationTypes.Relation, select?: string[][] | Record<string, any>): Promise<any>;
|
|
8
|
-
find(path: RelationTypes.Path, search: Search, relations: RelationTypes.Relation, select?: string[][] | Record<string, any>,
|
|
8
|
+
find(path: RelationTypes.Path, search: Search, relations: RelationTypes.Relation, select?: string[][] | Record<string, any>, dbFindOpts?: DbFindOpts): Promise<any[]>;
|
|
9
9
|
}
|
|
10
10
|
export default Relation;
|
package/dist/helpers/relation.js
CHANGED
|
@@ -32,7 +32,7 @@ function convertSearchObjToSearchArray(obj, parentKeys = []) {
|
|
|
32
32
|
if (!value) {
|
|
33
33
|
return acc;
|
|
34
34
|
}
|
|
35
|
-
else if (typeof value ===
|
|
35
|
+
else if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
36
36
|
return [...acc, ...convertSearchObjToSearchArray(value, currentPath)];
|
|
37
37
|
}
|
|
38
38
|
else {
|
|
@@ -87,7 +87,7 @@ async function processRelations(dbs, cfg, data, parentList = null) {
|
|
|
87
87
|
else if (type === "1n") {
|
|
88
88
|
const ids = targets.map(i => i[pk]);
|
|
89
89
|
const [selectSafe, deleteSelect] = autoSelect(rel, fk);
|
|
90
|
-
const results = await db.find(coll, { $in: { [fk]: ids } },
|
|
90
|
+
const results = await db.find(coll, { $in: { [fk]: ids } }, findOpts || {}, { select: selectSafe });
|
|
91
91
|
const grouped = results.reduce((acc, row) => {
|
|
92
92
|
const id = row[fk];
|
|
93
93
|
(acc[id] ||= []).push(row);
|
|
@@ -105,7 +105,7 @@ async function processRelations(dbs, cfg, data, parentList = null) {
|
|
|
105
105
|
}
|
|
106
106
|
else if (type === "nm") {
|
|
107
107
|
if (!through || !through.table || !through.pk || !through.fk) {
|
|
108
|
-
throw new Error(`Relation type "nm" requires a defined
|
|
108
|
+
throw new Error(`Relation type "nm" requires a defined "through" in "${key}"`);
|
|
109
109
|
}
|
|
110
110
|
for (const item of targets) {
|
|
111
111
|
const pivotDb = dbs[through.db || dbKey];
|
|
@@ -140,10 +140,10 @@ class Relation {
|
|
|
140
140
|
await processRelations(this.dbs, relations, data);
|
|
141
141
|
return select ? pickByPath(data, select) : data;
|
|
142
142
|
}
|
|
143
|
-
async find(path, search, relations, select,
|
|
143
|
+
async find(path, search, relations, select, dbFindOpts = {}) {
|
|
144
144
|
const [dbKey, coll] = path;
|
|
145
145
|
const db = this.dbs[dbKey];
|
|
146
|
-
const data = await db.find(coll, search,
|
|
146
|
+
const data = await db.find(coll, search, dbFindOpts);
|
|
147
147
|
if (relations)
|
|
148
148
|
await processRelations(this.dbs, relations, null, data);
|
|
149
149
|
if (typeof select === "object" && !Array.isArray(select)) {
|
package/dist/types/valthera.d.ts
CHANGED
|
@@ -10,14 +10,19 @@ export interface ValtheraCompatible {
|
|
|
10
10
|
ensureCollection(collection: string): Promise<boolean>;
|
|
11
11
|
issetCollection(collection: string): Promise<boolean>;
|
|
12
12
|
add<T = Data>(collection: string, data: Arg<T>, id_gen?: boolean): Promise<T>;
|
|
13
|
-
find<T = Data>(collection: string, search
|
|
14
|
-
findOne<T = Data>(collection: string, search
|
|
13
|
+
find<T = Data>(collection: string, search?: Search<T>, options?: DbFindOpts<T>, findOpts?: FindOpts<T>, context?: VContext): Promise<T[]>;
|
|
14
|
+
findOne<T = Data>(collection: string, search?: Search<T>, findOpts?: FindOpts<T>, context?: VContext): Promise<T | null>;
|
|
15
15
|
update<T = Data>(collection: string, search: Search<T>, updater: Updater<T>, context?: VContext): Promise<boolean>;
|
|
16
16
|
updateOne<T = Data>(collection: string, search: Search<T>, updater: Updater<T>, context?: VContext): Promise<boolean>;
|
|
17
17
|
remove<T = Data>(collection: string, search: Search<T>, context?: VContext): Promise<boolean>;
|
|
18
18
|
removeOne<T = Data>(collection: string, search: Search<T>, context?: VContext): Promise<boolean>;
|
|
19
19
|
removeCollection(collection: string): Promise<boolean>;
|
|
20
|
-
updateOneOrAdd<T = Data>(collection: string, search: Search<T>, updater: Updater<T>,
|
|
20
|
+
updateOneOrAdd<T = Data>(collection: string, search: Search<T>, updater: Updater<T>, opts: UpdateOneOrAdd<T>): Promise<boolean>;
|
|
21
|
+
}
|
|
22
|
+
export interface UpdateOneOrAdd<T> {
|
|
23
|
+
add_arg?: Arg<T>;
|
|
24
|
+
id_gen?: boolean;
|
|
25
|
+
context?: VContext;
|
|
21
26
|
}
|
|
22
27
|
export interface ValtheraCompatibleInternal {
|
|
23
28
|
c(config: VQuery): CollectionManager;
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = "0.
|
|
1
|
+
export const version = "0.2.0";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wxn0brp/db-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"types": "dist/index.d.ts",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"author": "wxn0brP",
|
|
@@ -12,6 +12,9 @@
|
|
|
12
12
|
"url": "https://github.com/wxn0brP/ValtheraDB-core.git"
|
|
13
13
|
},
|
|
14
14
|
"homepage": "https://github.com/wxn0brP/ValtheraDB",
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@wxn0brp/event-emitter": "^0.0.2"
|
|
17
|
+
},
|
|
15
18
|
"devDependencies": {
|
|
16
19
|
"@types/node": "^24.2.0",
|
|
17
20
|
"tsc-alias": "^1.8.10",
|
|
@@ -31,8 +34,5 @@
|
|
|
31
34
|
"import": "./dist/*.js",
|
|
32
35
|
"default": "./dist/*.js"
|
|
33
36
|
}
|
|
34
|
-
},
|
|
35
|
-
"dependencies": {
|
|
36
|
-
"@wxn0brp/event-emitter": "^0.0.2"
|
|
37
37
|
}
|
|
38
38
|
}
|