@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
@@ -0,0 +1,66 @@
1
+ import { Arg, Search, Updater } from "./types/arg.js";
2
+ import { DbFindOpts, DbOpts, FindOpts } from "./types/options.js";
3
+ import { Context } from "./types/types";
4
+ import { SearchOptions } from "./types/searchOpts.js";
5
+ import Data from "./types/data.js";
6
+ /**
7
+ * A class representing database actions on files.
8
+ * @class
9
+ */
10
+ declare class dbActionC {
11
+ folder: string;
12
+ options: DbOpts;
13
+ /**
14
+ * Creates a new instance of dbActionC.
15
+ * @constructor
16
+ * @param folder - The folder where database files are stored.
17
+ * @param options - The options object.
18
+ */
19
+ constructor(folder: string, options: DbOpts);
20
+ _getCollectionPath(collection: string): string;
21
+ /**
22
+ * Get a list of available databases in the specified folder.
23
+ */
24
+ getCollections(): string[];
25
+ /**
26
+ * Check and create the specified collection if it doesn't exist.
27
+ */
28
+ checkCollection(collection: string): void;
29
+ /**
30
+ * Check if a collection exists.
31
+ */
32
+ issetCollection(collection: string): boolean;
33
+ /**
34
+ * Add a new entry to the specified database.
35
+ */
36
+ add(collection: string, arg: Arg, id_gen?: boolean): Promise<Arg>;
37
+ /**
38
+ * Find entries in the specified database based on search criteria.
39
+ */
40
+ find(collection: string, arg: Search, context?: Context, options?: DbFindOpts, findOpts?: FindOpts): Promise<any[]>;
41
+ /**
42
+ * Find the first matching entry in the specified database based on search criteria.
43
+ */
44
+ findOne(collection: string, arg: SearchOptions, context?: Context, findOpts?: FindOpts): Promise<Data>;
45
+ /**
46
+ * Update entries in the specified database based on search criteria and an updater function or object.
47
+ */
48
+ update(collection: string, arg: Search, updater: Updater, context?: {}): Promise<boolean>;
49
+ /**
50
+ * Update the first matching entry in the specified database based on search criteria and an updater function or object.
51
+ */
52
+ updateOne(collection: string, arg: Search, updater: Updater, context?: Context): Promise<boolean>;
53
+ /**
54
+ * Remove entries from the specified database based on search criteria.
55
+ */
56
+ remove(collection: string, arg: Search, context?: Context): Promise<boolean>;
57
+ /**
58
+ * Remove the first matching entry from the specified database based on search criteria.
59
+ */
60
+ removeOne(collection: string, arg: Search, context?: Context): Promise<boolean>;
61
+ /**
62
+ * Removes a database collection from the file system.
63
+ */
64
+ removeCollection(collection: string): void;
65
+ }
66
+ export default dbActionC;
@@ -0,0 +1,188 @@
1
+ import { existsSync, mkdirSync, readdirSync, appendFileSync, rmSync, writeFileSync, statSync } from "fs";
2
+ import gen from "./gen.js";
3
+ import { stringify } from "./format.js";
4
+ import { find as _find, findOne as _findOne, update as _update, remove as _remove } from "./file/index.js";
5
+ /**
6
+ * A class representing database actions on files.
7
+ * @class
8
+ */
9
+ class dbActionC {
10
+ folder;
11
+ options;
12
+ /**
13
+ * Creates a new instance of dbActionC.
14
+ * @constructor
15
+ * @param folder - The folder where database files are stored.
16
+ * @param options - The options object.
17
+ */
18
+ constructor(folder, options) {
19
+ this.folder = folder;
20
+ this.options = {
21
+ maxFileSize: 2 * 1024 * 1024, //2 MB
22
+ ...options,
23
+ };
24
+ if (!existsSync(folder))
25
+ mkdirSync(folder, { recursive: true });
26
+ }
27
+ _getCollectionPath(collection) {
28
+ return this.folder + "/" + collection + "/";
29
+ }
30
+ /**
31
+ * Get a list of available databases in the specified folder.
32
+ */
33
+ getCollections() {
34
+ const collections = readdirSync(this.folder, { recursive: true, withFileTypes: true })
35
+ .filter(dirent => dirent.isDirectory())
36
+ .map(dirent => {
37
+ if (dirent.parentPath === this.folder)
38
+ return dirent.name;
39
+ return dirent.parentPath.replace(this.folder + "/", "") + "/" + dirent.name;
40
+ });
41
+ return collections;
42
+ }
43
+ /**
44
+ * Check and create the specified collection if it doesn't exist.
45
+ */
46
+ checkCollection(collection) {
47
+ const cpath = this._getCollectionPath(collection);
48
+ if (!existsSync(cpath))
49
+ mkdirSync(cpath, { recursive: true });
50
+ }
51
+ /**
52
+ * Check if a collection exists.
53
+ */
54
+ issetCollection(collection) {
55
+ const path = this.folder + "/" + collection;
56
+ return existsSync(path);
57
+ }
58
+ /**
59
+ * Add a new entry to the specified database.
60
+ */
61
+ async add(collection, arg, id_gen = true) {
62
+ this.checkCollection(collection);
63
+ const cpath = this._getCollectionPath(collection);
64
+ const file = cpath + getLastFile(cpath, this.options.maxFileSize);
65
+ if (id_gen)
66
+ arg._id = arg._id || gen();
67
+ const data = stringify(arg);
68
+ appendFileSync(file, data + "\n");
69
+ return arg;
70
+ }
71
+ /**
72
+ * Find entries in the specified database based on search criteria.
73
+ */
74
+ async find(collection, arg, context = {}, options = {}, findOpts = {}) {
75
+ options.reverse = options.reverse || false;
76
+ options.max = options.max || -1;
77
+ this.checkCollection(collection);
78
+ const cpath = this._getCollectionPath(collection);
79
+ const files = getSortedFiles(cpath).map(f => f.f);
80
+ if (options.reverse)
81
+ files.reverse();
82
+ let datas = [];
83
+ let totalEntries = 0;
84
+ for (let f of files) {
85
+ let data = await _find(cpath + f, arg, context, findOpts);
86
+ if (options.reverse)
87
+ data.reverse();
88
+ if (options.max !== -1) {
89
+ if (totalEntries + data.length > options.max) {
90
+ let remainingEntries = options.max - totalEntries;
91
+ data = data.slice(0, remainingEntries);
92
+ totalEntries = options.max;
93
+ }
94
+ else {
95
+ totalEntries += data.length;
96
+ }
97
+ }
98
+ datas = datas.concat(data);
99
+ if (options.max !== -1 && totalEntries >= options.max)
100
+ break;
101
+ }
102
+ return datas;
103
+ }
104
+ /**
105
+ * Find the first matching entry in the specified database based on search criteria.
106
+ */
107
+ async findOne(collection, arg, context = {}, findOpts = {}) {
108
+ this.checkCollection(collection);
109
+ const cpath = this._getCollectionPath(collection);
110
+ const files = getSortedFiles(cpath).map(f => f.f);
111
+ for (let f of files) {
112
+ let data = await _findOne(cpath + f, arg, context, findOpts);
113
+ if (data)
114
+ return data;
115
+ }
116
+ return null;
117
+ }
118
+ /**
119
+ * Update entries in the specified database based on search criteria and an updater function or object.
120
+ */
121
+ async update(collection, arg, updater, context = {}) {
122
+ this.checkCollection(collection);
123
+ return await _update(this._getCollectionPath(collection), arg, updater, context);
124
+ }
125
+ /**
126
+ * Update the first matching entry in the specified database based on search criteria and an updater function or object.
127
+ */
128
+ async updateOne(collection, arg, updater, context = {}) {
129
+ this.checkCollection(collection);
130
+ return await _update(this._getCollectionPath(collection), arg, updater, context, true);
131
+ }
132
+ /**
133
+ * Remove entries from the specified database based on search criteria.
134
+ */
135
+ async remove(collection, arg, context = {}) {
136
+ this.checkCollection(collection);
137
+ return await _remove(this._getCollectionPath(collection), arg, context);
138
+ }
139
+ /**
140
+ * Remove the first matching entry from the specified database based on search criteria.
141
+ */
142
+ async removeOne(collection, arg, context = {}) {
143
+ this.checkCollection(collection);
144
+ return await _remove(this._getCollectionPath(collection), arg, context, true);
145
+ }
146
+ /**
147
+ * Removes a database collection from the file system.
148
+ */
149
+ removeCollection(collection) {
150
+ rmSync(this.folder + "/" + collection, { recursive: true, force: true });
151
+ }
152
+ }
153
+ /**
154
+ * Get the last file in the specified directory.
155
+ */
156
+ function getLastFile(path, maxFileSize = 1024 * 1024) {
157
+ if (!existsSync(path))
158
+ mkdirSync(path, { recursive: true });
159
+ const files = getSortedFiles(path);
160
+ if (files.length == 0) {
161
+ writeFileSync(path + "/1.db", "");
162
+ return "1.db";
163
+ }
164
+ const last = files[files.length - 1];
165
+ const info = path + "/" + last.f;
166
+ if (statSync(info).size < maxFileSize)
167
+ return last.f;
168
+ const num = last.i + 1;
169
+ writeFileSync(path + "/" + num + ".db", "");
170
+ return num + ".db";
171
+ }
172
+ /**
173
+ * Get all files in a directory sorted by name.
174
+ */
175
+ function getSortedFiles(path) {
176
+ const files = readdirSync(path).filter(file => file.endsWith(".db"));
177
+ if (files.length == 0)
178
+ return [];
179
+ const filesWithoutExt = files.map(file => parseInt(file.replace(".db", "")));
180
+ filesWithoutExt.sort();
181
+ return filesWithoutExt.map(file => {
182
+ return {
183
+ i: file,
184
+ f: file + ".db"
185
+ };
186
+ });
187
+ }
188
+ export default dbActionC;
@@ -0,0 +1,72 @@
1
+ import CollectionManager from "../CollectionManager.js";
2
+ import { Remote, RequestData } from "./remote.js";
3
+ import { Arg, Search, Updater } from "../types/arg";
4
+ import { DbFindOpts, FindOpts } from "../types/options.js";
5
+ import { Context } from "../types/types";
6
+ import Data from "../types/data.js";
7
+ /**
8
+ * Represents a database management class for performing CRUD operations.
9
+ * Uses a remote database.
10
+ * @class
11
+ */
12
+ declare class DataBaseRemote {
13
+ remote: Remote;
14
+ constructor(remote: Remote);
15
+ /**
16
+ * Make a request to the remote database.
17
+ */
18
+ _request<T>(type: string, data: RequestData): Promise<T>;
19
+ /**
20
+ * Create a new instance of a CollectionManager class.
21
+ */
22
+ c(collection: string): CollectionManager;
23
+ /**
24
+ * Get the names of all available databases.
25
+ */
26
+ getCollections(): Promise<string[]>;
27
+ /**
28
+ * Check and create the specified collection if it doesn't exist.
29
+ */
30
+ checkCollection(collection: string): Promise<boolean>;
31
+ /**
32
+ * Check if a collection exists.
33
+ */
34
+ issetCollection(collection: string): Promise<boolean>;
35
+ /**
36
+ * Add data to a database.
37
+ */
38
+ add<T = Data>(collection: string, data: Arg, id_gen?: boolean): Promise<T>;
39
+ /**
40
+ * Find data in a database.
41
+ */
42
+ find<T = Data>(collection: string, search: Search, context?: Context, options?: DbFindOpts, findOpts?: FindOpts): Promise<T[]>;
43
+ /**
44
+ * Find one data entry in a database.
45
+ */
46
+ findOne<T = Data>(collection: string, search: Search, context?: Context, findOpts?: FindOpts): Promise<T>;
47
+ /**
48
+ * Update data in a database.
49
+ */
50
+ update(collection: string, search: Search, updater: Updater, context?: Context): Promise<boolean>;
51
+ /**
52
+ * Update one data entry in a database.
53
+ */
54
+ updateOne(collection: string, search: Search, updater: Updater, context?: Context): Promise<boolean>;
55
+ /**
56
+ * Remove data from a database.
57
+ */
58
+ remove(collection: string, search: Search, context?: Context): Promise<boolean>;
59
+ /**
60
+ * Remove one data entry from a database.
61
+ */
62
+ removeOne(collection: string, search: Search, context?: Context): Promise<boolean>;
63
+ /**
64
+ * Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
65
+ */
66
+ updateOneOrAdd(collection: string, search: Search, arg: Search, add_arg?: Arg, context?: Context, id_gen?: boolean): Promise<boolean>;
67
+ /**
68
+ * Removes a database collection from the file system.
69
+ */
70
+ removeCollection(name: string): Promise<boolean>;
71
+ }
72
+ export default DataBaseRemote;
@@ -0,0 +1,140 @@
1
+ import got from "got";
2
+ import CollectionManager from "../CollectionManager.js";
3
+ import serializeFunctions from "./function.js";
4
+ /**
5
+ * Represents a database management class for performing CRUD operations.
6
+ * Uses a remote database.
7
+ * @class
8
+ */
9
+ class DataBaseRemote {
10
+ remote;
11
+ constructor(remote) {
12
+ this.remote = remote;
13
+ }
14
+ /**
15
+ * Make a request to the remote database.
16
+ */
17
+ async _request(type, data) {
18
+ data.db = this.remote.name;
19
+ const processed = serializeFunctions(data);
20
+ data = {
21
+ keys: processed.keys,
22
+ ...processed.data
23
+ };
24
+ const res = await got.post(this.remote.url + "/db/database/" + type, {
25
+ // @ts-ignore: Some jerk can't do the types correctly.
26
+ body: data,
27
+ headers: {
28
+ "Authorization": this.remote.auth
29
+ },
30
+ json: true,
31
+ responseType: "json"
32
+ });
33
+ if (res.body.err)
34
+ throw new Error(res.body.msg);
35
+ return res.body.result;
36
+ }
37
+ /**
38
+ * Create a new instance of a CollectionManager class.
39
+ */
40
+ c(collection) {
41
+ return new CollectionManager(this, collection);
42
+ }
43
+ /**
44
+ * Get the names of all available databases.
45
+ */
46
+ async getCollections() {
47
+ return await this._request("getCollections", {});
48
+ }
49
+ /**
50
+ * Check and create the specified collection if it doesn't exist.
51
+ */
52
+ async checkCollection(collection) {
53
+ return await this._request("checkCollection", { collection });
54
+ }
55
+ /**
56
+ * Check if a collection exists.
57
+ */
58
+ async issetCollection(collection) {
59
+ return await this._request("issetCollection", { collection });
60
+ }
61
+ /**
62
+ * Add data to a database.
63
+ */
64
+ async add(collection, data, id_gen = true) {
65
+ return await this._request("add", { collection, data, id_gen });
66
+ }
67
+ /**
68
+ * Find data in a database.
69
+ */
70
+ async find(collection, search, context = {}, options = {}, findOpts = {}) {
71
+ const searchStr = typeof search === "function" ? search.toString() : search;
72
+ const findOptsRemote = {};
73
+ if (findOpts.select)
74
+ findOptsRemote.select = findOpts.select;
75
+ if (findOpts.exclude)
76
+ findOptsRemote.exclude = findOpts.exclude;
77
+ if (findOpts.transform)
78
+ findOptsRemote.transform = findOpts.transform.toString();
79
+ return await this._request("find", { collection, search: searchStr, options, context, findOpts });
80
+ }
81
+ /**
82
+ * Find one data entry in a database.
83
+ */
84
+ async findOne(collection, search, context = {}, findOpts = {}) {
85
+ const searchStr = typeof search === "function" ? search.toString() : search;
86
+ const findOptsRemote = {};
87
+ if (findOpts.select)
88
+ findOptsRemote.select = findOpts.select;
89
+ if (findOpts.exclude)
90
+ findOptsRemote.exclude = findOpts.exclude;
91
+ if (findOpts.transform)
92
+ findOptsRemote.transform = findOpts.transform.toString();
93
+ return await this._request("findOne", { collection, search: searchStr, context, findOpts });
94
+ }
95
+ /**
96
+ * Update data in a database.
97
+ */
98
+ async update(collection, search, updater, context = {}) {
99
+ const searchStr = typeof search === "function" ? search.toString() : search;
100
+ const argStr = typeof updater === "function" ? updater.toString() : updater;
101
+ return await this._request("update", { collection, search: searchStr, arg: argStr, context });
102
+ }
103
+ /**
104
+ * Update one data entry in a database.
105
+ */
106
+ async updateOne(collection, search, updater, context = {}) {
107
+ const searchStr = typeof search === "function" ? search.toString() : search;
108
+ const argStr = typeof updater === "function" ? updater.toString() : updater;
109
+ return await this._request("updateOne", { collection, search: searchStr, arg: argStr, context });
110
+ }
111
+ /**
112
+ * Remove data from a database.
113
+ */
114
+ async remove(collection, search, context = {}) {
115
+ const searchStr = typeof search === "function" ? search.toString() : search;
116
+ return await this._request("remove", { collection, search: searchStr, context });
117
+ }
118
+ /**
119
+ * Remove one data entry from a database.
120
+ */
121
+ async removeOne(collection, search, context = {}) {
122
+ const searchStr = typeof search === "function" ? search.toString() : search;
123
+ return await this._request("removeOne", { collection, search: searchStr, context });
124
+ }
125
+ /**
126
+ * Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
127
+ */
128
+ async updateOneOrAdd(collection, search, arg, add_arg = {}, context = {}, id_gen = true) {
129
+ const searchStr = typeof search === "function" ? search.toString() : search;
130
+ const argStr = typeof arg === "function" ? arg.toString() : arg;
131
+ return await this._request("updateOneOrAdd", { collection, search: searchStr, arg: argStr, add_arg, id_gen, context });
132
+ }
133
+ /**
134
+ * Removes a database collection from the file system.
135
+ */
136
+ async removeCollection(name) {
137
+ return await this._request("removeCollection", { name });
138
+ }
139
+ }
140
+ export default DataBaseRemote;
@@ -0,0 +1,5 @@
1
+ declare function serializeFunctions(data: Object): {
2
+ data: Object;
3
+ keys: string[];
4
+ };
5
+ export default serializeFunctions;
@@ -0,0 +1,30 @@
1
+ function serializeFunctions(data) {
2
+ const functionKeys = [];
3
+ function convertFunctionToString(fn) {
4
+ return typeof fn === "function" ? fn.toString() : fn;
5
+ }
6
+ function traverseAndSerialize(obj, path = "") {
7
+ Object.keys(obj).forEach((key) => {
8
+ const value = obj[key];
9
+ const fullPath = path ? `${path}.${key.replace(/\./g, "[dot]")}` : key;
10
+ if (typeof value === "function") {
11
+ functionKeys.push(fullPath);
12
+ obj[key] = convertFunctionToString(value);
13
+ }
14
+ else if (Array.isArray(value)) {
15
+ value.forEach((item, index) => {
16
+ if (typeof item === "function") {
17
+ functionKeys.push(`${fullPath}[${index}]`);
18
+ value[index] = convertFunctionToString(item);
19
+ }
20
+ });
21
+ }
22
+ else if (typeof value === "object" && value !== null) {
23
+ traverseAndSerialize(value, fullPath);
24
+ }
25
+ });
26
+ }
27
+ traverseAndSerialize(data);
28
+ return { data, keys: functionKeys };
29
+ }
30
+ export default serializeFunctions;
@@ -0,0 +1,54 @@
1
+ import { Remote, RequestData } from "./remote";
2
+ /**
3
+ * A class representing a graph database.
4
+ * Uses a remote database.
5
+ * @class
6
+ */
7
+ declare class GraphRemote {
8
+ remote: Remote;
9
+ /**
10
+ * Create a new database instance.
11
+ */
12
+ constructor(remote: Remote);
13
+ /**
14
+ * Make a request to the remote database.
15
+ */
16
+ _request(type: string, data: RequestData): Promise<any>;
17
+ /**
18
+ * Adds an edge between two nodes.
19
+ */
20
+ add(collection: string, nodeA: string, nodeB: string): Promise<any>;
21
+ /**
22
+ * Removes an edge between two nodes.
23
+ */
24
+ remove(collection: string, nodeA: string, nodeB: string): Promise<any>;
25
+ /**
26
+ * Finds all edges with either node equal to `node`.
27
+ */
28
+ find(collection: string, node: string): Promise<any>;
29
+ /**
30
+ * Finds one edge with either node equal to `nodeA` and the other equal to `nodeB`.
31
+ */
32
+ findOne(collection: string, nodeA: string, nodeB: string): Promise<any>;
33
+ /**
34
+ * Get all edges in the collection.
35
+ */
36
+ getAll(collection: string): Promise<any>;
37
+ /**
38
+ * Get the names of all available databases.
39
+ */
40
+ getCollections(): Promise<any>;
41
+ /**
42
+ * Check and create the specified collection if it doesn't exist.
43
+ */
44
+ checkCollection(collection: string): Promise<any>;
45
+ /**
46
+ * Check if a collection exists.
47
+ */
48
+ issetCollection(collection: string): Promise<any>;
49
+ /**
50
+ * Remove the specified collection.
51
+ */
52
+ removeCollection(collection: string): Promise<any>;
53
+ }
54
+ export default GraphRemote;
@@ -0,0 +1,88 @@
1
+ import got from "got";
2
+ /**
3
+ * A class representing a graph database.
4
+ * Uses a remote database.
5
+ * @class
6
+ */
7
+ class GraphRemote {
8
+ remote;
9
+ /**
10
+ * Create a new database instance.
11
+ */
12
+ constructor(remote) {
13
+ this.remote = remote;
14
+ }
15
+ /**
16
+ * Make a request to the remote database.
17
+ */
18
+ async _request(type, data) {
19
+ data.db = this.remote.name;
20
+ const res = await got.post(this.remote.url + "/db/graph/" + type, {
21
+ // @ts-ignore
22
+ body: data,
23
+ headers: {
24
+ "Authorization": this.remote.auth
25
+ },
26
+ json: true,
27
+ responseType: "json"
28
+ });
29
+ if (res.body.err)
30
+ throw new Error(res.body.msg);
31
+ return res.body.result;
32
+ }
33
+ /**
34
+ * Adds an edge between two nodes.
35
+ */
36
+ async add(collection, nodeA, nodeB) {
37
+ return await this._request("add", { collection, nodeA, nodeB });
38
+ }
39
+ /**
40
+ * Removes an edge between two nodes.
41
+ */
42
+ async remove(collection, nodeA, nodeB) {
43
+ return await this._request("remove", { collection, nodeA, nodeB });
44
+ }
45
+ /**
46
+ * Finds all edges with either node equal to `node`.
47
+ */
48
+ async find(collection, node) {
49
+ return await this._request("find", { collection, node });
50
+ }
51
+ /**
52
+ * Finds one edge with either node equal to `nodeA` and the other equal to `nodeB`.
53
+ */
54
+ async findOne(collection, nodeA, nodeB) {
55
+ return await this._request("findOne", { collection, nodeA, nodeB });
56
+ }
57
+ /**
58
+ * Get all edges in the collection.
59
+ */
60
+ async getAll(collection) {
61
+ return await this._request("getAll", { collection });
62
+ }
63
+ /**
64
+ * Get the names of all available databases.
65
+ */
66
+ async getCollections() {
67
+ return await this._request("getCollections", {});
68
+ }
69
+ /**
70
+ * Check and create the specified collection if it doesn't exist.
71
+ */
72
+ async checkCollection(collection) {
73
+ return await this._request("checkCollection", { collection });
74
+ }
75
+ /**
76
+ * Check if a collection exists.
77
+ */
78
+ async issetCollection(collection) {
79
+ return await this._request("issetCollection", { collection });
80
+ }
81
+ /**
82
+ * Remove the specified collection.
83
+ */
84
+ removeCollection(collection) {
85
+ return this._request("removeCollection", { collection });
86
+ }
87
+ }
88
+ export default GraphRemote;
@@ -0,0 +1,16 @@
1
+ export interface Remote {
2
+ name: string;
3
+ url: string;
4
+ auth: string;
5
+ }
6
+ export interface RequestData {
7
+ collection?: string;
8
+ db?: string;
9
+ keys?: string[];
10
+ [key: string]: any;
11
+ }
12
+ export interface findOptsRemote {
13
+ select?: string[];
14
+ exclude?: string[];
15
+ transform?: string;
16
+ }
@@ -0,0 +1 @@
1
+ export {};