@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
@@ -11,9 +11,9 @@ const { generateUserMetadataID, isDevAppID } = require("../db/utils")
11
11
  const { dbExists } = require("@budibase/backend-core/db")
12
12
  const { isUserInAppTenant } = require("@budibase/backend-core/tenancy")
13
13
  const { getCachedSelf } = require("../utilities/global")
14
- const CouchDB = require("../db")
15
14
  const env = require("../environment")
16
15
  const { isWebhookEndpoint } = require("./utils")
16
+ const { doInAppContext } = require("@budibase/backend-core/context")
17
17
 
18
18
  module.exports = async (ctx, next) => {
19
19
  // try to get the appID from the request
@@ -31,7 +31,7 @@ module.exports = async (ctx, next) => {
31
31
  // check the app exists referenced in cookie
32
32
  if (appCookie) {
33
33
  const appId = appCookie.appId
34
- const exists = await dbExists(CouchDB, appId)
34
+ const exists = await dbExists(appId)
35
35
  if (!exists) {
36
36
  clearCookie(ctx, Cookies.CurrentApp)
37
37
  return next()
@@ -41,13 +41,15 @@ module.exports = async (ctx, next) => {
41
41
  }
42
42
 
43
43
  // deny access to application preview
44
- if (
45
- isDevAppID(requestAppId) &&
46
- !isWebhookEndpoint(ctx) &&
47
- (!ctx.user || !ctx.user.builder || !ctx.user.builder.global)
48
- ) {
49
- clearCookie(ctx, Cookies.CurrentApp)
50
- return ctx.redirect("/")
44
+ if (!env.isTest()) {
45
+ if (
46
+ isDevAppID(requestAppId) &&
47
+ !isWebhookEndpoint(ctx) &&
48
+ (!ctx.user || !ctx.user.builder || !ctx.user.builder.global)
49
+ ) {
50
+ clearCookie(ctx, Cookies.CurrentApp)
51
+ return ctx.redirect("/")
52
+ }
51
53
  }
52
54
 
53
55
  let appId,
@@ -68,44 +70,46 @@ module.exports = async (ctx, next) => {
68
70
  return next()
69
71
  }
70
72
 
71
- let noCookieSet = false
72
- // if the user not in the right tenant then make sure they have no permissions
73
- // need to judge this only based on the request app ID,
74
- if (
75
- env.MULTI_TENANCY &&
76
- ctx.user &&
77
- requestAppId &&
78
- !isUserInAppTenant(requestAppId)
79
- ) {
80
- // don't error, simply remove the users rights (they are a public user)
81
- delete ctx.user.builder
82
- delete ctx.user.admin
83
- delete ctx.user.roles
84
- roleId = BUILTIN_ROLE_IDS.PUBLIC
85
- noCookieSet = true
86
- }
73
+ return doInAppContext(appId, async () => {
74
+ let noCookieSet = false
75
+ // if the user not in the right tenant then make sure they have no permissions
76
+ // need to judge this only based on the request app ID,
77
+ if (
78
+ env.MULTI_TENANCY &&
79
+ ctx.user &&
80
+ requestAppId &&
81
+ !isUserInAppTenant(requestAppId)
82
+ ) {
83
+ // don't error, simply remove the users rights (they are a public user)
84
+ delete ctx.user.builder
85
+ delete ctx.user.admin
86
+ delete ctx.user.roles
87
+ roleId = BUILTIN_ROLE_IDS.PUBLIC
88
+ noCookieSet = true
89
+ }
87
90
 
88
- ctx.appId = appId
89
- if (roleId) {
90
- ctx.roleId = roleId
91
- const userId = ctx.user ? generateUserMetadataID(ctx.user._id) : null
92
- ctx.user = {
93
- ...ctx.user,
94
- // override userID with metadata one
95
- _id: userId,
96
- userId,
97
- roleId,
98
- role: await getRole(appId, roleId),
91
+ ctx.appId = appId
92
+ if (roleId) {
93
+ ctx.roleId = roleId
94
+ const userId = ctx.user ? generateUserMetadataID(ctx.user._id) : null
95
+ ctx.user = {
96
+ ...ctx.user,
97
+ // override userID with metadata one
98
+ _id: userId,
99
+ userId,
100
+ roleId,
101
+ role: await getRole(roleId),
102
+ }
103
+ }
104
+ if (
105
+ (requestAppId !== appId ||
106
+ appCookie == null ||
107
+ appCookie.appId !== requestAppId) &&
108
+ !noCookieSet
109
+ ) {
110
+ setCookie(ctx, { appId }, Cookies.CurrentApp)
99
111
  }
100
- }
101
- if (
102
- (requestAppId !== appId ||
103
- appCookie == null ||
104
- appCookie.appId !== requestAppId) &&
105
- !noCookieSet
106
- ) {
107
- setCookie(ctx, { appId }, Cookies.CurrentApp)
108
- }
109
112
 
110
- return next()
113
+ return next()
114
+ })
111
115
  }
@@ -11,6 +11,9 @@ const authorizedMiddleware = require("../authorized")
11
11
  const env = require("../../environment")
12
12
  const { PermissionTypes, PermissionLevels } = require("@budibase/backend-core/permissions")
13
13
  require("@budibase/backend-core").init(require("../../db"))
14
+ const { doInAppContext } = require("@budibase/backend-core/context")
15
+
16
+ const APP_ID = ""
14
17
 
15
18
  class TestConfiguration {
16
19
  constructor(role) {
@@ -23,7 +26,7 @@ class TestConfiguration {
23
26
  request: {
24
27
  url: ""
25
28
  },
26
- appId: "",
29
+ appId: APP_ID,
27
30
  auth: {},
28
31
  next: this.next,
29
32
  throw: this.throw,
@@ -32,7 +35,9 @@ class TestConfiguration {
32
35
  }
33
36
 
34
37
  executeMiddleware() {
35
- return this.middleware(this.ctx, this.next)
38
+ return doInAppContext(APP_ID, () => {
39
+ return this.middleware(this.ctx, this.next)
40
+ })
36
41
  }
37
42
 
38
43
  setUser(user) {
@@ -1,6 +1,11 @@
1
1
  mockAuthWithNoCookie()
2
2
  mockWorker()
3
3
 
4
+ jest.mock("@budibase/backend-core/db", () => ({
5
+ ...jest.requireActual("@budibase/backend-core/db"),
6
+ dbExists: () => true,
7
+ }))
8
+
4
9
  function mockWorker() {
5
10
  jest.mock("../../utilities/workerRequests", () => ({
6
11
  getGlobalSelf: () => {
@@ -50,6 +55,7 @@ function mockAuthWithCookie() {
50
55
  return "app_test"
51
56
  },
52
57
  setCookie: jest.fn(),
58
+ clearCookie: jest.fn(),
53
59
  getCookie: () => ({appId: "app_different", roleId: "PUBLIC"}),
54
60
  }))
55
61
  jest.mock("@budibase/backend-core/constants", () => ({
@@ -1,10 +1,10 @@
1
- const CouchDB = require("../db")
2
1
  const usageQuota = require("../utilities/usageQuota")
3
2
  const { getUniqueRows } = require("../utilities/usageQuota/rows")
4
3
  const {
5
4
  isExternalTable,
6
5
  isRowId: isExternalRowId,
7
6
  } = require("../integrations/utils")
7
+ const { getAppDB } = require("@budibase/backend-core/context")
8
8
 
9
9
  // currently only counting new writes and deletes
10
10
  const METHOD_MAP = {
@@ -46,7 +46,7 @@ module.exports = async (ctx, next) => {
46
46
  const usageId = ctx.request.body._id
47
47
  try {
48
48
  if (ctx.appId) {
49
- const db = new CouchDB(ctx.appId)
49
+ const db = getAppDB()
50
50
  await db.get(usageId)
51
51
  }
52
52
  return next()
@@ -9,7 +9,16 @@ import { getAppUrl } from "../../api/controllers/application"
9
9
  * Add the url to the app metadata if it doesn't exist
10
10
  */
11
11
  export const run = async (appDb: any) => {
12
- const metadata = await appDb.get(DocumentTypes.APP_METADATA)
12
+ let metadata
13
+ try {
14
+ metadata = await appDb.get(DocumentTypes.APP_METADATA)
15
+ } catch (e) {
16
+ // sometimes the metadata document doesn't exist
17
+ // exit early instead of failing the migration
18
+ console.error("Error retrieving app metadata. Skipping", e)
19
+ return
20
+ }
21
+
13
22
  if (!metadata.url) {
14
23
  const context = {
15
24
  request: {
@@ -20,6 +29,6 @@ export const run = async (appDb: any) => {
20
29
  }
21
30
  metadata.url = getAppUrl(context)
22
31
  console.log(`Adding url to app: ${metadata.url}`)
32
+ await appDb.put(metadata)
23
33
  }
24
- await appDb.put(metadata)
25
34
  }
@@ -1,12 +1,13 @@
1
- const { getGlobalDB, getTenantId } = require("@budibase/backend-core/tenancy")
2
- const { getAllApps } = require("@budibase/backend-core/db")
3
- import CouchDB from "../../../db"
1
+ // @ts-ignore
2
+ import { getGlobalDB, getTenantId } from "@budibase/backend-core/tenancy"
3
+ // @ts-ignore
4
+ import { getAllApps } from "@budibase/backend-core/db"
4
5
  import { getUsageQuotaDoc } from "../../../utilities/usageQuota"
5
6
 
6
7
  export const run = async () => {
7
8
  const db = getGlobalDB()
8
9
  // get app count
9
- const devApps = await getAllApps(CouchDB, { dev: true })
10
+ const devApps = await getAllApps({ dev: true })
10
11
  const appCount = devApps ? devApps.length : 0
11
12
 
12
13
  // sync app count
@@ -1,13 +1,14 @@
1
- const { getGlobalDB, getTenantId } = require("@budibase/backend-core/tenancy")
2
- const { getAllApps } = require("@budibase/backend-core/db")
3
- import CouchDB from "../../../db"
1
+ // @ts-ignore
2
+ import { getGlobalDB, getTenantId } from "@budibase/backend-core/tenancy"
3
+ // @ts-ignore
4
+ import { getAllApps } from "@budibase/backend-core/db"
4
5
  import { getUsageQuotaDoc } from "../../../utilities/usageQuota"
5
6
  import { getUniqueRows } from "../../../utilities/usageQuota/rows"
6
7
 
7
8
  export const run = async () => {
8
9
  const db = getGlobalDB()
9
10
  // get all rows in all apps
10
- const allApps = await getAllApps(CouchDB, { all: true })
11
+ const allApps = await getAllApps({ all: true })
11
12
  const appIds = allApps ? allApps.map((app: { appId: any }) => app.appId) : []
12
13
  const rows = await getUniqueRows(appIds)
13
14
  const rowCount = rows ? rows.length : 0
@@ -1,3 +1,6 @@
1
+ const core = require("@budibase/backend-core")
2
+ const CouchDB = require("../../db")
3
+ core.init(CouchDB)
1
4
  const { BUILTIN_ROLE_IDS } = require("@budibase/backend-core/roles")
2
5
  const env = require("../../environment")
3
6
  const {
@@ -17,13 +20,11 @@ const supertest = require("supertest")
17
20
  const { cleanup } = require("../../utilities/fileSystem")
18
21
  const { Cookies, Headers } = require("@budibase/backend-core/constants")
19
22
  const { jwt } = require("@budibase/backend-core/auth")
20
- const core = require("@budibase/backend-core")
21
23
  const { getGlobalDB } = require("@budibase/backend-core/tenancy")
22
24
  const { createASession } = require("@budibase/backend-core/sessions")
23
25
  const { user: userCache } = require("@budibase/backend-core/cache")
24
- const CouchDB = require("../../db")
25
26
  const newid = require("../../db/newid")
26
- core.init(CouchDB)
27
+ const context = require("@budibase/backend-core/context")
27
28
 
28
29
  const GLOBAL_USER_ID = "us_uuid1"
29
30
  const EMAIL = "babs@babs.com"
@@ -65,11 +66,21 @@ class TestConfiguration {
65
66
  request.request = {
66
67
  body: config,
67
68
  }
68
- if (params) {
69
- request.params = params
69
+ async function run() {
70
+ if (params) {
71
+ request.params = params
72
+ }
73
+ await controlFunc(request)
74
+ return request.body
75
+ }
76
+ // check if already in a context
77
+ if (context.getAppId() == null) {
78
+ return context.doInAppContext(this.appId, async () => {
79
+ return run()
80
+ })
81
+ } else {
82
+ return run()
70
83
  }
71
- await controlFunc(request)
72
- return request.body
73
84
  }
74
85
 
75
86
  async globalUser({
@@ -175,6 +186,7 @@ class TestConfiguration {
175
186
  // create dev app
176
187
  this.app = await this._req({ name: appName }, null, controllers.app.create)
177
188
  this.appId = this.app.appId
189
+ context.updateAppId(this.appId)
178
190
 
179
191
  // create production app
180
192
  this.prodApp = await this.deploy()
@@ -187,14 +199,16 @@ class TestConfiguration {
187
199
  }
188
200
 
189
201
  async deploy() {
190
- const deployment = await this._req(null, null, controllers.deploy.deployApp)
191
- const prodAppId = deployment.appId.replace("_dev", "")
192
- const appPackage = await this._req(
193
- null,
194
- { appId: prodAppId },
195
- controllers.app.fetchAppPackage
196
- )
197
- return appPackage.application
202
+ await this._req(null, null, controllers.deploy.deployApp)
203
+ const prodAppId = this.getAppId().replace("_dev", "")
204
+ return context.doInAppContext(prodAppId, async () => {
205
+ const appPackage = await this._req(
206
+ null,
207
+ { appId: prodAppId },
208
+ controllers.app.fetchAppPackage
209
+ )
210
+ return appPackage.application
211
+ })
198
212
  }
199
213
 
200
214
  async updateTable(config = null) {
@@ -423,42 +437,47 @@ class TestConfiguration {
423
437
 
424
438
  async login({ roleId, userId, builder, prodApp = false } = {}) {
425
439
  const appId = prodApp ? this.prodAppId : this.appId
426
-
427
- userId = !userId ? `us_uuid1` : userId
428
- if (!this.request) {
429
- throw "Server has not been opened, cannot login."
430
- }
431
- // make sure the user exists in the global DB
432
- if (roleId !== BUILTIN_ROLE_IDS.PUBLIC) {
433
- await this.globalUser({
434
- userId,
435
- builder,
436
- roles: { [this.prodAppId]: roleId },
440
+ return context.doInAppContext(appId, async () => {
441
+ userId = !userId ? `us_uuid1` : userId
442
+ if (!this.request) {
443
+ throw "Server has not been opened, cannot login."
444
+ }
445
+ // make sure the user exists in the global DB
446
+ if (roleId !== BUILTIN_ROLE_IDS.PUBLIC) {
447
+ await this.globalUser({
448
+ id: userId,
449
+ builder,
450
+ roles: { [this.prodAppId]: roleId },
451
+ })
452
+ }
453
+ await createASession(userId, {
454
+ sessionId: "sessionid",
455
+ tenantId: TENANT_ID,
437
456
  })
438
- }
439
- // have to fake this
440
- const auth = {
441
- userId,
442
- sessionId: "sessionid",
443
- tenantId: TENANT_ID,
444
- }
445
- const app = {
446
- roleId: roleId,
447
- appId,
448
- }
449
- const authToken = jwt.sign(auth, env.JWT_SECRET)
450
- const appToken = jwt.sign(app, env.JWT_SECRET)
451
-
452
- // returning necessary request headers
453
- await userCache.invalidateUser(userId)
454
- return {
455
- Accept: "application/json",
456
- Cookie: [
457
- `${Cookies.Auth}=${authToken}`,
458
- `${Cookies.CurrentApp}=${appToken}`,
459
- ],
460
- [Headers.APP_ID]: appId,
461
- }
457
+ // have to fake this
458
+ const auth = {
459
+ userId,
460
+ sessionId: "sessionid",
461
+ tenantId: TENANT_ID,
462
+ }
463
+ const app = {
464
+ roleId: roleId,
465
+ appId,
466
+ }
467
+ const authToken = jwt.sign(auth, env.JWT_SECRET)
468
+ const appToken = jwt.sign(app, env.JWT_SECRET)
469
+
470
+ // returning necessary request headers
471
+ await userCache.invalidateUser(userId)
472
+ return {
473
+ Accept: "application/json",
474
+ Cookie: [
475
+ `${Cookies.Auth}=${authToken}`,
476
+ `${Cookies.CurrentApp}=${appToken}`,
477
+ ],
478
+ [Headers.APP_ID]: appId,
479
+ }
480
+ })
462
481
  }
463
482
  }
464
483
 
@@ -5,11 +5,11 @@ const automationUtils = require("../automations/automationUtils")
5
5
  const AutomationEmitter = require("../events/AutomationEmitter")
6
6
  const { processObject } = require("@budibase/string-templates")
7
7
  const { DEFAULT_TENANT_ID } = require("@budibase/backend-core/constants")
8
- const CouchDB = require("../db")
9
8
  const { DocumentTypes, isDevAppID } = require("../db/utils")
10
9
  const { doInTenant } = require("@budibase/backend-core/tenancy")
11
10
  const usage = require("../utilities/usageQuota")
12
11
  const { definitions: triggerDefs } = require("../automations/triggerInfo")
12
+ const { doInAppContext, getAppDB } = require("@budibase/backend-core/context")
13
13
 
14
14
  const FILTER_STEP_ID = actions.ACTION_DEFINITIONS.FILTER.stepId
15
15
  const CRON_STEP_ID = triggerDefs.CRON.stepId
@@ -59,11 +59,10 @@ class Orchestrator {
59
59
  }
60
60
 
61
61
  async getApp() {
62
- const appId = this._appId
63
62
  if (this._app) {
64
63
  return this._app
65
64
  }
66
- const db = new CouchDB(appId)
65
+ const db = getAppDB()
67
66
  this._app = await db.get(DocumentTypes.APP_METADATA)
68
67
  return this._app
69
68
  }
@@ -131,16 +130,19 @@ class Orchestrator {
131
130
  }
132
131
 
133
132
  module.exports = (input, callback) => {
134
- const automationOrchestrator = new Orchestrator(
135
- input.data.automation,
136
- input.data.event
137
- )
138
- automationOrchestrator
139
- .execute()
140
- .then(response => {
141
- callback(null, response)
142
- })
143
- .catch(err => {
144
- callback(err)
145
- })
133
+ const appId = input.data.event.appId
134
+ doInAppContext(appId, () => {
135
+ const automationOrchestrator = new Orchestrator(
136
+ input.data.automation,
137
+ input.data.event
138
+ )
139
+ automationOrchestrator
140
+ .execute()
141
+ .then(response => {
142
+ callback(null, response)
143
+ })
144
+ .catch(err => {
145
+ callback(err)
146
+ })
147
+ })
146
148
  }
@@ -3,14 +3,13 @@ threadUtils.threadSetup()
3
3
  const ScriptRunner = require("../utilities/scriptRunner")
4
4
  const { integrations } = require("../integrations")
5
5
  const { processStringSync } = require("@budibase/string-templates")
6
- const CouchDB = require("../db")
6
+ const { doInAppContext, getAppDB } = require("@budibase/backend-core/context")
7
7
 
8
8
  const IS_TRIPLE_BRACE = new RegExp(/^{{3}.*}{3}$/)
9
9
  const IS_HANDLEBARS = new RegExp(/^{{2}.*}{2}$/)
10
10
 
11
11
  class QueryRunner {
12
12
  constructor(input, flags = { noRecursiveQuery: false }) {
13
- this.appId = input.appId
14
13
  this.datasource = input.datasource
15
14
  this.queryVerb = input.queryVerb
16
15
  this.fields = input.fields
@@ -104,12 +103,11 @@ class QueryRunner {
104
103
  }
105
104
 
106
105
  async runAnotherQuery(queryId, parameters) {
107
- const db = new CouchDB(this.appId)
106
+ const db = getAppDB()
108
107
  const query = await db.get(queryId)
109
108
  const datasource = await db.get(query.datasourceId)
110
109
  return new QueryRunner(
111
110
  {
112
- appId: this.appId,
113
111
  datasource,
114
112
  queryVerb: query.queryVerb,
115
113
  fields: query.fields,
@@ -223,12 +221,14 @@ class QueryRunner {
223
221
  }
224
222
 
225
223
  module.exports = (input, callback) => {
226
- const Runner = new QueryRunner(input)
227
- Runner.execute()
228
- .then(response => {
229
- callback(null, response)
230
- })
231
- .catch(err => {
232
- callback(err)
233
- })
224
+ doInAppContext(input.appId, () => {
225
+ const Runner = new QueryRunner(input)
226
+ Runner.execute()
227
+ .then(response => {
228
+ callback(null, response)
229
+ })
230
+ .catch(err => {
231
+ callback(err)
232
+ })
233
+ })
234
234
  }
@@ -1,5 +1,4 @@
1
1
  const { budibaseTempDir } = require("../budibaseDir")
2
- const { isDev } = require("../index")
3
2
  const fs = require("fs")
4
3
  const { join } = require("path")
5
4
  const uuid = require("uuid/v4")
@@ -20,6 +19,7 @@ const {
20
19
  LINK_USER_METADATA_PREFIX,
21
20
  } = require("../../db/utils")
22
21
  const MemoryStream = require("memorystream")
22
+ const { getAppId } = require("@budibase/backend-core/context")
23
23
 
24
24
  const TOP_LEVEL_PATH = join(__dirname, "..", "..", "..")
25
25
  const NODE_MODULES_PATH = join(TOP_LEVEL_PATH, "node_modules")
@@ -51,7 +51,7 @@ exports.init = () => {
51
51
  * everything required to function is ready.
52
52
  */
53
53
  exports.checkDevelopmentEnvironment = () => {
54
- if (!isDev()) {
54
+ if (!env.isDev() || env.isTest()) {
55
55
  return
56
56
  }
57
57
  if (!fs.existsSync(budibaseTempDir())) {
@@ -251,7 +251,8 @@ exports.downloadTemplate = async (type, name) => {
251
251
  /**
252
252
  * Retrieves component libraries from object store (or tmp symlink if in local)
253
253
  */
254
- exports.getComponentLibraryManifest = async (appId, library) => {
254
+ exports.getComponentLibraryManifest = async library => {
255
+ const appId = getAppId()
255
256
  const filename = "manifest.json"
256
257
  /* istanbul ignore next */
257
258
  // when testing in cypress and so on we need to get the package
@@ -3,7 +3,7 @@ const {
3
3
  getGlobalIDFromUserMetadataID,
4
4
  } = require("../db/utils")
5
5
  const { BUILTIN_ROLE_IDS } = require("@budibase/backend-core/roles")
6
- const { getDeployedAppID } = require("@budibase/backend-core/db")
6
+ const { getProdAppID } = require("@budibase/backend-core/db")
7
7
  const { getGlobalUserParams } = require("@budibase/backend-core/db")
8
8
  const { user: userCache } = require("@budibase/backend-core/cache")
9
9
  const {
@@ -11,8 +11,10 @@ const {
11
11
  isUserInAppTenant,
12
12
  } = require("@budibase/backend-core/tenancy")
13
13
  const env = require("../environment")
14
+ const { getAppId } = require("@budibase/backend-core/context")
14
15
 
15
- exports.updateAppRole = (appId, user) => {
16
+ exports.updateAppRole = (user, { appId } = {}) => {
17
+ appId = appId || getAppId()
16
18
  if (!user || !user.roles) {
17
19
  return user
18
20
  }
@@ -24,7 +26,7 @@ exports.updateAppRole = (appId, user) => {
24
26
  return user
25
27
  }
26
28
  // always use the deployed app
27
- user.roleId = user.roles[getDeployedAppID(appId)]
29
+ user.roleId = user.roles[getProdAppID(appId)]
28
30
  // if a role wasn't found then either set as admin (builder) or public (everyone else)
29
31
  if (!user.roleId && user.builder && user.builder.global) {
30
32
  user.roleId = BUILTIN_ROLE_IDS.ADMIN
@@ -35,18 +37,18 @@ exports.updateAppRole = (appId, user) => {
35
37
  return user
36
38
  }
37
39
 
38
- function processUser(appId, user) {
40
+ function processUser(user, { appId } = {}) {
39
41
  if (user) {
40
42
  delete user.password
41
43
  }
42
- return exports.updateAppRole(appId, user)
44
+ return exports.updateAppRole(user, { appId })
43
45
  }
44
46
 
45
47
  exports.getCachedSelf = async (ctx, appId) => {
46
48
  // this has to be tenant aware, can't depend on the context to find it out
47
49
  // running some middlewares before the tenancy causes context to break
48
50
  const user = await userCache.getUser(ctx.user._id)
49
- return processUser(appId, user)
51
+ return processUser(user, { appId })
50
52
  }
51
53
 
52
54
  exports.getRawGlobalUser = async userId => {
@@ -54,12 +56,13 @@ exports.getRawGlobalUser = async userId => {
54
56
  return db.get(getGlobalIDFromUserMetadataID(userId))
55
57
  }
56
58
 
57
- exports.getGlobalUser = async (appId, userId) => {
59
+ exports.getGlobalUser = async userId => {
58
60
  let user = await exports.getRawGlobalUser(userId)
59
- return processUser(appId, user)
61
+ return processUser(user)
60
62
  }
61
63
 
62
- exports.getGlobalUsers = async (appId = null, users = null) => {
64
+ exports.getGlobalUsers = async (users = null) => {
65
+ const appId = getAppId()
63
66
  const db = getGlobalDB()
64
67
  let globalUsers
65
68
  if (users) {
@@ -86,11 +89,11 @@ exports.getGlobalUsers = async (appId = null, users = null) => {
86
89
  if (!appId) {
87
90
  return globalUsers
88
91
  }
89
- return globalUsers.map(user => exports.updateAppRole(appId, user))
92
+ return globalUsers.map(user => exports.updateAppRole(user))
90
93
  }
91
94
 
92
- exports.getGlobalUsersFromMetadata = async (appId, users) => {
93
- const globalUsers = await exports.getGlobalUsers(appId, users)
95
+ exports.getGlobalUsersFromMetadata = async users => {
96
+ const globalUsers = await exports.getGlobalUsers(users)
94
97
  return users.map(user => {
95
98
  const globalUser = globalUsers.find(
96
99
  globalUser => globalUser && user._id.includes(globalUser._id)