@budibase/server 2.1.40-alpha.4 → 2.1.41

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 (787) hide show
  1. package/__mocks__/aws-sdk.ts +0 -14
  2. package/__mocks__/node-fetch.ts +1 -5
  3. package/builder/assets/{index.ddf1f73a.js → index.2467ba90.js} +324 -324
  4. package/builder/assets/{index.144dddb2.css → index.534f6c29.css} +2 -2
  5. package/builder/index.html +2 -2
  6. package/coverage/clover.xml +3741 -10731
  7. package/coverage/coverage-final.json +118 -266
  8. package/coverage/lcov-report/{src/api/controllers/apikeys.ts.html → api/controllers/apikeys.js.html} +26 -26
  9. package/coverage/lcov-report/{src/api/controllers/automation.ts.html → api/controllers/automation.js.html} +75 -75
  10. package/coverage/lcov-report/{src/api/controllers/cloud.ts.html → api/controllers/cloud.js.html} +47 -47
  11. package/coverage/lcov-report/{src/api/controllers/datasource.ts.html → api/controllers/datasource.js.html} +87 -246
  12. package/coverage/lcov-report/{src/api/controllers/dev.ts.html → api/controllers/dev.js.html} +71 -53
  13. package/coverage/lcov-report/api/controllers/index.html +326 -0
  14. package/coverage/lcov-report/{src/api/controllers/integration.ts.html → api/controllers/integration.js.html} +22 -19
  15. package/coverage/lcov-report/{src/api/controllers/layout.ts.html → api/controllers/layout.js.html} +26 -26
  16. package/coverage/lcov-report/{src/api/controllers/metadata.ts.html → api/controllers/metadata.js.html} +23 -26
  17. package/coverage/lcov-report/{src/api/controllers/migrations.ts.html → api/controllers/migrations.js.html} +15 -18
  18. package/coverage/lcov-report/{src/api/controllers/permission.ts.html → api/controllers/permission.js.html} +71 -80
  19. package/coverage/lcov-report/{src/utilities/routing → api/controllers/query}/index.html +8 -8
  20. package/coverage/lcov-report/{src/api/controllers/query/validation.ts.html → api/controllers/query/validation.js.html} +17 -17
  21. package/coverage/lcov-report/{src/api/controllers/role.ts.html → api/controllers/role.js.html} +56 -44
  22. package/coverage/lcov-report/{src/api/controllers/routing.ts.html → api/controllers/routing.js.html} +56 -65
  23. package/coverage/lcov-report/{src/api/controllers/row/external.ts.html → api/controllers/row/external.js.html} +94 -145
  24. package/coverage/lcov-report/{src/api → api}/controllers/row/index.html +52 -82
  25. package/coverage/lcov-report/{src/api/controllers/row/internal.ts.html → api/controllers/row/internal.js.html} +95 -137
  26. package/coverage/lcov-report/{src/api/controllers/row/internalSearch.ts.html → api/controllers/row/internalSearch.js.html} +110 -182
  27. package/coverage/lcov-report/{src/api/controllers/row/staticFormula.ts.html → api/controllers/row/staticFormula.js.html} +51 -66
  28. package/coverage/lcov-report/{src/api/controllers/row/utils.ts.html → api/controllers/row/utils.js.html} +47 -77
  29. package/coverage/lcov-report/{src/api/controllers/script.ts.html → api/controllers/script.js.html} +14 -17
  30. package/coverage/lcov-report/{src/api/controllers/table/bulkFormula.ts.html → api/controllers/table/bulkFormula.js.html} +54 -96
  31. package/coverage/lcov-report/{src/api/controllers/table/external.ts.html → api/controllers/table/external.js.html} +103 -196
  32. package/coverage/lcov-report/{src/utilities/fileSystem → api/controllers/table}/index.html +44 -44
  33. package/coverage/lcov-report/{src/api/controllers/table/index.ts.html → api/controllers/table/index.js.html} +37 -46
  34. package/coverage/lcov-report/{src/api/controllers/templates.ts.html → api/controllers/templates.js.html} +25 -34
  35. package/coverage/lcov-report/{src/api/controllers/user.ts.html → api/controllers/user.js.html} +85 -79
  36. package/coverage/lcov-report/{src/api/controllers/view/exporters.ts.html → api/controllers/view/exporters.js.html} +14 -20
  37. package/coverage/lcov-report/{src/api → api}/controllers/view/index.html +36 -36
  38. package/coverage/lcov-report/{src/api/controllers/view/index.ts.html → api/controllers/view/index.js.html} +48 -75
  39. package/coverage/lcov-report/{src/api/controllers/view/utils.ts.html → api/controllers/view/utils.js.html} +43 -61
  40. package/coverage/lcov-report/{src/api/controllers/view/viewBuilder.ts.html → api/controllers/view/viewBuilder.js.html} +32 -74
  41. package/coverage/lcov-report/{src/api → api}/index.html +23 -23
  42. package/coverage/lcov-report/{src/api/index.ts.html → api/index.js.html} +56 -35
  43. package/coverage/lcov-report/{src/api/routes/analytics.ts.html → api/routes/analytics.js.html} +14 -14
  44. package/coverage/lcov-report/{src/middleware/selfhost.ts.html → api/routes/apikeys.js.html} +27 -27
  45. package/coverage/lcov-report/{src/api/routes/automation.ts.html → api/routes/automation.js.html} +42 -48
  46. package/coverage/lcov-report/{src/api/routes/backup.ts.html → api/routes/cloud.js.html} +21 -21
  47. package/coverage/lcov-report/{src/api/routes/component.ts.html → api/routes/component.js.html} +17 -17
  48. package/coverage/lcov-report/{src/constants/screens.ts.html → api/routes/datasource.js.html} +65 -56
  49. package/coverage/lcov-report/{src/api/routes/cloud.ts.html → api/routes/deploy.js.html} +22 -25
  50. package/coverage/lcov-report/{src/automations/index.ts.html → api/routes/dev.js.html} +42 -42
  51. package/coverage/lcov-report/{src/api → api}/routes/index.html +47 -167
  52. package/coverage/lcov-report/{src/api/routes/apikeys.ts.html → api/routes/integration.js.html} +18 -18
  53. package/coverage/lcov-report/{src/api/routes/layout.ts.html → api/routes/layout.js.html} +18 -18
  54. package/coverage/lcov-report/{src/api/routes/metadata.ts.html → api/routes/metadata.js.html} +24 -24
  55. package/coverage/lcov-report/{src/sdk/index.ts.html → api/routes/migrations.js.html} +21 -24
  56. package/coverage/lcov-report/{src/api/routes/permission.ts.html → api/routes/permission.js.html} +24 -48
  57. package/coverage/lcov-report/{src/api/routes/query.ts.html → api/routes/query.js.html} +42 -33
  58. package/coverage/lcov-report/{src/db/index.ts.html → api/routes/role.js.html} +25 -28
  59. package/coverage/lcov-report/{src/api/routes/routing.ts.html → api/routes/routing.js.html} +17 -17
  60. package/coverage/lcov-report/api/routes/screen.js.html +139 -0
  61. package/coverage/lcov-report/{src/api/routes/script.ts.html → api/routes/script.js.html} +17 -17
  62. package/coverage/lcov-report/{src/api/routes/table.ts.html → api/routes/table.js.html} +33 -24
  63. package/coverage/lcov-report/{src/api/routes/templates.ts.html → api/routes/templates.js.html} +18 -18
  64. package/coverage/lcov-report/{src/api/routes/user.ts.html → api/routes/user.js.html} +35 -29
  65. package/coverage/lcov-report/{src/api → api}/routes/utils/index.html +15 -15
  66. package/coverage/lcov-report/{src/api/routes/utils/validators.ts.html → api/routes/utils/validators.js.html} +58 -46
  67. package/coverage/lcov-report/{src/api/routes/view.ts.html → api/routes/view.js.html} +31 -40
  68. package/coverage/lcov-report/{src/automations/actions.ts.html → automations/actions.js.html} +41 -50
  69. package/coverage/lcov-report/{src/automations/automationUtils.ts.html → automations/automationUtils.js.html} +27 -30
  70. package/coverage/lcov-report/{src/automations/bullboard.ts.html → automations/bullboard.js.html} +33 -30
  71. package/coverage/lcov-report/{src/automations → automations}/index.html +48 -63
  72. package/coverage/lcov-report/{src/websocket.ts.html → automations/index.js.html} +50 -41
  73. package/coverage/lcov-report/{src/automations/steps/bash.ts.html → automations/steps/bash.js.html} +25 -40
  74. package/coverage/lcov-report/{src/automations/steps/delay.ts.html → automations/steps/delay.js.html} +17 -32
  75. package/coverage/lcov-report/{src/automations/steps/discord.ts.html → automations/steps/discord.js.html} +23 -38
  76. package/coverage/lcov-report/{src/automations/steps/executeQuery.ts.html → automations/steps/executeQuery.js.html} +26 -41
  77. package/coverage/lcov-report/{src/automations/steps/executeScript.ts.html → automations/steps/executeScript.js.html} +24 -54
  78. package/coverage/lcov-report/{src/automations/steps/filter.ts.html → automations/steps/filter.js.html} +27 -36
  79. package/coverage/lcov-report/automations/steps/index.html +341 -0
  80. package/coverage/lcov-report/{src/automations/steps/integromat.ts.html → automations/steps/integromat.js.html} +20 -35
  81. package/coverage/lcov-report/{src/automations/steps/loop.ts.html → automations/steps/loop.js.html} +15 -21
  82. package/coverage/lcov-report/{src/automations/steps/outgoingWebhook.ts.html → automations/steps/outgoingWebhook.js.html} +33 -48
  83. package/coverage/lcov-report/{src/automations/steps/queryRows.ts.html → automations/steps/queryRows.js.html} +58 -94
  84. package/coverage/lcov-report/{src/automations/steps/sendSmtpEmail.ts.html → automations/steps/sendSmtpEmail.js.html} +22 -37
  85. package/coverage/lcov-report/{src/automations/steps/serverLog.ts.html → automations/steps/serverLog.js.html} +20 -38
  86. package/coverage/lcov-report/{src/automations/steps/slack.ts.html → automations/steps/slack.js.html} +20 -35
  87. package/coverage/lcov-report/{src/automations/steps/updateRow.ts.html → automations/steps/updateRow.js.html} +21 -36
  88. package/coverage/lcov-report/{src/automations/steps/utils.ts.html → automations/steps/utils.js.html} +26 -44
  89. package/coverage/lcov-report/{src/automations/steps/zapier.ts.html → automations/steps/zapier.js.html} +20 -38
  90. package/coverage/lcov-report/{src/definitions/automations.ts.html → automations/triggerInfo/app.js.html} +42 -42
  91. package/coverage/lcov-report/{src/automations/triggerInfo/cron.ts.html → automations/triggerInfo/cron.js.html} +15 -30
  92. package/coverage/lcov-report/{src/automations → automations}/triggerInfo/index.html +31 -31
  93. package/coverage/lcov-report/{src/automations/triggerInfo/index.ts.html → automations/triggerInfo/index.js.html} +17 -17
  94. package/coverage/lcov-report/{src/automations/triggerInfo/rowDeleted.ts.html → automations/triggerInfo/rowDeleted.js.html} +15 -30
  95. package/coverage/lcov-report/{src/automations/triggerInfo/rowSaved.ts.html → automations/triggerInfo/rowSaved.js.html} +15 -30
  96. package/coverage/lcov-report/{src/automations/triggerInfo/rowUpdated.ts.html → automations/triggerInfo/rowUpdated.js.html} +15 -30
  97. package/coverage/lcov-report/{src/automations/triggerInfo/webhook.ts.html → automations/triggerInfo/webhook.js.html} +15 -30
  98. package/coverage/lcov-report/{src/automations/triggers.ts.html → automations/triggers.js.html} +47 -59
  99. package/coverage/lcov-report/{src/constants → constants}/index.html +26 -26
  100. package/coverage/lcov-report/{src/constants/index.ts.html → constants/index.js.html} +185 -173
  101. package/coverage/lcov-report/{src/constants/layouts.ts.html → constants/layouts.js.html} +34 -16
  102. package/coverage/lcov-report/{src/definitions/datasource.ts.html → constants/screens.js.html} +60 -72
  103. package/coverage/lcov-report/{src/db/inMemoryView.ts.html → db/inMemoryView.js.html} +26 -44
  104. package/coverage/lcov-report/{src/db → db}/index.html +33 -48
  105. package/coverage/lcov-report/{src/api/routes/integration.ts.html → db/index.js.html} +36 -36
  106. package/coverage/lcov-report/{src/db/linkedRows/LinkController.ts.html → db/linkedRows/LinkController.js.html} +155 -92
  107. package/coverage/lcov-report/db/linkedRows/index.html +146 -0
  108. package/coverage/lcov-report/{src/db/linkedRows/index.ts.html → db/linkedRows/index.js.html} +68 -83
  109. package/coverage/lcov-report/{src/db/linkedRows/linkUtils.ts.html → db/linkedRows/linkUtils.js.html} +46 -73
  110. package/coverage/lcov-report/{src/db/newid.ts.html → db/newid.js.html} +11 -11
  111. package/coverage/lcov-report/{src/db/utils.ts.html → db/utils.js.html} +153 -105
  112. package/coverage/lcov-report/{src/events/AutomationEmitter.ts.html → events/AutomationEmitter.js.html} +21 -36
  113. package/coverage/lcov-report/{src/events → events}/index.html +33 -48
  114. package/coverage/lcov-report/{src/events/BudibaseEmitter.ts.html → events/index.js.html} +26 -23
  115. package/coverage/lcov-report/{src/automations/triggerInfo/app.ts.html → events/utils.js.html} +72 -69
  116. package/coverage/lcov-report/index.html +184 -679
  117. package/coverage/lcov-report/{src/integrations → integrations}/tests/TestConfiguration.js.html +10 -10
  118. package/coverage/lcov-report/{src/integrations → integrations}/tests/index.html +10 -10
  119. package/coverage/lcov-report/{src/middleware/appInfo.ts.html → middleware/appInfo.js.html} +28 -31
  120. package/coverage/lcov-report/{src/middleware/builder.ts.html → middleware/builder.js.html} +37 -43
  121. package/coverage/lcov-report/{src/middleware/currentapp.ts.html → middleware/currentapp.js.html} +68 -77
  122. package/coverage/lcov-report/{src/middleware → middleware}/index.html +63 -78
  123. package/coverage/lcov-report/{src/middleware/joi-validator.ts.html → middleware/joi-validator.js.html} +22 -25
  124. package/coverage/lcov-report/{src/middleware/publicApi.ts.html → middleware/publicApi.js.html} +24 -24
  125. package/coverage/lcov-report/{src/middleware/resourceId.ts.html → middleware/resourceId.js.html} +25 -40
  126. package/coverage/lcov-report/{src/tests/utilities/index.ts.html → middleware/selfhost.js.html} +30 -33
  127. package/coverage/lcov-report/{src/middleware/utils.ts.html → middleware/utils.js.html} +13 -19
  128. package/coverage/lcov-report/{src/utilities/budibaseDir.ts.html → utilities/budibaseDir.js.html} +12 -12
  129. package/coverage/lcov-report/{src/utilities/centralPath.ts.html → utilities/centralPath.js.html} +13 -13
  130. package/coverage/lcov-report/{src/utilities/csvParser.ts.html → utilities/csvParser.js.html} +56 -104
  131. package/coverage/lcov-report/{src/utilities/global.ts.html → utilities/global.js.html} +74 -107
  132. package/coverage/lcov-report/{src/utilities → utilities}/index.html +77 -92
  133. package/coverage/lcov-report/{src/utilities/index.ts.html → utilities/index.js.html} +54 -78
  134. package/coverage/lcov-report/{src/utilities/plugins.ts.html → utilities/plugins.js.html} +19 -22
  135. package/coverage/lcov-report/{src/integrations/airtable.ts.html → utilities/queue/inMemoryQueue.js.html} +135 -213
  136. package/coverage/lcov-report/{src/migrations → utilities/queue}/index.html +23 -23
  137. package/coverage/lcov-report/{src/sdk → utilities/routing}/index.html +10 -10
  138. package/coverage/lcov-report/{src/api/routes/role.ts.html → utilities/routing/index.js.html} +42 -39
  139. package/coverage/lcov-report/{src/definitions → utilities/rowProcessor}/index.html +34 -34
  140. package/coverage/lcov-report/{src/utilities/rowProcessor/index.ts.html → utilities/rowProcessor/index.js.html} +346 -115
  141. package/coverage/lcov-report/{src/utilities/rowProcessor/utils.ts.html → utilities/rowProcessor/utils.js.html} +38 -50
  142. package/coverage/lcov-report/{src/utilities/scriptRunner.ts.html → utilities/scriptRunner.js.html} +16 -28
  143. package/coverage/lcov-report/{src/utilities/security.ts.html → utilities/security.js.html} +74 -56
  144. package/coverage/lcov-report/{src/utilities/statusCodes.ts.html → utilities/statusCodes.js.html} +11 -11
  145. package/coverage/lcov-report/{src/utilities → utilities}/usageQuota/index.html +28 -28
  146. package/coverage/lcov-report/{src/utilities/usageQuota/rows.ts.html → utilities/usageQuota/rows.js.html} +46 -34
  147. package/coverage/lcov-report/{src/utilities/usageQuota/usageQuoteReset.ts.html → utilities/usageQuota/usageQuoteReset.js.html} +13 -13
  148. package/coverage/lcov-report/{src/utilities/users.ts.html → utilities/users.js.html} +35 -29
  149. package/coverage/lcov-report/{src/utilities/workerRequests.ts.html → utilities/workerRequests.js.html} +114 -129
  150. package/coverage/lcov.info +6987 -19540
  151. package/dist/api/controllers/apikeys.js +9 -12
  152. package/dist/api/controllers/application.js +45 -48
  153. package/dist/api/controllers/automation.js +82 -122
  154. package/dist/api/controllers/cloud.js +71 -84
  155. package/dist/api/controllers/component.js +2 -4
  156. package/dist/api/controllers/datasource.js +92 -144
  157. package/dist/api/controllers/deploy/Deployment.js +6 -10
  158. package/dist/api/controllers/deploy/index.js +16 -13
  159. package/dist/api/controllers/dev.js +81 -96
  160. package/dist/api/controllers/integration.js +8 -11
  161. package/dist/api/controllers/layout.js +15 -18
  162. package/dist/api/controllers/metadata.js +37 -51
  163. package/dist/api/controllers/migrations.js +11 -19
  164. package/dist/api/controllers/permission.js +43 -52
  165. package/dist/api/controllers/plugin/index.js +7 -6
  166. package/dist/api/controllers/plugin/utils.js +1 -2
  167. package/dist/api/controllers/public/applications.js +9 -17
  168. package/dist/api/controllers/public/tables.js +9 -29
  169. package/dist/api/controllers/public/utils.js +2 -2
  170. package/dist/api/controllers/query/import/index.js +2 -1
  171. package/dist/api/controllers/query/index.js +13 -10
  172. package/dist/api/controllers/query/validation.js +33 -41
  173. package/dist/api/controllers/role.js +27 -29
  174. package/dist/api/controllers/routing.js +58 -71
  175. package/dist/api/controllers/row/ExternalRequest.js +542 -538
  176. package/dist/api/controllers/row/external.js +190 -258
  177. package/dist/api/controllers/row/index.js +14 -43
  178. package/dist/api/controllers/row/internal.js +295 -354
  179. package/dist/api/controllers/row/internalSearch.js +80 -95
  180. package/dist/api/controllers/row/staticFormula.js +112 -125
  181. package/dist/api/controllers/row/utils.js +84 -122
  182. package/dist/api/controllers/screen.js +66 -81
  183. package/dist/api/controllers/script.js +6 -13
  184. package/dist/api/controllers/static/index.js +14 -11
  185. package/dist/api/controllers/table/bulkFormula.js +35 -47
  186. package/dist/api/controllers/table/external.js +53 -89
  187. package/dist/api/controllers/table/index.js +31 -64
  188. package/dist/api/controllers/table/internal.js +6 -5
  189. package/dist/api/controllers/table/utils.js +35 -39
  190. package/dist/api/controllers/templates.js +10 -17
  191. package/dist/api/controllers/user.js +70 -82
  192. package/dist/api/controllers/view/exporters.js +4 -8
  193. package/dist/api/controllers/view/index.js +153 -201
  194. package/dist/api/controllers/view/utils.js +121 -151
  195. package/dist/api/controllers/view/viewBuilder.js +16 -4
  196. package/dist/api/controllers/webhook.js +2 -25
  197. package/dist/api/index.js +34 -38
  198. package/dist/api/routes/analytics.js +3 -29
  199. package/dist/api/routes/apikeys.js +7 -33
  200. package/dist/api/routes/application.js +9 -8
  201. package/dist/api/routes/auth.js +2 -1
  202. package/dist/api/routes/automation.js +20 -46
  203. package/dist/api/routes/backup.js +4 -3
  204. package/dist/api/routes/cloud.js +6 -32
  205. package/dist/api/routes/component.js +6 -32
  206. package/dist/api/routes/datasource.js +13 -39
  207. package/dist/api/routes/deploy.js +8 -34
  208. package/dist/api/routes/dev.js +10 -36
  209. package/dist/api/routes/integration.js +7 -33
  210. package/dist/api/routes/layout.js +7 -33
  211. package/dist/api/routes/metadata.js +10 -36
  212. package/dist/api/routes/migrations.js +6 -32
  213. package/dist/api/routes/permission.js +12 -38
  214. package/dist/api/routes/plugin.js +5 -5
  215. package/dist/api/routes/public/index.js +11 -11
  216. package/dist/api/routes/query.js +15 -42
  217. package/dist/api/routes/role.js +10 -36
  218. package/dist/api/routes/routing.js +6 -32
  219. package/dist/api/routes/row.js +11 -12
  220. package/dist/api/routes/screen.js +9 -35
  221. package/dist/api/routes/script.js +6 -32
  222. package/dist/api/routes/static.js +9 -9
  223. package/dist/api/routes/table.js +13 -40
  224. package/dist/api/routes/templates.js +7 -33
  225. package/dist/api/routes/tests/utilities/TestFunctions.js +8 -6
  226. package/dist/api/routes/tests/utilities/index.js +32 -69
  227. package/dist/api/routes/user.js +13 -40
  228. package/dist/api/routes/utils/validators.js +126 -142
  229. package/dist/api/routes/view.js +12 -38
  230. package/dist/api/routes/webhook.js +2 -1
  231. package/dist/app.js +22 -22
  232. package/dist/automations/actions.js +24 -53
  233. package/dist/automations/automationUtils.js +19 -31
  234. package/dist/automations/bullboard.js +29 -59
  235. package/dist/automations/index.js +18 -23
  236. package/dist/automations/steps/bash.js +10 -40
  237. package/dist/automations/steps/createRow.js +1 -2
  238. package/dist/automations/steps/delay.js +5 -9
  239. package/dist/automations/steps/deleteRow.js +1 -2
  240. package/dist/automations/steps/discord.js +7 -14
  241. package/dist/automations/steps/executeQuery.js +7 -34
  242. package/dist/automations/steps/executeScript.js +7 -34
  243. package/dist/automations/steps/filter.js +18 -20
  244. package/dist/automations/steps/integromat.js +7 -14
  245. package/dist/automations/steps/loop.js +1 -4
  246. package/dist/automations/steps/outgoingWebhook.js +14 -45
  247. package/dist/automations/steps/queryRows.js +38 -71
  248. package/dist/automations/steps/sendSmtpEmail.js +6 -33
  249. package/dist/automations/steps/serverLog.js +8 -12
  250. package/dist/automations/steps/slack.js +7 -14
  251. package/dist/automations/steps/updateRow.js +7 -34
  252. package/dist/automations/steps/utils.js +16 -22
  253. package/dist/automations/steps/zapier.js +7 -15
  254. package/dist/automations/tests/utilities/index.js +38 -62
  255. package/dist/automations/triggerInfo/app.js +1 -4
  256. package/dist/automations/triggerInfo/cron.js +1 -4
  257. package/dist/automations/triggerInfo/index.js +6 -31
  258. package/dist/automations/triggerInfo/rowDeleted.js +1 -4
  259. package/dist/automations/triggerInfo/rowSaved.js +1 -4
  260. package/dist/automations/triggerInfo/rowUpdated.js +1 -4
  261. package/dist/automations/triggerInfo/webhook.js +1 -4
  262. package/dist/automations/triggers.js +57 -90
  263. package/dist/automations/utils.js +8 -9
  264. package/dist/constants/index.js +104 -118
  265. package/dist/constants/layouts.js +10 -7
  266. package/dist/constants/screens.js +44 -49
  267. package/dist/db/dynamoClient.js +20 -20
  268. package/dist/db/inMemoryView.js +38 -47
  269. package/dist/db/index.js +4 -7
  270. package/dist/db/linkedRows/LinkController.js +70 -51
  271. package/dist/db/linkedRows/index.js +81 -93
  272. package/dist/db/linkedRows/linkUtils.js +32 -42
  273. package/dist/db/utils.js +119 -146
  274. package/dist/db/views/staticViews.js +74 -91
  275. package/dist/environment.js +6 -9
  276. package/dist/events/AutomationEmitter.js +10 -13
  277. package/dist/events/index.js +23 -5
  278. package/dist/events/utils.js +10 -18
  279. package/dist/integrations/elasticsearch.js +2 -26
  280. package/dist/integrations/googlesheets.js +6 -5
  281. package/dist/integrations/index.js +6 -15
  282. package/dist/integrations/s3.js +1 -157
  283. package/dist/integrations/tests/TestConfiguration.js +1 -0
  284. package/dist/middleware/appInfo.js +9 -14
  285. package/dist/middleware/authorized.js +21 -22
  286. package/dist/middleware/builder.js +40 -41
  287. package/dist/middleware/currentapp.js +37 -39
  288. package/dist/middleware/joi-validator.js +10 -17
  289. package/dist/middleware/publicApi.js +6 -5
  290. package/dist/middleware/resourceId.js +9 -16
  291. package/dist/middleware/selfhost.js +4 -5
  292. package/dist/middleware/utils.js +2 -5
  293. package/dist/migrations/functions/appUrls.js +2 -2
  294. package/dist/migrations/functions/backfill/global.js +1 -1
  295. package/dist/migrations/functions/usageQuotas/syncApps.js +4 -3
  296. package/dist/migrations/functions/usageQuotas/syncRows.js +4 -3
  297. package/dist/migrations/functions/userEmailViewCasing.js +3 -3
  298. package/dist/package.json +45 -11
  299. package/dist/sdk/app/backups/exports.js +2 -1
  300. package/dist/sdk/app/backups/imports.js +3 -2
  301. package/dist/sdk/app/tables/index.js +5 -5
  302. package/dist/startup.js +10 -10
  303. package/dist/tests/utilities/TestConfiguration.js +87 -111
  304. package/dist/tests/utilities/controllers.js +15 -38
  305. package/dist/tests/utilities/structures.js +53 -72
  306. package/dist/threads/automation.js +27 -49
  307. package/dist/threads/query.js +19 -17
  308. package/dist/threads/utils.js +8 -33
  309. package/dist/tsconfig.build.tsbuildinfo +1 -1
  310. package/dist/utilities/budibaseDir.js +2 -4
  311. package/dist/utilities/centralPath.js +7 -14
  312. package/dist/utilities/csvParser.js +21 -27
  313. package/dist/utilities/fileSystem/clientLibrary.js +73 -87
  314. package/dist/utilities/fileSystem/index.js +175 -203
  315. package/dist/utilities/fileSystem/processor.js +3 -8
  316. package/dist/utilities/fileSystem/utilities.js +22 -0
  317. package/dist/utilities/global.js +66 -87
  318. package/dist/utilities/index.js +75 -102
  319. package/dist/utilities/plugins.js +9 -15
  320. package/dist/utilities/queue/inMemoryQueue.js +126 -0
  321. package/dist/utilities/redis.js +65 -100
  322. package/dist/utilities/routing/index.js +23 -27
  323. package/dist/utilities/rowProcessor/index.js +191 -133
  324. package/dist/utilities/rowProcessor/utils.js +26 -35
  325. package/dist/utilities/scriptRunner.js +5 -8
  326. package/dist/utilities/security.js +35 -39
  327. package/dist/utilities/usageQuota/rows.js +49 -56
  328. package/dist/utilities/users.js +28 -31
  329. package/dist/utilities/workerRequests.js +82 -115
  330. package/dist/watch.js +2 -1
  331. package/package.json +46 -12
  332. package/scripts/exportAppTemplate.js +2 -2
  333. package/{src/tests/jestSetup.ts → scripts/jestSetup.js} +2 -11
  334. package/scripts/localdomain.js +6 -34
  335. package/src/api/controllers/{apikeys.ts → apikeys.js} +10 -10
  336. package/src/api/controllers/application.ts +42 -56
  337. package/src/api/controllers/auth.ts +3 -4
  338. package/src/api/controllers/{automation.ts → automation.js} +48 -48
  339. package/src/api/controllers/{cloud.ts → cloud.js} +24 -24
  340. package/src/api/controllers/component.ts +1 -2
  341. package/src/api/controllers/{datasource.ts → datasource.js} +47 -100
  342. package/src/api/controllers/deploy/{Deployment.ts → Deployment.js} +10 -13
  343. package/src/api/controllers/deploy/index.ts +25 -13
  344. package/src/api/controllers/{dev.ts → dev.js} +37 -31
  345. package/src/api/controllers/integration.js +14 -0
  346. package/src/api/controllers/{layout.ts → layout.js} +8 -8
  347. package/src/api/controllers/{metadata.ts → metadata.js} +10 -11
  348. package/src/api/controllers/migrations.js +13 -0
  349. package/src/api/controllers/{permission.ts → permission.js} +46 -49
  350. package/src/api/controllers/plugin/index.ts +5 -4
  351. package/src/api/controllers/plugin/utils.ts +5 -3
  352. package/src/api/controllers/public/applications.ts +9 -15
  353. package/src/api/controllers/public/rows.ts +1 -1
  354. package/src/api/controllers/public/tables.ts +2 -2
  355. package/src/api/controllers/public/utils.ts +2 -2
  356. package/src/api/controllers/query/import/index.ts +3 -2
  357. package/src/api/controllers/query/index.ts +14 -11
  358. package/src/api/controllers/query/{validation.ts → validation.js} +7 -7
  359. package/src/api/controllers/{role.ts → role.js} +25 -21
  360. package/src/api/controllers/{routing.ts → routing.js} +34 -37
  361. package/src/api/controllers/row/ExternalRequest.ts +624 -602
  362. package/src/api/controllers/row/{external.ts → external.js} +62 -79
  363. package/src/api/controllers/row/index.ts +11 -19
  364. package/src/api/controllers/row/{internal.ts → internal.js} +73 -87
  365. package/src/api/controllers/row/{internalSearch.ts → internalSearch.js} +68 -92
  366. package/src/api/controllers/row/{staticFormula.ts → staticFormula.js} +25 -30
  367. package/src/api/controllers/row/{utils.ts → utils.js} +22 -32
  368. package/src/api/controllers/screen.ts +6 -10
  369. package/src/api/controllers/script.js +11 -0
  370. package/src/api/controllers/static/index.ts +16 -14
  371. package/src/api/controllers/table/{bulkFormula.ts → bulkFormula.js} +25 -39
  372. package/src/api/controllers/table/{external.ts → external.js} +55 -86
  373. package/src/api/controllers/table/{index.ts → index.js} +18 -21
  374. package/src/api/controllers/table/internal.ts +6 -5
  375. package/src/api/controllers/table/utils.ts +21 -26
  376. package/src/api/controllers/{templates.ts → templates.js} +7 -10
  377. package/src/api/controllers/{user.ts → user.js} +49 -47
  378. package/src/api/controllers/view/{exporters.ts → exporters.js} +3 -5
  379. package/src/api/controllers/view/{index.ts → index.js} +27 -36
  380. package/src/api/controllers/view/{utils.ts → utils.js} +20 -26
  381. package/src/api/controllers/view/{viewBuilder.ts → viewBuilder.js} +11 -25
  382. package/src/api/controllers/webhook.ts +1 -1
  383. package/src/api/{index.ts → index.js} +22 -15
  384. package/src/api/routes/analytics.js +9 -0
  385. package/src/api/routes/apikeys.js +12 -0
  386. package/src/api/routes/application.ts +9 -17
  387. package/src/api/routes/auth.ts +2 -2
  388. package/src/api/routes/automation.js +85 -0
  389. package/src/api/routes/backup.ts +4 -8
  390. package/src/api/routes/cloud.js +14 -0
  391. package/src/api/routes/component.js +14 -0
  392. package/src/api/routes/datasource.js +51 -0
  393. package/src/api/routes/deploy.js +17 -0
  394. package/src/api/routes/dev.js +26 -0
  395. package/src/api/routes/integration.js +12 -0
  396. package/src/api/routes/layout.js +16 -0
  397. package/src/api/routes/{metadata.ts → metadata.js} +13 -13
  398. package/src/api/routes/migrations.js +14 -0
  399. package/src/api/routes/permission.js +33 -0
  400. package/src/api/routes/plugin.ts +6 -14
  401. package/src/api/routes/public/index.ts +14 -11
  402. package/src/api/routes/public/tests/compare.spec.js +1 -1
  403. package/src/api/routes/{query.ts → query.js} +22 -19
  404. package/src/api/routes/role.js +15 -0
  405. package/src/api/routes/routing.js +14 -0
  406. package/src/api/routes/row.ts +15 -13
  407. package/src/api/routes/screen.js +18 -0
  408. package/src/api/routes/script.js +10 -0
  409. package/src/api/routes/static.ts +10 -7
  410. package/src/api/routes/{table.ts → table.js} +13 -10
  411. package/src/api/routes/templates.js +16 -0
  412. package/src/api/routes/tests/analytics.spec.js +1 -1
  413. package/src/api/routes/tests/backup.spec.js +1 -9
  414. package/src/api/routes/tests/deployment.spec.ts +1 -1
  415. package/src/api/routes/tests/permissions.spec.js +1 -2
  416. package/src/api/routes/tests/query.spec.js +10 -15
  417. package/src/api/routes/tests/role.spec.js +9 -7
  418. package/src/api/routes/tests/routing.spec.js +1 -2
  419. package/src/api/routes/tests/row.spec.js +4 -3
  420. package/src/api/routes/tests/static.spec.js +2 -2
  421. package/src/api/routes/tests/table.spec.js +36 -40
  422. package/src/api/routes/tests/user.spec.js +1 -2
  423. package/src/api/routes/tests/utilities/TestFunctions.ts +8 -6
  424. package/src/api/routes/tests/utilities/{index.ts → index.js} +12 -12
  425. package/src/api/routes/tests/view.spec.js +3 -3
  426. package/src/api/routes/user.js +53 -0
  427. package/src/api/routes/utils/{validators.ts → validators.js} +37 -33
  428. package/src/api/routes/view.js +31 -0
  429. package/src/api/routes/webhook.ts +2 -2
  430. package/src/app.ts +14 -15
  431. package/src/automations/{actions.ts → actions.js} +23 -26
  432. package/src/automations/{automationUtils.ts → automationUtils.js} +12 -13
  433. package/src/automations/bullboard.js +39 -0
  434. package/src/automations/index.js +29 -0
  435. package/src/automations/steps/{bash.ts → bash.js} +8 -13
  436. package/src/automations/steps/createRow.ts +3 -8
  437. package/src/automations/steps/{delay.ts → delay.js} +4 -9
  438. package/src/automations/steps/deleteRow.ts +3 -8
  439. package/src/automations/steps/{discord.ts → discord.js} +5 -10
  440. package/src/automations/steps/{executeQuery.ts → executeQuery.js} +7 -12
  441. package/src/automations/steps/{executeScript.ts → executeScript.js} +7 -17
  442. package/src/automations/steps/{filter.ts → filter.js} +9 -12
  443. package/src/automations/steps/{integromat.ts → integromat.js} +5 -10
  444. package/src/automations/steps/{loop.ts → loop.js} +2 -4
  445. package/src/automations/steps/{outgoingWebhook.ts → outgoingWebhook.js} +13 -18
  446. package/src/automations/steps/{queryRows.ts → queryRows.js} +36 -48
  447. package/src/automations/steps/{sendSmtpEmail.ts → sendSmtpEmail.js} +5 -10
  448. package/src/automations/steps/{serverLog.ts → serverLog.js} +3 -9
  449. package/src/automations/steps/{slack.ts → slack.js} +5 -10
  450. package/src/automations/steps/{updateRow.ts → updateRow.js} +7 -12
  451. package/src/automations/steps/{utils.ts → utils.js} +4 -10
  452. package/src/automations/steps/{zapier.ts → zapier.js} +5 -11
  453. package/src/automations/tests/updateRow.spec.js +1 -0
  454. package/src/automations/tests/utilities/index.js +51 -0
  455. package/src/automations/triggerInfo/{app.ts → app.js} +2 -7
  456. package/src/automations/triggerInfo/{cron.ts → cron.js} +2 -7
  457. package/src/automations/triggerInfo/index.js +15 -0
  458. package/src/automations/triggerInfo/{rowDeleted.ts → rowDeleted.js} +2 -7
  459. package/src/automations/triggerInfo/{rowSaved.ts → rowSaved.js} +2 -7
  460. package/src/automations/triggerInfo/{rowUpdated.ts → rowUpdated.js} +2 -7
  461. package/src/automations/triggerInfo/{webhook.ts → webhook.js} +2 -7
  462. package/src/automations/{triggers.ts → triggers.js} +26 -30
  463. package/src/automations/utils.ts +12 -9
  464. package/src/constants/index.js +208 -0
  465. package/src/constants/{layouts.ts → layouts.js} +9 -3
  466. package/src/constants/screens.js +46 -0
  467. package/src/db/{dynamoClient.ts → dynamoClient.js} +19 -39
  468. package/src/db/{inMemoryView.ts → inMemoryView.js} +9 -15
  469. package/src/db/index.js +16 -0
  470. package/src/db/linkedRows/{LinkController.ts → LinkController.js} +85 -64
  471. package/src/db/linkedRows/{index.ts → index.js} +38 -43
  472. package/src/db/linkedRows/{linkUtils.ts → linkUtils.js} +24 -33
  473. package/src/db/{newid.ts → newid.js} +1 -1
  474. package/src/db/tests/linkController.spec.js +57 -73
  475. package/src/db/tests/linkTests.spec.js +14 -17
  476. package/src/db/{utils.ts → utils.js} +93 -77
  477. package/src/db/views/{staticViews.ts → staticViews.js} +13 -20
  478. package/src/{environment.ts → environment.js} +7 -11
  479. package/src/events/{AutomationEmitter.ts → AutomationEmitter.js} +7 -12
  480. package/src/events/{BudibaseEmitter.ts → index.js} +8 -7
  481. package/src/events/utils.js +38 -0
  482. package/src/integrations/base/sql.ts +1 -1
  483. package/src/integrations/base/sqlTable.ts +1 -1
  484. package/src/integrations/elasticsearch.ts +2 -32
  485. package/src/integrations/googlesheets.ts +7 -5
  486. package/src/integrations/index.ts +41 -45
  487. package/src/integrations/s3.ts +3 -179
  488. package/src/integrations/tests/s3.spec.ts +1 -85
  489. package/src/integrations/utils.ts +2 -6
  490. package/src/middleware/appInfo.js +19 -0
  491. package/src/middleware/authorized.ts +28 -21
  492. package/src/middleware/{builder.ts → builder.js} +15 -17
  493. package/src/middleware/{currentapp.ts → currentapp.js} +39 -42
  494. package/src/middleware/{joi-validator.ts → joi-validator.js} +8 -9
  495. package/src/middleware/publicApi.js +21 -0
  496. package/src/middleware/{resourceId.ts → resourceId.js} +11 -16
  497. package/src/middleware/{selfhost.ts → selfhost.js} +2 -4
  498. package/src/middleware/tests/authorized.spec.js +6 -5
  499. package/src/middleware/tests/currentapp.spec.js +63 -118
  500. package/src/middleware/{utils.ts → utils.js} +1 -3
  501. package/src/migrations/functions/appUrls.ts +2 -2
  502. package/src/migrations/functions/backfill/app/tables.ts +5 -2
  503. package/src/migrations/functions/backfill/global.ts +1 -1
  504. package/src/migrations/functions/tests/appUrls.spec.js +4 -4
  505. package/src/migrations/functions/tests/userEmailViewCasing.spec.js +9 -14
  506. package/src/migrations/functions/usageQuotas/syncApps.ts +4 -3
  507. package/src/migrations/functions/usageQuotas/syncRows.ts +5 -4
  508. package/src/migrations/functions/usageQuotas/tests/syncRows.spec.ts +7 -7
  509. package/src/migrations/functions/userEmailViewCasing.ts +3 -3
  510. package/src/migrations/tests/index.spec.ts +1 -1
  511. package/src/module.d.ts +14 -0
  512. package/src/sdk/app/backups/exports.ts +3 -5
  513. package/src/sdk/app/backups/imports.ts +14 -7
  514. package/src/sdk/app/backups/statistics.ts +18 -9
  515. package/src/sdk/app/tables/index.ts +8 -7
  516. package/src/startup.ts +5 -5
  517. package/src/tests/utilities/{TestConfiguration.ts → TestConfiguration.js} +78 -111
  518. package/src/tests/utilities/controllers.js +16 -0
  519. package/src/tests/utilities/{index.ts → index.js} +2 -2
  520. package/src/tests/utilities/{structures.ts → structures.js} +36 -41
  521. package/src/threads/automation.ts +19 -18
  522. package/src/threads/query.ts +16 -11
  523. package/src/threads/utils.ts +5 -4
  524. package/src/utilities/budibaseDir.js +3 -0
  525. package/src/utilities/{centralPath.ts → centralPath.js} +3 -3
  526. package/src/utilities/{csvParser.ts → csvParser.js} +26 -42
  527. package/src/utilities/fileSystem/{clientLibrary.ts → clientLibrary.js} +20 -20
  528. package/src/utilities/fileSystem/{index.ts → index.js} +65 -61
  529. package/src/utilities/fileSystem/{processor.ts → processor.js} +5 -3
  530. package/src/utilities/fileSystem/utilities.js +36 -0
  531. package/src/utilities/global.js +127 -0
  532. package/src/utilities/{index.ts → index.js} +32 -40
  533. package/src/utilities/{plugins.ts → plugins.js} +4 -5
  534. package/src/utilities/queue/inMemoryQueue.js +122 -0
  535. package/src/utilities/{redis.ts → redis.js} +20 -21
  536. package/src/utilities/routing/index.js +25 -0
  537. package/src/utilities/rowProcessor/{index.ts → index.js} +144 -67
  538. package/src/utilities/rowProcessor/{utils.ts → utils.js} +15 -19
  539. package/src/utilities/{scriptRunner.ts → scriptRunner.js} +4 -8
  540. package/src/utilities/security.js +71 -0
  541. package/src/utilities/{statusCodes.ts → statusCodes.js} +1 -1
  542. package/src/utilities/usageQuota/{rows.ts → rows.js} +18 -14
  543. package/src/utilities/usageQuota/{usageQuoteReset.ts → usageQuoteReset.js} +1 -1
  544. package/src/utilities/{users.ts → users.js} +13 -11
  545. package/src/utilities/{workerRequests.ts → workerRequests.js} +38 -43
  546. package/src/watch.ts +3 -2
  547. package/tsconfig.build.json +2 -1
  548. package/tsconfig.json +1 -3
  549. package/coverage/lcov-report/src/api/controllers/analytics.ts.html +0 -190
  550. package/coverage/lcov-report/src/api/controllers/application.ts.html +0 -2038
  551. package/coverage/lcov-report/src/api/controllers/auth.ts.html +0 -301
  552. package/coverage/lcov-report/src/api/controllers/backup.ts.html +0 -148
  553. package/coverage/lcov-report/src/api/controllers/component.ts.html +0 -259
  554. package/coverage/lcov-report/src/api/controllers/deploy/Deployment.ts.html +0 -259
  555. package/coverage/lcov-report/src/api/controllers/deploy/index.html +0 -131
  556. package/coverage/lcov-report/src/api/controllers/deploy/index.ts.html +0 -730
  557. package/coverage/lcov-report/src/api/controllers/index.html +0 -431
  558. package/coverage/lcov-report/src/api/controllers/plugin/file.ts.html +0 -130
  559. package/coverage/lcov-report/src/api/controllers/plugin/github.ts.html +0 -310
  560. package/coverage/lcov-report/src/api/controllers/plugin/index.html +0 -206
  561. package/coverage/lcov-report/src/api/controllers/plugin/index.ts.html +0 -484
  562. package/coverage/lcov-report/src/api/controllers/plugin/npm.ts.html +0 -253
  563. package/coverage/lcov-report/src/api/controllers/plugin/uploaders.ts.html +0 -97
  564. package/coverage/lcov-report/src/api/controllers/plugin/url.ts.html +0 -121
  565. package/coverage/lcov-report/src/api/controllers/plugin/utils.ts.html +0 -136
  566. package/coverage/lcov-report/src/api/controllers/public/applications.ts.html +0 -334
  567. package/coverage/lcov-report/src/api/controllers/public/index.html +0 -191
  568. package/coverage/lcov-report/src/api/controllers/public/mapping/applications.ts.html +0 -181
  569. package/coverage/lcov-report/src/api/controllers/public/mapping/index.html +0 -191
  570. package/coverage/lcov-report/src/api/controllers/public/mapping/index.ts.html +0 -124
  571. package/coverage/lcov-report/src/api/controllers/public/mapping/queries.ts.html +0 -202
  572. package/coverage/lcov-report/src/api/controllers/public/mapping/rows.ts.html +0 -178
  573. package/coverage/lcov-report/src/api/controllers/public/mapping/tables.ts.html +0 -163
  574. package/coverage/lcov-report/src/api/controllers/public/mapping/users.ts.html +0 -181
  575. package/coverage/lcov-report/src/api/controllers/public/queries.ts.html +0 -145
  576. package/coverage/lcov-report/src/api/controllers/public/rows.ts.html +0 -328
  577. package/coverage/lcov-report/src/api/controllers/public/tables.ts.html +0 -253
  578. package/coverage/lcov-report/src/api/controllers/public/users.ts.html +0 -268
  579. package/coverage/lcov-report/src/api/controllers/public/utils.ts.html +0 -220
  580. package/coverage/lcov-report/src/api/controllers/query/import/index.html +0 -116
  581. package/coverage/lcov-report/src/api/controllers/query/import/index.ts.html +0 -373
  582. package/coverage/lcov-report/src/api/controllers/query/import/sources/base/index.html +0 -131
  583. package/coverage/lcov-report/src/api/controllers/query/import/sources/base/index.ts.html +0 -379
  584. package/coverage/lcov-report/src/api/controllers/query/import/sources/base/openapi.ts.html +0 -157
  585. package/coverage/lcov-report/src/api/controllers/query/import/sources/curl.ts.html +0 -403
  586. package/coverage/lcov-report/src/api/controllers/query/import/sources/index.html +0 -146
  587. package/coverage/lcov-report/src/api/controllers/query/import/sources/openapi2.ts.html +0 -586
  588. package/coverage/lcov-report/src/api/controllers/query/import/sources/openapi3.ts.html +0 -712
  589. package/coverage/lcov-report/src/api/controllers/query/index.html +0 -131
  590. package/coverage/lcov-report/src/api/controllers/query/index.ts.html +0 -976
  591. package/coverage/lcov-report/src/api/controllers/row/ExternalRequest.ts.html +0 -2260
  592. package/coverage/lcov-report/src/api/controllers/row/index.ts.html +0 -541
  593. package/coverage/lcov-report/src/api/controllers/screen.ts.html +0 -463
  594. package/coverage/lcov-report/src/api/controllers/static/index.html +0 -116
  595. package/coverage/lcov-report/src/api/controllers/static/index.ts.html +0 -694
  596. package/coverage/lcov-report/src/api/controllers/table/index.html +0 -176
  597. package/coverage/lcov-report/src/api/controllers/table/internal.ts.html +0 -661
  598. package/coverage/lcov-report/src/api/controllers/table/utils.ts.html +0 -1255
  599. package/coverage/lcov-report/src/api/controllers/webhook.ts.html +0 -379
  600. package/coverage/lcov-report/src/api/routes/application.ts.html +0 -223
  601. package/coverage/lcov-report/src/api/routes/auth.ts.html +0 -109
  602. package/coverage/lcov-report/src/api/routes/datasource.ts.html +0 -265
  603. package/coverage/lcov-report/src/api/routes/deploy.ts.html +0 -148
  604. package/coverage/lcov-report/src/api/routes/dev.ts.html +0 -199
  605. package/coverage/lcov-report/src/api/routes/index.ts.html +0 -289
  606. package/coverage/lcov-report/src/api/routes/migrations.ts.html +0 -127
  607. package/coverage/lcov-report/src/api/routes/plugin.ts.html +0 -151
  608. package/coverage/lcov-report/src/api/routes/public/applications.ts.html +0 -541
  609. package/coverage/lcov-report/src/api/routes/public/index.html +0 -191
  610. package/coverage/lcov-report/src/api/routes/public/index.ts.html +0 -493
  611. package/coverage/lcov-report/src/api/routes/public/middleware/index.html +0 -116
  612. package/coverage/lcov-report/src/api/routes/public/middleware/mapper.ts.html +0 -328
  613. package/coverage/lcov-report/src/api/routes/public/queries.ts.html +0 -313
  614. package/coverage/lcov-report/src/api/routes/public/rows.ts.html +0 -598
  615. package/coverage/lcov-report/src/api/routes/public/tables.ts.html +0 -586
  616. package/coverage/lcov-report/src/api/routes/public/users.ts.html +0 -511
  617. package/coverage/lcov-report/src/api/routes/public/utils/Endpoint.ts.html +0 -238
  618. package/coverage/lcov-report/src/api/routes/public/utils/index.html +0 -116
  619. package/coverage/lcov-report/src/api/routes/row.ts.html +0 -883
  620. package/coverage/lcov-report/src/api/routes/screen.ts.html +0 -154
  621. package/coverage/lcov-report/src/api/routes/static.ts.html +0 -280
  622. package/coverage/lcov-report/src/api/routes/tests/utilities/TestFunctions.ts.html +0 -550
  623. package/coverage/lcov-report/src/api/routes/tests/utilities/index.html +0 -131
  624. package/coverage/lcov-report/src/api/routes/tests/utilities/index.ts.html +0 -367
  625. package/coverage/lcov-report/src/api/routes/webhook.ts.html +0 -166
  626. package/coverage/lcov-report/src/app.ts.html +0 -406
  627. package/coverage/lcov-report/src/automations/logging/index.html +0 -116
  628. package/coverage/lcov-report/src/automations/logging/index.ts.html +0 -199
  629. package/coverage/lcov-report/src/automations/steps/createRow.ts.html +0 -382
  630. package/coverage/lcov-report/src/automations/steps/deleteRow.ts.html +0 -349
  631. package/coverage/lcov-report/src/automations/steps/index.html +0 -371
  632. package/coverage/lcov-report/src/automations/tests/utilities/index.html +0 -116
  633. package/coverage/lcov-report/src/automations/tests/utilities/index.ts.html +0 -265
  634. package/coverage/lcov-report/src/automations/utils.ts.html +0 -886
  635. package/coverage/lcov-report/src/db/defaultData/datasource_bb_default.ts.html +0 -2032
  636. package/coverage/lcov-report/src/db/defaultData/employeeImport.ts.html +0 -541
  637. package/coverage/lcov-report/src/db/defaultData/expensesImport.ts.html +0 -427
  638. package/coverage/lcov-report/src/db/defaultData/index.html +0 -176
  639. package/coverage/lcov-report/src/db/defaultData/inventoryImport.ts.html +0 -403
  640. package/coverage/lcov-report/src/db/defaultData/jobsImport.ts.html +0 -559
  641. package/coverage/lcov-report/src/db/dynamoClient.ts.html +0 -526
  642. package/coverage/lcov-report/src/db/linkedRows/LinkDocument.ts.html +0 -265
  643. package/coverage/lcov-report/src/db/linkedRows/index.html +0 -161
  644. package/coverage/lcov-report/src/environment.ts.html +0 -457
  645. package/coverage/lcov-report/src/events/index.ts.html +0 -100
  646. package/coverage/lcov-report/src/events/utils.ts.html +0 -319
  647. package/coverage/lcov-report/src/index.html +0 -191
  648. package/coverage/lcov-report/src/index.ts.html +0 -133
  649. package/coverage/lcov-report/src/integrations/arangodb.ts.html +0 -415
  650. package/coverage/lcov-report/src/integrations/base/index.html +0 -161
  651. package/coverage/lcov-report/src/integrations/base/query.ts.html +0 -133
  652. package/coverage/lcov-report/src/integrations/base/sql.ts.html +0 -1951
  653. package/coverage/lcov-report/src/integrations/base/sqlTable.ts.html +0 -691
  654. package/coverage/lcov-report/src/integrations/base/utils.ts.html +0 -121
  655. package/coverage/lcov-report/src/integrations/couchdb.ts.html +0 -406
  656. package/coverage/lcov-report/src/integrations/dynamodb.ts.html +0 -745
  657. package/coverage/lcov-report/src/integrations/elasticsearch.ts.html +0 -634
  658. package/coverage/lcov-report/src/integrations/firebase.ts.html +0 -646
  659. package/coverage/lcov-report/src/integrations/googlesheets.ts.html +0 -1315
  660. package/coverage/lcov-report/src/integrations/index.html +0 -371
  661. package/coverage/lcov-report/src/integrations/index.ts.html +0 -445
  662. package/coverage/lcov-report/src/integrations/microsoftSqlServer.ts.html +0 -1006
  663. package/coverage/lcov-report/src/integrations/mongodb.ts.html +0 -1867
  664. package/coverage/lcov-report/src/integrations/mysql.ts.html +0 -964
  665. package/coverage/lcov-report/src/integrations/oracle.ts.html +0 -1474
  666. package/coverage/lcov-report/src/integrations/postgres.ts.html +0 -1069
  667. package/coverage/lcov-report/src/integrations/queries/index.html +0 -116
  668. package/coverage/lcov-report/src/integrations/queries/sql.ts.html +0 -463
  669. package/coverage/lcov-report/src/integrations/redis.ts.html +0 -550
  670. package/coverage/lcov-report/src/integrations/rest.ts.html +0 -1381
  671. package/coverage/lcov-report/src/integrations/s3.ts.html +0 -862
  672. package/coverage/lcov-report/src/integrations/snowflake.ts.html +0 -376
  673. package/coverage/lcov-report/src/integrations/utils.ts.html +0 -1018
  674. package/coverage/lcov-report/src/middleware/authorized.ts.html +0 -460
  675. package/coverage/lcov-report/src/migrations/functions/appUrls.ts.html +0 -187
  676. package/coverage/lcov-report/src/migrations/functions/backfill/app/automations.ts.html +0 -163
  677. package/coverage/lcov-report/src/migrations/functions/backfill/app/datasources.ts.html +0 -151
  678. package/coverage/lcov-report/src/migrations/functions/backfill/app/index.html +0 -206
  679. package/coverage/lcov-report/src/migrations/functions/backfill/app/layouts.ts.html +0 -172
  680. package/coverage/lcov-report/src/migrations/functions/backfill/app/queries.ts.html +0 -226
  681. package/coverage/lcov-report/src/migrations/functions/backfill/app/roles.ts.html +0 -151
  682. package/coverage/lcov-report/src/migrations/functions/backfill/app/screens.ts.html +0 -151
  683. package/coverage/lcov-report/src/migrations/functions/backfill/app/tables.ts.html +0 -166
  684. package/coverage/lcov-report/src/migrations/functions/backfill/app.ts.html +0 -532
  685. package/coverage/lcov-report/src/migrations/functions/backfill/global/configs.ts.html +0 -283
  686. package/coverage/lcov-report/src/migrations/functions/backfill/global/index.html +0 -146
  687. package/coverage/lcov-report/src/migrations/functions/backfill/global/quotas.ts.html +0 -265
  688. package/coverage/lcov-report/src/migrations/functions/backfill/global/users.ts.html +0 -232
  689. package/coverage/lcov-report/src/migrations/functions/backfill/global.ts.html +0 -724
  690. package/coverage/lcov-report/src/migrations/functions/backfill/index.html +0 -161
  691. package/coverage/lcov-report/src/migrations/functions/backfill/index.ts.html +0 -106
  692. package/coverage/lcov-report/src/migrations/functions/backfill/installation.ts.html +0 -235
  693. package/coverage/lcov-report/src/migrations/functions/index.html +0 -146
  694. package/coverage/lcov-report/src/migrations/functions/syncQuotas.ts.html +0 -130
  695. package/coverage/lcov-report/src/migrations/functions/usageQuotas/index.html +0 -161
  696. package/coverage/lcov-report/src/migrations/functions/usageQuotas/index.ts.html +0 -94
  697. package/coverage/lcov-report/src/migrations/functions/usageQuotas/syncApps.ts.html +0 -127
  698. package/coverage/lcov-report/src/migrations/functions/usageQuotas/syncPlugins.ts.html +0 -115
  699. package/coverage/lcov-report/src/migrations/functions/usageQuotas/syncRows.ts.html +0 -169
  700. package/coverage/lcov-report/src/migrations/functions/userEmailViewCasing.ts.html +0 -124
  701. package/coverage/lcov-report/src/migrations/index.ts.html +0 -403
  702. package/coverage/lcov-report/src/migrations/tests/helpers.ts.html +0 -205
  703. package/coverage/lcov-report/src/migrations/tests/index.html +0 -131
  704. package/coverage/lcov-report/src/migrations/tests/structures.ts.html +0 -283
  705. package/coverage/lcov-report/src/sdk/app/automations/index.html +0 -131
  706. package/coverage/lcov-report/src/sdk/app/automations/index.ts.html +0 -100
  707. package/coverage/lcov-report/src/sdk/app/automations/webhook.ts.html +0 -214
  708. package/coverage/lcov-report/src/sdk/app/backups/constants.ts.html +0 -91
  709. package/coverage/lcov-report/src/sdk/app/backups/exports.ts.html +0 -604
  710. package/coverage/lcov-report/src/sdk/app/backups/imports.ts.html +0 -682
  711. package/coverage/lcov-report/src/sdk/app/backups/index.html +0 -176
  712. package/coverage/lcov-report/src/sdk/app/backups/index.ts.html +0 -112
  713. package/coverage/lcov-report/src/sdk/app/backups/statistics.ts.html +0 -292
  714. package/coverage/lcov-report/src/sdk/app/tables/index.html +0 -116
  715. package/coverage/lcov-report/src/sdk/app/tables/index.ts.html +0 -265
  716. package/coverage/lcov-report/src/startup.ts.html +0 -499
  717. package/coverage/lcov-report/src/tests/utilities/TestConfiguration.ts.html +0 -1966
  718. package/coverage/lcov-report/src/tests/utilities/controllers.ts.html +0 -127
  719. package/coverage/lcov-report/src/tests/utilities/index.html +0 -161
  720. package/coverage/lcov-report/src/tests/utilities/structures.ts.html +0 -667
  721. package/coverage/lcov-report/src/threads/automation.ts.html +0 -1594
  722. package/coverage/lcov-report/src/threads/index.html +0 -161
  723. package/coverage/lcov-report/src/threads/index.ts.html +0 -418
  724. package/coverage/lcov-report/src/threads/query.ts.html +0 -961
  725. package/coverage/lcov-report/src/threads/utils.ts.html +0 -373
  726. package/coverage/lcov-report/src/utilities/fileSystem/clientLibrary.ts.html +0 -547
  727. package/coverage/lcov-report/src/utilities/fileSystem/index.ts.html +0 -1129
  728. package/coverage/lcov-report/src/utilities/fileSystem/processor.ts.html +0 -142
  729. package/coverage/lcov-report/src/utilities/redis.ts.html +0 -343
  730. package/coverage/lcov-report/src/utilities/routing/index.ts.html +0 -181
  731. package/coverage/lcov-report/src/utilities/rowProcessor/index.html +0 -146
  732. package/coverage/lcov-report/src/utilities/rowProcessor/map.ts.html +0 -373
  733. package/coverage/lcov-report/src/watch.ts.html +0 -196
  734. package/dist/db/defaultData/datasource_bb_default.js +0 -571
  735. package/dist/db/defaultData/employeeImport.js +0 -155
  736. package/dist/db/defaultData/expensesImport.js +0 -117
  737. package/dist/db/defaultData/inventoryImport.js +0 -109
  738. package/dist/db/defaultData/jobsImport.js +0 -152
  739. package/dist/db/linkedRows/LinkDocument.js +0 -32
  740. package/dist/events/BudibaseEmitter.js +0 -24
  741. package/dist/utilities/rowProcessor/map.js +0 -99
  742. package/jest.config.ts +0 -33
  743. package/src/api/controllers/integration.ts +0 -13
  744. package/src/api/controllers/migrations.ts +0 -14
  745. package/src/api/controllers/script.ts +0 -12
  746. package/src/api/routes/analytics.ts +0 -9
  747. package/src/api/routes/apikeys.ts +0 -12
  748. package/src/api/routes/automation.ts +0 -87
  749. package/src/api/routes/cloud.ts +0 -18
  750. package/src/api/routes/component.ts +0 -14
  751. package/src/api/routes/datasource.ts +0 -60
  752. package/src/api/routes/deploy.ts +0 -21
  753. package/src/api/routes/dev.ts +0 -38
  754. package/src/api/routes/integration.ts +0 -16
  755. package/src/api/routes/layout.ts +0 -16
  756. package/src/api/routes/migrations.ts +0 -14
  757. package/src/api/routes/permission.ts +0 -41
  758. package/src/api/routes/role.ts +0 -24
  759. package/src/api/routes/routing.ts +0 -14
  760. package/src/api/routes/screen.ts +0 -23
  761. package/src/api/routes/script.ts +0 -10
  762. package/src/api/routes/templates.ts +0 -16
  763. package/src/api/routes/user.ts +0 -51
  764. package/src/api/routes/view.ts +0 -34
  765. package/src/automations/bullboard.ts +0 -38
  766. package/src/automations/index.ts +0 -26
  767. package/src/automations/tests/utilities/index.ts +0 -60
  768. package/src/automations/triggerInfo/index.ts +0 -15
  769. package/src/constants/index.ts +0 -204
  770. package/src/constants/screens.ts +0 -48
  771. package/src/db/defaultData/datasource_bb_default.ts +0 -649
  772. package/src/db/defaultData/employeeImport.ts +0 -152
  773. package/src/db/defaultData/expensesImport.ts +0 -114
  774. package/src/db/defaultData/inventoryImport.ts +0 -106
  775. package/src/db/defaultData/jobsImport.ts +0 -158
  776. package/src/db/index.ts +0 -16
  777. package/src/db/linkedRows/LinkDocument.ts +0 -60
  778. package/src/events/index.ts +0 -5
  779. package/src/events/utils.ts +0 -78
  780. package/src/middleware/appInfo.ts +0 -20
  781. package/src/middleware/publicApi.ts +0 -21
  782. package/src/tests/utilities/controllers.ts +0 -14
  783. package/src/utilities/budibaseDir.ts +0 -3
  784. package/src/utilities/global.ts +0 -138
  785. package/src/utilities/routing/index.ts +0 -32
  786. package/src/utilities/rowProcessor/map.ts +0 -96
  787. package/src/utilities/security.ts +0 -65
@@ -20,7 +20,6 @@ var __rest = (this && this.__rest) || function (s, e) {
20
20
  return t;
21
21
  };
22
22
  Object.defineProperty(exports, "__esModule", { value: true });
23
- exports.ExternalRequest = void 0;
24
23
  const types_1 = require("@budibase/types");
25
24
  const utils_1 = require("../../../integrations/utils");
26
25
  const utils_2 = require("./utils");
@@ -29,597 +28,602 @@ const utils_3 = require("../../../integrations/utils");
29
28
  const string_templates_1 = require("@budibase/string-templates");
30
29
  // @ts-ignore
31
30
  const fp_1 = require("lodash/fp");
32
- const rowProcessor_1 = require("../../../utilities/rowProcessor");
33
- const backend_core_1 = require("@budibase/backend-core");
34
- function buildFilters(id, filters, table) {
35
- const primary = table.primary;
36
- // if passed in array need to copy for shifting etc
37
- let idCopy = (0, fp_1.cloneDeep)(id);
38
- if (filters) {
39
- // need to map over the filters and make sure the _id field isn't present
40
- for (let filter of Object.values(filters)) {
41
- if (filter._id && primary) {
42
- const parts = (0, utils_1.breakRowIdField)(filter._id);
43
- for (let field of primary) {
44
- filter[field] = parts.shift();
31
+ const utils_4 = require("../../../utilities/rowProcessor/utils");
32
+ // @ts-ignore
33
+ const context_1 = require("@budibase/backend-core/context");
34
+ var External;
35
+ (function (External) {
36
+ function buildFilters(id, filters, table) {
37
+ const primary = table.primary;
38
+ // if passed in array need to copy for shifting etc
39
+ let idCopy = (0, fp_1.cloneDeep)(id);
40
+ if (filters) {
41
+ // need to map over the filters and make sure the _id field isn't present
42
+ for (let filter of Object.values(filters)) {
43
+ if (filter._id && primary) {
44
+ const parts = (0, utils_1.breakRowIdField)(filter._id);
45
+ for (let field of primary) {
46
+ filter[field] = parts.shift();
47
+ }
45
48
  }
49
+ // make sure this field doesn't exist on any filter
50
+ delete filter._id;
46
51
  }
47
- // make sure this field doesn't exist on any filter
48
- delete filter._id;
49
52
  }
50
- }
51
- // there is no id, just use the user provided filters
52
- if (!idCopy || !table) {
53
- return filters;
54
- }
55
- // if used as URL parameter it will have been joined
56
- if (!Array.isArray(idCopy)) {
57
- idCopy = (0, utils_1.breakRowIdField)(idCopy);
58
- }
59
- const equal = {};
60
- if (primary && idCopy) {
61
- for (let field of primary) {
62
- // work through the ID and get the parts
63
- equal[field] = idCopy.shift();
53
+ // there is no id, just use the user provided filters
54
+ if (!idCopy || !table) {
55
+ return filters;
64
56
  }
65
- }
66
- return {
67
- equal,
68
- };
69
- }
70
- /**
71
- * This function checks the incoming parameters to make sure all the inputs are
72
- * valid based on on the table schema. The main thing this is looking for is when a
73
- * user has made use of the _id field of a row for a foreign key or a search parameter.
74
- * In these cases the key will be sent up as [1], rather than 1. In these cases we will
75
- * simplify it down to the requirements. This function is quite complex as we try to be
76
- * relatively restrictive over what types of columns we will perform this action for.
77
- */
78
- function cleanupConfig(config, table) {
79
- const primaryOptions = [
80
- constants_1.FieldTypes.STRING,
81
- constants_1.FieldTypes.LONGFORM,
82
- constants_1.FieldTypes.OPTIONS,
83
- constants_1.FieldTypes.NUMBER,
84
- ];
85
- // filter out fields which cannot be keys
86
- const fieldNames = Object.entries(table.schema)
87
- .filter(schema => primaryOptions.find(val => val === schema[1].type))
88
- .map(([fieldName]) => fieldName);
89
- const iterateObject = (obj) => {
90
- for (let [field, value] of Object.entries(obj)) {
91
- if (fieldNames.find(name => name === field) && (0, utils_1.isRowId)(value)) {
92
- obj[field] = (0, utils_1.convertRowId)(value);
93
- }
57
+ // if used as URL parameter it will have been joined
58
+ if (!Array.isArray(idCopy)) {
59
+ idCopy = (0, utils_1.breakRowIdField)(idCopy);
94
60
  }
95
- };
96
- // check the row and filters to make sure they aren't a key of some sort
97
- if (config.filters) {
98
- for (let [key, filter] of Object.entries(config.filters)) {
99
- // oneOf is an array, don't iterate it
100
- if (typeof filter !== "object" ||
101
- Object.keys(filter).length === 0 ||
102
- key === types_1.FilterType.ONE_OF) {
103
- continue;
61
+ const equal = {};
62
+ if (primary && idCopy) {
63
+ for (let field of primary) {
64
+ // work through the ID and get the parts
65
+ equal[field] = idCopy.shift();
104
66
  }
105
- iterateObject(filter);
106
67
  }
68
+ return {
69
+ equal,
70
+ };
107
71
  }
108
- if (config.row) {
109
- iterateObject(config.row);
110
- }
111
- return config;
112
- }
113
- function generateIdForRow(row, table) {
114
- const primary = table.primary;
115
- if (!row || !primary) {
116
- return "";
117
- }
118
- // build id array
119
- let idParts = [];
120
- for (let field of primary) {
121
- // need to handle table name + field or just field, depending on if relationships used
122
- const fieldValue = row[`${table.name}.${field}`] || row[field];
123
- if (fieldValue) {
124
- idParts.push(fieldValue);
72
+ /**
73
+ * This function checks the incoming parameters to make sure all the inputs are
74
+ * valid based on on the table schema. The main thing this is looking for is when a
75
+ * user has made use of the _id field of a row for a foreign key or a search parameter.
76
+ * In these cases the key will be sent up as [1], rather than 1. In these cases we will
77
+ * simplify it down to the requirements. This function is quite complex as we try to be
78
+ * relatively restrictive over what types of columns we will perform this action for.
79
+ */
80
+ function cleanupConfig(config, table) {
81
+ const primaryOptions = [
82
+ constants_1.FieldTypes.STRING,
83
+ constants_1.FieldTypes.LONGFORM,
84
+ constants_1.FieldTypes.OPTIONS,
85
+ constants_1.FieldTypes.NUMBER,
86
+ ];
87
+ // filter out fields which cannot be keys
88
+ const fieldNames = Object.entries(table.schema)
89
+ .filter(schema => primaryOptions.find(val => val === schema[1].type))
90
+ .map(([fieldName]) => fieldName);
91
+ const iterateObject = (obj) => {
92
+ for (let [field, value] of Object.entries(obj)) {
93
+ if (fieldNames.find(name => name === field) && (0, utils_1.isRowId)(value)) {
94
+ obj[field] = (0, utils_1.convertRowId)(value);
95
+ }
96
+ }
97
+ };
98
+ // check the row and filters to make sure they aren't a key of some sort
99
+ if (config.filters) {
100
+ for (let [key, filter] of Object.entries(config.filters)) {
101
+ // oneOf is an array, don't iterate it
102
+ if (typeof filter !== "object" ||
103
+ Object.keys(filter).length === 0 ||
104
+ key === types_1.FilterType.ONE_OF) {
105
+ continue;
106
+ }
107
+ iterateObject(filter);
108
+ }
125
109
  }
126
- }
127
- if (idParts.length === 0) {
128
- return "";
129
- }
130
- return (0, utils_1.generateRowIdField)(idParts);
131
- }
132
- function getEndpoint(tableId, operation) {
133
- if (!tableId) {
134
- return {};
135
- }
136
- const { datasourceId, tableName } = (0, utils_3.breakExternalTableId)(tableId);
137
- return {
138
- datasourceId,
139
- entityId: tableName,
140
- operation,
141
- };
142
- }
143
- function basicProcessing(row, table) {
144
- const thisRow = {};
145
- // filter the row down to what is actually the row (not joined)
146
- for (let fieldName of Object.keys(table.schema)) {
147
- const pathValue = row[`${table.name}.${fieldName}`];
148
- const value = pathValue != null ? pathValue : row[fieldName];
149
- // all responses include "select col as table.col" so that overlaps are handled
150
- if (value != null) {
151
- thisRow[fieldName] = value;
110
+ if (config.row) {
111
+ iterateObject(config.row);
152
112
  }
113
+ return config;
153
114
  }
154
- thisRow._id = generateIdForRow(row, table);
155
- thisRow.tableId = table._id;
156
- thisRow._rev = "rev";
157
- return (0, rowProcessor_1.processFormulas)(table, thisRow);
158
- }
159
- function fixArrayTypes(row, table) {
160
- for (let [fieldName, schema] of Object.entries(table.schema)) {
161
- if (schema.type === constants_1.FieldTypes.ARRAY &&
162
- typeof row[fieldName] === "string") {
163
- try {
164
- row[fieldName] = JSON.parse(row[fieldName]);
165
- }
166
- catch (err) {
167
- // couldn't convert back to array, ignore
168
- delete row[fieldName];
115
+ function generateIdForRow(row, table) {
116
+ const primary = table.primary;
117
+ if (!row || !primary) {
118
+ return "";
119
+ }
120
+ // build id array
121
+ let idParts = [];
122
+ for (let field of primary) {
123
+ // need to handle table name + field or just field, depending on if relationships used
124
+ const fieldValue = row[`${table.name}.${field}`] || row[field];
125
+ if (fieldValue) {
126
+ idParts.push(fieldValue);
169
127
  }
170
128
  }
171
- }
172
- return row;
173
- }
174
- function isOneSide(field) {
175
- return (field.relationshipType && field.relationshipType.split("-")[0] === "one");
176
- }
177
- class ExternalRequest {
178
- constructor(operation, tableId, datasource) {
179
- this.tables = {};
180
- this.operation = operation;
181
- this.tableId = tableId;
182
- this.datasource = datasource;
183
- if (datasource && datasource.entities) {
184
- this.tables = datasource.entities;
129
+ if (idParts.length === 0) {
130
+ return "";
185
131
  }
132
+ return (0, utils_1.generateRowIdField)(idParts);
186
133
  }
187
- getTable(tableId) {
134
+ function getEndpoint(tableId, operation) {
188
135
  if (!tableId) {
189
- throw "Table ID is unknown, cannot find table";
190
- }
191
- const { tableName } = (0, utils_3.breakExternalTableId)(tableId);
192
- if (tableName) {
193
- return this.tables[tableName];
136
+ return {};
194
137
  }
138
+ const { datasourceId, tableName } = (0, utils_3.breakExternalTableId)(tableId);
139
+ return {
140
+ datasourceId,
141
+ entityId: tableName,
142
+ operation,
143
+ };
195
144
  }
196
- inputProcessing(row, table) {
197
- var _a;
198
- if (!row) {
199
- return { row, manyRelationships: [] };
200
- }
201
- // we don't really support composite keys for relationships, this is why [0] is used
202
- // @ts-ignore
203
- const tablePrimary = table.primary[0];
204
- let newRow = {}, manyRelationships = [];
205
- for (let [key, field] of Object.entries(table.schema)) {
206
- // if set already, or not set just skip it
207
- if (row[key] == null ||
208
- newRow[key] ||
209
- field.autocolumn ||
210
- field.type === constants_1.FieldTypes.FORMULA) {
211
- continue;
212
- }
213
- // if its an empty string then it means return the column to null (if possible)
214
- if (row[key] === "") {
215
- newRow[key] = null;
216
- continue;
217
- }
218
- // parse floats/numbers
219
- if (field.type === constants_1.FieldTypes.NUMBER && !isNaN(parseFloat(row[key]))) {
220
- newRow[key] = parseFloat(row[key]);
221
- }
222
- // if its not a link then just copy it over
223
- if (field.type !== constants_1.FieldTypes.LINK) {
224
- newRow[key] = row[key];
225
- continue;
226
- }
227
- const { tableName: linkTableName } = (0, utils_3.breakExternalTableId)(field === null || field === void 0 ? void 0 : field.tableId);
228
- // table has to exist for many to many
229
- if (!linkTableName || !this.tables[linkTableName]) {
230
- continue;
231
- }
232
- const linkTable = this.tables[linkTableName];
233
- // @ts-ignore
234
- const linkTablePrimary = linkTable.primary[0];
235
- // one to many
236
- if (isOneSide(field)) {
237
- let id = row[key][0];
238
- if (typeof row[key] === "string") {
239
- id = (_a = decodeURIComponent(row[key]).match(/\[(.*?)\]/)) === null || _a === void 0 ? void 0 : _a[1];
240
- }
241
- newRow[field.foreignKey || linkTablePrimary] = (0, utils_1.breakRowIdField)(id)[0];
242
- }
243
- // many to many
244
- else if (field.through) {
245
- // we're not inserting a doc, will be a bunch of update calls
246
- const otherKey = field.throughFrom || linkTablePrimary;
247
- const thisKey = field.throughTo || tablePrimary;
248
- row[key].map((relationship) => {
249
- manyRelationships.push({
250
- tableId: field.through || field.tableId,
251
- isUpdate: false,
252
- key: otherKey,
253
- [otherKey]: (0, utils_1.breakRowIdField)(relationship)[0],
254
- // leave the ID for enrichment later
255
- [thisKey]: `{{ literal ${tablePrimary} }}`,
256
- });
257
- });
258
- }
259
- // many to one
260
- else {
261
- const thisKey = "id";
262
- // @ts-ignore
263
- const otherKey = field.fieldName;
264
- row[key].map((relationship) => {
265
- manyRelationships.push({
266
- tableId: field.tableId,
267
- isUpdate: true,
268
- key: otherKey,
269
- [thisKey]: (0, utils_1.breakRowIdField)(relationship)[0],
270
- // leave the ID for enrichment later
271
- [otherKey]: `{{ literal ${tablePrimary} }}`,
272
- });
273
- });
145
+ function basicProcessing(row, table) {
146
+ const thisRow = {};
147
+ // filter the row down to what is actually the row (not joined)
148
+ for (let fieldName of Object.keys(table.schema)) {
149
+ const pathValue = row[`${table.name}.${fieldName}`];
150
+ const value = pathValue != null ? pathValue : row[fieldName];
151
+ // all responses include "select col as table.col" so that overlaps are handled
152
+ if (value != null) {
153
+ thisRow[fieldName] = value;
274
154
  }
275
155
  }
276
- // we return the relationships that may need to be created in the through table
277
- // we do this so that if the ID is generated by the DB it can be inserted
278
- // after the fact
279
- return { row: newRow, manyRelationships };
156
+ thisRow._id = generateIdForRow(row, table);
157
+ thisRow.tableId = table._id;
158
+ thisRow._rev = "rev";
159
+ return (0, utils_4.processFormulas)(table, thisRow);
280
160
  }
281
- squashRelationshipColumns(table, row, relationships) {
282
- for (let relationship of relationships) {
283
- const linkedTable = this.tables[relationship.tableName];
284
- if (!linkedTable || !row[relationship.column]) {
285
- continue;
286
- }
287
- const display = linkedTable.primaryDisplay;
288
- for (let key of Object.keys(row[relationship.column])) {
289
- const related = row[relationship.column][key];
290
- row[relationship.column][key] = {
291
- primaryDisplay: display ? related[display] : undefined,
292
- _id: related._id,
293
- };
161
+ function fixArrayTypes(row, table) {
162
+ for (let [fieldName, schema] of Object.entries(table.schema)) {
163
+ if (schema.type === constants_1.FieldTypes.ARRAY &&
164
+ typeof row[fieldName] === "string") {
165
+ try {
166
+ row[fieldName] = JSON.parse(row[fieldName]);
167
+ }
168
+ catch (err) {
169
+ // couldn't convert back to array, ignore
170
+ delete row[fieldName];
171
+ }
294
172
  }
295
173
  }
296
174
  return row;
297
175
  }
298
- /**
299
- * This iterates through the returned rows and works out what elements of the rows
300
- * actually match up to another row (based on primary keys) - this is pretty specific
301
- * to SQL and the way that SQL relationships are returned based on joins.
302
- * This is complicated, but the idea is that when a SQL query returns all the relations
303
- * will be separate rows, with all of the data in each row. We have to decipher what comes
304
- * from where (which tables) and how to convert that into budibase columns.
305
- */
306
- updateRelationshipColumns(table, row, rows, relationships) {
307
- var _a, _b;
308
- const columns = {};
309
- for (let relationship of relationships) {
310
- const linkedTable = this.tables[relationship.tableName];
311
- if (!linkedTable) {
312
- continue;
313
- }
314
- const fromColumn = `${table.name}.${relationship.from}`;
315
- const toColumn = `${linkedTable.name}.${relationship.to}`;
316
- // this is important when working with multiple relationships
317
- // between the same tables, don't want to overlap/multiply the relations
318
- if (!relationship.through &&
319
- ((_a = row[fromColumn]) === null || _a === void 0 ? void 0 : _a.toString()) !== ((_b = row[toColumn]) === null || _b === void 0 ? void 0 : _b.toString())) {
320
- continue;
321
- }
322
- let linked = basicProcessing(row, linkedTable);
323
- if (!linked._id) {
324
- continue;
176
+ function isOneSide(field) {
177
+ return (field.relationshipType && field.relationshipType.split("-")[0] === "one");
178
+ }
179
+ class ExternalRequest {
180
+ constructor(operation, tableId, datasource) {
181
+ this.tables = {};
182
+ this.operation = operation;
183
+ this.tableId = tableId;
184
+ this.datasource = datasource;
185
+ if (datasource && datasource.entities) {
186
+ this.tables = datasource.entities;
325
187
  }
326
- columns[relationship.column] = linked;
327
188
  }
328
- for (let [column, related] of Object.entries(columns)) {
329
- if (!row._id) {
330
- continue;
331
- }
332
- const rowId = row._id;
333
- if (!Array.isArray(rows[rowId][column])) {
334
- rows[rowId][column] = [];
189
+ getTable(tableId) {
190
+ if (!tableId) {
191
+ throw "Table ID is unknown, cannot find table";
335
192
  }
336
- // make sure relationship hasn't been found already
337
- if (!rows[rowId][column].find((relation) => relation._id === related._id)) {
338
- rows[rowId][column].push(related);
193
+ const { tableName } = (0, utils_3.breakExternalTableId)(tableId);
194
+ if (tableName) {
195
+ return this.tables[tableName];
339
196
  }
340
197
  }
341
- return rows;
342
- }
343
- outputProcessing(rows = [], table, relationships) {
344
- if (!rows || rows.length === 0 || rows[0].read === true) {
345
- return [];
346
- }
347
- let finalRows = {};
348
- for (let row of rows) {
349
- const rowId = generateIdForRow(row, table);
350
- row._id = rowId;
351
- // this is a relationship of some sort
352
- if (finalRows[rowId]) {
353
- finalRows = this.updateRelationshipColumns(table, row, finalRows, relationships);
354
- continue;
198
+ inputProcessing(row, table) {
199
+ if (!row) {
200
+ return { row, manyRelationships: [] };
355
201
  }
356
- const thisRow = fixArrayTypes(basicProcessing(row, table), table);
357
- if (thisRow._id == null) {
358
- throw "Unable to generate row ID for SQL rows";
202
+ // we don't really support composite keys for relationships, this is why [0] is used
203
+ // @ts-ignore
204
+ const tablePrimary = table.primary[0];
205
+ let newRow = {}, manyRelationships = [];
206
+ for (let [key, field] of Object.entries(table.schema)) {
207
+ // if set already, or not set just skip it
208
+ if (row[key] == null ||
209
+ newRow[key] ||
210
+ field.autocolumn ||
211
+ field.type === constants_1.FieldTypes.FORMULA) {
212
+ continue;
213
+ }
214
+ // if its an empty string then it means return the column to null (if possible)
215
+ if (row[key] === "") {
216
+ newRow[key] = null;
217
+ continue;
218
+ }
219
+ // parse floats/numbers
220
+ if (field.type === constants_1.FieldTypes.NUMBER && !isNaN(parseFloat(row[key]))) {
221
+ newRow[key] = parseFloat(row[key]);
222
+ }
223
+ // if its not a link then just copy it over
224
+ if (field.type !== constants_1.FieldTypes.LINK) {
225
+ newRow[key] = row[key];
226
+ continue;
227
+ }
228
+ const { tableName: linkTableName } = (0, utils_3.breakExternalTableId)(field === null || field === void 0 ? void 0 : field.tableId);
229
+ // table has to exist for many to many
230
+ if (!linkTableName || !this.tables[linkTableName]) {
231
+ continue;
232
+ }
233
+ const linkTable = this.tables[linkTableName];
234
+ // @ts-ignore
235
+ const linkTablePrimary = linkTable.primary[0];
236
+ // one to many
237
+ if (isOneSide(field)) {
238
+ newRow[field.foreignKey || linkTablePrimary] = (0, utils_1.breakRowIdField)(row[key][0])[0];
239
+ }
240
+ // many to many
241
+ else if (field.through) {
242
+ // we're not inserting a doc, will be a bunch of update calls
243
+ const otherKey = field.throughFrom || linkTablePrimary;
244
+ const thisKey = field.throughTo || tablePrimary;
245
+ row[key].map((relationship) => {
246
+ manyRelationships.push({
247
+ tableId: field.through || field.tableId,
248
+ isUpdate: false,
249
+ key: otherKey,
250
+ [otherKey]: (0, utils_1.breakRowIdField)(relationship)[0],
251
+ // leave the ID for enrichment later
252
+ [thisKey]: `{{ literal ${tablePrimary} }}`,
253
+ });
254
+ });
255
+ }
256
+ // many to one
257
+ else {
258
+ const thisKey = "id";
259
+ // @ts-ignore
260
+ const otherKey = field.fieldName;
261
+ row[key].map((relationship) => {
262
+ manyRelationships.push({
263
+ tableId: field.tableId,
264
+ isUpdate: true,
265
+ key: otherKey,
266
+ [thisKey]: (0, utils_1.breakRowIdField)(relationship)[0],
267
+ // leave the ID for enrichment later
268
+ [otherKey]: `{{ literal ${tablePrimary} }}`,
269
+ });
270
+ });
271
+ }
359
272
  }
360
- finalRows[thisRow._id] = thisRow;
361
- // do this at end once its been added to the final rows
362
- finalRows = this.updateRelationshipColumns(table, row, finalRows, relationships);
273
+ // we return the relationships that may need to be created in the through table
274
+ // we do this so that if the ID is generated by the DB it can be inserted
275
+ // after the fact
276
+ return { row: newRow, manyRelationships };
363
277
  }
364
- // Process some additional data types
365
- let finalRowArray = Object.values(finalRows);
366
- finalRowArray = (0, rowProcessor_1.processDates)(table, finalRowArray);
367
- finalRowArray = (0, rowProcessor_1.processFormulas)(table, finalRowArray);
368
- return finalRowArray.map((row) => this.squashRelationshipColumns(table, row, relationships));
369
- }
370
- /**
371
- * Gets the list of relationship JSON structures based on the columns in the table,
372
- * this will be used by the underlying library to build whatever relationship mechanism
373
- * it has (e.g. SQL joins).
374
- */
375
- buildRelationships(table) {
376
- const relationships = [];
377
- for (let [fieldName, field] of Object.entries(table.schema)) {
378
- if (field.type !== constants_1.FieldTypes.LINK) {
379
- continue;
380
- }
381
- const { tableName: linkTableName } = (0, utils_3.breakExternalTableId)(field.tableId);
382
- // no table to link to, this is not a valid relationships
383
- if (!linkTableName || !this.tables[linkTableName]) {
384
- continue;
278
+ squashRelationshipColumns(table, row, relationships) {
279
+ for (let relationship of relationships) {
280
+ const linkedTable = this.tables[relationship.tableName];
281
+ if (!linkedTable || !row[relationship.column]) {
282
+ continue;
283
+ }
284
+ const display = linkedTable.primaryDisplay;
285
+ for (let key of Object.keys(row[relationship.column])) {
286
+ const related = row[relationship.column][key];
287
+ row[relationship.column][key] = {
288
+ primaryDisplay: display ? related[display] : undefined,
289
+ _id: related._id,
290
+ };
291
+ }
385
292
  }
386
- const linkTable = this.tables[linkTableName];
387
- if (!table.primary || !linkTable.primary) {
388
- continue;
293
+ return row;
294
+ }
295
+ /**
296
+ * This iterates through the returned rows and works out what elements of the rows
297
+ * actually match up to another row (based on primary keys) - this is pretty specific
298
+ * to SQL and the way that SQL relationships are returned based on joins.
299
+ * This is complicated, but the idea is that when a SQL query returns all the relations
300
+ * will be separate rows, with all of the data in each row. We have to decipher what comes
301
+ * from where (which tables) and how to convert that into budibase columns.
302
+ */
303
+ updateRelationshipColumns(table, row, rows, relationships) {
304
+ var _a, _b;
305
+ const columns = {};
306
+ for (let relationship of relationships) {
307
+ const linkedTable = this.tables[relationship.tableName];
308
+ if (!linkedTable) {
309
+ continue;
310
+ }
311
+ const fromColumn = `${table.name}.${relationship.from}`;
312
+ const toColumn = `${linkedTable.name}.${relationship.to}`;
313
+ // this is important when working with multiple relationships
314
+ // between the same tables, don't want to overlap/multiply the relations
315
+ if (!relationship.through &&
316
+ ((_a = row[fromColumn]) === null || _a === void 0 ? void 0 : _a.toString()) !== ((_b = row[toColumn]) === null || _b === void 0 ? void 0 : _b.toString())) {
317
+ continue;
318
+ }
319
+ let linked = basicProcessing(row, linkedTable);
320
+ if (!linked._id) {
321
+ continue;
322
+ }
323
+ columns[relationship.column] = linked;
389
324
  }
390
- const definition = {
391
- // if no foreign key specified then use the name of the field in other table
392
- from: field.foreignKey || table.primary[0],
393
- to: field.fieldName,
394
- tableName: linkTableName,
395
- // need to specify where to put this back into
396
- column: fieldName,
397
- };
398
- if (field.through) {
399
- const { tableName: throughTableName } = (0, utils_3.breakExternalTableId)(field.through);
400
- definition.through = throughTableName;
401
- // don't support composite keys for relationships
402
- definition.from = field.throughTo || table.primary[0];
403
- definition.to = field.throughFrom || linkTable.primary[0];
404
- definition.fromPrimary = table.primary[0];
405
- definition.toPrimary = linkTable.primary[0];
325
+ for (let [column, related] of Object.entries(columns)) {
326
+ if (!row._id) {
327
+ continue;
328
+ }
329
+ const rowId = row._id;
330
+ if (!Array.isArray(rows[rowId][column])) {
331
+ rows[rowId][column] = [];
332
+ }
333
+ // make sure relationship hasn't been found already
334
+ if (!rows[rowId][column].find((relation) => relation._id === related._id)) {
335
+ rows[rowId][column].push(related);
336
+ }
406
337
  }
407
- relationships.push(definition);
338
+ return rows;
408
339
  }
409
- return relationships;
410
- }
411
- /**
412
- * This is a cached lookup, of relationship records, this is mainly for creating/deleting junction
413
- * information.
414
- */
415
- lookupRelations(tableId, row) {
416
- return __awaiter(this, void 0, void 0, function* () {
417
- const related = {};
418
- const { tableName } = (0, utils_3.breakExternalTableId)(tableId);
419
- if (!tableName) {
420
- return related;
340
+ outputProcessing(rows = [], table, relationships) {
341
+ if (!rows || rows.length === 0 || rows[0].read === true) {
342
+ return [];
343
+ }
344
+ let finalRows = {};
345
+ for (let row of rows) {
346
+ const rowId = generateIdForRow(row, table);
347
+ row._id = rowId;
348
+ // this is a relationship of some sort
349
+ if (finalRows[rowId]) {
350
+ finalRows = this.updateRelationshipColumns(table, row, finalRows, relationships);
351
+ continue;
352
+ }
353
+ const thisRow = fixArrayTypes(basicProcessing(row, table), table);
354
+ if (thisRow._id == null) {
355
+ throw "Unable to generate row ID for SQL rows";
356
+ }
357
+ finalRows[thisRow._id] = thisRow;
358
+ // do this at end once its been added to the final rows
359
+ finalRows = this.updateRelationshipColumns(table, row, finalRows, relationships);
421
360
  }
422
- const table = this.tables[tableName];
423
- // @ts-ignore
424
- const primaryKey = table.primary[0];
425
- // make a new request to get the row with all its relationships
426
- // we need this to work out if any relationships need removed
427
- for (let field of Object.values(table.schema)) {
428
- if (field.type !== constants_1.FieldTypes.LINK ||
429
- !field.fieldName ||
430
- isOneSide(field)) {
361
+ // Process some additional data types
362
+ let finalRowArray = Object.values(finalRows);
363
+ finalRowArray = (0, utils_4.processDates)(table, finalRowArray);
364
+ finalRowArray = (0, utils_4.processFormulas)(table, finalRowArray);
365
+ return finalRowArray.map((row) => this.squashRelationshipColumns(table, row, relationships));
366
+ }
367
+ /**
368
+ * Gets the list of relationship JSON structures based on the columns in the table,
369
+ * this will be used by the underlying library to build whatever relationship mechanism
370
+ * it has (e.g. SQL joins).
371
+ */
372
+ buildRelationships(table) {
373
+ const relationships = [];
374
+ for (let [fieldName, field] of Object.entries(table.schema)) {
375
+ if (field.type !== constants_1.FieldTypes.LINK) {
431
376
  continue;
432
377
  }
433
- const isMany = field.relationshipType === constants_1.RelationshipTypes.MANY_TO_MANY;
434
- const tableId = isMany ? field.through : field.tableId;
435
- const { tableName: relatedTableName } = (0, utils_3.breakExternalTableId)(tableId);
436
- // @ts-ignore
437
- const linkPrimaryKey = this.tables[relatedTableName].primary[0];
438
- const manyKey = field.throughTo || primaryKey;
439
- const lookupField = isMany ? primaryKey : field.foreignKey;
440
- const fieldName = isMany ? manyKey : field.fieldName;
441
- if (!lookupField || !row[lookupField]) {
378
+ const { tableName: linkTableName } = (0, utils_3.breakExternalTableId)(field.tableId);
379
+ // no table to link to, this is not a valid relationships
380
+ if (!linkTableName || !this.tables[linkTableName]) {
442
381
  continue;
443
382
  }
444
- const response = yield (0, utils_2.getDatasourceAndQuery)({
445
- endpoint: getEndpoint(tableId, types_1.Operation.READ),
446
- filters: {
447
- equal: {
448
- [fieldName]: row[lookupField],
449
- },
450
- },
451
- });
452
- // this is the response from knex if no rows found
453
- const rows = !response[0].read ? response : [];
454
- const storeTo = isMany ? field.throughFrom || linkPrimaryKey : fieldName;
455
- related[storeTo] = { rows, isMany, tableId };
456
- }
457
- return related;
458
- });
459
- }
460
- /**
461
- * Once a row has been written we may need to update a many field, e.g. updating foreign keys
462
- * in a bunch of rows in another table, or inserting/deleting rows from a junction table (many to many).
463
- * This is quite a complex process and is handled by this function, there are a few things going on here:
464
- * 1. If updating foreign keys its relatively simple, just create a filter for the row that needs updated
465
- * and write the various components.
466
- * 2. If junction table, then we lookup what exists already, write what doesn't exist, work out what
467
- * isn't supposed to exist anymore and delete those. This is better than the usual method of delete them
468
- * all and then re-create, as theres no chance of losing data (e.g. delete succeed, but write fail).
469
- */
470
- handleManyRelationships(mainTableId, row, relationships) {
471
- var _a;
472
- return __awaiter(this, void 0, void 0, function* () {
473
- // if we're creating (in a through table) need to wipe the existing ones first
474
- const promises = [];
475
- const related = yield this.lookupRelations(mainTableId, row);
476
- for (let relationship of relationships) {
477
- const { key, tableId, isUpdate, id } = relationship, rest = __rest(relationship, ["key", "tableId", "isUpdate", "id"]);
478
- const body = (0, string_templates_1.processObjectSync)(rest, row, {});
479
- const linkTable = this.getTable(tableId);
480
- // @ts-ignore
481
- const linkPrimary = linkTable === null || linkTable === void 0 ? void 0 : linkTable.primary[0];
482
- if (!linkTable || !linkPrimary) {
483
- return;
484
- }
485
- const rows = ((_a = related[key]) === null || _a === void 0 ? void 0 : _a.rows) || [];
486
- const found = rows.find((row) => row[linkPrimary] === relationship.id ||
487
- row[linkPrimary] === (body === null || body === void 0 ? void 0 : body[linkPrimary]));
488
- const operation = isUpdate ? types_1.Operation.UPDATE : types_1.Operation.CREATE;
489
- if (!found) {
490
- promises.push((0, utils_2.getDatasourceAndQuery)({
491
- endpoint: getEndpoint(tableId, operation),
492
- // if we're doing many relationships then we're writing, only one response
493
- body,
494
- filters: buildFilters(id, {}, linkTable),
495
- }));
383
+ const linkTable = this.tables[linkTableName];
384
+ if (!table.primary || !linkTable.primary) {
385
+ continue;
496
386
  }
497
- else {
498
- // remove the relationship from cache so it isn't adjusted again
499
- rows.splice(rows.indexOf(found), 1);
387
+ const definition = {
388
+ // if no foreign key specified then use the name of the field in other table
389
+ from: field.foreignKey || table.primary[0],
390
+ to: field.fieldName,
391
+ tableName: linkTableName,
392
+ // need to specify where to put this back into
393
+ column: fieldName,
394
+ };
395
+ if (field.through) {
396
+ const { tableName: throughTableName } = (0, utils_3.breakExternalTableId)(field.through);
397
+ definition.through = throughTableName;
398
+ // don't support composite keys for relationships
399
+ definition.from = field.throughTo || table.primary[0];
400
+ definition.to = field.throughFrom || linkTable.primary[0];
401
+ definition.fromPrimary = table.primary[0];
402
+ definition.toPrimary = linkTable.primary[0];
500
403
  }
404
+ relationships.push(definition);
501
405
  }
502
- // finally cleanup anything that needs to be removed
503
- for (let [colName, { isMany, rows, tableId }] of Object.entries(related)) {
504
- const table = this.getTable(tableId);
505
- // if its not the foreign key skip it, nothing to do
506
- if (!table || (table.primary && table.primary.indexOf(colName) !== -1)) {
507
- continue;
406
+ return relationships;
407
+ }
408
+ /**
409
+ * This is a cached lookup, of relationship records, this is mainly for creating/deleting junction
410
+ * information.
411
+ */
412
+ lookupRelations(tableId, row) {
413
+ return __awaiter(this, void 0, void 0, function* () {
414
+ const related = {};
415
+ const { tableName } = (0, utils_3.breakExternalTableId)(tableId);
416
+ if (!tableName) {
417
+ return related;
418
+ }
419
+ const table = this.tables[tableName];
420
+ // @ts-ignore
421
+ const primaryKey = table.primary[0];
422
+ // make a new request to get the row with all its relationships
423
+ // we need this to work out if any relationships need removed
424
+ for (let field of Object.values(table.schema)) {
425
+ if (field.type !== constants_1.FieldTypes.LINK ||
426
+ !field.fieldName ||
427
+ isOneSide(field)) {
428
+ continue;
429
+ }
430
+ const isMany = field.relationshipType === constants_1.RelationshipTypes.MANY_TO_MANY;
431
+ const tableId = isMany ? field.through : field.tableId;
432
+ const { tableName: relatedTableName } = (0, utils_3.breakExternalTableId)(tableId);
433
+ // @ts-ignore
434
+ const linkPrimaryKey = this.tables[relatedTableName].primary[0];
435
+ const manyKey = field.throughTo || primaryKey;
436
+ const lookupField = isMany ? primaryKey : field.foreignKey;
437
+ const fieldName = isMany ? manyKey : field.fieldName;
438
+ if (!lookupField || !row[lookupField]) {
439
+ continue;
440
+ }
441
+ const response = yield (0, utils_2.getDatasourceAndQuery)({
442
+ endpoint: getEndpoint(tableId, constants_1.DataSourceOperation.READ),
443
+ filters: {
444
+ equal: {
445
+ [fieldName]: row[lookupField],
446
+ },
447
+ },
448
+ });
449
+ // this is the response from knex if no rows found
450
+ const rows = !response[0].read ? response : [];
451
+ const storeTo = isMany ? field.throughFrom || linkPrimaryKey : fieldName;
452
+ related[storeTo] = { rows, isMany, tableId };
508
453
  }
509
- for (let row of rows) {
510
- const filters = buildFilters(generateIdForRow(row, table), {}, table);
511
- // safety check, if there are no filters on deletion bad things happen
512
- if (Object.keys(filters).length !== 0) {
513
- const op = isMany ? types_1.Operation.DELETE : types_1.Operation.UPDATE;
514
- const body = isMany ? null : { [colName]: null };
454
+ return related;
455
+ });
456
+ }
457
+ /**
458
+ * Once a row has been written we may need to update a many field, e.g. updating foreign keys
459
+ * in a bunch of rows in another table, or inserting/deleting rows from a junction table (many to many).
460
+ * This is quite a complex process and is handled by this function, there are a few things going on here:
461
+ * 1. If updating foreign keys its relatively simple, just create a filter for the row that needs updated
462
+ * and write the various components.
463
+ * 2. If junction table, then we lookup what exists already, write what doesn't exist, work out what
464
+ * isn't supposed to exist anymore and delete those. This is better than the usual method of delete them
465
+ * all and then re-create, as theres no chance of losing data (e.g. delete succeed, but write fail).
466
+ */
467
+ handleManyRelationships(mainTableId, row, relationships) {
468
+ var _a;
469
+ return __awaiter(this, void 0, void 0, function* () {
470
+ // if we're creating (in a through table) need to wipe the existing ones first
471
+ const promises = [];
472
+ const related = yield this.lookupRelations(mainTableId, row);
473
+ for (let relationship of relationships) {
474
+ const { key, tableId, isUpdate, id } = relationship, rest = __rest(relationship, ["key", "tableId", "isUpdate", "id"]);
475
+ const body = (0, string_templates_1.processObjectSync)(rest, row, {});
476
+ const linkTable = this.getTable(tableId);
477
+ // @ts-ignore
478
+ const linkPrimary = linkTable === null || linkTable === void 0 ? void 0 : linkTable.primary[0];
479
+ if (!linkTable || !linkPrimary) {
480
+ return;
481
+ }
482
+ const rows = ((_a = related[key]) === null || _a === void 0 ? void 0 : _a.rows) || [];
483
+ const found = rows.find((row) => row[linkPrimary] === relationship.id ||
484
+ row[linkPrimary] === (body === null || body === void 0 ? void 0 : body[linkPrimary]));
485
+ const operation = isUpdate
486
+ ? constants_1.DataSourceOperation.UPDATE
487
+ : constants_1.DataSourceOperation.CREATE;
488
+ if (!found) {
515
489
  promises.push((0, utils_2.getDatasourceAndQuery)({
516
- endpoint: getEndpoint(tableId, op),
490
+ endpoint: getEndpoint(tableId, operation),
491
+ // if we're doing many relationships then we're writing, only one response
517
492
  body,
518
- filters,
493
+ filters: buildFilters(id, {}, linkTable),
519
494
  }));
520
495
  }
496
+ else {
497
+ // remove the relationship from cache so it isn't adjusted again
498
+ rows.splice(rows.indexOf(found), 1);
499
+ }
521
500
  }
522
- }
523
- yield Promise.all(promises);
524
- });
525
- }
526
- /**
527
- * This function is a bit crazy, but the exact purpose of it is to protect against the scenario in which
528
- * you have column overlap in relationships, e.g. we join a few different tables and they all have the
529
- * concept of an ID, but for some of them it will be null (if they say don't have a relationship).
530
- * Creating the specific list of fields that we desire, and excluding the ones that are no use to us
531
- * is more performant and has the added benefit of protecting against this scenario.
532
- */
533
- buildFields(table, includeRelations = types_1.IncludeRelationship.INCLUDE) {
534
- function extractRealFields(table, existing = []) {
535
- return Object.entries(table.schema)
536
- .filter(column => column[1].type !== constants_1.FieldTypes.LINK &&
537
- column[1].type !== constants_1.FieldTypes.FORMULA &&
538
- !existing.find((field) => field === column[0]))
539
- .map(column => `${table.name}.${column[0]}`);
501
+ // finally cleanup anything that needs to be removed
502
+ for (let [colName, { isMany, rows, tableId }] of Object.entries(related)) {
503
+ const table = this.getTable(tableId);
504
+ // if its not the foreign key skip it, nothing to do
505
+ if (!table ||
506
+ (table.primary && table.primary.indexOf(colName) !== -1)) {
507
+ continue;
508
+ }
509
+ for (let row of rows) {
510
+ const filters = buildFilters(generateIdForRow(row, table), {}, table);
511
+ // safety check, if there are no filters on deletion bad things happen
512
+ if (Object.keys(filters).length !== 0) {
513
+ const op = isMany
514
+ ? constants_1.DataSourceOperation.DELETE
515
+ : constants_1.DataSourceOperation.UPDATE;
516
+ const body = isMany ? null : { [colName]: null };
517
+ promises.push((0, utils_2.getDatasourceAndQuery)({
518
+ endpoint: getEndpoint(tableId, op),
519
+ body,
520
+ filters,
521
+ }));
522
+ }
523
+ }
524
+ }
525
+ yield Promise.all(promises);
526
+ });
540
527
  }
541
- let fields = extractRealFields(table);
542
- for (let field of Object.values(table.schema)) {
543
- if (field.type !== constants_1.FieldTypes.LINK || !includeRelations) {
544
- continue;
545
- }
546
- const { tableName: linkTableName } = (0, utils_3.breakExternalTableId)(field.tableId);
547
- if (linkTableName) {
548
- const linkTable = this.tables[linkTableName];
549
- if (linkTable) {
550
- const linkedFields = extractRealFields(linkTable, fields);
551
- fields = fields.concat(linkedFields);
528
+ /**
529
+ * This function is a bit crazy, but the exact purpose of it is to protect against the scenario in which
530
+ * you have column overlap in relationships, e.g. we join a few different tables and they all have the
531
+ * concept of an ID, but for some of them it will be null (if they say don't have a relationship).
532
+ * Creating the specific list of fields that we desire, and excluding the ones that are no use to us
533
+ * is more performant and has the added benefit of protecting against this scenario.
534
+ */
535
+ buildFields(table, includeRelations = types_1.IncludeRelationship.INCLUDE) {
536
+ function extractRealFields(table, existing = []) {
537
+ return Object.entries(table.schema)
538
+ .filter(column => column[1].type !== constants_1.FieldTypes.LINK &&
539
+ column[1].type !== constants_1.FieldTypes.FORMULA &&
540
+ !existing.find((field) => field === column[0]))
541
+ .map(column => `${table.name}.${column[0]}`);
542
+ }
543
+ let fields = extractRealFields(table);
544
+ for (let field of Object.values(table.schema)) {
545
+ if (field.type !== constants_1.FieldTypes.LINK || !includeRelations) {
546
+ continue;
547
+ }
548
+ const { tableName: linkTableName } = (0, utils_3.breakExternalTableId)(field.tableId);
549
+ if (linkTableName) {
550
+ const linkTable = this.tables[linkTableName];
551
+ if (linkTable) {
552
+ const linkedFields = extractRealFields(linkTable, fields);
553
+ fields = fields.concat(linkedFields);
554
+ }
552
555
  }
553
556
  }
557
+ return fields;
554
558
  }
555
- return fields;
556
- }
557
- run(config) {
558
- return __awaiter(this, void 0, void 0, function* () {
559
- const { operation, tableId } = this;
560
- let { datasourceId, tableName } = (0, utils_3.breakExternalTableId)(tableId);
561
- if (!tableName) {
562
- throw "Unable to run without a table name";
563
- }
564
- if (!this.datasource) {
565
- const db = backend_core_1.context.getAppDB();
566
- this.datasource = yield db.get(datasourceId);
567
- if (!this.datasource || !this.datasource.entities) {
568
- throw "No tables found, fetch tables before query.";
559
+ run(config) {
560
+ return __awaiter(this, void 0, void 0, function* () {
561
+ const { operation, tableId } = this;
562
+ let { datasourceId, tableName } = (0, utils_3.breakExternalTableId)(tableId);
563
+ if (!tableName) {
564
+ throw "Unable to run without a table name";
569
565
  }
570
- this.tables = this.datasource.entities;
571
- }
572
- const table = this.tables[tableName];
573
- let isSql = (0, utils_3.isSQL)(this.datasource);
574
- if (!table) {
575
- throw `Unable to process query, table "${tableName}" not defined.`;
576
- }
577
- // look for specific components of config which may not be considered acceptable
578
- let { id, row, filters, sort, paginate, rows } = cleanupConfig(config, table);
579
- filters = buildFilters(id, filters || {}, table);
580
- const relationships = this.buildRelationships(table);
581
- // clean up row on ingress using schema
582
- const processed = this.inputProcessing(row, table);
583
- row = processed.row;
584
- if (operation === types_1.Operation.DELETE &&
585
- (filters == null || Object.keys(filters).length === 0)) {
586
- throw "Deletion must be filtered";
587
- }
588
- let json = {
589
- endpoint: {
590
- datasourceId,
591
- entityId: tableName,
592
- operation,
593
- },
594
- resource: {
595
- // have to specify the fields to avoid column overlap (for SQL)
596
- fields: isSql ? this.buildFields(table) : [],
597
- },
598
- filters,
599
- sort,
600
- paginate,
601
- relationships,
602
- body: row || rows,
603
- // pass an id filter into extra, purely for mysql/returning
604
- extra: {
605
- idFilter: buildFilters(id || generateIdForRow(row, table), {}, table),
606
- },
607
- meta: {
608
- table,
609
- },
610
- };
611
- // can't really use response right now
612
- const response = yield (0, utils_2.getDatasourceAndQuery)(json);
613
- // handle many to many relationships now if we know the ID (could be auto increment)
614
- if (operation !== types_1.Operation.READ && processed.manyRelationships) {
615
- yield this.handleManyRelationships(table._id || "", response[0], processed.manyRelationships);
616
- }
617
- const output = this.outputProcessing(response, table, relationships);
618
- // if reading it'll just be an array of rows, return whole thing
619
- return operation === types_1.Operation.READ && Array.isArray(response)
620
- ? output
621
- : { row: output[0], table };
622
- });
566
+ if (!this.datasource) {
567
+ const db = (0, context_1.getAppDB)();
568
+ this.datasource = yield db.get(datasourceId);
569
+ if (!this.datasource || !this.datasource.entities) {
570
+ throw "No tables found, fetch tables before query.";
571
+ }
572
+ this.tables = this.datasource.entities;
573
+ }
574
+ const table = this.tables[tableName];
575
+ let isSql = (0, utils_3.isSQL)(this.datasource);
576
+ if (!table) {
577
+ throw `Unable to process query, table "${tableName}" not defined.`;
578
+ }
579
+ // look for specific components of config which may not be considered acceptable
580
+ let { id, row, filters, sort, paginate, rows } = cleanupConfig(config, table);
581
+ filters = buildFilters(id, filters || {}, table);
582
+ const relationships = this.buildRelationships(table);
583
+ // clean up row on ingress using schema
584
+ const processed = this.inputProcessing(row, table);
585
+ row = processed.row;
586
+ if (operation === constants_1.DataSourceOperation.DELETE &&
587
+ (filters == null || Object.keys(filters).length === 0)) {
588
+ throw "Deletion must be filtered";
589
+ }
590
+ let json = {
591
+ endpoint: {
592
+ datasourceId,
593
+ entityId: tableName,
594
+ operation,
595
+ },
596
+ resource: {
597
+ // have to specify the fields to avoid column overlap (for SQL)
598
+ fields: isSql ? this.buildFields(table) : [],
599
+ },
600
+ filters,
601
+ sort,
602
+ paginate,
603
+ relationships,
604
+ body: row || rows,
605
+ // pass an id filter into extra, purely for mysql/returning
606
+ extra: {
607
+ idFilter: buildFilters(id || generateIdForRow(row, table), {}, table),
608
+ },
609
+ meta: {
610
+ table,
611
+ },
612
+ };
613
+ // can't really use response right now
614
+ const response = yield (0, utils_2.getDatasourceAndQuery)(json);
615
+ // handle many to many relationships now if we know the ID (could be auto increment)
616
+ if (operation !== constants_1.DataSourceOperation.READ &&
617
+ processed.manyRelationships) {
618
+ yield this.handleManyRelationships(table._id || "", response[0], processed.manyRelationships);
619
+ }
620
+ const output = this.outputProcessing(response, table, relationships);
621
+ // if reading it'll just be an array of rows, return whole thing
622
+ return operation === constants_1.DataSourceOperation.READ && Array.isArray(response)
623
+ ? output
624
+ : { row: output[0], table };
625
+ });
626
+ }
623
627
  }
624
- }
625
- exports.ExternalRequest = ExternalRequest;
628
+ module.exports = ExternalRequest;
629
+ })(External || (External = {}));