@wxn0brp/db 0.0.4 → 0.0.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 +3 -0
- package/dist/cjs/CollectionManager.d.ts +43 -0
- package/dist/cjs/CollectionManager.js +59 -0
- package/dist/cjs/action.d.ts +71 -0
- package/dist/cjs/action.js +199 -0
- package/dist/cjs/database.d.ts +77 -0
- package/dist/cjs/database.js +118 -0
- package/dist/cjs/executor.d.ts +28 -0
- package/{executor.js → dist/cjs/executor.js} +47 -54
- package/dist/cjs/file/find.d.ts +11 -0
- package/dist/cjs/file/find.js +80 -0
- package/dist/cjs/file/index.d.ts +3 -0
- package/dist/cjs/file/index.js +25 -0
- package/dist/cjs/file/remove.d.ts +7 -0
- package/dist/cjs/file/remove.js +61 -0
- package/dist/cjs/file/update.d.ts +7 -0
- package/dist/cjs/file/update.js +68 -0
- package/dist/cjs/file/utils.d.ts +8 -0
- package/dist/cjs/file/utils.js +23 -0
- package/dist/cjs/format.d.ts +18 -0
- package/dist/cjs/format.js +36 -0
- package/dist/cjs/gen.d.ts +5 -0
- package/dist/cjs/gen.js +78 -0
- package/dist/cjs/graph.d.ts +47 -0
- package/dist/cjs/graph.js +90 -0
- package/{index.d.ts → dist/cjs/index.d.ts} +2 -2
- package/dist/cjs/index.js +18 -0
- package/dist/cjs/relation.d.ts +64 -0
- package/{relation.js → dist/cjs/relation.js} +26 -45
- package/dist/cjs/remote/client/database.d.ts +71 -0
- package/dist/cjs/remote/client/database.js +145 -0
- package/dist/cjs/remote/client/function.d.ts +5 -0
- package/dist/cjs/remote/client/function.js +32 -0
- package/dist/cjs/remote/client/graph.d.ts +54 -0
- package/dist/cjs/remote/client/graph.js +93 -0
- package/dist/cjs/remote/client/remote.d.ts +16 -0
- package/dist/cjs/remote/client/remote.js +2 -0
- package/dist/cjs/remote/server/auth.d.ts +31 -0
- package/dist/cjs/remote/server/auth.js +99 -0
- package/dist/cjs/remote/server/db.d.ts +2 -0
- package/dist/cjs/remote/server/db.js +223 -0
- package/dist/cjs/remote/server/function.d.ts +2 -0
- package/dist/cjs/remote/server/function.js +89 -0
- package/dist/cjs/remote/server/graph.d.ts +2 -0
- package/dist/cjs/remote/server/graph.js +145 -0
- package/dist/cjs/remote/server/index.d.ts +1 -0
- package/dist/cjs/remote/server/index.js +66 -0
- package/dist/cjs/remote/server/initDataBases.d.ts +1 -0
- package/dist/cjs/remote/server/initDataBases.js +25 -0
- package/dist/cjs/remote/server/pathUtils.d.ts +1 -0
- package/dist/cjs/remote/server/pathUtils.js +12 -0
- package/dist/cjs/remote/server/secret.d.ts +1 -0
- package/dist/cjs/remote/server/secret.js +25 -0
- package/dist/cjs/remote/serverMgmt/index.d.ts +1 -0
- package/dist/cjs/remote/serverMgmt/index.js +87 -0
- package/dist/cjs/types/Id.d.ts +3 -0
- package/dist/cjs/types/Id.js +2 -0
- package/dist/cjs/types/arg.d.ts +6 -0
- package/dist/cjs/types/arg.js +2 -0
- package/dist/cjs/types/data.d.ts +4 -0
- package/dist/cjs/types/data.js +2 -0
- package/dist/cjs/types/options.d.ts +12 -0
- package/dist/cjs/types/options.js +2 -0
- package/dist/cjs/types/searchOpts.d.ts +61 -0
- package/dist/cjs/types/searchOpts.js +8 -0
- package/dist/cjs/types/types.d.ts +6 -0
- package/dist/cjs/types/types.js +2 -0
- package/dist/cjs/utils/hasFields.d.ts +8 -0
- package/dist/cjs/utils/hasFields.js +22 -0
- package/dist/cjs/utils/hasFieldsAdvanced.d.ts +5 -0
- package/dist/cjs/utils/hasFieldsAdvanced.js +182 -0
- package/dist/cjs/utils/updateFindObject.d.ts +11 -0
- package/dist/cjs/utils/updateFindObject.js +32 -0
- package/dist/cjs/utils/updateObject.d.ts +8 -0
- package/dist/cjs/utils/updateObject.js +18 -0
- package/dist/esm/CollectionManager.d.ts +43 -0
- package/dist/esm/CollectionManager.js +57 -0
- package/dist/esm/action.d.ts +71 -0
- package/dist/esm/action.js +194 -0
- package/dist/esm/database.d.ts +77 -0
- package/dist/esm/database.js +113 -0
- package/dist/esm/executor.d.ts +28 -0
- package/dist/esm/executor.js +45 -0
- package/dist/esm/file/find.d.ts +11 -0
- package/dist/esm/file/find.js +73 -0
- package/dist/esm/file/index.d.ts +3 -0
- package/{file → dist/esm/file}/index.js +1 -1
- package/dist/esm/file/remove.d.ts +7 -0
- package/dist/esm/file/remove.js +56 -0
- package/dist/esm/file/update.d.ts +7 -0
- package/dist/esm/file/update.js +63 -0
- package/dist/esm/file/utils.d.ts +8 -0
- package/{file → dist/esm/file}/utils.js +3 -11
- package/dist/esm/format.d.ts +18 -0
- package/{format.js → dist/esm/format.js} +29 -29
- package/dist/esm/gen.d.ts +5 -0
- package/dist/esm/gen.js +75 -0
- package/dist/esm/graph.d.ts +47 -0
- package/dist/esm/graph.js +85 -0
- package/dist/esm/index.d.ts +7 -0
- package/{index.js → dist/esm/index.js} +1 -9
- package/dist/esm/relation.d.ts +64 -0
- package/dist/esm/relation.js +65 -0
- package/dist/esm/remote/client/database.d.ts +71 -0
- package/dist/esm/remote/client/database.js +140 -0
- package/dist/esm/remote/client/function.d.ts +5 -0
- package/dist/esm/remote/client/function.js +30 -0
- package/dist/esm/remote/client/graph.d.ts +54 -0
- package/dist/esm/remote/client/graph.js +88 -0
- package/dist/esm/remote/client/remote.d.ts +16 -0
- package/dist/esm/remote/client/remote.js +1 -0
- package/dist/esm/remote/server/auth.d.ts +31 -0
- package/{remote → dist/esm/remote}/server/auth.js +31 -44
- package/dist/esm/remote/server/db.d.ts +2 -0
- package/dist/esm/remote/server/db.js +218 -0
- package/dist/esm/remote/server/function.d.ts +2 -0
- package/dist/esm/remote/server/function.js +87 -0
- package/dist/esm/remote/server/graph.d.ts +2 -0
- package/{remote → dist/esm/remote}/server/graph.js +62 -55
- package/dist/esm/remote/server/gui/css/main.css +130 -0
- package/dist/esm/remote/server/gui/css/scrool.css +81 -0
- package/dist/esm/remote/server/gui/css/style.css +61 -0
- package/dist/esm/remote/server/gui/favicon.svg +12 -0
- package/dist/esm/remote/server/gui/html/data.html +15 -0
- package/dist/esm/remote/server/gui/html/main.html +46 -0
- package/dist/esm/remote/server/gui/html/nav.html +25 -0
- package/dist/esm/remote/server/gui/html/popup.html +51 -0
- package/dist/esm/remote/server/gui/index.html +49 -0
- package/dist/esm/remote/server/gui/js/api.js +166 -0
- package/dist/esm/remote/server/gui/js/index.js +17 -0
- package/dist/esm/remote/server/gui/js/loadHTML.js +16 -0
- package/dist/esm/remote/server/gui/js/popUp.js +72 -0
- package/dist/esm/remote/server/gui/js/queryApi.js +51 -0
- package/dist/esm/remote/server/gui/js/queryDb.js +79 -0
- package/dist/esm/remote/server/gui/js/queryGraph.js +144 -0
- package/dist/esm/remote/server/gui/js/render.js +64 -0
- package/dist/esm/remote/server/gui/js/templates.js +31 -0
- package/dist/esm/remote/server/gui/js/utils.js +36 -0
- package/dist/esm/remote/server/gui/js/vars.js +9 -0
- package/dist/esm/remote/server/gui/libs/core.js +176 -0
- package/dist/esm/remote/server/gui/libs/d3.v7.min.js +2 -0
- package/dist/esm/remote/server/gui/libs/handlebars.min.js +29 -0
- package/dist/esm/remote/server/gui/libs/json5.min.js +1 -0
- package/dist/esm/remote/server/index.d.ts +1 -0
- package/dist/esm/remote/server/index.js +61 -0
- package/dist/esm/remote/server/initDataBases.d.ts +1 -0
- package/dist/esm/remote/server/initDataBases.js +20 -0
- package/dist/esm/remote/server/pathUtils.d.ts +1 -0
- package/{remote → dist/esm/remote}/server/pathUtils.js +2 -3
- package/dist/esm/remote/server/secret.d.ts +1 -0
- package/{remote → dist/esm/remote}/server/secret.js +3 -7
- package/dist/esm/remote/serverMgmt/index.d.ts +1 -0
- package/{remote → dist/esm/remote}/serverMgmt/index.js +27 -31
- package/dist/esm/types/Id.d.ts +3 -0
- package/dist/esm/types/Id.js +1 -0
- package/dist/esm/types/arg.d.ts +6 -0
- package/dist/esm/types/arg.js +1 -0
- package/dist/esm/types/data.d.ts +4 -0
- package/dist/esm/types/data.js +1 -0
- package/dist/esm/types/options.d.ts +12 -0
- package/dist/esm/types/options.js +1 -0
- package/dist/esm/types/searchOpts.d.ts +61 -0
- package/dist/esm/types/searchOpts.js +7 -0
- package/dist/esm/types/types.d.ts +6 -0
- package/dist/esm/types/types.js +1 -0
- package/dist/esm/utils/hasFields.d.ts +8 -0
- package/{utils → dist/esm/utils}/hasFields.js +4 -4
- package/dist/esm/utils/hasFieldsAdvanced.d.ts +5 -0
- package/dist/esm/utils/hasFieldsAdvanced.js +176 -0
- package/dist/esm/utils/updateFindObject.d.ts +11 -0
- package/{utils → dist/esm/utils}/updateFindObject.js +11 -16
- package/dist/esm/utils/updateObject.d.ts +8 -0
- package/{utils → dist/esm/utils}/updateObject.js +4 -4
- package/package.json +55 -36
- package/CollectionManager.js +0 -119
- package/action.js +0 -258
- package/database.d.ts +0 -44
- package/database.js +0 -203
- package/docs/database.md +0 -140
- package/docs/graph.md +0 -86
- package/docs/relation.md +0 -51
- package/docs/remote.md +0 -30
- package/docs/remote_server.md +0 -35
- package/docs/search_opts.md +0 -227
- package/file/find.js +0 -89
- package/file/remove.js +0 -74
- package/file/update.js +0 -83
- package/gen.d.ts +0 -1
- package/gen.js +0 -97
- package/graph.d.ts +0 -27
- package/graph.js +0 -140
- package/relation.d.ts +0 -23
- package/remote/client/database.d.ts +0 -41
- package/remote/client/database.js +0 -228
- package/remote/client/graph.d.ts +0 -31
- package/remote/client/graph.js +0 -148
- package/remote/server/db.js +0 -197
- package/remote/server/function.js +0 -43
- package/remote/server/index.js +0 -63
- package/remote/server/initDataBases.js +0 -20
- package/test/hasFieldsAdvanced.test.js +0 -70
- package/utils/hasFieldsAdvanced.js +0 -184
- /package/{remote → dist/cjs/remote}/server/gui/css/main.css +0 -0
- /package/{remote → dist/cjs/remote}/server/gui/css/scrool.css +0 -0
- /package/{remote → dist/cjs/remote}/server/gui/css/style.css +0 -0
- /package/{remote → dist/cjs/remote}/server/gui/favicon.svg +0 -0
- /package/{remote → dist/cjs/remote}/server/gui/html/data.html +0 -0
- /package/{remote → dist/cjs/remote}/server/gui/html/main.html +0 -0
- /package/{remote → dist/cjs/remote}/server/gui/html/nav.html +0 -0
- /package/{remote → dist/cjs/remote}/server/gui/html/popup.html +0 -0
- /package/{remote → dist/cjs/remote}/server/gui/index.html +0 -0
- /package/{remote → dist/cjs/remote}/server/gui/js/api.js +0 -0
- /package/{remote → dist/cjs/remote}/server/gui/js/index.js +0 -0
- /package/{remote → dist/cjs/remote}/server/gui/js/loadHTML.js +0 -0
- /package/{remote → dist/cjs/remote}/server/gui/js/popUp.js +0 -0
- /package/{remote → dist/cjs/remote}/server/gui/js/queryApi.js +0 -0
- /package/{remote → dist/cjs/remote}/server/gui/js/queryDb.js +0 -0
- /package/{remote → dist/cjs/remote}/server/gui/js/queryGraph.js +0 -0
- /package/{remote → dist/cjs/remote}/server/gui/js/render.js +0 -0
- /package/{remote → dist/cjs/remote}/server/gui/js/templates.js +0 -0
- /package/{remote → dist/cjs/remote}/server/gui/js/utils.js +0 -0
- /package/{remote → dist/cjs/remote}/server/gui/js/vars.js +0 -0
- /package/{remote → dist/cjs/remote}/server/gui/libs/core.js +0 -0
- /package/{remote → dist/cjs/remote}/server/gui/libs/d3.v7.min.js +0 -0
- /package/{remote → dist/cjs/remote}/server/gui/libs/handlebars.min.js +0 -0
- /package/{remote → dist/cjs/remote}/server/gui/libs/json5.min.js +0 -0
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const database_js_1 = __importDefault(require("./database.js"));
|
|
7
|
+
/**
|
|
8
|
+
* A class representing a graph database.
|
|
9
|
+
* @class
|
|
10
|
+
*/
|
|
11
|
+
class Graph {
|
|
12
|
+
db;
|
|
13
|
+
constructor(databaseFolder) {
|
|
14
|
+
this.db = new database_js_1.default(databaseFolder);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Adds an edge between two nodes.
|
|
18
|
+
*/
|
|
19
|
+
async add(collection, nodeA, nodeB) {
|
|
20
|
+
const sortedNodes = [nodeA, nodeB].sort();
|
|
21
|
+
return await this.db.add(collection, {
|
|
22
|
+
a: sortedNodes[0],
|
|
23
|
+
b: sortedNodes[1]
|
|
24
|
+
}, false);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Removes an edge between two nodes.
|
|
28
|
+
*/
|
|
29
|
+
async remove(collection, nodeA, nodeB) {
|
|
30
|
+
const sortedNodes = [nodeA, nodeB].sort();
|
|
31
|
+
const query = { a: sortedNodes[0], b: sortedNodes[1] };
|
|
32
|
+
return await this.db.removeOne(collection, query);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Finds all edges with either node equal to `node`.
|
|
36
|
+
*/
|
|
37
|
+
async find(collection, node) {
|
|
38
|
+
const edges = [];
|
|
39
|
+
const edgesByANode = await this.db.find(collection, { a: node });
|
|
40
|
+
const edgesByBNode = await this.db.find(collection, { b: node });
|
|
41
|
+
if (edgesByANode)
|
|
42
|
+
edges.push(...edgesByANode);
|
|
43
|
+
if (edgesByBNode)
|
|
44
|
+
edges.push(...edgesByBNode);
|
|
45
|
+
return edges;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Finds one edge with either node equal to `nodeA` and the other equal to `nodeB`.
|
|
49
|
+
*/
|
|
50
|
+
async findOne(collection, nodeA, nodeB) {
|
|
51
|
+
const edgeAB = await this.db.findOne(collection, { a: nodeA, b: nodeB });
|
|
52
|
+
if (edgeAB)
|
|
53
|
+
return edgeAB;
|
|
54
|
+
const edgeBA = await this.db.findOne(collection, { a: nodeB, b: nodeA });
|
|
55
|
+
if (edgeBA)
|
|
56
|
+
return edgeBA;
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Gets all edges in the database.
|
|
61
|
+
*/
|
|
62
|
+
async getAll(collection) {
|
|
63
|
+
return await this.db.find(collection, {});
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Get the names of all available databases.
|
|
67
|
+
*/
|
|
68
|
+
async getCollections() {
|
|
69
|
+
return await this.db.getCollections();
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Check and create the specified collection if it doesn't exist.
|
|
73
|
+
*/
|
|
74
|
+
async checkCollection(collection) {
|
|
75
|
+
await this.db.checkCollection(collection);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Check if a collection exists.
|
|
79
|
+
*/
|
|
80
|
+
async issetCollection(collection) {
|
|
81
|
+
return await this.db.issetCollection(collection);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Removes a database collection from the file system.
|
|
85
|
+
*/
|
|
86
|
+
removeCollection(collection) {
|
|
87
|
+
this.db.removeCollection(collection);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
exports.default = Graph;
|
|
@@ -3,5 +3,5 @@ import Graph from "./graph.js";
|
|
|
3
3
|
import DataBaseRemote from "./remote/client/database.js";
|
|
4
4
|
import GraphRemote from "./remote/client/graph.js";
|
|
5
5
|
import genId from "./gen.js";
|
|
6
|
-
|
|
7
|
-
export { DataBase, Graph, DataBaseRemote, GraphRemote, genId };
|
|
6
|
+
import Relation from "./relation.js";
|
|
7
|
+
export { DataBase, Graph, DataBaseRemote, GraphRemote, Relation, genId };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.genId = exports.Relation = exports.GraphRemote = exports.DataBaseRemote = exports.Graph = exports.DataBase = void 0;
|
|
7
|
+
const database_js_1 = __importDefault(require("./database.js"));
|
|
8
|
+
exports.DataBase = database_js_1.default;
|
|
9
|
+
const graph_js_1 = __importDefault(require("./graph.js"));
|
|
10
|
+
exports.Graph = graph_js_1.default;
|
|
11
|
+
const database_js_2 = __importDefault(require("./remote/client/database.js"));
|
|
12
|
+
exports.DataBaseRemote = database_js_2.default;
|
|
13
|
+
const graph_js_2 = __importDefault(require("./remote/client/graph.js"));
|
|
14
|
+
exports.GraphRemote = graph_js_2.default;
|
|
15
|
+
const gen_js_1 = __importDefault(require("./gen.js"));
|
|
16
|
+
exports.genId = gen_js_1.default;
|
|
17
|
+
const relation_js_1 = __importDefault(require("./relation.js"));
|
|
18
|
+
exports.Relation = relation_js_1.default;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import DataBase from "./database.js";
|
|
2
|
+
import { ArgOrFunc } from "./types/arg.js";
|
|
3
|
+
import { DbFindOpts } from "./types/options.js";
|
|
4
|
+
export interface Databases {
|
|
5
|
+
[key: string]: DataBase;
|
|
6
|
+
}
|
|
7
|
+
declare class Relation {
|
|
8
|
+
databases: Databases;
|
|
9
|
+
constructor(databases: Databases);
|
|
10
|
+
/**
|
|
11
|
+
* Resolves the relation path in format 'dbName.collectionName'
|
|
12
|
+
*/
|
|
13
|
+
_resolvePath(path: string): {
|
|
14
|
+
db: DataBase;
|
|
15
|
+
collection: string;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Processes relations for a single item
|
|
19
|
+
* @param {Object} item - Item to process relations for
|
|
20
|
+
* @param {Object} relations - Relations configuration
|
|
21
|
+
* @returns {Promise<Object>} Processed item with resolved relations
|
|
22
|
+
*/
|
|
23
|
+
_processItemRelations(item: Object, relations: Object): Promise<{
|
|
24
|
+
constructor: Function;
|
|
25
|
+
toString(): string;
|
|
26
|
+
toLocaleString(): string;
|
|
27
|
+
valueOf(): Object;
|
|
28
|
+
hasOwnProperty(v: PropertyKey): boolean;
|
|
29
|
+
isPrototypeOf(v: Object): boolean;
|
|
30
|
+
propertyIsEnumerable(v: PropertyKey): boolean;
|
|
31
|
+
}>;
|
|
32
|
+
/**
|
|
33
|
+
* Finds items with relations
|
|
34
|
+
* @param path - Path in format 'dbName.collectionName'
|
|
35
|
+
* @param search - Search query or function
|
|
36
|
+
* @param relations - Relations configuration
|
|
37
|
+
* @param options - Search options
|
|
38
|
+
*/
|
|
39
|
+
find(path: string, search: ArgOrFunc, relations?: {}, options?: DbFindOpts): Promise<{
|
|
40
|
+
constructor: Function;
|
|
41
|
+
toString(): string;
|
|
42
|
+
toLocaleString(): string;
|
|
43
|
+
valueOf(): Object;
|
|
44
|
+
hasOwnProperty(v: PropertyKey): boolean;
|
|
45
|
+
isPrototypeOf(v: Object): boolean;
|
|
46
|
+
propertyIsEnumerable(v: PropertyKey): boolean;
|
|
47
|
+
}[]>;
|
|
48
|
+
/**
|
|
49
|
+
* Finds one item with relations
|
|
50
|
+
* @param path - Path in format 'dbName.collectionName'
|
|
51
|
+
* @param search - Search query or function
|
|
52
|
+
* @param relations - Relations configuration
|
|
53
|
+
*/
|
|
54
|
+
findOne(path: string, search: ArgOrFunc, relations?: Object): Promise<{
|
|
55
|
+
constructor: Function;
|
|
56
|
+
toString(): string;
|
|
57
|
+
toLocaleString(): string;
|
|
58
|
+
valueOf(): Object;
|
|
59
|
+
hasOwnProperty(v: PropertyKey): boolean;
|
|
60
|
+
isPrototypeOf(v: Object): boolean;
|
|
61
|
+
propertyIsEnumerable(v: PropertyKey): boolean;
|
|
62
|
+
}>;
|
|
63
|
+
}
|
|
64
|
+
export default Relation;
|
|
@@ -1,86 +1,67 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class Relation {
|
|
4
|
+
databases;
|
|
5
|
+
constructor(databases) {
|
|
3
6
|
this.databases = databases;
|
|
4
7
|
}
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
* @returns {Object} Object containing database and collection name
|
|
10
|
-
* @throws {Error} If database doesn't exist
|
|
11
|
-
*/
|
|
12
|
-
_resolvePath(path){
|
|
8
|
+
/**
|
|
9
|
+
* Resolves the relation path in format 'dbName.collectionName'
|
|
10
|
+
*/
|
|
11
|
+
_resolvePath(path) {
|
|
13
12
|
const sanitizedPath = path.replace(/\\\./g, '\uffff');
|
|
14
13
|
const separatorIndex = sanitizedPath.indexOf('.');
|
|
15
|
-
|
|
16
|
-
if(separatorIndex === -1)
|
|
14
|
+
if (separatorIndex === -1)
|
|
17
15
|
throw new Error(`Invalid path format "${path}". Expected format 'dbName.collectionName'.`);
|
|
18
|
-
|
|
19
16
|
const dbName = sanitizedPath.slice(0, separatorIndex).replace(/\uffff/g, '.');
|
|
20
17
|
const collectionName = sanitizedPath.slice(separatorIndex + 1).replace(/\uffff/g, '.');
|
|
21
|
-
|
|
22
|
-
if(!this.databases[dbName])
|
|
18
|
+
if (!this.databases[dbName])
|
|
23
19
|
throw new Error(`Database "${dbName}" not found`);
|
|
24
|
-
|
|
25
20
|
return { db: this.databases[dbName], collection: collectionName };
|
|
26
21
|
}
|
|
27
|
-
|
|
28
22
|
/**
|
|
29
23
|
* Processes relations for a single item
|
|
30
24
|
* @param {Object} item - Item to process relations for
|
|
31
25
|
* @param {Object} relations - Relations configuration
|
|
32
26
|
* @returns {Promise<Object>} Processed item with resolved relations
|
|
33
27
|
*/
|
|
34
|
-
async _processItemRelations(item, relations){
|
|
28
|
+
async _processItemRelations(item, relations) {
|
|
35
29
|
const result = { ...item };
|
|
36
|
-
|
|
37
|
-
for(const [field, relationConfig] of Object.entries(relations)){
|
|
30
|
+
for (const [field, relationConfig] of Object.entries(relations)) {
|
|
38
31
|
const { from, localField, foreignField, as, multiple = false } = relationConfig;
|
|
39
32
|
const { db, collection } = this._resolvePath(from);
|
|
40
|
-
|
|
41
33
|
const searchQuery = { [foreignField]: item[localField] };
|
|
42
34
|
const fn = multiple ? db.find : db.findOne;
|
|
43
35
|
const relatedItem = await fn(collection, searchQuery);
|
|
44
36
|
result[as || field] = relatedItem;
|
|
45
37
|
}
|
|
46
|
-
|
|
47
38
|
return result;
|
|
48
39
|
}
|
|
49
|
-
|
|
50
40
|
/**
|
|
51
41
|
* Finds items with relations
|
|
52
|
-
* @param
|
|
53
|
-
* @param
|
|
54
|
-
* @param
|
|
55
|
-
* @param
|
|
56
|
-
* @returns {Promise<Array>} Array of items with resolved relations
|
|
42
|
+
* @param path - Path in format 'dbName.collectionName'
|
|
43
|
+
* @param search - Search query or function
|
|
44
|
+
* @param relations - Relations configuration
|
|
45
|
+
* @param options - Search options
|
|
57
46
|
*/
|
|
58
|
-
async find(path, search, relations={}, options={}){
|
|
47
|
+
async find(path, search, relations = {}, options = {}) {
|
|
59
48
|
const { db, collection } = this._resolvePath(path);
|
|
60
49
|
const items = await db.find(collection, search, {}, options);
|
|
61
|
-
|
|
62
|
-
const results = await Promise.all(
|
|
63
|
-
items.map(item => this._processItemRelations(item, relations))
|
|
64
|
-
);
|
|
65
|
-
|
|
50
|
+
const results = await Promise.all(items.map(item => this._processItemRelations(item, relations)));
|
|
66
51
|
return results;
|
|
67
52
|
}
|
|
68
|
-
|
|
69
53
|
/**
|
|
70
54
|
* Finds one item with relations
|
|
71
|
-
* @param
|
|
72
|
-
* @param
|
|
73
|
-
* @param
|
|
74
|
-
* @returns {Promise<Object>} Item with resolved relations
|
|
55
|
+
* @param path - Path in format 'dbName.collectionName'
|
|
56
|
+
* @param search - Search query or function
|
|
57
|
+
* @param relations - Relations configuration
|
|
75
58
|
*/
|
|
76
|
-
async findOne(path, search, relations={}){
|
|
59
|
+
async findOne(path, search, relations = {}) {
|
|
77
60
|
const { db, collection } = this._resolvePath(path);
|
|
78
61
|
const item = await db.findOne(collection, search);
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
62
|
+
if (!item)
|
|
63
|
+
return null;
|
|
82
64
|
return await this._processItemRelations(item, relations);
|
|
83
65
|
}
|
|
84
66
|
}
|
|
85
|
-
|
|
86
|
-
export default Relation;
|
|
67
|
+
exports.default = Relation;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import CollectionManager from "../../CollectionManager.js";
|
|
2
|
+
import { Remote, RequestData } from "./remote.js";
|
|
3
|
+
import { Arg, ArgOrFunc } from "../../types/arg";
|
|
4
|
+
import { DbFindOpts, FindOpts } from "../../types/options.js";
|
|
5
|
+
import { Context } from "../../types/types";
|
|
6
|
+
/**
|
|
7
|
+
* Represents a database management class for performing CRUD operations.
|
|
8
|
+
* Uses a remote database.
|
|
9
|
+
* @class
|
|
10
|
+
*/
|
|
11
|
+
declare class DataBaseRemote {
|
|
12
|
+
remote: Remote;
|
|
13
|
+
constructor(remote: Remote);
|
|
14
|
+
/**
|
|
15
|
+
* Make a request to the remote database.
|
|
16
|
+
*/
|
|
17
|
+
_request(type: string, data: RequestData): Promise<any>;
|
|
18
|
+
/**
|
|
19
|
+
* Create a new instance of a CollectionManager class.
|
|
20
|
+
*/
|
|
21
|
+
c(collection: string): CollectionManager;
|
|
22
|
+
/**
|
|
23
|
+
* Get the names of all available databases.
|
|
24
|
+
*/
|
|
25
|
+
getCollections(): Promise<any>;
|
|
26
|
+
/**
|
|
27
|
+
* Check and create the specified collection if it doesn't exist.
|
|
28
|
+
*/
|
|
29
|
+
checkCollection(collection: string): Promise<any>;
|
|
30
|
+
/**
|
|
31
|
+
* Check if a collection exists.
|
|
32
|
+
*/
|
|
33
|
+
issetCollection(collection: any): Promise<any>;
|
|
34
|
+
/**
|
|
35
|
+
* Add data to a database.
|
|
36
|
+
*/
|
|
37
|
+
add(collection: any, data: any, id_gen?: boolean): Promise<any>;
|
|
38
|
+
/**
|
|
39
|
+
* Find data in a database.
|
|
40
|
+
*/
|
|
41
|
+
find(collection: string, search: ArgOrFunc, context?: Context, options?: DbFindOpts, findOpts?: FindOpts): Promise<any>;
|
|
42
|
+
/**
|
|
43
|
+
* Find one data entry in a database.
|
|
44
|
+
*/
|
|
45
|
+
findOne(collection: string, search: ArgOrFunc, context?: Context, findOpts?: FindOpts): Promise<any>;
|
|
46
|
+
/**
|
|
47
|
+
* Update data in a database.
|
|
48
|
+
*/
|
|
49
|
+
update(collection: string, search: ArgOrFunc, arg: ArgOrFunc, context?: Context): Promise<any>;
|
|
50
|
+
/**
|
|
51
|
+
* Update one data entry in a database.
|
|
52
|
+
*/
|
|
53
|
+
updateOne(collection: string, search: ArgOrFunc, arg: ArgOrFunc, context?: Context): Promise<any>;
|
|
54
|
+
/**
|
|
55
|
+
* Remove data from a database.
|
|
56
|
+
*/
|
|
57
|
+
remove(collection: string, search: ArgOrFunc, context?: Context): Promise<any>;
|
|
58
|
+
/**
|
|
59
|
+
* Remove one data entry from a database.
|
|
60
|
+
*/
|
|
61
|
+
removeOne(collection: string, search: ArgOrFunc, context?: Context): Promise<any>;
|
|
62
|
+
/**
|
|
63
|
+
* Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
|
|
64
|
+
*/
|
|
65
|
+
updateOneOrAdd(collection: string, search: ArgOrFunc, arg: ArgOrFunc, add_arg?: Arg, context?: Context, id_gen?: boolean): Promise<any>;
|
|
66
|
+
/**
|
|
67
|
+
* Removes a database collection from the file system.
|
|
68
|
+
*/
|
|
69
|
+
removeCollection(name: string): Promise<any>;
|
|
70
|
+
}
|
|
71
|
+
export default DataBaseRemote;
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const got_1 = __importDefault(require("got"));
|
|
7
|
+
const CollectionManager_js_1 = __importDefault(require("../../CollectionManager.js"));
|
|
8
|
+
const function_js_1 = __importDefault(require("./function.js"));
|
|
9
|
+
/**
|
|
10
|
+
* Represents a database management class for performing CRUD operations.
|
|
11
|
+
* Uses a remote database.
|
|
12
|
+
* @class
|
|
13
|
+
*/
|
|
14
|
+
class DataBaseRemote {
|
|
15
|
+
remote;
|
|
16
|
+
constructor(remote) {
|
|
17
|
+
this.remote = remote;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Make a request to the remote database.
|
|
21
|
+
*/
|
|
22
|
+
async _request(type, data) {
|
|
23
|
+
data.db = this.remote.name;
|
|
24
|
+
const procesed = (0, function_js_1.default)(data);
|
|
25
|
+
data = {
|
|
26
|
+
keys: procesed.keys,
|
|
27
|
+
...procesed.data
|
|
28
|
+
};
|
|
29
|
+
const res = await got_1.default.post(this.remote.url + "/db/database/" + type, {
|
|
30
|
+
// @ts-ignore: Some jerk can't do the types correctly.
|
|
31
|
+
body: data,
|
|
32
|
+
headers: {
|
|
33
|
+
"Authorization": this.remote.auth
|
|
34
|
+
},
|
|
35
|
+
json: true,
|
|
36
|
+
responseType: "json"
|
|
37
|
+
});
|
|
38
|
+
if (res.body.err)
|
|
39
|
+
throw new Error(res.body.msg);
|
|
40
|
+
return res.body.result;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Create a new instance of a CollectionManager class.
|
|
44
|
+
*/
|
|
45
|
+
c(collection) {
|
|
46
|
+
return new CollectionManager_js_1.default(this, collection);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Get the names of all available databases.
|
|
50
|
+
*/
|
|
51
|
+
async getCollections() {
|
|
52
|
+
return await this._request("getCollections", {});
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Check and create the specified collection if it doesn't exist.
|
|
56
|
+
*/
|
|
57
|
+
async checkCollection(collection) {
|
|
58
|
+
return await this._request("checkCollection", { collection });
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Check if a collection exists.
|
|
62
|
+
*/
|
|
63
|
+
async issetCollection(collection) {
|
|
64
|
+
return await this._request("issetCollection", { collection });
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Add data to a database.
|
|
68
|
+
*/
|
|
69
|
+
async add(collection, data, id_gen = true) {
|
|
70
|
+
return await this._request("add", { collection, data, id_gen });
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Find data in a database.
|
|
74
|
+
*/
|
|
75
|
+
async find(collection, search, context = {}, options = {}, findOpts = {}) {
|
|
76
|
+
const searchStr = typeof search === "function" ? search.toString() : search;
|
|
77
|
+
const findOptsRemote = {};
|
|
78
|
+
if (findOpts.select)
|
|
79
|
+
findOptsRemote.select = findOpts.select;
|
|
80
|
+
if (findOpts.exclude)
|
|
81
|
+
findOptsRemote.exclude = findOpts.exclude;
|
|
82
|
+
if (findOpts.transform)
|
|
83
|
+
findOptsRemote.transform = findOpts.transform.toString();
|
|
84
|
+
return await this._request("find", { collection, search: searchStr, options, context, findOpts });
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Find one data entry in a database.
|
|
88
|
+
*/
|
|
89
|
+
async findOne(collection, search, context = {}, findOpts = {}) {
|
|
90
|
+
const searchStr = typeof search === "function" ? search.toString() : search;
|
|
91
|
+
const findOptsRemote = {};
|
|
92
|
+
if (findOpts.select)
|
|
93
|
+
findOptsRemote.select = findOpts.select;
|
|
94
|
+
if (findOpts.exclude)
|
|
95
|
+
findOptsRemote.exclude = findOpts.exclude;
|
|
96
|
+
if (findOpts.transform)
|
|
97
|
+
findOptsRemote.transform = findOpts.transform.toString();
|
|
98
|
+
return await this._request("findOne", { collection, search: searchStr, context, findOpts });
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Update data in a database.
|
|
102
|
+
*/
|
|
103
|
+
async update(collection, search, arg, context = {}) {
|
|
104
|
+
const searchStr = typeof search === "function" ? search.toString() : search;
|
|
105
|
+
const argStr = typeof arg === "function" ? arg.toString() : arg;
|
|
106
|
+
return await this._request("update", { collection, search: searchStr, arg: argStr, context });
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Update one data entry in a database.
|
|
110
|
+
*/
|
|
111
|
+
async updateOne(collection, search, arg, context = {}) {
|
|
112
|
+
const searchStr = typeof search === "function" ? search.toString() : search;
|
|
113
|
+
const argStr = typeof arg === "function" ? arg.toString() : arg;
|
|
114
|
+
return await this._request("updateOne", { collection, search: searchStr, arg: argStr, context });
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Remove data from a database.
|
|
118
|
+
*/
|
|
119
|
+
async remove(collection, search, context = {}) {
|
|
120
|
+
const searchStr = typeof search === "function" ? search.toString() : search;
|
|
121
|
+
return await this._request("remove", { collection, search: searchStr, context });
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Remove one data entry from a database.
|
|
125
|
+
*/
|
|
126
|
+
async removeOne(collection, search, context = {}) {
|
|
127
|
+
const searchStr = typeof search === "function" ? search.toString() : search;
|
|
128
|
+
return await this._request("removeOne", { collection, search: searchStr, context });
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
|
|
132
|
+
*/
|
|
133
|
+
async updateOneOrAdd(collection, search, arg, add_arg = {}, context = {}, id_gen = true) {
|
|
134
|
+
const searchStr = typeof search === "function" ? search.toString() : search;
|
|
135
|
+
const argStr = typeof arg === "function" ? arg.toString() : arg;
|
|
136
|
+
return await this._request("updateOneOrAdd", { collection, search: searchStr, arg: argStr, add_arg, id_gen, context });
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Removes a database collection from the file system.
|
|
140
|
+
*/
|
|
141
|
+
removeCollection(name) {
|
|
142
|
+
return this._request("removeCollection", { name });
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
exports.default = DataBaseRemote;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
function serializeFunctions(data) {
|
|
4
|
+
const functionKeys = [];
|
|
5
|
+
function convertFunctionToString(fn) {
|
|
6
|
+
return typeof fn === "function" ? fn.toString() : fn;
|
|
7
|
+
}
|
|
8
|
+
function traverseAndSerialize(obj, path = "") {
|
|
9
|
+
Object.keys(obj).forEach((key) => {
|
|
10
|
+
const value = obj[key];
|
|
11
|
+
const fullPath = path ? `${path}.${key.replace(/\./g, "[dot]")}` : key;
|
|
12
|
+
if (typeof value === "function") {
|
|
13
|
+
functionKeys.push(fullPath);
|
|
14
|
+
obj[key] = convertFunctionToString(value);
|
|
15
|
+
}
|
|
16
|
+
else if (Array.isArray(value)) {
|
|
17
|
+
value.forEach((item, index) => {
|
|
18
|
+
if (typeof item === "function") {
|
|
19
|
+
functionKeys.push(`${fullPath}[${index}]`);
|
|
20
|
+
value[index] = convertFunctionToString(item);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
else if (typeof value === "object" && value !== null) {
|
|
25
|
+
traverseAndSerialize(value, fullPath);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
traverseAndSerialize(data);
|
|
30
|
+
return { data, keys: functionKeys };
|
|
31
|
+
}
|
|
32
|
+
exports.default = serializeFunctions;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Remote, RequestData } from "./remote";
|
|
2
|
+
/**
|
|
3
|
+
* A class representing a graph database.
|
|
4
|
+
* Uses a remote database.
|
|
5
|
+
* @class
|
|
6
|
+
*/
|
|
7
|
+
declare class GraphRemote {
|
|
8
|
+
remote: Remote;
|
|
9
|
+
/**
|
|
10
|
+
* Create a new database instance.
|
|
11
|
+
*/
|
|
12
|
+
constructor(remote: Remote);
|
|
13
|
+
/**
|
|
14
|
+
* Make a request to the remote database.
|
|
15
|
+
*/
|
|
16
|
+
_request(type: string, data: RequestData): Promise<any>;
|
|
17
|
+
/**
|
|
18
|
+
* Adds an edge between two nodes.
|
|
19
|
+
*/
|
|
20
|
+
add(collection: string, nodeA: string, nodeB: string): Promise<any>;
|
|
21
|
+
/**
|
|
22
|
+
* Removes an edge between two nodes.
|
|
23
|
+
*/
|
|
24
|
+
remove(collection: string, nodeA: string, nodeB: string): Promise<any>;
|
|
25
|
+
/**
|
|
26
|
+
* Finds all edges with either node equal to `node`.
|
|
27
|
+
*/
|
|
28
|
+
find(collection: string, node: string): Promise<any>;
|
|
29
|
+
/**
|
|
30
|
+
* Finds one edge with either node equal to `nodeA` and the other equal to `nodeB`.
|
|
31
|
+
*/
|
|
32
|
+
findOne(collection: string, nodeA: string, nodeB: string): Promise<any>;
|
|
33
|
+
/**
|
|
34
|
+
* Get all edges in the collection.
|
|
35
|
+
*/
|
|
36
|
+
getAll(collection: string): Promise<any>;
|
|
37
|
+
/**
|
|
38
|
+
* Get the names of all available databases.
|
|
39
|
+
*/
|
|
40
|
+
getCollections(): Promise<any>;
|
|
41
|
+
/**
|
|
42
|
+
* Check and create the specified collection if it doesn't exist.
|
|
43
|
+
*/
|
|
44
|
+
checkCollection(collection: string): Promise<any>;
|
|
45
|
+
/**
|
|
46
|
+
* Check if a collection exists.
|
|
47
|
+
*/
|
|
48
|
+
issetCollection(collection: string): Promise<any>;
|
|
49
|
+
/**
|
|
50
|
+
* Remove the specified collection.
|
|
51
|
+
*/
|
|
52
|
+
removeCollection(collection: string): Promise<any>;
|
|
53
|
+
}
|
|
54
|
+
export default GraphRemote;
|