@wxn0brp/db 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (226) hide show
  1. package/README.md +3 -0
  2. package/dist/cjs/CollectionManager.d.ts +43 -0
  3. package/dist/cjs/CollectionManager.js +59 -0
  4. package/dist/cjs/action.d.ts +71 -0
  5. package/dist/cjs/action.js +199 -0
  6. package/dist/cjs/database.d.ts +77 -0
  7. package/dist/cjs/database.js +118 -0
  8. package/dist/cjs/executor.d.ts +28 -0
  9. package/{executor.js → dist/cjs/executor.js} +47 -54
  10. package/dist/cjs/file/find.d.ts +11 -0
  11. package/dist/cjs/file/find.js +80 -0
  12. package/dist/cjs/file/index.d.ts +3 -0
  13. package/dist/cjs/file/index.js +25 -0
  14. package/dist/cjs/file/remove.d.ts +7 -0
  15. package/dist/cjs/file/remove.js +61 -0
  16. package/dist/cjs/file/update.d.ts +7 -0
  17. package/dist/cjs/file/update.js +68 -0
  18. package/dist/cjs/file/utils.d.ts +8 -0
  19. package/dist/cjs/file/utils.js +23 -0
  20. package/dist/cjs/format.d.ts +18 -0
  21. package/dist/cjs/format.js +36 -0
  22. package/dist/cjs/gen.d.ts +5 -0
  23. package/dist/cjs/gen.js +78 -0
  24. package/dist/cjs/graph.d.ts +47 -0
  25. package/dist/cjs/graph.js +90 -0
  26. package/{index.d.ts → dist/cjs/index.d.ts} +2 -2
  27. package/dist/cjs/index.js +18 -0
  28. package/dist/cjs/relation.d.ts +64 -0
  29. package/{relation.js → dist/cjs/relation.js} +26 -45
  30. package/dist/cjs/remote/client/database.d.ts +71 -0
  31. package/dist/cjs/remote/client/database.js +145 -0
  32. package/dist/cjs/remote/client/function.d.ts +5 -0
  33. package/dist/cjs/remote/client/function.js +32 -0
  34. package/dist/cjs/remote/client/graph.d.ts +54 -0
  35. package/dist/cjs/remote/client/graph.js +93 -0
  36. package/dist/cjs/remote/client/remote.d.ts +16 -0
  37. package/dist/cjs/remote/client/remote.js +2 -0
  38. package/dist/cjs/remote/server/auth.d.ts +31 -0
  39. package/dist/cjs/remote/server/auth.js +99 -0
  40. package/dist/cjs/remote/server/db.d.ts +2 -0
  41. package/dist/cjs/remote/server/db.js +223 -0
  42. package/dist/cjs/remote/server/function.d.ts +2 -0
  43. package/dist/cjs/remote/server/function.js +89 -0
  44. package/dist/cjs/remote/server/graph.d.ts +2 -0
  45. package/dist/cjs/remote/server/graph.js +145 -0
  46. package/dist/cjs/remote/server/index.d.ts +1 -0
  47. package/dist/cjs/remote/server/index.js +66 -0
  48. package/dist/cjs/remote/server/initDataBases.d.ts +1 -0
  49. package/dist/cjs/remote/server/initDataBases.js +25 -0
  50. package/dist/cjs/remote/server/pathUtils.d.ts +1 -0
  51. package/dist/cjs/remote/server/pathUtils.js +12 -0
  52. package/dist/cjs/remote/server/secret.d.ts +1 -0
  53. package/dist/cjs/remote/server/secret.js +25 -0
  54. package/dist/cjs/remote/serverMgmt/index.d.ts +1 -0
  55. package/dist/cjs/remote/serverMgmt/index.js +87 -0
  56. package/dist/cjs/types/Id.d.ts +3 -0
  57. package/dist/cjs/types/Id.js +2 -0
  58. package/dist/cjs/types/arg.d.ts +6 -0
  59. package/dist/cjs/types/arg.js +2 -0
  60. package/dist/cjs/types/data.d.ts +4 -0
  61. package/dist/cjs/types/data.js +2 -0
  62. package/dist/cjs/types/options.d.ts +12 -0
  63. package/dist/cjs/types/options.js +2 -0
  64. package/dist/cjs/types/searchOpts.d.ts +61 -0
  65. package/dist/cjs/types/searchOpts.js +8 -0
  66. package/dist/cjs/types/types.d.ts +6 -0
  67. package/dist/cjs/types/types.js +2 -0
  68. package/dist/cjs/utils/hasFields.d.ts +8 -0
  69. package/dist/cjs/utils/hasFields.js +22 -0
  70. package/dist/cjs/utils/hasFieldsAdvanced.d.ts +5 -0
  71. package/dist/cjs/utils/hasFieldsAdvanced.js +182 -0
  72. package/dist/cjs/utils/updateFindObject.d.ts +11 -0
  73. package/dist/cjs/utils/updateFindObject.js +32 -0
  74. package/dist/cjs/utils/updateObject.d.ts +8 -0
  75. package/dist/cjs/utils/updateObject.js +18 -0
  76. package/dist/esm/CollectionManager.d.ts +43 -0
  77. package/dist/esm/CollectionManager.js +57 -0
  78. package/dist/esm/action.d.ts +71 -0
  79. package/dist/esm/action.js +194 -0
  80. package/dist/esm/database.d.ts +77 -0
  81. package/dist/esm/database.js +113 -0
  82. package/dist/esm/executor.d.ts +28 -0
  83. package/dist/esm/executor.js +45 -0
  84. package/dist/esm/file/find.d.ts +11 -0
  85. package/dist/esm/file/find.js +73 -0
  86. package/dist/esm/file/index.d.ts +3 -0
  87. package/{file → dist/esm/file}/index.js +1 -1
  88. package/dist/esm/file/remove.d.ts +7 -0
  89. package/dist/esm/file/remove.js +56 -0
  90. package/dist/esm/file/update.d.ts +7 -0
  91. package/dist/esm/file/update.js +63 -0
  92. package/dist/esm/file/utils.d.ts +8 -0
  93. package/{file → dist/esm/file}/utils.js +3 -11
  94. package/dist/esm/format.d.ts +18 -0
  95. package/{format.js → dist/esm/format.js} +29 -29
  96. package/dist/esm/gen.d.ts +5 -0
  97. package/dist/esm/gen.js +75 -0
  98. package/dist/esm/graph.d.ts +47 -0
  99. package/dist/esm/graph.js +85 -0
  100. package/dist/esm/index.d.ts +7 -0
  101. package/{index.js → dist/esm/index.js} +1 -9
  102. package/dist/esm/relation.d.ts +64 -0
  103. package/dist/esm/relation.js +65 -0
  104. package/dist/esm/remote/client/database.d.ts +71 -0
  105. package/dist/esm/remote/client/database.js +140 -0
  106. package/dist/esm/remote/client/function.d.ts +5 -0
  107. package/dist/esm/remote/client/function.js +30 -0
  108. package/dist/esm/remote/client/graph.d.ts +54 -0
  109. package/dist/esm/remote/client/graph.js +88 -0
  110. package/dist/esm/remote/client/remote.d.ts +16 -0
  111. package/dist/esm/remote/client/remote.js +1 -0
  112. package/dist/esm/remote/server/auth.d.ts +31 -0
  113. package/{remote → dist/esm/remote}/server/auth.js +31 -44
  114. package/dist/esm/remote/server/db.d.ts +2 -0
  115. package/dist/esm/remote/server/db.js +218 -0
  116. package/dist/esm/remote/server/function.d.ts +2 -0
  117. package/dist/esm/remote/server/function.js +87 -0
  118. package/dist/esm/remote/server/graph.d.ts +2 -0
  119. package/{remote → dist/esm/remote}/server/graph.js +62 -55
  120. package/dist/esm/remote/server/gui/css/main.css +130 -0
  121. package/dist/esm/remote/server/gui/css/scrool.css +81 -0
  122. package/dist/esm/remote/server/gui/css/style.css +61 -0
  123. package/dist/esm/remote/server/gui/favicon.svg +12 -0
  124. package/dist/esm/remote/server/gui/html/data.html +15 -0
  125. package/dist/esm/remote/server/gui/html/main.html +46 -0
  126. package/dist/esm/remote/server/gui/html/nav.html +25 -0
  127. package/dist/esm/remote/server/gui/html/popup.html +51 -0
  128. package/dist/esm/remote/server/gui/index.html +49 -0
  129. package/dist/esm/remote/server/gui/js/api.js +166 -0
  130. package/dist/esm/remote/server/gui/js/index.js +17 -0
  131. package/dist/esm/remote/server/gui/js/loadHTML.js +16 -0
  132. package/dist/esm/remote/server/gui/js/popUp.js +72 -0
  133. package/dist/esm/remote/server/gui/js/queryApi.js +51 -0
  134. package/dist/esm/remote/server/gui/js/queryDb.js +79 -0
  135. package/dist/esm/remote/server/gui/js/queryGraph.js +144 -0
  136. package/dist/esm/remote/server/gui/js/render.js +64 -0
  137. package/dist/esm/remote/server/gui/js/templates.js +31 -0
  138. package/dist/esm/remote/server/gui/js/utils.js +36 -0
  139. package/dist/esm/remote/server/gui/js/vars.js +9 -0
  140. package/dist/esm/remote/server/gui/libs/core.js +176 -0
  141. package/dist/esm/remote/server/gui/libs/d3.v7.min.js +2 -0
  142. package/dist/esm/remote/server/gui/libs/handlebars.min.js +29 -0
  143. package/dist/esm/remote/server/gui/libs/json5.min.js +1 -0
  144. package/dist/esm/remote/server/index.d.ts +1 -0
  145. package/dist/esm/remote/server/index.js +61 -0
  146. package/dist/esm/remote/server/initDataBases.d.ts +1 -0
  147. package/dist/esm/remote/server/initDataBases.js +20 -0
  148. package/dist/esm/remote/server/pathUtils.d.ts +1 -0
  149. package/{remote → dist/esm/remote}/server/pathUtils.js +2 -3
  150. package/dist/esm/remote/server/secret.d.ts +1 -0
  151. package/{remote → dist/esm/remote}/server/secret.js +3 -7
  152. package/dist/esm/remote/serverMgmt/index.d.ts +1 -0
  153. package/{remote → dist/esm/remote}/serverMgmt/index.js +27 -31
  154. package/dist/esm/types/Id.d.ts +3 -0
  155. package/dist/esm/types/Id.js +1 -0
  156. package/dist/esm/types/arg.d.ts +6 -0
  157. package/dist/esm/types/arg.js +1 -0
  158. package/dist/esm/types/data.d.ts +4 -0
  159. package/dist/esm/types/data.js +1 -0
  160. package/dist/esm/types/options.d.ts +12 -0
  161. package/dist/esm/types/options.js +1 -0
  162. package/dist/esm/types/searchOpts.d.ts +61 -0
  163. package/dist/esm/types/searchOpts.js +7 -0
  164. package/dist/esm/types/types.d.ts +6 -0
  165. package/dist/esm/types/types.js +1 -0
  166. package/dist/esm/utils/hasFields.d.ts +8 -0
  167. package/{utils → dist/esm/utils}/hasFields.js +4 -4
  168. package/dist/esm/utils/hasFieldsAdvanced.d.ts +5 -0
  169. package/dist/esm/utils/hasFieldsAdvanced.js +176 -0
  170. package/dist/esm/utils/updateFindObject.d.ts +11 -0
  171. package/{utils → dist/esm/utils}/updateFindObject.js +11 -16
  172. package/dist/esm/utils/updateObject.d.ts +8 -0
  173. package/{utils → dist/esm/utils}/updateObject.js +4 -4
  174. package/package.json +55 -36
  175. package/CollectionManager.js +0 -119
  176. package/action.js +0 -258
  177. package/database.d.ts +0 -44
  178. package/database.js +0 -203
  179. package/docs/database.md +0 -140
  180. package/docs/graph.md +0 -86
  181. package/docs/relation.md +0 -51
  182. package/docs/remote.md +0 -30
  183. package/docs/remote_server.md +0 -35
  184. package/docs/search_opts.md +0 -227
  185. package/file/find.js +0 -89
  186. package/file/remove.js +0 -74
  187. package/file/update.js +0 -83
  188. package/gen.d.ts +0 -1
  189. package/gen.js +0 -97
  190. package/graph.d.ts +0 -27
  191. package/graph.js +0 -140
  192. package/relation.d.ts +0 -23
  193. package/remote/client/database.d.ts +0 -41
  194. package/remote/client/database.js +0 -228
  195. package/remote/client/graph.d.ts +0 -31
  196. package/remote/client/graph.js +0 -148
  197. package/remote/server/db.js +0 -197
  198. package/remote/server/function.js +0 -43
  199. package/remote/server/index.js +0 -63
  200. package/remote/server/initDataBases.js +0 -20
  201. package/test/hasFieldsAdvanced.test.js +0 -70
  202. package/utils/hasFieldsAdvanced.js +0 -184
  203. /package/{remote → dist/cjs/remote}/server/gui/css/main.css +0 -0
  204. /package/{remote → dist/cjs/remote}/server/gui/css/scrool.css +0 -0
  205. /package/{remote → dist/cjs/remote}/server/gui/css/style.css +0 -0
  206. /package/{remote → dist/cjs/remote}/server/gui/favicon.svg +0 -0
  207. /package/{remote → dist/cjs/remote}/server/gui/html/data.html +0 -0
  208. /package/{remote → dist/cjs/remote}/server/gui/html/main.html +0 -0
  209. /package/{remote → dist/cjs/remote}/server/gui/html/nav.html +0 -0
  210. /package/{remote → dist/cjs/remote}/server/gui/html/popup.html +0 -0
  211. /package/{remote → dist/cjs/remote}/server/gui/index.html +0 -0
  212. /package/{remote → dist/cjs/remote}/server/gui/js/api.js +0 -0
  213. /package/{remote → dist/cjs/remote}/server/gui/js/index.js +0 -0
  214. /package/{remote → dist/cjs/remote}/server/gui/js/loadHTML.js +0 -0
  215. /package/{remote → dist/cjs/remote}/server/gui/js/popUp.js +0 -0
  216. /package/{remote → dist/cjs/remote}/server/gui/js/queryApi.js +0 -0
  217. /package/{remote → dist/cjs/remote}/server/gui/js/queryDb.js +0 -0
  218. /package/{remote → dist/cjs/remote}/server/gui/js/queryGraph.js +0 -0
  219. /package/{remote → dist/cjs/remote}/server/gui/js/render.js +0 -0
  220. /package/{remote → dist/cjs/remote}/server/gui/js/templates.js +0 -0
  221. /package/{remote → dist/cjs/remote}/server/gui/js/utils.js +0 -0
  222. /package/{remote → dist/cjs/remote}/server/gui/js/vars.js +0 -0
  223. /package/{remote → dist/cjs/remote}/server/gui/libs/core.js +0 -0
  224. /package/{remote → dist/cjs/remote}/server/gui/libs/d3.v7.min.js +0 -0
  225. /package/{remote → dist/cjs/remote}/server/gui/libs/handlebars.min.js +0 -0
  226. /package/{remote → dist/cjs/remote}/server/gui/libs/json5.min.js +0 -0
package/action.js DELETED
@@ -1,258 +0,0 @@
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
- /**
7
- * A class representing database actions on files.
8
- * @class
9
- */
10
- class dbActionC{
11
- /**
12
- * Creates a new instance of dbActionC.
13
- * @constructor
14
- * @param {string} folder - The folder where database files are stored.
15
- * @param {object} options - The options object.
16
- */
17
- constructor(folder, options){
18
- this.folder = folder;
19
- this.options = {
20
- maxFileSize: 2 * 1024 * 1024, //2 MB
21
- ...options,
22
- };
23
-
24
- if(!existsSync(folder)) mkdirSync(folder, { recursive: true });
25
- }
26
-
27
- _getCollectionPath(collection){
28
- return this.folder + "/" + collection + "/";
29
- }
30
-
31
- /**
32
- * Get a list of available databases in the specified folder.
33
- * @returns {string[]} An array of database names.
34
- */
35
- getCollections(){
36
- const collections = readdirSync(this.folder, { recursive: true, withFileTypes: true })
37
- .filter(dirent => dirent.isDirectory())
38
- .map(dirent => {
39
- if(dirent.parentPath === this.folder) return dirent.name;
40
- return dirent.parentPath.replace(this.folder + "/", "") + "/" + dirent.name
41
- });
42
-
43
- return collections;
44
- }
45
-
46
- /**
47
- * Check and create the specified collection if it doesn't exist.
48
- * @function
49
- * @param {string} collection - The collection to check.
50
- */
51
- checkCollection(collection){
52
- const cpath = this._getCollectionPath(collection);
53
- if(!existsSync(cpath)) mkdirSync(cpath, { recursive: true });
54
- }
55
-
56
- /**
57
- * Check if a collection exists.
58
- * @function
59
- * @param {string} collection - The name of the collection.
60
- * @returns {boolean} True if the collection exists, false otherwise.
61
- */
62
- issetCollection(collection){
63
- const path = this.folder + "/" + collection;
64
- return existsSync(path);
65
- }
66
-
67
- /**
68
- * Add a new entry to the specified database.
69
- * @async
70
- * @param {string} collection - The name of the collection.
71
- * @param {Object} arg - The data to be added to the database.
72
- * @param {boolean} id_gen - Whether to generate an ID for the entry. Default is true.
73
- * @returns {Promise<Object>} A Promise that resolves to the added data.
74
- */
75
- async add(collection, arg, id_gen=true){
76
- await this.checkCollection(collection);
77
- const cpath = this._getCollectionPath(collection);
78
- const file = cpath + getLastFile(cpath, this.options.maxFileSize);
79
-
80
- if(id_gen) arg._id = arg._id || gen();
81
- const data = stringify(arg);
82
- appendFileSync(file, data+"\n");
83
- return arg;
84
- }
85
-
86
- /**
87
- * Find entries in the specified database based on search criteria.
88
- * @async
89
- * @param {string} collection - The name of the collection.
90
- * @param {function|Object} arg - The search criteria. It can be a function or an object.
91
- * @param {Object} context - The context object (for functions).
92
- * @param {Object} options - The options for the search.
93
- * @param {number} options.max - The maximum number of entries to return. Default is -1, meaning no limit.
94
- * @param {boolean} options.reverse - Whether to reverse the order of returned entries. Default is false.
95
- * @param {Object} findOpts - Update result object with findOpts options.
96
- * @returns {Promise<Object[]>} A Promise that resolves to an array of matching entries.
97
- */
98
- async find(collection, arg, context={}, options={}, findOpts={}){
99
- options.reverse = options.reverse || false;
100
- options.max = options.max || -1;
101
-
102
- await this.checkCollection(collection);
103
- const cpath = this._getCollectionPath(collection);
104
- const files = getSortedFiles(cpath).map(f => f.f);
105
- if(options.reverse) files.reverse();
106
- let datas = [];
107
-
108
- let totalEntries = 0;
109
-
110
- for(let f of files){
111
- let data = await _find(cpath + f, arg, context, findOpts);
112
- if(options.reverse) data.reverse();
113
-
114
- if(options.max !== -1){
115
- if(totalEntries + data.length > options.max){
116
- let remainingEntries = options.max - totalEntries;
117
- data = data.slice(0, remainingEntries);
118
- totalEntries = options.max;
119
- }else{
120
- totalEntries += data.length;
121
- }
122
- }
123
-
124
- datas = datas.concat(data);
125
-
126
- if(options.max !== -1 && totalEntries >= options.max) break;
127
- }
128
- return datas;
129
- }
130
-
131
- /**
132
- * Find the first matching entry in the specified database based on search criteria.
133
- * @async
134
- * @param {string} collection - Name of the database collection.
135
- * @param {function|Object} arg - The search criteria. It can be a function or an object.
136
- * @param {Object} context - The context object (for functions).
137
- * @param {Object} findOpts - Update result object with findOpts options.
138
- * @returns {Promise<Object|null>} A Promise that resolves to the first matching entry or null if not found.
139
- */
140
- async findOne(collection, arg, context={}, findOpts={}){
141
- await this.checkCollection(collection);
142
- const cpath = this._getCollectionPath(collection);
143
- const files = getSortedFiles(cpath).map(f => f.f);
144
-
145
- for(let f of files){
146
- let data = await _findOne(cpath + f, arg, context, findOpts);
147
- if(data){
148
- return data;
149
- }
150
- }
151
- return null;
152
- }
153
-
154
- /**
155
- * Update entries in the specified database based on search criteria and an updater function or object.
156
- * @async
157
- * @param {string} collection - Name of the database collection.
158
- * @param {function|Object} arg - The search criteria. It can be a function or an object.
159
- * @param {function|Object} obj - The updater function or object.
160
- * @param {Object} context - The context object (for functions).
161
- * @returns {Promise<boolean>} A Promise that resolves to `true` if entries were updated, or `false` otherwise.
162
- */
163
- async update(collection, arg, obj, context={}){
164
- await this.checkCollection(collection);
165
- return await _update(this._getCollectionPath(collection), arg, obj, context);
166
- }
167
-
168
- /**
169
- * Update the first matching entry in the specified database based on search criteria and an updater function or object.
170
- * @async
171
- * @param {string} collection - Name of the database collection.
172
- * @param {function|Object} arg - The search criteria. It can be a function or an object.
173
- * @param {function|Object} obj - The updater function or object.
174
- * @param {Object} context - The context object (for functions).
175
- * @returns {Promise<boolean>} A Promise that resolves to `true` if one entry was updated, or `false` otherwise.
176
- */
177
- async updateOne(collection, arg, obj, context={}){
178
- await this.checkCollection(collection);
179
- return await _update(this._getCollectionPath(collection), arg, obj, context, true);
180
- }
181
-
182
- /**
183
- * Remove entries from the specified database based on search criteria.
184
- * @async
185
- * @param {string} collection - Name of the database collection.
186
- * @param {function|Object} arg - The search criteria. It can be a function or an object.
187
- * @param {Object} context - The context object (for functions).
188
- * @returns {Promise<boolean>} A Promise that resolves to `true` if entries were removed, or `false` otherwise.
189
- */
190
- async remove(collection, arg, context={}){
191
- await this.checkCollection(collection);
192
- return await _remove(this._getCollectionPath(collection), arg, context);
193
- }
194
-
195
- /**
196
- * Remove the first matching entry from the specified database based on search criteria.
197
- * @async
198
- * @param {string} collection - Name of the database collection.
199
- * @param {function|Object} arg - The search criteria. It can be a function or an object.
200
- * @param {Object} context - The context object (for functions).
201
- * @returns {Promise<boolean>} A Promise that resolves to `true` if one entry was removed, or `false` otherwise.
202
- */
203
- async removeOne(collection, arg, context={}){
204
- await this.checkCollection(collection);
205
- return await _remove(this._getCollectionPath(collection), arg, context, true);
206
- }
207
-
208
- /**
209
- * Removes a database collection from the file system.
210
- *
211
- * @param {string} collection - The name of the collection to remove.
212
- * @return {void}
213
- */
214
- removeCollection(collection){
215
- rmSync(this.folder + "/" + collection, { recursive: true, force: true });
216
- }
217
- }
218
-
219
- /**
220
- * Get the last file in the specified directory.
221
- * @param {string} path - The directory path.
222
- * @param {number} maxFileSize - The maximum file size in bytes. Default is 1 MB.
223
- * @returns {string} The name of the last file in the directory.
224
- */
225
- function getLastFile(path, maxFileSize=1024*1024){
226
- if(!existsSync(path)) mkdirSync(path, { recursive: true });
227
- const files = getSortedFiles(path);
228
-
229
- if(files.length == 0){
230
- writeFileSync(path+"/1.db", "");
231
- return "1.db";
232
- }
233
-
234
- const last = files[files.length-1];
235
- const info = path + "/" + last.f;
236
-
237
- if(statSync(info).size < maxFileSize) return last.f;
238
-
239
- const num = last.i + 1;
240
- writeFileSync(path + "/" + num + ".db", "");
241
- return num+".db";
242
- }
243
-
244
- /**
245
- * Get all files in a directory sorted by name.
246
- * @param {string} path - The path to the directory.
247
- * @return {string[]} An array of file names sorted by name.
248
- */
249
- function getSortedFiles(path){
250
- let files = readdirSync(path).filter(file => file.endsWith(".db"));
251
- if(files.length == 0) return [];
252
- files = files.map(file => parseInt(file.replace(".db", "")))
253
- files = files.sort();
254
- files = files.map(file => { return { i: file, f: file+".db" } });
255
- return files;
256
- }
257
-
258
- export default dbActionC;
package/database.d.ts DELETED
@@ -1,44 +0,0 @@
1
- import dbActionC from "./action.js";
2
- import executorC from "./executor.js";
3
- import CollectionManager from "./CollectionManager.js";
4
-
5
- declare class DataBase {
6
- constructor(folder: string, options?: {
7
- cacheThreshold?: number,
8
- cacheTTL?: number
9
- });
10
-
11
- dbAction: dbActionC;
12
- executor: executorC;
13
-
14
- c(collection: string): CollectionManager;
15
-
16
- getCollections(): Promise<string[]>;
17
-
18
- checkCollection(collection: string): Promise<void>;
19
-
20
- issetCollection(collection: string): Promise<boolean>;
21
-
22
- add(collection: string, data: object, id_gen?: boolean): Promise<object>;
23
-
24
- find(collection: string, search: object | Function, context?: object, options?: {
25
- max?: number,
26
- reverse?: boolean
27
- }, findOpts?: object): Promise<object[]>;
28
-
29
- findOne(collection: string, search: object | Function, context?: object, findOpts?: object): Promise<object | null>;
30
-
31
- update(collection: string, search: object | Function, arg: object | Function, context?: object): Promise<boolean>;
32
-
33
- updateOne(collection: string, search: object | Function, arg: object | Function, context?: object): Promise<boolean>;
34
-
35
- remove(collection: string, search: object | Function, context?: object): Promise<boolean>;
36
-
37
- removeOne(collection: string, search: object | Function, context?: object): Promise<boolean>;
38
-
39
- updateOneOrAdd(collection: string, search: object | Function, arg: object | Function, add_arg?: object, context?: object, id_gen?: boolean): Promise<boolean>;
40
-
41
- removeCollection(collection: string): void;
42
- }
43
-
44
- export default DataBase;
package/database.js DELETED
@@ -1,203 +0,0 @@
1
- import dbActionC from "./action.js";
2
- import executorC from "./executor.js";
3
- import CollectionManager from "./CollectionManager.js";
4
-
5
- /**
6
- * Represents a database management class for performing CRUD operations.
7
- * @class
8
- */
9
- class DataBase{
10
- /**
11
- * Create a new database instance.
12
- * @constructor
13
- * @param {string} folder - The folder path where the database files are stored.
14
- * @param {object} [options] - The options object.
15
- * @param {number} [options.maxFileSize=2*1024*1024] - The maximum size of a file in bytes. Default is 2 MB.
16
- * @param {number}
17
- */
18
- constructor(folder, options={}){
19
- this.dbAction = new dbActionC(folder, options);
20
- this.executor = new executorC();
21
- }
22
-
23
- /**
24
- * Create a new instance of a CollectionManager class.
25
- * @function
26
- * @param {string} collection - The name of the collection.
27
- * @returns {CollectionManager} A new instance of CollectionManager.
28
- */
29
- c(collection){
30
- return new CollectionManager(this, collection);
31
- }
32
-
33
- /**
34
- * Get the names of all available databases.
35
- *
36
- * @function
37
- * @returns {string[]} An array of database names.
38
- */
39
- async getCollections(){
40
- return await this.dbAction.getCollections();
41
- }
42
-
43
- /**
44
- * Check and create the specified collection if it doesn't exist.
45
- *
46
- * @function
47
- * @param {string} collection - The collection to check.
48
- */
49
- async checkCollection(collection){
50
- await this.dbAction.checkCollection(collection);
51
- }
52
-
53
- /**
54
- * Check if a collection exists.
55
- *
56
- * @function
57
- * @param {string} collection - The name of the collection.
58
- * @returns {boolean} True if the collection exists, false otherwise.
59
- */
60
- async issetCollection(collection){
61
- return await this.dbAction.issetCollection(collection);
62
- }
63
-
64
- /**
65
- * Add data to a database.
66
- *
67
- * @async
68
- * @function
69
- * @param {string} collection - The name of the collection.
70
- * @param {Object} data - The data to add.
71
- * @param {boolean} id_gen - Whether to generate an ID for the entry. Default is true.
72
- * @returns {Promise<Object>} A Promise that resolves with the added data.
73
- */
74
- async add(collection, data, id_gen=true){
75
- return await this.executor.addOp(this.dbAction.add.bind(this.dbAction), collection, data, id_gen);
76
- }
77
-
78
- /**
79
- * Find data in a database.
80
- *
81
- * @async
82
- * @function
83
- * @param {string} collection - Name of the database collection.
84
- * @param {function|Object} search - The query. It can be an object or a function.
85
- * @param {Object} context - The context object (for functions).
86
- * @param {Object} options - The options for the search.
87
- * @param {number} options.max - The maximum number of entries to return. Default is -1, meaning no limit.
88
- * @param {boolean} options.reverse - Whether to reverse the order of returned entries. Default is false.
89
- * @param {Object} findOpts - Update result object with findOpts options.
90
- * @returns {Promise<Array<Object>>} A Promise that resolves with the matching data.
91
- */
92
- async find(collection, search, context={}, options={}, findOpts={}){
93
- return await this.executor.addOp(this.dbAction.find.bind(this.dbAction), collection, search, context, options, findOpts);
94
- }
95
-
96
- /**
97
- * Find one data entry in a database.
98
- *
99
- * @async
100
- * @function
101
- * @param {string} collection - Name of the database collection.
102
- * @param {function|Object} search - The query. It can be an object or a function.
103
- * @param {Object} context - The context object (for functions).
104
- * @param {Object} findOpts - Update result object with findOpts options.
105
- * @returns {Promise<Object|null>} A Promise that resolves with the first matching data entry.
106
- */
107
- async findOne(collection, search, context={}, findOpts={}){
108
- return await this.executor.addOp(this.dbAction.findOne.bind(this.dbAction), collection, search, context, findOpts);
109
- }
110
-
111
- /**
112
- * Update data in a database.
113
- *
114
- * @async
115
- * @function
116
- * @param {string} collection - Name of the database collection.
117
- * @param {function|Object} search - The query. It can be an object or a function.
118
- * @param {function|Object} arg - Update arguments.
119
- * @param {Object} context - The context object (for functions).
120
- * @returns {Promise<boolean>} A Promise that resolves when the data is updated.
121
- */
122
- async update(collection, search, arg, context={}){
123
- return await this.executor.addOp(this.dbAction.update.bind(this.dbAction), collection, search, arg, context);
124
- }
125
-
126
- /**
127
- * Update one data entry in a database.
128
- *
129
- * @async
130
- * @function
131
- * @param {string} collection - Name of the database collection.
132
- * @param {function|Object} search - The query. It can be an object or a function.
133
- * @param {function|Object} arg - The query.
134
- * @param {Object} context - The context object (for functions).
135
- * @returns {Promise<boolean>} A Promise that resolves when the data entry is updated.
136
- */
137
- async updateOne(collection, search, arg, context={}){
138
- return await this.executor.addOp(this.dbAction.updateOne.bind(this.dbAction), collection, search, arg, context);
139
- }
140
-
141
- /**
142
- * Remove data from a database.
143
- *
144
- * @async
145
- * @function
146
- * @param {string} collection - Name of the database collection.
147
- * @param {function|Object} search - The query. It can be an object or a function.
148
- * @param {Object} context - The context object (for functions).
149
- * @returns {Promise<boolean>} A Promise that resolves when the data is removed.
150
- */
151
- async remove(collection, search, context={}){
152
- return await this.executor.addOp(this.dbAction.remove.bind(this.dbAction), collection, search, context);
153
- }
154
-
155
- /**
156
- * Remove one data entry from a database.
157
- *
158
- * @async
159
- * @function
160
- * @param {string} collection - Name of the database collection.
161
- * @param {function|Object} search - The query. It can be an object or a function.
162
- * @param {Object} context - The context object (for functions).
163
- * @returns {Promise<boolean>} A Promise that resolves when the data entry is removed.
164
- */
165
- async removeOne(collection, search, context={}){
166
- return await this.executor.addOp(this.dbAction.removeOne.bind(this.dbAction), collection, search, context);
167
- }
168
-
169
- /**
170
- * Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
171
- *
172
- * @param {string} collection - Name of the database collection.
173
- * @param {function|Object} search - The query. It can be an object or a function.
174
- * @param {function|Object} arg - The search criteria for the update.
175
- * @param {function|Object} add_arg - The arguments to be added to the new entry.
176
- * @param {Object} context - The context object (for functions).
177
- * @param {boolean} id_gen - Whether to generate an ID for the entry. Default is true.
178
- * @return {Promise<boolean>} A Promise that resolves to `true` if the entry was updated, or `false` if it was added.
179
- */
180
- async updateOneOrAdd(collection, search, arg, add_arg={}, context={}, id_gen=true){
181
- const res = await this.updateOne(collection, search, arg, context);
182
- if(!res){
183
- const assignData = [];
184
- if(typeof search === "object" && !Array.isArray(search)) assignData.push(search);
185
- if(typeof arg === "object" && !Array.isArray(arg)) assignData.push(arg);
186
- if(typeof add_arg === "object" && !Array.isArray(add_arg)) assignData.push(add_arg);
187
- await this.add(collection, Object.assign({}, ...assignData), id_gen);
188
- }
189
- return res;
190
- }
191
-
192
- /**
193
- * Removes a database collection from the file system.
194
- *
195
- * @param {string} collection - The name of the collection to remove.
196
- * @return {void}
197
- */
198
- removeCollection(collection){
199
- this.dbAction.removeCollection(collection);
200
- }
201
- }
202
-
203
- export default DataBase;
package/docs/database.md DELETED
@@ -1,140 +0,0 @@
1
- # DataBase Class Documentation
2
-
3
- This documentation provides a detailed overview of the `DataBase` class, designed for performing CRUD operations on database collections. The class uses the `dbActionC` module for file-based operations and `executorC` for managing execution tasks.
4
-
5
- ## Class: `DataBase`
6
-
7
- ### Constructor: `DataBase(folder, options={})`
8
- Creates a new instance of the `DataBase` class.
9
-
10
- - **Parameters:**
11
- - `folder` (`string`): The folder path where the database files are stored.
12
- - `options` (`object`): Optional configuration options.
13
- - `cacheThreshold` (`number`, default: 3): The threshold for caching entries.
14
- - `cacheTTL` (`number`, default: 300000): Time-to-live for cache entries in milliseconds (default: 5 minutes).
15
-
16
- ### Method: `c(collection)`
17
- Creates a new instance of the `CollectionManager` class for the specified collection.
18
-
19
- - **Parameters:**
20
- - `collection` (`string`): The name of the collection.
21
- - **Returns:**
22
- - `CollectionManager`: A new instance of `CollectionManager`.
23
-
24
- ### Method: `async getCollections()`
25
- Gets the names of all available collections in the database.
26
-
27
- - **Returns:**
28
- - `Promise<string[]>`: A promise that resolves with an array of collection names.
29
-
30
- ### Method: `async checkCollection(collection)`
31
- Checks and creates the specified collection if it doesn't exist.
32
-
33
- - **Parameters:**
34
- - `collection` (`string`): The name of the collection to check.
35
-
36
- ### Method: `async issetCollection(collection)`
37
- Checks if a collection exists.
38
-
39
- - **Parameters:**
40
- - `collection` (`string`): The name of the collection.
41
- - **Returns:**
42
- - `Promise<boolean>`: A promise that resolves to `true` if the collection exists, otherwise `false`.
43
-
44
- ### Method: `async add(collection, data, id_gen=true)`
45
- Adds data to a specified collection.
46
-
47
- - **Parameters:**
48
- - `collection` (`string`): The name of the collection.
49
- - `data` (`Object`): The data to add.
50
- - `id_gen` (`boolean`, default: true): Whether to generate an ID for the entry.
51
- - **Returns:**
52
- - `Promise<Object>`: A promise that resolves with the added data.
53
-
54
- ### Method: `async find(collection, search, context={}, options={}, findOpts={})`
55
- Finds data in a collection based on a query.
56
-
57
- - **Parameters:**
58
- - `collection` (`string`): The name of the collection.
59
- - `search` (`function|Object`): The search query.
60
- - `context` (`Object`): The context object (for functions).
61
- - `options` (`Object`): Search options.
62
- - `max` (`number`, default: -1): Maximum number of entries to return.
63
- - `reverse` (`boolean`, default: false): Whether to reverse the search results.
64
- - `findOpts` (`Object`): Options for updating the search result.
65
- - **Returns:**
66
- - `Promise<Array<Object>>`: A promise that resolves with the matching data.
67
-
68
- ### Method: `async findOne(collection, search, context={}, findOpts={})`
69
- Finds one matching entry in a collection.
70
-
71
- - **Parameters:**
72
- - `collection` (`string`): The name of the collection.
73
- - `search` (`function|Object`): The search query.
74
- - `context` (`Object`): The context object (for functions).
75
- - `findOpts` (`Object`): Options for updating the search result.
76
- - **Returns:**
77
- - `Promise<Object|null>`: A promise that resolves with the found entry, or `null` if no match is found.
78
-
79
- ### Method: `async update(collection, search, arg, context={})`
80
- Updates data in a collection.
81
-
82
- - **Parameters:**
83
- - `collection` (`string`): The name of the collection.
84
- - `search` (`function|Object`): The search query.
85
- - `arg` (`function|Object`): Update arguments.
86
- - `context` (`Object`): The context object (for functions).
87
- - **Returns:**
88
- - `Promise<boolean>`: A promise that resolves when the data is updated.
89
-
90
- ### Method: `async updateOne(collection, search, arg, context={})`
91
- Updates one entry in a collection.
92
-
93
- - **Parameters:**
94
- - `collection` (`string`): The name of the collection.
95
- - `search` (`function|Object`): The search query.
96
- - `arg` (`function|Object`): Update arguments.
97
- - `context` (`Object`): The context object (for functions).
98
- - **Returns:**
99
- - `Promise<boolean>`: A promise that resolves when the data is updated.
100
-
101
- ### Method: `async updateOneOrAdd(collection, search, arg, add_arg={}, context={}, id_gen=true)`
102
- Updates one entry or adds a new one if it doesn't exist.
103
-
104
- - **Parameters:**
105
- - `collection` (`string`): The name of the collection.
106
- - `search` (`function|Object`): The search query.
107
- - `arg` (`function|Object`): Update arguments.
108
- - `add_arg` (`function|Object`): Data to add if no match is found.
109
- - `context` (`Object`): The context object (for functions).
110
- - `id_gen` (`boolean`, default: true): Whether to generate an ID for the new entry.
111
- - **Returns:**
112
- - `Promise<boolean>`: A promise that resolves to `true` if the entry was updated, otherwise `false`.
113
-
114
- ### Method: `async remove(collection, search, context={})`
115
- Removes data from a collection.
116
-
117
- - **Parameters:**
118
- - `collection` (`string`): The name of the collection.
119
- - `search` (`function|Object`): The search query.
120
- - `context` (`Object`): The context object (for functions).
121
- - **Returns:**
122
- - `Promise<boolean>`: A promise that resolves when the data is removed.
123
-
124
- ### Method: `async removeOne(collection, search, context={})`
125
- Removes one entry from a collection.
126
-
127
- - **Parameters:**
128
- - `collection` (`string`): The name of the collection.
129
- - `search` (`function|Object`): The search query.
130
- - `context` (`Object`): The context object (for functions).
131
- - **Returns:**
132
- - `Promise<boolean>`: A promise that resolves when the entry is removed.
133
-
134
- ### Method: `removeCollection(collection)`
135
- Removes the specified collection from the database file system.
136
-
137
- - **Parameters:**
138
- - `collection` (`string`): The name of the collection to remove.
139
- - **Returns:**
140
- - `void`