@wxn0brp/db 0.4.2 → 0.5.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/database.d.ts +1 -1
- package/dist/database.js +1 -1
- package/dist/index.d.ts +15 -8
- package/dist/index.js +2 -2
- package/dist/memory.d.ts +4 -0
- package/dist/memory.js +9 -0
- package/dist/relation.d.ts +23 -27
- package/dist/relation.js +70 -52
- package/package.json +1 -1
package/dist/database.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ import Data from "./types/data.js";
|
|
|
7
7
|
import { Context } from "./types/types.js";
|
|
8
8
|
import FileCpu from "./types/fileCpu.js";
|
|
9
9
|
import { Transaction } from "./types/transactions.js";
|
|
10
|
-
import EventEmitter from "events";
|
|
10
|
+
import { EventEmitter } from "events";
|
|
11
11
|
/**
|
|
12
12
|
* Represents a database management class for performing CRUD operations.
|
|
13
13
|
* @class
|
package/dist/database.js
CHANGED
|
@@ -2,7 +2,7 @@ import dbActionC from "./action.js";
|
|
|
2
2
|
import executorC from "./executor.js";
|
|
3
3
|
import CollectionManager from "./CollectionManager.js";
|
|
4
4
|
import vFileCpu from "./file/index.js";
|
|
5
|
-
import EventEmitter from "events";
|
|
5
|
+
import { EventEmitter } from "events";
|
|
6
6
|
/**
|
|
7
7
|
* Represents a database management class for performing CRUD operations.
|
|
8
8
|
* @class
|
package/dist/index.d.ts
CHANGED
|
@@ -5,11 +5,18 @@ import GraphRemote from "./client/graph.js";
|
|
|
5
5
|
import genId from "./gen.js";
|
|
6
6
|
import Relation from "./relation.js";
|
|
7
7
|
import CustomFileCpu from "./file/customFileCpu.js";
|
|
8
|
-
import ValtheraMemory from "./memory.js";
|
|
9
|
-
export { DataBase
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
8
|
+
import ValtheraMemory, { createMemoryValthera } from "./memory.js";
|
|
9
|
+
export { DataBase as Valthera, Graph, DataBaseRemote as ValtheraRemote, GraphRemote, Relation, genId, CustomFileCpu, ValtheraMemory, createMemoryValthera };
|
|
10
|
+
export type Id = import("./types/Id.js").Id;
|
|
11
|
+
export declare namespace ValtheraTypes {
|
|
12
|
+
type Arg = import("./types/arg.js").Arg;
|
|
13
|
+
type Search = import("./types/arg.js").Search;
|
|
14
|
+
type Updater = import("./types/arg.js").Updater;
|
|
15
|
+
type DbFindOpts = import("./types/options.js").DbFindOpts;
|
|
16
|
+
type FindOpts = import("./types/options.js").FindOpts;
|
|
17
|
+
type DbOpts = import("./types/options.js").DbOpts;
|
|
18
|
+
type Data = import("./types/data.js").Data;
|
|
19
|
+
type SearchOptions = import("./types/searchOpts.js").SearchOptions;
|
|
20
|
+
}
|
|
21
|
+
import type { RelationTypes } from "./relation.js";
|
|
22
|
+
export type { RelationTypes };
|
package/dist/index.js
CHANGED
|
@@ -5,5 +5,5 @@ import GraphRemote from "./client/graph.js";
|
|
|
5
5
|
import genId from "./gen.js";
|
|
6
6
|
import Relation from "./relation.js";
|
|
7
7
|
import CustomFileCpu from "./file/customFileCpu.js";
|
|
8
|
-
import ValtheraMemory from "./memory.js";
|
|
9
|
-
export { DataBase
|
|
8
|
+
import ValtheraMemory, { createMemoryValthera } from "./memory.js";
|
|
9
|
+
export { DataBase as Valthera, Graph, DataBaseRemote as ValtheraRemote, GraphRemote, Relation, genId, CustomFileCpu, ValtheraMemory, createMemoryValthera };
|
package/dist/memory.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import DataBase from "./database.js";
|
|
2
|
+
import Data from "./types/data.js";
|
|
2
3
|
export default class ValtheraMemory extends DataBase {
|
|
3
4
|
constructor(...args: any[]);
|
|
4
5
|
}
|
|
6
|
+
export declare function createMemoryValthera(data?: {
|
|
7
|
+
[key: string]: Data[];
|
|
8
|
+
}): ValtheraMemory;
|
package/dist/memory.js
CHANGED
|
@@ -127,3 +127,12 @@ export default class ValtheraMemory extends DataBase {
|
|
|
127
127
|
super("", { dbAction: new MemoryAction() });
|
|
128
128
|
}
|
|
129
129
|
}
|
|
130
|
+
export function createMemoryValthera(data) {
|
|
131
|
+
const db = new ValtheraMemory();
|
|
132
|
+
if (!data)
|
|
133
|
+
return db;
|
|
134
|
+
for (const collection of Object.keys(data)) {
|
|
135
|
+
db.dbAction.memory.set(collection, data[collection]);
|
|
136
|
+
}
|
|
137
|
+
return db;
|
|
138
|
+
}
|
package/dist/relation.d.ts
CHANGED
|
@@ -1,34 +1,30 @@
|
|
|
1
1
|
import DataBase from "./database.js";
|
|
2
2
|
import { Search } from "./types/arg.js";
|
|
3
3
|
import { DbFindOpts } from "./types/options.js";
|
|
4
|
-
export
|
|
5
|
-
[
|
|
6
|
-
|
|
7
|
-
interface
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
export declare namespace RelationTypes {
|
|
5
|
+
type Path = [string, string];
|
|
6
|
+
type FieldPath = string[];
|
|
7
|
+
interface DBS {
|
|
8
|
+
[key: string]: DataBase;
|
|
9
|
+
}
|
|
10
|
+
interface Relation {
|
|
11
|
+
[key: string]: RelationConfig;
|
|
12
|
+
}
|
|
13
|
+
interface RelationConfig {
|
|
14
|
+
path: Path;
|
|
15
|
+
pk?: string;
|
|
16
|
+
fk?: string;
|
|
17
|
+
as?: string;
|
|
18
|
+
select?: string[];
|
|
19
|
+
findOpts?: DbFindOpts;
|
|
20
|
+
type?: "1" | "1n" | "nm";
|
|
21
|
+
relations?: Relation;
|
|
22
|
+
}
|
|
13
23
|
}
|
|
14
24
|
declare class Relation {
|
|
15
|
-
|
|
16
|
-
constructor(
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
*/
|
|
20
|
-
private _resolvePath;
|
|
21
|
-
/**
|
|
22
|
-
* Processes relations for a single item.
|
|
23
|
-
*/
|
|
24
|
-
private _processItemRelations;
|
|
25
|
-
/**
|
|
26
|
-
* Finds multiple items with relations.
|
|
27
|
-
*/
|
|
28
|
-
find(path: string, search: Search, relations?: Record<string, RelationConfig>, options?: DbFindOpts): Promise<Record<string, any>[]>;
|
|
29
|
-
/**
|
|
30
|
-
* Finds a single item with relations.
|
|
31
|
-
*/
|
|
32
|
-
findOne(path: string, search: Search, relations?: Record<string, RelationConfig>): Promise<Record<string, any> | null>;
|
|
25
|
+
dbs: RelationTypes.DBS;
|
|
26
|
+
constructor(dbs: RelationTypes.DBS);
|
|
27
|
+
findOne(path: RelationTypes.Path, search: Search, relations: RelationTypes.Relation, select: RelationTypes.FieldPath[]): Promise<any>;
|
|
28
|
+
find(path: RelationTypes.Path, search: Search, relations: RelationTypes.Relation, select: RelationTypes.FieldPath[], findOpts?: DbFindOpts): Promise<any[]>;
|
|
33
29
|
}
|
|
34
30
|
export default Relation;
|
package/dist/relation.js
CHANGED
|
@@ -1,62 +1,80 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
throw new Error(`Invalid path format "${path}". Expected format 'dbName.collectionName'.`);
|
|
12
|
-
}
|
|
13
|
-
const sanitizedPath = path.replace(/\\\./g, "\uffff");
|
|
14
|
-
const [dbName, collectionName] = sanitizedPath.split(".", 2).map(part => part.replace(/\uffff/g, "."));
|
|
15
|
-
const db = this.databases[dbName];
|
|
16
|
-
if (!db) {
|
|
17
|
-
throw new Error(`Database "${dbName}" not found.`);
|
|
18
|
-
}
|
|
19
|
-
return { db, collection: collectionName };
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Processes relations for a single item.
|
|
23
|
-
*/
|
|
24
|
-
async _processItemRelations(item, relations) {
|
|
25
|
-
if (!item || typeof item !== "object")
|
|
26
|
-
return item;
|
|
27
|
-
const result = { ...item };
|
|
28
|
-
for (const [field, relationConfig] of Object.entries(relations)) {
|
|
29
|
-
if (!relationConfig.from || !relationConfig.localField || !relationConfig.foreignField) {
|
|
30
|
-
console.warn(`Skipping invalid relation configuration for field: "${field}"`);
|
|
1
|
+
async function processRelations(dbs, cfg, data) {
|
|
2
|
+
for (const [key, relation] of Object.entries(cfg)) {
|
|
3
|
+
const { pk = "_id", fk = "_id", type = "1" } = relation;
|
|
4
|
+
if (type === "1") {
|
|
5
|
+
const db = dbs[relation.path[0]];
|
|
6
|
+
const collection = relation.path[1];
|
|
7
|
+
const item = await db.findOne(collection, { [fk]: data[pk] }, {}, { select: relation.select || null });
|
|
8
|
+
const field = relation.as || key;
|
|
9
|
+
if (!item) {
|
|
10
|
+
data[field] = null;
|
|
31
11
|
continue;
|
|
32
12
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
13
|
+
if (relation.relations) {
|
|
14
|
+
await processRelations(dbs, relation.relations, item);
|
|
15
|
+
}
|
|
16
|
+
data[field] = item;
|
|
17
|
+
}
|
|
18
|
+
else if (type === "1n") {
|
|
19
|
+
const db = dbs[relation.path[0]];
|
|
20
|
+
const collection = relation.path[1];
|
|
21
|
+
const items = await db.find(collection, { [fk]: data[pk] }, {}, relation.findOpts || {}, { select: relation.select || null });
|
|
22
|
+
const field = relation.as || key;
|
|
23
|
+
if (relation.relations) {
|
|
24
|
+
await Promise.all(items.map(item => processRelations(dbs, relation.relations, item)));
|
|
38
25
|
}
|
|
39
|
-
|
|
40
|
-
|
|
26
|
+
data[field] = items;
|
|
27
|
+
}
|
|
28
|
+
else if (type === "nm") {
|
|
29
|
+
const db = dbs[relation.path[0]];
|
|
30
|
+
const collection = relation.path[1];
|
|
31
|
+
const items = await db.find(collection, {}, {}, {}, { select: relation.select || null });
|
|
32
|
+
const field = relation.as || key;
|
|
33
|
+
if (relation.relations) {
|
|
34
|
+
await Promise.all(items.map(item => processRelations(dbs, relation.relations, item)));
|
|
41
35
|
}
|
|
36
|
+
data[field] = items;
|
|
42
37
|
}
|
|
43
|
-
|
|
38
|
+
else {
|
|
39
|
+
throw new Error(`Unknown relation type: ${relation.type}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function selectDataSelf(data, select) {
|
|
44
|
+
if (!data)
|
|
45
|
+
return null;
|
|
46
|
+
if (select.length === 0)
|
|
47
|
+
return data;
|
|
48
|
+
if (Array.isArray(data))
|
|
49
|
+
return data.map(item => selectDataSelf(item, select));
|
|
50
|
+
return selectDataSelf(data[select[0]], select.slice(1));
|
|
51
|
+
}
|
|
52
|
+
function selectData(data, select) {
|
|
53
|
+
if (select.length === 0)
|
|
54
|
+
return data;
|
|
55
|
+
const newData = {};
|
|
56
|
+
for (const field of select) {
|
|
57
|
+
const key = field.map(f => f.replaceAll(".", "\\.")).join(".");
|
|
58
|
+
newData[key] = selectDataSelf(data, field);
|
|
59
|
+
}
|
|
60
|
+
return newData;
|
|
61
|
+
}
|
|
62
|
+
class Relation {
|
|
63
|
+
dbs;
|
|
64
|
+
constructor(dbs) {
|
|
65
|
+
this.dbs = dbs;
|
|
44
66
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const items = await db.find(collection, search, {}, options);
|
|
51
|
-
return Promise.all(items.map(item => this._processItemRelations(item, relations)));
|
|
67
|
+
async findOne(path, search, relations, select) {
|
|
68
|
+
const db = this.dbs[path[0]];
|
|
69
|
+
const data = await db.findOne(path[1], search);
|
|
70
|
+
await processRelations(this.dbs, relations, data);
|
|
71
|
+
return selectData(data, select);
|
|
52
72
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const item = await db.findOne(collection, search);
|
|
59
|
-
return item ? this._processItemRelations(item, relations) : null;
|
|
73
|
+
async find(path, search, relations, select, findOpts = {}) {
|
|
74
|
+
const db = this.dbs[path[0]];
|
|
75
|
+
const data = await db.find(path[1], search, {}, findOpts);
|
|
76
|
+
await Promise.all(data.map(item => processRelations(this.dbs, relations, item)));
|
|
77
|
+
return data.map(item => selectData(item, select));
|
|
60
78
|
}
|
|
61
79
|
}
|
|
62
80
|
export default Relation;
|
package/package.json
CHANGED