@wxn0brp/db 0.0.6 → 0.0.8

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 (144) hide show
  1. package/README.md +1 -1
  2. package/dist/cjs/CollectionManager.d.ts +11 -10
  3. package/dist/cjs/CollectionManager.js +4 -4
  4. package/dist/cjs/action.d.ts +10 -15
  5. package/dist/cjs/action.js +6 -12
  6. package/dist/cjs/client/database.d.ts +72 -0
  7. package/dist/cjs/{remote/client → client}/database.js +10 -10
  8. package/dist/cjs/database.d.ts +10 -18
  9. package/dist/cjs/database.js +9 -17
  10. package/dist/cjs/file/find.d.ts +3 -3
  11. package/dist/cjs/file/remove.d.ts +2 -2
  12. package/dist/cjs/file/update.d.ts +2 -2
  13. package/dist/cjs/file/update.js +8 -6
  14. package/dist/cjs/index.d.ts +8 -2
  15. package/dist/cjs/index.js +2 -2
  16. package/dist/cjs/relation.d.ts +3 -3
  17. package/dist/cjs/types/arg.d.ts +7 -1
  18. package/dist/cjs/types/searchOpts.d.ts +5 -4
  19. package/dist/cjs/types/types.d.ts +1 -0
  20. package/dist/cjs/types/updater.d.ts +26 -0
  21. package/dist/cjs/types/updater.js +5 -0
  22. package/dist/cjs/utils/updateObject.d.ts +4 -5
  23. package/dist/cjs/utils/updateObject.js +135 -5
  24. package/dist/esm/CollectionManager.d.ts +11 -10
  25. package/dist/esm/CollectionManager.js +4 -4
  26. package/dist/esm/action.d.ts +10 -15
  27. package/dist/esm/action.js +6 -12
  28. package/dist/esm/client/database.d.ts +72 -0
  29. package/dist/esm/{remote/client → client}/database.js +10 -10
  30. package/dist/esm/database.d.ts +10 -18
  31. package/dist/esm/database.js +9 -17
  32. package/dist/esm/file/find.d.ts +3 -3
  33. package/dist/esm/file/remove.d.ts +2 -2
  34. package/dist/esm/file/update.d.ts +2 -2
  35. package/dist/esm/file/update.js +11 -9
  36. package/dist/esm/index.d.ts +8 -2
  37. package/dist/esm/index.js +2 -2
  38. package/dist/esm/relation.d.ts +3 -3
  39. package/dist/esm/types/arg.d.ts +7 -1
  40. package/dist/esm/types/searchOpts.d.ts +5 -4
  41. package/dist/esm/types/types.d.ts +1 -0
  42. package/dist/esm/types/updater.d.ts +26 -0
  43. package/dist/esm/types/updater.js +4 -0
  44. package/dist/esm/utils/updateObject.d.ts +4 -5
  45. package/dist/esm/utils/updateObject.js +135 -5
  46. package/package.json +8 -11
  47. package/dist/cjs/remote/client/database.d.ts +0 -71
  48. package/dist/cjs/remote/server/auth.d.ts +0 -31
  49. package/dist/cjs/remote/server/auth.js +0 -99
  50. package/dist/cjs/remote/server/db.d.ts +0 -2
  51. package/dist/cjs/remote/server/db.js +0 -223
  52. package/dist/cjs/remote/server/function.d.ts +0 -2
  53. package/dist/cjs/remote/server/function.js +0 -89
  54. package/dist/cjs/remote/server/graph.d.ts +0 -2
  55. package/dist/cjs/remote/server/graph.js +0 -145
  56. package/dist/cjs/remote/server/gui/css/main.css +0 -130
  57. package/dist/cjs/remote/server/gui/css/scrool.css +0 -81
  58. package/dist/cjs/remote/server/gui/css/style.css +0 -61
  59. package/dist/cjs/remote/server/gui/favicon.svg +0 -12
  60. package/dist/cjs/remote/server/gui/html/data.html +0 -15
  61. package/dist/cjs/remote/server/gui/html/main.html +0 -46
  62. package/dist/cjs/remote/server/gui/html/nav.html +0 -25
  63. package/dist/cjs/remote/server/gui/html/popup.html +0 -51
  64. package/dist/cjs/remote/server/gui/index.html +0 -49
  65. package/dist/cjs/remote/server/gui/js/api.js +0 -166
  66. package/dist/cjs/remote/server/gui/js/index.js +0 -17
  67. package/dist/cjs/remote/server/gui/js/loadHTML.js +0 -16
  68. package/dist/cjs/remote/server/gui/js/popUp.js +0 -72
  69. package/dist/cjs/remote/server/gui/js/queryApi.js +0 -51
  70. package/dist/cjs/remote/server/gui/js/queryDb.js +0 -79
  71. package/dist/cjs/remote/server/gui/js/queryGraph.js +0 -144
  72. package/dist/cjs/remote/server/gui/js/render.js +0 -64
  73. package/dist/cjs/remote/server/gui/js/templates.js +0 -31
  74. package/dist/cjs/remote/server/gui/js/utils.js +0 -36
  75. package/dist/cjs/remote/server/gui/js/vars.js +0 -9
  76. package/dist/cjs/remote/server/gui/libs/core.js +0 -176
  77. package/dist/cjs/remote/server/gui/libs/d3.v7.min.js +0 -2
  78. package/dist/cjs/remote/server/gui/libs/handlebars.min.js +0 -29
  79. package/dist/cjs/remote/server/gui/libs/json5.min.js +0 -1
  80. package/dist/cjs/remote/server/index.d.ts +0 -1
  81. package/dist/cjs/remote/server/index.js +0 -66
  82. package/dist/cjs/remote/server/initDataBases.d.ts +0 -1
  83. package/dist/cjs/remote/server/initDataBases.js +0 -25
  84. package/dist/cjs/remote/server/pathUtils.d.ts +0 -1
  85. package/dist/cjs/remote/server/pathUtils.js +0 -12
  86. package/dist/cjs/remote/server/secret.d.ts +0 -1
  87. package/dist/cjs/remote/server/secret.js +0 -25
  88. package/dist/cjs/remote/serverMgmt/index.d.ts +0 -1
  89. package/dist/cjs/remote/serverMgmt/index.js +0 -87
  90. package/dist/esm/remote/client/database.d.ts +0 -71
  91. package/dist/esm/remote/server/auth.d.ts +0 -31
  92. package/dist/esm/remote/server/auth.js +0 -87
  93. package/dist/esm/remote/server/db.d.ts +0 -2
  94. package/dist/esm/remote/server/db.js +0 -218
  95. package/dist/esm/remote/server/function.d.ts +0 -2
  96. package/dist/esm/remote/server/function.js +0 -87
  97. package/dist/esm/remote/server/graph.d.ts +0 -2
  98. package/dist/esm/remote/server/graph.js +0 -143
  99. package/dist/esm/remote/server/gui/css/main.css +0 -130
  100. package/dist/esm/remote/server/gui/css/scrool.css +0 -81
  101. package/dist/esm/remote/server/gui/css/style.css +0 -61
  102. package/dist/esm/remote/server/gui/favicon.svg +0 -12
  103. package/dist/esm/remote/server/gui/html/data.html +0 -15
  104. package/dist/esm/remote/server/gui/html/main.html +0 -46
  105. package/dist/esm/remote/server/gui/html/nav.html +0 -25
  106. package/dist/esm/remote/server/gui/html/popup.html +0 -51
  107. package/dist/esm/remote/server/gui/index.html +0 -49
  108. package/dist/esm/remote/server/gui/js/api.js +0 -166
  109. package/dist/esm/remote/server/gui/js/index.js +0 -17
  110. package/dist/esm/remote/server/gui/js/loadHTML.js +0 -16
  111. package/dist/esm/remote/server/gui/js/popUp.js +0 -72
  112. package/dist/esm/remote/server/gui/js/queryApi.js +0 -51
  113. package/dist/esm/remote/server/gui/js/queryDb.js +0 -79
  114. package/dist/esm/remote/server/gui/js/queryGraph.js +0 -144
  115. package/dist/esm/remote/server/gui/js/render.js +0 -64
  116. package/dist/esm/remote/server/gui/js/templates.js +0 -31
  117. package/dist/esm/remote/server/gui/js/utils.js +0 -36
  118. package/dist/esm/remote/server/gui/js/vars.js +0 -9
  119. package/dist/esm/remote/server/gui/libs/core.js +0 -176
  120. package/dist/esm/remote/server/gui/libs/d3.v7.min.js +0 -2
  121. package/dist/esm/remote/server/gui/libs/handlebars.min.js +0 -29
  122. package/dist/esm/remote/server/gui/libs/json5.min.js +0 -1
  123. package/dist/esm/remote/server/index.d.ts +0 -1
  124. package/dist/esm/remote/server/index.js +0 -61
  125. package/dist/esm/remote/server/initDataBases.d.ts +0 -1
  126. package/dist/esm/remote/server/initDataBases.js +0 -20
  127. package/dist/esm/remote/server/pathUtils.d.ts +0 -1
  128. package/dist/esm/remote/server/pathUtils.js +0 -6
  129. package/dist/esm/remote/server/secret.d.ts +0 -1
  130. package/dist/esm/remote/server/secret.js +0 -19
  131. package/dist/esm/remote/serverMgmt/index.d.ts +0 -1
  132. package/dist/esm/remote/serverMgmt/index.js +0 -82
  133. /package/dist/cjs/{remote/client → client}/function.d.ts +0 -0
  134. /package/dist/cjs/{remote/client → client}/function.js +0 -0
  135. /package/dist/cjs/{remote/client → client}/graph.d.ts +0 -0
  136. /package/dist/cjs/{remote/client → client}/graph.js +0 -0
  137. /package/dist/cjs/{remote/client → client}/remote.d.ts +0 -0
  138. /package/dist/cjs/{remote/client → client}/remote.js +0 -0
  139. /package/dist/esm/{remote/client → client}/function.d.ts +0 -0
  140. /package/dist/esm/{remote/client → client}/function.js +0 -0
  141. /package/dist/esm/{remote/client → client}/graph.d.ts +0 -0
  142. /package/dist/esm/{remote/client → client}/graph.js +0 -0
  143. /package/dist/esm/{remote/client → client}/remote.d.ts +0 -0
  144. /package/dist/esm/{remote/client → client}/remote.js +0 -0
@@ -1,71 +0,0 @@
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;
@@ -1,31 +0,0 @@
1
- export declare function authMiddleware(req: any, res: any, next: any): Promise<any>;
2
- export declare function loginFunction(login: any, password: any): Promise<{
3
- err: boolean;
4
- msg: string;
5
- token?: undefined;
6
- } | {
7
- err: boolean;
8
- token: string;
9
- msg?: undefined;
10
- }>;
11
- export declare function generateToken(payload: any): Promise<string>;
12
- export declare function removeToken(token: any): Promise<any>;
13
- export declare function addUserAccess(login: any, password: any): Promise<{
14
- err: boolean;
15
- msg: string;
16
- user?: undefined;
17
- } | {
18
- err: boolean;
19
- user: any;
20
- msg?: undefined;
21
- }>;
22
- export declare function checkUserAccess(login: any, password: any): Promise<{
23
- err: boolean;
24
- msg: string;
25
- user?: undefined;
26
- } | {
27
- err: boolean;
28
- user: any;
29
- msg?: undefined;
30
- }>;
31
- export declare function removeUser(idOrLogin: any): Promise<any>;
@@ -1,87 +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
- const TOKEN_CACHE_TTL = parseInt(process.env.TOKEN_CACHE_TTL) || 900; // 15 minutes
6
- const cache = new NodeCache({ stdTTL: TOKEN_CACHE_TTL, checkperiod: TOKEN_CACHE_TTL });
7
- const secret = getSecret();
8
- export async function authMiddleware(req, res, next) {
9
- const token = req.headers["authorization"];
10
- if (!token) {
11
- return res.status(401).json({ err: true, msg: "Access denied. No token provided." });
12
- }
13
- if (cache.has(token)) {
14
- req.user = cache.get(token);
15
- return next();
16
- }
17
- try {
18
- const user = jwt.decode(token, secret);
19
- if (!user || !user._id) {
20
- return res.status(401).json({ err: true, msg: "Invalid token." });
21
- }
22
- const tokenD = await global.db.findOne("token", { token });
23
- if (!tokenD) {
24
- return res.status(401).json({ err: true, msg: "Invalid token." });
25
- }
26
- const userD = await global.db.findOne("user", { _id: user._id });
27
- if (!userD) {
28
- return res.status(401).json({ err: true, msg: "Invalid token." });
29
- }
30
- req.user = user;
31
- cache.set(token, user);
32
- next();
33
- }
34
- catch (err) {
35
- res.status(400).json({ err: true, msg: "An error occurred during authentication." });
36
- }
37
- }
38
- export async function loginFunction(login, password) {
39
- const { err, user } = await checkUserAccess(login, password);
40
- if (err) {
41
- return { err: true, msg: "Invalid login or password." };
42
- }
43
- const token = await generateToken({ _id: user._id });
44
- cache.set(token, user);
45
- return { err: false, token };
46
- }
47
- export async function generateToken(payload) {
48
- const token = jwt.encode(payload, secret);
49
- await global.db.add("token", { token }, false);
50
- return token;
51
- }
52
- export async function removeToken(token) {
53
- return await global.db.removeOne("token", { token });
54
- }
55
- export async function addUserAccess(login, password) {
56
- if (!/^[a-zA-Z0-9]+$/.test(login))
57
- return { err: true, msg: "Login can only contain letters and numbers." };
58
- if (login.length < 3 || login.length > 10)
59
- return { err: true, msg: "Login must be between 3 and 10 characters." };
60
- if (password.length < 8 || password.length > 300)
61
- return { err: true, msg: "Password must be between 8 and 300 characters." };
62
- const userExists = await global.db.findOne("user", { login });
63
- if (userExists)
64
- return { err: true, msg: "Login already exists." };
65
- password = generateHash(password);
66
- const user = await global.db.add("user", {
67
- login,
68
- password
69
- });
70
- return { err: false, user };
71
- }
72
- export async function checkUserAccess(login, password) {
73
- const user = await global.db.findOne("user", { login });
74
- if (!user)
75
- return { err: true, msg: "Invalid login or password." };
76
- const hash = generateHash(password);
77
- if (hash !== user.password)
78
- return { err: true, msg: "Invalid login or password." };
79
- delete user.password;
80
- return { err: false, user };
81
- }
82
- export async function removeUser(idOrLogin) {
83
- return await global.db.removeOne("user", { $or: [{ _id: idOrLogin }, { login: idOrLogin }] });
84
- }
85
- function generateHash(password) {
86
- return crypto.createHash("sha256").update(password).digest("hex");
87
- }
@@ -1,2 +0,0 @@
1
- declare const router: any;
2
- export default router;
@@ -1,218 +0,0 @@
1
- import { Router } from "express";
2
- import deserializeFunctions from "./function.js";
3
- import { isPathSafe } from "./pathUtils.js";
4
- const router = Router();
5
- router.use((req, res, next) => {
6
- if (req.dbType == "database")
7
- return next();
8
- return res.status(400).json({ err: true, msg: "Invalid data center type." });
9
- });
10
- router.post("/getCollections", async (req, res) => {
11
- try {
12
- const db = req.dataCenter;
13
- const result = await db.getCollections();
14
- res.json({ err: false, result });
15
- }
16
- catch (err) {
17
- console.error(err);
18
- res.status(500).json({ err: true, msg: err.message });
19
- }
20
- });
21
- router.post("/checkCollection", async (req, res) => {
22
- const { collection } = req.body;
23
- if (!collection)
24
- return res.status(400).json({ err: true, msg: "collection is required" });
25
- if (!isPathSafe(global.baseDir, collection))
26
- return res.status(400).json({ err: true, msg: "invalid collection" });
27
- try {
28
- const db = req.dataCenter;
29
- await db.checkCollection(collection);
30
- res.json({ err: false, result: true });
31
- }
32
- catch (err) {
33
- console.error(err);
34
- res.status(500).json({ err: true, msg: err.message });
35
- }
36
- });
37
- router.post("/issetCollection", async (req, res) => {
38
- const { collection } = req.body;
39
- if (!collection)
40
- return res.status(400).json({ err: true, msg: "collection is required" });
41
- if (!isPathSafe(global.baseDir, collection))
42
- return res.status(400).json({ err: true, msg: "invalid collection" });
43
- try {
44
- const db = req.dataCenter;
45
- const result = await db.issetCollection(collection);
46
- res.json({ err: false, result });
47
- }
48
- catch (err) {
49
- console.error(err);
50
- res.status(500).json({ err: true, msg: err.message });
51
- }
52
- });
53
- router.post("/add", async (req, res) => {
54
- const { collection, data } = req.body;
55
- if (!collection || !data)
56
- return res.status(400).json({ err: true, msg: "collection & data is required" });
57
- if (!isPathSafe(global.baseDir, collection))
58
- return res.status(400).json({ err: true, msg: "invalid collection" });
59
- try {
60
- const db = req.dataCenter;
61
- const result = await db.add(collection, data, req.body.id_gen || true);
62
- res.json({ err: false, result });
63
- }
64
- catch (err) {
65
- console.error(err);
66
- res.status(500).json({ err: true, msg: err.message });
67
- }
68
- });
69
- router.post("/find", async (req, res) => {
70
- const { collection, search, context, options, findOpts, keys } = req.body;
71
- if (!collection || !search)
72
- return res.status(400).json({ err: true, msg: "collection & search is required" });
73
- if (!isPathSafe(global.baseDir, collection))
74
- return res.status(400).json({ err: true, msg: "invalid collection" });
75
- if (keys && !Array.isArray(keys))
76
- return res.status(400).json({ err: true, msg: "keys must be an array" });
77
- try {
78
- const data = deserializeFunctions({ search, context, options, findOpts }, keys || []);
79
- const db = req.dataCenter;
80
- const result = await db.find(collection, data.search, data.context || {}, data.options || {}, data.findOpts || {});
81
- res.json({ err: false, result });
82
- }
83
- catch (err) {
84
- console.error(err);
85
- res.status(500).json({ err: true, msg: err.message });
86
- }
87
- });
88
- router.post("/findOne", async (req, res) => {
89
- const { collection, search, context, findOpts, keys } = req.body;
90
- if (!collection || !search)
91
- return res.status(400).json({ err: true, msg: "collection & search is required" });
92
- if (!isPathSafe(global.baseDir, collection))
93
- return res.status(400).json({ err: true, msg: "invalid collection" });
94
- if (keys && !Array.isArray(keys))
95
- return res.status(400).json({ err: true, msg: "keys must be an array" });
96
- try {
97
- const data = deserializeFunctions({ search, context, findOpts }, keys || []);
98
- const db = req.dataCenter;
99
- const result = await db.findOne(collection, data.search, data.context || {}, data.findOpts || {});
100
- res.json({ err: false, result });
101
- }
102
- catch (err) {
103
- console.error(err);
104
- res.status(500).json({ err: true, msg: err.message });
105
- }
106
- });
107
- router.post("/update", async (req, res) => {
108
- const { collection, search, arg, context, keys } = req.body;
109
- if (!collection || !search || !arg)
110
- return res.status(400).json({ err: true, msg: "collection & search & arg is required" });
111
- if (!isPathSafe(global.baseDir, collection))
112
- return res.status(400).json({ err: true, msg: "invalid collection" });
113
- if (keys && !Array.isArray(keys))
114
- return res.status(400).json({ err: true, msg: "keys must be an array" });
115
- try {
116
- const data = deserializeFunctions({ search, arg, context }, keys || []);
117
- const db = req.dataCenter;
118
- const result = await db.update(collection, data.search, data.arg, data.context || {});
119
- res.json({ err: false, result });
120
- }
121
- catch (err) {
122
- console.error(err);
123
- res.status(500).json({ err: true, msg: err.message });
124
- }
125
- });
126
- router.post("/updateOne", async (req, res) => {
127
- const { collection, search, arg, context, keys } = req.body;
128
- if (!collection || !search || !arg)
129
- return res.status(400).json({ err: true, msg: "collection & search & arg is required" });
130
- if (!isPathSafe(global.baseDir, collection))
131
- return res.status(400).json({ err: true, msg: "invalid collection" });
132
- if (keys && !Array.isArray(keys))
133
- return res.status(400).json({ err: true, msg: "keys must be an array" });
134
- try {
135
- const data = deserializeFunctions({ search, arg, context }, keys || []);
136
- const db = req.dataCenter;
137
- const result = await db.updateOne(collection, data.search, data.arg, data.context || {});
138
- res.json({ err: false, result });
139
- }
140
- catch (err) {
141
- console.error(err);
142
- res.status(500).json({ err: true, msg: err.message });
143
- }
144
- });
145
- router.post("/remove", async (req, res) => {
146
- const { collection, search, context, keys } = req.body;
147
- if (!collection || !search)
148
- return res.status(400).json({ err: true, msg: "collection & search is required" });
149
- if (!isPathSafe(global.baseDir, collection))
150
- return res.status(400).json({ err: true, msg: "invalid collection" });
151
- if (keys && !Array.isArray(keys))
152
- return res.status(400).json({ err: true, msg: "keys must be an array" });
153
- try {
154
- const data = deserializeFunctions({ search, context }, keys || []);
155
- const db = req.dataCenter;
156
- const result = await db.remove(collection, data.search, data.context || {});
157
- res.json({ err: false, result });
158
- }
159
- catch (err) {
160
- console.error(err);
161
- res.status(500).json({ err: true, msg: err.message });
162
- }
163
- });
164
- router.post("/removeOne", async (req, res) => {
165
- const { collection, search, context, keys } = req.body;
166
- if (!collection || !search)
167
- return res.status(400).json({ err: true, msg: "collection & search is required" });
168
- if (!isPathSafe(global.baseDir, collection))
169
- return res.status(400).json({ err: true, msg: "invalid collection" });
170
- if (keys && !Array.isArray(keys))
171
- return res.status(400).json({ err: true, msg: "keys must be an array" });
172
- try {
173
- const data = deserializeFunctions({ search, context }, keys || []);
174
- const db = req.dataCenter;
175
- const result = await db.removeOne(collection, data.search, data.context || {});
176
- res.json({ err: false, result });
177
- }
178
- catch (err) {
179
- console.error(err);
180
- res.status(500).json({ err: true, msg: err.message });
181
- }
182
- });
183
- router.post("/updateOneOrAdd", async (req, res) => {
184
- const { collection, search, arg, add_arg, context, id_gen, keys } = req.body;
185
- if (!collection || !search || !arg)
186
- return res.status(400).json({ err: true, msg: "collection & search & arg is required" });
187
- if (!isPathSafe(global.baseDir, collection))
188
- return res.status(400).json({ err: true, msg: "invalid collection" });
189
- if (keys && !Array.isArray(keys))
190
- return res.status(400).json({ err: true, msg: "keys must be an array" });
191
- try {
192
- const data = deserializeFunctions({ search, arg, add_arg, context }, keys || []);
193
- const db = req.dataCenter;
194
- const result = await db.updateOneOrAdd(collection, data.search, data.arg, data.add_arg || {}, data.context || {}, id_gen || true);
195
- res.json({ err: false, result });
196
- }
197
- catch (err) {
198
- console.error(err);
199
- res.status(500).json({ err: true, msg: err.message });
200
- }
201
- });
202
- router.post("/removeCollection", async (req, res) => {
203
- const { name } = req.body;
204
- if (!name)
205
- return res.status(400).json({ err: true, msg: "name is required" });
206
- if (!isPathSafe(global.baseDir, name))
207
- return res.status(400).json({ err: true, msg: "invalid name" });
208
- try {
209
- const db = req.dataCenter;
210
- const result = await db.removeCollection(name);
211
- res.json({ err: false, result });
212
- }
213
- catch (err) {
214
- console.error(err);
215
- res.status(500).json({ err: true, msg: err.message });
216
- }
217
- });
218
- export default router;
@@ -1,2 +0,0 @@
1
- declare function deserializeFunctions(data: any, keys: any): any;
2
- export default deserializeFunctions;
@@ -1,87 +0,0 @@
1
- import { createContext, runInContext } from "node:vm";
2
- const sandbox = {
3
- console: {
4
- log: console.log,
5
- },
6
- Math,
7
- Date,
8
- setTimeout: undefined,
9
- setInterval: undefined,
10
- setImmediate: undefined,
11
- clearTimeout: undefined,
12
- clearInterval: undefined,
13
- clearImmediate: undefined,
14
- require: undefined,
15
- module: undefined,
16
- exports: undefined,
17
- process: undefined,
18
- Buffer: undefined,
19
- };
20
- const context = createContext(sandbox);
21
- function changeStringToFunction(func) {
22
- try {
23
- const userFunction = runInContext(`(${func})`, context);
24
- return userFunction;
25
- }
26
- catch (e) {
27
- throw new Error("Invalid function");
28
- }
29
- }
30
- function deserializeFunctions(data, keys) {
31
- const setAtPath = (obj, path, value) => {
32
- const segments = path.split(".").map(segment => segment.replace(/\[dot\]/g, "."));
33
- let currentLevel = obj;
34
- for (let i = 0; i < segments.length - 1; i++) {
35
- const segment = segments[i];
36
- if (Array.isArray(currentLevel)) {
37
- const index = parseInt(segment, 10);
38
- if (!currentLevel[index])
39
- currentLevel[index] = {};
40
- currentLevel = currentLevel[index];
41
- }
42
- else if (!currentLevel[segment]) {
43
- currentLevel[segment] = {};
44
- currentLevel = currentLevel[segment];
45
- }
46
- else {
47
- currentLevel = currentLevel[segment];
48
- }
49
- }
50
- currentLevel[segments[segments.length - 1]] = value;
51
- };
52
- const getAtPath = (obj, path) => {
53
- const segments = path.split(".").map(key => key.replace(/\[dot\]/g, "."));
54
- return segments.reduce((acc, key) => {
55
- if (Array.isArray(acc)) {
56
- const index = parseInt(key, 10);
57
- return acc[index];
58
- }
59
- return acc && acc[key];
60
- }, obj);
61
- };
62
- keys.forEach((keyPath) => {
63
- const value = getAtPath(data, keyPath);
64
- if (typeof value === "string") {
65
- const fn = changeStringToFunction(value);
66
- setAtPath(data, keyPath, fn);
67
- }
68
- else if (typeof value === "object" && value !== null) {
69
- if (Array.isArray(value)) {
70
- value.forEach((item, index) => {
71
- if (typeof item === "string") {
72
- value[index] = changeStringToFunction(item);
73
- }
74
- });
75
- }
76
- else {
77
- Object.keys(value).forEach((key) => {
78
- if (typeof value[key] === "string") {
79
- value[key] = changeStringToFunction(value[key]);
80
- }
81
- });
82
- }
83
- }
84
- });
85
- return data;
86
- }
87
- export default deserializeFunctions;
@@ -1,2 +0,0 @@
1
- declare const router: any;
2
- export default router;
@@ -1,143 +0,0 @@
1
- import { Router } from "express";
2
- import { isPathSafe } from "./pathUtils.js";
3
- const router = Router();
4
- router.use((req, res, next) => {
5
- if (req.dbType == "graph")
6
- return next();
7
- return res.status(400).json({ err: true, msg: "Invalid data center type." });
8
- });
9
- router.post("/add", async (req, res) => {
10
- const { collection, a, b } = req.body;
11
- if (!collection || !a || !b)
12
- return res.status(400).json({ err: true, msg: "collection & a & b is required" });
13
- if (!isPathSafe(global.baseDir, collection))
14
- return res.status(400).json({ err: true, msg: "invalid collection" });
15
- const db = req.dataCenter;
16
- try {
17
- const result = await db.add(collection, a, b);
18
- res.json({ err: false, result });
19
- }
20
- catch (err) {
21
- res.status(500).json({ err: true, msg: err.message });
22
- }
23
- });
24
- router.post("/find", async (req, res) => {
25
- const { collection, node } = req.body;
26
- if (!collection || !node)
27
- return res.status(400).json({ err: true, msg: "collection & node is required" });
28
- if (!isPathSafe(global.baseDir, collection))
29
- return res.status(400).json({ err: true, msg: "invalid collection" });
30
- const db = req.dataCenter;
31
- try {
32
- const result = await db.find(collection, node);
33
- res.json({ err: false, result });
34
- }
35
- catch (err) {
36
- res.status(500).json({ err: true, msg: err.message });
37
- }
38
- });
39
- router.post("/findOne", async (req, res) => {
40
- const { collection, nodeA, nodeB } = req.body;
41
- if (!collection || !nodeA || !nodeB)
42
- return res.status(400).json({ err: true, msg: "collection & nodeA & nodeB is required" });
43
- if (!isPathSafe(global.baseDir, collection))
44
- return res.status(400).json({ err: true, msg: "invalid collection" });
45
- const db = req.dataCenter;
46
- try {
47
- const result = await db.findOne(collection, nodeA, nodeB);
48
- res.json({ err: false, result });
49
- }
50
- catch (err) {
51
- res.status(500).json({ err: true, msg: err.message });
52
- }
53
- });
54
- router.post("/getAll", async (req, res) => {
55
- const { collection } = req.body;
56
- if (!collection)
57
- return res.status(400).json({ err: true, msg: "collection is required" });
58
- if (!isPathSafe(global.baseDir, collection))
59
- return res.status(400).json({ err: true, msg: "invalid collection" });
60
- const db = req.dataCenter;
61
- try {
62
- const result = await db.getAll(collection);
63
- res.json({ err: false, result });
64
- }
65
- catch (err) {
66
- res.status(500).json({ err: true, msg: err.message });
67
- }
68
- });
69
- router.post("/remove", async (req, res) => {
70
- const { collection, nodeA, nodeB } = req.body;
71
- if (!collection || !nodeA || !nodeB)
72
- return res.status(400).json({ err: true, msg: "collection & nodeA & nodeB is required" });
73
- if (!isPathSafe(global.baseDir, collection))
74
- return res.status(400).json({ err: true, msg: "invalid collection" });
75
- const db = req.dataCenter;
76
- try {
77
- const result = await db.remove(collection, nodeA, nodeB);
78
- res.json({ err: false, result });
79
- }
80
- catch (err) {
81
- res.status(500).json({ err: true, msg: err.message });
82
- }
83
- });
84
- router.post("/getCollections", async (req, res) => {
85
- try {
86
- const db = req.dataCenter;
87
- const result = await db.getCollections();
88
- res.json({ err: false, result });
89
- }
90
- catch (err) {
91
- console.error(err);
92
- res.status(500).json({ err: true, msg: err.message });
93
- }
94
- });
95
- router.post("/checkCollection", async (req, res) => {
96
- const { collection } = req.body;
97
- if (!collection)
98
- return res.status(400).json({ err: true, msg: "collection is required" });
99
- if (!isPathSafe(global.baseDir, collection))
100
- return res.status(400).json({ err: true, msg: "invalid collection" });
101
- try {
102
- const db = req.dataCenter;
103
- await db.checkCollection(collection);
104
- res.json({ err: false, result: true });
105
- }
106
- catch (err) {
107
- console.error(err);
108
- res.status(500).json({ err: true, msg: err.message });
109
- }
110
- });
111
- router.post("/issetCollection", async (req, res) => {
112
- const { collection } = req.body;
113
- if (!collection)
114
- return res.status(400).json({ err: true, msg: "collection is required" });
115
- if (!isPathSafe(global.baseDir, collection))
116
- return res.status(400).json({ err: true, msg: "invalid collection" });
117
- try {
118
- const db = req.dataCenter;
119
- const result = await db.issetCollection(collection);
120
- res.json({ err: false, result });
121
- }
122
- catch (err) {
123
- console.error(err);
124
- res.status(500).json({ err: true, msg: err.message });
125
- }
126
- });
127
- router.post("/removeCollection", async (req, res) => {
128
- const { collection } = req.body;
129
- if (!collection)
130
- return res.status(400).json({ err: true, msg: "collection is required" });
131
- if (!isPathSafe(global.baseDir, collection))
132
- return res.status(400).json({ err: true, msg: "invalid collection" });
133
- try {
134
- const db = req.dataCenter;
135
- const result = await db.removeCollection(collection);
136
- res.json({ err: false, result });
137
- }
138
- catch (err) {
139
- console.error(err);
140
- res.status(500).json({ err: true, msg: err.message });
141
- }
142
- });
143
- export default router;