@budibase/server 1.0.49 → 1.0.50-alpha.3

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 (381) 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.2c37968e.js → index.9fe798bd.js} +252 -249
  8. package/builder/assets/index.c0cc369d.css +6 -0
  9. package/builder/assets/{vendor.f9faf6ab.js → vendor.5ad9695a.js} +202 -106
  10. package/builder/index.html +3 -3
  11. package/coverage/clover.xml +3105 -2814
  12. package/coverage/coverage-final.json +106 -101
  13. package/coverage/lcov-report/api/controllers/analytics.js.html +1 -1
  14. package/coverage/lcov-report/api/controllers/apikeys.js.html +1 -1
  15. package/coverage/lcov-report/api/controllers/application.js.html +257 -152
  16. package/coverage/lcov-report/api/controllers/auth.js.html +22 -13
  17. package/coverage/lcov-report/api/controllers/automation.js.html +39 -69
  18. package/coverage/lcov-report/api/controllers/backup.js.html +1 -1
  19. package/coverage/lcov-report/api/controllers/cloud.js.html +8 -11
  20. package/coverage/lcov-report/api/controllers/component.js.html +13 -16
  21. package/coverage/lcov-report/api/controllers/datasource.js.html +81 -51
  22. package/coverage/lcov-report/api/controllers/dev.js.html +6 -6
  23. package/coverage/lcov-report/api/controllers/index.html +78 -78
  24. package/coverage/lcov-report/api/controllers/integration.js.html +1 -1
  25. package/coverage/lcov-report/api/controllers/layout.js.html +9 -9
  26. package/coverage/lcov-report/api/controllers/metadata.js.html +6 -21
  27. package/coverage/lcov-report/api/controllers/migrations.js.html +119 -0
  28. package/coverage/lcov-report/api/controllers/permission.js.html +19 -19
  29. package/coverage/lcov-report/api/controllers/query/index.html +10 -10
  30. package/coverage/lcov-report/api/controllers/query/index.js.html +38 -35
  31. package/coverage/lcov-report/api/controllers/query/validation.js.html +6 -6
  32. package/coverage/lcov-report/api/controllers/role.js.html +16 -16
  33. package/coverage/lcov-report/api/controllers/routing.js.html +7 -10
  34. package/coverage/lcov-report/api/controllers/row/external.js.html +48 -141
  35. package/coverage/lcov-report/api/controllers/row/index.html +55 -40
  36. package/coverage/lcov-report/api/controllers/row/index.js.html +26 -26
  37. package/coverage/lcov-report/api/controllers/row/internal.js.html +118 -226
  38. package/coverage/lcov-report/api/controllers/row/internalSearch.js.html +52 -34
  39. package/coverage/lcov-report/api/controllers/row/staticFormula.js.html +548 -0
  40. package/coverage/lcov-report/api/controllers/row/utils.js.html +47 -62
  41. package/coverage/lcov-report/api/controllers/screen.js.html +15 -21
  42. package/coverage/lcov-report/api/controllers/script.js.html +4 -4
  43. package/coverage/lcov-report/api/controllers/table/bulkFormula.js.html +599 -0
  44. package/coverage/lcov-report/api/controllers/table/external.js.html +25 -34
  45. package/coverage/lcov-report/api/controllers/table/index.html +58 -43
  46. package/coverage/lcov-report/api/controllers/table/index.js.html +58 -82
  47. package/coverage/lcov-report/api/controllers/table/internal.js.html +81 -66
  48. package/coverage/lcov-report/api/controllers/table/utils.js.html +218 -110
  49. package/coverage/lcov-report/api/controllers/templates.js.html +1 -1
  50. package/coverage/lcov-report/api/controllers/user.js.html +79 -76
  51. package/coverage/lcov-report/api/controllers/view/exporters.js.html +4 -4
  52. package/coverage/lcov-report/api/controllers/view/index.html +16 -16
  53. package/coverage/lcov-report/api/controllers/view/index.js.html +27 -33
  54. package/coverage/lcov-report/api/controllers/view/utils.js.html +35 -20
  55. package/coverage/lcov-report/api/controllers/view/viewBuilder.js.html +7 -7
  56. package/coverage/lcov-report/api/controllers/webhook.js.html +34 -28
  57. package/coverage/lcov-report/api/index.html +1 -1
  58. package/coverage/lcov-report/api/index.js.html +8 -8
  59. package/coverage/lcov-report/api/routes/analytics.js.html +1 -1
  60. package/coverage/lcov-report/api/routes/apikeys.js.html +1 -1
  61. package/coverage/lcov-report/api/routes/application.js.html +1 -1
  62. package/coverage/lcov-report/api/routes/auth.js.html +1 -1
  63. package/coverage/lcov-report/api/routes/automation.js.html +1 -1
  64. package/coverage/lcov-report/api/routes/backup.js.html +1 -1
  65. package/coverage/lcov-report/api/routes/cloud.js.html +1 -1
  66. package/coverage/lcov-report/api/routes/component.js.html +1 -1
  67. package/coverage/lcov-report/api/routes/datasource.js.html +1 -1
  68. package/coverage/lcov-report/api/routes/deploy.js.html +1 -1
  69. package/coverage/lcov-report/api/routes/dev.js.html +1 -1
  70. package/coverage/lcov-report/api/routes/index.html +18 -18
  71. package/coverage/lcov-report/api/routes/index.js.html +3 -3
  72. package/coverage/lcov-report/api/routes/integration.js.html +1 -1
  73. package/coverage/lcov-report/api/routes/layout.js.html +1 -1
  74. package/coverage/lcov-report/api/routes/metadata.js.html +1 -1
  75. package/coverage/lcov-report/api/routes/{hosting.js.html → migrations.js.html} +17 -14
  76. package/coverage/lcov-report/api/routes/permission.js.html +1 -1
  77. package/coverage/lcov-report/api/routes/query.js.html +1 -1
  78. package/coverage/lcov-report/api/routes/role.js.html +1 -1
  79. package/coverage/lcov-report/api/routes/routing.js.html +1 -1
  80. package/coverage/lcov-report/api/routes/row.js.html +1 -1
  81. package/coverage/lcov-report/api/routes/screen.js.html +1 -1
  82. package/coverage/lcov-report/api/routes/script.js.html +1 -1
  83. package/coverage/lcov-report/api/routes/static.js.html +17 -2
  84. package/coverage/lcov-report/api/routes/table.js.html +1 -1
  85. package/coverage/lcov-report/api/routes/templates.js.html +1 -1
  86. package/coverage/lcov-report/api/routes/user.js.html +1 -1
  87. package/coverage/lcov-report/api/routes/view.js.html +1 -1
  88. package/coverage/lcov-report/api/routes/webhook.js.html +1 -1
  89. package/coverage/lcov-report/automations/actions.js.html +24 -24
  90. package/coverage/lcov-report/automations/automationUtils.js.html +7 -10
  91. package/coverage/lcov-report/automations/bullboard.js.html +16 -16
  92. package/coverage/lcov-report/automations/index.html +18 -18
  93. package/coverage/lcov-report/automations/index.js.html +1 -1
  94. package/coverage/lcov-report/automations/steps/bash.js.html +6 -6
  95. package/coverage/lcov-report/automations/steps/createRow.js.html +13 -22
  96. package/coverage/lcov-report/automations/steps/delay.js.html +4 -4
  97. package/coverage/lcov-report/automations/steps/deleteRow.js.html +11 -20
  98. package/coverage/lcov-report/automations/steps/discord.js.html +7 -7
  99. package/coverage/lcov-report/automations/steps/executeQuery.js.html +6 -6
  100. package/coverage/lcov-report/automations/steps/executeScript.js.html +6 -6
  101. package/coverage/lcov-report/automations/steps/filter.js.html +7 -7
  102. package/coverage/lcov-report/automations/steps/index.html +13 -13
  103. package/coverage/lcov-report/automations/steps/integromat.js.html +5 -5
  104. package/coverage/lcov-report/automations/steps/outgoingWebhook.js.html +8 -8
  105. package/coverage/lcov-report/automations/steps/queryRows.js.html +10 -10
  106. package/coverage/lcov-report/automations/steps/sendSmtpEmail.js.html +5 -5
  107. package/coverage/lcov-report/automations/steps/serverLog.js.html +3 -3
  108. package/coverage/lcov-report/automations/steps/slack.js.html +5 -5
  109. package/coverage/lcov-report/automations/steps/updateRow.js.html +7 -7
  110. package/coverage/lcov-report/automations/steps/utils.js.html +3 -3
  111. package/coverage/lcov-report/automations/steps/zapier.js.html +5 -5
  112. package/coverage/lcov-report/automations/triggerInfo/app.js.html +2 -2
  113. package/coverage/lcov-report/automations/triggerInfo/cron.js.html +2 -2
  114. package/coverage/lcov-report/automations/triggerInfo/index.html +1 -1
  115. package/coverage/lcov-report/automations/triggerInfo/index.js.html +8 -8
  116. package/coverage/lcov-report/automations/triggerInfo/rowDeleted.js.html +2 -2
  117. package/coverage/lcov-report/automations/triggerInfo/rowSaved.js.html +2 -2
  118. package/coverage/lcov-report/automations/triggerInfo/rowUpdated.js.html +2 -2
  119. package/coverage/lcov-report/automations/triggerInfo/webhook.js.html +2 -2
  120. package/coverage/lcov-report/automations/triggers.js.html +61 -55
  121. package/coverage/lcov-report/automations/utils.js.html +54 -42
  122. package/coverage/lcov-report/constants/index.html +5 -5
  123. package/coverage/lcov-report/constants/index.js.html +53 -26
  124. package/coverage/lcov-report/constants/layouts.js.html +5 -5
  125. package/coverage/lcov-report/constants/screens.js.html +4 -4
  126. package/coverage/lcov-report/db/client.js.html +18 -18
  127. package/coverage/lcov-report/db/inMemoryView.js.html +7 -7
  128. package/coverage/lcov-report/db/index.html +15 -15
  129. package/coverage/lcov-report/db/index.js.html +3 -3
  130. package/coverage/lcov-report/db/linkedRows/LinkController.js.html +25 -37
  131. package/coverage/lcov-report/db/linkedRows/index.html +27 -27
  132. package/coverage/lcov-report/db/linkedRows/index.js.html +51 -96
  133. package/coverage/lcov-report/db/linkedRows/linkUtils.js.html +42 -42
  134. package/coverage/lcov-report/db/newid.js.html +4 -4
  135. package/coverage/lcov-report/db/utils.js.html +87 -87
  136. package/coverage/lcov-report/events/AutomationEmitter.js.html +1 -1
  137. package/coverage/lcov-report/events/index.html +1 -1
  138. package/coverage/lcov-report/events/index.js.html +5 -5
  139. package/coverage/lcov-report/events/utils.js.html +3 -3
  140. package/coverage/lcov-report/index.html +135 -120
  141. package/coverage/lcov-report/integrations/tests/TestConfiguration.js.html +1 -1
  142. package/coverage/lcov-report/integrations/tests/index.html +1 -1
  143. package/coverage/lcov-report/middleware/appInfo.js.html +1 -1
  144. package/coverage/lcov-report/middleware/authorized.js.html +173 -65
  145. package/coverage/lcov-report/middleware/builder.js.html +10 -10
  146. package/coverage/lcov-report/middleware/currentapp.js.html +95 -83
  147. package/coverage/lcov-report/middleware/index.html +38 -38
  148. package/coverage/lcov-report/middleware/joi-validator.js.html +3 -3
  149. package/coverage/lcov-report/middleware/resourceId.js.html +1 -1
  150. package/coverage/lcov-report/middleware/selfhost.js.html +1 -1
  151. package/coverage/lcov-report/middleware/usageQuota.js.html +281 -53
  152. package/coverage/lcov-report/middleware/utils.js.html +2 -2
  153. package/coverage/lcov-report/threads/automation.js.html +30 -24
  154. package/coverage/lcov-report/threads/index.html +26 -26
  155. package/coverage/lcov-report/threads/index.js.html +10 -10
  156. package/coverage/lcov-report/threads/query.js.html +39 -24
  157. package/coverage/lcov-report/threads/utils.js.html +14 -14
  158. package/coverage/lcov-report/utilities/bcrypt.js.html +1 -1
  159. package/coverage/lcov-report/utilities/budibaseDir.js.html +7 -7
  160. package/coverage/lcov-report/utilities/centralPath.js.html +4 -4
  161. package/coverage/lcov-report/utilities/csvParser.js.html +6 -6
  162. package/coverage/lcov-report/utilities/global.js.html +58 -49
  163. package/coverage/lcov-report/utilities/index.html +37 -37
  164. package/coverage/lcov-report/utilities/index.js.html +43 -73
  165. package/coverage/lcov-report/utilities/queue/inMemoryQueue.js.html +8 -8
  166. package/coverage/lcov-report/utilities/queue/index.html +1 -1
  167. package/coverage/lcov-report/utilities/routing/index.html +1 -1
  168. package/coverage/lcov-report/utilities/routing/index.js.html +7 -7
  169. package/coverage/lcov-report/utilities/rowProcessor/index.html +27 -27
  170. package/coverage/lcov-report/utilities/rowProcessor/index.js.html +97 -103
  171. package/coverage/lcov-report/utilities/rowProcessor/utils.js.html +75 -27
  172. package/coverage/lcov-report/utilities/scriptRunner.js.html +5 -5
  173. package/coverage/lcov-report/utilities/security.js.html +8 -8
  174. package/coverage/lcov-report/utilities/statusCodes.js.html +1 -1
  175. package/coverage/lcov-report/utilities/usageQuota/index.html +141 -0
  176. package/coverage/lcov-report/utilities/usageQuota/index.js.html +359 -0
  177. package/coverage/lcov-report/utilities/usageQuota/rows.js.html +305 -0
  178. package/coverage/lcov-report/{api/controllers/hosting.js.html → utilities/usageQuota/usageQuoteReset.js.html} +36 -48
  179. package/coverage/lcov-report/utilities/users.js.html +9 -9
  180. package/coverage/lcov-report/utilities/workerRequests.js.html +43 -112
  181. package/coverage/lcov.info +5010 -4425
  182. package/dist/api/controllers/application.js +90 -60
  183. package/dist/api/controllers/auth.js +8 -5
  184. package/dist/api/controllers/automation.js +14 -19
  185. package/dist/api/controllers/cloud.js +2 -3
  186. package/dist/api/controllers/component.js +3 -4
  187. package/dist/api/controllers/datasource.js +20 -10
  188. package/dist/api/controllers/deploy/Deployment.js +3 -6
  189. package/dist/api/controllers/deploy/index.js +12 -13
  190. package/dist/api/controllers/dev.js +5 -5
  191. package/dist/api/controllers/layout.js +3 -3
  192. package/dist/api/controllers/metadata.js +4 -4
  193. package/dist/api/controllers/migrations.js +21 -0
  194. package/dist/api/controllers/permission.js +4 -4
  195. package/dist/api/controllers/query/import/index.js +4 -6
  196. package/dist/api/controllers/query/index.js +11 -10
  197. package/dist/api/controllers/role.js +5 -5
  198. package/dist/api/controllers/routing.js +5 -6
  199. package/dist/api/controllers/row/ExternalRequest.js +90 -67
  200. package/dist/api/controllers/row/external.js +14 -22
  201. package/dist/api/controllers/row/internal.js +55 -93
  202. package/dist/api/controllers/row/internalSearch.js +23 -17
  203. package/dist/api/controllers/row/staticFormula.js +147 -0
  204. package/dist/api/controllers/row/utils.js +13 -14
  205. package/dist/api/controllers/screen.js +5 -7
  206. package/dist/api/controllers/static/index.js +62 -16
  207. package/dist/api/controllers/table/bulkFormula.js +159 -0
  208. package/dist/api/controllers/table/external.js +7 -10
  209. package/dist/api/controllers/table/index.js +16 -13
  210. package/dist/api/controllers/table/internal.js +22 -16
  211. package/dist/api/controllers/table/utils.js +62 -35
  212. package/dist/api/controllers/user.js +43 -43
  213. package/dist/api/controllers/view/index.js +8 -10
  214. package/dist/api/controllers/view/utils.js +9 -4
  215. package/dist/api/controllers/webhook.js +10 -8
  216. package/dist/api/routes/index.js +2 -2
  217. package/dist/api/routes/migrations.js +9 -0
  218. package/dist/api/routes/static.js +2 -1
  219. package/dist/api/routes/tests/utilities/TestFunctions.js +37 -17
  220. package/dist/app.js +35 -5
  221. package/dist/automations/automationUtils.js +2 -3
  222. package/dist/automations/steps/createRow.js +3 -5
  223. package/dist/automations/steps/deleteRow.js +1 -4
  224. package/dist/automations/steps/updateRow.js +1 -1
  225. package/dist/automations/triggers.js +27 -25
  226. package/dist/automations/utils.js +10 -5
  227. package/dist/constants/index.js +7 -0
  228. package/dist/db/linkedRows/LinkController.js +3 -7
  229. package/dist/db/linkedRows/index.js +16 -27
  230. package/dist/db/linkedRows/linkUtils.js +6 -6
  231. package/dist/db/views/staticViews.js +9 -10
  232. package/dist/definitions/datasource.js +1 -0
  233. package/dist/environment.js +4 -1
  234. package/dist/integrations/base/sql.js +62 -21
  235. package/dist/integrations/googlesheets.js +340 -0
  236. package/dist/integrations/index.js +3 -0
  237. package/dist/integrations/utils.js +60 -9
  238. package/dist/middleware/authorized.js +57 -30
  239. package/dist/middleware/builder.js +2 -2
  240. package/dist/middleware/currentapp.js +41 -37
  241. package/dist/middleware/usageQuota.js +82 -15
  242. package/dist/migrations/functions/appUrls.js +46 -0
  243. package/dist/migrations/functions/quotas1.js +50 -0
  244. package/dist/migrations/functions/usageQuotas/index.js +20 -0
  245. package/dist/migrations/functions/usageQuotas/syncApps.js +29 -0
  246. package/dist/migrations/functions/usageQuotas/syncRows.js +33 -0
  247. package/dist/migrations/functions/userEmailViewCasing.js +24 -0
  248. package/dist/migrations/index.js +62 -0
  249. package/dist/package.json +8 -5
  250. package/dist/tests/utilities/TestConfiguration.js +80 -50
  251. package/dist/threads/automation.js +13 -11
  252. package/dist/threads/query.js +15 -11
  253. package/dist/tsconfig.tsbuildinfo +1 -1
  254. package/dist/utilities/fileSystem/index.js +4 -3
  255. package/dist/utilities/global.js +15 -12
  256. package/dist/utilities/index.js +7 -17
  257. package/dist/utilities/routing/index.js +5 -5
  258. package/dist/utilities/rowProcessor/index.js +23 -17
  259. package/dist/utilities/rowProcessor/utils.js +14 -4
  260. package/dist/utilities/{usageQuota.js → usageQuota/index.js} +45 -31
  261. package/dist/utilities/usageQuota/rows.js +78 -0
  262. package/dist/utilities/usageQuota/usageQuoteReset.js +16 -0
  263. package/dist/utilities/users.js +3 -3
  264. package/dist/utilities/workerRequests.js +4 -24
  265. package/nodemon.json +1 -1
  266. package/package.json +9 -6
  267. package/scripts/integrations/mssql/data/Dockerfile +1 -1
  268. package/scripts/integrations/mssql/data/setup.sql +17 -4
  269. package/scripts/integrations/postgres/init.sql +11 -6
  270. package/scripts/likeCypress.ts +1 -1
  271. package/src/api/controllers/application.js +85 -50
  272. package/src/api/controllers/auth.js +8 -5
  273. package/src/api/controllers/automation.js +13 -23
  274. package/src/api/controllers/cloud.js +2 -3
  275. package/src/api/controllers/component.js +3 -4
  276. package/src/api/controllers/datasource.js +21 -11
  277. package/src/api/controllers/deploy/Deployment.js +3 -7
  278. package/src/api/controllers/deploy/index.js +20 -13
  279. package/src/api/controllers/dev.js +5 -5
  280. package/src/api/controllers/layout.js +3 -3
  281. package/src/api/controllers/metadata.js +4 -9
  282. package/src/api/controllers/migrations.js +13 -0
  283. package/src/api/controllers/permission.js +4 -4
  284. package/src/api/controllers/query/import/index.ts +4 -6
  285. package/src/api/controllers/query/import/tests/index.spec.js +2 -1
  286. package/src/api/controllers/query/index.js +11 -10
  287. package/src/api/controllers/role.js +5 -5
  288. package/src/api/controllers/routing.js +5 -6
  289. package/src/api/controllers/row/ExternalRequest.ts +67 -43
  290. package/src/api/controllers/row/external.js +27 -58
  291. package/src/api/controllers/row/internal.js +54 -90
  292. package/src/api/controllers/row/internalSearch.js +23 -17
  293. package/src/api/controllers/row/staticFormula.js +156 -0
  294. package/src/api/controllers/row/utils.js +13 -18
  295. package/src/api/controllers/screen.js +5 -7
  296. package/src/api/controllers/static/index.js +66 -15
  297. package/src/api/controllers/table/bulkFormula.js +173 -0
  298. package/src/api/controllers/table/external.js +7 -10
  299. package/src/api/controllers/table/index.js +20 -28
  300. package/src/api/controllers/table/internal.js +24 -19
  301. package/src/api/controllers/table/utils.js +78 -42
  302. package/src/api/controllers/user.js +46 -45
  303. package/src/api/controllers/view/index.js +8 -10
  304. package/src/api/controllers/view/utils.js +9 -4
  305. package/src/api/controllers/webhook.js +10 -8
  306. package/src/api/routes/index.js +2 -2
  307. package/src/api/routes/migrations.js +14 -0
  308. package/src/api/routes/static.js +5 -0
  309. package/src/api/routes/tests/application.spec.js +2 -2
  310. package/src/api/routes/tests/auth.spec.js +1 -2
  311. package/src/api/routes/tests/automation.spec.js +1 -0
  312. package/src/api/routes/tests/misc.spec.js +0 -1
  313. package/src/api/routes/tests/routing.spec.js +21 -12
  314. package/src/api/routes/tests/row.spec.js +7 -4
  315. package/src/api/routes/tests/static.spec.js +98 -0
  316. package/src/api/routes/tests/utilities/TestFunctions.js +39 -17
  317. package/src/app.ts +18 -5
  318. package/src/automations/automationUtils.js +2 -3
  319. package/src/automations/steps/createRow.js +2 -5
  320. package/src/automations/steps/deleteRow.js +1 -4
  321. package/src/automations/steps/updateRow.js +1 -1
  322. package/src/automations/triggers.js +33 -31
  323. package/src/automations/utils.js +9 -5
  324. package/src/constants/index.js +9 -0
  325. package/src/db/linkedRows/LinkController.js +3 -7
  326. package/src/db/linkedRows/index.js +16 -31
  327. package/src/db/linkedRows/linkUtils.js +6 -6
  328. package/src/db/tests/linkController.spec.js +0 -1
  329. package/src/db/tests/linkTests.spec.js +5 -6
  330. package/src/db/views/staticViews.js +8 -10
  331. package/src/definitions/common.ts +3 -0
  332. package/src/definitions/datasource.ts +3 -0
  333. package/src/environment.js +4 -1
  334. package/src/integrations/base/sql.ts +75 -29
  335. package/src/integrations/googlesheets.ts +353 -0
  336. package/src/integrations/index.ts +3 -0
  337. package/src/integrations/tests/sql.spec.js +16 -0
  338. package/src/integrations/utils.ts +75 -12
  339. package/src/middleware/authorized.js +66 -30
  340. package/src/middleware/builder.js +2 -2
  341. package/src/middleware/currentapp.js +50 -46
  342. package/src/middleware/tests/authorized.spec.js +13 -6
  343. package/src/middleware/tests/currentapp.spec.js +6 -0
  344. package/src/middleware/tests/usageQuota.spec.js +18 -24
  345. package/src/middleware/usageQuota.js +93 -17
  346. package/src/migrations/functions/appUrls.ts +34 -0
  347. package/src/migrations/functions/quotas1.ts +20 -0
  348. package/src/migrations/functions/tests/appUrls.spec.js +29 -0
  349. package/src/migrations/functions/tests/quotas1.spec.js +27 -0
  350. package/src/migrations/functions/tests/userEmailViewCasing.spec.js +25 -0
  351. package/src/migrations/functions/usageQuotas/index.ts +8 -0
  352. package/src/migrations/functions/usageQuotas/syncApps.ts +18 -0
  353. package/src/migrations/functions/usageQuotas/syncRows.ts +22 -0
  354. package/src/migrations/functions/usageQuotas/tests/syncApps.spec.js +37 -0
  355. package/src/migrations/functions/usageQuotas/tests/syncRows.spec.js +43 -0
  356. package/src/migrations/functions/userEmailViewCasing.ts +13 -0
  357. package/src/migrations/index.ts +56 -0
  358. package/src/module.d.ts +3 -0
  359. package/src/tests/utilities/TestConfiguration.js +82 -55
  360. package/src/threads/automation.js +17 -15
  361. package/src/threads/query.js +17 -12
  362. package/src/utilities/fileSystem/index.js +4 -3
  363. package/src/utilities/global.js +15 -12
  364. package/src/utilities/index.js +7 -17
  365. package/src/utilities/routing/index.js +5 -5
  366. package/src/utilities/rowProcessor/index.js +25 -27
  367. package/src/utilities/rowProcessor/utils.js +23 -7
  368. package/src/utilities/tests/usageQuota/usageQuota.spec.js +72 -0
  369. package/src/utilities/usageQuota/index.js +93 -0
  370. package/src/utilities/usageQuota/rows.js +75 -0
  371. package/src/utilities/usageQuota/usageQuoteReset.js +18 -0
  372. package/src/utilities/users.js +3 -3
  373. package/src/utilities/workerRequests.js +4 -27
  374. package/tsconfig.json +4 -2
  375. package/builder/assets/index.39e01193.css +0 -1
  376. package/dist/api/controllers/hosting.js +0 -30
  377. package/dist/api/routes/hosting.js +0 -11
  378. package/src/api/controllers/hosting.js +0 -22
  379. package/src/api/routes/hosting.js +0 -13
  380. package/src/api/routes/tests/hosting.spec.js +0 -36
  381. package/src/utilities/usageQuota.js +0 -73
@@ -9,7 +9,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  const { budibaseTempDir } = require("../budibaseDir");
12
- const { isDev } = require("../index");
13
12
  const fs = require("fs");
14
13
  const { join } = require("path");
15
14
  const uuid = require("uuid/v4");
@@ -20,6 +19,7 @@ const { updateClientLibrary } = require("./clientLibrary");
20
19
  const env = require("../../environment");
21
20
  const { USER_METDATA_PREFIX, LINK_USER_METADATA_PREFIX, } = require("../../db/utils");
22
21
  const MemoryStream = require("memorystream");
22
+ const { getAppId } = require("@budibase/backend-core/context");
23
23
  const TOP_LEVEL_PATH = join(__dirname, "..", "..", "..");
24
24
  const NODE_MODULES_PATH = join(TOP_LEVEL_PATH, "node_modules");
25
25
  /**
@@ -47,7 +47,7 @@ exports.init = () => {
47
47
  * everything required to function is ready.
48
48
  */
49
49
  exports.checkDevelopmentEnvironment = () => {
50
- if (!isDev()) {
50
+ if (!env.isDev() || env.isTest()) {
51
51
  return;
52
52
  }
53
53
  if (!fs.existsSync(budibaseTempDir())) {
@@ -218,7 +218,8 @@ exports.downloadTemplate = (type, name) => __awaiter(void 0, void 0, void 0, fun
218
218
  /**
219
219
  * Retrieves component libraries from object store (or tmp symlink if in local)
220
220
  */
221
- exports.getComponentLibraryManifest = (appId, library) => __awaiter(void 0, void 0, void 0, function* () {
221
+ exports.getComponentLibraryManifest = (library) => __awaiter(void 0, void 0, void 0, function* () {
222
+ const appId = getAppId();
222
223
  const filename = "manifest.json";
223
224
  /* istanbul ignore next */
224
225
  // when testing in cypress and so on we need to get the package
@@ -10,12 +10,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  const { getMultiIDParams, getGlobalIDFromUserMetadataID, } = require("../db/utils");
12
12
  const { BUILTIN_ROLE_IDS } = require("@budibase/backend-core/roles");
13
- const { getDeployedAppID } = require("@budibase/backend-core/db");
13
+ const { getProdAppID } = require("@budibase/backend-core/db");
14
14
  const { getGlobalUserParams } = require("@budibase/backend-core/db");
15
15
  const { user: userCache } = require("@budibase/backend-core/cache");
16
16
  const { getGlobalDB, isUserInAppTenant, } = require("@budibase/backend-core/tenancy");
17
17
  const env = require("../environment");
18
- exports.updateAppRole = (appId, user) => {
18
+ const { getAppId } = require("@budibase/backend-core/context");
19
+ exports.updateAppRole = (user, { appId } = {}) => {
20
+ appId = appId || getAppId();
19
21
  if (!user || !user.roles) {
20
22
  return user;
21
23
  }
@@ -27,7 +29,7 @@ exports.updateAppRole = (appId, user) => {
27
29
  return user;
28
30
  }
29
31
  // always use the deployed app
30
- user.roleId = user.roles[getDeployedAppID(appId)];
32
+ user.roleId = user.roles[getProdAppID(appId)];
31
33
  // if a role wasn't found then either set as admin (builder) or public (everyone else)
32
34
  if (!user.roleId && user.builder && user.builder.global) {
33
35
  user.roleId = BUILTIN_ROLE_IDS.ADMIN;
@@ -38,27 +40,28 @@ exports.updateAppRole = (appId, user) => {
38
40
  delete user.roles;
39
41
  return user;
40
42
  };
41
- function processUser(appId, user) {
43
+ function processUser(user, { appId } = {}) {
42
44
  if (user) {
43
45
  delete user.password;
44
46
  }
45
- return exports.updateAppRole(appId, user);
47
+ return exports.updateAppRole(user, { appId });
46
48
  }
47
49
  exports.getCachedSelf = (ctx, appId) => __awaiter(void 0, void 0, void 0, function* () {
48
50
  // this has to be tenant aware, can't depend on the context to find it out
49
51
  // running some middlewares before the tenancy causes context to break
50
52
  const user = yield userCache.getUser(ctx.user._id);
51
- return processUser(appId, user);
53
+ return processUser(user, { appId });
52
54
  });
53
55
  exports.getRawGlobalUser = (userId) => __awaiter(void 0, void 0, void 0, function* () {
54
56
  const db = getGlobalDB();
55
57
  return db.get(getGlobalIDFromUserMetadataID(userId));
56
58
  });
57
- exports.getGlobalUser = (appId, userId) => __awaiter(void 0, void 0, void 0, function* () {
59
+ exports.getGlobalUser = (userId) => __awaiter(void 0, void 0, void 0, function* () {
58
60
  let user = yield exports.getRawGlobalUser(userId);
59
- return processUser(appId, user);
61
+ return processUser(user);
60
62
  });
61
- exports.getGlobalUsers = (appId = null, users = null) => __awaiter(void 0, void 0, void 0, function* () {
63
+ exports.getGlobalUsers = (users = null) => __awaiter(void 0, void 0, void 0, function* () {
64
+ const appId = getAppId();
62
65
  const db = getGlobalDB();
63
66
  let globalUsers;
64
67
  if (users) {
@@ -80,10 +83,10 @@ exports.getGlobalUsers = (appId = null, users = null) => __awaiter(void 0, void
80
83
  if (!appId) {
81
84
  return globalUsers;
82
85
  }
83
- return globalUsers.map(user => exports.updateAppRole(appId, user));
86
+ return globalUsers.map(user => exports.updateAppRole(user));
84
87
  });
85
- exports.getGlobalUsersFromMetadata = (appId, users) => __awaiter(void 0, void 0, void 0, function* () {
86
- const globalUsers = yield exports.getGlobalUsers(appId, users);
88
+ exports.getGlobalUsersFromMetadata = (users) => __awaiter(void 0, void 0, void 0, function* () {
89
+ const globalUsers = yield exports.getGlobalUsers(users);
87
90
  return users.map(user => {
88
91
  const globalUser = globalUsers.find(globalUser => globalUser && user._id.includes(globalUser._id));
89
92
  return Object.assign(Object.assign({}, globalUser), user);
@@ -11,9 +11,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  const env = require("../environment");
12
12
  const { OBJ_STORE_DIRECTORY } = require("../constants");
13
13
  const { sanitizeKey } = require("@budibase/backend-core/objectStore");
14
- const CouchDB = require("../db");
15
14
  const { generateMetadataID } = require("../db/utils");
16
15
  const Readable = require("stream").Readable;
16
+ const { getAppDB } = require("@budibase/backend-core/context");
17
17
  const BB_CDN = "https://cdn.budi.live";
18
18
  exports.wait = ms => new Promise(resolve => setTimeout(resolve, ms));
19
19
  exports.isDev = env.isDev;
@@ -72,8 +72,8 @@ exports.clientLibraryPath = (appId, version) => {
72
72
  exports.attachmentsRelativeURL = attachmentKey => {
73
73
  return exports.checkSlashesInUrl(`${exports.objectStoreUrl()}/${attachmentKey}`);
74
74
  };
75
- exports.updateEntityMetadata = (appId, type, entityId, updateFn) => __awaiter(void 0, void 0, void 0, function* () {
76
- const db = new CouchDB(appId);
75
+ exports.updateEntityMetadata = (type, entityId, updateFn) => __awaiter(void 0, void 0, void 0, function* () {
76
+ const db = getAppDB();
77
77
  const id = generateMetadataID(type, entityId);
78
78
  // read it to see if it exists, we'll overwrite it no matter what
79
79
  let rev, metadata = {};
@@ -93,13 +93,13 @@ exports.updateEntityMetadata = (appId, type, entityId, updateFn) => __awaiter(vo
93
93
  const response = yield db.put(metadata);
94
94
  return Object.assign(Object.assign({}, metadata), { _id: id, _rev: response.rev });
95
95
  });
96
- exports.saveEntityMetadata = (appId, type, entityId, metadata) => __awaiter(void 0, void 0, void 0, function* () {
97
- return exports.updateEntityMetadata(appId, type, entityId, () => {
96
+ exports.saveEntityMetadata = (type, entityId, metadata) => __awaiter(void 0, void 0, void 0, function* () {
97
+ return exports.updateEntityMetadata(type, entityId, () => {
98
98
  return metadata;
99
99
  });
100
100
  });
101
- exports.deleteEntityMetadata = (appId, type, entityId) => __awaiter(void 0, void 0, void 0, function* () {
102
- const db = new CouchDB(appId);
101
+ exports.deleteEntityMetadata = (type, entityId) => __awaiter(void 0, void 0, void 0, function* () {
102
+ const db = getAppDB();
103
103
  const id = generateMetadataID(type, entityId);
104
104
  let rev;
105
105
  try {
@@ -132,16 +132,6 @@ exports.stringToReadStream = string => {
132
132
  },
133
133
  });
134
134
  };
135
- exports.doesDatabaseExist = (dbName) => __awaiter(void 0, void 0, void 0, function* () {
136
- try {
137
- const db = new CouchDB(dbName, { skip_setup: true });
138
- const info = yield db.info();
139
- return info && !info.error;
140
- }
141
- catch (err) {
142
- return false;
143
- }
144
- });
145
135
  exports.formatBytes = bytes => {
146
136
  const units = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
147
137
  const byteIncrements = 1024;
@@ -8,11 +8,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
- const CouchDB = require("../../db");
12
11
  const { createRoutingView } = require("../../db/views/staticViews");
13
12
  const { ViewNames, getQueryIndex, UNICODE_MAX } = require("../../db/utils");
14
- exports.getRoutingInfo = (appId) => __awaiter(void 0, void 0, void 0, function* () {
15
- const db = new CouchDB(appId);
13
+ const { getAppDB } = require("@budibase/backend-core/context");
14
+ exports.getRoutingInfo = () => __awaiter(void 0, void 0, void 0, function* () {
15
+ const db = getAppDB();
16
16
  try {
17
17
  const allRouting = yield db.query(getQueryIndex(ViewNames.ROUTING), {
18
18
  startKey: "",
@@ -24,8 +24,8 @@ exports.getRoutingInfo = (appId) => __awaiter(void 0, void 0, void 0, function*
24
24
  // check if the view doesn't exist, it should for all new instances
25
25
  /* istanbul ignore next */
26
26
  if (err != null && err.name === "not_found") {
27
- yield createRoutingView(appId);
28
- return exports.getRoutingInfo(appId);
27
+ yield createRoutingView();
28
+ return exports.getRoutingInfo();
29
29
  }
30
30
  else {
31
31
  throw err;
@@ -15,8 +15,8 @@ const { attachmentsRelativeURL } = require("../index");
15
15
  const { processFormulas } = require("./utils");
16
16
  const { deleteFiles } = require("../../utilities/fileSystem/utilities");
17
17
  const { ObjectStoreBuckets } = require("../../constants");
18
- const { isProdAppID, getDeployedAppID, dbExists, } = require("@budibase/backend-core/db");
19
- const CouchDB = require("../../db");
18
+ const { isProdAppID, getProdAppID, dbExists, } = require("@budibase/backend-core/db");
19
+ const { getAppId } = require("@budibase/backend-core/context");
20
20
  const BASE_AUTO_ID = 1;
21
21
  /**
22
22
  * A map of how we convert various properties in rows to each other based on the row type.
@@ -175,6 +175,7 @@ function processAutoColumn(user, table, row, opts = { reprocessing: false, noAut
175
175
  return { table, row };
176
176
  }
177
177
  exports.processAutoColumn = processAutoColumn;
178
+ exports.processFormulas = processFormulas;
178
179
  /**
179
180
  * This will coerce a value to the correct types based on the type transform map
180
181
  * @param {object} row The value to coerce
@@ -218,11 +219,11 @@ exports.inputProcessing = (user = {}, table, row, opts = { noAutoRelationships:
218
219
  }
219
220
  continue;
220
221
  }
221
- // specific case to delete formula values if they get saved
222
- // type coercion cannot completely remove the field, so have to do it here
222
+ // remove any formula values, they are to be generated
223
223
  if (field.type === FieldTypes.FORMULA) {
224
224
  delete clonedRow[key];
225
225
  }
226
+ // otherwise coerce what is there to correct types
226
227
  else {
227
228
  clonedRow[key] = exports.coerce(value, field.type);
228
229
  }
@@ -237,24 +238,22 @@ exports.inputProcessing = (user = {}, table, row, opts = { noAutoRelationships:
237
238
  /**
238
239
  * This function enriches the input rows with anything they are supposed to contain, for example
239
240
  * link records or attachment links.
240
- * @param {object} ctx the request which is looking for enriched rows.
241
241
  * @param {object} table the table from which these rows came from originally, this is used to determine
242
242
  * the schema of the rows and then enrich.
243
243
  * @param {object[]|object} rows the rows which are to be enriched.
244
244
  * @param {object} opts used to set some options for the output, such as disabling relationship squashing.
245
245
  * @returns {object[]|object} the enriched rows will be returned.
246
246
  */
247
- exports.outputProcessing = (ctx, table, rows, opts = { squash: true }) => __awaiter(void 0, void 0, void 0, function* () {
248
- const appId = ctx.appId;
247
+ exports.outputProcessing = (table, rows, opts = { squash: true }) => __awaiter(void 0, void 0, void 0, function* () {
249
248
  let wasArray = true;
250
249
  if (!(rows instanceof Array)) {
251
250
  rows = [rows];
252
251
  wasArray = false;
253
252
  }
254
253
  // attach any linked row information
255
- let enriched = yield linkRows.attachFullLinkedDocs(ctx, table, rows);
254
+ let enriched = yield linkRows.attachFullLinkedDocs(table, rows);
256
255
  // process formulas
257
- enriched = processFormulas(table, enriched);
256
+ enriched = processFormulas(table, enriched, { dynamic: true });
258
257
  // update the attachments URL depending on hosting
259
258
  for (let [property, column] of Object.entries(table.schema)) {
260
259
  if (column.type === FieldTypes.ATTACHMENT) {
@@ -269,24 +268,26 @@ exports.outputProcessing = (ctx, table, rows, opts = { squash: true }) => __awai
269
268
  }
270
269
  }
271
270
  if (opts.squash) {
272
- enriched = yield linkRows.squashLinksToPrimaryDisplay(appId, table, enriched);
271
+ enriched = yield linkRows.squashLinksToPrimaryDisplay(table, enriched);
273
272
  }
274
273
  return wasArray ? enriched : enriched[0];
275
274
  });
276
275
  /**
277
276
  * Clean up any attachments that were attached to a row.
278
- * @param {string} appId The ID of the app from which a row is being deleted.
279
277
  * @param {object} table The table from which a row is being removed.
280
278
  * @param {any} row optional - the row being removed.
281
279
  * @param {any} rows optional - if multiple rows being deleted can do this in bulk.
282
280
  * @param {any} oldRow optional - if updating a row this will determine the difference.
281
+ * @param {any} oldTable optional - if updating a table, can supply the old table to look for
282
+ * deleted attachment columns.
283
283
  * @return {Promise<void>} When all attachments have been removed this will return.
284
284
  */
285
- exports.cleanupAttachments = (appId, table, { row, rows, oldRow }) => __awaiter(void 0, void 0, void 0, function* () {
285
+ exports.cleanupAttachments = (table, { row, rows, oldRow, oldTable }) => __awaiter(void 0, void 0, void 0, function* () {
286
+ const appId = getAppId();
286
287
  if (!isProdAppID(appId)) {
287
- const prodAppId = getDeployedAppID(appId);
288
+ const prodAppId = getProdAppID(appId);
288
289
  // if prod exists, then don't allow deleting
289
- const exists = yield dbExists(CouchDB, prodAppId);
290
+ const exists = yield dbExists(prodAppId);
290
291
  if (exists) {
291
292
  return;
292
293
  }
@@ -297,12 +298,17 @@ exports.cleanupAttachments = (appId, table, { row, rows, oldRow }) => __awaiter(
297
298
  files = files.concat(row[key].map(attachment => attachment.key));
298
299
  }
299
300
  }
300
- for (let [key, schema] of Object.entries(table.schema)) {
301
+ const schemaToUse = oldTable ? oldTable.schema : table.schema;
302
+ for (let [key, schema] of Object.entries(schemaToUse)) {
301
303
  if (schema.type !== FieldTypes.ATTACHMENT) {
302
304
  continue;
303
305
  }
304
- // if updating, need to manage the differences
305
- if (oldRow && row) {
306
+ // old table had this column, new table doesn't - delete it
307
+ if (oldTable && !table.schema[key]) {
308
+ rows.forEach(row => addFiles(row, key));
309
+ }
310
+ else if (oldRow && row) {
311
+ // if updating, need to manage the differences
306
312
  files = files.concat(getRemovedAttachmentKeys(oldRow, row, key));
307
313
  }
308
314
  else if (row) {
@@ -1,20 +1,30 @@
1
1
  "use strict";
2
- const { FieldTypes } = require("../../constants");
2
+ const { FieldTypes, FormulaTypes } = require("../../constants");
3
3
  const { processStringSync } = require("@budibase/string-templates");
4
4
  /**
5
5
  * Looks through the rows provided and finds formulas - which it then processes.
6
6
  */
7
- exports.processFormulas = (table, rows) => {
7
+ exports.processFormulas = (table, rows, { dynamic, contextRows } = { dynamic: true }) => {
8
8
  const single = !Array.isArray(rows);
9
9
  if (single) {
10
10
  rows = [rows];
11
+ contextRows = contextRows ? [contextRows] : contextRows;
11
12
  }
12
13
  for (let [column, schema] of Object.entries(table.schema)) {
13
- if (schema.type !== FieldTypes.FORMULA) {
14
+ const isStatic = schema.formulaType === FormulaTypes.STATIC;
15
+ if (schema.type !== FieldTypes.FORMULA ||
16
+ (dynamic && isStatic) ||
17
+ (!dynamic && !isStatic)) {
14
18
  continue;
15
19
  }
16
20
  // iterate through rows and process formula
17
- rows = rows.map(row => (Object.assign(Object.assign({}, row), { [column]: processStringSync(schema.formula, row) })));
21
+ for (let i = 0; i < rows.length; i++) {
22
+ if (schema.formula) {
23
+ let row = rows[i];
24
+ let context = contextRows ? contextRows[i] : row;
25
+ rows[i] = Object.assign(Object.assign({}, row), { [column]: processStringSync(schema.formula, context) });
26
+ }
27
+ }
18
28
  }
19
29
  return single ? rows[0] : rows;
20
30
  };
@@ -8,12 +8,24 @@ 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 env = require("../environment");
12
- const { getGlobalDB } = require("@budibase/backend-core/tenancy");
11
+ const env = require("../../environment");
12
+ const { getGlobalDB, getTenantId } = require("@budibase/backend-core/tenancy");
13
13
  const { StaticDatabases, generateNewUsageQuotaDoc, } = require("@budibase/backend-core/db");
14
- function getNewQuotaReset() {
15
- return Date.now() + 2592000000;
16
- }
14
+ exports.useQuotas = () => {
15
+ // check if quotas are enabled
16
+ if (env.USE_QUOTAS) {
17
+ // check if there are any tenants without limits
18
+ if (env.EXCLUDE_QUOTAS_TENANTS) {
19
+ const excludedTenants = env.EXCLUDE_QUOTAS_TENANTS.replace(/\s/g, "").split(",");
20
+ const tenantId = getTenantId();
21
+ if (excludedTenants.includes(tenantId)) {
22
+ return false;
23
+ }
24
+ }
25
+ return true;
26
+ }
27
+ return false;
28
+ };
17
29
  exports.Properties = {
18
30
  ROW: "rows",
19
31
  UPLOAD: "storage",
@@ -21,49 +33,51 @@ exports.Properties = {
21
33
  USER: "users",
22
34
  AUTOMATION: "automationRuns",
23
35
  APPS: "apps",
24
- EMAILS: "emails",
36
+ EMAILS: "emails", // doesn't work yet
25
37
  };
26
- function getUsageQuotaDoc(db) {
27
- return __awaiter(this, void 0, void 0, function* () {
28
- let quota;
29
- try {
30
- quota = yield db.get(StaticDatabases.PLATFORM_INFO.docs.usageQuota);
31
- }
32
- catch (err) {
33
- // doc doesn't exist. Create it
34
- quota = yield db.post(generateNewUsageQuotaDoc());
35
- }
36
- return quota;
37
- });
38
- }
38
+ exports.getUsageQuotaDoc = (db) => __awaiter(void 0, void 0, void 0, function* () {
39
+ let quota;
40
+ try {
41
+ quota = yield db.get(StaticDatabases.GLOBAL.docs.usageQuota);
42
+ }
43
+ catch (err) {
44
+ // doc doesn't exist. Create it
45
+ quota = generateNewUsageQuotaDoc();
46
+ const response = yield db.put(quota);
47
+ quota._rev = response.rev;
48
+ }
49
+ return quota;
50
+ });
39
51
  /**
40
52
  * Given a specified tenantId this will add to the usage object for the specified property.
41
53
  * @param {string} property The property which is to be added to (within the nested usageQuota object).
42
54
  * @param {number} usage The amount (this can be negative) to adjust the number by.
55
+ * @param {object} opts optional - options such as dryRun, to check what update will do.
43
56
  * @returns {Promise<void>} When this completes the API key will now be up to date - the quota period may have
44
57
  * also been reset after this call.
45
58
  */
46
- exports.update = (property, usage) => __awaiter(void 0, void 0, void 0, function* () {
47
- if (!env.USE_QUOTAS) {
59
+ exports.update = (property, usage, opts = { dryRun: false }) => __awaiter(void 0, void 0, void 0, function* () {
60
+ if (!exports.useQuotas()) {
48
61
  return;
49
62
  }
50
63
  try {
51
64
  const db = getGlobalDB();
52
- const quota = yield getUsageQuotaDoc(db);
53
- // Check if the quota needs reset
54
- if (Date.now() >= quota.quotaReset) {
55
- quota.quotaReset = getNewQuotaReset();
56
- for (let prop of Object.keys(quota.usageQuota)) {
57
- quota.usageQuota[prop] = 0;
58
- }
59
- }
65
+ const quota = yield exports.getUsageQuotaDoc(db);
60
66
  // increment the quota
61
67
  quota.usageQuota[property] += usage;
62
- if (quota.usageQuota[property] > quota.usageLimits[property]) {
68
+ if (quota.usageQuota[property] > quota.usageLimits[property] &&
69
+ usage > 0 // allow for decrementing usage when the quota is already exceeded
70
+ ) {
63
71
  throw new Error(`You have exceeded your usage quota of ${quota.usageLimits[property]} ${property}.`);
64
72
  }
73
+ if (quota.usageQuota[property] < 0) {
74
+ // never go negative if the quota has previously been exceeded
75
+ quota.usageQuota[property] = 0;
76
+ }
65
77
  // update the usage quotas
66
- yield db.put(quota);
78
+ if (!opts.dryRun) {
79
+ yield db.put(quota);
80
+ }
67
81
  }
68
82
  catch (err) {
69
83
  console.error(`Error updating usage quotas for ${property}`, err);
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ const { getRowParams, USER_METDATA_PREFIX } = require("../../db/utils");
12
+ const CouchDB = require("../../db");
13
+ const { isDevAppID, getDevelopmentAppID } = require("@budibase/backend-core/db");
14
+ const ROW_EXCLUSIONS = [USER_METDATA_PREFIX];
15
+ const getAppPairs = appIds => {
16
+ // collect the app ids into dev / prod pairs
17
+ // keyed by the dev app id
18
+ const pairs = {};
19
+ for (let appId of appIds) {
20
+ const devId = getDevelopmentAppID(appId);
21
+ if (!pairs[devId]) {
22
+ pairs[devId] = {};
23
+ }
24
+ if (isDevAppID(appId)) {
25
+ pairs[devId].devId = appId;
26
+ }
27
+ else {
28
+ pairs[devId].prodId = appId;
29
+ }
30
+ }
31
+ return pairs;
32
+ };
33
+ const getAppRows = (appId) => __awaiter(void 0, void 0, void 0, function* () {
34
+ // need to specify the app ID, as this is used for different apps in one call
35
+ const appDb = new CouchDB(appId);
36
+ const response = yield appDb.allDocs(getRowParams(null, null, {
37
+ include_docs: false,
38
+ }));
39
+ return response.rows
40
+ .map(r => r.id)
41
+ .filter(id => {
42
+ for (let exclusion of ROW_EXCLUSIONS) {
43
+ if (id.startsWith(exclusion)) {
44
+ return false;
45
+ }
46
+ }
47
+ return true;
48
+ });
49
+ });
50
+ /**
51
+ * Return a set of all rows in the given app ids.
52
+ * The returned rows will be unique on a per dev/prod app basis.
53
+ * Rows duplicates may exist across apps due to data import so they are not filtered out.
54
+ */
55
+ exports.getUniqueRows = (appIds) => __awaiter(void 0, void 0, void 0, function* () {
56
+ let uniqueRows = [];
57
+ const pairs = getAppPairs(appIds);
58
+ for (let pair of Object.values(pairs)) {
59
+ let appRows = [];
60
+ for (let appId of [pair.devId, pair.prodId]) {
61
+ if (!appId) {
62
+ continue;
63
+ }
64
+ try {
65
+ appRows.push(yield getAppRows(appId));
66
+ }
67
+ catch (e) {
68
+ console.error(e);
69
+ // don't error out if we can't count the app rows, just continue
70
+ }
71
+ }
72
+ // ensure uniqueness on a per app pair basis
73
+ // this can't be done on all rows because app import results in
74
+ // duplicate row ids across apps
75
+ uniqueRows = uniqueRows.concat(...new Set(appRows));
76
+ }
77
+ return uniqueRows;
78
+ });
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ // UNUSED CODE
3
+ // Preserved for future use
4
+ /* eslint-disable no-unused-vars */
5
+ function getNewQuotaReset() {
6
+ return Date.now() + 2592000000;
7
+ }
8
+ function resetQuotasIfRequired(quota) {
9
+ // Check if the quota needs reset
10
+ if (Date.now() >= quota.quotaReset) {
11
+ quota.quotaReset = getNewQuotaReset();
12
+ for (let prop of Object.keys(quota.usageQuota)) {
13
+ quota.usageQuota[prop] = 0;
14
+ }
15
+ }
16
+ }
@@ -8,15 +8,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
- const CouchDB = require("../db");
12
11
  const { InternalTables } = require("../db/utils");
13
12
  const { getGlobalUser } = require("../utilities/global");
13
+ const { getAppDB } = require("@budibase/backend-core/context");
14
14
  exports.getFullUser = (ctx, userId) => __awaiter(void 0, void 0, void 0, function* () {
15
- const global = yield getGlobalUser(ctx.appId, userId);
15
+ const global = yield getGlobalUser(userId);
16
16
  let metadata;
17
17
  try {
18
18
  // this will throw an error if the db doesn't exist, or there is no appId
19
- const db = new CouchDB(ctx.appId);
19
+ const db = getAppDB();
20
20
  metadata = yield db.get(userId);
21
21
  }
22
22
  catch (err) {
@@ -11,7 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  const fetch = require("node-fetch");
12
12
  const env = require("../environment");
13
13
  const { checkSlashesInUrl } = require("./index");
14
- const { getDeployedAppID } = require("@budibase/backend-core/db");
14
+ const { getProdAppID } = require("@budibase/backend-core/db");
15
15
  const { updateAppRole } = require("./global");
16
16
  const { Headers } = require("@budibase/backend-core/constants");
17
17
  const { getTenantId, isTenantIdSet } = require("@budibase/backend-core/tenancy");
@@ -61,26 +61,6 @@ exports.sendSmtpEmail = (to, from, subject, contents, automation) => __awaiter(v
61
61
  }
62
62
  return response.json();
63
63
  });
64
- exports.getDeployedApps = () => __awaiter(void 0, void 0, void 0, function* () {
65
- try {
66
- const response = yield fetch(checkSlashesInUrl(env.WORKER_URL + `/api/apps`), request(null, {
67
- method: "GET",
68
- }));
69
- const json = yield response.json();
70
- const apps = {};
71
- for (let [key, value] of Object.entries(json)) {
72
- if (value.url) {
73
- value.url = value.url.toLowerCase();
74
- apps[key.toLowerCase()] = value;
75
- }
76
- }
77
- return apps;
78
- }
79
- catch (err) {
80
- // error, cannot determine deployed apps, don't stop app creation - sort this later
81
- return {};
82
- }
83
- });
84
64
  exports.getGlobalSelf = (ctx, appId = null) => __awaiter(void 0, void 0, void 0, function* () {
85
65
  const endpoint = `/api/global/users/self`;
86
66
  const response = yield fetch(checkSlashesInUrl(env.WORKER_URL + endpoint),
@@ -91,13 +71,13 @@ exports.getGlobalSelf = (ctx, appId = null) => __awaiter(void 0, void 0, void 0,
91
71
  }
92
72
  let json = yield response.json();
93
73
  if (appId) {
94
- json = updateAppRole(appId, json);
74
+ json = updateAppRole(json);
95
75
  }
96
76
  return json;
97
77
  });
98
78
  exports.removeAppFromUserRoles = (ctx, appId) => __awaiter(void 0, void 0, void 0, function* () {
99
- const deployedAppId = getDeployedAppID(appId);
100
- const response = yield fetch(checkSlashesInUrl(env.WORKER_URL + `/api/global/roles/${deployedAppId}`), request(ctx, {
79
+ const prodAppId = getProdAppID(appId);
80
+ const response = yield fetch(checkSlashesInUrl(env.WORKER_URL + `/api/global/roles/${prodAppId}`), request(ctx, {
101
81
  method: "DELETE",
102
82
  }));
103
83
  if (response.status !== 200) {
package/nodemon.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "watch": ["src", "../auth"],
2
+ "watch": ["src", "../backend-core"],
3
3
  "ext": "js,ts,json",
4
4
  "ignore": ["src/**/*.spec.ts", "src/**/*.spec.js"],
5
5
  "exec": "ts-node src/index.ts"