@vandenberghinc/volt 1.1.5 → 1.1.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 (287) hide show
  1. package/backend/dist/cjs/database.d.ts +41 -68
  2. package/backend/dist/cjs/database.js +127 -76
  3. package/backend/dist/cjs/endpoint.d.ts +23 -9
  4. package/backend/dist/cjs/endpoint.js +98 -21
  5. package/backend/dist/cjs/frontend.d.ts +0 -2
  6. package/backend/dist/cjs/frontend.js +9 -9
  7. package/backend/dist/cjs/image_endpoint.d.ts +3 -1
  8. package/backend/dist/cjs/image_endpoint.js +2 -1
  9. package/backend/dist/cjs/payments/paddle.js +10 -2
  10. package/backend/dist/cjs/plugins/css.d.ts +6 -5
  11. package/backend/dist/cjs/plugins/css.js +32 -7
  12. package/backend/dist/cjs/plugins/ts/compiler.d.ts +6 -1
  13. package/backend/dist/cjs/plugins/ts/compiler.js +26 -2
  14. package/backend/dist/cjs/plugins/ts/preprocessing.js +5 -3
  15. package/backend/dist/cjs/server.d.ts +7 -13
  16. package/backend/dist/cjs/server.js +184 -303
  17. package/backend/dist/cjs/status.d.ts +1 -0
  18. package/backend/dist/cjs/status.js +2 -1
  19. package/backend/dist/cjs/stream.d.ts +5 -3
  20. package/backend/dist/cjs/stream.js +13 -4
  21. package/backend/dist/cjs/users.d.ts +1 -1
  22. package/backend/dist/cjs/users.js +87 -72
  23. package/backend/dist/cjs/utils.d.ts +17 -9
  24. package/backend/dist/cjs/utils.js +22 -64
  25. package/backend/dist/cjs/view.d.ts +2 -2
  26. package/backend/dist/cjs/view.js +38 -40
  27. package/backend/dist/cjs/volt.d.ts +3 -2
  28. package/backend/dist/cjs/volt.js +2 -2
  29. package/backend/dist/css/volt.css +5 -0
  30. package/backend/dist/esm/database.d.ts +41 -68
  31. package/backend/dist/esm/database.js +127 -76
  32. package/backend/dist/esm/endpoint.d.ts +23 -9
  33. package/backend/dist/esm/endpoint.js +99 -22
  34. package/backend/dist/esm/frontend.d.ts +0 -2
  35. package/backend/dist/esm/frontend.js +9 -9
  36. package/backend/dist/esm/image_endpoint.d.ts +3 -1
  37. package/backend/dist/esm/image_endpoint.js +2 -1
  38. package/backend/dist/esm/payments/paddle.js +11 -3
  39. package/backend/dist/esm/plugins/css.d.ts +6 -5
  40. package/backend/dist/esm/plugins/css.js +32 -6
  41. package/backend/dist/esm/plugins/ts/compiler.d.ts +6 -1
  42. package/backend/dist/esm/plugins/ts/compiler.js +26 -2
  43. package/backend/dist/esm/plugins/ts/preprocessing.js +5 -3
  44. package/backend/dist/esm/server.d.ts +7 -13
  45. package/backend/dist/esm/server.js +182 -301
  46. package/backend/dist/esm/status.d.ts +1 -0
  47. package/backend/dist/esm/status.js +1 -0
  48. package/backend/dist/esm/stream.d.ts +5 -3
  49. package/backend/dist/esm/stream.js +13 -4
  50. package/backend/dist/esm/users.d.ts +1 -1
  51. package/backend/dist/esm/users.js +87 -72
  52. package/backend/dist/esm/utils.d.ts +17 -9
  53. package/backend/dist/esm/utils.js +21 -62
  54. package/backend/dist/esm/view.d.ts +2 -2
  55. package/backend/dist/esm/view.js +38 -40
  56. package/backend/dist/esm/volt.d.ts +3 -2
  57. package/backend/dist/esm/volt.js +2 -1
  58. package/backend/dist/esm-dev/blacklist.js +1 -1
  59. package/backend/dist/esm-dev/cli.js +2 -2
  60. package/backend/dist/esm-dev/database.d.ts +41 -68
  61. package/backend/dist/esm-dev/database.js +128 -77
  62. package/backend/dist/esm-dev/endpoint.d.ts +23 -9
  63. package/backend/dist/esm-dev/endpoint.js +100 -23
  64. package/backend/dist/esm-dev/file_watcher.js +1 -1
  65. package/backend/dist/esm-dev/frontend.d.ts +0 -2
  66. package/backend/dist/esm-dev/frontend.js +9 -9
  67. package/backend/dist/esm-dev/image_endpoint.d.ts +3 -1
  68. package/backend/dist/esm-dev/image_endpoint.js +2 -1
  69. package/backend/dist/esm-dev/logger.js +1 -1
  70. package/backend/dist/esm-dev/payments/paddle.js +12 -4
  71. package/backend/dist/esm-dev/plugins/css.d.ts +6 -5
  72. package/backend/dist/esm-dev/plugins/css.js +33 -7
  73. package/backend/dist/esm-dev/plugins/ts/compiler.d.ts +6 -1
  74. package/backend/dist/esm-dev/plugins/ts/compiler.js +27 -3
  75. package/backend/dist/esm-dev/plugins/ts/preprocessing.js +7 -5
  76. package/backend/dist/esm-dev/rate_limit.js +1 -1
  77. package/backend/dist/esm-dev/server.d.ts +7 -13
  78. package/backend/dist/esm-dev/server.js +184 -303
  79. package/backend/dist/esm-dev/status.d.ts +1 -0
  80. package/backend/dist/esm-dev/status.js +1 -0
  81. package/backend/dist/esm-dev/stream.d.ts +5 -3
  82. package/backend/dist/esm-dev/stream.js +13 -4
  83. package/backend/dist/esm-dev/users.d.ts +1 -1
  84. package/backend/dist/esm-dev/users.js +88 -73
  85. package/backend/dist/esm-dev/utils.d.ts +17 -9
  86. package/backend/dist/esm-dev/utils.js +22 -63
  87. package/backend/dist/esm-dev/view.d.ts +2 -2
  88. package/backend/dist/esm-dev/view.js +39 -41
  89. package/backend/dist/esm-dev/volt.d.ts +3 -2
  90. package/backend/dist/esm-dev/volt.js +2 -1
  91. package/backend/src/database.ts +163 -152
  92. package/backend/src/endpoint.ts +123 -31
  93. package/backend/src/frontend.ts +9 -8
  94. package/backend/src/image_endpoint.ts +4 -0
  95. package/backend/src/payments/paddle.ts +11 -3
  96. package/backend/src/plugins/css.ts +36 -8
  97. package/backend/src/plugins/ts/compiler.ts +37 -1
  98. package/backend/src/plugins/ts/preprocessing.ts +5 -3
  99. package/backend/src/server.ts +167 -306
  100. package/backend/src/status.ts +1 -0
  101. package/backend/src/stream.ts +28 -8
  102. package/backend/src/users.ts +87 -72
  103. package/backend/src/utils.ts +58 -25
  104. package/backend/src/view.ts +30 -28
  105. package/backend/src/{volt.js → volt.ts} +2 -1
  106. package/backend/tsconfig.cjs.json +3 -3
  107. package/backend/tsconfig.esm.json +3 -3
  108. package/frontend/dist/elements/base.d.ts +397 -415
  109. package/frontend/dist/elements/base.js +565 -328
  110. package/frontend/dist/elements/module.d.ts +26 -12
  111. package/frontend/dist/elements/module.js +69 -32
  112. package/frontend/dist/elements/register_element.d.ts +3 -0
  113. package/frontend/dist/elements/register_element.js +22 -0
  114. package/frontend/dist/modules/auth.d.ts +1 -0
  115. package/frontend/dist/modules/auth.js +6 -5
  116. package/frontend/dist/modules/color.d.ts +159 -0
  117. package/frontend/dist/modules/color.js +315 -0
  118. package/frontend/dist/modules/colors.d.ts +1 -26
  119. package/frontend/dist/modules/colors.js +417 -338
  120. package/frontend/dist/modules/cookies.d.ts +1 -0
  121. package/frontend/dist/modules/cookies.js +1 -0
  122. package/frontend/dist/modules/events.d.ts +1 -0
  123. package/frontend/dist/modules/events.js +1 -0
  124. package/frontend/dist/modules/google.d.ts +1 -0
  125. package/frontend/dist/modules/google.js +1 -0
  126. package/frontend/dist/modules/meta.d.ts +1 -0
  127. package/frontend/dist/modules/meta.js +1 -0
  128. package/frontend/dist/modules/mutex.d.ts +1 -2
  129. package/frontend/dist/modules/mutex.js +3 -4
  130. package/frontend/dist/modules/paddle.d.ts +1 -0
  131. package/frontend/dist/modules/paddle.js +14 -13
  132. package/frontend/dist/modules/scheme.d.ts +1 -0
  133. package/frontend/dist/modules/scheme.js +4 -2
  134. package/frontend/dist/modules/statics.d.ts +1 -0
  135. package/frontend/dist/modules/statics.js +1 -0
  136. package/frontend/dist/modules/support.d.ts +1 -0
  137. package/frontend/dist/modules/support.js +3 -2
  138. package/frontend/dist/modules/theme.d.ts +56 -0
  139. package/frontend/dist/{ui → modules}/theme.js +186 -75
  140. package/frontend/dist/modules/themes.d.ts +1 -1
  141. package/frontend/dist/modules/themes.js +1 -0
  142. package/frontend/dist/modules/user.d.ts +1 -0
  143. package/frontend/dist/modules/user.js +11 -10
  144. package/frontend/dist/modules/utils.d.ts +23 -2
  145. package/frontend/dist/modules/utils.js +93 -1
  146. package/frontend/dist/types/gradient.js +4 -0
  147. package/frontend/dist/ui/border_button.d.ts +0 -25
  148. package/frontend/dist/ui/border_button.js +50 -51
  149. package/frontend/dist/ui/button.d.ts +0 -21
  150. package/frontend/dist/ui/button.js +41 -46
  151. package/frontend/dist/ui/canvas.js +15 -15
  152. package/frontend/dist/ui/checkbox.d.ts +3 -17
  153. package/frontend/dist/ui/checkbox.js +36 -30
  154. package/frontend/dist/ui/code.d.ts +15 -82
  155. package/frontend/dist/ui/code.js +150 -125
  156. package/frontend/dist/ui/color.d.ts +0 -1
  157. package/frontend/dist/ui/color.js +1 -1
  158. package/frontend/dist/ui/context_menu.d.ts +4 -2
  159. package/frontend/dist/ui/context_menu.js +16 -17
  160. package/frontend/dist/ui/css.js +2 -0
  161. package/frontend/dist/ui/divider.d.ts +0 -7
  162. package/frontend/dist/ui/divider.js +21 -25
  163. package/frontend/dist/ui/dropdown.d.ts +13 -7
  164. package/frontend/dist/ui/dropdown.js +65 -30
  165. package/frontend/dist/ui/for_each.d.ts +0 -5
  166. package/frontend/dist/ui/for_each.js +17 -22
  167. package/frontend/dist/ui/form.d.ts +17 -12
  168. package/frontend/dist/ui/form.js +21 -18
  169. package/frontend/dist/ui/frame_modes.d.ts +9 -12
  170. package/frontend/dist/ui/frame_modes.js +8 -10
  171. package/frontend/dist/ui/google_map.d.ts +0 -11
  172. package/frontend/dist/ui/google_map.js +23 -28
  173. package/frontend/dist/ui/gradient.d.ts +0 -5
  174. package/frontend/dist/ui/gradient.js +17 -22
  175. package/frontend/dist/ui/image.d.ts +27 -58
  176. package/frontend/dist/ui/image.js +99 -93
  177. package/frontend/dist/ui/input.d.ts +20 -97
  178. package/frontend/dist/ui/input.js +192 -170
  179. package/frontend/dist/ui/link.d.ts +0 -18
  180. package/frontend/dist/ui/link.js +42 -48
  181. package/frontend/dist/ui/list.js +36 -37
  182. package/frontend/dist/ui/loader_button.d.ts +4 -19
  183. package/frontend/dist/ui/loader_button.js +35 -37
  184. package/frontend/dist/ui/loaders.d.ts +0 -8
  185. package/frontend/dist/ui/loaders.js +20 -25
  186. package/frontend/dist/ui/popup.d.ts +11 -8
  187. package/frontend/dist/ui/popup.js +183 -24
  188. package/frontend/dist/ui/pseudo.d.ts +3 -3
  189. package/frontend/dist/ui/pseudo.js +14 -17
  190. package/frontend/dist/ui/scroller.d.ts +10 -48
  191. package/frontend/dist/ui/scroller.js +306 -300
  192. package/frontend/dist/ui/slider.d.ts +9 -3
  193. package/frontend/dist/ui/slider.js +31 -17
  194. package/frontend/dist/ui/spacer.d.ts +0 -9
  195. package/frontend/dist/ui/spacer.js +21 -26
  196. package/frontend/dist/ui/span.js +13 -15
  197. package/frontend/dist/ui/stack.d.ts +14 -75
  198. package/frontend/dist/ui/stack.js +166 -169
  199. package/frontend/dist/ui/steps.d.ts +10 -23
  200. package/frontend/dist/ui/steps.js +47 -34
  201. package/frontend/dist/ui/style.d.ts +4 -3
  202. package/frontend/dist/ui/style.js +13 -18
  203. package/frontend/dist/ui/switch.d.ts +10 -4
  204. package/frontend/dist/ui/switch.js +24 -16
  205. package/frontend/dist/ui/table.d.ts +0 -23
  206. package/frontend/dist/ui/table.js +113 -119
  207. package/frontend/dist/ui/tabs.d.ts +3 -19
  208. package/frontend/dist/ui/tabs.js +35 -29
  209. package/frontend/dist/ui/text.d.ts +0 -8
  210. package/frontend/dist/ui/text.js +20 -25
  211. package/frontend/dist/ui/title.d.ts +0 -15
  212. package/frontend/dist/ui/title.js +39 -45
  213. package/frontend/dist/ui/ui.d.ts +0 -2
  214. package/frontend/dist/ui/ui.js +0 -2
  215. package/frontend/dist/ui/view.d.ts +3 -17
  216. package/frontend/dist/ui/view.js +27 -32
  217. package/frontend/dist/volt.d.ts +2 -1
  218. package/frontend/dist/volt.js +3 -1
  219. package/frontend/examples/dashboard/dashboard.ts +774 -0
  220. package/frontend/examples/theme/theme.ts +58 -0
  221. package/frontend/src/css/volt.css +5 -0
  222. package/frontend/src/elements/base.ts +767 -545
  223. package/frontend/src/elements/module.ts +90 -29
  224. package/frontend/src/elements/register_element.ts +24 -0
  225. package/frontend/src/modules/auth.ts +7 -6
  226. package/frontend/src/modules/color.ts +348 -0
  227. package/frontend/src/modules/colors.ts +468 -449
  228. package/frontend/src/modules/cookies.ts +1 -0
  229. package/frontend/src/modules/events.ts +1 -0
  230. package/frontend/src/modules/google.ts +1 -0
  231. package/frontend/src/modules/meta.ts +2 -1
  232. package/frontend/src/modules/mutex.ts +2 -4
  233. package/frontend/src/modules/paddle.ts +21 -20
  234. package/frontend/src/modules/scheme.ts +4 -3
  235. package/frontend/src/modules/statics.ts +2 -1
  236. package/frontend/src/modules/support.ts +3 -2
  237. package/frontend/src/modules/theme.ts +413 -0
  238. package/frontend/src/modules/themes.ts +2 -1
  239. package/frontend/src/modules/user.ts +12 -11
  240. package/frontend/src/modules/utils.ts +125 -2
  241. package/frontend/src/ui/border_button.ts +41 -37
  242. package/frontend/src/ui/button.ts +33 -32
  243. package/frontend/src/ui/canvas.ts +5 -2
  244. package/frontend/src/ui/checkbox.ts +21 -22
  245. package/frontend/src/ui/code.ts +92 -86
  246. package/frontend/src/ui/context_menu.ts +7 -5
  247. package/frontend/src/ui/css.ts +1 -1
  248. package/frontend/src/ui/divider.ts +15 -10
  249. package/frontend/src/ui/dropdown.ts +38 -21
  250. package/frontend/src/ui/for_each.ts +9 -8
  251. package/frontend/src/ui/form.ts +26 -21
  252. package/frontend/src/ui/frame_modes.ts +13 -17
  253. package/frontend/src/ui/google_map.ts +15 -13
  254. package/frontend/src/ui/gradient.ts +9 -8
  255. package/frontend/src/ui/image.ts +108 -86
  256. package/frontend/src/ui/input.ts +145 -144
  257. package/frontend/src/ui/link.ts +25 -23
  258. package/frontend/src/ui/list.ts +12 -6
  259. package/frontend/src/ui/loader_button.ts +26 -25
  260. package/frontend/src/ui/loaders.ts +12 -11
  261. package/frontend/src/ui/popup.ts +168 -14
  262. package/frontend/src/ui/pseudo.ts +5 -3
  263. package/frontend/src/ui/scroller.ts +303 -294
  264. package/frontend/src/ui/slider.ts +15 -10
  265. package/frontend/src/ui/spacer.ts +14 -11
  266. package/frontend/src/ui/span.ts +6 -2
  267. package/frontend/src/ui/stack.ts +196 -183
  268. package/frontend/src/ui/steps.ts +38 -22
  269. package/frontend/src/ui/style.ts +7 -4
  270. package/frontend/src/ui/switch.ts +16 -11
  271. package/frontend/src/ui/table.ts +42 -34
  272. package/frontend/src/ui/tabs.ts +20 -19
  273. package/frontend/src/ui/text.ts +12 -11
  274. package/frontend/src/ui/title.ts +22 -20
  275. package/frontend/src/ui/ui.ts +0 -2
  276. package/frontend/src/ui/view.ts +20 -19
  277. package/frontend/src/volt.ts +3 -1
  278. package/frontend/{compile.js → tools/compile.old.js} +2 -2
  279. package/frontend/tools/embed_scripts.js +69 -0
  280. package/frontend/tsconfig.json +26 -0
  281. package/package.json +8 -8
  282. package/frontend/dist/ui/theme.d.ts +0 -25
  283. package/frontend/exports.json +0 -1340
  284. package/frontend/src/modules/date.js +0 -535
  285. package/frontend/src/ui/color.ts +0 -117
  286. package/frontend/src/ui/theme.ts +0 -279
  287. /package/backend/src/{vinc.dev.js → vinc.dev.ts} +0 -0
@@ -13,9 +13,19 @@ import { MongoClient, Collection as MongoCollection, ObjectId, ServerApiVersion
13
13
  import { logger, LogSource } from "./logger.js";
14
14
  import { Status } from "./status.js";
15
15
  import { vlib } from "@vinc";
16
+ import { resolve } from "path";
16
17
 
17
18
  const log_source = new LogSource("Database")
18
19
 
20
+ interface BaseOptions {
21
+ options?: Record<string, any>;
22
+ commit_quorum?: any;
23
+ forced?: boolean;
24
+ }
25
+ type IndexOptions =
26
+ (BaseOptions & { key: string; keys?: never }) |
27
+ (BaseOptions & { key?: never; keys: string[] });
28
+
19
29
  // ---------------------------------------------------------
20
30
  // Collection.
21
31
  // Path based collection, so "myfile", "mydir/myfile".
@@ -39,7 +49,7 @@ class Collection {
39
49
  static constructor_scheme = {
40
50
  name: "string",
41
51
  uid_based: "boolean",
42
- ttl: {type: "number", default: null},
52
+ ttl: {type: "number", default: null}, // ttl in msec
43
53
  indexes: {
44
54
  type: "array",
45
55
  default: [],
@@ -48,7 +58,7 @@ class Collection {
48
58
  scheme: {
49
59
  key: {type: "string", required: (data) => data.key == null && data.keys == null },
50
60
  keys: {
51
- type: ["string", "array"],
61
+ type: ["array"],
52
62
  required: (data) => data.key == null && data.keys == null, value_scheme: "string",
53
63
  postprocess: (keys) => typeof keys === "string" ? [keys] : keys,
54
64
  },
@@ -58,11 +68,6 @@ class Collection {
58
68
  },
59
69
  postprocess: (info) => {
60
70
  if (typeof info === "string") return {keys: [info]}
61
- if (typeof info === "object" && info.key) {
62
- info.keys = info.key;
63
- delete info.key;
64
- return info;
65
- }
66
71
  return info;
67
72
  },
68
73
  },
@@ -74,19 +79,14 @@ class Collection {
74
79
  col: MongoCollection;
75
80
  name: string;
76
81
  uid_based: boolean;
77
- ttl: number | null;
82
+ ttl: number | null; // ttl in msec
78
83
  ttl_enabled: boolean;
79
84
 
80
85
  constructor(
81
86
  name: string,
82
87
  collection: MongoCollection,
83
- ttl: number | null = null,
84
- indexes: Array<{
85
- keys: string[] | string | Record<string, any>,
86
- options?: Record<string, any>,
87
- commit_quorum?: any,
88
- forced?: boolean
89
- }> = [],
88
+ ttl: number | null = null, // ttl in msec
89
+ indexes: IndexOptions[] = [],
90
90
  uid_based: boolean = false,
91
91
  ) {
92
92
 
@@ -135,13 +135,8 @@ class Collection {
135
135
 
136
136
  // Create indexes.
137
137
  if (Array.isArray(indexes) && indexes.length > 0) {
138
- for (const index of indexes) {
139
- this.create_index({
140
- keys: index.keys,
141
- options: index.options,
142
- commit_quorum: index.commit_quorum,
143
- forced: index.forced,
144
- })
138
+ for (let item of indexes) {
139
+ this.create_index(item)
145
140
  }
146
141
  }
147
142
  }
@@ -270,31 +265,27 @@ class Collection {
270
265
  @desc: The `commitQuorum` argument for the orignal mongodb `createIndex()` function.
271
266
  */
272
267
  async create_index({
273
- keys,
274
- options = null,
275
- commit_quorum = null,
268
+ key = undefined,
269
+ keys = undefined, // following the IndexOptions "key" or "keys" must be defined in typescript but not in raw js.
270
+ options = undefined,
271
+ commit_quorum = undefined,
276
272
  forced = false
277
- }: {
278
- keys: string[] | string | Record<string, any>,
279
- options?: Record<string, any> | null,
280
- commit_quorum?: any,
281
- forced?: boolean
282
- }): Promise<string> {
273
+ }: IndexOptions): Promise<string> {
274
+
275
+ // Create keys objs per input type.
283
276
  let keys_obj: Record<string, number> = {};
284
- if (typeof keys === "string") {
277
+ if (key) {
285
278
  keys_obj = {};
286
- keys_obj[keys] = 1;
279
+ keys_obj[key] = 1;
287
280
  }
288
- else if (Array.isArray(keys)) {
281
+ else if (Array.isArray(keys) && keys.length > 0) {
289
282
  keys_obj = {};
290
283
  for (const key of keys) {
291
284
  keys_obj[key] = 1;
292
285
  }
293
286
  }
294
- else if (keys == null || typeof keys !== "object") {
295
- vlib.Scheme.throw_invalid_type("keys", keys, ["string", "string[]", "object"], true);
296
- } else {
297
- keys_obj = keys;
287
+ else {
288
+ throw new Error("Define one of the following parameters: [key, keys].");
298
289
  }
299
290
 
300
291
  // Drop index.
@@ -876,11 +867,7 @@ class UIDCollection {
876
867
  constructor(
877
868
  name: string,
878
869
  collection: MongoCollection,
879
- indexes: Array<{
880
- keys: string[] | string | Record<string, any>,
881
- options?: Record<string, any>,
882
- commit_quorum?: any
883
- }> = [],
870
+ indexes: IndexOptions[] = [],
884
871
  ttl: number | null = null,
885
872
  ) {
886
873
  this._col = new Collection(name, collection, ttl, indexes, true);
@@ -902,16 +889,8 @@ class UIDCollection {
902
889
  @name: commitQuorum
903
890
  @desc: The `commitQuorum` argument for the orignal mongodb `createIndex()` function.
904
891
  */
905
- async create_index({
906
- keys,
907
- options = null,
908
- commit_quorum = null
909
- }: {
910
- keys: string[] | string | Record<string, any>,
911
- options?: Record<string, any> | null,
912
- commit_quorum?: any
913
- }): Promise<string> {
914
- return this._col.create_index({keys, options, commit_quorum});
892
+ async create_index(args: IndexOptions): Promise<string> {
893
+ return this._col.create_index(args);
915
894
  }
916
895
 
917
896
  /* @docs:
@@ -1354,34 +1333,26 @@ class Database {
1354
1333
  preview: {type: "boolean", default: true},
1355
1334
  preview_ip_whitelist: {type: "array", default: []},
1356
1335
  daemon: {type: ["object", "boolean"], default: {}},
1357
- _server: "object",
1336
+ _server: {type: ["object", "undefined"]},
1358
1337
  }
1359
1338
 
1360
1339
  uri: string | null;
1361
- preview: boolean;
1362
- preview_ip_whitelist: string[];
1340
+ // preview: boolean;
1341
+ // preview_ip_whitelist: string[];
1363
1342
  client_opts: Record<string, any> | null;
1364
1343
  config: Record<string, any>;
1365
- source: any; // Using vlib.Path type
1344
+ source: vlib.Path | undefined; // Using vlib.Path type
1366
1345
  start_args: string[];
1367
- _collections: Array<{
1346
+ _collections: {
1368
1347
  name: string,
1369
1348
  ttl?: number | null,
1370
- indexes?: Array<{
1371
- keys: string[] | string | Record<string, any>,
1372
- options?: Record<string, any>,
1373
- commit_quorum?: any
1374
- }>,
1375
- }>;
1376
- _uid_collections: Array<{
1349
+ indexes?: string[] | IndexOptions[],
1350
+ }[];
1351
+ _uid_collections: {
1377
1352
  name: string,
1378
1353
  ttl?: number | null,
1379
- indexes?: Array<{
1380
- keys: string[] | string | Record<string, any>,
1381
- options?: Record<string, any>,
1382
- commit_quorum?: any
1383
- }>,
1384
- }>;
1354
+ indexes?: string[] | IndexOptions[],
1355
+ }[];
1385
1356
  server: any;
1386
1357
  client: MongoClient | null;
1387
1358
  collections: Record<string, Collection | UIDCollection>;
@@ -1389,6 +1360,9 @@ class Database {
1389
1360
  daemon?: any;
1390
1361
  db?: any;
1391
1362
 
1363
+ // System.
1364
+ _listed_cols: any;
1365
+
1392
1366
  constructor({
1393
1367
  uri = null,
1394
1368
  source = null,
@@ -1397,41 +1371,33 @@ class Database {
1397
1371
  client = null,
1398
1372
  collections = [],
1399
1373
  uid_collections = [],
1400
- preview = true,
1401
- preview_ip_whitelist = [],
1374
+ // preview = true,
1375
+ // preview_ip_whitelist = [],
1402
1376
  daemon = {},
1403
- _server,
1377
+ _server = undefined,
1404
1378
  }: {
1405
1379
  uri?: string | null,
1406
1380
  source?: string | null,
1407
1381
  config?: Record<string, any> | null,
1408
1382
  start_args?: string[],
1409
1383
  client?: Record<string, any> | null,
1410
- collections?: Array<{
1384
+ collections?: {
1411
1385
  name: string,
1412
1386
  ttl?: number | null,
1413
- indexes?: Array<{
1414
- keys: string[] | string | Record<string, any>,
1415
- options?: Record<string, any>,
1416
- commit_quorum?: any
1417
- }>,
1418
- }>,
1419
- uid_collections?: Array<{
1387
+ indexes?: string[] | IndexOptions[],
1388
+ }[],
1389
+ uid_collections?: {
1420
1390
  name: string,
1421
1391
  ttl?: number | null,
1422
- indexes?: Array<{
1423
- keys: string[] | string | Record<string, any>,
1424
- options?: Record<string, any>,
1425
- commit_quorum?: any
1426
- }>,
1427
- }>,
1392
+ indexes?: string[] | IndexOptions[],
1393
+ }[],
1428
1394
  preview?: boolean,
1429
1395
  preview_ip_whitelist?: string[],
1430
1396
  daemon?: Record<string, any> | boolean,
1431
1397
  _server: any,
1432
1398
  }) {
1433
1399
  // Checks.
1434
- if (_server.is_primary && uri == null) {
1400
+ if (!_server || (_server.is_primary && uri == null)) {
1435
1401
  ({uri, config, start_args, config, client} = vlib.Scheme.verify({
1436
1402
  object: arguments[0],
1437
1403
  check_unknown: true,
@@ -1440,14 +1406,14 @@ class Database {
1440
1406
  }
1441
1407
  // Arguments.
1442
1408
  this.uri = uri;
1443
- this.preview = preview;
1444
- this.preview_ip_whitelist = preview_ip_whitelist;
1409
+ // this.preview = preview;
1410
+ // this.preview_ip_whitelist = preview_ip_whitelist;
1445
1411
  this.client_opts = client;
1446
1412
  this.config = config || {};
1447
- this.source = source != null ? new vlib.Path(source) : _server.source.join(".db");
1413
+ this.source = source != null ? new vlib.Path(source) : _server?.source.join(".db");
1448
1414
  this.start_args = start_args;
1449
- this._collections = collections;
1450
- this._uid_collections = uid_collections;
1415
+ this._collections = collections ?? [];
1416
+ this._uid_collections = uid_collections ?? [];
1451
1417
  this.server = _server;
1452
1418
 
1453
1419
  // Attributes.
@@ -1455,18 +1421,21 @@ class Database {
1455
1421
  this.collections = {};
1456
1422
 
1457
1423
  // Initialize the service daemon.
1458
- if (this.server.daemon && daemon !== false) {
1424
+ if (this.server?.daemon && daemon !== false) {
1459
1425
  const log_source = this.server.source.join(".logs");
1460
1426
  if (!log_source.exists()) {
1461
1427
  log_source.mkdir_sync();
1462
1428
  }
1429
+ if (!this.server) {
1430
+ throw new Error("Parameter 'Database._server' must be defined for this behaviour.");
1431
+ }
1463
1432
  this.daemon = new vlib.Daemon({
1464
1433
  name: this.server.daemon.name + ".mongodb",
1465
1434
  user: (daemon as Record<string, any>).user || this.server.daemon.user,
1466
1435
  group: (daemon as Record<string, any>).group || this.server.daemon.group,
1467
1436
  command: "mongod",
1468
1437
  cwd: this.server.daemon.cwd,
1469
- args: ["--config", this.source.join("mongod.json").str(), ...this.start_args],
1438
+ args: ["--config", this.source!.join("mongod.json").str(), ...this.start_args],
1470
1439
  env: (daemon as Record<string, any>).env || this.server.daemon.env,
1471
1440
  description: (daemon as Record<string, any>).description || `Service daemon for the mongo database of website ${this.server.domain}.`,
1472
1441
  auto_restart: true,
@@ -1476,9 +1445,9 @@ class Database {
1476
1445
  }
1477
1446
  }
1478
1447
 
1479
- // Database preview.
1448
+ /* Database preview.
1480
1449
  _initialize_db_preview(): void {
1481
- /*
1450
+
1482
1451
  if (this.preview && this.server.production === false) {
1483
1452
  this.server.endpoint(
1484
1453
 
@@ -2002,9 +1971,8 @@ class Database {
2002
1971
  }
2003
1972
  },
2004
1973
  )
2005
- }
2006
- */
2007
- }
1974
+ }
1975
+ }*/
2008
1976
 
2009
1977
  // Connect.
2010
1978
  async connect(): Promise<void> {
@@ -2024,7 +1992,7 @@ class Database {
2024
1992
  // Set default config.
2025
1993
  if (this.config.systemLog === undefined) { this.config.systemLog = {}; }
2026
1994
 
2027
- this.config.systemLog.path = this.source.join("mongod.log").str()
1995
+ this.config.systemLog.path = this.source?.join("mongod.log").str()
2028
1996
 
2029
1997
  if (this.config.systemLog.destination === undefined) {
2030
1998
  this.config.systemLog.destination = "file";
@@ -2035,30 +2003,32 @@ class Database {
2035
2003
  if (this.config.systemLog.logRotate === undefined) {
2036
2004
  this.config.systemLog.logRotate = "reopen";
2037
2005
  }
2038
- if (this.config.systemLog.verbosity === undefined) {
2006
+ if (this.config.systemLog.verbosity === undefined && this.server) {
2039
2007
  this.config.systemLog.verbosity = this.server.production ? 0 : 1;
2040
2008
  }
2041
2009
 
2042
2010
  if (this.config.storage === undefined) { this.config.storage = {}; }
2043
2011
 
2044
- const db_path = this.source.join("db");
2045
- this.config.storage.dbPath = db_path.str()
2046
- if (!db_path.exists()) {
2047
- db_path.mkdir_sync();
2048
- }
2012
+ if (this.source) {
2013
+ const db_path = this.source.join("db");
2014
+ this.config.storage.dbPath = db_path.str()
2015
+ if (!db_path.exists()) {
2016
+ db_path.mkdir_sync();
2017
+ }
2049
2018
 
2050
- if (this.config.processManagement === undefined) { this.config.processManagement = {}; }
2051
- this.config.processManagement.pidFilePath = this.source.join("mongod.pid").str()
2019
+ if (this.config.processManagement === undefined) { this.config.processManagement = {}; }
2020
+ this.config.processManagement.pidFilePath = this.source.join("mongod.pid").str()
2021
+ }
2052
2022
 
2053
2023
  if (this.config.net === undefined) { this.config.net = {}; }
2054
2024
  if (this.config.net.port === undefined) { this.config.net.port = 27017; }
2055
2025
  if (this.config.net.bindIp === undefined) { this.config.net.bindIp = "127.0.0.1"; }
2056
2026
 
2057
2027
  // Mode 2: Start database.
2058
- if (this.server.is_primary && this.uri == null) {
2028
+ if (this.server?.is_primary && this.uri == null) {
2059
2029
  // Create the database.
2060
- if (!this.source.exists()) {
2061
- this.source.mkdir_sync();
2030
+ if (!this.source!.exists()) {
2031
+ this.source!.mkdir_sync();
2062
2032
  }
2063
2033
 
2064
2034
  // Set the uri.
@@ -2067,7 +2037,7 @@ class Database {
2067
2037
  }
2068
2038
 
2069
2039
  // Save the config.
2070
- const config_path = this.source.join("mongod.json");
2040
+ const config_path = this.source!.join("mongod.json");
2071
2041
  config_path.save_sync(JSON.stringify(this.config));
2072
2042
 
2073
2043
  // Start the database.
@@ -2093,7 +2063,7 @@ class Database {
2093
2063
  }
2094
2064
 
2095
2065
  // Assign URI.
2096
- else if (!this.server.is_primary && this.uri == null) {
2066
+ else if (this.server && !this.server.is_primary && this.uri == null) {
2097
2067
  this.uri = `mongodb://${this.config.net.bindIp}:${this.config.net.port}/main`
2098
2068
  }
2099
2069
 
@@ -2111,33 +2081,56 @@ class Database {
2111
2081
  // Connect.
2112
2082
  await this.connect();
2113
2083
 
2084
+ // Batch.
2085
+ const multiprocessing = true;
2086
+ const promises: Promise<any>[] = [];
2087
+
2114
2088
  // Create collections.
2115
- this._collections.forEach((info) => {
2116
- if (this[info.name as keyof this] !== undefined) {
2117
- throw Error(`Unable to initialize database collection "${info.name}", this attribute name is already used.`);
2118
- }
2119
- if (Array.isArray(info.indexes)) {
2120
- for (const index of info.indexes) {
2121
- if (index != null && typeof index === "object") {
2122
- (index as any).forced = true;
2089
+ for (const info of this._collections) {
2090
+ const promise = new Promise<void>(resolve => {
2091
+ if (this[info.name as keyof this] !== undefined) {
2092
+ throw Error(`Unable to initialize database collection "${info.name}", this attribute name is reserved for a system value.`);
2093
+ }
2094
+ if (Array.isArray(info.indexes)) {
2095
+ for (let index = 0; index < info.indexes.length; index++) {
2096
+ let item = info.indexes[index];
2097
+ if (typeof item === "string") {
2098
+ info.indexes[index] = item = { keys: [item] } as IndexOptions
2099
+ }
2123
2100
  }
2124
2101
  }
2125
- }
2126
- this[info.name] = this.create_collection(info);
2127
- })
2128
- this._uid_collections.forEach((info) => {
2129
- if (this[info.name as keyof this] !== undefined) {
2130
- throw Error(`Unable to initialize database collection "${info.name}", this attribute name is already used.`);
2131
- }
2132
- if (Array.isArray(info.indexes)) {
2133
- for (const index of info.indexes) {
2134
- if (index != null && typeof index === "object") {
2135
- (index as any).forced = true;
2102
+ this.create_collection(info as any).then(data => {
2103
+ this[info.name] = data;
2104
+ resolve();
2105
+ })
2106
+ });
2107
+ if (multiprocessing) { promises.push(promise) }
2108
+ else { await promise }
2109
+ }
2110
+ for (const info of this._uid_collections) {
2111
+ const promise = new Promise<void>(resolve => {
2112
+ if (this[info.name as keyof this] !== undefined) {
2113
+ throw Error(`Unable to initialize database collection "${info.name}", this attribute name is reserved for a system value.`);
2114
+ }
2115
+ if (Array.isArray(info.indexes)) {
2116
+ for (let index = 0; index < info.indexes.length; index++) {
2117
+ let item = info.indexes[index];
2118
+ if (typeof item === "string") {
2119
+ info.indexes[index] = item = {
2120
+ key: item,
2121
+ } as IndexOptions
2122
+ }
2136
2123
  }
2137
2124
  }
2138
- }
2139
- this[info.name] = this.create_uid_collection(info);
2140
- })
2125
+ this.create_uid_collection(info as any).then(data => {
2126
+ this[info.name] = data;
2127
+ resolve();
2128
+ })
2129
+ });
2130
+ if (multiprocessing) { promises.push(promise) }
2131
+ else { await promise }
2132
+ }
2133
+ if (multiprocessing) { await Promise.all(promises); }
2141
2134
  }
2142
2135
 
2143
2136
  // Close.
@@ -2150,15 +2143,11 @@ class Database {
2150
2143
  * @title: Create Collection
2151
2144
  * @description: Create a database collection.
2152
2145
  */
2153
- create_collection(info: {
2146
+ async create_collection(info: {
2154
2147
  name: string,
2155
- indexes?: Array<{
2156
- keys: string[] | string | Record<string, any>,
2157
- options?: Record<string, any>,
2158
- commit_quorum?: any
2159
- }>,
2148
+ indexes?: IndexOptions[],
2160
2149
  ttl?: number | null
2161
- } | string): Collection {
2150
+ } | string): Promise<Collection> {
2162
2151
  // Set name by single string argument.
2163
2152
  let name: string;
2164
2153
  let indexes: Array<any> = [];
@@ -2176,6 +2165,20 @@ class Database {
2176
2165
  if (name in this.collections) {
2177
2166
  throw Error(`Collection "${name}" is already initialized.`)
2178
2167
  }
2168
+
2169
+ // Logs.
2170
+ logger.log(1, log_source, `Initializing collection "${name}".`);
2171
+
2172
+ // Check if the collection exists
2173
+ if (this._listed_cols == null) {
2174
+ this._listed_cols = await this.db.listCollections().toArray();
2175
+ }
2176
+ if (!this._listed_cols.find(x => x.name === name)) {
2177
+ logger.log(0, log_source, `Creating collection "${name}".`);
2178
+ await this.db.createCollection(name);
2179
+ }
2180
+
2181
+ // Create collection.
2179
2182
  const col = new Collection(
2180
2183
  name,
2181
2184
  this.db.collection(name),
@@ -2190,15 +2193,11 @@ class Database {
2190
2193
  * @title: Create UID Based Collection
2191
2194
  * @description: Create a UID based database collection.
2192
2195
  */
2193
- create_uid_collection(info: {
2196
+ async create_uid_collection(info: {
2194
2197
  name: string,
2195
- indexes?: Array<{
2196
- keys: string[] | string | Record<string, any>,
2197
- options?: Record<string, any>,
2198
- commit_quorum?: any
2199
- }>,
2198
+ indexes?: IndexOptions[],
2200
2199
  ttl?: number | null
2201
- } | string): UIDCollection {
2200
+ } | string): Promise<UIDCollection> {
2202
2201
  // Set name by single string argument.
2203
2202
  let name: string;
2204
2203
  let indexes: Array<any> = [];
@@ -2212,10 +2211,22 @@ class Database {
2212
2211
  ttl = info.ttl || null;
2213
2212
  }
2214
2213
 
2215
- // Check collection.
2214
+ // Check duplicate collection.
2216
2215
  if (name in this.collections) {
2217
2216
  throw Error(`Collection "${name}" is already initialized.`)
2217
+ }
2218
+
2219
+ // Logs.
2220
+ logger.log(1, log_source, `Initializing uid collection "${name}".`);
2221
+
2222
+ // Check if the collection exists
2223
+ const collections = await this.db.listCollections({ name: name }).toArray();
2224
+ if (collections.length === 0) {
2225
+ logger.log(0, log_source, `Creating uid collection "${name}".`);
2226
+ await this.db.createCollection(name);
2218
2227
  }
2228
+
2229
+ // Collection.
2219
2230
  const col = new UIDCollection(
2220
2231
  name,
2221
2232
  this.db.collection(name),