@budibase/server 2.1.45 → 2.1.46-alpha.1

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 (832) hide show
  1. package/__mocks__/aws-sdk.ts +14 -0
  2. package/__mocks__/node-fetch.ts +5 -1
  3. package/builder/assets/index.156490b5.js +1020 -0
  4. package/builder/assets/index.538092be.css +6 -0
  5. package/builder/index.html +2 -2
  6. package/coverage/clover.xml +11101 -3980
  7. package/coverage/coverage-final.json +273 -118
  8. package/coverage/lcov-report/index.html +727 -187
  9. package/coverage/lcov-report/src/api/controllers/analytics.ts.html +190 -0
  10. package/coverage/lcov-report/{api/controllers/apikeys.js.html → src/api/controllers/apikeys.ts.html} +30 -30
  11. package/coverage/lcov-report/src/api/controllers/application.ts.html +1891 -0
  12. package/coverage/lcov-report/src/api/controllers/auth.ts.html +301 -0
  13. package/coverage/lcov-report/{api/controllers/automation.js.html → src/api/controllers/automation.ts.html} +99 -99
  14. package/coverage/lcov-report/src/api/controllers/backup.ts.html +148 -0
  15. package/coverage/lcov-report/{api/controllers/cloud.js.html → src/api/controllers/cloud.ts.html} +61 -58
  16. package/coverage/lcov-report/src/api/controllers/component.ts.html +259 -0
  17. package/coverage/lcov-report/{api/controllers/datasource.js.html → src/api/controllers/datasource.ts.html} +287 -107
  18. package/coverage/lcov-report/src/api/controllers/deploy/Deployment.ts.html +259 -0
  19. package/coverage/lcov-report/{utilities/rowProcessor → src/api/controllers/deploy}/index.html +40 -40
  20. package/coverage/lcov-report/src/api/controllers/deploy/index.ts.html +748 -0
  21. package/coverage/lcov-report/{api/controllers/dev.js.html → src/api/controllers/dev.ts.html} +69 -87
  22. package/coverage/lcov-report/src/api/controllers/index.html +431 -0
  23. package/coverage/lcov-report/src/api/controllers/integration.ts.html +124 -0
  24. package/coverage/lcov-report/{api/controllers/layout.js.html → src/api/controllers/layout.ts.html} +31 -31
  25. package/coverage/lcov-report/{api/controllers/metadata.js.html → src/api/controllers/metadata.ts.html} +33 -30
  26. package/coverage/lcov-report/{api/controllers/migrations.js.html → src/api/controllers/migrations.ts.html} +20 -17
  27. package/coverage/lcov-report/{api/controllers/permission.js.html → src/api/controllers/permission.ts.html} +88 -79
  28. package/coverage/lcov-report/src/api/controllers/plugin/file.ts.html +130 -0
  29. package/coverage/lcov-report/src/api/controllers/plugin/github.ts.html +310 -0
  30. package/coverage/lcov-report/src/api/controllers/plugin/index.html +206 -0
  31. package/coverage/lcov-report/src/api/controllers/plugin/index.ts.html +484 -0
  32. package/coverage/lcov-report/src/api/controllers/plugin/npm.ts.html +253 -0
  33. package/coverage/lcov-report/src/api/controllers/plugin/uploaders.ts.html +97 -0
  34. package/coverage/lcov-report/src/api/controllers/plugin/url.ts.html +121 -0
  35. package/coverage/lcov-report/src/api/controllers/plugin/utils.ts.html +136 -0
  36. package/coverage/lcov-report/src/api/controllers/public/applications.ts.html +334 -0
  37. package/coverage/lcov-report/src/api/controllers/public/index.html +191 -0
  38. package/coverage/lcov-report/src/api/controllers/public/mapping/applications.ts.html +181 -0
  39. package/coverage/lcov-report/src/api/controllers/public/mapping/index.html +191 -0
  40. package/coverage/lcov-report/src/api/controllers/public/mapping/index.ts.html +124 -0
  41. package/coverage/lcov-report/src/api/controllers/public/mapping/queries.ts.html +202 -0
  42. package/coverage/lcov-report/src/api/controllers/public/mapping/rows.ts.html +178 -0
  43. package/coverage/lcov-report/{utilities/routing/index.js.html → src/api/controllers/public/mapping/tables.ts.html} +46 -43
  44. package/coverage/lcov-report/src/api/controllers/public/mapping/users.ts.html +181 -0
  45. package/coverage/lcov-report/src/api/controllers/public/queries.ts.html +145 -0
  46. package/coverage/lcov-report/src/api/controllers/public/rows.ts.html +328 -0
  47. package/coverage/lcov-report/src/api/controllers/public/tables.ts.html +253 -0
  48. package/coverage/lcov-report/src/api/controllers/public/users.ts.html +325 -0
  49. package/coverage/lcov-report/src/api/controllers/public/utils.ts.html +220 -0
  50. package/coverage/lcov-report/src/api/controllers/query/import/index.html +116 -0
  51. package/coverage/lcov-report/src/api/controllers/query/import/index.ts.html +373 -0
  52. package/coverage/lcov-report/src/api/controllers/query/import/sources/base/index.html +131 -0
  53. package/coverage/lcov-report/src/api/controllers/query/import/sources/base/index.ts.html +379 -0
  54. package/coverage/lcov-report/src/api/controllers/query/import/sources/base/openapi.ts.html +157 -0
  55. package/coverage/lcov-report/src/api/controllers/query/import/sources/curl.ts.html +403 -0
  56. package/coverage/lcov-report/src/api/controllers/query/import/sources/index.html +146 -0
  57. package/coverage/lcov-report/src/api/controllers/query/import/sources/openapi2.ts.html +586 -0
  58. package/coverage/lcov-report/src/api/controllers/query/import/sources/openapi3.ts.html +712 -0
  59. package/coverage/lcov-report/src/api/controllers/query/index.html +131 -0
  60. package/coverage/lcov-report/src/api/controllers/query/index.ts.html +976 -0
  61. package/coverage/lcov-report/{api/controllers/query/validation.js.html → src/api/controllers/query/validation.ts.html} +26 -26
  62. package/coverage/lcov-report/{api/controllers/role.js.html → src/api/controllers/role.ts.html} +51 -63
  63. package/coverage/lcov-report/{api/controllers/routing.js.html → src/api/controllers/routing.ts.html} +67 -58
  64. package/coverage/lcov-report/src/api/controllers/row/ExternalRequest.ts.html +2269 -0
  65. package/coverage/lcov-report/{api/controllers/row/external.js.html → src/api/controllers/row/external.ts.html} +162 -111
  66. package/coverage/lcov-report/{api → src/api}/controllers/row/index.html +82 -52
  67. package/coverage/lcov-report/src/api/controllers/row/index.ts.html +541 -0
  68. package/coverage/lcov-report/{api/controllers/row/internal.js.html → src/api/controllers/row/internal.ts.html} +163 -121
  69. package/coverage/lcov-report/{api/controllers/row/internalSearch.js.html → src/api/controllers/row/internalSearch.ts.html} +189 -117
  70. package/coverage/lcov-report/{api/controllers/row/staticFormula.js.html → src/api/controllers/row/staticFormula.ts.html} +73 -58
  71. package/coverage/lcov-report/{api/controllers/row/utils.js.html → src/api/controllers/row/utils.ts.html} +88 -58
  72. package/coverage/lcov-report/src/api/controllers/screen.ts.html +463 -0
  73. package/coverage/lcov-report/{api/controllers/script.js.html → src/api/controllers/script.ts.html} +19 -16
  74. package/coverage/lcov-report/src/api/controllers/static/index.html +116 -0
  75. package/coverage/lcov-report/src/api/controllers/static/index.ts.html +694 -0
  76. package/coverage/lcov-report/{api/controllers/table/bulkFormula.js.html → src/api/controllers/table/bulkFormula.ts.html} +104 -62
  77. package/coverage/lcov-report/{api/controllers/table/external.js.html → src/api/controllers/table/external.ts.html} +204 -111
  78. package/coverage/lcov-report/src/api/controllers/table/index.html +176 -0
  79. package/coverage/lcov-report/{api/controllers/table/index.js.html → src/api/controllers/table/index.ts.html} +58 -49
  80. package/coverage/lcov-report/src/api/controllers/table/internal.ts.html +661 -0
  81. package/coverage/lcov-report/src/api/controllers/table/utils.ts.html +1255 -0
  82. package/coverage/lcov-report/{api/controllers/templates.js.html → src/api/controllers/templates.ts.html} +38 -29
  83. package/coverage/lcov-report/{api/controllers/user.js.html → src/api/controllers/user.ts.html} +78 -255
  84. package/coverage/lcov-report/{api/controllers/view/exporters.js.html → src/api/controllers/view/exporters.ts.html} +22 -16
  85. package/coverage/lcov-report/{api → src/api}/controllers/view/index.html +36 -36
  86. package/coverage/lcov-report/{api/controllers/view/index.js.html → src/api/controllers/view/index.ts.html} +91 -64
  87. package/coverage/lcov-report/{api/controllers/view/utils.js.html → src/api/controllers/view/utils.ts.html} +72 -54
  88. package/coverage/lcov-report/{api/controllers/view/viewBuilder.js.html → src/api/controllers/view/viewBuilder.ts.html} +78 -36
  89. package/coverage/lcov-report/src/api/controllers/webhook.ts.html +379 -0
  90. package/coverage/lcov-report/{api → src/api}/index.html +23 -23
  91. package/coverage/lcov-report/{api/index.js.html → src/api/index.ts.html} +63 -84
  92. package/coverage/lcov-report/src/api/routes/analytics.ts.html +112 -0
  93. package/coverage/lcov-report/{api/routes/integration.js.html → src/api/routes/apikeys.ts.html} +25 -25
  94. package/coverage/lcov-report/src/api/routes/application.ts.html +223 -0
  95. package/coverage/lcov-report/src/api/routes/auth.ts.html +109 -0
  96. package/coverage/lcov-report/{api/routes/automation.js.html → src/api/routes/automation.ts.html} +53 -47
  97. package/coverage/lcov-report/{api/routes/cloud.js.html → src/api/routes/backup.ts.html} +28 -28
  98. package/coverage/lcov-report/{api/routes/deploy.js.html → src/api/routes/cloud.ts.html} +31 -28
  99. package/coverage/lcov-report/{api/routes/component.js.html → src/api/routes/component.ts.html} +24 -24
  100. package/coverage/lcov-report/src/api/routes/datasource.ts.html +265 -0
  101. package/coverage/lcov-report/src/api/routes/deploy.ts.html +148 -0
  102. package/coverage/lcov-report/src/api/routes/dev.ts.html +199 -0
  103. package/coverage/lcov-report/{api → src/api}/routes/index.html +167 -47
  104. package/coverage/lcov-report/src/api/routes/index.ts.html +289 -0
  105. package/coverage/lcov-report/src/api/routes/integration.ts.html +133 -0
  106. package/coverage/lcov-report/src/api/routes/layout.ts.html +133 -0
  107. package/coverage/lcov-report/{api/routes/metadata.js.html → src/api/routes/metadata.ts.html} +31 -31
  108. package/coverage/lcov-report/{api/routes/migrations.js.html → src/api/routes/migrations.ts.html} +24 -24
  109. package/coverage/lcov-report/{api/routes/permission.js.html → src/api/routes/permission.ts.html} +55 -31
  110. package/coverage/lcov-report/src/api/routes/plugin.ts.html +151 -0
  111. package/coverage/lcov-report/src/api/routes/public/applications.ts.html +541 -0
  112. package/coverage/lcov-report/src/api/routes/public/index.html +191 -0
  113. package/coverage/lcov-report/src/api/routes/public/index.ts.html +493 -0
  114. package/coverage/lcov-report/src/api/routes/public/middleware/index.html +116 -0
  115. package/coverage/lcov-report/src/api/routes/public/middleware/mapper.ts.html +328 -0
  116. package/coverage/lcov-report/src/api/routes/public/queries.ts.html +313 -0
  117. package/coverage/lcov-report/src/api/routes/public/rows.ts.html +598 -0
  118. package/coverage/lcov-report/src/api/routes/public/tables.ts.html +586 -0
  119. package/coverage/lcov-report/src/api/routes/public/tests/index.html +116 -0
  120. package/coverage/lcov-report/src/api/routes/public/tests/utils.ts.html +169 -0
  121. package/coverage/lcov-report/src/api/routes/public/users.ts.html +511 -0
  122. package/coverage/lcov-report/src/api/routes/public/utils/Endpoint.ts.html +238 -0
  123. package/coverage/lcov-report/src/api/routes/public/utils/index.html +116 -0
  124. package/coverage/lcov-report/{api/routes/query.js.html → src/api/routes/query.ts.html} +41 -50
  125. package/coverage/lcov-report/src/api/routes/role.ts.html +157 -0
  126. package/coverage/lcov-report/{api/routes/routing.js.html → src/api/routes/routing.ts.html} +24 -24
  127. package/coverage/lcov-report/src/api/routes/row.ts.html +883 -0
  128. package/coverage/lcov-report/{api/routes/screen.js.html → src/api/routes/screen.ts.html} +43 -28
  129. package/coverage/lcov-report/{api/routes/script.js.html → src/api/routes/script.ts.html} +17 -17
  130. package/coverage/lcov-report/src/api/routes/static.ts.html +280 -0
  131. package/coverage/lcov-report/{api/routes/table.js.html → src/api/routes/table.ts.html} +32 -41
  132. package/coverage/lcov-report/src/api/routes/templates.ts.html +133 -0
  133. package/coverage/lcov-report/src/api/routes/tests/utilities/TestFunctions.ts.html +550 -0
  134. package/coverage/lcov-report/src/api/routes/tests/utilities/index.html +131 -0
  135. package/coverage/lcov-report/src/api/routes/tests/utilities/index.ts.html +367 -0
  136. package/coverage/lcov-report/{api/routes/user.js.html → src/api/routes/user.ts.html} +35 -41
  137. package/coverage/lcov-report/{api → src/api}/routes/utils/index.html +15 -15
  138. package/coverage/lcov-report/{api/routes/utils/validators.js.html → src/api/routes/utils/validators.ts.html} +86 -98
  139. package/coverage/lcov-report/{api/routes/view.js.html → src/api/routes/view.ts.html} +45 -36
  140. package/coverage/lcov-report/src/api/routes/webhook.ts.html +166 -0
  141. package/coverage/lcov-report/src/app.ts.html +406 -0
  142. package/coverage/lcov-report/{automations/actions.js.html → src/automations/actions.ts.html} +69 -60
  143. package/coverage/lcov-report/{automations/automationUtils.js.html → src/automations/automationUtils.ts.html} +36 -33
  144. package/coverage/lcov-report/{automations/bullboard.js.html → src/automations/bullboard.ts.html} +39 -42
  145. package/coverage/lcov-report/{automations → src/automations}/index.html +63 -48
  146. package/coverage/lcov-report/{api/routes/dev.js.html → src/automations/index.ts.html} +46 -46
  147. package/coverage/lcov-report/src/automations/logging/index.html +116 -0
  148. package/coverage/lcov-report/src/automations/logging/index.ts.html +199 -0
  149. package/coverage/lcov-report/{automations/steps/bash.js.html → src/automations/steps/bash.ts.html} +45 -30
  150. package/coverage/lcov-report/src/automations/steps/createRow.ts.html +382 -0
  151. package/coverage/lcov-report/{automations/steps/delay.js.html → src/automations/steps/delay.ts.html} +34 -19
  152. package/coverage/lcov-report/src/automations/steps/deleteRow.ts.html +349 -0
  153. package/coverage/lcov-report/{automations/steps/discord.js.html → src/automations/steps/discord.ts.html} +43 -28
  154. package/coverage/lcov-report/{automations/steps/executeQuery.js.html → src/automations/steps/executeQuery.ts.html} +45 -30
  155. package/coverage/lcov-report/{automations/steps/executeScript.js.html → src/automations/steps/executeScript.ts.html} +58 -28
  156. package/coverage/lcov-report/{automations/steps/filter.js.html → src/automations/steps/filter.ts.html} +39 -30
  157. package/coverage/lcov-report/src/automations/steps/index.html +371 -0
  158. package/coverage/lcov-report/{automations/steps/integromat.js.html → src/automations/steps/integromat.ts.html} +38 -23
  159. package/coverage/lcov-report/{automations/steps/loop.js.html → src/automations/steps/loop.ts.html} +21 -15
  160. package/coverage/lcov-report/{automations/steps/outgoingWebhook.js.html → src/automations/steps/outgoingWebhook.ts.html} +54 -39
  161. package/coverage/lcov-report/{automations/steps/queryRows.js.html → src/automations/steps/queryRows.ts.html} +102 -66
  162. package/coverage/lcov-report/{automations/steps/sendSmtpEmail.js.html → src/automations/steps/sendSmtpEmail.ts.html} +40 -25
  163. package/coverage/lcov-report/{automations/steps/serverLog.js.html → src/automations/steps/serverLog.ts.html} +40 -22
  164. package/coverage/lcov-report/{automations/steps/slack.js.html → src/automations/steps/slack.ts.html} +38 -23
  165. package/coverage/lcov-report/{automations/steps/updateRow.js.html → src/automations/steps/updateRow.ts.html} +40 -25
  166. package/coverage/lcov-report/{automations/steps/utils.js.html → src/automations/steps/utils.ts.html} +45 -27
  167. package/coverage/lcov-report/{automations/steps/zapier.js.html → src/automations/steps/zapier.ts.html} +41 -23
  168. package/coverage/lcov-report/src/automations/tests/utilities/index.html +116 -0
  169. package/coverage/lcov-report/src/automations/tests/utilities/index.ts.html +265 -0
  170. package/coverage/lcov-report/src/automations/triggerInfo/app.ts.html +196 -0
  171. package/coverage/lcov-report/{automations/triggerInfo/cron.js.html → src/automations/triggerInfo/cron.ts.html} +30 -15
  172. package/coverage/lcov-report/{automations → src/automations}/triggerInfo/index.html +31 -31
  173. package/coverage/lcov-report/{automations/triggerInfo/index.js.html → src/automations/triggerInfo/index.ts.html} +24 -24
  174. package/coverage/lcov-report/{automations/triggerInfo/rowDeleted.js.html → src/automations/triggerInfo/rowDeleted.ts.html} +30 -15
  175. package/coverage/lcov-report/{automations/triggerInfo/rowSaved.js.html → src/automations/triggerInfo/rowSaved.ts.html} +30 -15
  176. package/coverage/lcov-report/{automations/triggerInfo/rowUpdated.js.html → src/automations/triggerInfo/rowUpdated.ts.html} +30 -15
  177. package/coverage/lcov-report/{automations/triggerInfo/webhook.js.html → src/automations/triggerInfo/webhook.ts.html} +30 -15
  178. package/coverage/lcov-report/{automations/triggers.js.html → src/automations/triggers.ts.html} +78 -66
  179. package/coverage/lcov-report/src/automations/utils.ts.html +886 -0
  180. package/coverage/lcov-report/{constants → src/constants}/index.html +26 -26
  181. package/coverage/lcov-report/src/constants/index.ts.html +697 -0
  182. package/coverage/lcov-report/{constants/layouts.js.html → src/constants/layouts.ts.html} +18 -36
  183. package/coverage/lcov-report/{constants/screens.js.html → src/constants/screens.ts.html} +64 -58
  184. package/coverage/lcov-report/src/db/defaultData/datasource_bb_default.ts.html +2059 -0
  185. package/coverage/lcov-report/src/db/defaultData/employeeImport.ts.html +541 -0
  186. package/coverage/lcov-report/src/db/defaultData/expensesImport.ts.html +427 -0
  187. package/coverage/lcov-report/src/db/defaultData/index.html +176 -0
  188. package/coverage/lcov-report/src/db/defaultData/inventoryImport.ts.html +403 -0
  189. package/coverage/lcov-report/src/db/defaultData/jobsImport.ts.html +559 -0
  190. package/coverage/lcov-report/src/db/dynamoClient.ts.html +526 -0
  191. package/coverage/lcov-report/{db/inMemoryView.js.html → src/db/inMemoryView.ts.html} +47 -29
  192. package/coverage/lcov-report/{db → src/db}/index.html +48 -33
  193. package/coverage/lcov-report/{api/routes/templates.js.html → src/db/index.ts.html} +28 -28
  194. package/coverage/lcov-report/{db/linkedRows/LinkController.js.html → src/db/linkedRows/LinkController.ts.html} +97 -160
  195. package/coverage/lcov-report/src/db/linkedRows/LinkDocument.ts.html +265 -0
  196. package/coverage/lcov-report/src/db/linkedRows/index.html +161 -0
  197. package/coverage/lcov-report/{db/linkedRows/index.js.html → src/db/linkedRows/index.ts.html} +93 -78
  198. package/coverage/lcov-report/{db/linkedRows/linkUtils.js.html → src/db/linkedRows/linkUtils.ts.html} +83 -56
  199. package/coverage/lcov-report/{db/newid.js.html → src/db/newid.ts.html} +14 -14
  200. package/coverage/lcov-report/{db/utils.js.html → src/db/utils.ts.html} +163 -211
  201. package/coverage/lcov-report/{automations/triggerInfo/app.js.html → src/definitions/automations.ts.html} +42 -42
  202. package/coverage/lcov-report/{api/routes/datasource.js.html → src/definitions/datasource.ts.html} +61 -64
  203. package/coverage/lcov-report/src/definitions/index.html +131 -0
  204. package/coverage/lcov-report/src/environment.ts.html +457 -0
  205. package/coverage/lcov-report/{events/AutomationEmitter.js.html → src/events/AutomationEmitter.ts.html} +40 -25
  206. package/coverage/lcov-report/{events/index.js.html → src/events/BudibaseEmitter.ts.html} +26 -29
  207. package/coverage/lcov-report/{events → src/events}/index.html +48 -33
  208. package/coverage/lcov-report/src/events/index.ts.html +100 -0
  209. package/coverage/lcov-report/src/events/utils.ts.html +319 -0
  210. package/coverage/lcov-report/src/index.html +191 -0
  211. package/coverage/lcov-report/{db/index.js.html → src/index.ts.html} +35 -35
  212. package/coverage/lcov-report/{utilities/queue/inMemoryQueue.js.html → src/integrations/airtable.ts.html} +213 -135
  213. package/coverage/lcov-report/src/integrations/arangodb.ts.html +415 -0
  214. package/coverage/lcov-report/src/integrations/base/index.html +161 -0
  215. package/coverage/lcov-report/src/integrations/base/query.ts.html +133 -0
  216. package/coverage/lcov-report/src/integrations/base/sql.ts.html +1951 -0
  217. package/coverage/lcov-report/src/integrations/base/sqlTable.ts.html +691 -0
  218. package/coverage/lcov-report/{api/routes/apikeys.js.html → src/integrations/base/utils.ts.html} +30 -30
  219. package/coverage/lcov-report/src/integrations/couchdb.ts.html +406 -0
  220. package/coverage/lcov-report/{constants/index.js.html → src/integrations/dynamodb.ts.html} +263 -227
  221. package/coverage/lcov-report/src/integrations/elasticsearch.ts.html +634 -0
  222. package/coverage/lcov-report/src/integrations/firebase.ts.html +646 -0
  223. package/coverage/lcov-report/src/integrations/googlesheets.ts.html +1315 -0
  224. package/coverage/lcov-report/src/integrations/index.html +371 -0
  225. package/coverage/lcov-report/src/integrations/index.ts.html +445 -0
  226. package/coverage/lcov-report/src/integrations/microsoftSqlServer.ts.html +1012 -0
  227. package/coverage/lcov-report/src/integrations/mongodb.ts.html +1867 -0
  228. package/coverage/lcov-report/src/integrations/mysql.ts.html +967 -0
  229. package/coverage/lcov-report/src/integrations/oracle.ts.html +1387 -0
  230. package/coverage/lcov-report/src/integrations/postgres.ts.html +1087 -0
  231. package/coverage/lcov-report/src/integrations/queries/index.html +116 -0
  232. package/coverage/lcov-report/src/integrations/queries/sql.ts.html +463 -0
  233. package/coverage/lcov-report/src/integrations/redis.ts.html +550 -0
  234. package/coverage/lcov-report/src/integrations/rest.ts.html +1381 -0
  235. package/coverage/lcov-report/src/integrations/s3.ts.html +862 -0
  236. package/coverage/lcov-report/src/integrations/snowflake.ts.html +376 -0
  237. package/coverage/lcov-report/{integrations → src/integrations}/tests/TestConfiguration.js.html +10 -10
  238. package/coverage/lcov-report/{integrations → src/integrations}/tests/index.html +10 -10
  239. package/coverage/lcov-report/src/integrations/utils.ts.html +1018 -0
  240. package/coverage/lcov-report/{middleware/appInfo.js.html → src/middleware/appInfo.ts.html} +33 -30
  241. package/coverage/lcov-report/src/middleware/authorized.ts.html +460 -0
  242. package/coverage/lcov-report/{middleware/builder.js.html → src/middleware/builder.ts.html} +57 -51
  243. package/coverage/lcov-report/{middleware/currentapp.js.html → src/middleware/currentapp.ts.html} +116 -107
  244. package/coverage/lcov-report/{middleware → src/middleware}/index.html +78 -63
  245. package/coverage/lcov-report/{middleware/joi-validator.js.html → src/middleware/joi-validator.ts.html} +25 -22
  246. package/coverage/lcov-report/{middleware/publicApi.js.html → src/middleware/publicApi.ts.html} +31 -31
  247. package/coverage/lcov-report/{middleware/resourceId.js.html → src/middleware/resourceId.ts.html} +66 -51
  248. package/coverage/lcov-report/{api/controllers/integration.js.html → src/middleware/selfhost.ts.html} +22 -28
  249. package/coverage/lcov-report/{api/routes/analytics.js.html → src/middleware/utils.ts.html} +19 -19
  250. package/coverage/lcov-report/src/migrations/functions/appUrls.ts.html +166 -0
  251. package/coverage/lcov-report/src/migrations/functions/backfill/app/automations.ts.html +163 -0
  252. package/coverage/lcov-report/src/migrations/functions/backfill/app/datasources.ts.html +151 -0
  253. package/coverage/lcov-report/src/migrations/functions/backfill/app/index.html +206 -0
  254. package/coverage/lcov-report/src/migrations/functions/backfill/app/layouts.ts.html +172 -0
  255. package/coverage/lcov-report/src/migrations/functions/backfill/app/queries.ts.html +226 -0
  256. package/coverage/lcov-report/src/migrations/functions/backfill/app/roles.ts.html +151 -0
  257. package/coverage/lcov-report/src/migrations/functions/backfill/app/screens.ts.html +151 -0
  258. package/coverage/lcov-report/src/migrations/functions/backfill/app/tables.ts.html +166 -0
  259. package/coverage/lcov-report/src/migrations/functions/backfill/app.ts.html +532 -0
  260. package/coverage/lcov-report/src/migrations/functions/backfill/global/configs.ts.html +283 -0
  261. package/coverage/lcov-report/src/migrations/functions/backfill/global/index.html +146 -0
  262. package/coverage/lcov-report/src/migrations/functions/backfill/global/quotas.ts.html +265 -0
  263. package/coverage/lcov-report/src/migrations/functions/backfill/global/users.ts.html +232 -0
  264. package/coverage/lcov-report/src/migrations/functions/backfill/global.ts.html +724 -0
  265. package/coverage/lcov-report/src/migrations/functions/backfill/index.html +161 -0
  266. package/coverage/lcov-report/src/migrations/functions/backfill/index.ts.html +106 -0
  267. package/coverage/lcov-report/src/migrations/functions/backfill/installation.ts.html +235 -0
  268. package/coverage/lcov-report/src/migrations/functions/index.html +161 -0
  269. package/coverage/lcov-report/{api/routes/role.js.html → src/migrations/functions/syncQuotas.ts.html} +36 -36
  270. package/coverage/lcov-report/src/migrations/functions/tableSettings.ts.html +520 -0
  271. package/coverage/lcov-report/src/migrations/functions/usageQuotas/index.html +161 -0
  272. package/coverage/lcov-report/src/migrations/functions/usageQuotas/index.ts.html +94 -0
  273. package/coverage/lcov-report/src/migrations/functions/usageQuotas/syncApps.ts.html +127 -0
  274. package/coverage/lcov-report/src/migrations/functions/usageQuotas/syncPlugins.ts.html +115 -0
  275. package/coverage/lcov-report/{automations/index.js.html → src/migrations/functions/usageQuotas/syncRows.ts.html} +58 -61
  276. package/coverage/lcov-report/src/migrations/functions/userEmailViewCasing.ts.html +124 -0
  277. package/coverage/lcov-report/{utilities/queue → src/migrations}/index.html +23 -23
  278. package/coverage/lcov-report/src/migrations/index.ts.html +430 -0
  279. package/coverage/lcov-report/src/migrations/tests/helpers.ts.html +205 -0
  280. package/coverage/lcov-report/src/migrations/tests/index.html +131 -0
  281. package/coverage/lcov-report/src/migrations/tests/structures.ts.html +283 -0
  282. package/coverage/lcov-report/src/sdk/app/applications/index.html +146 -0
  283. package/coverage/lcov-report/{middleware/utils.js.html → src/sdk/app/applications/index.ts.html} +19 -19
  284. package/coverage/lcov-report/src/sdk/app/applications/sync.ts.html +268 -0
  285. package/coverage/lcov-report/src/sdk/app/applications/utils.ts.html +136 -0
  286. package/coverage/lcov-report/src/sdk/app/automations/index.html +131 -0
  287. package/coverage/lcov-report/src/sdk/app/automations/index.ts.html +100 -0
  288. package/coverage/lcov-report/src/sdk/app/automations/webhook.ts.html +214 -0
  289. package/coverage/lcov-report/src/sdk/app/backups/constants.ts.html +91 -0
  290. package/coverage/lcov-report/src/sdk/app/backups/exports.ts.html +604 -0
  291. package/coverage/lcov-report/src/sdk/app/backups/imports.ts.html +682 -0
  292. package/coverage/lcov-report/src/sdk/app/backups/index.html +176 -0
  293. package/coverage/lcov-report/src/sdk/app/backups/index.ts.html +112 -0
  294. package/coverage/lcov-report/src/sdk/app/backups/statistics.ts.html +292 -0
  295. package/coverage/lcov-report/src/sdk/app/tables/index.html +116 -0
  296. package/coverage/lcov-report/src/sdk/app/tables/index.ts.html +265 -0
  297. package/coverage/lcov-report/{utilities/routing → src/sdk}/index.html +10 -10
  298. package/coverage/lcov-report/{api/routes/layout.js.html → src/sdk/index.ts.html} +24 -24
  299. package/coverage/lcov-report/src/sdk/users/index.html +131 -0
  300. package/coverage/lcov-report/src/sdk/users/index.ts.html +100 -0
  301. package/coverage/lcov-report/src/sdk/users/utils.ts.html +277 -0
  302. package/coverage/lcov-report/src/startup.ts.html +499 -0
  303. package/coverage/lcov-report/src/tests/utilities/TestConfiguration.ts.html +1966 -0
  304. package/coverage/lcov-report/src/tests/utilities/controllers.ts.html +127 -0
  305. package/coverage/lcov-report/src/tests/utilities/index.html +161 -0
  306. package/coverage/lcov-report/{middleware/selfhost.js.html → src/tests/utilities/index.ts.html} +33 -30
  307. package/coverage/lcov-report/src/tests/utilities/structures.ts.html +667 -0
  308. package/coverage/lcov-report/src/threads/automation.ts.html +1594 -0
  309. package/coverage/lcov-report/src/threads/index.html +161 -0
  310. package/coverage/lcov-report/src/threads/index.ts.html +418 -0
  311. package/coverage/lcov-report/src/threads/query.ts.html +961 -0
  312. package/coverage/lcov-report/src/threads/utils.ts.html +373 -0
  313. package/coverage/lcov-report/{utilities/budibaseDir.js.html → src/utilities/budibaseDir.ts.html} +14 -14
  314. package/coverage/lcov-report/{utilities/centralPath.js.html → src/utilities/centralPath.ts.html} +16 -16
  315. package/coverage/lcov-report/{utilities/csvParser.js.html → src/utilities/csvParser.ts.html} +107 -59
  316. package/coverage/lcov-report/src/utilities/fileSystem/clientLibrary.ts.html +547 -0
  317. package/coverage/lcov-report/{api/controllers/table → src/utilities/fileSystem}/index.html +44 -44
  318. package/coverage/lcov-report/src/utilities/fileSystem/index.ts.html +1129 -0
  319. package/coverage/lcov-report/src/utilities/fileSystem/processor.ts.html +142 -0
  320. package/coverage/lcov-report/{utilities/global.js.html → src/utilities/global.ts.html} +144 -111
  321. package/coverage/lcov-report/{utilities → src/utilities}/index.html +92 -77
  322. package/coverage/lcov-report/{utilities/index.js.html → src/utilities/index.ts.html} +98 -74
  323. package/coverage/lcov-report/{utilities/plugins.js.html → src/utilities/plugins.ts.html} +25 -22
  324. package/coverage/lcov-report/src/utilities/redis.ts.html +343 -0
  325. package/coverage/lcov-report/{api/controllers/query → src/utilities/routing}/index.html +8 -8
  326. package/coverage/lcov-report/src/utilities/routing/index.ts.html +181 -0
  327. package/coverage/lcov-report/src/utilities/rowProcessor/index.html +146 -0
  328. package/coverage/lcov-report/{utilities/rowProcessor/index.js.html → src/utilities/rowProcessor/index.ts.html} +128 -359
  329. package/coverage/lcov-report/src/utilities/rowProcessor/map.ts.html +373 -0
  330. package/coverage/lcov-report/{utilities/rowProcessor/utils.js.html → src/utilities/rowProcessor/utils.ts.html} +54 -42
  331. package/coverage/lcov-report/{utilities/scriptRunner.js.html → src/utilities/scriptRunner.ts.html} +31 -19
  332. package/coverage/lcov-report/{utilities/security.js.html → src/utilities/security.ts.html} +60 -78
  333. package/coverage/lcov-report/{utilities/statusCodes.js.html → src/utilities/statusCodes.ts.html} +11 -11
  334. package/coverage/lcov-report/{utilities → src/utilities}/usageQuota/index.html +30 -30
  335. package/coverage/lcov-report/{utilities/usageQuota/rows.js.html → src/utilities/usageQuota/rows.ts.html} +42 -54
  336. package/coverage/lcov-report/{utilities/usageQuota/usageQuoteReset.js.html → src/utilities/usageQuota/usageQuoteReset.ts.html} +13 -13
  337. package/coverage/lcov-report/{utilities/users.js.html → src/utilities/users.ts.html} +44 -50
  338. package/coverage/lcov-report/{utilities/workerRequests.js.html → src/utilities/workerRequests.ts.html} +127 -112
  339. package/coverage/lcov-report/{events/utils.js.html → src/watch.ts.html} +68 -71
  340. package/coverage/lcov-report/src/websocket.ts.html +163 -0
  341. package/coverage/lcov.info +19994 -7196
  342. package/dist/api/controllers/apikeys.js +12 -9
  343. package/dist/api/controllers/application.js +430 -446
  344. package/dist/api/controllers/automation.js +122 -82
  345. package/dist/api/controllers/cloud.js +85 -71
  346. package/dist/api/controllers/component.js +4 -2
  347. package/dist/api/controllers/datasource.js +151 -94
  348. package/dist/api/controllers/deploy/Deployment.js +10 -6
  349. package/dist/api/controllers/deploy/index.js +19 -16
  350. package/dist/api/controllers/dev.js +96 -81
  351. package/dist/api/controllers/integration.js +11 -8
  352. package/dist/api/controllers/layout.js +18 -15
  353. package/dist/api/controllers/metadata.js +51 -37
  354. package/dist/api/controllers/migrations.js +19 -11
  355. package/dist/api/controllers/permission.js +52 -43
  356. package/dist/api/controllers/plugin/index.js +6 -7
  357. package/dist/api/controllers/plugin/utils.js +2 -1
  358. package/dist/api/controllers/public/applications.js +17 -9
  359. package/dist/api/controllers/public/tables.js +29 -9
  360. package/dist/api/controllers/public/users.js +18 -0
  361. package/dist/api/controllers/public/utils.js +2 -2
  362. package/dist/api/controllers/query/import/index.js +1 -2
  363. package/dist/api/controllers/query/index.js +10 -13
  364. package/dist/api/controllers/query/validation.js +41 -33
  365. package/dist/api/controllers/role.js +29 -27
  366. package/dist/api/controllers/routing.js +71 -58
  367. package/dist/api/controllers/row/ExternalRequest.js +539 -542
  368. package/dist/api/controllers/row/external.js +258 -190
  369. package/dist/api/controllers/row/index.js +43 -14
  370. package/dist/api/controllers/row/internal.js +354 -295
  371. package/dist/api/controllers/row/internalSearch.js +95 -80
  372. package/dist/api/controllers/row/staticFormula.js +125 -112
  373. package/dist/api/controllers/row/utils.js +122 -84
  374. package/dist/api/controllers/screen.js +81 -66
  375. package/dist/api/controllers/script.js +13 -6
  376. package/dist/api/controllers/static/index.js +11 -14
  377. package/dist/api/controllers/table/bulkFormula.js +47 -35
  378. package/dist/api/controllers/table/external.js +89 -53
  379. package/dist/api/controllers/table/index.js +64 -31
  380. package/dist/api/controllers/table/internal.js +5 -6
  381. package/dist/api/controllers/table/utils.js +39 -35
  382. package/dist/api/controllers/templates.js +17 -10
  383. package/dist/api/controllers/user.js +65 -94
  384. package/dist/api/controllers/view/exporters.js +8 -4
  385. package/dist/api/controllers/view/index.js +201 -153
  386. package/dist/api/controllers/view/utils.js +151 -121
  387. package/dist/api/controllers/view/viewBuilder.js +4 -16
  388. package/dist/api/controllers/webhook.js +25 -2
  389. package/dist/api/index.js +38 -34
  390. package/dist/api/routes/analytics.js +29 -3
  391. package/dist/api/routes/apikeys.js +33 -7
  392. package/dist/api/routes/application.js +8 -9
  393. package/dist/api/routes/auth.js +1 -2
  394. package/dist/api/routes/automation.js +46 -20
  395. package/dist/api/routes/backup.js +3 -4
  396. package/dist/api/routes/cloud.js +32 -6
  397. package/dist/api/routes/component.js +32 -6
  398. package/dist/api/routes/datasource.js +39 -13
  399. package/dist/api/routes/deploy.js +34 -8
  400. package/dist/api/routes/dev.js +36 -10
  401. package/dist/api/routes/integration.js +33 -7
  402. package/dist/api/routes/layout.js +33 -7
  403. package/dist/api/routes/metadata.js +36 -10
  404. package/dist/api/routes/migrations.js +32 -6
  405. package/dist/api/routes/permission.js +38 -12
  406. package/dist/api/routes/plugin.js +5 -5
  407. package/dist/api/routes/public/index.js +11 -11
  408. package/dist/api/routes/public/tests/utils.js +33 -0
  409. package/dist/api/routes/query.js +42 -15
  410. package/dist/api/routes/role.js +36 -10
  411. package/dist/api/routes/routing.js +32 -6
  412. package/dist/api/routes/row.js +12 -11
  413. package/dist/api/routes/screen.js +35 -9
  414. package/dist/api/routes/script.js +32 -6
  415. package/dist/api/routes/static.js +9 -9
  416. package/dist/api/routes/table.js +40 -13
  417. package/dist/api/routes/templates.js +33 -7
  418. package/dist/api/routes/tests/utilities/TestFunctions.js +6 -8
  419. package/dist/api/routes/tests/utilities/index.js +69 -32
  420. package/dist/api/routes/user.js +40 -13
  421. package/dist/api/routes/utils/validators.js +142 -126
  422. package/dist/api/routes/view.js +38 -12
  423. package/dist/api/routes/webhook.js +1 -2
  424. package/dist/app.js +22 -22
  425. package/dist/automations/actions.js +53 -24
  426. package/dist/automations/automationUtils.js +31 -19
  427. package/dist/automations/bullboard.js +59 -29
  428. package/dist/automations/index.js +23 -18
  429. package/dist/automations/steps/bash.js +40 -10
  430. package/dist/automations/steps/createRow.js +2 -1
  431. package/dist/automations/steps/delay.js +9 -5
  432. package/dist/automations/steps/deleteRow.js +2 -1
  433. package/dist/automations/steps/discord.js +14 -7
  434. package/dist/automations/steps/executeQuery.js +34 -7
  435. package/dist/automations/steps/executeScript.js +34 -7
  436. package/dist/automations/steps/filter.js +20 -18
  437. package/dist/automations/steps/integromat.js +14 -7
  438. package/dist/automations/steps/loop.js +4 -1
  439. package/dist/automations/steps/outgoingWebhook.js +45 -14
  440. package/dist/automations/steps/queryRows.js +71 -38
  441. package/dist/automations/steps/sendSmtpEmail.js +33 -6
  442. package/dist/automations/steps/serverLog.js +12 -8
  443. package/dist/automations/steps/slack.js +14 -7
  444. package/dist/automations/steps/updateRow.js +34 -7
  445. package/dist/automations/steps/utils.js +22 -16
  446. package/dist/automations/steps/zapier.js +15 -7
  447. package/dist/automations/tests/utilities/index.js +62 -38
  448. package/dist/automations/triggerInfo/app.js +4 -1
  449. package/dist/automations/triggerInfo/cron.js +4 -1
  450. package/dist/automations/triggerInfo/index.js +31 -6
  451. package/dist/automations/triggerInfo/rowDeleted.js +4 -1
  452. package/dist/automations/triggerInfo/rowSaved.js +4 -1
  453. package/dist/automations/triggerInfo/rowUpdated.js +4 -1
  454. package/dist/automations/triggerInfo/webhook.js +4 -1
  455. package/dist/automations/triggers.js +90 -57
  456. package/dist/automations/utils.js +9 -8
  457. package/dist/constants/index.js +118 -104
  458. package/dist/constants/layouts.js +7 -10
  459. package/dist/constants/screens.js +49 -44
  460. package/dist/db/defaultData/datasource_bb_default.js +573 -0
  461. package/dist/db/defaultData/employeeImport.js +155 -0
  462. package/dist/db/defaultData/expensesImport.js +117 -0
  463. package/dist/db/defaultData/inventoryImport.js +109 -0
  464. package/dist/db/defaultData/jobsImport.js +152 -0
  465. package/dist/db/dynamoClient.js +20 -20
  466. package/dist/db/inMemoryView.js +47 -38
  467. package/dist/db/index.js +7 -4
  468. package/dist/db/linkedRows/LinkController.js +51 -70
  469. package/dist/db/linkedRows/LinkDocument.js +32 -0
  470. package/dist/db/linkedRows/index.js +93 -81
  471. package/dist/db/linkedRows/linkUtils.js +42 -32
  472. package/dist/db/utils.js +146 -119
  473. package/dist/db/views/staticViews.js +91 -74
  474. package/dist/environment.js +9 -6
  475. package/dist/events/AutomationEmitter.js +13 -10
  476. package/dist/events/BudibaseEmitter.js +24 -0
  477. package/dist/events/index.js +5 -23
  478. package/dist/events/utils.js +18 -10
  479. package/dist/integrations/base/types.js +2 -0
  480. package/dist/integrations/elasticsearch.js +26 -2
  481. package/dist/integrations/googlesheets.js +5 -6
  482. package/dist/integrations/index.js +15 -6
  483. package/dist/integrations/microsoftSqlServer.js +8 -3
  484. package/dist/integrations/oracle.js +8 -1
  485. package/dist/integrations/postgres.js +6 -3
  486. package/dist/integrations/s3.js +157 -1
  487. package/dist/middleware/appInfo.js +14 -9
  488. package/dist/middleware/authorized.js +22 -21
  489. package/dist/middleware/builder.js +41 -40
  490. package/dist/middleware/currentapp.js +39 -37
  491. package/dist/middleware/joi-validator.js +17 -10
  492. package/dist/middleware/publicApi.js +5 -6
  493. package/dist/middleware/resourceId.js +16 -9
  494. package/dist/middleware/selfhost.js +5 -4
  495. package/dist/middleware/utils.js +5 -2
  496. package/dist/migrations/functions/appUrls.js +7 -11
  497. package/dist/migrations/functions/backfill/global.js +1 -1
  498. package/dist/migrations/functions/tableSettings.js +130 -0
  499. package/dist/migrations/functions/usageQuotas/syncApps.js +3 -4
  500. package/dist/migrations/functions/usageQuotas/syncRows.js +3 -4
  501. package/dist/migrations/functions/userEmailViewCasing.js +3 -3
  502. package/dist/migrations/index.js +5 -0
  503. package/dist/package.json +11 -45
  504. package/dist/sdk/app/applications/index.js +28 -0
  505. package/dist/sdk/app/applications/sync.js +70 -0
  506. package/dist/sdk/app/applications/utils.js +21 -0
  507. package/dist/sdk/app/backups/exports.js +1 -2
  508. package/dist/sdk/app/backups/imports.js +2 -3
  509. package/dist/sdk/app/tables/index.js +5 -5
  510. package/dist/sdk/index.js +4 -2
  511. package/dist/sdk/users/index.js +27 -0
  512. package/dist/sdk/users/utils.js +65 -0
  513. package/dist/startup.js +10 -10
  514. package/dist/tests/utilities/TestConfiguration.js +111 -87
  515. package/dist/tests/utilities/controllers.js +38 -15
  516. package/dist/tests/utilities/structures.js +72 -53
  517. package/dist/threads/automation.js +49 -27
  518. package/dist/threads/query.js +17 -19
  519. package/dist/threads/utils.js +33 -8
  520. package/dist/tsconfig.build.tsbuildinfo +1 -1
  521. package/dist/utilities/budibaseDir.js +4 -2
  522. package/dist/utilities/centralPath.js +14 -7
  523. package/dist/utilities/csvParser.js +27 -21
  524. package/dist/utilities/fileSystem/clientLibrary.js +87 -73
  525. package/dist/utilities/fileSystem/index.js +203 -175
  526. package/dist/utilities/fileSystem/processor.js +8 -3
  527. package/dist/utilities/global.js +87 -66
  528. package/dist/utilities/index.js +102 -75
  529. package/dist/utilities/plugins.js +15 -9
  530. package/dist/utilities/redis.js +100 -65
  531. package/dist/utilities/routing/index.js +27 -23
  532. package/dist/utilities/rowProcessor/index.js +133 -191
  533. package/dist/utilities/rowProcessor/map.js +99 -0
  534. package/dist/utilities/rowProcessor/utils.js +35 -26
  535. package/dist/utilities/scriptRunner.js +8 -5
  536. package/dist/utilities/security.js +39 -35
  537. package/dist/utilities/usageQuota/rows.js +56 -49
  538. package/dist/utilities/users.js +31 -28
  539. package/dist/utilities/workerRequests.js +115 -82
  540. package/dist/watch.js +1 -2
  541. package/jest.config.ts +33 -0
  542. package/package.json +12 -46
  543. package/scripts/exportAppTemplate.js +2 -2
  544. package/scripts/integrations/mssql/data/setup.sql +3 -3
  545. package/scripts/integrations/postgres/emp-skill.sql +46 -0
  546. package/scripts/localdomain.js +34 -6
  547. package/src/api/controllers/{apikeys.js → apikeys.ts} +10 -10
  548. package/src/api/controllers/application.ts +100 -135
  549. package/src/api/controllers/auth.ts +4 -3
  550. package/src/api/controllers/{automation.js → automation.ts} +48 -48
  551. package/src/api/controllers/{cloud.js → cloud.ts} +25 -24
  552. package/src/api/controllers/component.ts +2 -1
  553. package/src/api/controllers/{datasource.js → datasource.ts} +109 -49
  554. package/src/api/controllers/deploy/{Deployment.js → Deployment.ts} +13 -10
  555. package/src/api/controllers/deploy/index.ts +19 -25
  556. package/src/api/controllers/{dev.js → dev.ts} +31 -37
  557. package/src/api/controllers/integration.ts +13 -0
  558. package/src/api/controllers/{layout.js → layout.ts} +8 -8
  559. package/src/api/controllers/{metadata.js → metadata.ts} +11 -10
  560. package/src/api/controllers/migrations.ts +14 -0
  561. package/src/api/controllers/{permission.js → permission.ts} +49 -46
  562. package/src/api/controllers/plugin/index.ts +4 -5
  563. package/src/api/controllers/plugin/utils.ts +3 -5
  564. package/src/api/controllers/public/applications.ts +15 -9
  565. package/src/api/controllers/public/rows.ts +1 -1
  566. package/src/api/controllers/public/tables.ts +2 -2
  567. package/src/api/controllers/public/users.ts +25 -6
  568. package/src/api/controllers/public/utils.ts +2 -2
  569. package/src/api/controllers/query/import/index.ts +2 -3
  570. package/src/api/controllers/query/index.ts +11 -14
  571. package/src/api/controllers/query/{validation.js → validation.ts} +7 -7
  572. package/src/api/controllers/{role.js → role.ts} +21 -25
  573. package/src/api/controllers/{routing.js → routing.ts} +37 -34
  574. package/src/api/controllers/row/ExternalRequest.ts +605 -624
  575. package/src/api/controllers/row/{external.js → external.ts} +79 -62
  576. package/src/api/controllers/row/index.ts +19 -11
  577. package/src/api/controllers/row/{internal.js → internal.ts} +87 -73
  578. package/src/api/controllers/row/{internalSearch.js → internalSearch.ts} +92 -68
  579. package/src/api/controllers/row/{staticFormula.js → staticFormula.ts} +30 -25
  580. package/src/api/controllers/row/{utils.js → utils.ts} +32 -22
  581. package/src/api/controllers/screen.ts +10 -6
  582. package/src/api/controllers/script.ts +12 -0
  583. package/src/api/controllers/static/index.ts +14 -16
  584. package/src/api/controllers/table/{bulkFormula.js → bulkFormula.ts} +39 -25
  585. package/src/api/controllers/table/{external.js → external.ts} +86 -55
  586. package/src/api/controllers/table/{index.js → index.ts} +21 -18
  587. package/src/api/controllers/table/internal.ts +5 -6
  588. package/src/api/controllers/table/utils.ts +26 -21
  589. package/src/api/controllers/{templates.js → templates.ts} +10 -7
  590. package/src/api/controllers/{user.js → user.ts} +42 -101
  591. package/src/api/controllers/view/{exporters.js → exporters.ts} +5 -3
  592. package/src/api/controllers/view/{index.js → index.ts} +36 -27
  593. package/src/api/controllers/view/{utils.js → utils.ts} +26 -20
  594. package/src/api/controllers/view/{viewBuilder.js → viewBuilder.ts} +25 -11
  595. package/src/api/controllers/webhook.ts +1 -1
  596. package/src/api/{index.js → index.ts} +15 -22
  597. package/src/api/routes/analytics.ts +9 -0
  598. package/src/api/routes/apikeys.ts +12 -0
  599. package/src/api/routes/application.ts +17 -9
  600. package/src/api/routes/auth.ts +2 -2
  601. package/src/api/routes/automation.ts +87 -0
  602. package/src/api/routes/backup.ts +8 -4
  603. package/src/api/routes/cloud.ts +18 -0
  604. package/src/api/routes/component.ts +14 -0
  605. package/src/api/routes/datasource.ts +60 -0
  606. package/src/api/routes/deploy.ts +21 -0
  607. package/src/api/routes/dev.ts +38 -0
  608. package/src/api/routes/integration.ts +16 -0
  609. package/src/api/routes/layout.ts +16 -0
  610. package/src/api/routes/{metadata.js → metadata.ts} +13 -13
  611. package/src/api/routes/migrations.ts +14 -0
  612. package/src/api/routes/permission.ts +41 -0
  613. package/src/api/routes/plugin.ts +14 -6
  614. package/src/api/routes/public/index.ts +11 -14
  615. package/src/api/routes/public/tests/compare.spec.js +3 -21
  616. package/src/api/routes/public/tests/users.spec.js +38 -0
  617. package/src/api/routes/public/tests/utils.ts +28 -0
  618. package/src/api/routes/{query.js → query.ts} +19 -22
  619. package/src/api/routes/role.ts +24 -0
  620. package/src/api/routes/routing.ts +14 -0
  621. package/src/api/routes/row.ts +13 -15
  622. package/src/api/routes/screen.ts +23 -0
  623. package/src/api/routes/script.ts +10 -0
  624. package/src/api/routes/static.ts +7 -10
  625. package/src/api/routes/{table.js → table.ts} +10 -13
  626. package/src/api/routes/templates.ts +16 -0
  627. package/src/api/routes/tests/analytics.spec.js +1 -1
  628. package/src/api/routes/tests/application.spec.js +0 -1
  629. package/src/api/routes/tests/backup.spec.js +9 -1
  630. package/src/api/routes/tests/deployment.spec.ts +1 -1
  631. package/src/api/routes/tests/permissions.spec.js +2 -1
  632. package/src/api/routes/tests/query.spec.js +15 -10
  633. package/src/api/routes/tests/role.spec.js +7 -9
  634. package/src/api/routes/tests/routing.spec.js +2 -1
  635. package/src/api/routes/tests/row.spec.js +3 -4
  636. package/src/api/routes/tests/static.spec.js +2 -2
  637. package/src/api/routes/tests/table.spec.js +40 -36
  638. package/src/api/routes/tests/user.spec.js +2 -1
  639. package/src/api/routes/tests/utilities/TestFunctions.ts +6 -8
  640. package/src/api/routes/tests/utilities/{index.js → index.ts} +12 -12
  641. package/src/api/routes/tests/view.spec.js +3 -3
  642. package/src/api/routes/user.ts +51 -0
  643. package/src/api/routes/utils/{validators.js → validators.ts} +33 -37
  644. package/src/api/routes/view.ts +34 -0
  645. package/src/api/routes/webhook.ts +2 -2
  646. package/src/app.ts +15 -14
  647. package/src/automations/{actions.js → actions.ts} +26 -23
  648. package/src/automations/{automationUtils.js → automationUtils.ts} +13 -12
  649. package/src/automations/bullboard.ts +38 -0
  650. package/src/automations/index.ts +26 -0
  651. package/src/automations/steps/{bash.js → bash.ts} +13 -8
  652. package/src/automations/steps/createRow.ts +8 -3
  653. package/src/automations/steps/{delay.js → delay.ts} +9 -4
  654. package/src/automations/steps/deleteRow.ts +8 -3
  655. package/src/automations/steps/{discord.js → discord.ts} +10 -5
  656. package/src/automations/steps/{executeQuery.js → executeQuery.ts} +12 -7
  657. package/src/automations/steps/{executeScript.js → executeScript.ts} +17 -7
  658. package/src/automations/steps/{filter.js → filter.ts} +12 -9
  659. package/src/automations/steps/{integromat.js → integromat.ts} +10 -5
  660. package/src/automations/steps/{loop.js → loop.ts} +4 -2
  661. package/src/automations/steps/{outgoingWebhook.js → outgoingWebhook.ts} +18 -13
  662. package/src/automations/steps/{queryRows.js → queryRows.ts} +48 -36
  663. package/src/automations/steps/{sendSmtpEmail.js → sendSmtpEmail.ts} +10 -5
  664. package/src/automations/steps/{serverLog.js → serverLog.ts} +9 -3
  665. package/src/automations/steps/{slack.js → slack.ts} +10 -5
  666. package/src/automations/steps/{updateRow.js → updateRow.ts} +12 -7
  667. package/src/automations/steps/{utils.js → utils.ts} +10 -4
  668. package/src/automations/steps/{zapier.js → zapier.ts} +11 -5
  669. package/src/automations/tests/updateRow.spec.js +0 -1
  670. package/src/automations/tests/utilities/index.ts +60 -0
  671. package/src/automations/triggerInfo/{app.js → app.ts} +7 -2
  672. package/src/automations/triggerInfo/{cron.js → cron.ts} +7 -2
  673. package/src/automations/triggerInfo/index.ts +15 -0
  674. package/src/automations/triggerInfo/{rowDeleted.js → rowDeleted.ts} +7 -2
  675. package/src/automations/triggerInfo/{rowSaved.js → rowSaved.ts} +7 -2
  676. package/src/automations/triggerInfo/{rowUpdated.js → rowUpdated.ts} +7 -2
  677. package/src/automations/triggerInfo/{webhook.js → webhook.ts} +7 -2
  678. package/src/automations/{triggers.js → triggers.ts} +30 -26
  679. package/src/automations/utils.ts +9 -12
  680. package/src/constants/index.ts +204 -0
  681. package/src/constants/{layouts.js → layouts.ts} +3 -9
  682. package/src/constants/screens.ts +48 -0
  683. package/src/db/defaultData/datasource_bb_default.ts +658 -0
  684. package/src/db/defaultData/employeeImport.ts +152 -0
  685. package/src/db/defaultData/expensesImport.ts +114 -0
  686. package/src/db/defaultData/inventoryImport.ts +106 -0
  687. package/src/db/defaultData/jobsImport.ts +158 -0
  688. package/src/db/{dynamoClient.js → dynamoClient.ts} +39 -19
  689. package/src/db/{inMemoryView.js → inMemoryView.ts} +15 -9
  690. package/src/db/index.ts +16 -0
  691. package/src/db/linkedRows/{LinkController.js → LinkController.ts} +64 -85
  692. package/src/db/linkedRows/LinkDocument.ts +60 -0
  693. package/src/db/linkedRows/{index.js → index.ts} +43 -38
  694. package/src/db/linkedRows/{linkUtils.js → linkUtils.ts} +33 -24
  695. package/src/db/{newid.js → newid.ts} +1 -1
  696. package/src/db/tests/linkController.spec.js +73 -57
  697. package/src/db/tests/linkTests.spec.js +17 -14
  698. package/src/db/{utils.js → utils.ts} +77 -93
  699. package/src/db/views/{staticViews.js → staticViews.ts} +20 -13
  700. package/src/{environment.js → environment.ts} +11 -7
  701. package/src/events/{AutomationEmitter.js → AutomationEmitter.ts} +12 -7
  702. package/src/events/{index.js → BudibaseEmitter.ts} +7 -8
  703. package/src/events/index.ts +5 -0
  704. package/src/events/utils.ts +78 -0
  705. package/src/integrations/base/sql.ts +1 -1
  706. package/src/integrations/base/sqlTable.ts +1 -1
  707. package/src/integrations/base/types.ts +134 -0
  708. package/src/integrations/elasticsearch.ts +32 -2
  709. package/src/integrations/googlesheets.ts +5 -7
  710. package/src/integrations/index.ts +45 -41
  711. package/src/integrations/microsoftSqlServer.ts +14 -12
  712. package/src/integrations/mysql.ts +4 -3
  713. package/src/integrations/oracle.ts +17 -46
  714. package/src/integrations/postgres.ts +8 -2
  715. package/src/integrations/s3.ts +179 -3
  716. package/src/integrations/tests/s3.spec.ts +85 -1
  717. package/src/integrations/utils.ts +6 -2
  718. package/src/middleware/appInfo.ts +20 -0
  719. package/src/middleware/authorized.ts +21 -28
  720. package/src/middleware/{builder.js → builder.ts} +17 -15
  721. package/src/middleware/{currentapp.js → currentapp.ts} +42 -39
  722. package/src/middleware/{joi-validator.js → joi-validator.ts} +9 -8
  723. package/src/middleware/publicApi.ts +21 -0
  724. package/src/middleware/{resourceId.js → resourceId.ts} +16 -11
  725. package/src/middleware/{selfhost.js → selfhost.ts} +4 -2
  726. package/src/middleware/tests/authorized.spec.js +5 -6
  727. package/src/middleware/tests/currentapp.spec.js +118 -63
  728. package/src/middleware/{utils.js → utils.ts} +3 -1
  729. package/src/migrations/functions/appUrls.ts +4 -11
  730. package/src/migrations/functions/backfill/app/tables.ts +2 -5
  731. package/src/migrations/functions/backfill/global.ts +1 -1
  732. package/src/migrations/functions/tableSettings.ts +145 -0
  733. package/src/migrations/functions/tests/appUrls.spec.js +4 -4
  734. package/src/migrations/functions/tests/tableSettings.spec.ts +144 -0
  735. package/src/migrations/functions/tests/userEmailViewCasing.spec.js +14 -9
  736. package/src/migrations/functions/usageQuotas/syncApps.ts +3 -4
  737. package/src/migrations/functions/usageQuotas/syncRows.ts +4 -5
  738. package/src/migrations/functions/usageQuotas/tests/syncRows.spec.ts +7 -7
  739. package/src/migrations/functions/userEmailViewCasing.ts +3 -3
  740. package/src/migrations/index.ts +9 -0
  741. package/src/migrations/tests/index.spec.ts +1 -1
  742. package/src/sdk/app/applications/index.ts +7 -0
  743. package/src/sdk/app/applications/sync.ts +61 -0
  744. package/src/sdk/app/applications/utils.ts +17 -0
  745. package/src/sdk/app/backups/exports.ts +5 -3
  746. package/src/sdk/app/backups/imports.ts +7 -14
  747. package/src/sdk/app/backups/statistics.ts +9 -18
  748. package/src/sdk/app/tables/index.ts +7 -8
  749. package/src/sdk/index.ts +4 -3
  750. package/src/sdk/users/index.ts +5 -0
  751. package/src/sdk/users/utils.ts +64 -0
  752. package/src/startup.ts +5 -5
  753. package/{scripts/jestSetup.js → src/tests/jestSetup.ts} +11 -2
  754. package/src/tests/utilities/{TestConfiguration.js → TestConfiguration.ts} +111 -78
  755. package/src/tests/utilities/controllers.ts +14 -0
  756. package/src/tests/utilities/{index.js → index.ts} +2 -2
  757. package/src/tests/utilities/{structures.js → structures.ts} +41 -36
  758. package/src/threads/automation.ts +18 -19
  759. package/src/threads/query.ts +11 -16
  760. package/src/threads/utils.ts +4 -5
  761. package/src/utilities/budibaseDir.ts +3 -0
  762. package/src/utilities/{centralPath.js → centralPath.ts} +3 -3
  763. package/src/utilities/{csvParser.js → csvParser.ts} +42 -26
  764. package/src/utilities/fileSystem/{clientLibrary.js → clientLibrary.ts} +20 -20
  765. package/src/utilities/fileSystem/{index.js → index.ts} +61 -65
  766. package/src/utilities/fileSystem/{processor.js → processor.ts} +3 -5
  767. package/src/utilities/global.ts +138 -0
  768. package/src/utilities/{index.js → index.ts} +40 -32
  769. package/src/utilities/{plugins.js → plugins.ts} +5 -4
  770. package/src/utilities/{redis.js → redis.ts} +21 -20
  771. package/src/utilities/routing/index.ts +32 -0
  772. package/src/utilities/rowProcessor/{index.js → index.ts} +67 -144
  773. package/src/utilities/rowProcessor/map.ts +96 -0
  774. package/src/utilities/rowProcessor/{utils.js → utils.ts} +19 -15
  775. package/src/utilities/{scriptRunner.js → scriptRunner.ts} +8 -4
  776. package/src/utilities/security.ts +65 -0
  777. package/src/utilities/{statusCodes.js → statusCodes.ts} +1 -1
  778. package/src/utilities/usageQuota/{rows.js → rows.ts} +14 -18
  779. package/src/utilities/usageQuota/{usageQuoteReset.js → usageQuoteReset.ts} +1 -1
  780. package/src/utilities/{users.js → users.ts} +11 -13
  781. package/src/utilities/{workerRequests.js → workerRequests.ts} +43 -38
  782. package/src/watch.ts +2 -3
  783. package/tsconfig.build.json +1 -2
  784. package/tsconfig.json +3 -1
  785. package/builder/assets/index.534f6c29.css +0 -6
  786. package/builder/assets/index.f1f9afd2.js +0 -1021
  787. package/coverage/lcov-report/api/controllers/index.html +0 -326
  788. package/coverage/lcov-report/automations/steps/index.html +0 -341
  789. package/coverage/lcov-report/db/linkedRows/index.html +0 -146
  790. package/dist/integrations/tests/TestConfiguration.js +0 -1
  791. package/dist/utilities/fileSystem/utilities.js +0 -22
  792. package/dist/utilities/queue/inMemoryQueue.js +0 -126
  793. package/src/api/controllers/integration.js +0 -14
  794. package/src/api/controllers/migrations.js +0 -13
  795. package/src/api/controllers/script.js +0 -11
  796. package/src/api/routes/analytics.js +0 -9
  797. package/src/api/routes/apikeys.js +0 -12
  798. package/src/api/routes/automation.js +0 -85
  799. package/src/api/routes/cloud.js +0 -14
  800. package/src/api/routes/component.js +0 -14
  801. package/src/api/routes/datasource.js +0 -51
  802. package/src/api/routes/deploy.js +0 -17
  803. package/src/api/routes/dev.js +0 -26
  804. package/src/api/routes/integration.js +0 -12
  805. package/src/api/routes/layout.js +0 -16
  806. package/src/api/routes/migrations.js +0 -14
  807. package/src/api/routes/permission.js +0 -33
  808. package/src/api/routes/role.js +0 -15
  809. package/src/api/routes/routing.js +0 -14
  810. package/src/api/routes/screen.js +0 -18
  811. package/src/api/routes/script.js +0 -10
  812. package/src/api/routes/templates.js +0 -16
  813. package/src/api/routes/user.js +0 -53
  814. package/src/api/routes/view.js +0 -31
  815. package/src/automations/bullboard.js +0 -39
  816. package/src/automations/index.js +0 -29
  817. package/src/automations/tests/utilities/index.js +0 -51
  818. package/src/automations/triggerInfo/index.js +0 -15
  819. package/src/constants/index.js +0 -208
  820. package/src/constants/screens.js +0 -46
  821. package/src/db/index.js +0 -16
  822. package/src/events/utils.js +0 -38
  823. package/src/middleware/appInfo.js +0 -19
  824. package/src/middleware/publicApi.js +0 -21
  825. package/src/module.d.ts +0 -14
  826. package/src/tests/utilities/controllers.js +0 -16
  827. package/src/utilities/budibaseDir.js +0 -3
  828. package/src/utilities/fileSystem/utilities.js +0 -36
  829. package/src/utilities/global.js +0 -127
  830. package/src/utilities/queue/inMemoryQueue.js +0 -122
  831. package/src/utilities/routing/index.js +0 -25
  832. package/src/utilities/security.js +0 -71
@@ -20,6 +20,7 @@ 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;
23
24
  const types_1 = require("@budibase/types");
24
25
  const utils_1 = require("../../../integrations/utils");
25
26
  const utils_2 = require("./utils");
@@ -28,602 +29,598 @@ const utils_3 = require("../../../integrations/utils");
28
29
  const string_templates_1 = require("@budibase/string-templates");
29
30
  // @ts-ignore
30
31
  const fp_1 = require("lodash/fp");
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
- }
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();
48
45
  }
49
- // make sure this field doesn't exist on any filter
50
- delete filter._id;
51
46
  }
47
+ // make sure this field doesn't exist on any filter
48
+ delete filter._id;
52
49
  }
53
- // there is no id, just use the user provided filters
54
- if (!idCopy || !table) {
55
- return filters;
56
- }
57
- // if used as URL parameter it will have been joined
58
- if (!Array.isArray(idCopy)) {
59
- idCopy = (0, utils_1.breakRowIdField)(idCopy);
60
- }
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();
66
- }
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();
67
64
  }
68
- return {
69
- equal,
70
- };
71
65
  }
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
- }
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);
96
93
  }
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);
94
+ }
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;
108
104
  }
105
+ iterateObject(filter);
109
106
  }
110
- if (config.row) {
111
- iterateObject(config.row);
107
+ }
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);
112
125
  }
113
- return config;
114
126
  }
115
- function generateIdForRow(row, table) {
116
- const primary = table.primary;
117
- if (!row || !primary) {
118
- return "";
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;
119
152
  }
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);
153
+ }
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];
127
169
  }
128
170
  }
129
- if (idParts.length === 0) {
130
- return "";
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;
131
185
  }
132
- return (0, utils_1.generateRowIdField)(idParts);
133
186
  }
134
- function getEndpoint(tableId, operation) {
187
+ getTable(tableId) {
135
188
  if (!tableId) {
136
- return {};
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];
137
194
  }
138
- const { datasourceId, tableName } = (0, utils_3.breakExternalTableId)(tableId);
139
- return {
140
- datasourceId,
141
- entityId: tableName,
142
- operation,
143
- };
144
195
  }
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;
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
+ });
154
274
  }
155
275
  }
156
- thisRow._id = generateIdForRow(row, table);
157
- thisRow.tableId = table._id;
158
- thisRow._rev = "rev";
159
- return (0, utils_4.processFormulas)(table, thisRow);
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 };
160
280
  }
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
- }
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
+ };
172
294
  }
173
295
  }
174
296
  return row;
175
297
  }
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;
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;
187
313
  }
188
- }
189
- getTable(tableId) {
190
- if (!tableId) {
191
- throw "Table ID is unknown, cannot find table";
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;
192
321
  }
193
- const { tableName } = (0, utils_3.breakExternalTableId)(tableId);
194
- if (tableName) {
195
- return this.tables[tableName];
322
+ let linked = basicProcessing(row, linkedTable);
323
+ if (!linked._id) {
324
+ continue;
196
325
  }
326
+ columns[relationship.column] = linked;
197
327
  }
198
- inputProcessing(row, table) {
199
- if (!row) {
200
- return { row, manyRelationships: [] };
328
+ for (let [column, related] of Object.entries(columns)) {
329
+ if (!row._id) {
330
+ continue;
201
331
  }
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
- }
332
+ const rowId = row._id;
333
+ if (!Array.isArray(rows[rowId][column])) {
334
+ rows[rowId][column] = [];
272
335
  }
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 };
277
- }
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
- }
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);
292
339
  }
293
- return row;
294
340
  }
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;
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;
324
355
  }
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
- }
356
+ const thisRow = fixArrayTypes(basicProcessing(row, table), table);
357
+ if (thisRow._id == null) {
358
+ throw "Unable to generate row ID for SQL rows";
337
359
  }
338
- return rows;
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);
339
363
  }
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);
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;
385
+ }
386
+ const linkTable = this.tables[linkTableName];
387
+ if (!table.primary || !linkTable.primary) {
388
+ continue;
389
+ }
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];
360
406
  }
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));
407
+ relationships.push(definition);
366
408
  }
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) {
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;
421
+ }
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)) {
376
431
  continue;
377
432
  }
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]) {
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]) {
381
442
  continue;
382
443
  }
383
- const linkTable = this.tables[linkTableName];
384
- if (!table.primary || !linkTable.primary) {
385
- continue;
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
+ }));
386
496
  }
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];
497
+ else {
498
+ // remove the relationship from cache so it isn't adjusted again
499
+ rows.splice(rows.indexOf(found), 1);
403
500
  }
404
- relationships.push(definition);
405
501
  }
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 };
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 ||
507
+ (!isMany && table.primary && table.primary.indexOf(colName) !== -1)) {
508
+ continue;
453
509
  }
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) {
510
+ for (let row of rows) {
511
+ const filters = buildFilters(generateIdForRow(row, table), {}, table);
512
+ // safety check, if there are no filters on deletion bad things happen
513
+ if (Object.keys(filters).length !== 0) {
514
+ const op = isMany ? types_1.Operation.DELETE : types_1.Operation.UPDATE;
515
+ const body = isMany ? null : { [colName]: null };
489
516
  promises.push((0, utils_2.getDatasourceAndQuery)({
490
- endpoint: getEndpoint(tableId, operation),
491
- // if we're doing many relationships then we're writing, only one response
517
+ endpoint: getEndpoint(tableId, op),
492
518
  body,
493
- filters: buildFilters(id, {}, linkTable),
519
+ filters,
494
520
  }));
495
521
  }
496
- else {
497
- // remove the relationship from cache so it isn't adjusted again
498
- rows.splice(rows.indexOf(found), 1);
499
- }
500
- }
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
522
  }
525
- yield Promise.all(promises);
526
- });
523
+ }
524
+ yield Promise.all(promises);
525
+ });
526
+ }
527
+ /**
528
+ * This function is a bit crazy, but the exact purpose of it is to protect against the scenario in which
529
+ * you have column overlap in relationships, e.g. we join a few different tables and they all have the
530
+ * concept of an ID, but for some of them it will be null (if they say don't have a relationship).
531
+ * Creating the specific list of fields that we desire, and excluding the ones that are no use to us
532
+ * is more performant and has the added benefit of protecting against this scenario.
533
+ */
534
+ buildFields(table, includeRelations = types_1.IncludeRelationship.INCLUDE) {
535
+ function extractRealFields(table, existing = []) {
536
+ return Object.entries(table.schema)
537
+ .filter(column => column[1].type !== constants_1.FieldTypes.LINK &&
538
+ column[1].type !== constants_1.FieldTypes.FORMULA &&
539
+ !existing.find((field) => field === column[0]))
540
+ .map(column => `${table.name}.${column[0]}`);
527
541
  }
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
- }
542
+ let fields = extractRealFields(table);
543
+ for (let field of Object.values(table.schema)) {
544
+ if (field.type !== constants_1.FieldTypes.LINK || !includeRelations) {
545
+ continue;
546
+ }
547
+ const { tableName: linkTableName } = (0, utils_3.breakExternalTableId)(field.tableId);
548
+ if (linkTableName) {
549
+ const linkTable = this.tables[linkTableName];
550
+ if (linkTable) {
551
+ const linkedFields = extractRealFields(linkTable, fields);
552
+ fields = fields.concat(linkedFields);
555
553
  }
556
554
  }
557
- return fields;
558
555
  }
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";
565
- }
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);
556
+ return fields;
557
+ }
558
+ run(config) {
559
+ return __awaiter(this, void 0, void 0, function* () {
560
+ const { operation, tableId } = this;
561
+ let { datasourceId, tableName } = (0, utils_3.breakExternalTableId)(tableId);
562
+ if (!tableName) {
563
+ throw "Unable to run without a table name";
564
+ }
565
+ if (!this.datasource) {
566
+ const db = backend_core_1.context.getAppDB();
567
+ this.datasource = yield db.get(datasourceId);
568
+ if (!this.datasource || !this.datasource.entities) {
569
+ throw "No tables found, fetch tables before query.";
619
570
  }
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
- }
571
+ this.tables = this.datasource.entities;
572
+ }
573
+ const table = this.tables[tableName];
574
+ let isSql = (0, utils_3.isSQL)(this.datasource);
575
+ if (!table) {
576
+ throw `Unable to process query, table "${tableName}" not defined.`;
577
+ }
578
+ // look for specific components of config which may not be considered acceptable
579
+ let { id, row, filters, sort, paginate, rows } = cleanupConfig(config, table);
580
+ filters = buildFilters(id, filters || {}, table);
581
+ const relationships = this.buildRelationships(table);
582
+ // clean up row on ingress using schema
583
+ const processed = this.inputProcessing(row, table);
584
+ row = processed.row;
585
+ if (operation === types_1.Operation.DELETE &&
586
+ (filters == null || Object.keys(filters).length === 0)) {
587
+ throw "Deletion must be filtered";
588
+ }
589
+ let json = {
590
+ endpoint: {
591
+ datasourceId,
592
+ entityId: tableName,
593
+ operation,
594
+ },
595
+ resource: {
596
+ // have to specify the fields to avoid column overlap (for SQL)
597
+ fields: isSql ? this.buildFields(table) : [],
598
+ },
599
+ filters,
600
+ sort,
601
+ paginate,
602
+ relationships,
603
+ body: row || rows,
604
+ // pass an id filter into extra, purely for mysql/returning
605
+ extra: {
606
+ idFilter: buildFilters(id || generateIdForRow(row, table), {}, table),
607
+ },
608
+ meta: {
609
+ table,
610
+ },
611
+ };
612
+ // can't really use response right now
613
+ const response = yield (0, utils_2.getDatasourceAndQuery)(json);
614
+ // handle many to many relationships now if we know the ID (could be auto increment)
615
+ if (operation !== types_1.Operation.READ && processed.manyRelationships) {
616
+ yield this.handleManyRelationships(table._id || "", response[0], processed.manyRelationships);
617
+ }
618
+ const output = this.outputProcessing(response, table, relationships);
619
+ // if reading it'll just be an array of rows, return whole thing
620
+ return operation === types_1.Operation.READ && Array.isArray(response)
621
+ ? output
622
+ : { row: output[0], table };
623
+ });
627
624
  }
628
- module.exports = ExternalRequest;
629
- })(External || (External = {}));
625
+ }
626
+ exports.ExternalRequest = ExternalRequest;