@budibase/server 1.0.49-alpha.1 → 1.0.49-alpha.13

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 (353) hide show
  1. package/__mocks__/arangojs.ts +1 -1
  2. package/__mocks__/aws-sdk.ts +4 -4
  3. package/__mocks__/mongodb.ts +4 -5
  4. package/__mocks__/mssql.ts +3 -3
  5. package/__mocks__/node-fetch.ts +2 -1
  6. package/__mocks__/pg.ts +3 -5
  7. package/builder/assets/{index.ba78c49d.css → index.83c0944f.css} +1 -1
  8. package/builder/assets/{index.ed1cf508.js → index.f686d3cd.js} +184 -184
  9. package/builder/index.html +2 -2
  10. package/coverage/clover.xml +2656 -2712
  11. package/coverage/coverage-final.json +104 -105
  12. package/coverage/lcov-report/api/controllers/analytics.js.html +1 -1
  13. package/coverage/lcov-report/api/controllers/apikeys.js.html +1 -1
  14. package/coverage/lcov-report/api/controllers/application.js.html +157 -136
  15. package/coverage/lcov-report/api/controllers/auth.js.html +11 -14
  16. package/coverage/lcov-report/api/controllers/automation.js.html +39 -69
  17. package/coverage/lcov-report/api/controllers/backup.js.html +1 -1
  18. package/coverage/lcov-report/api/controllers/cloud.js.html +8 -11
  19. package/coverage/lcov-report/api/controllers/component.js.html +10 -13
  20. package/coverage/lcov-report/api/controllers/datasource.js.html +31 -37
  21. package/coverage/lcov-report/api/controllers/dev.js.html +6 -6
  22. package/coverage/lcov-report/api/controllers/index.html +69 -54
  23. package/coverage/lcov-report/api/controllers/integration.js.html +1 -1
  24. package/coverage/lcov-report/api/controllers/layout.js.html +9 -9
  25. package/coverage/lcov-report/api/controllers/metadata.js.html +6 -21
  26. package/coverage/lcov-report/{migrations/usageQuotas/syncApps.js.html → api/controllers/migrations.js.html} +30 -45
  27. package/coverage/lcov-report/api/controllers/permission.js.html +19 -19
  28. package/coverage/lcov-report/api/controllers/query/index.html +10 -10
  29. package/coverage/lcov-report/api/controllers/query/index.js.html +38 -35
  30. package/coverage/lcov-report/api/controllers/query/validation.js.html +6 -6
  31. package/coverage/lcov-report/api/controllers/role.js.html +16 -16
  32. package/coverage/lcov-report/api/controllers/routing.js.html +7 -10
  33. package/coverage/lcov-report/api/controllers/row/external.js.html +48 -141
  34. package/coverage/lcov-report/api/controllers/row/index.html +28 -28
  35. package/coverage/lcov-report/api/controllers/row/index.js.html +13 -13
  36. package/coverage/lcov-report/api/controllers/row/internal.js.html +60 -105
  37. package/coverage/lcov-report/api/controllers/row/internalSearch.js.html +25 -31
  38. package/coverage/lcov-report/api/controllers/row/staticFormula.js.html +21 -24
  39. package/coverage/lcov-report/api/controllers/row/utils.js.html +27 -24
  40. package/coverage/lcov-report/api/controllers/screen.js.html +15 -21
  41. package/coverage/lcov-report/api/controllers/script.js.html +4 -4
  42. package/coverage/lcov-report/api/controllers/table/bulkFormula.js.html +27 -57
  43. package/coverage/lcov-report/api/controllers/table/external.js.html +25 -34
  44. package/coverage/lcov-report/api/controllers/table/index.html +37 -37
  45. package/coverage/lcov-report/api/controllers/table/index.js.html +20 -20
  46. package/coverage/lcov-report/api/controllers/table/internal.js.html +45 -54
  47. package/coverage/lcov-report/api/controllers/table/utils.js.html +67 -103
  48. package/coverage/lcov-report/api/controllers/templates.js.html +1 -1
  49. package/coverage/lcov-report/api/controllers/user.js.html +43 -52
  50. package/coverage/lcov-report/api/controllers/view/exporters.js.html +4 -4
  51. package/coverage/lcov-report/api/controllers/view/index.html +16 -16
  52. package/coverage/lcov-report/api/controllers/view/index.js.html +27 -33
  53. package/coverage/lcov-report/api/controllers/view/utils.js.html +35 -20
  54. package/coverage/lcov-report/api/controllers/view/viewBuilder.js.html +7 -7
  55. package/coverage/lcov-report/api/controllers/webhook.js.html +34 -28
  56. package/coverage/lcov-report/api/index.html +1 -1
  57. package/coverage/lcov-report/api/index.js.html +3 -3
  58. package/coverage/lcov-report/api/routes/analytics.js.html +1 -1
  59. package/coverage/lcov-report/api/routes/apikeys.js.html +1 -1
  60. package/coverage/lcov-report/api/routes/application.js.html +1 -1
  61. package/coverage/lcov-report/api/routes/auth.js.html +1 -1
  62. package/coverage/lcov-report/api/routes/automation.js.html +1 -1
  63. package/coverage/lcov-report/api/routes/backup.js.html +1 -1
  64. package/coverage/lcov-report/api/routes/cloud.js.html +1 -1
  65. package/coverage/lcov-report/api/routes/component.js.html +1 -1
  66. package/coverage/lcov-report/api/routes/datasource.js.html +1 -1
  67. package/coverage/lcov-report/api/routes/deploy.js.html +1 -1
  68. package/coverage/lcov-report/api/routes/dev.js.html +1 -1
  69. package/coverage/lcov-report/api/routes/index.html +22 -7
  70. package/coverage/lcov-report/api/routes/index.js.html +10 -4
  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/{migrations/usageQuotas/index.js.html → api/routes/migrations.js.html} +25 -55
  75. package/coverage/lcov-report/api/routes/permission.js.html +1 -1
  76. package/coverage/lcov-report/api/routes/query.js.html +1 -1
  77. package/coverage/lcov-report/api/routes/role.js.html +1 -1
  78. package/coverage/lcov-report/api/routes/routing.js.html +1 -1
  79. package/coverage/lcov-report/api/routes/row.js.html +1 -1
  80. package/coverage/lcov-report/api/routes/screen.js.html +1 -1
  81. package/coverage/lcov-report/api/routes/script.js.html +1 -1
  82. package/coverage/lcov-report/api/routes/static.js.html +1 -1
  83. package/coverage/lcov-report/api/routes/table.js.html +1 -1
  84. package/coverage/lcov-report/api/routes/templates.js.html +1 -1
  85. package/coverage/lcov-report/api/routes/user.js.html +1 -1
  86. package/coverage/lcov-report/api/routes/view.js.html +1 -1
  87. package/coverage/lcov-report/api/routes/webhook.js.html +1 -1
  88. package/coverage/lcov-report/automations/actions.js.html +24 -24
  89. package/coverage/lcov-report/automations/automationUtils.js.html +7 -10
  90. package/coverage/lcov-report/automations/bullboard.js.html +16 -16
  91. package/coverage/lcov-report/automations/index.html +18 -18
  92. package/coverage/lcov-report/automations/index.js.html +1 -1
  93. package/coverage/lcov-report/automations/steps/bash.js.html +6 -6
  94. package/coverage/lcov-report/automations/steps/createRow.js.html +7 -10
  95. package/coverage/lcov-report/automations/steps/delay.js.html +4 -4
  96. package/coverage/lcov-report/automations/steps/deleteRow.js.html +7 -7
  97. package/coverage/lcov-report/automations/steps/discord.js.html +7 -7
  98. package/coverage/lcov-report/automations/steps/executeQuery.js.html +6 -6
  99. package/coverage/lcov-report/automations/steps/executeScript.js.html +6 -6
  100. package/coverage/lcov-report/automations/steps/filter.js.html +7 -7
  101. package/coverage/lcov-report/automations/steps/index.html +1 -1
  102. package/coverage/lcov-report/automations/steps/integromat.js.html +5 -5
  103. package/coverage/lcov-report/automations/steps/outgoingWebhook.js.html +8 -8
  104. package/coverage/lcov-report/automations/steps/queryRows.js.html +10 -10
  105. package/coverage/lcov-report/automations/steps/sendSmtpEmail.js.html +5 -5
  106. package/coverage/lcov-report/automations/steps/serverLog.js.html +3 -3
  107. package/coverage/lcov-report/automations/steps/slack.js.html +5 -5
  108. package/coverage/lcov-report/automations/steps/updateRow.js.html +7 -7
  109. package/coverage/lcov-report/automations/steps/utils.js.html +3 -3
  110. package/coverage/lcov-report/automations/steps/zapier.js.html +5 -5
  111. package/coverage/lcov-report/automations/triggerInfo/app.js.html +2 -2
  112. package/coverage/lcov-report/automations/triggerInfo/cron.js.html +2 -2
  113. package/coverage/lcov-report/automations/triggerInfo/index.html +1 -1
  114. package/coverage/lcov-report/automations/triggerInfo/index.js.html +8 -8
  115. package/coverage/lcov-report/automations/triggerInfo/rowDeleted.js.html +2 -2
  116. package/coverage/lcov-report/automations/triggerInfo/rowSaved.js.html +2 -2
  117. package/coverage/lcov-report/automations/triggerInfo/rowUpdated.js.html +2 -2
  118. package/coverage/lcov-report/automations/triggerInfo/webhook.js.html +2 -2
  119. package/coverage/lcov-report/automations/triggers.js.html +61 -55
  120. package/coverage/lcov-report/automations/utils.js.html +54 -42
  121. package/coverage/lcov-report/constants/index.html +1 -1
  122. package/coverage/lcov-report/constants/index.js.html +26 -26
  123. package/coverage/lcov-report/constants/layouts.js.html +5 -5
  124. package/coverage/lcov-report/constants/screens.js.html +4 -4
  125. package/coverage/lcov-report/db/client.js.html +18 -18
  126. package/coverage/lcov-report/db/inMemoryView.js.html +7 -7
  127. package/coverage/lcov-report/db/index.html +1 -1
  128. package/coverage/lcov-report/db/index.js.html +3 -3
  129. package/coverage/lcov-report/db/linkedRows/LinkController.js.html +15 -27
  130. package/coverage/lcov-report/db/linkedRows/index.html +27 -27
  131. package/coverage/lcov-report/db/linkedRows/index.js.html +40 -82
  132. package/coverage/lcov-report/db/linkedRows/linkUtils.js.html +40 -40
  133. package/coverage/lcov-report/db/newid.js.html +4 -4
  134. package/coverage/lcov-report/db/utils.js.html +74 -74
  135. package/coverage/lcov-report/events/AutomationEmitter.js.html +1 -1
  136. package/coverage/lcov-report/events/index.html +1 -1
  137. package/coverage/lcov-report/events/index.js.html +5 -5
  138. package/coverage/lcov-report/events/utils.js.html +3 -3
  139. package/coverage/lcov-report/index.html +97 -112
  140. package/coverage/lcov-report/integrations/tests/TestConfiguration.js.html +1 -1
  141. package/coverage/lcov-report/integrations/tests/index.html +1 -1
  142. package/coverage/lcov-report/middleware/appInfo.js.html +1 -1
  143. package/coverage/lcov-report/middleware/authorized.js.html +21 -15
  144. package/coverage/lcov-report/middleware/builder.js.html +3 -3
  145. package/coverage/lcov-report/middleware/currentapp.js.html +72 -60
  146. package/coverage/lcov-report/middleware/index.html +28 -28
  147. package/coverage/lcov-report/middleware/joi-validator.js.html +3 -3
  148. package/coverage/lcov-report/middleware/resourceId.js.html +1 -1
  149. package/coverage/lcov-report/middleware/selfhost.js.html +1 -1
  150. package/coverage/lcov-report/middleware/usageQuota.js.html +29 -35
  151. package/coverage/lcov-report/middleware/utils.js.html +2 -2
  152. package/coverage/lcov-report/threads/automation.js.html +30 -24
  153. package/coverage/lcov-report/threads/index.html +25 -25
  154. package/coverage/lcov-report/threads/index.js.html +10 -10
  155. package/coverage/lcov-report/threads/query.js.html +26 -47
  156. package/coverage/lcov-report/threads/utils.js.html +14 -14
  157. package/coverage/lcov-report/utilities/bcrypt.js.html +1 -1
  158. package/coverage/lcov-report/utilities/budibaseDir.js.html +7 -7
  159. package/coverage/lcov-report/utilities/centralPath.js.html +4 -4
  160. package/coverage/lcov-report/utilities/csvParser.js.html +6 -6
  161. package/coverage/lcov-report/utilities/global.js.html +57 -48
  162. package/coverage/lcov-report/utilities/index.html +27 -27
  163. package/coverage/lcov-report/utilities/index.js.html +42 -72
  164. package/coverage/lcov-report/utilities/queue/inMemoryQueue.js.html +8 -8
  165. package/coverage/lcov-report/utilities/queue/index.html +1 -1
  166. package/coverage/lcov-report/utilities/routing/index.html +1 -1
  167. package/coverage/lcov-report/utilities/routing/index.js.html +7 -7
  168. package/coverage/lcov-report/utilities/rowProcessor/index.html +11 -11
  169. package/coverage/lcov-report/utilities/rowProcessor/index.js.html +32 -74
  170. package/coverage/lcov-report/utilities/rowProcessor/utils.js.html +4 -4
  171. package/coverage/lcov-report/utilities/scriptRunner.js.html +5 -5
  172. package/coverage/lcov-report/utilities/security.js.html +8 -8
  173. package/coverage/lcov-report/utilities/statusCodes.js.html +1 -1
  174. package/coverage/lcov-report/utilities/usageQuota/index.html +1 -1
  175. package/coverage/lcov-report/utilities/usageQuota/index.js.html +13 -10
  176. package/coverage/lcov-report/utilities/usageQuota/rows.js.html +5 -2
  177. package/coverage/lcov-report/utilities/usageQuota/usageQuoteReset.js.html +1 -1
  178. package/coverage/lcov-report/utilities/users.js.html +9 -9
  179. package/coverage/lcov-report/utilities/workerRequests.js.html +16 -16
  180. package/coverage/lcov.info +4018 -4092
  181. package/dist/api/controllers/application.js +49 -48
  182. package/dist/api/controllers/auth.js +4 -5
  183. package/dist/api/controllers/automation.js +14 -19
  184. package/dist/api/controllers/cloud.js +2 -3
  185. package/dist/api/controllers/component.js +3 -4
  186. package/dist/api/controllers/datasource.js +9 -10
  187. package/dist/api/controllers/deploy/Deployment.js +3 -6
  188. package/dist/api/controllers/deploy/index.js +12 -13
  189. package/dist/api/controllers/dev.js +5 -5
  190. package/dist/api/controllers/layout.js +3 -3
  191. package/dist/api/controllers/metadata.js +4 -4
  192. package/dist/api/controllers/migrations.js +21 -0
  193. package/dist/api/controllers/permission.js +4 -4
  194. package/dist/api/controllers/query/import/index.js +4 -6
  195. package/dist/api/controllers/query/index.js +11 -10
  196. package/dist/api/controllers/role.js +5 -5
  197. package/dist/api/controllers/routing.js +5 -6
  198. package/dist/api/controllers/row/ExternalRequest.js +90 -67
  199. package/dist/api/controllers/row/external.js +14 -22
  200. package/dist/api/controllers/row/internal.js +29 -44
  201. package/dist/api/controllers/row/internalSearch.js +11 -13
  202. package/dist/api/controllers/row/staticFormula.js +11 -11
  203. package/dist/api/controllers/row/utils.js +7 -6
  204. package/dist/api/controllers/screen.js +5 -7
  205. package/dist/api/controllers/static/index.js +4 -4
  206. package/dist/api/controllers/table/bulkFormula.js +14 -15
  207. package/dist/api/controllers/table/external.js +7 -10
  208. package/dist/api/controllers/table/index.js +5 -5
  209. package/dist/api/controllers/table/internal.js +16 -19
  210. package/dist/api/controllers/table/utils.js +29 -31
  211. package/dist/api/controllers/user.js +16 -20
  212. package/dist/api/controllers/view/index.js +8 -10
  213. package/dist/api/controllers/view/utils.js +9 -4
  214. package/dist/api/controllers/webhook.js +10 -8
  215. package/dist/api/routes/index.js +2 -0
  216. package/dist/api/routes/migrations.js +9 -0
  217. package/dist/api/routes/tests/utilities/TestFunctions.js +37 -17
  218. package/dist/app.js +35 -5
  219. package/dist/automations/automationUtils.js +2 -3
  220. package/dist/automations/steps/createRow.js +1 -1
  221. package/dist/automations/steps/updateRow.js +1 -1
  222. package/dist/automations/triggers.js +27 -25
  223. package/dist/automations/utils.js +10 -5
  224. package/dist/db/linkedRows/LinkController.js +3 -7
  225. package/dist/db/linkedRows/index.js +16 -26
  226. package/dist/db/linkedRows/linkUtils.js +6 -6
  227. package/dist/db/views/staticViews.js +9 -10
  228. package/dist/environment.js +3 -1
  229. package/dist/integrations/base/sql.js +49 -15
  230. package/dist/integrations/utils.js +3 -0
  231. package/dist/middleware/authorized.js +5 -3
  232. package/dist/middleware/builder.js +2 -2
  233. package/dist/middleware/currentapp.js +41 -37
  234. package/dist/middleware/usageQuota.js +2 -4
  235. package/dist/migrations/functions/appUrls.js +46 -0
  236. package/dist/migrations/functions/quotas1.js +50 -0
  237. package/dist/migrations/{usageQuotas → functions/usageQuotas}/index.js +6 -10
  238. package/dist/migrations/{usageQuotas → functions/usageQuotas}/syncApps.js +12 -9
  239. package/dist/migrations/{usageQuotas → functions/usageQuotas}/syncRows.js +16 -12
  240. package/dist/migrations/functions/userEmailViewCasing.js +24 -0
  241. package/dist/migrations/index.js +62 -0
  242. package/dist/package.json +4 -4
  243. package/dist/tests/utilities/TestConfiguration.js +70 -44
  244. package/dist/threads/automation.js +13 -11
  245. package/dist/threads/query.js +13 -19
  246. package/dist/tsconfig.tsbuildinfo +1 -1
  247. package/dist/utilities/fileSystem/index.js +4 -3
  248. package/dist/utilities/global.js +15 -12
  249. package/dist/utilities/index.js +7 -17
  250. package/dist/utilities/routing/index.js +5 -5
  251. package/dist/utilities/rowProcessor/index.js +9 -10
  252. package/dist/utilities/usageQuota/index.js +1 -0
  253. package/dist/utilities/usageQuota/rows.js +1 -0
  254. package/dist/utilities/users.js +3 -3
  255. package/dist/utilities/workerRequests.js +4 -4
  256. package/package.json +5 -5
  257. package/scripts/integrations/mssql/data/Dockerfile +1 -1
  258. package/scripts/integrations/mssql/data/setup.sql +17 -4
  259. package/scripts/integrations/postgres/init.sql +11 -6
  260. package/scripts/likeCypress.ts +1 -1
  261. package/src/api/controllers/application.js +38 -31
  262. package/src/api/controllers/auth.js +4 -5
  263. package/src/api/controllers/automation.js +13 -23
  264. package/src/api/controllers/cloud.js +2 -3
  265. package/src/api/controllers/component.js +3 -4
  266. package/src/api/controllers/datasource.js +9 -11
  267. package/src/api/controllers/deploy/Deployment.js +3 -7
  268. package/src/api/controllers/deploy/index.js +20 -13
  269. package/src/api/controllers/dev.js +5 -5
  270. package/src/api/controllers/layout.js +3 -3
  271. package/src/api/controllers/metadata.js +4 -9
  272. package/src/api/controllers/migrations.js +13 -0
  273. package/src/api/controllers/permission.js +4 -4
  274. package/src/api/controllers/query/import/index.ts +4 -6
  275. package/src/api/controllers/query/import/tests/index.spec.js +2 -1
  276. package/src/api/controllers/query/index.js +11 -10
  277. package/src/api/controllers/role.js +5 -5
  278. package/src/api/controllers/routing.js +5 -6
  279. package/src/api/controllers/row/ExternalRequest.ts +67 -43
  280. package/src/api/controllers/row/external.js +27 -58
  281. package/src/api/controllers/row/internal.js +29 -44
  282. package/src/api/controllers/row/internalSearch.js +11 -13
  283. package/src/api/controllers/row/staticFormula.js +10 -11
  284. package/src/api/controllers/row/utils.js +7 -6
  285. package/src/api/controllers/screen.js +5 -7
  286. package/src/api/controllers/static/index.js +4 -4
  287. package/src/api/controllers/table/bulkFormula.js +13 -23
  288. package/src/api/controllers/table/external.js +7 -10
  289. package/src/api/controllers/table/index.js +5 -5
  290. package/src/api/controllers/table/internal.js +18 -21
  291. package/src/api/controllers/table/utils.js +29 -41
  292. package/src/api/controllers/user.js +17 -20
  293. package/src/api/controllers/view/index.js +8 -10
  294. package/src/api/controllers/view/utils.js +9 -4
  295. package/src/api/controllers/webhook.js +10 -8
  296. package/src/api/routes/index.js +2 -0
  297. package/src/api/routes/migrations.js +14 -0
  298. package/src/api/routes/tests/automation.spec.js +1 -0
  299. package/src/api/routes/tests/misc.spec.js +0 -1
  300. package/src/api/routes/tests/routing.spec.js +21 -12
  301. package/src/api/routes/tests/row.spec.js +7 -4
  302. package/src/api/routes/tests/utilities/TestFunctions.js +39 -17
  303. package/src/app.ts +18 -5
  304. package/src/automations/automationUtils.js +2 -3
  305. package/src/automations/steps/createRow.js +0 -1
  306. package/src/automations/steps/updateRow.js +1 -1
  307. package/src/automations/triggers.js +33 -31
  308. package/src/automations/utils.js +9 -5
  309. package/src/db/linkedRows/LinkController.js +3 -7
  310. package/src/db/linkedRows/index.js +16 -30
  311. package/src/db/linkedRows/linkUtils.js +6 -6
  312. package/src/db/tests/linkController.spec.js +0 -1
  313. package/src/db/tests/linkTests.spec.js +5 -6
  314. package/src/db/views/staticViews.js +8 -10
  315. package/src/environment.js +3 -1
  316. package/src/integrations/base/sql.ts +61 -22
  317. package/src/integrations/utils.ts +4 -1
  318. package/src/middleware/authorized.js +5 -3
  319. package/src/middleware/builder.js +2 -2
  320. package/src/middleware/currentapp.js +50 -46
  321. package/src/middleware/tests/authorized.spec.js +7 -2
  322. package/src/middleware/tests/currentapp.spec.js +6 -0
  323. package/src/middleware/usageQuota.js +2 -4
  324. package/src/migrations/functions/appUrls.ts +34 -0
  325. package/src/migrations/functions/quotas1.ts +20 -0
  326. package/src/migrations/functions/tests/appUrls.spec.js +29 -0
  327. package/src/migrations/{tests/usageQuotas/index.spec.js → functions/tests/quotas1.spec.js} +5 -5
  328. package/src/migrations/functions/tests/userEmailViewCasing.spec.js +25 -0
  329. package/src/migrations/functions/usageQuotas/index.ts +8 -0
  330. package/src/migrations/{usageQuotas/syncApps.js → functions/usageQuotas/syncApps.ts} +6 -6
  331. package/src/migrations/functions/usageQuotas/syncRows.ts +22 -0
  332. package/src/migrations/{tests/usageQuotas → functions/usageQuotas/tests}/syncApps.spec.js +4 -4
  333. package/src/migrations/{tests/usageQuotas → functions/usageQuotas/tests}/syncRows.spec.js +4 -4
  334. package/src/migrations/functions/userEmailViewCasing.ts +13 -0
  335. package/src/migrations/index.ts +56 -0
  336. package/src/module.d.ts +3 -0
  337. package/src/tests/utilities/TestConfiguration.js +73 -50
  338. package/src/threads/automation.js +17 -15
  339. package/src/threads/query.js +14 -21
  340. package/src/utilities/fileSystem/index.js +4 -3
  341. package/src/utilities/global.js +15 -12
  342. package/src/utilities/index.js +7 -17
  343. package/src/utilities/routing/index.js +5 -5
  344. package/src/utilities/rowProcessor/index.js +9 -23
  345. package/src/utilities/usageQuota/index.js +1 -0
  346. package/src/utilities/usageQuota/rows.js +1 -0
  347. package/src/utilities/users.js +3 -3
  348. package/src/utilities/workerRequests.js +4 -4
  349. package/tsconfig.json +2 -1
  350. package/coverage/lcov-report/migrations/usageQuotas/index.html +0 -141
  351. package/coverage/lcov-report/migrations/usageQuotas/syncRows.js.html +0 -143
  352. package/src/migrations/usageQuotas/index.js +0 -24
  353. package/src/migrations/usageQuotas/syncRows.js +0 -21
@@ -9,7 +9,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  const { budibaseTempDir } = require("../budibaseDir");
12
- const { isDev } = require("../index");
13
12
  const fs = require("fs");
14
13
  const { join } = require("path");
15
14
  const uuid = require("uuid/v4");
@@ -20,6 +19,7 @@ const { updateClientLibrary } = require("./clientLibrary");
20
19
  const env = require("../../environment");
21
20
  const { USER_METDATA_PREFIX, LINK_USER_METADATA_PREFIX, } = require("../../db/utils");
22
21
  const MemoryStream = require("memorystream");
22
+ const { getAppId } = require("@budibase/backend-core/context");
23
23
  const TOP_LEVEL_PATH = join(__dirname, "..", "..", "..");
24
24
  const NODE_MODULES_PATH = join(TOP_LEVEL_PATH, "node_modules");
25
25
  /**
@@ -47,7 +47,7 @@ exports.init = () => {
47
47
  * everything required to function is ready.
48
48
  */
49
49
  exports.checkDevelopmentEnvironment = () => {
50
- if (!isDev()) {
50
+ if (!env.isDev() || env.isTest()) {
51
51
  return;
52
52
  }
53
53
  if (!fs.existsSync(budibaseTempDir())) {
@@ -218,7 +218,8 @@ exports.downloadTemplate = (type, name) => __awaiter(void 0, void 0, void 0, fun
218
218
  /**
219
219
  * Retrieves component libraries from object store (or tmp symlink if in local)
220
220
  */
221
- exports.getComponentLibraryManifest = (appId, library) => __awaiter(void 0, void 0, void 0, function* () {
221
+ exports.getComponentLibraryManifest = (library) => __awaiter(void 0, void 0, void 0, function* () {
222
+ const appId = getAppId();
222
223
  const filename = "manifest.json";
223
224
  /* istanbul ignore next */
224
225
  // when testing in cypress and so on we need to get the package
@@ -10,12 +10,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  const { getMultiIDParams, getGlobalIDFromUserMetadataID, } = require("../db/utils");
12
12
  const { BUILTIN_ROLE_IDS } = require("@budibase/backend-core/roles");
13
- const { getDeployedAppID } = require("@budibase/backend-core/db");
13
+ const { getProdAppID } = require("@budibase/backend-core/db");
14
14
  const { getGlobalUserParams } = require("@budibase/backend-core/db");
15
15
  const { user: userCache } = require("@budibase/backend-core/cache");
16
16
  const { getGlobalDB, isUserInAppTenant, } = require("@budibase/backend-core/tenancy");
17
17
  const env = require("../environment");
18
- exports.updateAppRole = (appId, user) => {
18
+ const { getAppId } = require("@budibase/backend-core/context");
19
+ exports.updateAppRole = (user, { appId } = {}) => {
20
+ appId = appId || getAppId();
19
21
  if (!user || !user.roles) {
20
22
  return user;
21
23
  }
@@ -27,7 +29,7 @@ exports.updateAppRole = (appId, user) => {
27
29
  return user;
28
30
  }
29
31
  // always use the deployed app
30
- user.roleId = user.roles[getDeployedAppID(appId)];
32
+ user.roleId = user.roles[getProdAppID(appId)];
31
33
  // if a role wasn't found then either set as admin (builder) or public (everyone else)
32
34
  if (!user.roleId && user.builder && user.builder.global) {
33
35
  user.roleId = BUILTIN_ROLE_IDS.ADMIN;
@@ -38,27 +40,28 @@ exports.updateAppRole = (appId, user) => {
38
40
  delete user.roles;
39
41
  return user;
40
42
  };
41
- function processUser(appId, user) {
43
+ function processUser(user, { appId } = {}) {
42
44
  if (user) {
43
45
  delete user.password;
44
46
  }
45
- return exports.updateAppRole(appId, user);
47
+ return exports.updateAppRole(user, { appId });
46
48
  }
47
49
  exports.getCachedSelf = (ctx, appId) => __awaiter(void 0, void 0, void 0, function* () {
48
50
  // this has to be tenant aware, can't depend on the context to find it out
49
51
  // running some middlewares before the tenancy causes context to break
50
52
  const user = yield userCache.getUser(ctx.user._id);
51
- return processUser(appId, user);
53
+ return processUser(user, { appId });
52
54
  });
53
55
  exports.getRawGlobalUser = (userId) => __awaiter(void 0, void 0, void 0, function* () {
54
56
  const db = getGlobalDB();
55
57
  return db.get(getGlobalIDFromUserMetadataID(userId));
56
58
  });
57
- exports.getGlobalUser = (appId, userId) => __awaiter(void 0, void 0, void 0, function* () {
59
+ exports.getGlobalUser = (userId) => __awaiter(void 0, void 0, void 0, function* () {
58
60
  let user = yield exports.getRawGlobalUser(userId);
59
- return processUser(appId, user);
61
+ return processUser(user);
60
62
  });
61
- exports.getGlobalUsers = (appId = null, users = null) => __awaiter(void 0, void 0, void 0, function* () {
63
+ exports.getGlobalUsers = (users = null) => __awaiter(void 0, void 0, void 0, function* () {
64
+ const appId = getAppId();
62
65
  const db = getGlobalDB();
63
66
  let globalUsers;
64
67
  if (users) {
@@ -80,10 +83,10 @@ exports.getGlobalUsers = (appId = null, users = null) => __awaiter(void 0, void
80
83
  if (!appId) {
81
84
  return globalUsers;
82
85
  }
83
- return globalUsers.map(user => exports.updateAppRole(appId, user));
86
+ return globalUsers.map(user => exports.updateAppRole(user));
84
87
  });
85
- exports.getGlobalUsersFromMetadata = (appId, users) => __awaiter(void 0, void 0, void 0, function* () {
86
- const globalUsers = yield exports.getGlobalUsers(appId, users);
88
+ exports.getGlobalUsersFromMetadata = (users) => __awaiter(void 0, void 0, void 0, function* () {
89
+ const globalUsers = yield exports.getGlobalUsers(users);
87
90
  return users.map(user => {
88
91
  const globalUser = globalUsers.find(globalUser => globalUser && user._id.includes(globalUser._id));
89
92
  return Object.assign(Object.assign({}, globalUser), user);
@@ -11,9 +11,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  const env = require("../environment");
12
12
  const { OBJ_STORE_DIRECTORY } = require("../constants");
13
13
  const { sanitizeKey } = require("@budibase/backend-core/objectStore");
14
- const CouchDB = require("../db");
15
14
  const { generateMetadataID } = require("../db/utils");
16
15
  const Readable = require("stream").Readable;
16
+ const { getAppDB } = require("@budibase/backend-core/context");
17
17
  const BB_CDN = "https://cdn.budi.live";
18
18
  exports.wait = ms => new Promise(resolve => setTimeout(resolve, ms));
19
19
  exports.isDev = env.isDev;
@@ -72,8 +72,8 @@ exports.clientLibraryPath = (appId, version) => {
72
72
  exports.attachmentsRelativeURL = attachmentKey => {
73
73
  return exports.checkSlashesInUrl(`${exports.objectStoreUrl()}/${attachmentKey}`);
74
74
  };
75
- exports.updateEntityMetadata = (appId, type, entityId, updateFn) => __awaiter(void 0, void 0, void 0, function* () {
76
- const db = new CouchDB(appId);
75
+ exports.updateEntityMetadata = (type, entityId, updateFn) => __awaiter(void 0, void 0, void 0, function* () {
76
+ const db = getAppDB();
77
77
  const id = generateMetadataID(type, entityId);
78
78
  // read it to see if it exists, we'll overwrite it no matter what
79
79
  let rev, metadata = {};
@@ -93,13 +93,13 @@ exports.updateEntityMetadata = (appId, type, entityId, updateFn) => __awaiter(vo
93
93
  const response = yield db.put(metadata);
94
94
  return Object.assign(Object.assign({}, metadata), { _id: id, _rev: response.rev });
95
95
  });
96
- exports.saveEntityMetadata = (appId, type, entityId, metadata) => __awaiter(void 0, void 0, void 0, function* () {
97
- return exports.updateEntityMetadata(appId, type, entityId, () => {
96
+ exports.saveEntityMetadata = (type, entityId, metadata) => __awaiter(void 0, void 0, void 0, function* () {
97
+ return exports.updateEntityMetadata(type, entityId, () => {
98
98
  return metadata;
99
99
  });
100
100
  });
101
- exports.deleteEntityMetadata = (appId, type, entityId) => __awaiter(void 0, void 0, void 0, function* () {
102
- const db = new CouchDB(appId);
101
+ exports.deleteEntityMetadata = (type, entityId) => __awaiter(void 0, void 0, void 0, function* () {
102
+ const db = getAppDB();
103
103
  const id = generateMetadataID(type, entityId);
104
104
  let rev;
105
105
  try {
@@ -132,16 +132,6 @@ exports.stringToReadStream = string => {
132
132
  },
133
133
  });
134
134
  };
135
- exports.doesDatabaseExist = (dbName) => __awaiter(void 0, void 0, void 0, function* () {
136
- try {
137
- const db = new CouchDB(dbName, { skip_setup: true });
138
- const info = yield db.info();
139
- return info && !info.error;
140
- }
141
- catch (err) {
142
- return false;
143
- }
144
- });
145
135
  exports.formatBytes = bytes => {
146
136
  const units = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
147
137
  const byteIncrements = 1024;
@@ -8,11 +8,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
- const CouchDB = require("../../db");
12
11
  const { createRoutingView } = require("../../db/views/staticViews");
13
12
  const { ViewNames, getQueryIndex, UNICODE_MAX } = require("../../db/utils");
14
- exports.getRoutingInfo = (appId) => __awaiter(void 0, void 0, void 0, function* () {
15
- const db = new CouchDB(appId);
13
+ const { getAppDB } = require("@budibase/backend-core/context");
14
+ exports.getRoutingInfo = () => __awaiter(void 0, void 0, void 0, function* () {
15
+ const db = getAppDB();
16
16
  try {
17
17
  const allRouting = yield db.query(getQueryIndex(ViewNames.ROUTING), {
18
18
  startKey: "",
@@ -24,8 +24,8 @@ exports.getRoutingInfo = (appId) => __awaiter(void 0, void 0, void 0, function*
24
24
  // check if the view doesn't exist, it should for all new instances
25
25
  /* istanbul ignore next */
26
26
  if (err != null && err.name === "not_found") {
27
- yield createRoutingView(appId);
28
- return exports.getRoutingInfo(appId);
27
+ yield createRoutingView();
28
+ return exports.getRoutingInfo();
29
29
  }
30
30
  else {
31
31
  throw err;
@@ -15,8 +15,8 @@ const { attachmentsRelativeURL } = require("../index");
15
15
  const { processFormulas } = require("./utils");
16
16
  const { deleteFiles } = require("../../utilities/fileSystem/utilities");
17
17
  const { ObjectStoreBuckets } = require("../../constants");
18
- const { isProdAppID, getDeployedAppID, dbExists, } = require("@budibase/backend-core/db");
19
- const CouchDB = require("../../db");
18
+ const { isProdAppID, getProdAppID, dbExists, } = require("@budibase/backend-core/db");
19
+ const { getAppId } = require("@budibase/backend-core/context");
20
20
  const BASE_AUTO_ID = 1;
21
21
  /**
22
22
  * A map of how we convert various properties in rows to each other based on the row type.
@@ -238,21 +238,20 @@ exports.inputProcessing = (user = {}, table, row, opts = { noAutoRelationships:
238
238
  /**
239
239
  * This function enriches the input rows with anything they are supposed to contain, for example
240
240
  * link records or attachment links.
241
- * @param {string} appId the app in which the request is looking for enriched rows.
242
241
  * @param {object} table the table from which these rows came from originally, this is used to determine
243
242
  * the schema of the rows and then enrich.
244
243
  * @param {object[]|object} rows the rows which are to be enriched.
245
244
  * @param {object} opts used to set some options for the output, such as disabling relationship squashing.
246
245
  * @returns {object[]|object} the enriched rows will be returned.
247
246
  */
248
- exports.outputProcessing = ({ appId }, table, rows, opts = { squash: true }) => __awaiter(void 0, void 0, void 0, function* () {
247
+ exports.outputProcessing = (table, rows, opts = { squash: true }) => __awaiter(void 0, void 0, void 0, function* () {
249
248
  let wasArray = true;
250
249
  if (!(rows instanceof Array)) {
251
250
  rows = [rows];
252
251
  wasArray = false;
253
252
  }
254
253
  // attach any linked row information
255
- let enriched = yield linkRows.attachFullLinkedDocs(appId, table, rows);
254
+ let enriched = yield linkRows.attachFullLinkedDocs(table, rows);
256
255
  // process formulas
257
256
  enriched = processFormulas(table, enriched, { dynamic: true });
258
257
  // update the attachments URL depending on hosting
@@ -269,13 +268,12 @@ exports.outputProcessing = ({ appId }, table, rows, opts = { squash: true }) =>
269
268
  }
270
269
  }
271
270
  if (opts.squash) {
272
- enriched = yield linkRows.squashLinksToPrimaryDisplay(appId, table, enriched);
271
+ enriched = yield linkRows.squashLinksToPrimaryDisplay(table, enriched);
273
272
  }
274
273
  return wasArray ? enriched : enriched[0];
275
274
  });
276
275
  /**
277
276
  * Clean up any attachments that were attached to a row.
278
- * @param {string} appId The ID of the app from which a row is being deleted.
279
277
  * @param {object} table The table from which a row is being removed.
280
278
  * @param {any} row optional - the row being removed.
281
279
  * @param {any} rows optional - if multiple rows being deleted can do this in bulk.
@@ -284,11 +282,12 @@ exports.outputProcessing = ({ appId }, table, rows, opts = { squash: true }) =>
284
282
  * deleted attachment columns.
285
283
  * @return {Promise<void>} When all attachments have been removed this will return.
286
284
  */
287
- exports.cleanupAttachments = (appId, table, { row, rows, oldRow, oldTable }) => __awaiter(void 0, void 0, void 0, function* () {
285
+ exports.cleanupAttachments = (table, { row, rows, oldRow, oldTable }) => __awaiter(void 0, void 0, void 0, function* () {
286
+ const appId = getAppId();
288
287
  if (!isProdAppID(appId)) {
289
- const prodAppId = getDeployedAppID(appId);
288
+ const prodAppId = getProdAppID(appId);
290
289
  // if prod exists, then don't allow deleting
291
- const exists = yield dbExists(CouchDB, prodAppId);
290
+ const exists = yield dbExists(prodAppId);
292
291
  if (exists) {
293
292
  return;
294
293
  }
@@ -52,6 +52,7 @@ exports.getUsageQuotaDoc = (db) => __awaiter(void 0, void 0, void 0, function* (
52
52
  * Given a specified tenantId this will add to the usage object for the specified property.
53
53
  * @param {string} property The property which is to be added to (within the nested usageQuota object).
54
54
  * @param {number} usage The amount (this can be negative) to adjust the number by.
55
+ * @param {object} opts optional - options such as dryRun, to check what update will do.
55
56
  * @returns {Promise<void>} When this completes the API key will now be up to date - the quota period may have
56
57
  * also been reset after this call.
57
58
  */
@@ -31,6 +31,7 @@ const getAppPairs = appIds => {
31
31
  return pairs;
32
32
  };
33
33
  const getAppRows = (appId) => __awaiter(void 0, void 0, void 0, function* () {
34
+ // need to specify the app ID, as this is used for different apps in one call
34
35
  const appDb = new CouchDB(appId);
35
36
  const response = yield appDb.allDocs(getRowParams(null, null, {
36
37
  include_docs: false,
@@ -8,15 +8,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
- const CouchDB = require("../db");
12
11
  const { InternalTables } = require("../db/utils");
13
12
  const { getGlobalUser } = require("../utilities/global");
13
+ const { getAppDB } = require("@budibase/backend-core/context");
14
14
  exports.getFullUser = (ctx, userId) => __awaiter(void 0, void 0, void 0, function* () {
15
- const global = yield getGlobalUser(ctx.appId, userId);
15
+ const global = yield getGlobalUser(userId);
16
16
  let metadata;
17
17
  try {
18
18
  // this will throw an error if the db doesn't exist, or there is no appId
19
- const db = new CouchDB(ctx.appId);
19
+ const db = getAppDB();
20
20
  metadata = yield db.get(userId);
21
21
  }
22
22
  catch (err) {
@@ -11,7 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  const fetch = require("node-fetch");
12
12
  const env = require("../environment");
13
13
  const { checkSlashesInUrl } = require("./index");
14
- const { getDeployedAppID } = require("@budibase/backend-core/db");
14
+ const { getProdAppID } = require("@budibase/backend-core/db");
15
15
  const { updateAppRole } = require("./global");
16
16
  const { Headers } = require("@budibase/backend-core/constants");
17
17
  const { getTenantId, isTenantIdSet } = require("@budibase/backend-core/tenancy");
@@ -71,13 +71,13 @@ exports.getGlobalSelf = (ctx, appId = null) => __awaiter(void 0, void 0, void 0,
71
71
  }
72
72
  let json = yield response.json();
73
73
  if (appId) {
74
- json = updateAppRole(appId, json);
74
+ json = updateAppRole(json);
75
75
  }
76
76
  return json;
77
77
  });
78
78
  exports.removeAppFromUserRoles = (ctx, appId) => __awaiter(void 0, void 0, void 0, function* () {
79
- const deployedAppId = getDeployedAppID(appId);
80
- const response = yield fetch(checkSlashesInUrl(env.WORKER_URL + `/api/global/roles/${deployedAppId}`), request(ctx, {
79
+ const prodAppId = getProdAppID(appId);
80
+ const response = yield fetch(checkSlashesInUrl(env.WORKER_URL + `/api/global/roles/${prodAppId}`), request(ctx, {
81
81
  method: "DELETE",
82
82
  }));
83
83
  if (response.status !== 200) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@budibase/server",
3
3
  "email": "hi@budibase.com",
4
- "version": "1.0.49-alpha.1",
4
+ "version": "1.0.49-alpha.13",
5
5
  "description": "Budibase Web Server",
6
6
  "main": "src/index.ts",
7
7
  "repository": {
@@ -70,9 +70,9 @@
70
70
  "license": "GPL-3.0",
71
71
  "dependencies": {
72
72
  "@apidevtools/swagger-parser": "^10.0.3",
73
- "@budibase/backend-core": "^1.0.49-alpha.1",
74
- "@budibase/client": "^1.0.49-alpha.1",
75
- "@budibase/string-templates": "^1.0.49-alpha.1",
73
+ "@budibase/backend-core": "^1.0.49-alpha.13",
74
+ "@budibase/client": "^1.0.49-alpha.13",
75
+ "@budibase/string-templates": "^1.0.49-alpha.13",
76
76
  "@bull-board/api": "^3.7.0",
77
77
  "@bull-board/koa": "^3.7.0",
78
78
  "@elastic/elasticsearch": "7.10.0",
@@ -168,5 +168,5 @@
168
168
  "optionalDependencies": {
169
169
  "oracledb": "^5.3.0"
170
170
  },
171
- "gitHead": "3f8d81837dd8ba0ebf17dbd8106c3a5d3bec3e46"
171
+ "gitHead": "db3f93e8a964ef589addfc6106337a7bfd7214b0"
172
172
  }
@@ -1,4 +1,4 @@
1
- FROM mcr.microsoft.com/mssql/server
1
+ FROM mcr.microsoft.com/mssql/server:2017-latest
2
2
 
3
3
  ENV ACCEPT_EULA=Y
4
4
  ENV SA_PASSWORD=Passw0rd
@@ -48,7 +48,20 @@ INSERT tasks
48
48
  VALUES
49
49
  ('Processing', 1);
50
50
 
51
- INSERT people
52
- (name, age)
53
- VALUES
54
- ('Bob', '30');
51
+ INSERT INTO people (name, age)
52
+ VALUES ('Bob', '30'),
53
+ ('Bert', '10'),
54
+ ('Jack', '12'),
55
+ ('Mike', '31'),
56
+ ('Dave', '44'),
57
+ ('Jim', '43'),
58
+ ('Kerry', '32'),
59
+ ('Julie', '12'),
60
+ ('Kim', '55'),
61
+ ('Andy', '33'),
62
+ ('John', '22'),
63
+ ('Ruth', '66'),
64
+ ('Robert', '88'),
65
+ ('Bobert', '99'),
66
+ ('Jan', '22'),
67
+ ('Megan', '11');
@@ -9,12 +9,16 @@ CREATE TABLE Persons (
9
9
  );
10
10
  CREATE TABLE Tasks (
11
11
  TaskID SERIAL PRIMARY KEY,
12
- PersonID INT,
12
+ ExecutorID INT,
13
+ QaID INT,
13
14
  Completed BOOLEAN,
14
15
  TaskName varchar(255),
15
- CONSTRAINT fkPersons
16
- FOREIGN KEY(PersonID)
17
- REFERENCES Persons(PersonID)
16
+ CONSTRAINT fkexecutor
17
+ FOREIGN KEY(ExecutorID)
18
+ REFERENCES Persons(PersonID),
19
+ CONSTRAINT fkqa
20
+ FOREIGN KEY(QaID)
21
+ REFERENCES Persons(PersonID)
18
22
  );
19
23
  CREATE TABLE Products (
20
24
  ProductID SERIAL PRIMARY KEY,
@@ -32,8 +36,9 @@ CREATE TABLE Products_Tasks (
32
36
  PRIMARY KEY (ProductID, TaskID)
33
37
  );
34
38
  INSERT INTO Persons (FirstName, LastName, Address, City) VALUES ('Mike', 'Hughes', '123 Fake Street', 'Belfast');
35
- INSERT INTO Tasks (PersonID, TaskName, Completed) VALUES (1, 'assembling', TRUE);
36
- INSERT INTO Tasks (PersonID, TaskName, Completed) VALUES (1, 'processing', FALSE);
39
+ INSERT INTO Persons (FirstName, LastName, Address, City) Values ('John', 'Smith', '64 Updown Road', 'Dublin');
40
+ INSERT INTO Tasks (ExecutorID, QaID, TaskName, Completed) VALUES (1, 2, 'assembling', TRUE);
41
+ INSERT INTO Tasks (ExecutorID, QaID, TaskName, Completed) VALUES (2, 1, 'processing', FALSE);
37
42
  INSERT INTO Products (ProductName) VALUES ('Computers');
38
43
  INSERT INTO Products (ProductName) VALUES ('Laptops');
39
44
  INSERT INTO Products (ProductName) VALUES ('Chairs');
@@ -30,4 +30,4 @@ process.env.ALLOW_DEV_AUTOMATIONS = "1"
30
30
  const server = require("../src/app")
31
31
  process.env.PORT = WORKER_PORT
32
32
  const worker = require("../../worker/src/index")
33
- process.env.PORT = MAIN_PORT
33
+ process.env.PORT = MAIN_PORT
@@ -1,4 +1,3 @@
1
- const CouchDB = require("../../db")
2
1
  const env = require("../../environment")
3
2
  const packageJson = require("../../../package.json")
4
3
  const {
@@ -29,7 +28,7 @@ const { processObject } = require("@budibase/string-templates")
29
28
  const {
30
29
  getAllApps,
31
30
  isDevAppID,
32
- getDeployedAppID,
31
+ getProdAppID,
33
32
  Replication,
34
33
  } = require("@budibase/backend-core/db")
35
34
  const { USERS_TABLE_SCHEMA } = require("../../constants")
@@ -45,11 +44,17 @@ const { getTenantId, isMultiTenant } = require("@budibase/backend-core/tenancy")
45
44
  const { syncGlobalUsers } = require("./user")
46
45
  const { app: appCache } = require("@budibase/backend-core/cache")
47
46
  const { cleanupAutomations } = require("../../automations/utils")
47
+ const {
48
+ getAppDB,
49
+ getProdAppDB,
50
+ updateAppId,
51
+ } = require("@budibase/backend-core/context")
48
52
 
49
53
  const URL_REGEX_SLASH = /\/|\\/g
50
54
 
51
55
  // utility function, need to do away with this
52
- async function getLayouts(db) {
56
+ async function getLayouts() {
57
+ const db = getAppDB()
53
58
  return (
54
59
  await db.allDocs(
55
60
  getLayoutParams(null, {
@@ -59,7 +64,8 @@ async function getLayouts(db) {
59
64
  ).rows.map(row => row.doc)
60
65
  }
61
66
 
62
- async function getScreens(db) {
67
+ async function getScreens() {
68
+ const db = getAppDB()
63
69
  return (
64
70
  await db.allDocs(
65
71
  getScreenParams(null, {
@@ -75,7 +81,7 @@ function getUserRoleId(ctx) {
75
81
  : ctx.user.role._id
76
82
  }
77
83
 
78
- async function getAppUrl(ctx) {
84
+ exports.getAppUrl = ctx => {
79
85
  // construct the url
80
86
  let url
81
87
  if (ctx.request.body.url) {
@@ -117,8 +123,9 @@ async function createInstance(template) {
117
123
  const tenantId = isMultiTenant() ? getTenantId() : null
118
124
  const baseAppId = generateAppID(tenantId)
119
125
  const appId = generateDevAppID(baseAppId)
126
+ updateAppId(appId)
120
127
 
121
- const db = new CouchDB(appId)
128
+ const db = getAppDB()
122
129
  await db.put({
123
130
  _id: "_design/database",
124
131
  // view collation information, read before writing any complex views:
@@ -128,9 +135,9 @@ async function createInstance(template) {
128
135
 
129
136
  // NOTE: indexes need to be created before any tables/templates
130
137
  // add view for linked rows
131
- await createLinkView(appId)
132
- await createRoutingView(appId)
133
- await createAllSearchIndex(appId)
138
+ await createLinkView()
139
+ await createRoutingView()
140
+ await createAllSearchIndex()
134
141
 
135
142
  // replicate the template data to the instance DB
136
143
  // this is currently very hard to test, downloading and importing template files
@@ -156,7 +163,7 @@ async function createInstance(template) {
156
163
  exports.fetch = async ctx => {
157
164
  const dev = ctx.query && ctx.query.status === AppStatus.DEV
158
165
  const all = ctx.query && ctx.query.status === AppStatus.ALL
159
- const apps = await getAllApps(CouchDB, { dev, all })
166
+ const apps = await getAllApps({ dev, all })
160
167
 
161
168
  // get the locks for all the dev apps
162
169
  if (dev || all) {
@@ -179,12 +186,11 @@ exports.fetch = async ctx => {
179
186
  }
180
187
 
181
188
  exports.fetchAppDefinition = async ctx => {
182
- const db = new CouchDB(ctx.params.appId)
183
- const layouts = await getLayouts(db)
189
+ const layouts = await getLayouts()
184
190
  const userRoleId = getUserRoleId(ctx)
185
- const accessController = new AccessController(ctx.params.appId)
191
+ const accessController = new AccessController()
186
192
  const screens = await accessController.checkScreensAccess(
187
- await getScreens(db),
193
+ await getScreens(),
188
194
  userRoleId
189
195
  )
190
196
  ctx.body = {
@@ -195,15 +201,15 @@ exports.fetchAppDefinition = async ctx => {
195
201
  }
196
202
 
197
203
  exports.fetchAppPackage = async ctx => {
198
- const db = new CouchDB(ctx.params.appId)
204
+ const db = getAppDB()
199
205
  const application = await db.get(DocumentTypes.APP_METADATA)
200
- const layouts = await getLayouts(db)
201
- let screens = await getScreens(db)
206
+ const layouts = await getLayouts()
207
+ let screens = await getScreens()
202
208
 
203
209
  // Only filter screens if the user is not a builder
204
210
  if (!(ctx.user.builder && ctx.user.builder.global)) {
205
211
  const userRoleId = getUserRoleId(ctx)
206
- const accessController = new AccessController(ctx.params.appId)
212
+ const accessController = new AccessController()
207
213
  screens = await accessController.checkScreensAccess(screens, userRoleId)
208
214
  }
209
215
 
@@ -216,10 +222,10 @@ exports.fetchAppPackage = async ctx => {
216
222
  }
217
223
 
218
224
  exports.create = async ctx => {
219
- const apps = await getAllApps(CouchDB, { dev: true })
225
+ const apps = await getAllApps({ dev: true })
220
226
  const name = ctx.request.body.name
221
227
  checkAppName(ctx, apps, name)
222
- const url = await getAppUrl(ctx)
228
+ const url = exports.getAppUrl(ctx)
223
229
  checkAppUrl(ctx, apps, url)
224
230
 
225
231
  const { useTemplate, templateKey, templateString } = ctx.request.body
@@ -234,7 +240,7 @@ exports.create = async ctx => {
234
240
  const instance = await createInstance(instanceConfig)
235
241
  const appId = instance._id
236
242
 
237
- const db = new CouchDB(appId)
243
+ const db = getAppDB()
238
244
  let _rev
239
245
  try {
240
246
  // if template there will be an existing doc
@@ -280,13 +286,13 @@ exports.create = async ctx => {
280
286
  // This endpoint currently operates as a PATCH rather than a PUT
281
287
  // Thus name and url fields are handled only if present
282
288
  exports.update = async ctx => {
283
- const apps = await getAllApps(CouchDB, { dev: true })
289
+ const apps = await getAllApps({ dev: true })
284
290
  // validation
285
291
  const name = ctx.request.body.name
286
292
  if (name) {
287
293
  checkAppName(ctx, apps, name, ctx.params.appId)
288
294
  }
289
- const url = await getAppUrl(ctx)
295
+ const url = await exports.getAppUrl(ctx)
290
296
  if (url) {
291
297
  checkAppUrl(ctx, apps, url, ctx.params.appId)
292
298
  ctx.request.body.url = url
@@ -299,7 +305,7 @@ exports.update = async ctx => {
299
305
 
300
306
  exports.updateClient = async ctx => {
301
307
  // Get current app version
302
- const db = new CouchDB(ctx.params.appId)
308
+ const db = getAppDB()
303
309
  const application = await db.get(DocumentTypes.APP_METADATA)
304
310
  const currentVersion = application.version
305
311
 
@@ -321,7 +327,7 @@ exports.updateClient = async ctx => {
321
327
 
322
328
  exports.revertClient = async ctx => {
323
329
  // Check app can be reverted
324
- const db = new CouchDB(ctx.params.appId)
330
+ const db = getAppDB()
325
331
  const application = await db.get(DocumentTypes.APP_METADATA)
326
332
  if (!application.revertableVersion) {
327
333
  ctx.throw(400, "There is no version to revert to")
@@ -343,7 +349,7 @@ exports.revertClient = async ctx => {
343
349
  }
344
350
 
345
351
  exports.delete = async ctx => {
346
- const db = new CouchDB(ctx.params.appId)
352
+ const db = getAppDB()
347
353
 
348
354
  const result = await db.destroy()
349
355
  /* istanbul ignore next */
@@ -368,10 +374,11 @@ exports.sync = async (ctx, next) => {
368
374
  }
369
375
 
370
376
  // replicate prod to dev
371
- const prodAppId = getDeployedAppID(appId)
377
+ const prodAppId = getProdAppID(appId)
372
378
 
373
379
  try {
374
- const prodDb = new CouchDB(prodAppId, { skip_setup: true })
380
+ // specific case, want to make sure setup is skipped
381
+ const prodDb = getProdAppDB({ skip_setup: true })
375
382
  const info = await prodDb.info()
376
383
  if (info.error) throw info.error
377
384
  } catch (err) {
@@ -399,7 +406,7 @@ exports.sync = async (ctx, next) => {
399
406
  }
400
407
 
401
408
  // sync the users
402
- await syncGlobalUsers(appId)
409
+ await syncGlobalUsers()
403
410
 
404
411
  if (error) {
405
412
  ctx.throw(400, error)
@@ -411,7 +418,7 @@ exports.sync = async (ctx, next) => {
411
418
  }
412
419
 
413
420
  const updateAppPackage = async (appPackage, appId) => {
414
- const db = new CouchDB(appId)
421
+ const db = getAppDB()
415
422
  const application = await db.get(DocumentTypes.APP_METADATA)
416
423
 
417
424
  const newAppPackage = { ...application, ...appPackage }
@@ -430,7 +437,7 @@ const updateAppPackage = async (appPackage, appId) => {
430
437
  }
431
438
 
432
439
  const createEmptyAppPackage = async (ctx, app) => {
433
- const db = new CouchDB(app.appId)
440
+ const db = getAppDB()
434
441
 
435
442
  let screensAndLayouts = []
436
443
  for (let layout of BASE_LAYOUTS) {