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
@@ -1,112 +1,144 @@
1
- import { AnyObject, DbJoinMaker, DBSchema, isObject, JSONB, SQLHandler, TableHandler, ViewHandler } from "prostgles-types";
1
+ import {
2
+ AnyObject,
3
+ DbJoinMaker,
4
+ DBSchema,
5
+ isObject,
6
+ JSONB,
7
+ SQLHandler,
8
+ TableHandler,
9
+ ViewHandler,
10
+ } from "prostgles-types";
2
11
  import prostgles from ".";
3
12
  import { Auth } from "./Auth/AuthTypes";
4
13
  import { DboBuilder, escapeTSNames, postgresToTsType } from "./DboBuilder/DboBuilder";
5
- import { PublishAllOrNothing, PublishParams, PublishTableRule, PublishViewRule, } from "./PublishParser/PublishParser";
14
+ import {
15
+ PublishAllOrNothing,
16
+ PublishParams,
17
+ PublishTableRule,
18
+ PublishViewRule,
19
+ } from "./PublishParser/PublishParser";
6
20
  import { getJSONBSchemaTSTypes } from "./JSONBValidation/validation";
7
21
  import { DBHandlerServer, TableSchemaColumn, TX } from "./DboBuilder/DboBuilderTypes";
8
22
 
9
-
10
23
  export const getDBSchema = (dboBuilder: DboBuilder): string => {
11
24
  const tables: string[] = [];
12
25
 
13
26
  const getColTypeForDBSchema = (udt_name: TableSchemaColumn["udt_name"]): string => {
14
- if(udt_name === "interval"){
15
- const units = [ "years", "months", "days", "hours", "minutes", "seconds", "milliseconds"];
16
-
17
- return `{ ${units.map(u => `${u}?: number;`).join(" ")} }`;
27
+ if (udt_name === "interval") {
28
+ const units = ["years", "months", "days", "hours", "minutes", "seconds", "milliseconds"];
29
+
30
+ return `{ ${units.map((u) => `${u}?: number;`).join(" ")} }`;
18
31
  }
19
32
 
20
33
  return postgresToTsType(udt_name);
21
- }
22
-
34
+ };
35
+
23
36
  /** Tables and columns are sorted to avoid infinite loops due to changing order */
24
- dboBuilder.tablesOrViews?.slice(0).sort((a, b) => a.name.localeCompare(b.name)).forEach(tov => {
25
- const cols = tov.columns.slice(0).sort((a, b) => a.name.localeCompare(b.name));
26
- const getColType = (c: typeof cols[number]) => {
27
-
28
- let type: string = (c.is_nullable? "null | " : "") + getColTypeForDBSchema(c.udt_name) + ";"
29
- const colConf = dboBuilder.prostgles.tableConfigurator?.getColumnConfig(tov.name, c.name);
30
- if(colConf){
31
- if(isObject(colConf) && (colConf.jsonbSchema || colConf.jsonbSchemaType)){
32
- const schema: JSONB.JSONBSchema = colConf.jsonbSchema || { ...colConf, type: colConf.jsonbSchemaType };
33
-
34
- type = getJSONBSchemaTSTypes(schema, { nullable: colConf.nullable }, " ", dboBuilder.tablesOrViews ?? []);
35
- } else if(isObject(colConf) && "enum" in colConf){
36
- if(!colConf.enum) throw "colConf.enum missing"
37
- const types = colConf.enum.map(t => typeof t === "number"? t : JSON.stringify(t));
38
- if(colConf.nullable){
39
- types.unshift("null")
37
+ dboBuilder.tablesOrViews
38
+ ?.slice(0)
39
+ .sort((a, b) => a.name.localeCompare(b.name))
40
+ .forEach((tov) => {
41
+ const cols = tov.columns.slice(0).sort((a, b) => a.name.localeCompare(b.name));
42
+ const getColType = (c: (typeof cols)[number]) => {
43
+ let type: string =
44
+ (c.is_nullable ? "null | " : "") + getColTypeForDBSchema(c.udt_name) + ";";
45
+ const colConf = dboBuilder.prostgles.tableConfigurator?.getColumnConfig(tov.name, c.name);
46
+ if (colConf) {
47
+ if (isObject(colConf) && (colConf.jsonbSchema || colConf.jsonbSchemaType)) {
48
+ const schema: JSONB.JSONBSchema = colConf.jsonbSchema || {
49
+ ...colConf,
50
+ type: colConf.jsonbSchemaType,
51
+ };
52
+
53
+ type = getJSONBSchemaTSTypes(
54
+ schema,
55
+ { nullable: colConf.nullable },
56
+ " ",
57
+ dboBuilder.tablesOrViews ?? []
58
+ );
59
+ } else if (isObject(colConf) && "enum" in colConf) {
60
+ if (!colConf.enum) throw "colConf.enum missing";
61
+ const types = colConf.enum.map((t) => (typeof t === "number" ? t : JSON.stringify(t)));
62
+ if (colConf.nullable) {
63
+ types.unshift("null");
64
+ }
65
+ type = types.join(" | ");
40
66
  }
41
- type = types.join(" | ");
42
67
  }
43
- }
44
- /**
45
- * Columns that are nullable or have default values can be ommitted from an insert
46
- * Non nullable columns with default values cannot containt null values in an insert so they must contain a valid value or be omitted
47
- */
48
- return `${escapeTSNames(c.name)}${c.is_nullable || c.has_default? "?" : ""}: ${type}`
49
- }
50
- tables.push(`${escapeTSNames(tov.name)}: {
68
+ /**
69
+ * Columns that are nullable or have default values can be ommitted from an insert
70
+ * Non nullable columns with default values cannot containt null values in an insert so they must contain a valid value or be omitted
71
+ */
72
+ return `${escapeTSNames(c.name)}${c.is_nullable || c.has_default ? "?" : ""}: ${type}`;
73
+ };
74
+ tables.push(`${escapeTSNames(tov.name)}: {
51
75
  is_view: ${tov.is_view};
52
76
  select: ${tov.privileges.select};
53
77
  insert: ${tov.privileges.insert};
54
78
  update: ${tov.privileges.update};
55
79
  delete: ${tov.privileges.delete};
56
- columns: {${cols.map(c => `
57
- ${getColType(c)}`).join("")}
80
+ columns: {${cols
81
+ .map(
82
+ (c) => `
83
+ ${getColType(c)}`
84
+ )
85
+ .join("")}
58
86
  };
59
- };\n `)
60
- })
61
- return `
62
- export type DBSchemaGenerated = {
87
+ };\n `);
88
+ });
89
+ return `
90
+ export type DBGeneratedSchema = {
63
91
  ${tables.join("")}
64
92
  }
65
93
  `;
66
- }
67
-
68
- type ServerViewHandler<T extends AnyObject = AnyObject, Schema extends DBSchema | void = void> = ViewHandler<T, Schema> & { is_view: boolean; }
69
- type ServerTableHandler<T extends AnyObject = AnyObject, Schema extends DBSchema | void = void> = TableHandler<T, Schema> & { is_view: boolean; }
70
-
71
- export type DBTableHandlersFromSchema<Schema = void> = Schema extends DBSchema? {
72
- [tov_name in keyof Schema]: Schema[tov_name]["is_view"] extends true?
73
- ServerViewHandler<Schema[tov_name]["columns"], Schema> :
74
- ServerTableHandler<Schema[tov_name]["columns"], Schema>
75
- } : Record<string, Partial<ServerTableHandler>>;
94
+ };
95
+
96
+ type ServerViewHandler<
97
+ T extends AnyObject = AnyObject,
98
+ Schema extends DBSchema | void = void,
99
+ > = ViewHandler<T, Schema> & { is_view: boolean };
100
+ type ServerTableHandler<
101
+ T extends AnyObject = AnyObject,
102
+ Schema extends DBSchema | void = void,
103
+ > = TableHandler<T, Schema> & { is_view: boolean };
104
+
105
+ export type DBTableHandlersFromSchema<Schema = void> =
106
+ Schema extends DBSchema ?
107
+ {
108
+ [tov_name in keyof Schema]: Schema[tov_name]["is_view"] extends true ?
109
+ ServerViewHandler<Schema[tov_name]["columns"], Schema>
110
+ : ServerTableHandler<Schema[tov_name]["columns"], Schema>;
111
+ }
112
+ : Record<string, Partial<ServerTableHandler>>;
76
113
 
77
- export type DBHandlerServerExtra<TH = Record<string, Partial<ServerTableHandler>>, WithTransactions = true> = {
114
+ export type DBHandlerServerExtra<
115
+ TH = Record<string, Partial<ServerTableHandler>>,
116
+ WithTransactions = true,
117
+ > = {
78
118
  sql: SQLHandler;
79
- } & Partial<DbJoinMaker> & (
80
- WithTransactions extends true? { tx: TX<TH> } :
81
- Record<string, never>
82
- );
119
+ } & Partial<DbJoinMaker> &
120
+ (WithTransactions extends true ? { tx: TX<TH> } : Record<string, never>);
83
121
  // export type DBOFullyTyped<Schema = void> = Schema extends DBSchema? (
84
122
  // DBTableHandlersFromSchema<Schema> & DBHandlerServerExtra<DBTableHandlersFromSchema<Schema>>
85
- // ) :
123
+ // ) :
86
124
  // DBHandlerServer;
87
- export type DBOFullyTyped<Schema = void> = DBTableHandlersFromSchema<Schema> & DBHandlerServerExtra<DBTableHandlersFromSchema<Schema>>
88
-
89
- export type PublishFullyTyped<Schema = void> = Schema extends DBSchema? (
90
- | PublishAllOrNothing
91
- | {
92
- [tov_name in keyof Partial<Schema>]:
93
- | PublishAllOrNothing
94
- | (
95
- Schema[tov_name]["is_view"] extends true?
96
- PublishViewRule<Schema[tov_name]["columns"], Schema> :
97
- PublishTableRule<Schema[tov_name]["columns"], Schema>
98
- );
99
- }
100
- ) : (
101
- | PublishAllOrNothing
102
- | Record<string, PublishViewRule | PublishTableRule | PublishAllOrNothing>
103
- );
104
-
105
-
125
+ export type DBOFullyTyped<Schema = void> = DBTableHandlersFromSchema<Schema> &
126
+ DBHandlerServerExtra<DBTableHandlersFromSchema<Schema>>;
127
+
128
+ export type PublishFullyTyped<Schema = void> =
129
+ Schema extends DBSchema ?
130
+ | PublishAllOrNothing
131
+ | {
132
+ [tov_name in keyof Partial<Schema>]:
133
+ | PublishAllOrNothing
134
+ | (Schema[tov_name]["is_view"] extends true ?
135
+ PublishViewRule<Schema[tov_name]["columns"], Schema>
136
+ : PublishTableRule<Schema[tov_name]["columns"], Schema>);
137
+ }
138
+ : PublishAllOrNothing | Record<string, PublishViewRule | PublishTableRule | PublishAllOrNothing>;
106
139
 
107
140
  /** Type checks */
108
- (() => {
109
-
141
+ () => {
110
142
  const ddb: DBOFullyTyped = 1 as any;
111
143
  ddb.dwad?.insert;
112
144
  ddb.dwad?.delete;
@@ -124,102 +156,100 @@ export type PublishFullyTyped<Schema = void> = Schema extends DBSchema? (
124
156
  dbConnection: 1 as any,
125
157
  publish: async (params) => {
126
158
  const _rows = await params.dbo.dwadwa?.find?.({});
127
-
128
- return "*" as const
159
+
160
+ return "*" as const;
129
161
  },
130
162
  transactions: true,
131
163
  onReady: ({ dbo }) => {
132
164
  dbo.tdwa?.find!();
133
- dbo.tx?.(t => {
165
+ dbo.tx?.((t) => {
134
166
  t.dwa?.find!();
135
- })
136
- }
167
+ });
168
+ },
137
169
  });
138
170
 
139
-
140
171
  const _auth: Auth = {
141
172
  sidKeyName: "sid_token",
142
173
  getUser: async (sid, db, _db) => {
143
174
  db.dwadaw?.find;
144
175
  return 1 as any;
145
- }
146
- }
147
-
176
+ },
177
+ };
148
178
 
149
179
  type S = {
150
180
  tbl1: {
151
181
  columns: {
152
182
  col1: number | null;
153
- col2: string;
154
- }
155
- },
183
+ col2: string;
184
+ };
185
+ };
156
186
  tbl2: {
157
187
  columns: {
158
188
  col1: number | null;
159
- col2: string;
160
- }
161
- }
162
- }
189
+ col2: string;
190
+ };
191
+ };
192
+ };
163
193
 
164
194
  /** Test the created schema */
165
195
  const c: S = 1 as any;
166
196
  const _test: DBSchema = c;
167
197
  const dbt: DBOFullyTyped<S> = 1 as any;
168
198
 
169
- dbt.tx!(t => {
199
+ dbt.tx!((t) => {
170
200
  t.tbl1.delete();
171
201
  });
172
202
 
173
203
  const db: DBHandlerServer = 1 as any;
174
- db.tx!(t => {
175
- t.wadwa?.find!()
204
+ db.tx!((t) => {
205
+ t.wadwa?.find!();
176
206
  });
177
-
207
+
178
208
  const _publish = (): PublishFullyTyped<S> => {
179
209
  const r = {
180
210
  tbl1: {
181
211
  select: {
182
- fields: "*" as const,
183
- forcedFilter: { col1: 32, col2: "" }
212
+ fields: "*" as const,
213
+ forcedFilter: { col1: 32, col2: "" },
184
214
  },
185
215
  getColumns: true,
186
216
  getInfo: true,
187
217
  delete: {
188
- filterFields: {col1: 1}
189
- }
218
+ filterFields: { col1: 1 },
219
+ },
190
220
  },
191
221
  tbl2: {
192
222
  delete: {
193
223
  filterFields: "*" as const,
194
- forcedFilter: {col1: 2}
195
- }
196
- }
197
- }
224
+ forcedFilter: { col1: 2 },
225
+ },
226
+ },
227
+ };
198
228
  const res: PublishFullyTyped<S> = {
199
229
  tbl1: {
200
230
  select: {
201
231
  fields: "*",
202
- forcedFilter: { col1: 32, col2: "" }
232
+ forcedFilter: { col1: 32, col2: "" },
203
233
  },
204
234
  getColumns: true,
205
235
  getInfo: true,
206
236
  delete: {
207
- filterFields: { col1: 1 }
208
- }
237
+ filterFields: { col1: 1 },
238
+ },
209
239
  },
210
240
  tbl2: {
211
241
  delete: {
212
242
  filterFields: "*" as const,
213
- forcedFilter: { col1: 2 }
214
- }
215
- }
216
- }
217
- const _res1: PublishFullyTyped = r
218
-
243
+ forcedFilter: { col1: 2 },
244
+ },
245
+ },
246
+ };
247
+ const _res1: PublishFullyTyped = r;
248
+
219
249
  const p: PublishParams<undefined> = 1 as any;
220
-
250
+
221
251
  p.dbo.dwadaw?.find?.();
222
-
252
+
223
253
  return res;
224
- }
225
- })
254
+ };
255
+ };
@@ -82,7 +82,7 @@ type PrepareFieldValuesArgs = {
82
82
  row: AnyObject | undefined;
83
83
  forcedData: AnyObject | undefined;
84
84
  allowedCols: FieldFilter | undefined;
85
- removeDisallowedColumns?: boolean;
85
+ removeDisallowedFields?: boolean;
86
86
  tableHandler: TableHandler;
87
87
  }
88
88
  /**
@@ -94,7 +94,7 @@ type PrepareFieldValuesArgs = {
94
94
  * @param {Object} forcedData - set/override property
95
95
  * @param {string[]} allowed_cols - allowed columns (excluding forcedData) from table rules
96
96
  */
97
- const getValidatedRow = ({ row = {}, forcedData = {}, allowedCols, removeDisallowedColumns = false, tableHandler }: PrepareFieldValuesArgs): AnyObject => {
97
+ const getValidatedRow = ({ row = {}, forcedData = {}, allowedCols, removeDisallowedFields = false, tableHandler }: PrepareFieldValuesArgs): AnyObject => {
98
98
  const column_names = tableHandler.column_names.slice(0);
99
99
  if (!column_names.length) {
100
100
  throw "table column_names mising";
@@ -102,7 +102,7 @@ const getValidatedRow = ({ row = {}, forcedData = {}, allowedCols, removeDisallo
102
102
  const validatedAllowedColumns = tableHandler.parseFieldFilter(allowedCols, false);
103
103
 
104
104
  let finalRow = { ...row };
105
- if (removeDisallowedColumns && !isEmpty(finalRow)) {
105
+ if (removeDisallowedFields && !isEmpty(finalRow)) {
106
106
  finalRow = pickKeys(finalRow, validatedAllowedColumns);
107
107
  }
108
108
 
@@ -124,7 +124,7 @@ const getValidatedRow = ({ row = {}, forcedData = {}, allowedCols, removeDisallo
124
124
  * prepareFieldValues(): Apply forcedData, remove disallowed columns, validate against allowed columns
125
125
  * tableConfigurator?.checkColVal(): Validate column min/max/isText/lowerCased/trimmed values
126
126
  */
127
- export const prepareNewData = async ({ row, forcedData, allowedFields, tableRules, fixIssues = false, tableConfigurator, tableHandler }: ValidatedParams) => {
127
+ export const prepareNewData = async ({ row, forcedData, allowedFields, tableRules, removeDisallowedFields = false, tableConfigurator, tableHandler }: ValidatedParams) => {
128
128
  const synced_field = (tableRules ?? {})?.sync?.synced_field;
129
129
 
130
130
  /* Update synced_field if sync is on and missing */
@@ -132,7 +132,7 @@ export const prepareNewData = async ({ row, forcedData, allowedFields, tableRule
132
132
  row[synced_field] = Date.now();
133
133
  }
134
134
 
135
- const data = getValidatedRow({ tableHandler, row, forcedData, allowedCols: allowedFields , removeDisallowedColumns: fixIssues });
135
+ const data = getValidatedRow({ tableHandler, row, forcedData, allowedCols: allowedFields, removeDisallowedFields });
136
136
  const dataKeys = getKeys(data);
137
137
 
138
138
  dataKeys.forEach(col => {
@@ -22,7 +22,7 @@ export type ValidatedParams = {
22
22
  forcedData?: AnyObject;
23
23
  allowedFields?: FieldFilter;
24
24
  tableRules?: TableRule;
25
- fixIssues: boolean;
25
+ removeDisallowedFields: boolean;
26
26
  tableConfigurator: TableConfigurator | undefined;
27
27
  tableHandler: TableHandler;
28
28
  }
@@ -13,7 +13,7 @@ export async function insert(this: TableHandler, rowOrRows: AnyObject | AnyObjec
13
13
  const start = Date.now();
14
14
  try {
15
15
 
16
- const { fixIssues = false } = insertParams || {};
16
+ const { removeDisallowedFields = false } = insertParams || {};
17
17
  const { returnQuery = false, nestedInsert } = localParams || {};
18
18
 
19
19
  const finalDBtx = this.getFinalDBtx(localParams);
@@ -79,7 +79,7 @@ export async function insert(this: TableHandler, rowOrRows: AnyObject | AnyObjec
79
79
  forcedData,
80
80
  allowedFields: fields,
81
81
  tableRules,
82
- fixIssues,
82
+ removeDisallowedFields,
83
83
  tableConfigurator: this.dboBuilder.prostgles.tableConfigurator,
84
84
  tableHandler: this,
85
85
  });
@@ -165,7 +165,7 @@ const validateInsertParams = (params: InsertParams | undefined) => {
165
165
  }
166
166
 
167
167
  if (params) {
168
- const good_paramsObj: Record<keyof InsertParams, 1> = { returning: 1, returnType: 1, fixIssues: 1, onConflict: 1 };
168
+ const good_paramsObj: Record<keyof InsertParams, 1> = { returning: 1, returnType: 1, removeDisallowedFields: 1, onConflict: 1 };
169
169
  const good_params = Object.keys(good_paramsObj);
170
170
  const bad_params = Object.keys(params).filter(k => !good_params.includes(k));
171
171
  if (bad_params && bad_params.length) throw "Invalid params: " + bad_params.join(", ") + " \n Expecting: " + good_params.join(", ");
@@ -1,30 +1,46 @@
1
1
  import { AnyObject, UpdateParams } from "prostgles-types";
2
2
  import { TableRule } from "../../PublishParser/PublishParser";
3
- import { Filter, LocalParams, getErrorAsObject, getSerializedClientErrorFromPGError, withUserRLS } from "../DboBuilder";
3
+ import {
4
+ Filter,
5
+ LocalParams,
6
+ getErrorAsObject,
7
+ getSerializedClientErrorFromPGError,
8
+ withUserRLS,
9
+ } from "../DboBuilder";
4
10
  import { getInsertTableRules, getReferenceColumnInserts } from "../insertNestedRecords";
5
11
  import { prepareNewData } from "./DataValidator";
6
12
  import { runInsertUpdateQuery } from "./runInsertUpdateQuery";
7
13
  import { TableHandler } from "./TableHandler";
8
14
  import { updateFile } from "./updateFile";
9
15
 
10
- export async function update(this: TableHandler, filter: Filter, _newData: AnyObject, params?: UpdateParams, tableRules?: TableRule, localParams?: LocalParams): Promise<AnyObject | void> {
16
+ export async function update(
17
+ this: TableHandler,
18
+ filter: Filter,
19
+ _newData: AnyObject,
20
+ params?: UpdateParams,
21
+ tableRules?: TableRule,
22
+ localParams?: LocalParams
23
+ ): Promise<AnyObject | void> {
11
24
  const ACTION = "update";
12
25
  const start = Date.now();
13
26
  try {
14
27
  /** postValidate */
15
28
  const finalDBtx = this.getFinalDBtx(localParams);
16
- const wrapInTx = () => this.dboBuilder.getTX(_dbtx => _dbtx[this.name]?.[ACTION]?.(filter, _newData, params, tableRules, localParams))
17
- const rule = tableRules?.[ACTION]
18
- if(rule?.postValidate && !finalDBtx){
29
+ const wrapInTx = () =>
30
+ this.dboBuilder.getTX((_dbtx) =>
31
+ _dbtx[this.name]?.[ACTION]?.(filter, _newData, params, tableRules, localParams)
32
+ );
33
+ const rule = tableRules?.[ACTION];
34
+ if (rule?.postValidate && !finalDBtx) {
19
35
  return wrapInTx();
20
36
  }
21
37
 
22
38
  let newData = _newData;
23
- if(this.is_media){
39
+ if (this.is_media) {
24
40
  ({ newData } = await updateFile.bind(this)({ newData, filter, localParams, tableRules }));
25
41
  }
26
42
 
27
- const parsedRules = await this.parseUpdateRules(filter, params, tableRules, localParams)
43
+ const parsedRules = await this.parseUpdateRules(filter, params, tableRules, localParams);
28
44
  if (localParams?.testRule) {
29
45
  return parsedRules;
30
46
  }
@@ -33,25 +49,34 @@ export async function update(this: TableHandler, filter: Filter, _newData: AnyOb
33
49
  throw "no update data provided\nEXPECTING db.table.update(filter, updateData, options)";
34
50
  }
35
51
 
36
- const { fields, validateRow, forcedData, returningFields, forcedFilter, filterFields } = parsedRules;
37
- const { onConflict, fixIssues = false } = params || {};
52
+ const { fields, validateRow, forcedData, returningFields, forcedFilter, filterFields } =
53
+ parsedRules;
54
+ const { removeDisallowedFields = false } = params || {};
38
55
  const { returnQuery = false } = localParams ?? {};
39
56
 
40
57
  if (params) {
41
- const good_paramsObj: Record<keyof UpdateParams, 1> = { returning: 1, returnType: 1, fixIssues: 1, onConflict: 1, multi: 1 };
58
+ const good_paramsObj: Record<keyof UpdateParams, 1> = {
59
+ returning: 1,
60
+ returnType: 1,
61
+ removeDisallowedFields: 1,
62
+ multi: 1,
63
+ };
42
64
  const good_params = Object.keys(good_paramsObj);
43
- const bad_params = Object.keys(params).filter(k => !good_params.includes(k));
44
- if (bad_params && bad_params.length) throw "Invalid params: " + bad_params.join(", ") + " \n Expecting: " + good_params.join(", ");
65
+ const bad_params = Object.keys(params).filter((k) => !good_params.includes(k));
66
+ if (bad_params && bad_params.length)
67
+ throw (
68
+ "Invalid params: " + bad_params.join(", ") + " \n Expecting: " + good_params.join(", ")
69
+ );
45
70
  }
46
71
 
47
- const { data, allowedCols } = await prepareNewData({
48
- row: newData,
49
- forcedData,
50
- allowedFields: fields,
51
- tableRules,
52
- fixIssues,
72
+ const { data, allowedCols } = await prepareNewData({
73
+ row: newData,
74
+ forcedData,
75
+ allowedFields: fields,
76
+ tableRules,
77
+ removeDisallowedFields,
53
78
  tableConfigurator: this.dboBuilder.prostgles.tableConfigurator,
54
- tableHandler: this,
79
+ tableHandler: this,
55
80
  });
56
81
 
57
82
  const updateFilter = await this.prepareWhere({
@@ -60,46 +85,68 @@ export async function update(this: TableHandler, filter: Filter, _newData: AnyOb
60
85
  forcedFilter,
61
86
  filterFields,
62
87
  localParams,
63
- tableRule: tableRules
64
- })
65
-
88
+ tableRule: tableRules,
89
+ });
90
+
66
91
  /**
67
92
  * Nested inserts
68
93
  */
69
94
  const nData = { ...data };
70
95
  const nestedInserts = getReferenceColumnInserts(this, nData, true);
71
96
  const nestedInsertsResultsObj: Record<string, any> = {};
72
- if(nestedInserts.length){
97
+ if (nestedInserts.length) {
73
98
  const updateCount = await this.count(updateFilter.filter);
74
- if(+updateCount > 1){
99
+ if (+updateCount > 1) {
75
100
  throw "Cannot do a nestedInsert from an update that targets more than 1 row";
76
101
  }
77
- if(!finalDBtx){
102
+ if (!finalDBtx) {
78
103
  return wrapInTx();
79
104
  }
80
- await Promise.all(nestedInserts.map(async nestedInsert => {
81
- const nesedTableHandler = finalDBtx[nestedInsert.tableName] as TableHandler | undefined;
82
- if(!nesedTableHandler) throw `nestedInsert Tablehandler not found for ${nestedInsert.tableName}`;
83
- const refTableRules = !localParams? undefined : await getInsertTableRules(this, nestedInsert.tableName, localParams);
84
- const nestedLocalParams: LocalParams = { ...localParams, nestedInsert: { depth: 1, previousData: nData, previousTable: this.name, referencingColumn: nestedInsert.col } }
85
- const nestedInsertResult = await nesedTableHandler.insert(nestedInsert.data, { returning: "*" }, undefined, refTableRules, nestedLocalParams);
86
- nestedInsertsResultsObj[nestedInsert.col] = nestedInsertResult;
105
+ await Promise.all(
106
+ nestedInserts.map(async (nestedInsert) => {
107
+ const nesedTableHandler = finalDBtx[nestedInsert.tableName] as TableHandler | undefined;
108
+ if (!nesedTableHandler)
109
+ throw `nestedInsert Tablehandler not found for ${nestedInsert.tableName}`;
110
+ const refTableRules = !localParams
111
+ ? undefined
112
+ : await getInsertTableRules(this, nestedInsert.tableName, localParams);
113
+ const nestedLocalParams: LocalParams = {
114
+ ...localParams,
115
+ nestedInsert: {
116
+ depth: 1,
117
+ previousData: nData,
118
+ previousTable: this.name,
119
+ referencingColumn: nestedInsert.col,
120
+ },
121
+ };
122
+ const nestedInsertResult = await nesedTableHandler.insert(
123
+ nestedInsert.data,
124
+ { returning: "*" },
125
+ undefined,
126
+ refTableRules,
127
+ nestedLocalParams
128
+ );
129
+ nestedInsertsResultsObj[nestedInsert.col] = nestedInsertResult;
87
130
 
88
- nData[nestedInsert.col] = nestedInsertResult[nestedInsert.fcol];
89
- return {
90
- ...nestedInsert,
91
- result: nestedInsertResult,
92
- }
93
- }));
131
+ nData[nestedInsert.col] = nestedInsertResult[nestedInsert.fcol];
132
+ return {
133
+ ...nestedInsert,
134
+ result: nestedInsertResult,
135
+ };
136
+ })
137
+ );
94
138
  }
95
139
 
96
- // let query = await this.colSet.getUpdateQuery(nData, allowedCols, this.getFinalDbo(localParams), validateRow, localParams)
97
- let query = (await this.dataValidator.parse({ command: "update", rows: [nData], allowedCols, dbTx: this.getFinalDbo(localParams), validationOptions: { validate: validateRow, localParams }})).getQuery()
140
+ let query = (
141
+ await this.dataValidator.parse({
142
+ command: "update",
143
+ rows: [nData],
144
+ allowedCols,
145
+ dbTx: this.getFinalDbo(localParams),
146
+ validationOptions: { validate: validateRow, localParams },
147
+ })
148
+ ).getQuery();
98
149
  query += "\n" + updateFilter.where;
99
- if (onConflict === "DoNothing") query += " ON CONFLICT DO NOTHING ";
100
- if(onConflict === "DoUpdate"){
101
- throw "onConflict 'DoUpdate' not possible for an update";
102
- }
103
150
  const queryWithoutUserRLS = query;
104
151
  query = withUserRLS(localParams, query);
105
152
 
@@ -115,12 +162,23 @@ export async function update(this: TableHandler, filter: Filter, _newData: AnyOb
115
162
  returningFields,
116
163
  rule,
117
164
  type: "update",
118
- nestedInsertsResultsObj
165
+ nestedInsertsResultsObj,
166
+ });
167
+ await this._log({
168
+ command: "update",
169
+ localParams,
170
+ data: { filter, _newData, params },
171
+ duration: Date.now() - start,
119
172
  });
120
- await this._log({ command: "update", localParams, data: { filter, _newData, params }, duration: Date.now() - start });
121
173
  return result;
122
174
  } catch (e) {
123
- await this._log({ command: "update", localParams, data: { filter, _newData, params }, duration: Date.now() - start, error: getErrorAsObject(e) });
175
+ await this._log({
176
+ command: "update",
177
+ localParams,
178
+ data: { filter, _newData, params },
179
+ duration: Date.now() - start,
180
+ error: getErrorAsObject(e),
181
+ });
124
182
  throw getSerializedClientErrorFromPGError(e, { type: "tableMethod", localParams, view: this });
125
183
  }
126
- }
184
+ }