@budibase/server 1.0.49-alpha.2 → 1.0.49-alpha.6

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 (320) 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.82e12602.js → index.1b905242.js} +1 -1
  8. package/builder/index.html +1 -1
  9. package/coverage/clover.xml +1917 -1929
  10. package/coverage/coverage-final.json +54 -54
  11. package/coverage/lcov-report/api/controllers/analytics.js.html +1 -1
  12. package/coverage/lcov-report/api/controllers/apikeys.js.html +1 -1
  13. package/coverage/lcov-report/api/controllers/application.js.html +58 -37
  14. package/coverage/lcov-report/api/controllers/auth.js.html +11 -14
  15. package/coverage/lcov-report/api/controllers/automation.js.html +21 -51
  16. package/coverage/lcov-report/api/controllers/backup.js.html +1 -1
  17. package/coverage/lcov-report/api/controllers/cloud.js.html +8 -11
  18. package/coverage/lcov-report/api/controllers/component.js.html +10 -13
  19. package/coverage/lcov-report/api/controllers/datasource.js.html +17 -23
  20. package/coverage/lcov-report/api/controllers/dev.js.html +6 -6
  21. package/coverage/lcov-report/api/controllers/index.html +52 -52
  22. package/coverage/lcov-report/api/controllers/integration.js.html +1 -1
  23. package/coverage/lcov-report/api/controllers/layout.js.html +4 -4
  24. package/coverage/lcov-report/api/controllers/metadata.js.html +6 -21
  25. package/coverage/lcov-report/api/controllers/migrations.js.html +1 -1
  26. package/coverage/lcov-report/api/controllers/permission.js.html +6 -6
  27. package/coverage/lcov-report/api/controllers/query/index.html +10 -10
  28. package/coverage/lcov-report/api/controllers/query/index.js.html +21 -18
  29. package/coverage/lcov-report/api/controllers/query/validation.js.html +1 -1
  30. package/coverage/lcov-report/api/controllers/role.js.html +9 -9
  31. package/coverage/lcov-report/api/controllers/routing.js.html +7 -10
  32. package/coverage/lcov-report/api/controllers/row/external.js.html +6 -12
  33. package/coverage/lcov-report/api/controllers/row/index.html +22 -22
  34. package/coverage/lcov-report/api/controllers/row/index.js.html +1 -1
  35. package/coverage/lcov-report/api/controllers/row/internal.js.html +37 -82
  36. package/coverage/lcov-report/api/controllers/row/internalSearch.js.html +19 -25
  37. package/coverage/lcov-report/api/controllers/row/staticFormula.js.html +14 -17
  38. package/coverage/lcov-report/api/controllers/row/utils.js.html +15 -12
  39. package/coverage/lcov-report/api/controllers/screen.js.html +10 -16
  40. package/coverage/lcov-report/api/controllers/script.js.html +1 -1
  41. package/coverage/lcov-report/api/controllers/table/bulkFormula.js.html +20 -50
  42. package/coverage/lcov-report/api/controllers/table/external.js.html +15 -24
  43. package/coverage/lcov-report/api/controllers/table/index.html +37 -37
  44. package/coverage/lcov-report/api/controllers/table/index.js.html +7 -7
  45. package/coverage/lcov-report/api/controllers/table/internal.js.html +36 -45
  46. package/coverage/lcov-report/api/controllers/table/utils.js.html +40 -76
  47. package/coverage/lcov-report/api/controllers/templates.js.html +1 -1
  48. package/coverage/lcov-report/api/controllers/user.js.html +26 -35
  49. package/coverage/lcov-report/api/controllers/view/exporters.js.html +1 -1
  50. package/coverage/lcov-report/api/controllers/view/index.html +16 -16
  51. package/coverage/lcov-report/api/controllers/view/index.js.html +15 -21
  52. package/coverage/lcov-report/api/controllers/view/utils.js.html +25 -10
  53. package/coverage/lcov-report/api/controllers/view/viewBuilder.js.html +1 -1
  54. package/coverage/lcov-report/api/controllers/webhook.js.html +21 -15
  55. package/coverage/lcov-report/api/index.html +1 -1
  56. package/coverage/lcov-report/api/index.js.html +1 -1
  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/index.html +1 -1
  69. package/coverage/lcov-report/api/routes/index.js.html +1 -1
  70. package/coverage/lcov-report/api/routes/integration.js.html +1 -1
  71. package/coverage/lcov-report/api/routes/layout.js.html +1 -1
  72. package/coverage/lcov-report/api/routes/metadata.js.html +1 -1
  73. package/coverage/lcov-report/api/routes/migrations.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 +1 -1
  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 +1 -1
  88. package/coverage/lcov-report/automations/automationUtils.js.html +4 -7
  89. package/coverage/lcov-report/automations/bullboard.js.html +1 -1
  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 +1 -1
  93. package/coverage/lcov-report/automations/steps/createRow.js.html +2 -5
  94. package/coverage/lcov-report/automations/steps/delay.js.html +1 -1
  95. package/coverage/lcov-report/automations/steps/deleteRow.js.html +1 -1
  96. package/coverage/lcov-report/automations/steps/discord.js.html +1 -1
  97. package/coverage/lcov-report/automations/steps/executeQuery.js.html +1 -1
  98. package/coverage/lcov-report/automations/steps/executeScript.js.html +1 -1
  99. package/coverage/lcov-report/automations/steps/filter.js.html +1 -1
  100. package/coverage/lcov-report/automations/steps/index.html +1 -1
  101. package/coverage/lcov-report/automations/steps/integromat.js.html +1 -1
  102. package/coverage/lcov-report/automations/steps/outgoingWebhook.js.html +1 -1
  103. package/coverage/lcov-report/automations/steps/queryRows.js.html +1 -1
  104. package/coverage/lcov-report/automations/steps/sendSmtpEmail.js.html +1 -1
  105. package/coverage/lcov-report/automations/steps/serverLog.js.html +1 -1
  106. package/coverage/lcov-report/automations/steps/slack.js.html +1 -1
  107. package/coverage/lcov-report/automations/steps/updateRow.js.html +2 -2
  108. package/coverage/lcov-report/automations/steps/utils.js.html +1 -1
  109. package/coverage/lcov-report/automations/steps/zapier.js.html +1 -1
  110. package/coverage/lcov-report/automations/triggerInfo/app.js.html +1 -1
  111. package/coverage/lcov-report/automations/triggerInfo/cron.js.html +1 -1
  112. package/coverage/lcov-report/automations/triggerInfo/index.html +1 -1
  113. package/coverage/lcov-report/automations/triggerInfo/index.js.html +1 -1
  114. package/coverage/lcov-report/automations/triggerInfo/rowDeleted.js.html +1 -1
  115. package/coverage/lcov-report/automations/triggerInfo/rowSaved.js.html +1 -1
  116. package/coverage/lcov-report/automations/triggerInfo/rowUpdated.js.html +1 -1
  117. package/coverage/lcov-report/automations/triggerInfo/webhook.js.html +1 -1
  118. package/coverage/lcov-report/automations/triggers.js.html +46 -40
  119. package/coverage/lcov-report/automations/utils.js.html +24 -12
  120. package/coverage/lcov-report/constants/index.html +1 -1
  121. package/coverage/lcov-report/constants/index.js.html +1 -1
  122. package/coverage/lcov-report/constants/layouts.js.html +1 -1
  123. package/coverage/lcov-report/constants/screens.js.html +1 -1
  124. package/coverage/lcov-report/db/client.js.html +18 -18
  125. package/coverage/lcov-report/db/inMemoryView.js.html +1 -1
  126. package/coverage/lcov-report/db/index.html +1 -1
  127. package/coverage/lcov-report/db/index.js.html +3 -3
  128. package/coverage/lcov-report/db/linkedRows/LinkController.js.html +11 -23
  129. package/coverage/lcov-report/db/linkedRows/index.html +27 -27
  130. package/coverage/lcov-report/db/linkedRows/index.js.html +24 -66
  131. package/coverage/lcov-report/db/linkedRows/linkUtils.js.html +28 -28
  132. package/coverage/lcov-report/db/newid.js.html +1 -1
  133. package/coverage/lcov-report/db/utils.js.html +5 -5
  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 +1 -1
  137. package/coverage/lcov-report/events/utils.js.html +1 -1
  138. package/coverage/lcov-report/index.html +88 -88
  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 +21 -15
  143. package/coverage/lcov-report/middleware/builder.js.html +3 -3
  144. package/coverage/lcov-report/middleware/currentapp.js.html +72 -60
  145. package/coverage/lcov-report/middleware/index.html +28 -28
  146. package/coverage/lcov-report/middleware/joi-validator.js.html +1 -1
  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 +29 -29
  150. package/coverage/lcov-report/middleware/utils.js.html +2 -2
  151. package/coverage/lcov-report/threads/automation.js.html +30 -24
  152. package/coverage/lcov-report/threads/index.html +16 -16
  153. package/coverage/lcov-report/threads/index.js.html +1 -1
  154. package/coverage/lcov-report/threads/query.js.html +17 -17
  155. package/coverage/lcov-report/threads/utils.js.html +1 -1
  156. package/coverage/lcov-report/utilities/bcrypt.js.html +1 -1
  157. package/coverage/lcov-report/utilities/budibaseDir.js.html +1 -1
  158. package/coverage/lcov-report/utilities/centralPath.js.html +1 -1
  159. package/coverage/lcov-report/utilities/csvParser.js.html +1 -1
  160. package/coverage/lcov-report/utilities/global.js.html +44 -35
  161. package/coverage/lcov-report/utilities/index.html +27 -27
  162. package/coverage/lcov-report/utilities/index.js.html +40 -70
  163. package/coverage/lcov-report/utilities/queue/inMemoryQueue.js.html +1 -1
  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 +11 -11
  168. package/coverage/lcov-report/utilities/rowProcessor/index.js.html +16 -58
  169. package/coverage/lcov-report/utilities/rowProcessor/utils.js.html +1 -1
  170. package/coverage/lcov-report/utilities/scriptRunner.js.html +1 -1
  171. package/coverage/lcov-report/utilities/security.js.html +1 -1
  172. package/coverage/lcov-report/utilities/statusCodes.js.html +1 -1
  173. package/coverage/lcov-report/utilities/usageQuota/index.html +1 -1
  174. package/coverage/lcov-report/utilities/usageQuota/index.js.html +5 -2
  175. package/coverage/lcov-report/utilities/usageQuota/rows.js.html +5 -2
  176. package/coverage/lcov-report/utilities/usageQuota/usageQuoteReset.js.html +1 -1
  177. package/coverage/lcov-report/utilities/users.js.html +5 -5
  178. package/coverage/lcov-report/utilities/workerRequests.js.html +5 -5
  179. package/coverage/lcov.info +3189 -3198
  180. package/dist/api/controllers/application.js +31 -28
  181. package/dist/api/controllers/auth.js +4 -5
  182. package/dist/api/controllers/automation.js +14 -19
  183. package/dist/api/controllers/cloud.js +2 -3
  184. package/dist/api/controllers/component.js +3 -4
  185. package/dist/api/controllers/datasource.js +9 -10
  186. package/dist/api/controllers/deploy/Deployment.js +3 -6
  187. package/dist/api/controllers/deploy/index.js +12 -13
  188. package/dist/api/controllers/dev.js +5 -5
  189. package/dist/api/controllers/layout.js +3 -3
  190. package/dist/api/controllers/metadata.js +4 -4
  191. package/dist/api/controllers/permission.js +4 -4
  192. package/dist/api/controllers/query/import/index.js +4 -6
  193. package/dist/api/controllers/query/index.js +11 -10
  194. package/dist/api/controllers/role.js +5 -5
  195. package/dist/api/controllers/routing.js +5 -6
  196. package/dist/api/controllers/row/ExternalRequest.js +76 -63
  197. package/dist/api/controllers/row/external.js +4 -4
  198. package/dist/api/controllers/row/internal.js +29 -44
  199. package/dist/api/controllers/row/internalSearch.js +11 -13
  200. package/dist/api/controllers/row/staticFormula.js +11 -11
  201. package/dist/api/controllers/row/utils.js +7 -6
  202. package/dist/api/controllers/screen.js +5 -7
  203. package/dist/api/controllers/static/index.js +4 -4
  204. package/dist/api/controllers/table/bulkFormula.js +14 -15
  205. package/dist/api/controllers/table/external.js +7 -10
  206. package/dist/api/controllers/table/index.js +5 -5
  207. package/dist/api/controllers/table/internal.js +16 -19
  208. package/dist/api/controllers/table/utils.js +29 -31
  209. package/dist/api/controllers/user.js +16 -20
  210. package/dist/api/controllers/view/index.js +8 -10
  211. package/dist/api/controllers/view/utils.js +9 -4
  212. package/dist/api/controllers/webhook.js +10 -8
  213. package/dist/api/routes/tests/utilities/TestFunctions.js +37 -17
  214. package/dist/automations/automationUtils.js +2 -3
  215. package/dist/automations/steps/createRow.js +1 -1
  216. package/dist/automations/steps/updateRow.js +1 -1
  217. package/dist/automations/triggers.js +27 -25
  218. package/dist/automations/utils.js +10 -5
  219. package/dist/db/linkedRows/LinkController.js +3 -7
  220. package/dist/db/linkedRows/index.js +16 -26
  221. package/dist/db/linkedRows/linkUtils.js +6 -6
  222. package/dist/db/views/staticViews.js +9 -10
  223. package/dist/environment.js +2 -1
  224. package/dist/integrations/utils.js +3 -0
  225. package/dist/middleware/authorized.js +5 -3
  226. package/dist/middleware/builder.js +2 -2
  227. package/dist/middleware/currentapp.js +41 -37
  228. package/dist/middleware/usageQuota.js +2 -2
  229. package/dist/migrations/functions/appUrls.js +11 -2
  230. package/dist/migrations/functions/usageQuotas/syncApps.js +7 -9
  231. package/dist/migrations/functions/usageQuotas/syncRows.js +7 -9
  232. package/dist/package.json +4 -4
  233. package/dist/tests/utilities/TestConfiguration.js +67 -44
  234. package/dist/threads/automation.js +13 -11
  235. package/dist/threads/query.js +11 -11
  236. package/dist/tsconfig.tsbuildinfo +1 -1
  237. package/dist/utilities/fileSystem/index.js +4 -3
  238. package/dist/utilities/global.js +15 -12
  239. package/dist/utilities/index.js +7 -17
  240. package/dist/utilities/routing/index.js +5 -5
  241. package/dist/utilities/rowProcessor/index.js +9 -10
  242. package/dist/utilities/usageQuota/index.js +1 -0
  243. package/dist/utilities/usageQuota/rows.js +1 -0
  244. package/dist/utilities/users.js +3 -3
  245. package/dist/utilities/workerRequests.js +4 -4
  246. package/package.json +5 -5
  247. package/scripts/likeCypress.ts +1 -1
  248. package/src/api/controllers/application.js +35 -28
  249. package/src/api/controllers/auth.js +4 -5
  250. package/src/api/controllers/automation.js +13 -23
  251. package/src/api/controllers/cloud.js +2 -3
  252. package/src/api/controllers/component.js +3 -4
  253. package/src/api/controllers/datasource.js +9 -11
  254. package/src/api/controllers/deploy/Deployment.js +3 -7
  255. package/src/api/controllers/deploy/index.js +20 -13
  256. package/src/api/controllers/dev.js +5 -5
  257. package/src/api/controllers/layout.js +3 -3
  258. package/src/api/controllers/metadata.js +4 -9
  259. package/src/api/controllers/permission.js +4 -4
  260. package/src/api/controllers/query/import/index.ts +4 -6
  261. package/src/api/controllers/query/import/tests/index.spec.js +2 -1
  262. package/src/api/controllers/query/index.js +11 -10
  263. package/src/api/controllers/role.js +5 -5
  264. package/src/api/controllers/routing.js +5 -6
  265. package/src/api/controllers/row/ExternalRequest.ts +53 -42
  266. package/src/api/controllers/row/external.js +4 -6
  267. package/src/api/controllers/row/internal.js +29 -44
  268. package/src/api/controllers/row/internalSearch.js +11 -13
  269. package/src/api/controllers/row/staticFormula.js +10 -11
  270. package/src/api/controllers/row/utils.js +7 -6
  271. package/src/api/controllers/screen.js +5 -7
  272. package/src/api/controllers/static/index.js +4 -4
  273. package/src/api/controllers/table/bulkFormula.js +13 -23
  274. package/src/api/controllers/table/external.js +7 -10
  275. package/src/api/controllers/table/index.js +5 -5
  276. package/src/api/controllers/table/internal.js +18 -21
  277. package/src/api/controllers/table/utils.js +29 -41
  278. package/src/api/controllers/user.js +17 -20
  279. package/src/api/controllers/view/index.js +8 -10
  280. package/src/api/controllers/view/utils.js +9 -4
  281. package/src/api/controllers/webhook.js +10 -8
  282. package/src/api/routes/tests/automation.spec.js +1 -0
  283. package/src/api/routes/tests/misc.spec.js +0 -1
  284. package/src/api/routes/tests/routing.spec.js +21 -12
  285. package/src/api/routes/tests/row.spec.js +7 -4
  286. package/src/api/routes/tests/utilities/TestFunctions.js +39 -17
  287. package/src/automations/automationUtils.js +2 -3
  288. package/src/automations/steps/createRow.js +0 -1
  289. package/src/automations/steps/updateRow.js +1 -1
  290. package/src/automations/triggers.js +33 -31
  291. package/src/automations/utils.js +9 -5
  292. package/src/db/linkedRows/LinkController.js +3 -7
  293. package/src/db/linkedRows/index.js +16 -30
  294. package/src/db/linkedRows/linkUtils.js +6 -6
  295. package/src/db/tests/linkController.spec.js +0 -1
  296. package/src/db/tests/linkTests.spec.js +5 -6
  297. package/src/db/views/staticViews.js +8 -10
  298. package/src/environment.js +2 -1
  299. package/src/integrations/utils.ts +4 -1
  300. package/src/middleware/authorized.js +5 -3
  301. package/src/middleware/builder.js +2 -2
  302. package/src/middleware/currentapp.js +50 -46
  303. package/src/middleware/tests/authorized.spec.js +7 -2
  304. package/src/middleware/tests/currentapp.spec.js +6 -0
  305. package/src/middleware/usageQuota.js +2 -2
  306. package/src/migrations/functions/appUrls.ts +11 -2
  307. package/src/migrations/functions/usageQuotas/syncApps.ts +5 -4
  308. package/src/migrations/functions/usageQuotas/syncRows.ts +5 -4
  309. package/src/tests/utilities/TestConfiguration.js +69 -50
  310. package/src/threads/automation.js +17 -15
  311. package/src/threads/query.js +12 -12
  312. package/src/utilities/fileSystem/index.js +4 -3
  313. package/src/utilities/global.js +15 -12
  314. package/src/utilities/index.js +7 -17
  315. package/src/utilities/routing/index.js +5 -5
  316. package/src/utilities/rowProcessor/index.js +9 -23
  317. package/src/utilities/usageQuota/index.js +1 -0
  318. package/src/utilities/usageQuota/rows.js +1 -0
  319. package/src/utilities/users.js +3 -3
  320. package/src/utilities/workerRequests.js +4 -4
@@ -8,7 +8,6 @@ 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 env = require("../../environment");
13
12
  const packageJson = require("../../../package.json");
14
13
  const { createLinkView, createRoutingView, createAllSearchIndex, } = require("../../db/views/staticViews");
@@ -18,7 +17,7 @@ const { BUILTIN_ROLE_IDS, AccessController, } = require("@budibase/backend-core/
18
17
  const { BASE_LAYOUTS } = require("../../constants/layouts");
19
18
  const { cloneDeep } = require("lodash/fp");
20
19
  const { processObject } = require("@budibase/string-templates");
21
- const { getAllApps, isDevAppID, getDeployedAppID, Replication, } = require("@budibase/backend-core/db");
20
+ const { getAllApps, isDevAppID, getProdAppID, Replication, } = require("@budibase/backend-core/db");
22
21
  const { USERS_TABLE_SCHEMA } = require("../../constants");
23
22
  const { removeAppFromUserRoles } = require("../../utilities/workerRequests");
24
23
  const { clientLibraryPath, stringToReadStream } = require("../../utilities");
@@ -28,17 +27,20 @@ const { getTenantId, isMultiTenant } = require("@budibase/backend-core/tenancy")
28
27
  const { syncGlobalUsers } = require("./user");
29
28
  const { app: appCache } = require("@budibase/backend-core/cache");
30
29
  const { cleanupAutomations } = require("../../automations/utils");
30
+ const { getAppDB, getProdAppDB, updateAppId, } = require("@budibase/backend-core/context");
31
31
  const URL_REGEX_SLASH = /\/|\\/g;
32
32
  // utility function, need to do away with this
33
- function getLayouts(db) {
33
+ function getLayouts() {
34
34
  return __awaiter(this, void 0, void 0, function* () {
35
+ const db = getAppDB();
35
36
  return (yield db.allDocs(getLayoutParams(null, {
36
37
  include_docs: true,
37
38
  }))).rows.map(row => row.doc);
38
39
  });
39
40
  }
40
- function getScreens(db) {
41
+ function getScreens() {
41
42
  return __awaiter(this, void 0, void 0, function* () {
43
+ const db = getAppDB();
42
44
  return (yield db.allDocs(getScreenParams(null, {
43
45
  include_docs: true,
44
46
  }))).rows.map(row => row.doc);
@@ -90,7 +92,8 @@ function createInstance(template) {
90
92
  const tenantId = isMultiTenant() ? getTenantId() : null;
91
93
  const baseAppId = generateAppID(tenantId);
92
94
  const appId = generateDevAppID(baseAppId);
93
- const db = new CouchDB(appId);
95
+ updateAppId(appId);
96
+ const db = getAppDB();
94
97
  yield db.put({
95
98
  _id: "_design/database",
96
99
  // view collation information, read before writing any complex views:
@@ -99,9 +102,9 @@ function createInstance(template) {
99
102
  });
100
103
  // NOTE: indexes need to be created before any tables/templates
101
104
  // add view for linked rows
102
- yield createLinkView(appId);
103
- yield createRoutingView(appId);
104
- yield createAllSearchIndex(appId);
105
+ yield createLinkView();
106
+ yield createRoutingView();
107
+ yield createAllSearchIndex();
105
108
  // replicate the template data to the instance DB
106
109
  // this is currently very hard to test, downloading and importing template files
107
110
  if (template && template.templateString) {
@@ -127,7 +130,7 @@ function createInstance(template) {
127
130
  exports.fetch = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
128
131
  const dev = ctx.query && ctx.query.status === AppStatus.DEV;
129
132
  const all = ctx.query && ctx.query.status === AppStatus.ALL;
130
- const apps = yield getAllApps(CouchDB, { dev, all });
133
+ const apps = yield getAllApps({ dev, all });
131
134
  // get the locks for all the dev apps
132
135
  if (dev || all) {
133
136
  const locks = yield getAllLocks();
@@ -148,11 +151,10 @@ exports.fetch = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
148
151
  ctx.body = apps;
149
152
  });
150
153
  exports.fetchAppDefinition = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
151
- const db = new CouchDB(ctx.params.appId);
152
- const layouts = yield getLayouts(db);
154
+ const layouts = yield getLayouts();
153
155
  const userRoleId = getUserRoleId(ctx);
154
- const accessController = new AccessController(ctx.params.appId);
155
- const screens = yield accessController.checkScreensAccess(yield getScreens(db), userRoleId);
156
+ const accessController = new AccessController();
157
+ const screens = yield accessController.checkScreensAccess(yield getScreens(), userRoleId);
156
158
  ctx.body = {
157
159
  layouts,
158
160
  screens,
@@ -160,14 +162,14 @@ exports.fetchAppDefinition = (ctx) => __awaiter(void 0, void 0, void 0, function
160
162
  };
161
163
  });
162
164
  exports.fetchAppPackage = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
163
- const db = new CouchDB(ctx.params.appId);
165
+ const db = getAppDB();
164
166
  const application = yield db.get(DocumentTypes.APP_METADATA);
165
- const layouts = yield getLayouts(db);
166
- let screens = yield getScreens(db);
167
+ const layouts = yield getLayouts();
168
+ let screens = yield getScreens();
167
169
  // Only filter screens if the user is not a builder
168
170
  if (!(ctx.user.builder && ctx.user.builder.global)) {
169
171
  const userRoleId = getUserRoleId(ctx);
170
- const accessController = new AccessController(ctx.params.appId);
172
+ const accessController = new AccessController();
171
173
  screens = yield accessController.checkScreensAccess(screens, userRoleId);
172
174
  }
173
175
  ctx.body = {
@@ -178,7 +180,7 @@ exports.fetchAppPackage = (ctx) => __awaiter(void 0, void 0, void 0, function* (
178
180
  };
179
181
  });
180
182
  exports.create = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
181
- const apps = yield getAllApps(CouchDB, { dev: true });
183
+ const apps = yield getAllApps({ dev: true });
182
184
  const name = ctx.request.body.name;
183
185
  checkAppName(ctx, apps, name);
184
186
  const url = exports.getAppUrl(ctx);
@@ -194,7 +196,7 @@ exports.create = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
194
196
  }
195
197
  const instance = yield createInstance(instanceConfig);
196
198
  const appId = instance._id;
197
- const db = new CouchDB(appId);
199
+ const db = getAppDB();
198
200
  let _rev;
199
201
  try {
200
202
  // if template there will be an existing doc
@@ -237,7 +239,7 @@ exports.create = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
237
239
  // This endpoint currently operates as a PATCH rather than a PUT
238
240
  // Thus name and url fields are handled only if present
239
241
  exports.update = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
240
- const apps = yield getAllApps(CouchDB, { dev: true });
242
+ const apps = yield getAllApps({ dev: true });
241
243
  // validation
242
244
  const name = ctx.request.body.name;
243
245
  if (name) {
@@ -254,7 +256,7 @@ exports.update = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
254
256
  });
255
257
  exports.updateClient = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
256
258
  // Get current app version
257
- const db = new CouchDB(ctx.params.appId);
259
+ const db = getAppDB();
258
260
  const application = yield db.get(DocumentTypes.APP_METADATA);
259
261
  const currentVersion = application.version;
260
262
  // Update client library and manifest
@@ -273,7 +275,7 @@ exports.updateClient = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
273
275
  });
274
276
  exports.revertClient = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
275
277
  // Check app can be reverted
276
- const db = new CouchDB(ctx.params.appId);
278
+ const db = getAppDB();
277
279
  const application = yield db.get(DocumentTypes.APP_METADATA);
278
280
  if (!application.revertableVersion) {
279
281
  ctx.throw(400, "There is no version to revert to");
@@ -292,7 +294,7 @@ exports.revertClient = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
292
294
  ctx.body = data;
293
295
  });
294
296
  exports.delete = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
295
- const db = new CouchDB(ctx.params.appId);
297
+ const db = getAppDB();
296
298
  const result = yield db.destroy();
297
299
  /* istanbul ignore next */
298
300
  if (!env.isTest() && !ctx.query.unpublish) {
@@ -313,9 +315,10 @@ exports.sync = (ctx, next) => __awaiter(void 0, void 0, void 0, function* () {
313
315
  ctx.throw(400, "This action cannot be performed for production apps");
314
316
  }
315
317
  // replicate prod to dev
316
- const prodAppId = getDeployedAppID(appId);
318
+ const prodAppId = getProdAppID(appId);
317
319
  try {
318
- const prodDb = new CouchDB(prodAppId, { skip_setup: true });
320
+ // specific case, want to make sure setup is skipped
321
+ const prodDb = getProdAppDB({ skip_setup: true });
319
322
  const info = yield prodDb.info();
320
323
  if (info.error)
321
324
  throw info.error;
@@ -344,7 +347,7 @@ exports.sync = (ctx, next) => __awaiter(void 0, void 0, void 0, function* () {
344
347
  error = err;
345
348
  }
346
349
  // sync the users
347
- yield syncGlobalUsers(appId);
350
+ yield syncGlobalUsers();
348
351
  if (error) {
349
352
  ctx.throw(400, error);
350
353
  }
@@ -355,7 +358,7 @@ exports.sync = (ctx, next) => __awaiter(void 0, void 0, void 0, function* () {
355
358
  }
356
359
  });
357
360
  const updateAppPackage = (appPackage, appId) => __awaiter(void 0, void 0, void 0, function* () {
358
- const db = new CouchDB(appId);
361
+ const db = getAppDB();
359
362
  const application = yield db.get(DocumentTypes.APP_METADATA);
360
363
  const newAppPackage = Object.assign(Object.assign({}, application), appPackage);
361
364
  if (appPackage._rev !== application._rev) {
@@ -370,7 +373,7 @@ const updateAppPackage = (appPackage, appId) => __awaiter(void 0, void 0, void 0
370
373
  return response;
371
374
  });
372
375
  const createEmptyAppPackage = (ctx, app) => __awaiter(void 0, void 0, void 0, function* () {
373
- const db = new CouchDB(app.appId);
376
+ const db = getAppDB();
374
377
  let screensAndLayouts = [];
375
378
  for (let layout of BASE_LAYOUTS) {
376
379
  const cloned = cloneDeep(layout);
@@ -8,13 +8,12 @@ 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 { outputProcessing } = require("../../utilities/rowProcessor");
13
12
  const { InternalTables } = require("../../db/utils");
14
13
  const { getFullUser } = require("../../utilities/users");
15
14
  const { BUILTIN_ROLE_IDS } = require("@budibase/backend-core/roles");
15
+ const { getAppDB, getAppId } = require("@budibase/backend-core/context");
16
16
  exports.fetchSelf = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
17
- const appId = ctx.appId;
18
17
  let userId = ctx.user.userId || ctx.user._id;
19
18
  /* istanbul ignore next */
20
19
  if (!userId) {
@@ -26,8 +25,8 @@ exports.fetchSelf = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
26
25
  delete user.roles;
27
26
  // forward the csrf token from the session
28
27
  user.csrfToken = ctx.user.csrfToken;
29
- if (appId) {
30
- const db = new CouchDB(appId);
28
+ if (getAppId()) {
29
+ const db = getAppDB();
31
30
  // remove the full roles structure
32
31
  delete user.roles;
33
32
  try {
@@ -36,7 +35,7 @@ exports.fetchSelf = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
36
35
  // make sure there is never a stale csrf token
37
36
  delete metadata.csrfToken;
38
37
  // specifically needs to make sure is enriched
39
- ctx.body = yield outputProcessing(ctx, userTable, Object.assign(Object.assign({}, user), metadata));
38
+ ctx.body = yield outputProcessing(userTable, Object.assign(Object.assign({}, user), metadata));
40
39
  }
41
40
  catch (err) {
42
41
  let response;
@@ -8,7 +8,6 @@ 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 actions = require("../../automations/actions");
13
12
  const triggers = require("../../automations/triggers");
14
13
  const { getAutomationParams, generateAutomationID } = require("../../db/utils");
@@ -16,6 +15,7 @@ const { checkForWebhooks, updateTestHistory, removeDeprecated, } = require("../.
16
15
  const { deleteEntityMetadata } = require("../../utilities");
17
16
  const { MetadataTypes } = require("../../constants");
18
17
  const { setTestFlag, clearTestFlag } = require("../../utilities/redis");
18
+ const { getAppDB } = require("@budibase/backend-core/context");
19
19
  const ACTION_DEFS = removeDeprecated(actions.ACTION_DEFINITIONS);
20
20
  const TRIGGER_DEFS = removeDeprecated(triggers.TRIGGER_DEFINITIONS);
21
21
  /*************************
@@ -23,10 +23,10 @@ const TRIGGER_DEFS = removeDeprecated(triggers.TRIGGER_DEFINITIONS);
23
23
  * BUILDER FUNCTIONS *
24
24
  * *
25
25
  *************************/
26
- function cleanupAutomationMetadata(appId, automationId) {
26
+ function cleanupAutomationMetadata(automationId) {
27
27
  return __awaiter(this, void 0, void 0, function* () {
28
- yield deleteEntityMetadata(appId, MetadataTypes.AUTOMATION_TEST_INPUT, automationId);
29
- yield deleteEntityMetadata(appId, MetadataTypes.AUTOMATION_TEST_HISTORY, automationId);
28
+ yield deleteEntityMetadata(MetadataTypes.AUTOMATION_TEST_INPUT, automationId);
29
+ yield deleteEntityMetadata(MetadataTypes.AUTOMATION_TEST_HISTORY, automationId);
30
30
  });
31
31
  }
32
32
  function cleanAutomationInputs(automation) {
@@ -54,7 +54,7 @@ function cleanAutomationInputs(automation) {
54
54
  }
55
55
  exports.create = function (ctx) {
56
56
  return __awaiter(this, void 0, void 0, function* () {
57
- const db = new CouchDB(ctx.appId);
57
+ const db = getAppDB();
58
58
  let automation = ctx.request.body;
59
59
  automation.appId = ctx.appId;
60
60
  // call through to update if already exists
@@ -65,7 +65,6 @@ exports.create = function (ctx) {
65
65
  automation.type = "automation";
66
66
  automation = cleanAutomationInputs(automation);
67
67
  automation = yield checkForWebhooks({
68
- appId: ctx.appId,
69
68
  newAuto: automation,
70
69
  });
71
70
  const response = yield db.put(automation);
@@ -79,13 +78,12 @@ exports.create = function (ctx) {
79
78
  };
80
79
  exports.update = function (ctx) {
81
80
  return __awaiter(this, void 0, void 0, function* () {
82
- const db = new CouchDB(ctx.appId);
81
+ const db = getAppDB();
83
82
  let automation = ctx.request.body;
84
83
  automation.appId = ctx.appId;
85
84
  const oldAutomation = yield db.get(automation._id);
86
85
  automation = cleanAutomationInputs(automation);
87
86
  automation = yield checkForWebhooks({
88
- appId: ctx.appId,
89
87
  oldAuto: oldAutomation,
90
88
  newAuto: automation,
91
89
  });
@@ -110,7 +108,7 @@ exports.update = function (ctx) {
110
108
  };
111
109
  exports.fetch = function (ctx) {
112
110
  return __awaiter(this, void 0, void 0, function* () {
113
- const db = new CouchDB(ctx.appId);
111
+ const db = getAppDB();
114
112
  const response = yield db.allDocs(getAutomationParams(null, {
115
113
  include_docs: true,
116
114
  }));
@@ -119,21 +117,20 @@ exports.fetch = function (ctx) {
119
117
  };
120
118
  exports.find = function (ctx) {
121
119
  return __awaiter(this, void 0, void 0, function* () {
122
- const db = new CouchDB(ctx.appId);
120
+ const db = getAppDB();
123
121
  ctx.body = yield db.get(ctx.params.id);
124
122
  });
125
123
  };
126
124
  exports.destroy = function (ctx) {
127
125
  return __awaiter(this, void 0, void 0, function* () {
128
- const db = new CouchDB(ctx.appId);
126
+ const db = getAppDB();
129
127
  const automationId = ctx.params.id;
130
128
  const oldAutomation = yield db.get(automationId);
131
129
  yield checkForWebhooks({
132
- appId: ctx.appId,
133
130
  oldAuto: oldAutomation,
134
131
  });
135
132
  // delete metadata first
136
- yield cleanupAutomationMetadata(ctx.appId, automationId);
133
+ yield cleanupAutomationMetadata(automationId);
137
134
  ctx.body = yield db.remove(automationId, ctx.params.rev);
138
135
  });
139
136
  };
@@ -162,10 +159,9 @@ module.exports.getDefinitionList = function (ctx) {
162
159
  *********************/
163
160
  exports.trigger = function (ctx) {
164
161
  return __awaiter(this, void 0, void 0, function* () {
165
- const appId = ctx.appId;
166
- const db = new CouchDB(appId);
162
+ const db = getAppDB();
167
163
  let automation = yield db.get(ctx.params.id);
168
- yield triggers.externalTrigger(automation, Object.assign(Object.assign({}, ctx.request.body), { appId }));
164
+ yield triggers.externalTrigger(automation, Object.assign(Object.assign({}, ctx.request.body), { appId: ctx.appId }));
169
165
  ctx.body = {
170
166
  message: `Automation ${automation._id} has been triggered.`,
171
167
  automation,
@@ -184,12 +180,11 @@ function prepareTestInput(input) {
184
180
  }
185
181
  exports.test = function (ctx) {
186
182
  return __awaiter(this, void 0, void 0, function* () {
187
- const appId = ctx.appId;
188
- const db = new CouchDB(appId);
183
+ const db = getAppDB();
189
184
  let automation = yield db.get(ctx.params.id);
190
185
  yield setTestFlag(automation._id);
191
186
  const testInput = prepareTestInput(ctx.request.body);
192
- const response = yield triggers.externalTrigger(automation, Object.assign(Object.assign({}, testInput), { appId }), { getResponses: true });
187
+ const response = yield triggers.externalTrigger(automation, Object.assign(Object.assign({}, testInput), { appId: ctx.appId }), { getResponses: true });
193
188
  // save a test history run
194
189
  yield updateTestHistory(ctx.appId, automation, Object.assign(Object.assign({}, ctx.request.body), { occurredAt: new Date().getTime() }));
195
190
  yield clearTestFlag(automation._id);
@@ -10,7 +10,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  const env = require("../../environment");
12
12
  const { getAllApps } = require("@budibase/backend-core/db");
13
- const CouchDB = require("../../db");
14
13
  const { exportDB, sendTempFile, readFileSync, } = require("../../utilities/fileSystem");
15
14
  const { stringToReadStream } = require("../../utilities");
16
15
  const { getGlobalDBName, getGlobalDB, } = require("@budibase/backend-core/tenancy");
@@ -33,7 +32,7 @@ exports.exportApps = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
33
32
  if (env.SELF_HOSTED || !env.MULTI_TENANCY) {
34
33
  ctx.throw(400, "Exporting only allowed in multi-tenant cloud environments.");
35
34
  }
36
- const apps = yield getAllApps(CouchDB, { all: true });
35
+ const apps = yield getAllApps({ all: true });
37
36
  const globalDBString = yield exportDB(getGlobalDBName(), {
38
37
  filter: doc => !doc._id.startsWith(DocumentTypes.USER),
39
38
  });
@@ -65,7 +64,7 @@ function hasBeenImported() {
65
64
  if (!env.SELF_HOSTED || env.MULTI_TENANCY) {
66
65
  return true;
67
66
  }
68
- const apps = yield getAllApps(CouchDB, { all: true });
67
+ const apps = yield getAllApps({ all: true });
69
68
  return apps.length !== 0;
70
69
  });
71
70
  }
@@ -8,16 +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 { DocumentTypes } = require("../../db/utils");
13
12
  const { getComponentLibraryManifest } = require("../../utilities/fileSystem");
13
+ const { getAppDB } = require("@budibase/backend-core/context");
14
14
  exports.fetchAppComponentDefinitions = function (ctx) {
15
15
  return __awaiter(this, void 0, void 0, function* () {
16
- const appId = ctx.params.appId || ctx.appId;
17
- const db = new CouchDB(appId);
16
+ const db = getAppDB();
18
17
  const app = yield db.get(DocumentTypes.APP_METADATA);
19
18
  let componentManifests = yield Promise.all(app.componentLibraries.map((library) => __awaiter(this, void 0, void 0, function* () {
20
- let manifest = yield getComponentLibraryManifest(appId, library);
19
+ let manifest = yield getComponentLibraryManifest(library);
21
20
  return {
22
21
  manifest,
23
22
  library,
@@ -8,24 +8,23 @@ 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 { generateDatasourceID, getDatasourceParams, getQueryParams, DocumentTypes, BudibaseInternalDB, getTableParams, } = require("../../db/utils");
13
12
  const { BuildSchemaErrors, InvalidColumns } = require("../../constants");
14
13
  const { integrations } = require("../../integrations");
15
14
  const { getDatasourceAndQuery } = require("./row/utils");
16
15
  const { invalidateDynamicVariables } = require("../../threads/utils");
16
+ const { getAppDB } = require("@budibase/backend-core/context");
17
17
  exports.fetch = function (ctx) {
18
18
  return __awaiter(this, void 0, void 0, function* () {
19
- const database = new CouchDB(ctx.appId);
20
19
  // Get internal tables
21
- const db = new CouchDB(ctx.appId);
20
+ const db = getAppDB();
22
21
  const internalTables = yield db.allDocs(getTableParams(null, {
23
22
  include_docs: true,
24
23
  }));
25
24
  const internal = internalTables.rows.map(row => row.doc);
26
25
  const bbInternalDb = Object.assign(Object.assign({}, BudibaseInternalDB), { entities: internal });
27
26
  // Get external datasources
28
- const datasources = (yield database.allDocs(getDatasourceParams(null, {
27
+ const datasources = (yield db.allDocs(getDatasourceParams(null, {
29
28
  include_docs: true,
30
29
  }))).rows.map(row => row.doc);
31
30
  for (let datasource of datasources) {
@@ -39,7 +38,7 @@ exports.fetch = function (ctx) {
39
38
  };
40
39
  exports.buildSchemaFromDb = function (ctx) {
41
40
  return __awaiter(this, void 0, void 0, function* () {
42
- const db = new CouchDB(ctx.appId);
41
+ const db = getAppDB();
43
42
  const datasource = yield db.get(ctx.params.datasourceId);
44
43
  const { tables, error } = yield buildSchemaHelper(datasource);
45
44
  datasource.entities = tables;
@@ -81,7 +80,7 @@ const invalidateVariables = (existingDatasource, updatedDatasource) => __awaiter
81
80
  });
82
81
  exports.update = function (ctx) {
83
82
  return __awaiter(this, void 0, void 0, function* () {
84
- const db = new CouchDB(ctx.appId);
83
+ const db = getAppDB();
85
84
  const datasourceId = ctx.params.datasourceId;
86
85
  let datasource = yield db.get(datasourceId);
87
86
  const auth = datasource.config.auth;
@@ -107,7 +106,7 @@ exports.update = function (ctx) {
107
106
  };
108
107
  exports.save = function (ctx) {
109
108
  return __awaiter(this, void 0, void 0, function* () {
110
- const db = new CouchDB(ctx.appId);
109
+ const db = getAppDB();
111
110
  const plus = ctx.request.body.datasource.plus;
112
111
  const fetchSchema = ctx.request.body.fetchSchema;
113
112
  const datasource = Object.assign({ _id: generateDatasourceID({ plus }), type: plus ? DocumentTypes.DATASOURCE_PLUS : DocumentTypes.DATASOURCE }, ctx.request.body.datasource);
@@ -135,7 +134,7 @@ exports.save = function (ctx) {
135
134
  };
136
135
  exports.destroy = function (ctx) {
137
136
  return __awaiter(this, void 0, void 0, function* () {
138
- const db = new CouchDB(ctx.appId);
137
+ const db = getAppDB();
139
138
  // Delete all queries for the datasource
140
139
  const queries = yield db.allDocs(getQueryParams(ctx.params.datasourceId, null));
141
140
  yield db.bulkDocs(queries.rows.map(row => ({
@@ -151,7 +150,7 @@ exports.destroy = function (ctx) {
151
150
  };
152
151
  exports.find = function (ctx) {
153
152
  return __awaiter(this, void 0, void 0, function* () {
154
- const database = new CouchDB(ctx.appId);
153
+ const database = getAppDB();
155
154
  ctx.body = yield database.get(ctx.params.datasourceId);
156
155
  });
157
156
  };
@@ -160,7 +159,7 @@ exports.query = function (ctx) {
160
159
  return __awaiter(this, void 0, void 0, function* () {
161
160
  const queryJson = ctx.request.body;
162
161
  try {
163
- ctx.body = yield getDatasourceAndQuery(ctx.appId, queryJson);
162
+ ctx.body = yield getDatasourceAndQuery(queryJson);
164
163
  }
165
164
  catch (err) {
166
165
  ctx.throw(400, err);
@@ -1,16 +1,13 @@
1
1
  "use strict";
2
2
  const newid = require("../../../db/newid");
3
+ const { getAppId } = require("@budibase/backend-core/context");
3
4
  /**
4
5
  * This is used to pass around information about the deployment that is occurring
5
6
  */
6
7
  class Deployment {
7
- constructor(appId, id = null) {
8
- this.appId = appId;
8
+ constructor(id = null) {
9
9
  this._id = id || newid();
10
10
  }
11
- getAppId() {
12
- return this.appId;
13
- }
14
11
  setVerification(verification) {
15
12
  if (!verification) {
16
13
  return;
@@ -37,7 +34,7 @@ class Deployment {
37
34
  getJSON() {
38
35
  const obj = {
39
36
  _id: this._id,
40
- appId: this.appId,
37
+ appId: getAppId(),
41
38
  status: this.status,
42
39
  };
43
40
  if (this.err) {
@@ -8,12 +8,12 @@ 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 Deployment = require("./Deployment");
13
- const { Replication, getDeployedAppID } = require("@budibase/backend-core/db");
12
+ const { Replication, getProdAppID, getDevelopmentAppID, } = require("@budibase/backend-core/db");
14
13
  const { DocumentTypes, getAutomationParams } = require("../../../db/utils");
15
14
  const { disableAllCrons, enableCronTrigger, } = require("../../../automations/utils");
16
15
  const { app: appCache } = require("@budibase/backend-core/cache");
16
+ const { getAppId, getAppDB, getProdAppDB, } = require("@budibase/backend-core/context");
17
17
  // the max time we can wait for an invalidation to complete before considering it failed
18
18
  const MAX_PENDING_TIME_MS = 30 * 60000;
19
19
  const DeploymentStatus = {
@@ -39,9 +39,8 @@ function checkAllDeployments(deployments) {
39
39
  }
40
40
  function storeDeploymentHistory(deployment) {
41
41
  return __awaiter(this, void 0, void 0, function* () {
42
- const appId = deployment.getAppId();
43
42
  const deploymentJSON = deployment.getJSON();
44
- const db = new CouchDB(appId);
43
+ const db = getAppDB();
45
44
  let deploymentDoc;
46
45
  try {
47
46
  // theres only one deployment doc per app database
@@ -62,7 +61,7 @@ function storeDeploymentHistory(deployment) {
62
61
  }
63
62
  function initDeployedApp(prodAppId) {
64
63
  return __awaiter(this, void 0, void 0, function* () {
65
- const db = new CouchDB(prodAppId);
64
+ const db = getProdAppDB();
66
65
  console.log("Reading automation docs");
67
66
  const automations = (yield db.allDocs(getAutomationParams(null, {
68
67
  include_docs: true,
@@ -83,15 +82,17 @@ function initDeployedApp(prodAppId) {
83
82
  function deployApp(deployment) {
84
83
  return __awaiter(this, void 0, void 0, function* () {
85
84
  try {
86
- const productionAppId = getDeployedAppID(deployment.appId);
85
+ const appId = getAppId();
86
+ const devAppId = getDevelopmentAppID(appId);
87
+ const productionAppId = getProdAppID(appId);
87
88
  const replication = new Replication({
88
- source: deployment.appId,
89
+ source: devAppId,
89
90
  target: productionAppId,
90
91
  });
91
92
  console.log("Replication object created");
92
93
  yield replication.replicate();
93
94
  console.log("replication complete.. replacing app meta doc");
94
- const db = new CouchDB(productionAppId);
95
+ const db = getProdAppDB();
95
96
  const appDoc = yield db.get(DocumentTypes.APP_METADATA);
96
97
  appDoc.appId = productionAppId;
97
98
  appDoc.instance._id = productionAppId;
@@ -113,8 +114,7 @@ function deployApp(deployment) {
113
114
  exports.fetchDeployments = function (ctx) {
114
115
  return __awaiter(this, void 0, void 0, function* () {
115
116
  try {
116
- const appId = ctx.appId;
117
- const db = new CouchDB(appId);
117
+ const db = getAppDB();
118
118
  const deploymentDoc = yield db.get(DocumentTypes.DEPLOYMENTS);
119
119
  const { updated, deployments } = yield checkAllDeployments(deploymentDoc, ctx.user);
120
120
  if (updated) {
@@ -130,8 +130,7 @@ exports.fetchDeployments = function (ctx) {
130
130
  exports.deploymentProgress = function (ctx) {
131
131
  return __awaiter(this, void 0, void 0, function* () {
132
132
  try {
133
- const appId = ctx.appId;
134
- const db = new CouchDB(appId);
133
+ const db = getAppDB();
135
134
  const deploymentDoc = yield db.get(DocumentTypes.DEPLOYMENTS);
136
135
  ctx.body = deploymentDoc[ctx.params.deploymentId];
137
136
  }
@@ -142,7 +141,7 @@ exports.deploymentProgress = function (ctx) {
142
141
  };
143
142
  exports.deployApp = function (ctx) {
144
143
  return __awaiter(this, void 0, void 0, function* () {
145
- let deployment = new Deployment(ctx.appId);
144
+ let deployment = new Deployment();
146
145
  console.log("Deployment object created");
147
146
  deployment.setStatus(DeploymentStatus.PENDING);
148
147
  console.log("Deployment object set to pending");
@@ -9,14 +9,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  const fetch = require("node-fetch");
12
- const CouchDB = require("../../db");
13
12
  const env = require("../../environment");
14
13
  const { checkSlashesInUrl } = require("../../utilities");
15
14
  const { request } = require("../../utilities/workerRequests");
16
15
  const { clearLock } = require("../../utilities/redis");
17
- const { Replication } = require("@budibase/backend-core/db");
16
+ const { Replication, getProdAppID } = require("@budibase/backend-core/db");
18
17
  const { DocumentTypes } = require("../../db/utils");
19
18
  const { app: appCache } = require("@budibase/backend-core/cache");
19
+ const { getProdAppDB, getAppDB } = require("@budibase/backend-core/context");
20
20
  function redirect(ctx, method, path = "global") {
21
21
  return __awaiter(this, void 0, void 0, function* () {
22
22
  const { devPath } = ctx.params;
@@ -76,10 +76,10 @@ exports.clearLock = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
76
76
  });
77
77
  exports.revert = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
78
78
  const { appId } = ctx.params;
79
- const productionAppId = appId.replace("_dev", "");
79
+ const productionAppId = getProdAppID(appId);
80
80
  // App must have been deployed first
81
81
  try {
82
- const db = new CouchDB(productionAppId, { skip_setup: true });
82
+ const db = getProdAppDB({ skip_setup: true });
83
83
  const info = yield db.info();
84
84
  if (info.error)
85
85
  throw info.error;
@@ -99,7 +99,7 @@ exports.revert = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
99
99
  });
100
100
  yield replication.rollback();
101
101
  // update appID in reverted app to be dev version again
102
- const db = new CouchDB(appId);
102
+ const db = getAppDB();
103
103
  const appDoc = yield db.get(DocumentTypes.APP_METADATA);
104
104
  appDoc.appId = appId;
105
105
  appDoc.instance._id = appId;
@@ -9,11 +9,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  const { EMPTY_LAYOUT, BASE_LAYOUT_PROP_IDS, } = require("../../constants/layouts");
12
- const CouchDB = require("../../db");
13
12
  const { generateLayoutID, getScreenParams } = require("../../db/utils");
13
+ const { getAppDB } = require("@budibase/backend-core/context");
14
14
  exports.save = function (ctx) {
15
15
  return __awaiter(this, void 0, void 0, function* () {
16
- const db = new CouchDB(ctx.appId);
16
+ const db = getAppDB();
17
17
  let layout = ctx.request.body;
18
18
  if (!layout.props) {
19
19
  layout = Object.assign(Object.assign({}, EMPTY_LAYOUT), layout);
@@ -28,7 +28,7 @@ exports.save = function (ctx) {
28
28
  };
29
29
  exports.destroy = function (ctx) {
30
30
  return __awaiter(this, void 0, void 0, function* () {
31
- const db = new CouchDB(ctx.appId);
31
+ const db = getAppDB();
32
32
  const layoutId = ctx.params.layoutId, layoutRev = ctx.params.layoutRev;
33
33
  if (Object.values(BASE_LAYOUT_PROP_IDS).includes(layoutId)) {
34
34
  ctx.throw(400, "Cannot delete a built-in layout");