@budibase/server 1.0.49-alpha.6 → 1.0.49

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 (376) hide show
  1. package/__mocks__/arangojs.ts +1 -1
  2. package/__mocks__/aws-sdk.ts +4 -4
  3. package/__mocks__/mongodb.ts +5 -4
  4. package/__mocks__/mssql.ts +3 -3
  5. package/__mocks__/node-fetch.ts +1 -2
  6. package/__mocks__/pg.ts +5 -3
  7. package/builder/assets/{index.1b905242.js → index.2c37968e.js} +231 -234
  8. package/builder/assets/index.39e01193.css +1 -0
  9. package/builder/assets/{vendor.d526bfe8.js → vendor.f9faf6ab.js} +12 -12
  10. package/builder/index.html +3 -3
  11. package/coverage/clover.xml +2816 -3119
  12. package/coverage/coverage-final.json +101 -106
  13. package/coverage/lcov-report/api/controllers/analytics.js.html +1 -1
  14. package/coverage/lcov-report/api/controllers/apikeys.js.html +1 -1
  15. package/coverage/lcov-report/api/controllers/application.js.html +152 -257
  16. package/coverage/lcov-report/api/controllers/auth.js.html +13 -22
  17. package/coverage/lcov-report/api/controllers/automation.js.html +69 -39
  18. package/coverage/lcov-report/api/controllers/backup.js.html +1 -1
  19. package/coverage/lcov-report/api/controllers/cloud.js.html +11 -8
  20. package/coverage/lcov-report/api/controllers/component.js.html +16 -13
  21. package/coverage/lcov-report/api/controllers/datasource.js.html +51 -81
  22. package/coverage/lcov-report/api/controllers/dev.js.html +6 -6
  23. package/coverage/lcov-report/{utilities/usageQuota/usageQuoteReset.js.html → api/controllers/hosting.js.html} +48 -36
  24. package/coverage/lcov-report/api/controllers/index.html +76 -76
  25. package/coverage/lcov-report/api/controllers/integration.js.html +1 -1
  26. package/coverage/lcov-report/api/controllers/layout.js.html +9 -9
  27. package/coverage/lcov-report/api/controllers/metadata.js.html +21 -6
  28. package/coverage/lcov-report/api/controllers/permission.js.html +19 -19
  29. package/coverage/lcov-report/api/controllers/query/index.html +10 -10
  30. package/coverage/lcov-report/api/controllers/query/index.js.html +35 -38
  31. package/coverage/lcov-report/api/controllers/query/validation.js.html +6 -6
  32. package/coverage/lcov-report/api/controllers/role.js.html +16 -16
  33. package/coverage/lcov-report/api/controllers/routing.js.html +10 -7
  34. package/coverage/lcov-report/api/controllers/row/external.js.html +27 -21
  35. package/coverage/lcov-report/api/controllers/row/index.html +34 -49
  36. package/coverage/lcov-report/api/controllers/row/index.js.html +26 -26
  37. package/coverage/lcov-report/api/controllers/row/internal.js.html +226 -118
  38. package/coverage/lcov-report/api/controllers/row/internalSearch.js.html +34 -52
  39. package/coverage/lcov-report/api/controllers/row/utils.js.html +62 -47
  40. package/coverage/lcov-report/api/controllers/screen.js.html +21 -15
  41. package/coverage/lcov-report/api/controllers/script.js.html +4 -4
  42. package/coverage/lcov-report/api/controllers/table/external.js.html +34 -25
  43. package/coverage/lcov-report/api/controllers/table/index.html +43 -58
  44. package/coverage/lcov-report/api/controllers/table/index.js.html +82 -58
  45. package/coverage/lcov-report/api/controllers/table/internal.js.html +66 -81
  46. package/coverage/lcov-report/api/controllers/table/utils.js.html +110 -218
  47. package/coverage/lcov-report/api/controllers/templates.js.html +1 -1
  48. package/coverage/lcov-report/api/controllers/user.js.html +50 -47
  49. package/coverage/lcov-report/api/controllers/view/exporters.js.html +4 -4
  50. package/coverage/lcov-report/api/controllers/view/index.html +16 -16
  51. package/coverage/lcov-report/api/controllers/view/index.js.html +33 -27
  52. package/coverage/lcov-report/api/controllers/view/utils.js.html +20 -35
  53. package/coverage/lcov-report/api/controllers/view/viewBuilder.js.html +7 -7
  54. package/coverage/lcov-report/api/controllers/webhook.js.html +28 -34
  55. package/coverage/lcov-report/api/index.html +1 -1
  56. package/coverage/lcov-report/api/index.js.html +8 -8
  57. package/coverage/lcov-report/api/routes/analytics.js.html +1 -1
  58. package/coverage/lcov-report/api/routes/apikeys.js.html +1 -1
  59. package/coverage/lcov-report/api/routes/application.js.html +1 -1
  60. package/coverage/lcov-report/api/routes/auth.js.html +1 -1
  61. package/coverage/lcov-report/api/routes/automation.js.html +1 -1
  62. package/coverage/lcov-report/api/routes/backup.js.html +1 -1
  63. package/coverage/lcov-report/api/routes/cloud.js.html +1 -1
  64. package/coverage/lcov-report/api/routes/component.js.html +1 -1
  65. package/coverage/lcov-report/api/routes/datasource.js.html +1 -1
  66. package/coverage/lcov-report/api/routes/deploy.js.html +1 -1
  67. package/coverage/lcov-report/api/routes/dev.js.html +1 -1
  68. package/coverage/lcov-report/api/routes/{migrations.js.html → hosting.js.html} +14 -17
  69. package/coverage/lcov-report/api/routes/index.html +18 -18
  70. package/coverage/lcov-report/api/routes/index.js.html +3 -3
  71. package/coverage/lcov-report/api/routes/integration.js.html +1 -1
  72. package/coverage/lcov-report/api/routes/layout.js.html +1 -1
  73. package/coverage/lcov-report/api/routes/metadata.js.html +1 -1
  74. package/coverage/lcov-report/api/routes/permission.js.html +1 -1
  75. package/coverage/lcov-report/api/routes/query.js.html +1 -1
  76. package/coverage/lcov-report/api/routes/role.js.html +1 -1
  77. package/coverage/lcov-report/api/routes/routing.js.html +1 -1
  78. package/coverage/lcov-report/api/routes/row.js.html +1 -1
  79. package/coverage/lcov-report/api/routes/screen.js.html +1 -1
  80. package/coverage/lcov-report/api/routes/script.js.html +1 -1
  81. package/coverage/lcov-report/api/routes/static.js.html +2 -17
  82. package/coverage/lcov-report/api/routes/table.js.html +1 -1
  83. package/coverage/lcov-report/api/routes/templates.js.html +1 -1
  84. package/coverage/lcov-report/api/routes/user.js.html +1 -1
  85. package/coverage/lcov-report/api/routes/view.js.html +1 -1
  86. package/coverage/lcov-report/api/routes/webhook.js.html +1 -1
  87. package/coverage/lcov-report/automations/actions.js.html +24 -24
  88. package/coverage/lcov-report/automations/automationUtils.js.html +10 -7
  89. package/coverage/lcov-report/automations/bullboard.js.html +16 -16
  90. package/coverage/lcov-report/automations/index.html +18 -18
  91. package/coverage/lcov-report/automations/index.js.html +1 -1
  92. package/coverage/lcov-report/automations/steps/bash.js.html +6 -6
  93. package/coverage/lcov-report/automations/steps/createRow.js.html +22 -13
  94. package/coverage/lcov-report/automations/steps/delay.js.html +4 -4
  95. package/coverage/lcov-report/automations/steps/deleteRow.js.html +20 -11
  96. package/coverage/lcov-report/automations/steps/discord.js.html +7 -7
  97. package/coverage/lcov-report/automations/steps/executeQuery.js.html +6 -6
  98. package/coverage/lcov-report/automations/steps/executeScript.js.html +6 -6
  99. package/coverage/lcov-report/automations/steps/filter.js.html +7 -7
  100. package/coverage/lcov-report/automations/steps/index.html +13 -13
  101. package/coverage/lcov-report/automations/steps/integromat.js.html +5 -5
  102. package/coverage/lcov-report/automations/steps/outgoingWebhook.js.html +8 -8
  103. package/coverage/lcov-report/automations/steps/queryRows.js.html +10 -10
  104. package/coverage/lcov-report/automations/steps/sendSmtpEmail.js.html +5 -5
  105. package/coverage/lcov-report/automations/steps/serverLog.js.html +3 -3
  106. package/coverage/lcov-report/automations/steps/slack.js.html +5 -5
  107. package/coverage/lcov-report/automations/steps/updateRow.js.html +7 -7
  108. package/coverage/lcov-report/automations/steps/utils.js.html +3 -3
  109. package/coverage/lcov-report/automations/steps/zapier.js.html +5 -5
  110. package/coverage/lcov-report/automations/triggerInfo/app.js.html +2 -2
  111. package/coverage/lcov-report/automations/triggerInfo/cron.js.html +2 -2
  112. package/coverage/lcov-report/automations/triggerInfo/index.html +1 -1
  113. package/coverage/lcov-report/automations/triggerInfo/index.js.html +8 -8
  114. package/coverage/lcov-report/automations/triggerInfo/rowDeleted.js.html +2 -2
  115. package/coverage/lcov-report/automations/triggerInfo/rowSaved.js.html +2 -2
  116. package/coverage/lcov-report/automations/triggerInfo/rowUpdated.js.html +2 -2
  117. package/coverage/lcov-report/automations/triggerInfo/webhook.js.html +2 -2
  118. package/coverage/lcov-report/automations/triggers.js.html +55 -61
  119. package/coverage/lcov-report/automations/utils.js.html +42 -54
  120. package/coverage/lcov-report/constants/index.html +5 -5
  121. package/coverage/lcov-report/constants/index.js.html +26 -53
  122. package/coverage/lcov-report/constants/layouts.js.html +5 -5
  123. package/coverage/lcov-report/constants/screens.js.html +4 -4
  124. package/coverage/lcov-report/db/client.js.html +18 -18
  125. package/coverage/lcov-report/db/inMemoryView.js.html +7 -7
  126. package/coverage/lcov-report/db/index.html +15 -15
  127. package/coverage/lcov-report/db/index.js.html +3 -3
  128. package/coverage/lcov-report/db/linkedRows/LinkController.js.html +37 -25
  129. package/coverage/lcov-report/db/linkedRows/index.html +27 -27
  130. package/coverage/lcov-report/db/linkedRows/index.js.html +96 -51
  131. package/coverage/lcov-report/db/linkedRows/linkUtils.js.html +42 -42
  132. package/coverage/lcov-report/db/newid.js.html +4 -4
  133. package/coverage/lcov-report/db/utils.js.html +87 -87
  134. package/coverage/lcov-report/events/AutomationEmitter.js.html +1 -1
  135. package/coverage/lcov-report/events/index.html +1 -1
  136. package/coverage/lcov-report/events/index.js.html +5 -5
  137. package/coverage/lcov-report/events/utils.js.html +3 -3
  138. package/coverage/lcov-report/index.html +120 -135
  139. package/coverage/lcov-report/integrations/tests/TestConfiguration.js.html +1 -1
  140. package/coverage/lcov-report/integrations/tests/index.html +1 -1
  141. package/coverage/lcov-report/middleware/appInfo.js.html +1 -1
  142. package/coverage/lcov-report/middleware/authorized.js.html +65 -173
  143. package/coverage/lcov-report/middleware/builder.js.html +10 -10
  144. package/coverage/lcov-report/middleware/currentapp.js.html +83 -95
  145. package/coverage/lcov-report/middleware/index.html +38 -38
  146. package/coverage/lcov-report/middleware/joi-validator.js.html +3 -3
  147. package/coverage/lcov-report/middleware/resourceId.js.html +1 -1
  148. package/coverage/lcov-report/middleware/selfhost.js.html +1 -1
  149. package/coverage/lcov-report/middleware/usageQuota.js.html +53 -281
  150. package/coverage/lcov-report/middleware/utils.js.html +2 -2
  151. package/coverage/lcov-report/threads/automation.js.html +24 -30
  152. package/coverage/lcov-report/threads/index.html +26 -26
  153. package/coverage/lcov-report/threads/index.js.html +10 -10
  154. package/coverage/lcov-report/threads/query.js.html +26 -62
  155. package/coverage/lcov-report/threads/utils.js.html +14 -14
  156. package/coverage/lcov-report/utilities/bcrypt.js.html +1 -1
  157. package/coverage/lcov-report/utilities/budibaseDir.js.html +7 -7
  158. package/coverage/lcov-report/utilities/centralPath.js.html +4 -4
  159. package/coverage/lcov-report/utilities/csvParser.js.html +6 -6
  160. package/coverage/lcov-report/utilities/global.js.html +49 -58
  161. package/coverage/lcov-report/utilities/index.html +37 -37
  162. package/coverage/lcov-report/utilities/index.js.html +73 -43
  163. package/coverage/lcov-report/utilities/queue/inMemoryQueue.js.html +8 -8
  164. package/coverage/lcov-report/utilities/queue/index.html +1 -1
  165. package/coverage/lcov-report/utilities/routing/index.html +1 -1
  166. package/coverage/lcov-report/utilities/routing/index.js.html +7 -7
  167. package/coverage/lcov-report/utilities/rowProcessor/index.html +27 -27
  168. package/coverage/lcov-report/utilities/rowProcessor/index.js.html +103 -97
  169. package/coverage/lcov-report/utilities/rowProcessor/utils.js.html +27 -75
  170. package/coverage/lcov-report/utilities/scriptRunner.js.html +5 -5
  171. package/coverage/lcov-report/utilities/security.js.html +8 -8
  172. package/coverage/lcov-report/utilities/statusCodes.js.html +1 -1
  173. package/coverage/lcov-report/utilities/users.js.html +9 -9
  174. package/coverage/lcov-report/utilities/workerRequests.js.html +112 -43
  175. package/coverage/lcov.info +4423 -5022
  176. package/dist/api/controllers/application.js +60 -90
  177. package/dist/api/controllers/auth.js +5 -8
  178. package/dist/api/controllers/automation.js +19 -14
  179. package/dist/api/controllers/cloud.js +3 -2
  180. package/dist/api/controllers/component.js +4 -3
  181. package/dist/api/controllers/datasource.js +10 -20
  182. package/dist/api/controllers/deploy/Deployment.js +6 -3
  183. package/dist/api/controllers/deploy/index.js +13 -12
  184. package/dist/api/controllers/dev.js +5 -5
  185. package/dist/api/controllers/hosting.js +30 -0
  186. package/dist/api/controllers/layout.js +3 -3
  187. package/dist/api/controllers/metadata.js +4 -4
  188. package/dist/api/controllers/permission.js +4 -4
  189. package/dist/api/controllers/query/import/index.js +6 -4
  190. package/dist/api/controllers/query/index.js +10 -11
  191. package/dist/api/controllers/role.js +5 -5
  192. package/dist/api/controllers/routing.js +6 -5
  193. package/dist/api/controllers/row/ExternalRequest.js +63 -76
  194. package/dist/api/controllers/row/external.js +4 -4
  195. package/dist/api/controllers/row/internal.js +93 -55
  196. package/dist/api/controllers/row/internalSearch.js +17 -23
  197. package/dist/api/controllers/row/utils.js +14 -13
  198. package/dist/api/controllers/screen.js +7 -5
  199. package/dist/api/controllers/static/index.js +12 -61
  200. package/dist/api/controllers/table/external.js +10 -7
  201. package/dist/api/controllers/table/index.js +13 -16
  202. package/dist/api/controllers/table/internal.js +16 -22
  203. package/dist/api/controllers/table/utils.js +35 -62
  204. package/dist/api/controllers/user.js +20 -18
  205. package/dist/api/controllers/view/index.js +10 -8
  206. package/dist/api/controllers/view/utils.js +4 -9
  207. package/dist/api/controllers/webhook.js +8 -10
  208. package/dist/api/routes/hosting.js +11 -0
  209. package/dist/api/routes/index.js +2 -2
  210. package/dist/api/routes/static.js +1 -2
  211. package/dist/api/routes/tests/utilities/TestFunctions.js +17 -37
  212. package/dist/app.js +5 -35
  213. package/dist/automations/automationUtils.js +3 -2
  214. package/dist/automations/steps/createRow.js +5 -3
  215. package/dist/automations/steps/deleteRow.js +4 -1
  216. package/dist/automations/steps/updateRow.js +1 -1
  217. package/dist/automations/triggers.js +25 -27
  218. package/dist/automations/utils.js +5 -10
  219. package/dist/constants/index.js +0 -7
  220. package/dist/db/linkedRows/LinkController.js +7 -3
  221. package/dist/db/linkedRows/index.js +27 -16
  222. package/dist/db/linkedRows/linkUtils.js +6 -6
  223. package/dist/db/views/staticViews.js +10 -9
  224. package/dist/definitions/datasource.js +0 -1
  225. package/dist/environment.js +1 -4
  226. package/dist/integrations/base/sql.js +6 -13
  227. package/dist/integrations/index.js +0 -3
  228. package/dist/integrations/utils.js +9 -60
  229. package/dist/middleware/authorized.js +30 -57
  230. package/dist/middleware/builder.js +2 -2
  231. package/dist/middleware/currentapp.js +37 -41
  232. package/dist/middleware/usageQuota.js +15 -82
  233. package/dist/package.json +5 -8
  234. package/dist/tests/utilities/TestConfiguration.js +50 -80
  235. package/dist/threads/automation.js +11 -13
  236. package/dist/threads/query.js +13 -23
  237. package/dist/tsconfig.tsbuildinfo +1 -1
  238. package/dist/utilities/fileSystem/index.js +3 -4
  239. package/dist/utilities/global.js +12 -15
  240. package/dist/utilities/index.js +17 -7
  241. package/dist/utilities/routing/index.js +5 -5
  242. package/dist/utilities/rowProcessor/index.js +17 -23
  243. package/dist/utilities/rowProcessor/utils.js +4 -14
  244. package/dist/utilities/{usageQuota/index.js → usageQuota.js} +31 -45
  245. package/dist/utilities/users.js +3 -3
  246. package/dist/utilities/workerRequests.js +24 -4
  247. package/nodemon.json +1 -1
  248. package/package.json +6 -9
  249. package/scripts/likeCypress.ts +1 -1
  250. package/src/api/controllers/application.js +50 -85
  251. package/src/api/controllers/auth.js +5 -8
  252. package/src/api/controllers/automation.js +23 -13
  253. package/src/api/controllers/cloud.js +3 -2
  254. package/src/api/controllers/component.js +4 -3
  255. package/src/api/controllers/datasource.js +11 -21
  256. package/src/api/controllers/deploy/Deployment.js +7 -3
  257. package/src/api/controllers/deploy/index.js +13 -20
  258. package/src/api/controllers/dev.js +5 -5
  259. package/src/api/controllers/hosting.js +22 -0
  260. package/src/api/controllers/layout.js +3 -3
  261. package/src/api/controllers/metadata.js +9 -4
  262. package/src/api/controllers/permission.js +4 -4
  263. package/src/api/controllers/query/import/index.ts +6 -4
  264. package/src/api/controllers/query/import/tests/index.spec.js +1 -2
  265. package/src/api/controllers/query/index.js +10 -11
  266. package/src/api/controllers/role.js +5 -5
  267. package/src/api/controllers/routing.js +6 -5
  268. package/src/api/controllers/row/ExternalRequest.ts +42 -53
  269. package/src/api/controllers/row/external.js +6 -4
  270. package/src/api/controllers/row/internal.js +90 -54
  271. package/src/api/controllers/row/internalSearch.js +17 -23
  272. package/src/api/controllers/row/utils.js +18 -13
  273. package/src/api/controllers/screen.js +7 -5
  274. package/src/api/controllers/static/index.js +12 -65
  275. package/src/api/controllers/table/external.js +10 -7
  276. package/src/api/controllers/table/index.js +28 -20
  277. package/src/api/controllers/table/internal.js +19 -24
  278. package/src/api/controllers/table/utils.js +42 -78
  279. package/src/api/controllers/user.js +20 -19
  280. package/src/api/controllers/view/index.js +10 -8
  281. package/src/api/controllers/view/utils.js +4 -9
  282. package/src/api/controllers/webhook.js +8 -10
  283. package/src/api/routes/hosting.js +13 -0
  284. package/src/api/routes/index.js +2 -2
  285. package/src/api/routes/static.js +0 -5
  286. package/src/api/routes/tests/application.spec.js +2 -2
  287. package/src/api/routes/tests/auth.spec.js +2 -1
  288. package/src/api/routes/tests/automation.spec.js +0 -1
  289. package/src/api/routes/tests/hosting.spec.js +36 -0
  290. package/src/api/routes/tests/misc.spec.js +1 -0
  291. package/src/api/routes/tests/routing.spec.js +12 -21
  292. package/src/api/routes/tests/row.spec.js +4 -7
  293. package/src/api/routes/tests/utilities/TestFunctions.js +17 -39
  294. package/src/app.ts +5 -18
  295. package/src/automations/automationUtils.js +3 -2
  296. package/src/automations/steps/createRow.js +5 -2
  297. package/src/automations/steps/deleteRow.js +4 -1
  298. package/src/automations/steps/updateRow.js +1 -1
  299. package/src/automations/triggers.js +31 -33
  300. package/src/automations/utils.js +5 -9
  301. package/src/constants/index.js +0 -9
  302. package/src/db/linkedRows/LinkController.js +7 -3
  303. package/src/db/linkedRows/index.js +31 -16
  304. package/src/db/linkedRows/linkUtils.js +6 -6
  305. package/src/db/tests/linkController.spec.js +1 -0
  306. package/src/db/tests/linkTests.spec.js +6 -5
  307. package/src/db/views/staticViews.js +10 -8
  308. package/src/definitions/common.ts +0 -3
  309. package/src/definitions/datasource.ts +0 -3
  310. package/src/environment.js +1 -4
  311. package/src/integrations/base/sql.ts +7 -14
  312. package/src/integrations/index.ts +0 -3
  313. package/src/integrations/tests/sql.spec.js +0 -16
  314. package/src/integrations/utils.ts +12 -75
  315. package/src/middleware/authorized.js +30 -66
  316. package/src/middleware/builder.js +2 -2
  317. package/src/middleware/currentapp.js +46 -50
  318. package/src/middleware/tests/authorized.spec.js +6 -13
  319. package/src/middleware/tests/currentapp.spec.js +0 -6
  320. package/src/middleware/tests/usageQuota.spec.js +24 -18
  321. package/src/middleware/usageQuota.js +17 -93
  322. package/src/tests/utilities/TestConfiguration.js +55 -82
  323. package/src/threads/automation.js +15 -17
  324. package/src/threads/query.js +14 -26
  325. package/src/utilities/fileSystem/index.js +3 -4
  326. package/src/utilities/global.js +12 -15
  327. package/src/utilities/index.js +17 -7
  328. package/src/utilities/routing/index.js +5 -5
  329. package/src/utilities/rowProcessor/index.js +27 -25
  330. package/src/utilities/rowProcessor/utils.js +7 -23
  331. package/src/utilities/usageQuota.js +73 -0
  332. package/src/utilities/users.js +3 -3
  333. package/src/utilities/workerRequests.js +27 -4
  334. package/builder/assets/index.83c0944f.css +0 -1
  335. package/coverage/lcov-report/api/controllers/migrations.js.html +0 -119
  336. package/coverage/lcov-report/api/controllers/row/staticFormula.js.html +0 -548
  337. package/coverage/lcov-report/api/controllers/table/bulkFormula.js.html +0 -599
  338. package/coverage/lcov-report/utilities/usageQuota/index.html +0 -141
  339. package/coverage/lcov-report/utilities/usageQuota/index.js.html +0 -359
  340. package/coverage/lcov-report/utilities/usageQuota/rows.js.html +0 -305
  341. package/dist/api/controllers/migrations.js +0 -21
  342. package/dist/api/controllers/row/staticFormula.js +0 -147
  343. package/dist/api/controllers/table/bulkFormula.js +0 -159
  344. package/dist/api/routes/migrations.js +0 -9
  345. package/dist/integrations/googlesheets.js +0 -340
  346. package/dist/migrations/functions/appUrls.js +0 -46
  347. package/dist/migrations/functions/quotas1.js +0 -50
  348. package/dist/migrations/functions/usageQuotas/index.js +0 -20
  349. package/dist/migrations/functions/usageQuotas/syncApps.js +0 -30
  350. package/dist/migrations/functions/usageQuotas/syncRows.js +0 -33
  351. package/dist/migrations/functions/userEmailViewCasing.js +0 -24
  352. package/dist/migrations/index.js +0 -62
  353. package/dist/utilities/usageQuota/rows.js +0 -78
  354. package/dist/utilities/usageQuota/usageQuoteReset.js +0 -16
  355. package/src/api/controllers/migrations.js +0 -13
  356. package/src/api/controllers/row/staticFormula.js +0 -156
  357. package/src/api/controllers/table/bulkFormula.js +0 -173
  358. package/src/api/routes/migrations.js +0 -14
  359. package/src/api/routes/tests/static.spec.js +0 -98
  360. package/src/integrations/googlesheets.ts +0 -353
  361. package/src/migrations/functions/appUrls.ts +0 -34
  362. package/src/migrations/functions/quotas1.ts +0 -20
  363. package/src/migrations/functions/tests/appUrls.spec.js +0 -29
  364. package/src/migrations/functions/tests/quotas1.spec.js +0 -27
  365. package/src/migrations/functions/tests/userEmailViewCasing.spec.js +0 -25
  366. package/src/migrations/functions/usageQuotas/index.ts +0 -8
  367. package/src/migrations/functions/usageQuotas/syncApps.ts +0 -19
  368. package/src/migrations/functions/usageQuotas/syncRows.ts +0 -22
  369. package/src/migrations/functions/usageQuotas/tests/syncApps.spec.js +0 -37
  370. package/src/migrations/functions/usageQuotas/tests/syncRows.spec.js +0 -43
  371. package/src/migrations/functions/userEmailViewCasing.ts +0 -13
  372. package/src/migrations/index.ts +0 -56
  373. package/src/utilities/tests/usageQuota/usageQuota.spec.js +0 -72
  374. package/src/utilities/usageQuota/index.js +0 -93
  375. package/src/utilities/usageQuota/rows.js +0 -75
  376. package/src/utilities/usageQuota/usageQuoteReset.js +0 -18
@@ -1,15 +1,10 @@
1
1
  const setup = require("./utilities")
2
2
  const { basicScreen } = setup.structures
3
- const { checkBuilderEndpoint, runInProd } = require("./utilities/TestFunctions")
3
+ const { checkBuilderEndpoint } = require("./utilities/TestFunctions")
4
4
  const { BUILTIN_ROLE_IDS } = require("@budibase/backend-core/roles")
5
- const { doInAppContext } = require("@budibase/backend-core/context")
6
5
 
7
6
  const route = "/test"
8
7
 
9
- // there are checks which are disabled in test env,
10
- // these checks need to be enabled for this test
11
-
12
-
13
8
  describe("/routing", () => {
14
9
  let request = setup.getRequest()
15
10
  let config = setup.getConfig()
@@ -31,24 +26,20 @@ describe("/routing", () => {
31
26
 
32
27
  describe("fetch", () => {
33
28
  it("prevents a public user from accessing development app", async () => {
34
- await runInProd(() => {
35
- return request
36
- .get(`/api/routing/client`)
37
- .set(config.publicHeaders({ prodApp: false }))
38
- .expect(302)
39
- })
29
+ await request
30
+ .get(`/api/routing/client`)
31
+ .set(config.publicHeaders({ prodApp: false }))
32
+ .expect(302)
40
33
  })
41
34
 
42
35
  it("prevents a non builder from accessing development app", async () => {
43
- await runInProd(async () => {
44
- return request
45
- .get(`/api/routing/client`)
46
- .set(await config.roleHeaders({
47
- roleId: BUILTIN_ROLE_IDS.BASIC,
48
- prodApp: false
49
- }))
50
- .expect(302)
51
- })
36
+ await request
37
+ .get(`/api/routing/client`)
38
+ .set(await config.roleHeaders({
39
+ roleId: BUILTIN_ROLE_IDS.BASIC,
40
+ prodApp: false
41
+ }))
42
+ .expect(302)
52
43
  })
53
44
  it("returns the correct routing for basic user", async () => {
54
45
  const res = await request
@@ -1,7 +1,6 @@
1
1
  const { outputProcessing } = require("../../../utilities/rowProcessor")
2
2
  const setup = require("./utilities")
3
3
  const { basicRow } = setup.structures
4
- const { doInAppContext } = require("@budibase/backend-core/context")
5
4
 
6
5
  // mock the fetch for the search system
7
6
  jest.mock("node-fetch")
@@ -388,12 +387,10 @@ describe("/rows", () => {
388
387
  })
389
388
  // the environment needs configured for this
390
389
  await setup.switchToSelfHosted(async () => {
391
- doInAppContext(config.getAppId(), async () => {
392
- const enriched = await outputProcessing(table, [row])
393
- expect(enriched[0].attachment[0].url).toBe(
394
- `/prod-budi-app-assets/${config.getAppId()}/attachments/test/thing.csv`
395
- )
396
- })
390
+ const enriched = await outputProcessing({ appId: config.getAppId() }, table, [row])
391
+ expect(enriched[0].attachment[0].url).toBe(
392
+ `/prod-budi-app-assets/${config.getAppId()}/attachments/test/thing.csv`
393
+ )
397
394
  })
398
395
  })
399
396
  })
@@ -1,10 +1,9 @@
1
1
  const rowController = require("../../../controllers/row")
2
2
  const appController = require("../../../controllers/application")
3
+ const CouchDB = require("../../../../db")
3
4
  const { AppStatus } = require("../../../../db/utils")
4
5
  const { BUILTIN_ROLE_IDS } = require("@budibase/backend-core/roles")
5
6
  const { TENANT_ID } = require("../../../../tests/utilities/structures")
6
- const { getAppDB, doInAppContext } = require("@budibase/backend-core/context")
7
- const env = require("../../../../environment")
8
7
 
9
8
  function Request(appId, params) {
10
9
  this.appId = appId
@@ -12,15 +11,9 @@ function Request(appId, params) {
12
11
  this.request = {}
13
12
  }
14
13
 
15
- function runRequest(appId, controlFunc, request) {
16
- return doInAppContext(appId, async () => {
17
- return controlFunc(request)
18
- })
19
- }
20
-
21
14
  exports.getAllTableRows = async config => {
22
15
  const req = new Request(config.appId, { tableId: config.table._id })
23
- await runRequest(config.appId, rowController.fetch, req)
16
+ await rowController.fetch(req)
24
17
  return req.body
25
18
  }
26
19
 
@@ -33,17 +26,14 @@ exports.clearAllApps = async (tenantId = TENANT_ID) => {
33
26
  }
34
27
  for (let app of apps) {
35
28
  const { appId } = app
36
- const req = new Request(null, { appId })
37
- await runRequest(appId, appController.delete, req)
29
+ await appController.delete(new Request(null, { appId }))
38
30
  }
39
31
  }
40
32
 
41
33
  exports.clearAllAutomations = async config => {
42
34
  const automations = await config.getAllAutomations()
43
35
  for (let auto of automations) {
44
- await doInAppContext(config.appId, async () => {
45
- await config.deleteAutomation(auto)
46
- })
36
+ await config.deleteAutomation(auto)
47
37
  }
48
38
  }
49
39
 
@@ -106,32 +96,20 @@ exports.checkPermissionsEndpoint = async ({
106
96
  .expect(403)
107
97
  }
108
98
 
109
- exports.getDB = () => {
110
- return getAppDB()
99
+ exports.getDB = config => {
100
+ return new CouchDB(config.getAppId())
111
101
  }
112
102
 
113
103
  exports.testAutomation = async (config, automation) => {
114
- return runRequest(automation.appId, async () => {
115
- return await config.request
116
- .post(`/api/automations/${automation._id}/test`)
117
- .send({
118
- row: {
119
- name: "Test",
120
- description: "TEST",
121
- },
122
- })
123
- .set(config.defaultHeaders())
124
- .expect("Content-Type", /json/)
125
- .expect(200)
126
- })
127
- }
128
-
129
- exports.runInProd = async func => {
130
- const nodeEnv = env.NODE_ENV
131
- const workerId = env.JEST_WORKER_ID
132
- env._set("NODE_ENV", "PRODUCTION")
133
- env._set("JEST_WORKER_ID", null)
134
- await func()
135
- env._set("NODE_ENV", nodeEnv)
136
- env._set("JEST_WORKER_ID", workerId)
104
+ return await config.request
105
+ .post(`/api/automations/${automation._id}/test`)
106
+ .send({
107
+ row: {
108
+ name: "Test",
109
+ description: "TEST",
110
+ },
111
+ })
112
+ .set(config.defaultHeaders())
113
+ .expect("Content-Type", /json/)
114
+ .expect(200)
137
115
  }
package/src/app.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  // need to load environment first
2
2
  import { ExtendableContext } from "koa"
3
3
 
4
- import * as env from "./environment"
4
+ const env = require("./environment")
5
5
  const CouchDB = require("./db")
6
6
  require("@budibase/backend-core").init(CouchDB)
7
7
  const Koa = require("koa")
@@ -16,7 +16,6 @@ const Sentry = require("@sentry/node")
16
16
  const fileSystem = require("./utilities/fileSystem")
17
17
  const bullboard = require("./automations/bullboard")
18
18
  const redis = require("./utilities/redis")
19
- import * as migrations from "./migrations"
20
19
 
21
20
  const app = new Koa()
22
21
 
@@ -85,25 +84,13 @@ module.exports = server.listen(env.PORT || 0, async () => {
85
84
  await automations.init()
86
85
  })
87
86
 
88
- const shutdown = () => {
89
- server.close()
90
- server.destroy()
91
- }
92
-
93
87
  process.on("uncaughtException", err => {
94
88
  console.error(err)
95
- shutdown()
89
+ server.close()
90
+ server.destroy()
96
91
  })
97
92
 
98
93
  process.on("SIGTERM", () => {
99
- shutdown()
94
+ server.close()
95
+ server.destroy()
100
96
  })
101
-
102
- // run migrations on startup if not done via http
103
- // not recommended in a clustered environment
104
- if (!env.HTTP_MIGRATIONS) {
105
- migrations.migrate().catch(err => {
106
- console.error("Error performing migrations. Exiting.\n", err)
107
- shutdown()
108
- })
109
- }
@@ -53,12 +53,13 @@ exports.cleanInputValues = (inputs, schema) => {
53
53
  * the automation but is instead part of the Table/Table. This function will get the table schema and use it to instead
54
54
  * perform the cleanInputValues function on the input row.
55
55
  *
56
+ * @param {string} appId The instance which the Table/Table is contained under.
56
57
  * @param {string} tableId The ID of the Table/Table which the schema is to be retrieved for.
57
58
  * @param {object} row The input row structure which requires clean-up after having been through template statements.
58
59
  * @returns {Promise<Object>} The cleaned up rows object, will should now have all the required primitive types.
59
60
  */
60
- exports.cleanUpRow = async (tableId, row) => {
61
- let table = await getTable(tableId)
61
+ exports.cleanUpRow = async (appId, tableId, row) => {
62
+ let table = await getTable(appId, tableId)
62
63
  return exports.cleanInputValues(row, { properties: table.schema })
63
64
  }
64
65
 
@@ -1,5 +1,6 @@
1
1
  const rowController = require("../../api/controllers/row")
2
2
  const automationUtils = require("../automationUtils")
3
+ const env = require("../../environment")
3
4
  const usage = require("../../utilities/usageQuota")
4
5
  const { buildCtx } = require("./utils")
5
6
 
@@ -78,12 +79,14 @@ exports.run = async function ({ inputs, appId, emitter }) {
78
79
 
79
80
  try {
80
81
  inputs.row = await automationUtils.cleanUpRow(
82
+ appId,
81
83
  inputs.row.tableId,
82
84
  inputs.row
83
85
  )
84
- await usage.update(usage.Properties.ROW, 1, { dryRun: true })
86
+ if (env.USE_QUOTAS) {
87
+ await usage.update(usage.Properties.ROW, 1)
88
+ }
85
89
  await rowController.save(ctx)
86
- await usage.update(usage.Properties.ROW, 1)
87
90
  return {
88
91
  row: inputs.row,
89
92
  response: ctx.body,
@@ -1,4 +1,5 @@
1
1
  const rowController = require("../../api/controllers/row")
2
+ const env = require("../../environment")
2
3
  const usage = require("../../utilities/usageQuota")
3
4
  const { buildCtx } = require("./utils")
4
5
  const automationUtils = require("../automationUtils")
@@ -73,7 +74,9 @@ exports.run = async function ({ inputs, appId, emitter }) {
73
74
  })
74
75
 
75
76
  try {
76
- await usage.update(usage.Properties.ROW, -1)
77
+ if (env.isProd()) {
78
+ await usage.update(usage.Properties.ROW, -1)
79
+ }
77
80
  await rowController.destroy(ctx)
78
81
  return {
79
82
  response: ctx.body,
@@ -87,7 +87,7 @@ exports.run = async function ({ inputs, appId, emitter }) {
87
87
 
88
88
  try {
89
89
  if (tableId) {
90
- inputs.row = await automationUtils.cleanUpRow(tableId, inputs.row)
90
+ inputs.row = await automationUtils.cleanUpRow(appId, tableId, inputs.row)
91
91
  }
92
92
  await rowController.patch(ctx)
93
93
  return {
@@ -1,3 +1,4 @@
1
+ const CouchDB = require("../db")
1
2
  const emitter = require("../events/index")
2
3
  const { getAutomationParams } = require("../db/utils")
3
4
  const { coerce } = require("../utilities/rowProcessor")
@@ -8,7 +9,6 @@ const { queue } = require("./bullboard")
8
9
  const { checkTestFlag } = require("../utilities/redis")
9
10
  const utils = require("./utils")
10
11
  const env = require("../environment")
11
- const { doInAppContext, getAppDB } = require("@budibase/backend-core/context")
12
12
 
13
13
  const TRIGGER_DEFINITIONS = definitions
14
14
  const JOB_OPTS = {
@@ -21,41 +21,39 @@ async function queueRelevantRowAutomations(event, eventType) {
21
21
  throw `No appId specified for ${eventType} - check event emitters.`
22
22
  }
23
23
 
24
- doInAppContext(event.appId, async () => {
25
- const db = getAppDB()
26
- let automations = await db.allDocs(
27
- getAutomationParams(null, { include_docs: true })
28
- )
24
+ const db = new CouchDB(event.appId)
25
+ let automations = await db.allDocs(
26
+ getAutomationParams(null, { include_docs: true })
27
+ )
29
28
 
30
- // filter down to the correct event type
31
- automations = automations.rows
32
- .map(automation => automation.doc)
33
- .filter(automation => {
34
- const trigger = automation.definition.trigger
35
- return trigger && trigger.event === eventType
36
- })
29
+ // filter down to the correct event type
30
+ automations = automations.rows
31
+ .map(automation => automation.doc)
32
+ .filter(automation => {
33
+ const trigger = automation.definition.trigger
34
+ return trigger && trigger.event === eventType
35
+ })
37
36
 
38
- for (let automation of automations) {
39
- let automationDef = automation.definition
40
- let automationTrigger = automationDef ? automationDef.trigger : {}
41
- // don't queue events which are for dev apps, only way to test automations is
42
- // running tests on them, in production the test flag will never
43
- // be checked due to lazy evaluation (first always false)
44
- if (
45
- !env.ALLOW_DEV_AUTOMATIONS &&
46
- isDevAppID(event.appId) &&
47
- !(await checkTestFlag(automation._id))
48
- ) {
49
- continue
50
- }
51
- if (
52
- automationTrigger.inputs &&
53
- automationTrigger.inputs.tableId === event.row.tableId
54
- ) {
55
- await queue.add({ automation, event }, JOB_OPTS)
56
- }
37
+ for (let automation of automations) {
38
+ let automationDef = automation.definition
39
+ let automationTrigger = automationDef ? automationDef.trigger : {}
40
+ // don't queue events which are for dev apps, only way to test automations is
41
+ // running tests on them, in production the test flag will never
42
+ // be checked due to lazy evaluation (first always false)
43
+ if (
44
+ !env.ALLOW_DEV_AUTOMATIONS &&
45
+ isDevAppID(event.appId) &&
46
+ !(await checkTestFlag(automation._id))
47
+ ) {
48
+ continue
57
49
  }
58
- })
50
+ if (
51
+ automationTrigger.inputs &&
52
+ automationTrigger.inputs.tableId === event.row.tableId
53
+ ) {
54
+ await queue.add({ automation, event }, JOB_OPTS)
55
+ }
56
+ }
59
57
  }
60
58
 
61
59
  emitter.on("row:save", async function (event) {
@@ -6,9 +6,8 @@ const { queue } = require("./bullboard")
6
6
  const newid = require("../db/newid")
7
7
  const { updateEntityMetadata } = require("../utilities")
8
8
  const { MetadataTypes } = require("../constants")
9
- const { getProdAppID } = require("@budibase/backend-core/db")
9
+ const { getDeployedAppID } = require("@budibase/backend-core/db")
10
10
  const { cloneDeep } = require("lodash/fp")
11
- const { getAppDB, getAppId } = require("@budibase/backend-core/context")
12
11
 
13
12
  const WH_STEP_ID = definitions.WEBHOOK.stepId
14
13
  const CRON_STEP_ID = definitions.CRON.stepId
@@ -28,6 +27,7 @@ exports.processEvent = async job => {
28
27
 
29
28
  exports.updateTestHistory = async (appId, automation, history) => {
30
29
  return updateEntityMetadata(
30
+ appId,
31
31
  MetadataTypes.AUTOMATION_TEST_HISTORY,
32
32
  automation._id,
33
33
  metadata => {
@@ -93,9 +93,6 @@ exports.enableCronTrigger = async (appId, automation) => {
93
93
  )
94
94
  // Assign cron job ID from bull so we can remove it later if the cron trigger is removed
95
95
  trigger.cronJobId = job.id
96
- // can't use getAppDB here as this is likely to be called from dev app,
97
- // but this call could be for dev app or prod app, need to just use what
98
- // was passed in
99
96
  const db = new CouchDB(appId)
100
97
  const response = await db.put(automation)
101
98
  automation._id = response.id
@@ -112,8 +109,7 @@ exports.enableCronTrigger = async (appId, automation) => {
112
109
  * @returns {Promise<object|undefined>} After this is complete the new automation object may have been updated and should be
113
110
  * written to DB (this does not write to DB as it would be wasteful to repeat).
114
111
  */
115
- exports.checkForWebhooks = async ({ oldAuto, newAuto }) => {
116
- const appId = getAppId()
112
+ exports.checkForWebhooks = async ({ appId, oldAuto, newAuto }) => {
117
113
  const oldTrigger = oldAuto ? oldAuto.definition.trigger : null
118
114
  const newTrigger = newAuto ? newAuto.definition.trigger : null
119
115
  const triggerChanged =
@@ -132,7 +128,7 @@ exports.checkForWebhooks = async ({ oldAuto, newAuto }) => {
132
128
  oldTrigger.webhookId
133
129
  ) {
134
130
  try {
135
- let db = getAppDB()
131
+ let db = new CouchDB(appId)
136
132
  // need to get the webhook to get the rev
137
133
  const webhook = await db.get(oldTrigger.webhookId)
138
134
  const ctx = {
@@ -170,7 +166,7 @@ exports.checkForWebhooks = async ({ oldAuto, newAuto }) => {
170
166
  // the app ID has to be development for this endpoint
171
167
  // it can only be used when building the app
172
168
  // but the trigger endpoint will always be used in production
173
- const prodAppId = getProdAppID(appId)
169
+ const prodAppId = getDeployedAppID(appId)
174
170
  newTrigger.inputs = {
175
171
  schemaUrl: `api/webhooks/schema/${appId}/${id}`,
176
172
  triggerUrl: `api/webhooks/trigger/${prodAppId}/${id}`,
@@ -61,11 +61,6 @@ exports.RelationshipTypes = {
61
61
  MANY_TO_MANY: "many-to-many",
62
62
  }
63
63
 
64
- exports.FormulaTypes = {
65
- STATIC: "static",
66
- DYNAMIC: "dynamic",
67
- }
68
-
69
64
  exports.AuthTypes = {
70
65
  APP: "app",
71
66
  BUILDER: "builder",
@@ -83,10 +78,6 @@ exports.DataSourceOperation = {
83
78
  DELETE_TABLE: "DELETE_TABLE",
84
79
  }
85
80
 
86
- exports.DatasourceAuthTypes = {
87
- GOOGLE: "google",
88
- }
89
-
90
81
  exports.SortDirection = {
91
82
  ASCENDING: "ASCENDING",
92
83
  DESCENDING: "DESCENDING",
@@ -1,3 +1,4 @@
1
+ const CouchDB = require("../index")
1
2
  const { IncludeDocs, getLinkDocuments } = require("./linkUtils")
2
3
  const {
3
4
  generateLinkID,
@@ -6,7 +7,6 @@ const {
6
7
  } = require("../utils")
7
8
  const Sentry = require("@sentry/node")
8
9
  const { FieldTypes, RelationshipTypes } = require("../../constants")
9
- const { getAppDB } = require("@budibase/backend-core/context")
10
10
 
11
11
  /**
12
12
  * Creates a new link document structure which can be put to the database. It is important to
@@ -52,8 +52,9 @@ function LinkDocument(
52
52
  }
53
53
 
54
54
  class LinkController {
55
- constructor({ tableId, row, table, oldTable }) {
56
- this._db = getAppDB()
55
+ constructor({ appId, tableId, row, table, oldTable }) {
56
+ this._appId = appId
57
+ this._db = new CouchDB(appId)
57
58
  this._tableId = tableId
58
59
  this._row = row
59
60
  this._table = table
@@ -98,6 +99,7 @@ class LinkController {
98
99
  */
99
100
  getRowLinkDocs(rowId) {
100
101
  return getLinkDocuments({
102
+ appId: this._appId,
101
103
  tableId: this._tableId,
102
104
  rowId,
103
105
  includeDocs: IncludeDocs.INCLUDE,
@@ -109,6 +111,7 @@ class LinkController {
109
111
  */
110
112
  getTableLinkDocs() {
111
113
  return getLinkDocuments({
114
+ appId: this._appId,
112
115
  tableId: this._tableId,
113
116
  includeDocs: IncludeDocs.INCLUDE,
114
117
  })
@@ -227,6 +230,7 @@ class LinkController {
227
230
  if (linkedSchema.relationshipType === RelationshipTypes.ONE_TO_MANY) {
228
231
  let links = (
229
232
  await getLinkDocuments({
233
+ appId: this._appId,
230
234
  tableId: field.tableId,
231
235
  rowId: linkId,
232
236
  includeDocs: IncludeDocs.EXCLUDE,
@@ -9,12 +9,12 @@ const {
9
9
  getLinkedTable,
10
10
  } = require("./linkUtils")
11
11
  const { flatten } = require("lodash")
12
+ const CouchDB = require("../../db")
12
13
  const { FieldTypes } = require("../../constants")
13
14
  const { getMultiIDParams, USER_METDATA_PREFIX } = require("../../db/utils")
14
15
  const { partition } = require("lodash")
15
16
  const { getGlobalUsersFromMetadata } = require("../../utilities/global")
16
17
  const { processFormulas } = require("../../utilities/rowProcessor/utils")
17
- const { getAppDB } = require("@budibase/backend-core/context")
18
18
 
19
19
  /**
20
20
  * This functionality makes sure that when rows with links are created, updated or deleted they are processed
@@ -48,13 +48,14 @@ function clearRelationshipFields(table, rows) {
48
48
  return rows
49
49
  }
50
50
 
51
- async function getLinksForRows(rows) {
51
+ async function getLinksForRows(appId, rows) {
52
52
  const tableIds = [...new Set(rows.map(el => el.tableId))]
53
53
  // start by getting all the link values for performance reasons
54
54
  const responses = flatten(
55
55
  await Promise.all(
56
56
  tableIds.map(tableId =>
57
57
  getLinkDocuments({
58
+ appId,
58
59
  tableId: tableId,
59
60
  includeDocs: IncludeDocs.EXCLUDE,
60
61
  })
@@ -71,9 +72,9 @@ async function getLinksForRows(rows) {
71
72
  )
72
73
  }
73
74
 
74
- async function getFullLinkedDocs(links) {
75
+ async function getFullLinkedDocs(ctx, appId, links) {
75
76
  // create DBs
76
- const db = getAppDB()
77
+ const db = new CouchDB(appId)
77
78
  const linkedRowIds = links.map(link => link.id)
78
79
  const uniqueRowIds = [...new Set(linkedRowIds)]
79
80
  let dbRows = (await db.allDocs(getMultiIDParams(uniqueRowIds))).rows.map(
@@ -87,7 +88,7 @@ async function getFullLinkedDocs(links) {
87
88
  let [users, other] = partition(linked, linkRow =>
88
89
  linkRow._id.startsWith(USER_METDATA_PREFIX)
89
90
  )
90
- users = await getGlobalUsersFromMetadata(users)
91
+ users = await getGlobalUsersFromMetadata(appId, users)
91
92
  return [...other, ...users]
92
93
  }
93
94
 
@@ -95,16 +96,20 @@ async function getFullLinkedDocs(links) {
95
96
  * Update link documents for a row or table - this is to be called by the API controller when a change is occurring.
96
97
  * @param {string} args.eventType states what type of change which is occurring, means this can be expanded upon in the
97
98
  * future quite easily (all updates go through one function).
99
+ * @param {string} args.appId The ID of the instance in which the change is occurring.
98
100
  * @param {string} args.tableId The ID of the of the table which is being changed.
99
- * @param {object|undefined} args.row The row which is changing, e.g. created, updated or deleted.
100
- * @param {object|undefined} args.table If the table has already been retrieved this can be used to reduce database gets.
101
- * @param {object|undefined} args.oldTable If the table is being updated then the old table can be provided for differencing.
101
+ * @param {object|null} args.row The row which is changing, e.g. created, updated or deleted.
102
+ * @param {object|null} args.table If the table has already been retrieved this can be used to reduce database gets.
103
+ * @param {object|null} args.oldTable If the table is being updated then the old table can be provided for differencing.
102
104
  * @returns {Promise<object>} When the update is complete this will respond successfully. Returns the row for
103
105
  * row operations and the table for table operations.
104
106
  */
105
107
  exports.updateLinks = async function (args) {
106
- const { eventType, row, tableId, table, oldTable } = args
108
+ const { eventType, appId, row, tableId, table, oldTable } = args
107
109
  const baseReturnObj = row == null ? table : row
110
+ if (appId == null) {
111
+ throw "Cannot operate without an instance ID."
112
+ }
108
113
  // make sure table ID is set
109
114
  if (tableId == null && table != null) {
110
115
  args.tableId = table._id
@@ -141,23 +146,27 @@ exports.updateLinks = async function (args) {
141
146
  /**
142
147
  * Given a table and a list of rows this will retrieve all of the attached docs and enrich them into the row.
143
148
  * This is required for formula fields, this may only be utilised internally (for now).
149
+ * @param {object} ctx The request which is looking for rows.
144
150
  * @param {object} table The table from which the rows originated.
145
151
  * @param {array<object>} rows The rows which are to be enriched.
146
152
  * @return {Promise<*>} returns the rows with all of the enriched relationships on it.
147
153
  */
148
- exports.attachFullLinkedDocs = async (table, rows) => {
154
+ exports.attachFullLinkedDocs = async (ctx, table, rows) => {
155
+ const appId = ctx.appId
149
156
  const linkedTableIds = getLinkedTableIDs(table)
150
157
  if (linkedTableIds.length === 0) {
151
158
  return rows
152
159
  }
160
+ // create DBs
161
+ const db = new CouchDB(appId)
153
162
  // get all the links
154
- const links = (await getLinksForRows(rows)).filter(link =>
163
+ const links = (await getLinksForRows(appId, rows)).filter(link =>
155
164
  rows.some(row => row._id === link.thisId)
156
165
  )
157
166
  // clear any existing links that could be dupe'd
158
167
  rows = clearRelationshipFields(table, rows)
159
168
  // now get the docs and combine into the rows
160
- let linked = await getFullLinkedDocs(links)
169
+ let linked = await getFullLinkedDocs(ctx, appId, links)
161
170
  const linkedTables = []
162
171
  for (let row of rows) {
163
172
  for (let link of links.filter(link => link.thisId === row._id)) {
@@ -168,7 +177,11 @@ exports.attachFullLinkedDocs = async (table, rows) => {
168
177
  if (linkedRow) {
169
178
  const linkedTableId =
170
179
  linkedRow.tableId || getRelatedTableForField(table, link.fieldName)
171
- const linkedTable = await getLinkedTable(linkedTableId, linkedTables)
180
+ const linkedTable = await getLinkedTable(
181
+ db,
182
+ linkedTableId,
183
+ linkedTables
184
+ )
172
185
  if (linkedTable) {
173
186
  row[link.fieldName].push(processFormulas(linkedTable, linkedRow))
174
187
  }
@@ -180,16 +193,18 @@ exports.attachFullLinkedDocs = async (table, rows) => {
180
193
 
181
194
  /**
182
195
  * This function will take the given enriched rows and squash the links to only contain the primary display field.
196
+ * @param {string} appId The app in which the tables/rows/links exist.
183
197
  * @param {object} table The table from which the rows originated.
184
198
  * @param {array<object>} enriched The pre-enriched rows (full docs) which are to be squashed.
185
199
  * @returns {Promise<Array>} The rows after having their links squashed to only contain the ID and primary display.
186
200
  */
187
- exports.squashLinksToPrimaryDisplay = async (table, enriched) => {
201
+ exports.squashLinksToPrimaryDisplay = async (appId, table, enriched) => {
202
+ const db = new CouchDB(appId)
188
203
  // will populate this as we find them
189
204
  const linkedTables = [table]
190
205
  for (let row of enriched) {
191
206
  // this only fetches the table if its not already in array
192
- const rowTable = await getLinkedTable(row.tableId, linkedTables)
207
+ const rowTable = await getLinkedTable(db, row.tableId, linkedTables)
193
208
  for (let [column, schema] of Object.entries(rowTable.schema)) {
194
209
  if (schema.type !== FieldTypes.LINK || !Array.isArray(row[column])) {
195
210
  continue
@@ -197,7 +212,7 @@ exports.squashLinksToPrimaryDisplay = async (table, enriched) => {
197
212
  const newLinks = []
198
213
  for (let link of row[column]) {
199
214
  const linkTblId = link.tableId || getRelatedTableForField(table, column)
200
- const linkedTable = await getLinkedTable(linkTblId, linkedTables)
215
+ const linkedTable = await getLinkedTable(db, linkTblId, linkedTables)
201
216
  const obj = { _id: link._id }
202
217
  if (link[linkedTable.primaryDisplay]) {
203
218
  obj.primaryDisplay = link[linkedTable.primaryDisplay]