@wxn0brp/db 0.0.5 → 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
package/dist/esm/gen.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
const usedIdsMap = new Map();
|
|
2
|
+
/**
|
|
3
|
+
* Generates a unique identifier based on specified parts.
|
|
4
|
+
*/
|
|
5
|
+
export default function genId(parts = null, fill = 1) {
|
|
6
|
+
parts = changeInputToPartsArray(parts, fill);
|
|
7
|
+
const time = getTime();
|
|
8
|
+
const id = getUniqueRandom(time, parts);
|
|
9
|
+
return id;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Generates a unique random identifier based on time and parts.
|
|
13
|
+
* @param time - The current time in a base36 string format.
|
|
14
|
+
* @param parts - An array of parts to be used for generating the identifier.
|
|
15
|
+
* @param s - Recursion counter for handling collision (default: 0).
|
|
16
|
+
* @returns {string} The unique random identifier.
|
|
17
|
+
*/
|
|
18
|
+
function getUniqueRandom(time, partsA, s = 0) {
|
|
19
|
+
const parts = partsA.map(l => getRandom(l));
|
|
20
|
+
const id = [time, ...parts].join("-");
|
|
21
|
+
if (usedIdsMap.has(id)) {
|
|
22
|
+
s++;
|
|
23
|
+
if (s < 25)
|
|
24
|
+
return getUniqueRandom(time, partsA, s);
|
|
25
|
+
partsA = addOneToPods(partsA);
|
|
26
|
+
time = getTime();
|
|
27
|
+
return getUniqueRandom(time, partsA);
|
|
28
|
+
}
|
|
29
|
+
usedIdsMap.set(id, Date.now() + 2000);
|
|
30
|
+
usedIdsMap.forEach((value, key) => {
|
|
31
|
+
if (value < Date.now())
|
|
32
|
+
usedIdsMap.delete(key);
|
|
33
|
+
});
|
|
34
|
+
return id;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Generates a random string of base36 characters.
|
|
38
|
+
*/
|
|
39
|
+
function getRandom(unix) {
|
|
40
|
+
return (Math.floor(Math.random() * Math.pow(36, unix))).toString(36);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Gets the current time in a base36 string format.
|
|
44
|
+
*/
|
|
45
|
+
function getTime() {
|
|
46
|
+
return Math.floor(new Date().getTime() / 1000).toString(36);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Adds one to each part of the input array.
|
|
50
|
+
*/
|
|
51
|
+
function addOneToPods(array) {
|
|
52
|
+
const sum = array.reduce((acc, current) => acc + current, 0);
|
|
53
|
+
const num = sum + 1;
|
|
54
|
+
const len = array.length;
|
|
55
|
+
const result = [];
|
|
56
|
+
const quotient = Math.floor(num / len);
|
|
57
|
+
const remainder = num % len;
|
|
58
|
+
for (let i = 0; i < len; i++) {
|
|
59
|
+
if (i < remainder)
|
|
60
|
+
result.push(quotient + 1);
|
|
61
|
+
else
|
|
62
|
+
result.push(quotient);
|
|
63
|
+
}
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Converts input to an array of parts.
|
|
68
|
+
*/
|
|
69
|
+
function changeInputToPartsArray(parts = null, fill = 1) {
|
|
70
|
+
if (Array.isArray(parts))
|
|
71
|
+
return parts;
|
|
72
|
+
if (typeof parts == "number")
|
|
73
|
+
return Array(parts).fill(fill);
|
|
74
|
+
return [1, 1];
|
|
75
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import DataBase from "./database.js";
|
|
2
|
+
import Data from "./types/data.js";
|
|
3
|
+
/**
|
|
4
|
+
* A class representing a graph database.
|
|
5
|
+
* @class
|
|
6
|
+
*/
|
|
7
|
+
declare class Graph {
|
|
8
|
+
db: DataBase;
|
|
9
|
+
constructor(databaseFolder: string);
|
|
10
|
+
/**
|
|
11
|
+
* Adds an edge between two nodes.
|
|
12
|
+
*/
|
|
13
|
+
add(collection: string, nodeA: string, nodeB: string): Promise<Data>;
|
|
14
|
+
/**
|
|
15
|
+
* Removes an edge between two nodes.
|
|
16
|
+
*/
|
|
17
|
+
remove(collection: string, nodeA: string, nodeB: string): Promise<boolean>;
|
|
18
|
+
/**
|
|
19
|
+
* Finds all edges with either node equal to `node`.
|
|
20
|
+
*/
|
|
21
|
+
find(collection: string, node: string): Promise<Data[]>;
|
|
22
|
+
/**
|
|
23
|
+
* Finds one edge with either node equal to `nodeA` and the other equal to `nodeB`.
|
|
24
|
+
*/
|
|
25
|
+
findOne(collection: any, nodeA: any, nodeB: any): Promise<Data>;
|
|
26
|
+
/**
|
|
27
|
+
* Gets all edges in the database.
|
|
28
|
+
*/
|
|
29
|
+
getAll(collection: any): Promise<Data[]>;
|
|
30
|
+
/**
|
|
31
|
+
* Get the names of all available databases.
|
|
32
|
+
*/
|
|
33
|
+
getCollections(): Promise<string[]>;
|
|
34
|
+
/**
|
|
35
|
+
* Check and create the specified collection if it doesn't exist.
|
|
36
|
+
*/
|
|
37
|
+
checkCollection(collection: any): Promise<void>;
|
|
38
|
+
/**
|
|
39
|
+
* Check if a collection exists.
|
|
40
|
+
*/
|
|
41
|
+
issetCollection(collection: any): Promise<boolean>;
|
|
42
|
+
/**
|
|
43
|
+
* Removes a database collection from the file system.
|
|
44
|
+
*/
|
|
45
|
+
removeCollection(collection: any): void;
|
|
46
|
+
}
|
|
47
|
+
export default Graph;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import DataBase from "./database.js";
|
|
2
|
+
/**
|
|
3
|
+
* A class representing a graph database.
|
|
4
|
+
* @class
|
|
5
|
+
*/
|
|
6
|
+
class Graph {
|
|
7
|
+
db;
|
|
8
|
+
constructor(databaseFolder) {
|
|
9
|
+
this.db = new DataBase(databaseFolder);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Adds an edge between two nodes.
|
|
13
|
+
*/
|
|
14
|
+
async add(collection, nodeA, nodeB) {
|
|
15
|
+
const sortedNodes = [nodeA, nodeB].sort();
|
|
16
|
+
return await this.db.add(collection, {
|
|
17
|
+
a: sortedNodes[0],
|
|
18
|
+
b: sortedNodes[1]
|
|
19
|
+
}, false);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Removes an edge between two nodes.
|
|
23
|
+
*/
|
|
24
|
+
async remove(collection, nodeA, nodeB) {
|
|
25
|
+
const sortedNodes = [nodeA, nodeB].sort();
|
|
26
|
+
const query = { a: sortedNodes[0], b: sortedNodes[1] };
|
|
27
|
+
return await this.db.removeOne(collection, query);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Finds all edges with either node equal to `node`.
|
|
31
|
+
*/
|
|
32
|
+
async find(collection, node) {
|
|
33
|
+
const edges = [];
|
|
34
|
+
const edgesByANode = await this.db.find(collection, { a: node });
|
|
35
|
+
const edgesByBNode = await this.db.find(collection, { b: node });
|
|
36
|
+
if (edgesByANode)
|
|
37
|
+
edges.push(...edgesByANode);
|
|
38
|
+
if (edgesByBNode)
|
|
39
|
+
edges.push(...edgesByBNode);
|
|
40
|
+
return edges;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Finds one edge with either node equal to `nodeA` and the other equal to `nodeB`.
|
|
44
|
+
*/
|
|
45
|
+
async findOne(collection, nodeA, nodeB) {
|
|
46
|
+
const edgeAB = await this.db.findOne(collection, { a: nodeA, b: nodeB });
|
|
47
|
+
if (edgeAB)
|
|
48
|
+
return edgeAB;
|
|
49
|
+
const edgeBA = await this.db.findOne(collection, { a: nodeB, b: nodeA });
|
|
50
|
+
if (edgeBA)
|
|
51
|
+
return edgeBA;
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Gets all edges in the database.
|
|
56
|
+
*/
|
|
57
|
+
async getAll(collection) {
|
|
58
|
+
return await this.db.find(collection, {});
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Get the names of all available databases.
|
|
62
|
+
*/
|
|
63
|
+
async getCollections() {
|
|
64
|
+
return await this.db.getCollections();
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Check and create the specified collection if it doesn't exist.
|
|
68
|
+
*/
|
|
69
|
+
async checkCollection(collection) {
|
|
70
|
+
await this.db.checkCollection(collection);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Check if a collection exists.
|
|
74
|
+
*/
|
|
75
|
+
async issetCollection(collection) {
|
|
76
|
+
return await this.db.issetCollection(collection);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Removes a database collection from the file system.
|
|
80
|
+
*/
|
|
81
|
+
removeCollection(collection) {
|
|
82
|
+
this.db.removeCollection(collection);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
export default Graph;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import DataBase from "./database.js";
|
|
2
|
+
import Graph from "./graph.js";
|
|
3
|
+
import DataBaseRemote from "./remote/client/database.js";
|
|
4
|
+
import GraphRemote from "./remote/client/graph.js";
|
|
5
|
+
import genId from "./gen.js";
|
|
6
|
+
import Relation from "./relation.js";
|
|
7
|
+
export { DataBase, Graph, DataBaseRemote, GraphRemote, Relation, genId };
|
|
@@ -4,12 +4,4 @@ import DataBaseRemote from "./remote/client/database.js";
|
|
|
4
4
|
import GraphRemote from "./remote/client/graph.js";
|
|
5
5
|
import genId from "./gen.js";
|
|
6
6
|
import Relation from "./relation.js";
|
|
7
|
-
|
|
8
|
-
export {
|
|
9
|
-
DataBase,
|
|
10
|
-
Graph,
|
|
11
|
-
DataBaseRemote,
|
|
12
|
-
GraphRemote,
|
|
13
|
-
Relation,
|
|
14
|
-
genId
|
|
15
|
-
};
|
|
7
|
+
export { DataBase, Graph, DataBaseRemote, GraphRemote, Relation, genId };
|
|
@@ -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;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
class Relation {
|
|
2
|
+
databases;
|
|
3
|
+
constructor(databases) {
|
|
4
|
+
this.databases = databases;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Resolves the relation path in format 'dbName.collectionName'
|
|
8
|
+
*/
|
|
9
|
+
_resolvePath(path) {
|
|
10
|
+
const sanitizedPath = path.replace(/\\\./g, '\uffff');
|
|
11
|
+
const separatorIndex = sanitizedPath.indexOf('.');
|
|
12
|
+
if (separatorIndex === -1)
|
|
13
|
+
throw new Error(`Invalid path format "${path}". Expected format 'dbName.collectionName'.`);
|
|
14
|
+
const dbName = sanitizedPath.slice(0, separatorIndex).replace(/\uffff/g, '.');
|
|
15
|
+
const collectionName = sanitizedPath.slice(separatorIndex + 1).replace(/\uffff/g, '.');
|
|
16
|
+
if (!this.databases[dbName])
|
|
17
|
+
throw new Error(`Database "${dbName}" not found`);
|
|
18
|
+
return { db: this.databases[dbName], collection: collectionName };
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Processes relations for a single item
|
|
22
|
+
* @param {Object} item - Item to process relations for
|
|
23
|
+
* @param {Object} relations - Relations configuration
|
|
24
|
+
* @returns {Promise<Object>} Processed item with resolved relations
|
|
25
|
+
*/
|
|
26
|
+
async _processItemRelations(item, relations) {
|
|
27
|
+
const result = { ...item };
|
|
28
|
+
for (const [field, relationConfig] of Object.entries(relations)) {
|
|
29
|
+
const { from, localField, foreignField, as, multiple = false } = relationConfig;
|
|
30
|
+
const { db, collection } = this._resolvePath(from);
|
|
31
|
+
const searchQuery = { [foreignField]: item[localField] };
|
|
32
|
+
const fn = multiple ? db.find : db.findOne;
|
|
33
|
+
const relatedItem = await fn(collection, searchQuery);
|
|
34
|
+
result[as || field] = relatedItem;
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Finds items with relations
|
|
40
|
+
* @param path - Path in format 'dbName.collectionName'
|
|
41
|
+
* @param search - Search query or function
|
|
42
|
+
* @param relations - Relations configuration
|
|
43
|
+
* @param options - Search options
|
|
44
|
+
*/
|
|
45
|
+
async find(path, search, relations = {}, options = {}) {
|
|
46
|
+
const { db, collection } = this._resolvePath(path);
|
|
47
|
+
const items = await db.find(collection, search, {}, options);
|
|
48
|
+
const results = await Promise.all(items.map(item => this._processItemRelations(item, relations)));
|
|
49
|
+
return results;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Finds one item with relations
|
|
53
|
+
* @param path - Path in format 'dbName.collectionName'
|
|
54
|
+
* @param search - Search query or function
|
|
55
|
+
* @param relations - Relations configuration
|
|
56
|
+
*/
|
|
57
|
+
async findOne(path, search, relations = {}) {
|
|
58
|
+
const { db, collection } = this._resolvePath(path);
|
|
59
|
+
const item = await db.findOne(collection, search);
|
|
60
|
+
if (!item)
|
|
61
|
+
return null;
|
|
62
|
+
return await this._processItemRelations(item, relations);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
export 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,140 @@
|
|
|
1
|
+
import got from "got";
|
|
2
|
+
import CollectionManager from "../../CollectionManager.js";
|
|
3
|
+
import serializeFunctions from "./function.js";
|
|
4
|
+
/**
|
|
5
|
+
* Represents a database management class for performing CRUD operations.
|
|
6
|
+
* Uses a remote database.
|
|
7
|
+
* @class
|
|
8
|
+
*/
|
|
9
|
+
class DataBaseRemote {
|
|
10
|
+
remote;
|
|
11
|
+
constructor(remote) {
|
|
12
|
+
this.remote = remote;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Make a request to the remote database.
|
|
16
|
+
*/
|
|
17
|
+
async _request(type, data) {
|
|
18
|
+
data.db = this.remote.name;
|
|
19
|
+
const procesed = serializeFunctions(data);
|
|
20
|
+
data = {
|
|
21
|
+
keys: procesed.keys,
|
|
22
|
+
...procesed.data
|
|
23
|
+
};
|
|
24
|
+
const res = await got.post(this.remote.url + "/db/database/" + type, {
|
|
25
|
+
// @ts-ignore: Some jerk can't do the types correctly.
|
|
26
|
+
body: data,
|
|
27
|
+
headers: {
|
|
28
|
+
"Authorization": this.remote.auth
|
|
29
|
+
},
|
|
30
|
+
json: true,
|
|
31
|
+
responseType: "json"
|
|
32
|
+
});
|
|
33
|
+
if (res.body.err)
|
|
34
|
+
throw new Error(res.body.msg);
|
|
35
|
+
return res.body.result;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Create a new instance of a CollectionManager class.
|
|
39
|
+
*/
|
|
40
|
+
c(collection) {
|
|
41
|
+
return new CollectionManager(this, collection);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Get the names of all available databases.
|
|
45
|
+
*/
|
|
46
|
+
async getCollections() {
|
|
47
|
+
return await this._request("getCollections", {});
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Check and create the specified collection if it doesn't exist.
|
|
51
|
+
*/
|
|
52
|
+
async checkCollection(collection) {
|
|
53
|
+
return await this._request("checkCollection", { collection });
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Check if a collection exists.
|
|
57
|
+
*/
|
|
58
|
+
async issetCollection(collection) {
|
|
59
|
+
return await this._request("issetCollection", { collection });
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Add data to a database.
|
|
63
|
+
*/
|
|
64
|
+
async add(collection, data, id_gen = true) {
|
|
65
|
+
return await this._request("add", { collection, data, id_gen });
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Find data in a database.
|
|
69
|
+
*/
|
|
70
|
+
async find(collection, search, context = {}, options = {}, findOpts = {}) {
|
|
71
|
+
const searchStr = typeof search === "function" ? search.toString() : search;
|
|
72
|
+
const findOptsRemote = {};
|
|
73
|
+
if (findOpts.select)
|
|
74
|
+
findOptsRemote.select = findOpts.select;
|
|
75
|
+
if (findOpts.exclude)
|
|
76
|
+
findOptsRemote.exclude = findOpts.exclude;
|
|
77
|
+
if (findOpts.transform)
|
|
78
|
+
findOptsRemote.transform = findOpts.transform.toString();
|
|
79
|
+
return await this._request("find", { collection, search: searchStr, options, context, findOpts });
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Find one data entry in a database.
|
|
83
|
+
*/
|
|
84
|
+
async findOne(collection, search, context = {}, findOpts = {}) {
|
|
85
|
+
const searchStr = typeof search === "function" ? search.toString() : search;
|
|
86
|
+
const findOptsRemote = {};
|
|
87
|
+
if (findOpts.select)
|
|
88
|
+
findOptsRemote.select = findOpts.select;
|
|
89
|
+
if (findOpts.exclude)
|
|
90
|
+
findOptsRemote.exclude = findOpts.exclude;
|
|
91
|
+
if (findOpts.transform)
|
|
92
|
+
findOptsRemote.transform = findOpts.transform.toString();
|
|
93
|
+
return await this._request("findOne", { collection, search: searchStr, context, findOpts });
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Update data in a database.
|
|
97
|
+
*/
|
|
98
|
+
async update(collection, search, arg, context = {}) {
|
|
99
|
+
const searchStr = typeof search === "function" ? search.toString() : search;
|
|
100
|
+
const argStr = typeof arg === "function" ? arg.toString() : arg;
|
|
101
|
+
return await this._request("update", { collection, search: searchStr, arg: argStr, context });
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Update one data entry in a database.
|
|
105
|
+
*/
|
|
106
|
+
async updateOne(collection, search, arg, context = {}) {
|
|
107
|
+
const searchStr = typeof search === "function" ? search.toString() : search;
|
|
108
|
+
const argStr = typeof arg === "function" ? arg.toString() : arg;
|
|
109
|
+
return await this._request("updateOne", { collection, search: searchStr, arg: argStr, context });
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Remove data from a database.
|
|
113
|
+
*/
|
|
114
|
+
async remove(collection, search, context = {}) {
|
|
115
|
+
const searchStr = typeof search === "function" ? search.toString() : search;
|
|
116
|
+
return await this._request("remove", { collection, search: searchStr, context });
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Remove one data entry from a database.
|
|
120
|
+
*/
|
|
121
|
+
async removeOne(collection, search, context = {}) {
|
|
122
|
+
const searchStr = typeof search === "function" ? search.toString() : search;
|
|
123
|
+
return await this._request("removeOne", { collection, search: searchStr, context });
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
|
|
127
|
+
*/
|
|
128
|
+
async updateOneOrAdd(collection, search, arg, add_arg = {}, context = {}, id_gen = true) {
|
|
129
|
+
const searchStr = typeof search === "function" ? search.toString() : search;
|
|
130
|
+
const argStr = typeof arg === "function" ? arg.toString() : arg;
|
|
131
|
+
return await this._request("updateOneOrAdd", { collection, search: searchStr, arg: argStr, add_arg, id_gen, context });
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Removes a database collection from the file system.
|
|
135
|
+
*/
|
|
136
|
+
removeCollection(name) {
|
|
137
|
+
return this._request("removeCollection", { name });
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
export default DataBaseRemote;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
function serializeFunctions(data) {
|
|
2
|
+
const functionKeys = [];
|
|
3
|
+
function convertFunctionToString(fn) {
|
|
4
|
+
return typeof fn === "function" ? fn.toString() : fn;
|
|
5
|
+
}
|
|
6
|
+
function traverseAndSerialize(obj, path = "") {
|
|
7
|
+
Object.keys(obj).forEach((key) => {
|
|
8
|
+
const value = obj[key];
|
|
9
|
+
const fullPath = path ? `${path}.${key.replace(/\./g, "[dot]")}` : key;
|
|
10
|
+
if (typeof value === "function") {
|
|
11
|
+
functionKeys.push(fullPath);
|
|
12
|
+
obj[key] = convertFunctionToString(value);
|
|
13
|
+
}
|
|
14
|
+
else if (Array.isArray(value)) {
|
|
15
|
+
value.forEach((item, index) => {
|
|
16
|
+
if (typeof item === "function") {
|
|
17
|
+
functionKeys.push(`${fullPath}[${index}]`);
|
|
18
|
+
value[index] = convertFunctionToString(item);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
else if (typeof value === "object" && value !== null) {
|
|
23
|
+
traverseAndSerialize(value, fullPath);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
traverseAndSerialize(data);
|
|
28
|
+
return { data, keys: functionKeys };
|
|
29
|
+
}
|
|
30
|
+
export 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;
|