prostgles-server 4.2.179 → 4.2.181

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 (227) hide show
  1. package/.vscode/settings.json +10 -1
  2. package/README.md +48 -52
  3. package/dist/DBSchemaBuilder.d.ts +2 -2
  4. package/dist/DBSchemaBuilder.d.ts.map +1 -1
  5. package/dist/DBSchemaBuilder.js +34 -26
  6. package/dist/DBSchemaBuilder.js.map +1 -1
  7. package/dist/DboBuilder/TableHandler/DataValidator.d.ts +1 -1
  8. package/dist/DboBuilder/TableHandler/DataValidator.d.ts.map +1 -1
  9. package/dist/DboBuilder/TableHandler/DataValidator.js +4 -4
  10. package/dist/DboBuilder/TableHandler/DataValidator.js.map +1 -1
  11. package/dist/DboBuilder/TableHandler/TableHandler.d.ts +1 -1
  12. package/dist/DboBuilder/TableHandler/TableHandler.d.ts.map +1 -1
  13. package/dist/DboBuilder/TableHandler/insert.js +3 -3
  14. package/dist/DboBuilder/TableHandler/insert.js.map +1 -1
  15. package/dist/DboBuilder/TableHandler/update.d.ts.map +1 -1
  16. package/dist/DboBuilder/TableHandler/update.js +45 -19
  17. package/dist/DboBuilder/TableHandler/update.js.map +1 -1
  18. package/dist/DboBuilder/ViewHandler/ViewHandler.d.ts +1 -1
  19. package/dist/DboBuilder/ViewHandler/ViewHandler.d.ts.map +1 -1
  20. package/dist/DboBuilder/ViewHandler/ViewHandler.js +2 -2
  21. package/dist/DboBuilder/ViewHandler/ViewHandler.js.map +1 -1
  22. package/dist/DboBuilder/getColumns.d.ts.map +1 -1
  23. package/dist/DboBuilder/getColumns.js +41 -16
  24. package/dist/DboBuilder/getColumns.js.map +1 -1
  25. package/dist/DboBuilder/parseUpdateRules.js +1 -1
  26. package/dist/DboBuilder/parseUpdateRules.js.map +1 -1
  27. package/dist/Prostgles.d.ts +2 -2
  28. package/dist/Prostgles.d.ts.map +1 -1
  29. package/dist/Prostgles.js +117 -54
  30. package/dist/Prostgles.js.map +1 -1
  31. package/dist/ProstglesTypes.d.ts +81 -25
  32. package/dist/ProstglesTypes.d.ts.map +1 -1
  33. package/dist/ProstglesTypes.js.map +1 -1
  34. package/dist/SyncReplication.js +2 -2
  35. package/dist/SyncReplication.js.map +1 -1
  36. package/dist/initProstgles.d.ts +9 -2
  37. package/dist/initProstgles.d.ts.map +1 -1
  38. package/dist/initProstgles.js +68 -60
  39. package/dist/initProstgles.js.map +1 -1
  40. package/dist/typeTests/dboTypeCheck.d.ts.map +1 -1
  41. package/dist/typeTests/dboTypeCheck.js +11 -11
  42. package/dist/typeTests/dboTypeCheck.js.map +1 -1
  43. package/dist/utils.d.ts.map +1 -1
  44. package/dist/utils.js.map +1 -1
  45. package/documentation/METHODS.md +1467 -0
  46. package/documentation/SERVER.md +103 -0
  47. package/documentation/utils/clientTypes.ts +14415 -0
  48. package/documentation/utils/dist/clientTypes.d.ts +13211 -0
  49. package/documentation/utils/dist/clientTypes.d.ts.map +1 -0
  50. package/documentation/utils/dist/clientTypes.js +14417 -0
  51. package/documentation/utils/dist/definitions.d.ts +7705 -0
  52. package/documentation/utils/dist/definitions.d.ts.map +1 -0
  53. package/documentation/utils/dist/definitions.js +8438 -0
  54. package/documentation/utils/dist/generateClientDocs.d.ts +2 -0
  55. package/documentation/utils/dist/generateClientDocs.d.ts.map +1 -0
  56. package/documentation/utils/dist/generateClientDocs.js +165 -0
  57. package/documentation/utils/dist/generateDocs.d.ts +2 -0
  58. package/documentation/utils/dist/generateDocs.d.ts.map +1 -0
  59. package/documentation/utils/dist/generateDocs.js +6 -0
  60. package/documentation/utils/dist/generateMarkdown.d.ts +2 -0
  61. package/documentation/utils/dist/generateMarkdown.d.ts.map +1 -0
  62. package/documentation/utils/dist/generateMarkdown.js +68 -0
  63. package/documentation/utils/dist/generateServerDocs.d.ts +2 -0
  64. package/documentation/utils/dist/generateServerDocs.d.ts.map +1 -0
  65. package/documentation/utils/dist/generateServerDocs.js +81 -0
  66. package/documentation/utils/dist/getResolvedTypes.d.ts +15 -0
  67. package/documentation/utils/dist/getResolvedTypes.d.ts.map +1 -0
  68. package/documentation/utils/dist/getResolvedTypes.js +29 -0
  69. package/documentation/utils/dist/getSerializableType.d.ts +69 -0
  70. package/documentation/utils/dist/getSerializableType.d.ts.map +1 -0
  71. package/documentation/utils/dist/getSerializableType.js +279 -0
  72. package/documentation/utils/dist/getSymbolComments.d.ts +11 -0
  73. package/documentation/utils/dist/getSymbolComments.d.ts.map +1 -0
  74. package/documentation/utils/dist/getSymbolComments.js +60 -0
  75. package/documentation/utils/dist/loadTsFile.d.ts +7 -0
  76. package/documentation/utils/dist/loadTsFile.d.ts.map +1 -0
  77. package/documentation/utils/dist/loadTsFile.js +29 -0
  78. package/documentation/utils/dist/moduleResolver.d.ts +9 -0
  79. package/documentation/utils/dist/moduleResolver.d.ts.map +1 -0
  80. package/documentation/utils/dist/moduleResolver.js +27 -0
  81. package/documentation/utils/dist/serverTypes.d.ts +301 -0
  82. package/documentation/utils/dist/serverTypes.d.ts.map +1 -0
  83. package/documentation/utils/dist/serverTypes.js +341 -0
  84. package/documentation/utils/generateClientDocs.ts +210 -0
  85. package/documentation/utils/generateDocs.ts +4 -0
  86. package/documentation/utils/generateServerDocs.ts +84 -0
  87. package/documentation/utils/getResolvedTypes.ts +45 -0
  88. package/documentation/utils/getSerializableType.ts +489 -0
  89. package/documentation/utils/loadTsFile.ts +41 -0
  90. package/documentation/utils/moduleResolver.ts +31 -0
  91. package/documentation/utils/node_modules/.package-lock.json +67 -0
  92. package/documentation/utils/node_modules/@types/node/LICENSE +21 -0
  93. package/documentation/utils/node_modules/@types/node/README.md +15 -0
  94. package/documentation/utils/node_modules/@types/node/assert/strict.d.ts +8 -0
  95. package/documentation/utils/node_modules/@types/node/assert.d.ts +1040 -0
  96. package/documentation/utils/node_modules/@types/node/async_hooks.d.ts +541 -0
  97. package/documentation/utils/node_modules/@types/node/buffer.buffer.d.ts +385 -0
  98. package/documentation/utils/node_modules/@types/node/buffer.d.ts +1934 -0
  99. package/documentation/utils/node_modules/@types/node/child_process.d.ts +1548 -0
  100. package/documentation/utils/node_modules/@types/node/cluster.d.ts +578 -0
  101. package/documentation/utils/node_modules/@types/node/compatibility/disposable.d.ts +16 -0
  102. package/documentation/utils/node_modules/@types/node/compatibility/index.d.ts +9 -0
  103. package/documentation/utils/node_modules/@types/node/compatibility/indexable.d.ts +20 -0
  104. package/documentation/utils/node_modules/@types/node/compatibility/iterators.d.ts +21 -0
  105. package/documentation/utils/node_modules/@types/node/console.d.ts +452 -0
  106. package/documentation/utils/node_modules/@types/node/constants.d.ts +19 -0
  107. package/documentation/utils/node_modules/@types/node/crypto.d.ts +4519 -0
  108. package/documentation/utils/node_modules/@types/node/dgram.d.ts +596 -0
  109. package/documentation/utils/node_modules/@types/node/diagnostics_channel.d.ts +554 -0
  110. package/documentation/utils/node_modules/@types/node/dns/promises.d.ts +477 -0
  111. package/documentation/utils/node_modules/@types/node/dns.d.ts +864 -0
  112. package/documentation/utils/node_modules/@types/node/dom-events.d.ts +124 -0
  113. package/documentation/utils/node_modules/@types/node/domain.d.ts +170 -0
  114. package/documentation/utils/node_modules/@types/node/events.d.ts +931 -0
  115. package/documentation/utils/node_modules/@types/node/fs/promises.d.ts +1245 -0
  116. package/documentation/utils/node_modules/@types/node/fs.d.ts +4317 -0
  117. package/documentation/utils/node_modules/@types/node/globals.d.ts +468 -0
  118. package/documentation/utils/node_modules/@types/node/globals.typedarray.d.ts +21 -0
  119. package/documentation/utils/node_modules/@types/node/http.d.ts +1944 -0
  120. package/documentation/utils/node_modules/@types/node/http2.d.ts +2555 -0
  121. package/documentation/utils/node_modules/@types/node/https.d.ts +544 -0
  122. package/documentation/utils/node_modules/@types/node/index.d.ts +91 -0
  123. package/documentation/utils/node_modules/@types/node/inspector.d.ts +3696 -0
  124. package/documentation/utils/node_modules/@types/node/module.d.ts +315 -0
  125. package/documentation/utils/node_modules/@types/node/net.d.ts +1005 -0
  126. package/documentation/utils/node_modules/@types/node/os.d.ts +495 -0
  127. package/documentation/utils/node_modules/@types/node/package.json +220 -0
  128. package/documentation/utils/node_modules/@types/node/path.d.ts +200 -0
  129. package/documentation/utils/node_modules/@types/node/perf_hooks.d.ts +934 -0
  130. package/documentation/utils/node_modules/@types/node/process.d.ts +1879 -0
  131. package/documentation/utils/node_modules/@types/node/punycode.d.ts +117 -0
  132. package/documentation/utils/node_modules/@types/node/querystring.d.ts +153 -0
  133. package/documentation/utils/node_modules/@types/node/readline/promises.d.ts +162 -0
  134. package/documentation/utils/node_modules/@types/node/readline.d.ts +589 -0
  135. package/documentation/utils/node_modules/@types/node/repl.d.ts +430 -0
  136. package/documentation/utils/node_modules/@types/node/sea.d.ts +153 -0
  137. package/documentation/utils/node_modules/@types/node/stream/consumers.d.ts +12 -0
  138. package/documentation/utils/node_modules/@types/node/stream/promises.d.ts +90 -0
  139. package/documentation/utils/node_modules/@types/node/stream/web.d.ts +524 -0
  140. package/documentation/utils/node_modules/@types/node/stream.d.ts +1726 -0
  141. package/documentation/utils/node_modules/@types/node/string_decoder.d.ts +67 -0
  142. package/documentation/utils/node_modules/@types/node/test.d.ts +1938 -0
  143. package/documentation/utils/node_modules/@types/node/timers/promises.d.ts +97 -0
  144. package/documentation/utils/node_modules/@types/node/timers.d.ts +240 -0
  145. package/documentation/utils/node_modules/@types/node/tls.d.ts +1217 -0
  146. package/documentation/utils/node_modules/@types/node/trace_events.d.ts +197 -0
  147. package/documentation/utils/node_modules/@types/node/ts5.6/buffer.buffer.d.ts +385 -0
  148. package/documentation/utils/node_modules/@types/node/ts5.6/globals.typedarray.d.ts +19 -0
  149. package/documentation/utils/node_modules/@types/node/ts5.6/index.d.ts +91 -0
  150. package/documentation/utils/node_modules/@types/node/tty.d.ts +208 -0
  151. package/documentation/utils/node_modules/@types/node/url.d.ts +955 -0
  152. package/documentation/utils/node_modules/@types/node/util.d.ts +2306 -0
  153. package/documentation/utils/node_modules/@types/node/v8.d.ts +808 -0
  154. package/documentation/utils/node_modules/@types/node/vm.d.ts +922 -0
  155. package/documentation/utils/node_modules/@types/node/wasi.d.ts +181 -0
  156. package/documentation/utils/node_modules/@types/node/worker_threads.d.ts +694 -0
  157. package/documentation/utils/node_modules/@types/node/zlib.d.ts +539 -0
  158. package/documentation/utils/node_modules/undici-types/LICENSE +21 -0
  159. package/documentation/utils/node_modules/undici-types/README.md +6 -0
  160. package/documentation/utils/node_modules/undici-types/agent.d.ts +31 -0
  161. package/documentation/utils/node_modules/undici-types/api.d.ts +43 -0
  162. package/documentation/utils/node_modules/undici-types/balanced-pool.d.ts +29 -0
  163. package/documentation/utils/node_modules/undici-types/cache.d.ts +36 -0
  164. package/documentation/utils/node_modules/undici-types/client.d.ts +108 -0
  165. package/documentation/utils/node_modules/undici-types/connector.d.ts +34 -0
  166. package/documentation/utils/node_modules/undici-types/content-type.d.ts +21 -0
  167. package/documentation/utils/node_modules/undici-types/cookies.d.ts +28 -0
  168. package/documentation/utils/node_modules/undici-types/diagnostics-channel.d.ts +66 -0
  169. package/documentation/utils/node_modules/undici-types/dispatcher.d.ts +255 -0
  170. package/documentation/utils/node_modules/undici-types/env-http-proxy-agent.d.ts +21 -0
  171. package/documentation/utils/node_modules/undici-types/errors.d.ts +149 -0
  172. package/documentation/utils/node_modules/undici-types/eventsource.d.ts +63 -0
  173. package/documentation/utils/node_modules/undici-types/fetch.d.ts +209 -0
  174. package/documentation/utils/node_modules/undici-types/file.d.ts +39 -0
  175. package/documentation/utils/node_modules/undici-types/filereader.d.ts +54 -0
  176. package/documentation/utils/node_modules/undici-types/formdata.d.ts +108 -0
  177. package/documentation/utils/node_modules/undici-types/global-dispatcher.d.ts +9 -0
  178. package/documentation/utils/node_modules/undici-types/global-origin.d.ts +7 -0
  179. package/documentation/utils/node_modules/undici-types/handlers.d.ts +15 -0
  180. package/documentation/utils/node_modules/undici-types/header.d.ts +4 -0
  181. package/documentation/utils/node_modules/undici-types/index.d.ts +71 -0
  182. package/documentation/utils/node_modules/undici-types/interceptors.d.ts +15 -0
  183. package/documentation/utils/node_modules/undici-types/mock-agent.d.ts +50 -0
  184. package/documentation/utils/node_modules/undici-types/mock-client.d.ts +25 -0
  185. package/documentation/utils/node_modules/undici-types/mock-errors.d.ts +12 -0
  186. package/documentation/utils/node_modules/undici-types/mock-interceptor.d.ts +93 -0
  187. package/documentation/utils/node_modules/undici-types/mock-pool.d.ts +25 -0
  188. package/documentation/utils/node_modules/undici-types/package.json +55 -0
  189. package/documentation/utils/node_modules/undici-types/patch.d.ts +71 -0
  190. package/documentation/utils/node_modules/undici-types/pool-stats.d.ts +19 -0
  191. package/documentation/utils/node_modules/undici-types/pool.d.ts +39 -0
  192. package/documentation/utils/node_modules/undici-types/proxy-agent.d.ts +28 -0
  193. package/documentation/utils/node_modules/undici-types/readable.d.ts +60 -0
  194. package/documentation/utils/node_modules/undici-types/retry-agent.d.ts +8 -0
  195. package/documentation/utils/node_modules/undici-types/retry-handler.d.ts +116 -0
  196. package/documentation/utils/node_modules/undici-types/util.d.ts +18 -0
  197. package/documentation/utils/node_modules/undici-types/webidl.d.ts +222 -0
  198. package/documentation/utils/node_modules/undici-types/websocket.d.ts +152 -0
  199. package/documentation/utils/package-lock.json +75 -0
  200. package/documentation/utils/package.json +14 -0
  201. package/documentation/utils/serverTypes.ts +339 -0
  202. package/documentation/utils/tsconfig.json +28 -0
  203. package/examples/full-example-typescript/DBoGenerated.d.ts +2 -2
  204. package/examples/full-example-typescript/DBoGenerated.ts +2 -2
  205. package/examples/full-example-typescript/index.ts +39 -45
  206. package/examples/full-example-vanilla/index.js +51 -53
  207. package/examples/server/typescript/index.ts +24 -30
  208. package/lib/DBSchemaBuilder.ts +148 -118
  209. package/lib/DboBuilder/TableHandler/DataValidator.ts +5 -5
  210. package/lib/DboBuilder/TableHandler/TableHandler.ts +1 -1
  211. package/lib/DboBuilder/TableHandler/insert.ts +3 -3
  212. package/lib/DboBuilder/TableHandler/update.ts +106 -48
  213. package/lib/DboBuilder/ViewHandler/ViewHandler.ts +2 -2
  214. package/lib/DboBuilder/getColumns.ts +63 -31
  215. package/lib/DboBuilder/parseUpdateRules.ts +1 -1
  216. package/lib/Prostgles.ts +208 -143
  217. package/lib/ProstglesTypes.ts +134 -52
  218. package/lib/SyncReplication.ts +2 -2
  219. package/lib/initProstgles.ts +151 -118
  220. package/lib/typeTests/DBoGenerated.d.ts +23 -19
  221. package/lib/typeTests/dboTypeCheck.ts +49 -35
  222. package/lib/utils.ts +4 -5
  223. package/package.json +5 -3
  224. package/examples/server/typescript/index.d.ts +0 -2
  225. package/examples/server/typescript/index.d.ts.map +0 -1
  226. package/examples/server/typescript/index.js +0 -54
  227. package/examples/server/typescript/index.js.map +0 -1
package/lib/Prostgles.ts CHANGED
@@ -3,37 +3,37 @@
3
3
  * Licensed under the MIT License. See LICENSE in the project root for license information.
4
4
  *--------------------------------------------------------------------------------------------*/
5
5
 
6
- import * as pgPromise from 'pg-promise';
6
+ import * as pgPromise from "pg-promise";
7
7
  import { AuthHandler } from "./Auth/AuthHandler";
8
8
  import { FileManager } from "./FileManager/FileManager";
9
9
  import { SchemaWatch } from "./SchemaWatch/SchemaWatch";
10
10
  import { OnInitReason, initProstgles } from "./initProstgles";
11
11
  import { makeSocketError, onSocketConnected } from "./onSocketConnected";
12
12
  import { clientCanRunSqlRequest, runClientSqlRequest } from "./runClientRequest";
13
- import pg = require('pg-promise/typescript/pg-subset');
14
- const { version } = require('../package.json');
13
+ import pg = require("pg-promise/typescript/pg-subset");
14
+ const { version } = require("../package.json");
15
15
 
16
16
  import type { ProstglesInitOptions } from "./ProstglesTypes";
17
17
  import { RestApi } from "./RestApi";
18
18
  import TableConfigurator from "./TableConfig/TableConfig";
19
-
20
- import { DBHandlerServer, DboBuilder, LocalParams, PRGLIOSocket, getErrorAsObject } from "./DboBuilder/DboBuilder";
19
+
20
+ import {
21
+ DBHandlerServer,
22
+ DboBuilder,
23
+ LocalParams,
24
+ PRGLIOSocket,
25
+ getErrorAsObject,
26
+ } from "./DboBuilder/DboBuilder";
21
27
  export { DBHandlerServer };
22
28
  export type PGP = pgPromise.IMain<{}, pg.IClient>;
23
29
 
24
- import {
25
- CHANNELS,
26
- ClientSchema,
27
- SQLRequest,
28
- isObject, omitKeys, tryCatch
29
- } from "prostgles-types";
30
+ import { CHANNELS, ClientSchema, SQLRequest, isObject, omitKeys, tryCatch } from "prostgles-types";
30
31
  import { DBEventsManager } from "./DBEventsManager";
31
32
  import { PublishParser } from "./PublishParser/PublishParser";
32
33
  export { getOrSetTransporter, sendEmail, verifySMTPConfig } from "./Auth/sendEmail";
33
34
 
34
35
  export type DB = pgPromise.IDatabase<{}, pg.IClient>;
35
- export type DBorTx = DB | pgPromise.ITask<{}>
36
-
36
+ export type DBorTx = DB | pgPromise.ITask<{}>;
37
37
 
38
38
  export const TABLE_METHODS = ["update", "find", "findOne", "insert", "delete", "upsert"] as const;
39
39
 
@@ -48,17 +48,17 @@ export const TABLE_METHODS = ["update", "find", "findOne", "insert", "delete", "
48
48
  export type OnReady = {
49
49
  dbo: DBHandlerServer;
50
50
  db: DB;
51
- }
51
+ };
52
52
 
53
53
  const DEFAULT_KEYWORDS = {
54
54
  $filter: "$filter",
55
55
  $and: "$and",
56
56
  $or: "$or",
57
- $not: "$not"
57
+ $not: "$not",
58
58
  };
59
59
 
60
60
  import { randomUUID } from "crypto";
61
- import * as fs from 'fs';
61
+ import * as fs from "fs";
62
62
 
63
63
  export class Prostgles {
64
64
  /**
@@ -70,22 +70,22 @@ export class Prostgles {
70
70
  dbConnection: {
71
71
  host: "localhost",
72
72
  port: 5432,
73
- application_name: "prostgles_app"
73
+ application_name: "prostgles_app",
74
74
  },
75
- onReady: () => {
76
- //empty
75
+ onReady: () => {
76
+ //empty
77
77
  },
78
78
  watchSchema: false,
79
79
  watchSchemaType: "DDL_trigger",
80
80
  };
81
-
81
+
82
82
  db?: DB;
83
83
  pgp?: PGP;
84
84
  dbo?: DBHandlerServer;
85
85
  _dboBuilder?: DboBuilder;
86
86
  get dboBuilder(): DboBuilder {
87
87
  if (!this._dboBuilder) {
88
- console.trace(1)
88
+ console.trace(1);
89
89
  throw "get dboBuilder: it's undefined";
90
90
  }
91
91
  return this._dboBuilder;
@@ -118,14 +118,38 @@ export class Prostgles {
118
118
  if (!params) throw "ProstglesInitOptions missing";
119
119
 
120
120
  const config: Record<keyof ProstglesInitOptions, 1> = {
121
- transactions: 1, joins: 1, tsGeneratedTypesDir: 1, disableRealtime: 1,
122
- onReady: 1, dbConnection: 1, dbOptions: 1, publishMethods: 1,
123
- io: 1, publish: 1, schema: 1, publishRawSQL: 1, wsChannelNamePrefix: 1,
124
- onSocketConnect: 1, onSocketDisconnect: 1, sqlFilePath: 1, auth: 1,
125
- DEBUG_MODE: 1, watchSchema: 1, watchSchemaType: 1, fileTable: 1, onQuery: 1,
126
- tableConfig: 1, tableConfigMigrations: 1, keywords: 1, onNotice: 1, onLog: 1, restApi: 1, testRulesOnConnect: 1
121
+ transactions: 1,
122
+ joins: 1,
123
+ tsGeneratedTypesDir: 1,
124
+ disableRealtime: 1,
125
+ onReady: 1,
126
+ dbConnection: 1,
127
+ publishMethods: 1,
128
+ io: 1,
129
+ publish: 1,
130
+ schema: 1,
131
+ publishRawSQL: 1,
132
+ wsChannelNamePrefix: 1,
133
+ onSocketConnect: 1,
134
+ onSocketDisconnect: 1,
135
+ sqlFilePath: 1,
136
+ auth: 1,
137
+ DEBUG_MODE: 1,
138
+ watchSchema: 1,
139
+ watchSchemaType: 1,
140
+ fileTable: 1,
141
+ onQuery: 1,
142
+ tableConfig: 1,
143
+ tableConfigMigrations: 1,
144
+ keywords: 1,
145
+ onNotice: 1,
146
+ onLog: 1,
147
+ restApi: 1,
148
+ testRulesOnConnect: 1,
127
149
  };
128
- const unknownParams = Object.keys(params).filter((key: string) => !Object.keys(config).includes(key))
150
+ const unknownParams = Object.keys(params).filter(
151
+ (key: string) => !Object.keys(config).includes(key)
152
+ );
129
153
  if (unknownParams.length) {
130
154
  console.error(`Unrecognised ProstglesInitOptions params: ${unknownParams.join()}`);
131
155
  }
@@ -136,12 +160,12 @@ export class Prostgles {
136
160
  if (this.opts?.fileTable) {
137
161
  this.opts.fileTable.tableName ??= "media";
138
162
  }
139
- this.opts.schema ??= { "public": 1 };
163
+ this.opts.schema ??= { public: 1 };
140
164
 
141
165
  this.keywords = {
142
166
  ...DEFAULT_KEYWORDS,
143
167
  ...params.keywords,
144
- }
168
+ };
145
169
  }
146
170
 
147
171
  destroyed = false;
@@ -151,45 +175,45 @@ export class Prostgles {
151
175
  }
152
176
 
153
177
  getTSFileName() {
154
- const fileName = "DBoGenerated.d.ts" //`dbo_${this.schema}_types.ts`;
155
- const _dir = (this.opts.tsGeneratedTypesDir || "");
156
- const dir = _dir.endsWith("/")? _dir : `${_dir}/`;
178
+ const fileName = "DBGeneratedSchema.d.ts"; //`dbo_${this.schema}_types.ts`;
179
+ const _dir = this.opts.tsGeneratedTypesDir || "";
180
+ const dir = _dir.endsWith("/") ? _dir : `${_dir}/`;
157
181
  const fullPath = dir + fileName;
158
- return { fileName, fullPath }
182
+ return { fileName, fullPath };
159
183
  }
160
184
 
161
185
  private getFileText(fullPath: string, _format = "utf8"): Promise<string> {
162
186
  return new Promise((resolve, reject) => {
163
- fs.readFile(fullPath, 'utf8', function (err, data) {
187
+ fs.readFile(fullPath, "utf8", function (err, data) {
164
188
  if (err) reject(err);
165
189
  else resolve(data);
166
190
  });
167
- })
191
+ });
168
192
  }
169
193
 
170
194
  getTSFileContent = () => {
171
- const header = `/* This file was generated by Prostgles \n` +
172
- // `* ${(new Date).toUTCString()} \n`
173
- `*/ \n\n `;
195
+ const header =
196
+ `/* This file was generated by Prostgles \n` +
197
+ // `* ${(new Date).toUTCString()} \n`
198
+ `*/ \n\n `;
174
199
  return header + this.dboBuilder.tsTypesDefinition;
175
- }
200
+ };
176
201
 
177
202
  /**
178
203
  * Will write the Schema Typescript definitions to file (tsGeneratedTypesDir)
179
204
  */
180
205
  writeDBSchema(force = false) {
181
-
182
206
  if (this.opts.tsGeneratedTypesDir) {
183
207
  const { fullPath, fileName } = this.getTSFileName();
184
208
  const fileContent = this.getTSFileContent();
185
- fs.readFile(fullPath, 'utf8', function (err, data) {
186
- if (err || (force || data !== fileContent)) {
209
+ fs.readFile(fullPath, "utf8", function (err, data) {
210
+ if (err || force || data !== fileContent) {
187
211
  fs.writeFileSync(fullPath, fileContent);
188
- console.log("Prostgles: Created typescript schema definition file: \n " + fileName)
212
+ console.log("Prostgles: Created typescript schema definition file: \n " + fileName);
189
213
  }
190
214
  });
191
215
  } else if (force) {
192
- console.error("Schema changed. tsGeneratedTypesDir needs to be set to reload server")
216
+ console.error("Schema changed. tsGeneratedTypesDir needs to be set to reload server");
193
217
  }
194
218
  }
195
219
 
@@ -197,11 +221,11 @@ export class Prostgles {
197
221
  * Will re-create the dbo object
198
222
  */
199
223
  refreshDBO = async () => {
200
- await this.opts.onLog?.({
201
- type: "debug",
202
- command: "refreshDBO.start",
203
- duration: -1,
204
- data: { }
224
+ await this.opts.onLog?.({
225
+ type: "debug",
226
+ command: "refreshDBO.start",
227
+ duration: -1,
228
+ data: {},
205
229
  });
206
230
  const start = Date.now();
207
231
  if (this._dboBuilder) {
@@ -211,9 +235,13 @@ export class Prostgles {
211
235
  }
212
236
  if (!this.dboBuilder) throw "this.dboBuilder";
213
237
  this.dbo = this.dboBuilder.dbo;
214
- await this.opts.onLog?.({ type: "debug", command: "refreshDBO.end", duration: Date.now() - start })
238
+ await this.opts.onLog?.({
239
+ type: "debug",
240
+ command: "refreshDBO.end",
241
+ duration: Date.now() - start,
242
+ });
215
243
  return this.dbo;
216
- }
244
+ };
217
245
 
218
246
  initRestApi = async () => {
219
247
  if (this.opts.restApi) {
@@ -222,29 +250,36 @@ export class Prostgles {
222
250
  this.restApi?.destroy();
223
251
  this.restApi = undefined;
224
252
  }
225
- }
253
+ };
226
254
 
227
255
  initAuthHandler = async () => {
228
256
  this.authHandler?.destroy();
229
257
  this.authHandler = new AuthHandler(this as any);
230
258
  await this.authHandler.init();
231
- }
259
+ };
232
260
 
233
261
  initTableConfig = async (reason: OnInitReason) => {
234
262
  const res = await tryCatch(async () => {
235
-
236
- if(this.tableConfigurator?.initialising){
263
+ if (this.tableConfigurator?.initialising) {
237
264
  console.error("TableConfigurator WILL deadlock", { reason });
238
265
  }
239
266
  await this.tableConfigurator?.destroy();
240
267
  this.tableConfigurator = new TableConfigurator(this);
241
268
  try {
242
269
  const now = Date.now();
243
- await this.opts.onLog?.({ type: "debug", command: "tableConfigurator.init.start", duration: -1 });
270
+ await this.opts.onLog?.({
271
+ type: "debug",
272
+ command: "tableConfigurator.init.start",
273
+ duration: -1,
274
+ });
244
275
  await this.tableConfigurator.init();
245
- await this.opts.onLog?.({ type: "debug", command: "tableConfigurator.init.end", duration: Date.now() - now });
276
+ await this.opts.onLog?.({
277
+ type: "debug",
278
+ command: "tableConfigurator.init.end",
279
+ duration: Date.now() - now,
280
+ });
246
281
  } catch (e) {
247
- if(this.opts.tableConfigMigrations?.silentFail === false){
282
+ if (this.opts.tableConfigMigrations?.silentFail === false) {
248
283
  console.error("TableConfigurator silentFail: ", e);
249
284
  } else {
250
285
  throw e;
@@ -252,21 +287,21 @@ export class Prostgles {
252
287
  }
253
288
  });
254
289
  await this.opts.onLog?.({ type: "debug", command: "initTableConfig", ...res });
255
- if(res.hasError) throw res.error;
290
+ if (res.hasError) throw res.error;
256
291
  return res.data;
257
- }
292
+ };
258
293
 
259
294
  /* Create media table if required */
260
295
  initFileTable = async () => {
261
296
  const res = await tryCatch(async () => {
262
-
263
297
  if (this.opts.fileTable) {
264
298
  const { cloudClient, localConfig, imageOptions } = this.opts.fileTable;
265
299
  await this.refreshDBO();
266
- if (!cloudClient && !localConfig) throw "fileTable missing param: Must provide awsS3Config OR localConfig";
267
-
300
+ if (!cloudClient && !localConfig)
301
+ throw "fileTable missing param: Must provide awsS3Config OR localConfig";
302
+
268
303
  this.fileManager = new FileManager(cloudClient || localConfig!, imageOptions);
269
-
304
+
270
305
  try {
271
306
  await this.fileManager.init(this);
272
307
  } catch (e) {
@@ -278,55 +313,61 @@ export class Prostgles {
278
313
  this.fileManager = undefined;
279
314
  }
280
315
  await this.refreshDBO();
281
- return { data: {} }
316
+ return { data: {} };
282
317
  });
283
- await this.opts.onLog?.({
284
- type: "debug",
285
- command: "initFileTable",
318
+ await this.opts.onLog?.({
319
+ type: "debug",
320
+ command: "initFileTable",
286
321
  ...res,
287
322
  });
288
- if(res.error !== undefined) throw res.error;
323
+ if (res.error !== undefined) throw res.error;
289
324
  return res.data;
290
- }
325
+ };
291
326
 
292
327
  isSuperUser = false;
293
328
 
294
329
  init = initProstgles.bind(this);
295
330
 
296
331
  async runSQLFile(filePath: string) {
297
-
298
332
  const res = await tryCatch(async () => {
299
- const fileContent = await this.getFileText(filePath);//.then(console.log);
300
-
301
- const result = await this.db?.multi(fileContent)
333
+ const fileContent = await this.getFileText(filePath); //.then(console.log);
334
+
335
+ const result = await this.db
336
+ ?.multi(fileContent)
302
337
  .then((data) => {
303
338
  console.log("Prostgles: SQL file executed successfuly \n -> " + filePath);
304
339
  return data;
305
- }).catch((err) => {
340
+ })
341
+ .catch((err) => {
306
342
  const { position, length } = err,
307
343
  lines = fileContent.split("\n");
308
344
  let errMsg = filePath + " error: ";
309
-
345
+
310
346
  if (position && length && fileContent) {
311
- const startLine = Math.max(0, fileContent.substring(0, position).split("\n").length - 2),
347
+ const startLine = Math.max(
348
+ 0,
349
+ fileContent.substring(0, position).split("\n").length - 2
350
+ ),
312
351
  endLine = startLine + 3;
313
-
352
+
314
353
  errMsg += "\n\n";
315
- errMsg += lines.slice(startLine, endLine).map((txt, i) => `${startLine + i + 1} ${i === 1 ? "->" : " "} ${txt}`).join("\n");
354
+ errMsg += lines
355
+ .slice(startLine, endLine)
356
+ .map((txt, i) => `${startLine + i + 1} ${i === 1 ? "->" : " "} ${txt}`)
357
+ .join("\n");
316
358
  errMsg += "\n\n";
317
359
  }
318
360
  console.error(errMsg, err);
319
361
  return Promise.reject(err);
320
362
  });
321
- return { success: result?.length }
363
+ return { success: result?.length };
322
364
  });
323
365
 
324
366
  await this.opts.onLog?.({ type: "debug", command: "runSQLFile", ...res });
325
- if(res.error !== undefined) throw res.error;
367
+ if (res.error !== undefined) throw res.error;
326
368
  return res.success;
327
369
  }
328
370
 
329
-
330
371
  connectedSockets: PRGLIOSocket[] = [];
331
372
  async setSocketEvents() {
332
373
  this.checkDb();
@@ -334,11 +375,11 @@ export class Prostgles {
334
375
  if (!this.dbo) throw "dbo missing";
335
376
 
336
377
  const publishParser = new PublishParser(
337
- this.opts.publish,
338
- this.opts.publishMethods,
339
- this.opts.publishRawSQL,
340
- this.dbo,
341
- this.db!,
378
+ this.opts.publish,
379
+ this.opts.publishMethods,
380
+ this.opts.publishRawSQL,
381
+ this.dbo,
382
+ this.db!,
342
383
  this
343
384
  );
344
385
  this.publishParser = publishParser;
@@ -347,7 +388,7 @@ export class Prostgles {
347
388
 
348
389
  /* Already initialised. Only reconnect sockets */
349
390
  if (this.connectedSockets.length) {
350
- this.connectedSockets.forEach(s => {
391
+ this.connectedSockets.forEach((s) => {
351
392
  s.emit(CHANNELS.SCHEMA_CHANGED);
352
393
  this.pushSocketSchema(s);
353
394
  });
@@ -355,22 +396,23 @@ export class Prostgles {
355
396
  }
356
397
 
357
398
  /* Initialise */
358
- this.opts.io.removeAllListeners('connection');
359
- this.opts.io.on('connection', this.onSocketConnected);
399
+ this.opts.io.removeAllListeners("connection");
400
+ this.opts.io.on("connection", this.onSocketConnected);
360
401
  /** In some cases io will re-init with already connected sockets */
361
- this.opts.io?.sockets.sockets.forEach(socket => this.onSocketConnected(socket))
402
+ this.opts.io?.sockets.sockets.forEach((socket) => this.onSocketConnected(socket));
362
403
  }
363
404
 
364
405
  onSocketConnected = onSocketConnected.bind(this);
365
406
 
366
407
  getClientSchema = async (clientReq: Pick<LocalParams, "socket" | "httpReq">) => {
367
-
368
408
  const result = await tryCatch(async () => {
369
-
370
- const clientInfo = clientReq.socket? { type: "socket" as const, socket: clientReq.socket } : clientReq.httpReq? { type: "http" as const, httpReq: clientReq.httpReq } : undefined;
371
- if(!clientInfo) throw "Invalid client";
372
- if(!this.authHandler) throw "this.authHandler missing";
373
- const userData = await this.authHandler.getClientInfo(clientInfo);
409
+ const clientInfo =
410
+ clientReq.socket ? { type: "socket" as const, socket: clientReq.socket }
411
+ : clientReq.httpReq ? { type: "http" as const, httpReq: clientReq.httpReq }
412
+ : undefined;
413
+ if (!clientInfo) throw "Invalid client";
414
+ if (!this.authHandler) throw "this.authHandler missing";
415
+ const userData = await this.authHandler.getClientInfo(clientInfo);
374
416
  const { publishParser } = this;
375
417
  let fullSchema: Awaited<ReturnType<PublishParser["getSchemaFromPublish"]>> | undefined;
376
418
  let publishValidationError;
@@ -388,15 +430,19 @@ export class Prostgles {
388
430
  rawSQL = allowed;
389
431
  }
390
432
 
391
- const { schema, tables, tableSchemaErrors } = fullSchema ?? { schema: {}, tables: [], tableSchemaErrors: {} };
433
+ const { schema, tables, tableSchemaErrors } = fullSchema ?? {
434
+ schema: {},
435
+ tables: [],
436
+ tableSchemaErrors: {},
437
+ };
392
438
  const joinTables2: string[][] = [];
393
439
  if (this.opts.joins) {
394
- const _joinTables2 = this.dboBuilder.getAllJoinPaths()
395
- .filter(jp =>
396
- ![jp.t1, jp.t2].find(t => !schema[t] || !schema[t]?.findOne)
397
- ).map(jp => [jp.t1, jp.t2].sort());
398
- _joinTables2.map(jt => {
399
- if (!joinTables2.find(_jt => _jt.join() === jt.join())) {
440
+ const _joinTables2 = this.dboBuilder
441
+ .getAllJoinPaths()
442
+ .filter((jp) => ![jp.t1, jp.t2].find((t) => !schema[t] || !schema[t]?.findOne))
443
+ .map((jp) => [jp.t1, jp.t2].sort());
444
+ _joinTables2.map((jt) => {
445
+ if (!joinTables2.find((_jt) => _jt.join() === jt.join())) {
400
446
  joinTables2.push(jt);
401
447
  }
402
448
  });
@@ -404,78 +450,97 @@ export class Prostgles {
404
450
 
405
451
  const methods = await publishParser?.getAllowedMethods(clientInfo, userData);
406
452
 
407
- const methodSchema: ClientSchema["methods"] = !methods? [] : Object.entries(methods).map(([methodName, method]) => {
408
- if(isObject(method) && "run" in method){
409
- return {
410
- name: methodName,
411
- ...omitKeys(method, ["run"]),
412
- }
413
- }
414
- return methodName;
415
- }).sort((a, b) => {
416
- const aName = isObject(a)? a.name : a;
417
- const bName = isObject(b)? b.name : b;
418
- return aName.localeCompare(bName);
419
- });
453
+ const methodSchema: ClientSchema["methods"] =
454
+ !methods ?
455
+ []
456
+ : Object.entries(methods)
457
+ .map(([methodName, method]) => {
458
+ if (isObject(method) && "run" in method) {
459
+ return {
460
+ name: methodName,
461
+ ...omitKeys(method, ["run"]),
462
+ };
463
+ }
464
+ return methodName;
465
+ })
466
+ .sort((a, b) => {
467
+ const aName = isObject(a) ? a.name : a;
468
+ const bName = isObject(b) ? b.name : b;
469
+ return aName.localeCompare(bName);
470
+ });
420
471
 
421
472
  const { auth } = await this.authHandler.getClientAuth(clientReq);
422
-
473
+
423
474
  const clientSchema: ClientSchema = {
424
475
  schema,
425
- methods: methodSchema,
476
+ methods: methodSchema,
426
477
  tableSchema: tables,
427
478
  rawSQL,
428
479
  joinTables: joinTables2,
429
480
  tableSchemaErrors,
430
481
  auth,
431
482
  version,
432
- err: publishValidationError? "Server Error: User publish validation failed." : undefined
483
+ err: publishValidationError ? "Server Error: User publish validation failed." : undefined,
433
484
  };
434
-
485
+
435
486
  return {
436
487
  publishValidationError,
437
488
  clientSchema,
438
- userData
439
- }
489
+ userData,
490
+ };
440
491
  });
441
492
  const sid = result.userData?.sid ?? this.authHandler?.getSIDNoError(clientReq);
442
- await this.opts.onLog?.({
493
+ await this.opts.onLog?.({
443
494
  type: "connect.getClientSchema",
444
495
  duration: result.duration,
445
496
  sid,
446
497
  socketId: clientReq.socket?.id,
447
498
  error: result.error || result.publishValidationError,
448
499
  });
449
- if(result.hasError) throw result.error;
500
+ if (result.hasError) throw result.error;
450
501
  return result.clientSchema;
451
- }
502
+ };
452
503
 
453
504
  pushSocketSchema = async (socket: PRGLIOSocket) => {
454
-
455
505
  try {
456
506
  const clientSchema = await this.getClientSchema({ socket });
457
507
  socket.prostgles = clientSchema;
458
508
  if (clientSchema.rawSQL) {
459
- socket.removeAllListeners(CHANNELS.SQL)
460
- socket.on(CHANNELS.SQL, async ({ query, params, options }: SQLRequest, cb = (..._callback: any) => { /* Empty */ }) => {
461
-
462
- runClientSqlRequest.bind(this)({ type: "socket", socket, query, args: params, options }).then(res => {
463
- cb(null, res);
464
- }).catch(err => {
465
- makeSocketError(cb, err);
466
- });
467
- });
509
+ socket.removeAllListeners(CHANNELS.SQL);
510
+ socket.on(
511
+ CHANNELS.SQL,
512
+ async (
513
+ { query, params, options }: SQLRequest,
514
+ cb = (..._callback: any) => {
515
+ /* Empty */
516
+ }
517
+ ) => {
518
+ runClientSqlRequest
519
+ .bind(this)({ type: "socket", socket, query, args: params, options })
520
+ .then((res) => {
521
+ cb(null, res);
522
+ })
523
+ .catch((err) => {
524
+ makeSocketError(cb, err);
525
+ });
526
+ }
527
+ );
468
528
  }
469
- await this.dboBuilder.prostgles.opts.onLog?.({ type: "debug", command: "pushSocketSchema", duration: -1, data: { socketId: socket.id, clientSchema } });
529
+ await this.dboBuilder.prostgles.opts.onLog?.({
530
+ type: "debug",
531
+ command: "pushSocketSchema",
532
+ duration: -1,
533
+ data: { socketId: socket.id, clientSchema },
534
+ });
470
535
  socket.emit(CHANNELS.SCHEMA, clientSchema);
471
-
472
536
  } catch (err) {
473
537
  socket.emit(CHANNELS.SCHEMA, { err: getErrorAsObject(err) });
474
538
  }
475
- }
539
+ };
476
540
  }
477
541
 
478
-
479
542
  export async function getIsSuperUser(db: DB): Promise<boolean> {
480
- return db.oneOrNone("select usesuper from pg_user where usename = CURRENT_USER;").then(r => r.usesuper);
481
- }
543
+ return db
544
+ .oneOrNone("select usesuper from pg_user where usename = CURRENT_USER;")
545
+ .then((r) => r.usesuper);
546
+ }