@wxn0brp/db 0.0.5 → 0.0.7

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.
Files changed (178) hide show
  1. package/README.md +4 -1
  2. package/dist/cjs/CollectionManager.d.ts +44 -0
  3. package/dist/cjs/CollectionManager.js +59 -0
  4. package/dist/cjs/action.d.ts +66 -0
  5. package/dist/cjs/action.js +193 -0
  6. package/dist/cjs/client/database.d.ts +72 -0
  7. package/dist/cjs/client/database.js +145 -0
  8. package/dist/cjs/client/function.d.ts +5 -0
  9. package/dist/cjs/client/function.js +32 -0
  10. package/dist/cjs/client/graph.d.ts +54 -0
  11. package/dist/cjs/client/graph.js +93 -0
  12. package/dist/cjs/client/remote.d.ts +16 -0
  13. package/dist/cjs/client/remote.js +2 -0
  14. package/dist/cjs/database.d.ts +69 -0
  15. package/dist/cjs/database.js +110 -0
  16. package/dist/cjs/executor.d.ts +28 -0
  17. package/{executor.js → dist/cjs/executor.js} +47 -54
  18. package/dist/cjs/file/find.d.ts +11 -0
  19. package/dist/cjs/file/find.js +80 -0
  20. package/dist/cjs/file/index.d.ts +3 -0
  21. package/dist/cjs/file/index.js +25 -0
  22. package/dist/cjs/file/remove.d.ts +7 -0
  23. package/dist/cjs/file/remove.js +61 -0
  24. package/dist/cjs/file/update.d.ts +7 -0
  25. package/dist/cjs/file/update.js +70 -0
  26. package/dist/cjs/file/utils.d.ts +8 -0
  27. package/dist/cjs/file/utils.js +23 -0
  28. package/dist/cjs/format.d.ts +18 -0
  29. package/dist/cjs/format.js +36 -0
  30. package/dist/cjs/gen.d.ts +5 -0
  31. package/dist/cjs/gen.js +78 -0
  32. package/dist/cjs/graph.d.ts +47 -0
  33. package/dist/cjs/graph.js +90 -0
  34. package/dist/cjs/index.d.ts +13 -0
  35. package/dist/cjs/index.js +18 -0
  36. package/dist/cjs/relation.d.ts +64 -0
  37. package/{relation.js → dist/cjs/relation.js} +26 -45
  38. package/dist/cjs/types/Id.d.ts +3 -0
  39. package/dist/cjs/types/Id.js +2 -0
  40. package/dist/cjs/types/arg.d.ts +12 -0
  41. package/dist/cjs/types/arg.js +2 -0
  42. package/dist/cjs/types/data.d.ts +4 -0
  43. package/dist/cjs/types/data.js +2 -0
  44. package/dist/cjs/types/options.d.ts +12 -0
  45. package/dist/cjs/types/options.js +2 -0
  46. package/dist/cjs/types/searchOpts.d.ts +61 -0
  47. package/dist/cjs/types/searchOpts.js +8 -0
  48. package/dist/cjs/types/types.d.ts +6 -0
  49. package/dist/cjs/types/types.js +2 -0
  50. package/dist/cjs/types/updater.d.ts +26 -0
  51. package/dist/cjs/types/updater.js +5 -0
  52. package/dist/cjs/utils/hasFields.d.ts +8 -0
  53. package/dist/cjs/utils/hasFields.js +22 -0
  54. package/dist/cjs/utils/hasFieldsAdvanced.d.ts +5 -0
  55. package/dist/cjs/utils/hasFieldsAdvanced.js +182 -0
  56. package/dist/cjs/utils/updateFindObject.d.ts +11 -0
  57. package/dist/cjs/utils/updateFindObject.js +32 -0
  58. package/dist/cjs/utils/updateObject.d.ts +7 -0
  59. package/dist/cjs/utils/updateObject.js +148 -0
  60. package/dist/esm/CollectionManager.d.ts +44 -0
  61. package/dist/esm/CollectionManager.js +57 -0
  62. package/dist/esm/action.d.ts +66 -0
  63. package/dist/esm/action.js +188 -0
  64. package/dist/esm/client/database.d.ts +72 -0
  65. package/dist/esm/client/database.js +140 -0
  66. package/dist/esm/client/function.d.ts +5 -0
  67. package/dist/esm/client/function.js +30 -0
  68. package/dist/esm/client/graph.d.ts +54 -0
  69. package/dist/esm/client/graph.js +88 -0
  70. package/dist/esm/client/remote.d.ts +16 -0
  71. package/dist/esm/client/remote.js +1 -0
  72. package/dist/esm/database.d.ts +69 -0
  73. package/dist/esm/database.js +105 -0
  74. package/dist/esm/executor.d.ts +28 -0
  75. package/dist/esm/executor.js +45 -0
  76. package/dist/esm/file/find.d.ts +11 -0
  77. package/dist/esm/file/find.js +73 -0
  78. package/dist/esm/file/index.d.ts +3 -0
  79. package/{file → dist/esm/file}/index.js +1 -1
  80. package/dist/esm/file/remove.d.ts +7 -0
  81. package/dist/esm/file/remove.js +56 -0
  82. package/dist/esm/file/update.d.ts +7 -0
  83. package/dist/esm/file/update.js +65 -0
  84. package/dist/esm/file/utils.d.ts +8 -0
  85. package/{file → dist/esm/file}/utils.js +3 -11
  86. package/dist/esm/format.d.ts +18 -0
  87. package/{format.js → dist/esm/format.js} +29 -29
  88. package/dist/esm/gen.d.ts +5 -0
  89. package/dist/esm/gen.js +75 -0
  90. package/dist/esm/graph.d.ts +47 -0
  91. package/dist/esm/graph.js +85 -0
  92. package/dist/esm/index.d.ts +13 -0
  93. package/dist/esm/index.js +7 -0
  94. package/dist/esm/relation.d.ts +64 -0
  95. package/dist/esm/relation.js +65 -0
  96. package/dist/esm/types/Id.d.ts +3 -0
  97. package/dist/esm/types/Id.js +1 -0
  98. package/dist/esm/types/arg.d.ts +12 -0
  99. package/dist/esm/types/arg.js +1 -0
  100. package/dist/esm/types/data.d.ts +4 -0
  101. package/dist/esm/types/data.js +1 -0
  102. package/dist/esm/types/options.d.ts +12 -0
  103. package/dist/esm/types/options.js +1 -0
  104. package/dist/esm/types/searchOpts.d.ts +61 -0
  105. package/dist/esm/types/searchOpts.js +7 -0
  106. package/dist/esm/types/types.d.ts +6 -0
  107. package/dist/esm/types/types.js +1 -0
  108. package/dist/esm/types/updater.d.ts +26 -0
  109. package/dist/esm/types/updater.js +4 -0
  110. package/dist/esm/utils/hasFields.d.ts +8 -0
  111. package/{utils → dist/esm/utils}/hasFields.js +4 -4
  112. package/dist/esm/utils/hasFieldsAdvanced.d.ts +5 -0
  113. package/dist/esm/utils/hasFieldsAdvanced.js +176 -0
  114. package/dist/esm/utils/updateFindObject.d.ts +11 -0
  115. package/{utils → dist/esm/utils}/updateFindObject.js +11 -16
  116. package/dist/esm/utils/updateObject.d.ts +7 -0
  117. package/dist/esm/utils/updateObject.js +145 -0
  118. package/package.json +52 -36
  119. package/CollectionManager.js +0 -119
  120. package/action.js +0 -258
  121. package/database.d.ts +0 -44
  122. package/database.js +0 -203
  123. package/docs/database.md +0 -140
  124. package/docs/graph.md +0 -86
  125. package/docs/relation.md +0 -51
  126. package/docs/remote.md +0 -30
  127. package/docs/remote_server.md +0 -35
  128. package/docs/search_opts.md +0 -227
  129. package/file/find.js +0 -89
  130. package/file/remove.js +0 -74
  131. package/file/update.js +0 -83
  132. package/gen.d.ts +0 -1
  133. package/gen.js +0 -97
  134. package/graph.d.ts +0 -27
  135. package/graph.js +0 -140
  136. package/index.d.ts +0 -7
  137. package/index.js +0 -15
  138. package/relation.d.ts +0 -23
  139. package/remote/client/database.d.ts +0 -41
  140. package/remote/client/database.js +0 -228
  141. package/remote/client/graph.d.ts +0 -31
  142. package/remote/client/graph.js +0 -148
  143. package/remote/server/auth.js +0 -100
  144. package/remote/server/db.js +0 -197
  145. package/remote/server/function.js +0 -43
  146. package/remote/server/graph.js +0 -136
  147. package/remote/server/gui/css/main.css +0 -130
  148. package/remote/server/gui/css/scrool.css +0 -81
  149. package/remote/server/gui/css/style.css +0 -61
  150. package/remote/server/gui/favicon.svg +0 -12
  151. package/remote/server/gui/html/data.html +0 -15
  152. package/remote/server/gui/html/main.html +0 -46
  153. package/remote/server/gui/html/nav.html +0 -25
  154. package/remote/server/gui/html/popup.html +0 -51
  155. package/remote/server/gui/index.html +0 -49
  156. package/remote/server/gui/js/api.js +0 -166
  157. package/remote/server/gui/js/index.js +0 -17
  158. package/remote/server/gui/js/loadHTML.js +0 -16
  159. package/remote/server/gui/js/popUp.js +0 -72
  160. package/remote/server/gui/js/queryApi.js +0 -51
  161. package/remote/server/gui/js/queryDb.js +0 -79
  162. package/remote/server/gui/js/queryGraph.js +0 -144
  163. package/remote/server/gui/js/render.js +0 -64
  164. package/remote/server/gui/js/templates.js +0 -31
  165. package/remote/server/gui/js/utils.js +0 -36
  166. package/remote/server/gui/js/vars.js +0 -9
  167. package/remote/server/gui/libs/core.js +0 -176
  168. package/remote/server/gui/libs/d3.v7.min.js +0 -2
  169. package/remote/server/gui/libs/handlebars.min.js +0 -29
  170. package/remote/server/gui/libs/json5.min.js +0 -1
  171. package/remote/server/index.js +0 -63
  172. package/remote/server/initDataBases.js +0 -20
  173. package/remote/server/pathUtils.js +0 -7
  174. package/remote/server/secret.js +0 -23
  175. package/remote/serverMgmt/index.js +0 -86
  176. package/test/hasFieldsAdvanced.test.js +0 -70
  177. package/utils/hasFieldsAdvanced.js +0 -184
  178. package/utils/updateObject.js +0 -15
@@ -1,148 +0,0 @@
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.auth - The authentication token.
15
- * @param {string} remote.url - The URL of the remote database.
16
- */
17
- constructor(remote){
18
- this.remote = remote;
19
- }
20
-
21
- /**
22
- * Make a request to the remote database.
23
- * @async
24
- * @function
25
- * @param {string} type - The type of the request.
26
- * @param {object} data - The data to send with the request.
27
- * @returns {Promise<*>} A Promise that resolves with the result of the request.
28
- * @throws {Error} If the request failed.
29
- */
30
- async _request(type, data){
31
- data.db = this.remote.name;
32
- const res = await got.post(this.remote.url + "/db/graph/" + type, {
33
- json: data,
34
- headers: {
35
- "Authorization": this.remote.auth
36
- },
37
- responseType: "json"
38
- });
39
-
40
- if(res.body.err) throw new Error(res.body.msg);
41
- return res.body.result;
42
- }
43
-
44
- /**
45
- * Adds an edge between two nodes.
46
- * @async
47
- * @function
48
- * @param {string} collection - The name of the collection.
49
- * @param {string} nodeA - The first node.
50
- * @param {string} nodeB - The second node.
51
- * @returns {Promise<Object>} A promise that resolves with the added edge.
52
- */
53
- async add(collection, nodeA, nodeB){
54
- return await this._request("add", { collection, nodeA, nodeB });
55
- }
56
-
57
- /**
58
- * Removes an edge between two nodes.
59
- * @async
60
- * @function
61
- * @param {string} collection - The name of the collection.
62
- * @param {string} nodeA - The first node.
63
- * @param {string} nodeB - The second node.
64
- * @returns {Promise<boolean>} A promise that resolves when the edge is removed.
65
- */
66
- async remove(collection, nodeA, nodeB){
67
- return await this._request("remove", { collection, nodeA, nodeB });
68
- }
69
-
70
- /**
71
- * Finds all edges with either node equal to `node`.
72
- * @async
73
- * @function
74
- * @param {string} collection - The name of the collection.
75
- * @param {string} node - The node to search for.
76
- * @returns {Promise<Object[]>} A promise that resolves with the found edges.
77
- */
78
- async find(collection, node){
79
- return await this._request("find", { collection, node });
80
- }
81
-
82
- /**
83
- * Finds one edge with either node equal to `nodeA` and the other equal to `nodeB`.
84
- * @async
85
- * @function
86
- * @param {string} collection - The name of the collection.
87
- * @param {string} nodeA - The first node.
88
- * @param {string} nodeB - The second node.
89
- * @returns {Promise<Object|null>} A promise that resolves with the found edge or null if not found.
90
- */
91
- async findOne(collection, nodeA, nodeB){
92
- return await this._request("findOne", { collection, nodeA, nodeB });
93
- }
94
-
95
- /**
96
- * Get all edges in the collection.
97
- * @async
98
- * @function
99
- * @param {string} collection - The name of the collection.
100
- * @returns {Promise<Object[]>} A promise that resolves with the found edges.
101
- */
102
- async getAll(collection){
103
- return await this._request("getAll", { collection });
104
- }
105
-
106
- /**
107
- * Get the names of all available databases.
108
- *
109
- * @function
110
- * @returns {string[]} An array of database names.
111
- */
112
- async getCollections(){
113
- return await this._request("getCollections", {});
114
- }
115
-
116
- /**
117
- * Check and create the specified collection if it doesn't exist.
118
- *
119
- * @function
120
- * @param {string} collection - The collection to check.
121
- */
122
- async checkCollection(collection){
123
- return await this._request("checkCollection", { collection });
124
- }
125
-
126
- /**
127
- * Check if a collection exists.
128
- *
129
- * @function
130
- * @param {string} collection - The name of the collection.
131
- * @returns {boolean} True if the collection exists, false otherwise.
132
- */
133
- async issetCollection(collection){
134
- return await this._request("issetCollection", { collection });
135
- }
136
-
137
- /**
138
- * Remove the specified collection.
139
- *
140
- * @function
141
- * @param {string} collection - The name of the collection.
142
- */
143
- removeCollection(collection){
144
- return this._request("removeCollection", { collection });
145
- }
146
- }
147
-
148
- export default GraphRemote;
@@ -1,100 +0,0 @@
1
- import jwt from "jwt-simple";
2
- import NodeCache from "node-cache";
3
- import getSecret from "./secret.js";
4
- import crypto from "crypto";
5
-
6
- const TOKEN_CACHE_TTL = process.env.TOKEN_CACHE_TTL || 900; // 15 minutes
7
- const cache = new NodeCache({ stdTTL: TOKEN_CACHE_TTL, checkperiod: TOKEN_CACHE_TTL });
8
- const secret = getSecret();
9
-
10
- export async function authMiddleware(req, res, next){
11
- const token = req.headers["authorization"];
12
- if(!token){
13
- return res.status(401).json({ err: true, msg: "Access denied. No token provided." });
14
- }
15
-
16
- if(cache.has(token)){
17
- req.user = cache.get(token);
18
- return next();
19
- }
20
-
21
- try{
22
- const user = jwt.decode(token, secret);
23
- if(!user || !user._id){
24
- return res.status(401).json({ err: true, msg: "Invalid token." });
25
- }
26
-
27
- const tokenD = await global.db.findOne("token", { token });
28
- if(!tokenD){
29
- return res.status(401).json({ err: true, msg: "Invalid token." });
30
- }
31
-
32
- const userD = await global.db.findOne("user", { _id: user._id });
33
- if(!userD){
34
- return res.status(401).json({ err: true, msg: "Invalid token." });
35
- }
36
-
37
- req.user = user;
38
- cache.set(token, user);
39
- next();
40
- }catch(err){
41
- res.status(400).json({ err: true, msg: "An error occurred during authentication." });
42
- }
43
- }
44
-
45
- export async function loginFunction(login, password){
46
- const { err, user } = await checkUserAccess(login, password);
47
- if(err){
48
- return { err: true, msg: "Invalid login or password." };
49
- }
50
-
51
- const token = await generateToken({ _id: user._id });
52
- cache.set(token, user);
53
- return { err: false, token };
54
- }
55
-
56
- export async function generateToken(payload){
57
- const token = jwt.encode(payload, secret);
58
- await global.db.add("token", { token }, false);
59
- return token;
60
- }
61
-
62
- export async function removeToken(token){
63
- return await global.db.removeOne("token", { token });
64
- }
65
-
66
- export async function addUserAccess(login, password){
67
- if(!/^[a-zA-Z0-9]+$/.test(login)) return { err: true, msg: "Login can only contain letters and numbers." };
68
- if(login.length < 3 || login.length > 10) return { err: true, msg: "Login must be between 3 and 10 characters." };
69
- if(password.length < 8 || password.length > 300) return { err: true, msg: "Password must be between 8 and 300 characters." };
70
-
71
- const userExists = await global.db.findOne("user", { login });
72
- if(userExists) return { err: true, msg: "Login already exists." };
73
-
74
- password = generateHash(password);
75
-
76
- const user = await global.db.add("user", {
77
- login,
78
- password
79
- });
80
- return { err: false, user };
81
- }
82
-
83
- export async function checkUserAccess(login, password){
84
- const user = await global.db.findOne("user", { login });
85
- if(!user) return { err: true, msg: "Invalid login or password." };
86
-
87
- const hash = generateHash(password);
88
- if(hash !== user.password) return { err: true, msg: "Invalid login or password." };
89
-
90
- delete user.password;
91
- return { err: false, user };
92
- }
93
-
94
- export async function removeUser(idOrLogin){
95
- return await global.db.removeOne("user", { $or: [{ _id: idOrLogin }, { login: idOrLogin }] });
96
- }
97
-
98
- function generateHash(password){
99
- return crypto.createHash("sha256").update(password).digest("hex");
100
- }
@@ -1,197 +0,0 @@
1
- import { Router } from "express";
2
- import parseParam from "./function.js";
3
- import { isPathSafe } from "./pathUtils.js";
4
- const router = Router();
5
-
6
- router.use((req, res, next) => {
7
- if(req.dbType == "database") return next();
8
- return res.status(400).json({ err: true, msg: "Invalid data center type." });
9
- });
10
-
11
- router.post("/getCollections", async (req, res) => {
12
- try{
13
- const db = req.dataCenter;
14
- const result = await db.getCollections();
15
- res.json({ err: false, result });
16
- }catch(err){
17
- console.error(err);
18
- res.status(500).json({ err: true, msg: err.message });
19
- }
20
- });
21
-
22
- router.post("/checkCollection", async (req, res) => {
23
- const { collection } = req.body;
24
- if(!collection) return res.status(400).json({ err: true, msg: "collection is required" });
25
- if(!isPathSafe(baseDir, collection)) return res.status(400).json({ err: true, msg: "invalid collection" });
26
-
27
- try{
28
- const db = req.dataCenter;
29
- await db.checkCollection(collection);
30
- res.json({ err: false, result: true });
31
- }catch(err){
32
- console.error(err);
33
- res.status(500).json({ err: true, msg: err.message });
34
- }
35
- });
36
-
37
- router.post("/issetCollection", async (req, res) => {
38
- const { collection } = req.body;
39
- if(!collection) return res.status(400).json({ err: true, msg: "collection is required" });
40
- if(!isPathSafe(baseDir, collection)) return res.status(400).json({ err: true, msg: "invalid collection" });
41
-
42
- try{
43
- const db = req.dataCenter;
44
- const result = await db.issetCollection(collection);
45
- res.json({ err: false, result });
46
- }catch(err){
47
- console.error(err);
48
- res.status(500).json({ err: true, msg: err.message });
49
- }
50
- });
51
-
52
- router.post("/add", async (req, res) => {
53
- const { collection, data } = req.body;
54
- if(!collection || !data) return res.status(400).json({ err: true, msg: "collection & data is required" });
55
- if(!isPathSafe(baseDir, collection)) return res.status(400).json({ err: true, msg: "invalid collection" });
56
-
57
- try{
58
- const db = req.dataCenter;
59
- const result = await db.add(collection, data, req.body.id_gen || true);
60
- res.json({ err: false, result });
61
- }catch(err){
62
- console.error(err);
63
- res.status(500).json({ err: true, msg: err.message });
64
- }
65
- });
66
-
67
- router.post("/find", async (req, res) => {
68
- const { collection, search, context, options, findOpts } = req.body;
69
- if(!collection || !search) return res.status(400).json({ err: true, msg: "collection & search is required" });
70
- if(!isPathSafe(baseDir, collection)) return res.status(400).json({ err: true, msg: "invalid collection" });
71
-
72
- try{
73
- const parsedSearch = parseParam(search);
74
- const db = req.dataCenter;
75
- const result = await db.find(collection, parsedSearch, context || {}, options || {}, findOpts || {});
76
- res.json({ err: false, result });
77
- }catch(err){
78
- console.error(err);
79
- res.status(500).json({ err: true, msg: err.message });
80
- }
81
- });
82
-
83
- router.post("/findOne", async (req, res) => {
84
- const { collection, search, context, findOpts } = req.body;
85
- if(!collection || !search) return res.status(400).json({ err: true, msg: "collection & search is required" });
86
- if(!isPathSafe(baseDir, collection)) return res.status(400).json({ err: true, msg: "invalid collection" });
87
-
88
- try{
89
- const parsedSearch = parseParam(search);
90
- const db = req.dataCenter;
91
- const result = await db.findOne(collection, parsedSearch, context || {}, findOpts || {});
92
- res.json({ err: false, result });
93
- }catch(err){
94
- console.error(err);
95
- res.status(500).json({ err: true, msg: err.message });
96
- }
97
- });
98
-
99
- router.post("/update", async (req, res) => {
100
- const { collection, search, arg, context } = req.body;
101
- if(!collection || !search || !arg) return res.status(400).json({ err: true, msg: "collection & search & arg is required" });
102
-
103
- try{
104
- const parsedSearch = parseParam(search);
105
- const parsedArg = parseParam(arg);
106
- const db = req.dataCenter;
107
- const result = await db.update(collection, parsedSearch, parsedArg, context || {});
108
- res.json({ err: false, result });
109
- }catch(err){
110
- console.error(err);
111
- res.status(500).json({ err: true, msg: err.message });
112
- }
113
- });
114
-
115
- router.post("/updateOne", async (req, res) => {
116
- const { collection, search, arg, context } = req.body;
117
- if(!collection || !search || !arg) return res.status(400).json({ err: true, msg: "collection & search & arg is required" });
118
- if(!isPathSafe(baseDir, collection)) return res.status(400).json({ err: true, msg: "invalid collection" });
119
-
120
- try{
121
- const parsedSearch = parseParam(search);
122
- const parsedArg = parseParam(arg);
123
- const db = req.dataCenter;
124
- const result = await db.updateOne(collection, parsedSearch, parsedArg, context || {});
125
- res.json({ err: false, result });
126
- }catch(err){
127
- console.error(err);
128
- res.status(500).json({ err: true, msg: err.message });
129
- }
130
- });
131
-
132
- router.post("/remove", async (req, res) => {
133
- const { collection, search, context } = req.body;
134
- if(!collection || !search) return res.status(400).json({ err: true, msg: "collection & search is required" });
135
- if(!isPathSafe(baseDir, collection)) return res.status(400).json({ err: true, msg: "invalid collection" });
136
-
137
- try{
138
- const parsedSearch = parseParam(search);
139
- const db = req.dataCenter;
140
- const result = await db.remove(collection, parsedSearch, context || {});
141
- res.json({ err: false, result });
142
- }catch(err){
143
- console.error(err);
144
- res.status(500).json({ err: true, msg: err.message });
145
- }
146
- });
147
-
148
- router.post("/removeOne", async (req, res) => {
149
- const { collection, search, context } = req.body;
150
- if(!collection || !search) return res.status(400).json({ err: true, msg: "collection & search is required" });
151
- if(!isPathSafe(baseDir, collection)) return res.status(400).json({ err: true, msg: "invalid collection" });
152
-
153
- try{
154
- const parsedSearch = parseParam(search);
155
- const db = req.dataCenter;
156
- const result = await db.removeOne(collection, parsedSearch, context || {});
157
- res.json({ err: false, result });
158
- }catch(err){
159
- console.error(err);
160
- res.status(500).json({ err: true, msg: err.message });
161
- }
162
- });
163
-
164
- router.post("/updateOneOrAdd", async (req, res) => {
165
- const { collection, search, arg, add_arg, context, id_gen } = req.body;
166
- if(!collection || !search || !arg) return res.status(400).json({ err: true, msg: "collection & search & arg is required" });
167
- if(!isPathSafe(baseDir, collection)) return res.status(400).json({ err: true, msg: "invalid collection" });
168
-
169
- try{
170
- const parsedSearch = parseParam(search);
171
- const parsedArg = parseParam(arg);
172
- const parsedAddArg = parseParam(add_arg);
173
- const db = req.dataCenter;
174
- const result = await db.updateOneOrAdd(collection, parsedSearch, parsedArg, parsedAddArg || {}, context || {}, id_gen || true);
175
- res.json({ err: false, result });
176
- }catch(err){
177
- console.error(err);
178
- res.status(500).json({ err: true, msg: err.message });
179
- }
180
- });
181
-
182
- router.post("/removeCollection", async (req, res) => {
183
- const { name } = req.body;
184
- if(!name) return res.status(400).json({ err: true, msg: "name is required" });
185
- if(!isPathSafe(baseDir, name)) return res.status(400).json({ err: true, msg: "invalid name" });
186
-
187
- try{
188
- const db = req.dataCenter;
189
- const result = await db.removeCollection(name);
190
- res.json({ err: false, result });
191
- }catch(err){
192
- console.error(err);
193
- res.status(500).json({ err: true, msg: err.message });
194
- }
195
- });
196
-
197
- export default router;
@@ -1,43 +0,0 @@
1
- import { createContext, runInContext } from "node:vm";
2
-
3
- const sandbox = {
4
- console: {
5
- log: console.log,
6
- },
7
- Math,
8
- Date,
9
- setTimeout: undefined,
10
- setInterval: undefined,
11
- setImmediate: undefined,
12
- clearTimeout: undefined,
13
- clearInterval: undefined,
14
- clearImmediate: undefined,
15
- require: undefined,
16
- module: undefined,
17
- exports: undefined,
18
- process: undefined,
19
- Buffer: undefined,
20
- };
21
-
22
- const context = createContext(sandbox);
23
-
24
- function changeStringToFunction(func){
25
- try{
26
- const userFunction = runInContext(`(${func})`, context);
27
- return userFunction;
28
- }catch(e){
29
- throw new Error("Invalid function");
30
- }
31
- }
32
-
33
- function parseParam(param){
34
- if(typeof param === "string"){
35
- const userFunction = changeStringToFunction(param);
36
- if(typeof userFunction !== "function") throw new Error("Invalid function");
37
- return userFunction;
38
- }else{
39
- return param;
40
- }
41
- }
42
-
43
- export default parseParam;
@@ -1,136 +0,0 @@
1
- import { Router } from "express";
2
- import { isPathSafe } from "./pathUtils.js";
3
- const router = Router();
4
-
5
- router.use((req, res, next) => {
6
- if(req.dbType == "graph") return next();
7
- return res.status(400).json({ err: true, msg: "Invalid data center type." });
8
- });
9
-
10
- router.post("/add", async (req, res) => {
11
- const { collection, a, b } = req.body;
12
- if(!collection || !a || !b) return res.status(400).json({ err: true, msg: "collection & a & b is required" });
13
- if(!isPathSafe(baseDir, collection)) return res.status(400).json({ err: true, msg: "invalid collection" });
14
-
15
- const db = req.dataCenter;
16
- try{
17
- const result = await db.add(collection, a, b);
18
- res.json({ err: false, result });
19
- }catch(err){
20
- res.status(500).json({ err: true, msg: err.message });
21
- }
22
- });
23
-
24
- router.post("/find", async (req, res) => {
25
- const { collection, node } = req.body;
26
- if(!collection || !node) return res.status(400).json({ err: true, msg: "collection & node is required" });
27
- if(!isPathSafe(baseDir, collection)) return res.status(400).json({ err: true, msg: "invalid collection" });
28
-
29
- const db = req.dataCenter;
30
- try{
31
- const result = await db.find(collection, node);
32
- res.json({ err: false, result });
33
- }catch(err){
34
- res.status(500).json({ err: true, msg: err.message });
35
- }
36
- });
37
-
38
- router.post("/findOne", async (req, res) => {
39
- const { collection, nodeA, nodeB } = req.body;
40
- if(!collection || !nodeA || !nodeB) return res.status(400).json({ err: true, msg: "collection & nodeA & nodeB is required" });
41
- if(!isPathSafe(baseDir, collection)) return res.status(400).json({ err: true, msg: "invalid collection" });
42
-
43
- const db = req.dataCenter;
44
- try{
45
- const result = await db.findOne(collection, nodeA, nodeB);
46
- res.json({ err: false, result });
47
- }catch(err){
48
- res.status(500).json({ err: true, msg: err.message });
49
- }
50
- });
51
-
52
- router.post("/getAll", async (req, res) => {
53
- const { collection } = req.body;
54
- if(!collection) return res.status(400).json({ err: true, msg: "collection is required" });
55
- if(!isPathSafe(baseDir, collection)) return res.status(400).json({ err: true, msg: "invalid collection" });
56
-
57
- const db = req.dataCenter;
58
- try{
59
- const result = await db.getAll(collection);
60
- res.json({ err: false, result });
61
- }catch(err){
62
- res.status(500).json({ err: true, msg: err.message });
63
- }
64
- });
65
-
66
- router.post("/remove", async (req, res) => {
67
- const { collection, nodeA, nodeB } = req.body;
68
- if(!collection || !nodeA || !nodeB) return res.status(400).json({ err: true, msg: "collection & nodeA & nodeB is required" });
69
- if(!isPathSafe(baseDir, collection)) return res.status(400).json({ err: true, msg: "invalid collection" });
70
-
71
- const db = req.dataCenter;
72
- try{
73
- const result = await db.remove(collection, nodeA, nodeB);
74
- res.json({ err: false, result });
75
- }catch(err){
76
- res.status(500).json({ err: true, msg: err.message });
77
- }
78
- });
79
-
80
- router.post("/getCollections", async (req, res) => {
81
- try{
82
- const db = req.dataCenter;
83
- const result = await db.getCollections();
84
- res.json({ err: false, result });
85
- }catch(err){
86
- console.error(err);
87
- res.status(500).json({ err: true, msg: err.message });
88
- }
89
- });
90
-
91
- router.post("/checkCollection", async (req, res) => {
92
- const { collection } = req.body;
93
- if(!collection) return res.status(400).json({ err: true, msg: "collection is required" });
94
- if(!isPathSafe(baseDir, collection)) return res.status(400).json({ err: true, msg: "invalid collection" });
95
-
96
- try{
97
- const db = req.dataCenter;
98
- await db.checkCollection(collection);
99
- res.json({ err: false, result: true });
100
- }catch(err){
101
- console.error(err);
102
- res.status(500).json({ err: true, msg: err.message });
103
- }
104
- });
105
-
106
- router.post("/issetCollection", async (req, res) => {
107
- const { collection } = req.body;
108
- if(!collection) return res.status(400).json({ err: true, msg: "collection is required" });
109
- if(!isPathSafe(baseDir, collection)) return res.status(400).json({ err: true, msg: "invalid collection" });
110
-
111
- try{
112
- const db = req.dataCenter;
113
- const result = await db.issetCollection(collection);
114
- res.json({ err: false, result });
115
- }catch(err){
116
- console.error(err);
117
- res.status(500).json({ err: true, msg: err.message });
118
- }
119
- });
120
-
121
- router.post("/removeCollection", async (req, res) => {
122
- const { collection } = req.body;
123
- if(!collection) return res.status(400).json({ err: true, msg: "collection is required" });
124
- if(!isPathSafe(baseDir, collection)) return res.status(400).json({ err: true, msg: "invalid collection" });
125
-
126
- try{
127
- const db = req.dataCenter;
128
- const result = await db.removeCollection(collection);
129
- res.json({ err: false, result });
130
- }catch(err){
131
- console.error(err);
132
- res.status(500).json({ err: true, msg: err.message });
133
- }
134
- });
135
-
136
- export default router;