@wxn0brp/db 0.0.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/CollectionManager.js +119 -0
- package/LICENSE +21 -0
- package/README.md +194 -0
- package/action.js +250 -0
- package/cacheManager.js +83 -0
- package/database.js +208 -0
- package/executor.js +54 -0
- package/file/find.js +88 -0
- package/file/index.js +3 -0
- package/file/remove.js +75 -0
- package/file/update.js +83 -0
- package/file/utils.js +27 -0
- package/format.js +29 -0
- package/gen.js +97 -0
- package/graph.js +130 -0
- package/more.js +103 -0
- package/package.json +32 -0
- package/remote/client/database.js +229 -0
- package/remote/client/graph.js +139 -0
- package/remote/client/index.js +10 -0
- package/remote/server/auth.js +100 -0
- package/remote/server/db.js +197 -0
- package/remote/server/function.js +43 -0
- package/remote/server/graph.js +121 -0
- package/remote/server/gui/css/main.css +130 -0
- package/remote/server/gui/css/scrool.css +81 -0
- package/remote/server/gui/css/style.css +61 -0
- package/remote/server/gui/favicon.svg +12 -0
- package/remote/server/gui/html/data.html +15 -0
- package/remote/server/gui/html/main.html +46 -0
- package/remote/server/gui/html/nav.html +25 -0
- package/remote/server/gui/html/popup.html +51 -0
- package/remote/server/gui/index.html +49 -0
- package/remote/server/gui/js/api.js +166 -0
- package/remote/server/gui/js/index.js +17 -0
- package/remote/server/gui/js/loadHTML.js +16 -0
- package/remote/server/gui/js/popUp.js +72 -0
- package/remote/server/gui/js/queryApi.js +51 -0
- package/remote/server/gui/js/queryDb.js +79 -0
- package/remote/server/gui/js/queryGraph.js +144 -0
- package/remote/server/gui/js/render.js +64 -0
- package/remote/server/gui/js/templates.js +31 -0
- package/remote/server/gui/js/utils.js +36 -0
- package/remote/server/gui/js/vars.js +9 -0
- package/remote/server/gui/libs/core.js +176 -0
- package/remote/server/gui/libs/d3.v7.min.js +2 -0
- package/remote/server/gui/libs/handlebars.min.js +29 -0
- package/remote/server/gui/libs/json5.min.js +1 -0
- package/remote/server/index.js +63 -0
- package/remote/server/initDataBases.js +20 -0
- package/remote/server/pathUtils.js +7 -0
- package/remote/server/secret.js +23 -0
- package/remote/serverMgmt/index.js +86 -0
package/graph.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import DataBase from "./database.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A class representing a graph database.
|
|
5
|
+
* @class
|
|
6
|
+
*/
|
|
7
|
+
class Graph{
|
|
8
|
+
/**
|
|
9
|
+
* Initializes the graph database.
|
|
10
|
+
* @constructor
|
|
11
|
+
* @param {string} databaseFolder - The folder where the database is stored.
|
|
12
|
+
*/
|
|
13
|
+
constructor(databaseFolder){
|
|
14
|
+
this.db = new DataBase(databaseFolder);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Adds an edge between two nodes.
|
|
19
|
+
* @async
|
|
20
|
+
* @function
|
|
21
|
+
* @param {string} collection - The name of the collection.
|
|
22
|
+
* @param {string} nodeA - The first node.
|
|
23
|
+
* @param {string} nodeB - The second node.
|
|
24
|
+
* @returns {Promise<Object>} A promise that resolves with the added edge.
|
|
25
|
+
*/
|
|
26
|
+
async add(collection, nodeA, nodeB){
|
|
27
|
+
const sortedNodes = [nodeA, nodeB].sort();
|
|
28
|
+
return await this.db.add(collection, {
|
|
29
|
+
a: sortedNodes[0],
|
|
30
|
+
b: sortedNodes[1]
|
|
31
|
+
}, false);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Removes an edge between two nodes.
|
|
36
|
+
* @async
|
|
37
|
+
* @function
|
|
38
|
+
* @param {string} collection - The name of the collection.
|
|
39
|
+
* @param {string} nodeA - The first node.
|
|
40
|
+
* @param {string} nodeB - The second node.
|
|
41
|
+
* @returns {Promise<boolean>} A promise that resolves when the edge is removed.
|
|
42
|
+
*/
|
|
43
|
+
async remove(collection, nodeA, nodeB){
|
|
44
|
+
const sortedNodes = [nodeA, nodeB].sort();
|
|
45
|
+
const query = { a: sortedNodes[0], b: sortedNodes[1] };
|
|
46
|
+
return await this.db.removeOne(collection, query);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Finds all edges with either node equal to `node`.
|
|
51
|
+
* @async
|
|
52
|
+
* @function
|
|
53
|
+
* @param {string} collection - The name of the collection.
|
|
54
|
+
* @param {string} node - The node to search for.
|
|
55
|
+
* @returns {Promise<Object[]>} A promise that resolves with the found edges.
|
|
56
|
+
*/
|
|
57
|
+
async find(collection, node){
|
|
58
|
+
const edges = [];
|
|
59
|
+
const edgesByANode = await this.db.find(collection, { a: node });
|
|
60
|
+
const edgesByBNode = await this.db.find(collection, { b: node });
|
|
61
|
+
|
|
62
|
+
if(edgesByANode) edges.push(...edgesByANode);
|
|
63
|
+
if(edgesByBNode) edges.push(...edgesByBNode);
|
|
64
|
+
|
|
65
|
+
return edges;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Finds one edge with either node equal to `nodeA` and the other equal to `nodeB`.
|
|
70
|
+
* @async
|
|
71
|
+
* @function
|
|
72
|
+
* @param {string} collection - The name of the collection.
|
|
73
|
+
* @param {string} nodeA - The first node.
|
|
74
|
+
* @param {string} nodeB - The second node.
|
|
75
|
+
* @returns {Promise<Object|null>} A promise that resolves with the found edge or null if not found.
|
|
76
|
+
*/
|
|
77
|
+
async findOne(collection, nodeA, nodeB){
|
|
78
|
+
const edgeAB = await this.db.findOne(collection, { a: nodeA, b: nodeB });
|
|
79
|
+
if(edgeAB) return edgeAB;
|
|
80
|
+
|
|
81
|
+
const edgeBA = await this.db.findOne(collection, { a: nodeB, b: nodeA });
|
|
82
|
+
if(edgeBA) return edgeBA;
|
|
83
|
+
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Gets all edges in the database.
|
|
89
|
+
* @async
|
|
90
|
+
* @function
|
|
91
|
+
* @param {string} collection - The name of the collection.
|
|
92
|
+
* @returns {Promise<Object[]>} A promise that resolves with all edges in the database.
|
|
93
|
+
*/
|
|
94
|
+
async getAll(collection){
|
|
95
|
+
return await this.db.find(collection, {});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Get the names of all available databases.
|
|
100
|
+
*
|
|
101
|
+
* @function
|
|
102
|
+
* @returns {string[]} An array of database names.
|
|
103
|
+
*/
|
|
104
|
+
async getCollections(){
|
|
105
|
+
return await this.db.getCollections();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Check and create the specified collection if it doesn't exist.
|
|
110
|
+
*
|
|
111
|
+
* @function
|
|
112
|
+
* @param {string} collection - The collection to check.
|
|
113
|
+
*/
|
|
114
|
+
async checkCollection(collection){
|
|
115
|
+
await this.dbAction.checkCollection(collection);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Check if a collection exists.
|
|
120
|
+
*
|
|
121
|
+
* @function
|
|
122
|
+
* @param {string} collection - The name of the collection.
|
|
123
|
+
* @returns {boolean} True if the collection exists, false otherwise.
|
|
124
|
+
*/
|
|
125
|
+
async issetCollection(collection){
|
|
126
|
+
return await this.dbAction.issetCollection(collection);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export default Graph;
|
package/more.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if an object meets the criteria specified in the fields with operators.
|
|
3
|
+
* @function
|
|
4
|
+
* @param {Object} obj - The object to check.
|
|
5
|
+
* @param {Object} fields - Criteria with operators.
|
|
6
|
+
* @returns {boolean} - Whether the object meets the criteria.
|
|
7
|
+
* @throws {Error} - If fields is not an object.
|
|
8
|
+
*/
|
|
9
|
+
export function hasFieldsAdvanced(obj, fields){
|
|
10
|
+
if(typeof fields !== "object" || fields === null){
|
|
11
|
+
throw new Error("Fields must be an object");
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if("$and" in fields){
|
|
15
|
+
return fields["$and"].every(subFields => hasFieldsAdvanced(obj, subFields));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if("$or" in fields){
|
|
19
|
+
return fields["$or"].some(subFields => hasFieldsAdvanced(obj, subFields));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if("$set" in fields){
|
|
23
|
+
const setFields = fields["$set"];
|
|
24
|
+
return hasFields(obj, setFields);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if("$not" in fields){
|
|
28
|
+
return !hasFieldsAdvanced(obj, fields["$not"]);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return hasFields(obj, fields);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Checks if an object matches the standard field comparison.
|
|
36
|
+
* @function
|
|
37
|
+
* @param {Object} obj - The object to check.
|
|
38
|
+
* @param {Object} fields - Criteria to compare.
|
|
39
|
+
* @returns {boolean} - Whether the object matches the criteria.
|
|
40
|
+
*/
|
|
41
|
+
export function hasFields(obj, fields){
|
|
42
|
+
const keys = Object.keys(fields);
|
|
43
|
+
return keys.every(key => {
|
|
44
|
+
if(obj[key]){
|
|
45
|
+
if(typeof fields[key] === "object" && fields[key] !== null){
|
|
46
|
+
return hasFields(obj[key], fields[key]);
|
|
47
|
+
}
|
|
48
|
+
return obj[key] === fields[key];
|
|
49
|
+
}
|
|
50
|
+
return false;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Updates an object with new values.
|
|
56
|
+
* @function
|
|
57
|
+
* @param {Object} obj - The object to update.
|
|
58
|
+
* @param {Object} newVal - An object containing new values to update in the target object.
|
|
59
|
+
* @returns {Object} The updated object.
|
|
60
|
+
*/
|
|
61
|
+
export function updateObject(obj, newVal){
|
|
62
|
+
for(let key in newVal){
|
|
63
|
+
if(newVal.hasOwnProperty(key)){
|
|
64
|
+
obj[key] = newVal[key];
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return obj;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Updates an object with new values from a findOpts object.
|
|
72
|
+
* @function
|
|
73
|
+
* @param {Object} obj - The object to update.
|
|
74
|
+
* @param {Object} findOpts - An object containing options to update the target object.
|
|
75
|
+
* @param {function} [findOpts.transform] - A function to transform the object before applying the other options.
|
|
76
|
+
* @param {string[]} [findOpts.select] - An array of fields to select from the target object.
|
|
77
|
+
* @param {string[]} [findOpts.exclude] - An array of fields to exclude from the target object.
|
|
78
|
+
* @returns {Object} The updated object.
|
|
79
|
+
*/
|
|
80
|
+
export function updateFindObject(obj, findOpts){
|
|
81
|
+
const {
|
|
82
|
+
transform,
|
|
83
|
+
select,
|
|
84
|
+
exclude,
|
|
85
|
+
} = findOpts;
|
|
86
|
+
|
|
87
|
+
if(typeof transform === "function") obj = transform(obj);
|
|
88
|
+
|
|
89
|
+
if(Array.isArray(exclude)){
|
|
90
|
+
exclude.forEach(field => {
|
|
91
|
+
if(obj.hasOwnProperty(field)) delete obj[field];
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if(Array.isArray(select)){
|
|
96
|
+
obj = select.reduce((acc, field) => {
|
|
97
|
+
if(obj.hasOwnProperty(field)) acc[field] = obj[field];
|
|
98
|
+
return acc;
|
|
99
|
+
}, {});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return obj;
|
|
103
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wxn0brp/db",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"description": "A simple file-based database management system with support for CRUD operations, custom queries, and graph structures.",
|
|
6
|
+
"homepage": "https://github.com/wxn0brP/database",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/wxn0brP/database.git"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"database",
|
|
13
|
+
"file-based",
|
|
14
|
+
"CRUD",
|
|
15
|
+
"graph",
|
|
16
|
+
"query"
|
|
17
|
+
],
|
|
18
|
+
"author": "wxn0brP",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"type": "module",
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"body-parser": "^1.20.3",
|
|
23
|
+
"cors": "^2.8.5",
|
|
24
|
+
"dotenv": "^16.4.5",
|
|
25
|
+
"express": "^4.21.1",
|
|
26
|
+
"got": "^14.4.2",
|
|
27
|
+
"json5": "^2.2.3",
|
|
28
|
+
"jwt-simple": "^0.5.6",
|
|
29
|
+
"node-cache": "^5.1.2",
|
|
30
|
+
"readline": "^1.3.0"
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import got from "got";
|
|
2
|
+
import CollectionManager from "../../CollectionManager.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Represents a database management class for performing CRUD operations.
|
|
6
|
+
* Uses a remote database.
|
|
7
|
+
* @class
|
|
8
|
+
*/
|
|
9
|
+
class DataBaseRemote{
|
|
10
|
+
/**
|
|
11
|
+
* Create a new database instance.
|
|
12
|
+
* @constructor
|
|
13
|
+
* @param {object} remote - The remote database object.
|
|
14
|
+
* @param {string} remote.name - The name of the database.
|
|
15
|
+
* @param {string} remote.folder - The folder path where the database files are stored.
|
|
16
|
+
* @param {string} remote.auth - The authentication token.
|
|
17
|
+
* @param {string} remote.url - The URL of the remote database.
|
|
18
|
+
*/
|
|
19
|
+
constructor(remote){
|
|
20
|
+
this.remote = remote;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Make a request to the remote database.
|
|
25
|
+
* @async
|
|
26
|
+
* @function
|
|
27
|
+
* @param {string} type - The type of the request.
|
|
28
|
+
* @param {object} data - The data to send with the request.
|
|
29
|
+
* @returns {Promise<*>} A Promise that resolves with the result of the request.
|
|
30
|
+
* @throws {Error} If the request failed.
|
|
31
|
+
*/
|
|
32
|
+
async _request(type, data){
|
|
33
|
+
data.db = this.remote.name;
|
|
34
|
+
const res = await got.post(this.remote.url + "/db/database/" + type, {
|
|
35
|
+
json: data,
|
|
36
|
+
headers: {
|
|
37
|
+
"Authorization": this.remote.auth
|
|
38
|
+
},
|
|
39
|
+
responseType: "json"
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
if(res.body.err) throw new Error(res.body.msg);
|
|
43
|
+
return res.body.result;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Create a new instance of a CollectionManager class.
|
|
48
|
+
* @function
|
|
49
|
+
* @param {string} collection - The name of the collection.
|
|
50
|
+
* @returns {CollectionManager} A new instance of CollectionManager.
|
|
51
|
+
*/
|
|
52
|
+
c(collection){
|
|
53
|
+
return new CollectionManager(this, collection);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Get the names of all available databases.
|
|
58
|
+
*
|
|
59
|
+
* @function
|
|
60
|
+
* @returns {string[]} An array of database names.
|
|
61
|
+
*/
|
|
62
|
+
async getCollections(){
|
|
63
|
+
return await this._request("getCollections", {});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Check and create the specified collection if it doesn't exist.
|
|
68
|
+
*
|
|
69
|
+
* @function
|
|
70
|
+
* @param {string} collection - The collection to check.
|
|
71
|
+
*/
|
|
72
|
+
async checkCollection(collection){
|
|
73
|
+
return await this._request("checkCollection", { collection });
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Check if a collection exists.
|
|
78
|
+
*
|
|
79
|
+
* @function
|
|
80
|
+
* @param {string} collection - The name of the collection.
|
|
81
|
+
* @returns {boolean} True if the collection exists, false otherwise.
|
|
82
|
+
*/
|
|
83
|
+
async issetCollection(collection){
|
|
84
|
+
return await this._request("issetCollection", { collection });
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Add data to a database.
|
|
89
|
+
*
|
|
90
|
+
* @async
|
|
91
|
+
* @function
|
|
92
|
+
* @param {string} collection - The name of the collection.
|
|
93
|
+
* @param {Object} data - The data to add.
|
|
94
|
+
* @param {boolean} id_gen - Whether to generate an ID for the entry. Default is true.
|
|
95
|
+
* @returns {Promise<Object>} A Promise that resolves with the added data.
|
|
96
|
+
*/
|
|
97
|
+
async add(collection, data, id_gen=true){
|
|
98
|
+
return await this._request("add", { collection, data, id_gen });
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Find data in a database.
|
|
103
|
+
*
|
|
104
|
+
* @async
|
|
105
|
+
* @function
|
|
106
|
+
* @param {string} collection - Name of the database collection.
|
|
107
|
+
* @param {function|Object} search - The query. It can be an object or a function.
|
|
108
|
+
* @param {Object} context - The context object (for functions).
|
|
109
|
+
* @param {Object} options - The options for the search.
|
|
110
|
+
* @param {number} options.max - The maximum number of entries to return. Default is -1, meaning no limit.
|
|
111
|
+
* @param {boolean} options.reverse - Whether to reverse the order of returned entries. Default is false.
|
|
112
|
+
* @param {Object} findOpts - Update result object with findOpts options.
|
|
113
|
+
* @returns {Promise<Array<Object>>} A Promise that resolves with the matching data.
|
|
114
|
+
*/
|
|
115
|
+
async find(collection, search, context={}, options={}, findOpts={}){
|
|
116
|
+
if(typeof search === "function") search = search.toString();
|
|
117
|
+
return await this._request("find", { collection, search, options, context, findOpts });
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Find one data entry in a database.
|
|
122
|
+
*
|
|
123
|
+
* @async
|
|
124
|
+
* @function
|
|
125
|
+
* @param {string} collection - Name of the database collection.
|
|
126
|
+
* @param {function|Object} search - The query. It can be an object or a function.
|
|
127
|
+
* @param {Object} context - The context object (for functions).
|
|
128
|
+
* @param {Object} findOpts - Update result object with findOpts options.
|
|
129
|
+
* @returns {Promise<Object|null>} A Promise that resolves with the first matching data entry.
|
|
130
|
+
*/
|
|
131
|
+
async findOne(collection, search, context={}, findOpts={}){
|
|
132
|
+
if(typeof search === "function") search = search.toString();
|
|
133
|
+
return await this._request("findOne", { collection, search, context, findOpts });
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Update data in a database.
|
|
138
|
+
*
|
|
139
|
+
* @async
|
|
140
|
+
* @function
|
|
141
|
+
* @param {string} collection - Name of the database collection.
|
|
142
|
+
* @param {function|Object} search - The query. It can be an object or a function.
|
|
143
|
+
* @param {function|Object} arg - Update arguments.
|
|
144
|
+
* @param {Object} context - The context object (for functions).
|
|
145
|
+
* @returns {Promise<boolean>} A Promise that resolves when the data is updated.
|
|
146
|
+
*/
|
|
147
|
+
async update(collection, search, arg, context={}){
|
|
148
|
+
if(typeof search === "function") search = search.toString();
|
|
149
|
+
if(typeof arg === "function") arg = arg.toString();
|
|
150
|
+
return await this._request("update", { collection, search, arg, context });
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Update one data entry in a database.
|
|
155
|
+
*
|
|
156
|
+
* @async
|
|
157
|
+
* @function
|
|
158
|
+
* @param {string} collection - Name of the database collection.
|
|
159
|
+
* @param {function|Object} search - The query. It can be an object or a function.
|
|
160
|
+
* @param {function|Object} arg - The query.
|
|
161
|
+
* @param {Object} context - The context object (for functions).
|
|
162
|
+
* @returns {Promise<boolean>} A Promise that resolves when the data entry is updated.
|
|
163
|
+
*/
|
|
164
|
+
async updateOne(collection, search, arg, context={}){
|
|
165
|
+
if(typeof search === "function") search = search.toString();
|
|
166
|
+
if(typeof arg === "function") arg = arg.toString();
|
|
167
|
+
return await this._request("updateOne", { collection, search, arg, context });
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Remove data from a database.
|
|
172
|
+
*
|
|
173
|
+
* @async
|
|
174
|
+
* @function
|
|
175
|
+
* @param {string} collection - Name of the database collection.
|
|
176
|
+
* @param {function|Object} search - The query. It can be an object or a function.
|
|
177
|
+
* @param {Object} context - The context object (for functions).
|
|
178
|
+
* @returns {Promise<boolean>} A Promise that resolves when the data is removed.
|
|
179
|
+
*/
|
|
180
|
+
async remove(collection, search, context={}){
|
|
181
|
+
if(typeof search === "function") search = search.toString();
|
|
182
|
+
return await this._request("remove", { collection, search, context });
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Remove one data entry from a database.
|
|
187
|
+
*
|
|
188
|
+
* @async
|
|
189
|
+
* @function
|
|
190
|
+
* @param {string} collection - Name of the database collection.
|
|
191
|
+
* @param {function|Object} search - The query. It can be an object or a function.
|
|
192
|
+
* @param {Object} context - The context object (for functions).
|
|
193
|
+
* @returns {Promise<boolean>} A Promise that resolves when the data entry is removed.
|
|
194
|
+
*/
|
|
195
|
+
async removeOne(collection, search, context={}){
|
|
196
|
+
if(typeof search === "function") search = search.toString();
|
|
197
|
+
return await this._request("removeOne", { collection, search, context });
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
|
|
202
|
+
*
|
|
203
|
+
* @param {string} collection - Name of the database collection.
|
|
204
|
+
* @param {function|Object} search - The query. It can be an object or a function.
|
|
205
|
+
* @param {function|Object} arg - The search criteria for the update.
|
|
206
|
+
* @param {function|Object} add_arg - The arguments to be added to the new entry.
|
|
207
|
+
* @param {Object} context - The context object (for functions).
|
|
208
|
+
* @param {boolean} id_gen - Whether to generate an ID for the entry. Default is true.
|
|
209
|
+
* @return {Promise<boolean>} A Promise that resolves to `true` if the entry was updated, or `false` if it was added.
|
|
210
|
+
*/
|
|
211
|
+
async updateOneOrAdd(collection, search, arg, add_arg={}, context={}, id_gen=true){
|
|
212
|
+
if(typeof search === "function") search = search.toString();
|
|
213
|
+
if(typeof arg === "function") arg = arg.toString();
|
|
214
|
+
if(typeof add_arg === "function") add_arg = add_arg.toString();
|
|
215
|
+
return await this._request("updateOneOrAdd", { collection, search, arg, add_arg, id_gen, context });
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Removes a database collection from the file system.
|
|
220
|
+
*
|
|
221
|
+
* @param {string} collection - The name of the collection to remove.
|
|
222
|
+
* @return {void}
|
|
223
|
+
*/
|
|
224
|
+
removeDb(name){
|
|
225
|
+
return this._request("removeDb", { name });
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export default DataBaseRemote;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import got from "got";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A class representing a graph database.
|
|
5
|
+
* Uses a remote database.
|
|
6
|
+
* @class
|
|
7
|
+
*/
|
|
8
|
+
class GraphRemote{
|
|
9
|
+
/**
|
|
10
|
+
* Create a new database instance.
|
|
11
|
+
* @constructor
|
|
12
|
+
* @param {object} remote - The remote database object.
|
|
13
|
+
* @param {string} remote.name - The name of the database.
|
|
14
|
+
* @param {string} remote.folder - The folder path where the database files are stored.
|
|
15
|
+
* @param {string} remote.auth - The authentication token.
|
|
16
|
+
* @param {string} remote.url - The URL of the remote database.
|
|
17
|
+
*/
|
|
18
|
+
constructor(remote){
|
|
19
|
+
this.remote = remote;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Make a request to the remote database.
|
|
24
|
+
* @async
|
|
25
|
+
* @function
|
|
26
|
+
* @param {string} type - The type of the request.
|
|
27
|
+
* @param {object} data - The data to send with the request.
|
|
28
|
+
* @returns {Promise<*>} A Promise that resolves with the result of the request.
|
|
29
|
+
* @throws {Error} If the request failed.
|
|
30
|
+
*/
|
|
31
|
+
async _request(type, data){
|
|
32
|
+
data.db = this.remote.name;
|
|
33
|
+
const res = await got.post(this.remote.url + "/db/graph/" + type, {
|
|
34
|
+
json: data,
|
|
35
|
+
headers: {
|
|
36
|
+
"Authorization": this.remote.auth
|
|
37
|
+
},
|
|
38
|
+
responseType: "json"
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
if(res.body.err) throw new Error(res.body.msg);
|
|
42
|
+
return res.body.result;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Adds an edge between two nodes.
|
|
47
|
+
* @async
|
|
48
|
+
* @function
|
|
49
|
+
* @param {string} collection - The name of the collection.
|
|
50
|
+
* @param {string} nodeA - The first node.
|
|
51
|
+
* @param {string} nodeB - The second node.
|
|
52
|
+
* @returns {Promise<Object>} A promise that resolves with the added edge.
|
|
53
|
+
*/
|
|
54
|
+
async add(collection, nodeA, nodeB){
|
|
55
|
+
return await this._request("add", { collection, nodeA, nodeB });
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Removes an edge between two nodes.
|
|
60
|
+
* @async
|
|
61
|
+
* @function
|
|
62
|
+
* @param {string} collection - The name of the collection.
|
|
63
|
+
* @param {string} nodeA - The first node.
|
|
64
|
+
* @param {string} nodeB - The second node.
|
|
65
|
+
* @returns {Promise<boolean>} A promise that resolves when the edge is removed.
|
|
66
|
+
*/
|
|
67
|
+
async remove(collection, nodeA, nodeB){
|
|
68
|
+
return await this._request("remove", { collection, nodeA, nodeB });
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Finds all edges with either node equal to `node`.
|
|
73
|
+
* @async
|
|
74
|
+
* @function
|
|
75
|
+
* @param {string} collection - The name of the collection.
|
|
76
|
+
* @param {string} node - The node to search for.
|
|
77
|
+
* @returns {Promise<Object[]>} A promise that resolves with the found edges.
|
|
78
|
+
*/
|
|
79
|
+
async find(collection, node){
|
|
80
|
+
return await this._request("find", { collection, node });
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Finds one edge with either node equal to `nodeA` and the other equal to `nodeB`.
|
|
85
|
+
* @async
|
|
86
|
+
* @function
|
|
87
|
+
* @param {string} collection - The name of the collection.
|
|
88
|
+
* @param {string} nodeA - The first node.
|
|
89
|
+
* @param {string} nodeB - The second node.
|
|
90
|
+
* @returns {Promise<Object|null>} A promise that resolves with the found edge or null if not found.
|
|
91
|
+
*/
|
|
92
|
+
async findOne(collection, nodeA, nodeB){
|
|
93
|
+
return await this._request("findOne", { collection, nodeA, nodeB });
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Get all edges in the collection.
|
|
98
|
+
* @async
|
|
99
|
+
* @function
|
|
100
|
+
* @param {string} collection - The name of the collection.
|
|
101
|
+
* @returns {Promise<Object[]>} A promise that resolves with the found edges.
|
|
102
|
+
*/
|
|
103
|
+
async getAll(collection){
|
|
104
|
+
return await this._request("getAll", { collection });
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Get the names of all available databases.
|
|
109
|
+
*
|
|
110
|
+
* @function
|
|
111
|
+
* @returns {string[]} An array of database names.
|
|
112
|
+
*/
|
|
113
|
+
async getCollections(){
|
|
114
|
+
return await this._request("getCollections", {});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Check and create the specified collection if it doesn't exist.
|
|
119
|
+
*
|
|
120
|
+
* @function
|
|
121
|
+
* @param {string} collection - The collection to check.
|
|
122
|
+
*/
|
|
123
|
+
async checkCollection(collection){
|
|
124
|
+
return await this._request("checkCollection", { collection });
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Check if a collection exists.
|
|
129
|
+
*
|
|
130
|
+
* @function
|
|
131
|
+
* @param {string} collection - The name of the collection.
|
|
132
|
+
* @returns {boolean} True if the collection exists, false otherwise.
|
|
133
|
+
*/
|
|
134
|
+
async issetCollection(collection){
|
|
135
|
+
return await this._request("issetCollection", { collection });
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export default GraphRemote;
|