@budibase/server 1.0.47 → 1.0.49-alpha.2

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 (291) hide show
  1. package/builder/assets/{index.937b2767.js → index.82e12602.js} +234 -231
  2. package/builder/assets/index.83c0944f.css +1 -0
  3. package/builder/assets/{vendor.f9faf6ab.js → vendor.d526bfe8.js} +12 -12
  4. package/builder/index.html +3 -3
  5. package/coverage/clover.xml +2334 -2013
  6. package/coverage/coverage-final.json +100 -95
  7. package/coverage/lcov-report/api/controllers/analytics.js.html +1 -1
  8. package/coverage/lcov-report/api/controllers/apikeys.js.html +1 -1
  9. package/coverage/lcov-report/api/controllers/application.js.html +210 -126
  10. package/coverage/lcov-report/api/controllers/auth.js.html +18 -6
  11. package/coverage/lcov-report/api/controllers/automation.js.html +21 -21
  12. package/coverage/lcov-report/api/controllers/backup.js.html +1 -1
  13. package/coverage/lcov-report/api/controllers/cloud.js.html +1 -1
  14. package/coverage/lcov-report/api/controllers/component.js.html +4 -4
  15. package/coverage/lcov-report/api/controllers/datasource.js.html +72 -36
  16. package/coverage/lcov-report/api/controllers/dev.js.html +1 -1
  17. package/coverage/lcov-report/api/controllers/index.html +53 -53
  18. package/coverage/lcov-report/api/controllers/integration.js.html +1 -1
  19. package/coverage/lcov-report/api/controllers/layout.js.html +6 -6
  20. package/coverage/lcov-report/api/controllers/metadata.js.html +1 -1
  21. package/coverage/lcov-report/api/controllers/migrations.js.html +119 -0
  22. package/coverage/lcov-report/api/controllers/permission.js.html +15 -15
  23. package/coverage/lcov-report/api/controllers/query/index.html +1 -1
  24. package/coverage/lcov-report/api/controllers/query/index.js.html +19 -19
  25. package/coverage/lcov-report/api/controllers/query/validation.js.html +6 -6
  26. package/coverage/lcov-report/api/controllers/role.js.html +9 -9
  27. package/coverage/lcov-report/api/controllers/routing.js.html +1 -1
  28. package/coverage/lcov-report/api/controllers/row/external.js.html +16 -16
  29. package/coverage/lcov-report/api/controllers/row/index.html +48 -33
  30. package/coverage/lcov-report/api/controllers/row/index.js.html +26 -26
  31. package/coverage/lcov-report/api/controllers/row/internal.js.html +95 -158
  32. package/coverage/lcov-report/api/controllers/row/internalSearch.js.html +41 -17
  33. package/coverage/lcov-report/api/controllers/row/staticFormula.js.html +551 -0
  34. package/coverage/lcov-report/api/controllers/row/utils.js.html +39 -57
  35. package/coverage/lcov-report/api/controllers/screen.js.html +7 -7
  36. package/coverage/lcov-report/api/controllers/script.js.html +4 -4
  37. package/coverage/lcov-report/api/controllers/table/bulkFormula.js.html +629 -0
  38. package/coverage/lcov-report/api/controllers/table/external.js.html +12 -12
  39. package/coverage/lcov-report/api/controllers/table/index.html +49 -34
  40. package/coverage/lcov-report/api/controllers/table/index.js.html +53 -77
  41. package/coverage/lcov-report/api/controllers/table/internal.js.html +59 -35
  42. package/coverage/lcov-report/api/controllers/table/utils.js.html +283 -76
  43. package/coverage/lcov-report/api/controllers/templates.js.html +1 -1
  44. package/coverage/lcov-report/api/controllers/user.js.html +30 -24
  45. package/coverage/lcov-report/api/controllers/view/exporters.js.html +4 -4
  46. package/coverage/lcov-report/api/controllers/view/index.html +1 -1
  47. package/coverage/lcov-report/api/controllers/view/index.js.html +14 -14
  48. package/coverage/lcov-report/api/controllers/view/utils.js.html +11 -11
  49. package/coverage/lcov-report/api/controllers/view/viewBuilder.js.html +7 -7
  50. package/coverage/lcov-report/api/controllers/webhook.js.html +15 -15
  51. package/coverage/lcov-report/api/index.html +1 -1
  52. package/coverage/lcov-report/api/index.js.html +8 -8
  53. package/coverage/lcov-report/api/routes/analytics.js.html +1 -1
  54. package/coverage/lcov-report/api/routes/apikeys.js.html +1 -1
  55. package/coverage/lcov-report/api/routes/application.js.html +1 -1
  56. package/coverage/lcov-report/api/routes/auth.js.html +1 -1
  57. package/coverage/lcov-report/api/routes/automation.js.html +1 -1
  58. package/coverage/lcov-report/api/routes/backup.js.html +1 -1
  59. package/coverage/lcov-report/api/routes/cloud.js.html +1 -1
  60. package/coverage/lcov-report/api/routes/component.js.html +1 -1
  61. package/coverage/lcov-report/api/routes/datasource.js.html +1 -1
  62. package/coverage/lcov-report/api/routes/deploy.js.html +1 -1
  63. package/coverage/lcov-report/api/routes/dev.js.html +1 -1
  64. package/coverage/lcov-report/api/routes/index.html +18 -18
  65. package/coverage/lcov-report/api/routes/index.js.html +3 -3
  66. package/coverage/lcov-report/api/routes/integration.js.html +1 -1
  67. package/coverage/lcov-report/api/routes/layout.js.html +1 -1
  68. package/coverage/lcov-report/api/routes/metadata.js.html +1 -1
  69. package/coverage/lcov-report/api/routes/{hosting.js.html → migrations.js.html} +17 -14
  70. package/coverage/lcov-report/api/routes/permission.js.html +1 -1
  71. package/coverage/lcov-report/api/routes/query.js.html +1 -1
  72. package/coverage/lcov-report/api/routes/role.js.html +1 -1
  73. package/coverage/lcov-report/api/routes/routing.js.html +1 -1
  74. package/coverage/lcov-report/api/routes/row.js.html +1 -1
  75. package/coverage/lcov-report/api/routes/screen.js.html +1 -1
  76. package/coverage/lcov-report/api/routes/script.js.html +1 -1
  77. package/coverage/lcov-report/api/routes/static.js.html +17 -2
  78. package/coverage/lcov-report/api/routes/table.js.html +1 -1
  79. package/coverage/lcov-report/api/routes/templates.js.html +1 -1
  80. package/coverage/lcov-report/api/routes/user.js.html +1 -1
  81. package/coverage/lcov-report/api/routes/view.js.html +1 -1
  82. package/coverage/lcov-report/api/routes/webhook.js.html +1 -1
  83. package/coverage/lcov-report/automations/actions.js.html +24 -24
  84. package/coverage/lcov-report/automations/automationUtils.js.html +5 -5
  85. package/coverage/lcov-report/automations/bullboard.js.html +16 -16
  86. package/coverage/lcov-report/automations/index.html +1 -1
  87. package/coverage/lcov-report/automations/index.js.html +1 -1
  88. package/coverage/lcov-report/automations/steps/bash.js.html +6 -6
  89. package/coverage/lcov-report/automations/steps/createRow.js.html +13 -19
  90. package/coverage/lcov-report/automations/steps/delay.js.html +4 -4
  91. package/coverage/lcov-report/automations/steps/deleteRow.js.html +11 -20
  92. package/coverage/lcov-report/automations/steps/discord.js.html +7 -7
  93. package/coverage/lcov-report/automations/steps/executeQuery.js.html +6 -6
  94. package/coverage/lcov-report/automations/steps/executeScript.js.html +6 -6
  95. package/coverage/lcov-report/automations/steps/filter.js.html +7 -7
  96. package/coverage/lcov-report/automations/steps/index.html +13 -13
  97. package/coverage/lcov-report/automations/steps/integromat.js.html +5 -5
  98. package/coverage/lcov-report/automations/steps/outgoingWebhook.js.html +8 -8
  99. package/coverage/lcov-report/automations/steps/queryRows.js.html +10 -10
  100. package/coverage/lcov-report/automations/steps/sendSmtpEmail.js.html +5 -5
  101. package/coverage/lcov-report/automations/steps/serverLog.js.html +3 -3
  102. package/coverage/lcov-report/automations/steps/slack.js.html +5 -5
  103. package/coverage/lcov-report/automations/steps/updateRow.js.html +6 -6
  104. package/coverage/lcov-report/automations/steps/utils.js.html +3 -3
  105. package/coverage/lcov-report/automations/steps/zapier.js.html +5 -5
  106. package/coverage/lcov-report/automations/triggerInfo/app.js.html +2 -2
  107. package/coverage/lcov-report/automations/triggerInfo/cron.js.html +2 -2
  108. package/coverage/lcov-report/automations/triggerInfo/index.html +1 -1
  109. package/coverage/lcov-report/automations/triggerInfo/index.js.html +8 -8
  110. package/coverage/lcov-report/automations/triggerInfo/rowDeleted.js.html +2 -2
  111. package/coverage/lcov-report/automations/triggerInfo/rowSaved.js.html +2 -2
  112. package/coverage/lcov-report/automations/triggerInfo/rowUpdated.js.html +2 -2
  113. package/coverage/lcov-report/automations/triggerInfo/webhook.js.html +2 -2
  114. package/coverage/lcov-report/automations/triggers.js.html +18 -18
  115. package/coverage/lcov-report/automations/utils.js.html +32 -32
  116. package/coverage/lcov-report/constants/index.html +11 -11
  117. package/coverage/lcov-report/constants/index.js.html +68 -32
  118. package/coverage/lcov-report/constants/layouts.js.html +5 -5
  119. package/coverage/lcov-report/constants/screens.js.html +4 -4
  120. package/coverage/lcov-report/db/client.js.html +18 -18
  121. package/coverage/lcov-report/db/inMemoryView.js.html +7 -7
  122. package/coverage/lcov-report/db/index.html +15 -15
  123. package/coverage/lcov-report/db/index.js.html +3 -3
  124. package/coverage/lcov-report/db/linkedRows/LinkController.js.html +18 -18
  125. package/coverage/lcov-report/db/linkedRows/index.html +10 -10
  126. package/coverage/lcov-report/db/linkedRows/index.js.html +38 -41
  127. package/coverage/lcov-report/db/linkedRows/linkUtils.js.html +15 -15
  128. package/coverage/lcov-report/db/newid.js.html +4 -4
  129. package/coverage/lcov-report/db/utils.js.html +86 -86
  130. package/coverage/lcov-report/events/AutomationEmitter.js.html +1 -1
  131. package/coverage/lcov-report/events/index.html +1 -1
  132. package/coverage/lcov-report/events/index.js.html +5 -5
  133. package/coverage/lcov-report/events/utils.js.html +3 -3
  134. package/coverage/lcov-report/index.html +117 -102
  135. package/coverage/lcov-report/integrations/tests/TestConfiguration.js.html +1 -1
  136. package/coverage/lcov-report/integrations/tests/index.html +1 -1
  137. package/coverage/lcov-report/middleware/appInfo.js.html +1 -1
  138. package/coverage/lcov-report/middleware/authorized.js.html +166 -64
  139. package/coverage/lcov-report/middleware/builder.js.html +8 -8
  140. package/coverage/lcov-report/middleware/currentapp.js.html +29 -29
  141. package/coverage/lcov-report/middleware/index.html +30 -30
  142. package/coverage/lcov-report/middleware/joi-validator.js.html +3 -3
  143. package/coverage/lcov-report/middleware/resourceId.js.html +1 -1
  144. package/coverage/lcov-report/middleware/selfhost.js.html +1 -1
  145. package/coverage/lcov-report/middleware/usageQuota.js.html +270 -42
  146. package/coverage/lcov-report/middleware/utils.js.html +2 -2
  147. package/coverage/lcov-report/threads/automation.js.html +1 -1
  148. package/coverage/lcov-report/threads/index.html +15 -15
  149. package/coverage/lcov-report/threads/index.js.html +10 -10
  150. package/coverage/lcov-report/threads/query.js.html +23 -8
  151. package/coverage/lcov-report/threads/utils.js.html +14 -14
  152. package/coverage/lcov-report/utilities/bcrypt.js.html +1 -1
  153. package/coverage/lcov-report/utilities/budibaseDir.js.html +7 -7
  154. package/coverage/lcov-report/utilities/centralPath.js.html +4 -4
  155. package/coverage/lcov-report/utilities/csvParser.js.html +6 -6
  156. package/coverage/lcov-report/utilities/global.js.html +26 -26
  157. package/coverage/lcov-report/utilities/index.html +19 -19
  158. package/coverage/lcov-report/utilities/index.js.html +25 -25
  159. package/coverage/lcov-report/utilities/queue/inMemoryQueue.js.html +8 -8
  160. package/coverage/lcov-report/utilities/queue/index.html +1 -1
  161. package/coverage/lcov-report/utilities/routing/index.html +1 -1
  162. package/coverage/lcov-report/utilities/routing/index.js.html +1 -1
  163. package/coverage/lcov-report/utilities/rowProcessor/index.html +27 -27
  164. package/coverage/lcov-report/utilities/rowProcessor/index.js.html +115 -79
  165. package/coverage/lcov-report/utilities/rowProcessor/utils.js.html +75 -27
  166. package/coverage/lcov-report/utilities/scriptRunner.js.html +5 -5
  167. package/coverage/lcov-report/utilities/security.js.html +8 -8
  168. package/coverage/lcov-report/utilities/statusCodes.js.html +1 -1
  169. package/coverage/lcov-report/utilities/usageQuota/index.html +141 -0
  170. package/coverage/lcov-report/utilities/usageQuota/index.js.html +356 -0
  171. package/coverage/lcov-report/utilities/usageQuota/rows.js.html +302 -0
  172. package/coverage/lcov-report/{api/controllers/hosting.js.html → utilities/usageQuota/usageQuoteReset.js.html} +36 -48
  173. package/coverage/lcov-report/utilities/users.js.html +5 -5
  174. package/coverage/lcov-report/utilities/workerRequests.js.html +39 -108
  175. package/coverage/lcov.info +3665 -3040
  176. package/dist/api/controllers/application.js +61 -34
  177. package/dist/api/controllers/auth.js +4 -0
  178. package/dist/api/controllers/datasource.js +11 -0
  179. package/dist/api/controllers/migrations.js +21 -0
  180. package/dist/api/controllers/row/ExternalRequest.js +2 -1
  181. package/dist/api/controllers/row/internal.js +30 -53
  182. package/dist/api/controllers/row/internalSearch.js +12 -4
  183. package/dist/api/controllers/row/staticFormula.js +147 -0
  184. package/dist/api/controllers/row/utils.js +6 -8
  185. package/dist/api/controllers/static/index.js +59 -10
  186. package/dist/api/controllers/table/bulkFormula.js +160 -0
  187. package/dist/api/controllers/table/index.js +12 -9
  188. package/dist/api/controllers/table/internal.js +9 -0
  189. package/dist/api/controllers/table/utils.js +56 -14
  190. package/dist/api/controllers/user.js +2 -0
  191. package/dist/api/routes/index.js +2 -2
  192. package/dist/api/routes/migrations.js +9 -0
  193. package/dist/api/routes/static.js +2 -1
  194. package/dist/app.js +35 -5
  195. package/dist/automations/steps/createRow.js +2 -4
  196. package/dist/automations/steps/deleteRow.js +1 -4
  197. package/dist/constants/index.js +12 -5
  198. package/dist/db/linkedRows/index.js +4 -5
  199. package/dist/definitions/datasource.js +1 -0
  200. package/dist/environment.js +2 -0
  201. package/dist/integrations/base/sql.js +13 -6
  202. package/dist/integrations/googlesheets.js +340 -0
  203. package/dist/integrations/index.js +3 -0
  204. package/dist/integrations/utils.js +57 -8
  205. package/dist/middleware/authorized.js +55 -30
  206. package/dist/middleware/usageQuota.js +80 -13
  207. package/dist/migrations/functions/appUrls.js +37 -0
  208. package/dist/migrations/functions/quotas1.js +50 -0
  209. package/dist/migrations/functions/usageQuotas/index.js +20 -0
  210. package/dist/migrations/functions/usageQuotas/syncApps.js +32 -0
  211. package/dist/migrations/functions/usageQuotas/syncRows.js +35 -0
  212. package/dist/migrations/functions/userEmailViewCasing.js +24 -0
  213. package/dist/migrations/index.js +62 -0
  214. package/dist/package.json +8 -5
  215. package/dist/tests/utilities/TestConfiguration.js +13 -6
  216. package/dist/threads/query.js +4 -0
  217. package/dist/tsconfig.tsbuildinfo +1 -1
  218. package/dist/utilities/rowProcessor/index.js +18 -11
  219. package/dist/utilities/rowProcessor/utils.js +14 -4
  220. package/dist/utilities/{usageQuota.js → usageQuota/index.js} +44 -31
  221. package/dist/utilities/usageQuota/rows.js +77 -0
  222. package/dist/utilities/usageQuota/usageQuoteReset.js +16 -0
  223. package/dist/utilities/workerRequests.js +0 -20
  224. package/nodemon.json +1 -1
  225. package/package.json +9 -6
  226. package/src/api/controllers/application.js +52 -24
  227. package/src/api/controllers/auth.js +4 -0
  228. package/src/api/controllers/datasource.js +12 -0
  229. package/src/api/controllers/migrations.js +13 -0
  230. package/src/api/controllers/row/ExternalRequest.ts +1 -1
  231. package/src/api/controllers/row/internal.js +29 -50
  232. package/src/api/controllers/row/internalSearch.js +12 -4
  233. package/src/api/controllers/row/staticFormula.js +157 -0
  234. package/src/api/controllers/row/utils.js +6 -12
  235. package/src/api/controllers/static/index.js +63 -10
  236. package/src/api/controllers/table/bulkFormula.js +183 -0
  237. package/src/api/controllers/table/index.js +16 -24
  238. package/src/api/controllers/table/internal.js +9 -1
  239. package/src/api/controllers/table/utils.js +80 -11
  240. package/src/api/controllers/user.js +2 -0
  241. package/src/api/routes/index.js +2 -2
  242. package/src/api/routes/migrations.js +14 -0
  243. package/src/api/routes/static.js +5 -0
  244. package/src/api/routes/tests/application.spec.js +2 -2
  245. package/src/api/routes/tests/auth.spec.js +1 -2
  246. package/src/api/routes/tests/static.spec.js +98 -0
  247. package/src/app.ts +18 -5
  248. package/src/automations/steps/createRow.js +2 -4
  249. package/src/automations/steps/deleteRow.js +1 -4
  250. package/src/constants/index.js +17 -5
  251. package/src/db/linkedRows/index.js +4 -5
  252. package/src/definitions/common.ts +3 -0
  253. package/src/definitions/datasource.ts +3 -0
  254. package/src/environment.js +2 -0
  255. package/src/integrations/base/sql.ts +14 -7
  256. package/src/integrations/googlesheets.ts +353 -0
  257. package/src/integrations/index.ts +3 -0
  258. package/src/integrations/tests/sql.spec.js +16 -0
  259. package/src/integrations/utils.ts +71 -10
  260. package/src/middleware/authorized.js +64 -30
  261. package/src/middleware/tests/authorized.spec.js +6 -4
  262. package/src/middleware/tests/usageQuota.spec.js +18 -24
  263. package/src/middleware/usageQuota.js +91 -15
  264. package/src/migrations/functions/appUrls.ts +25 -0
  265. package/src/migrations/functions/quotas1.ts +20 -0
  266. package/src/migrations/functions/tests/appUrls.spec.js +29 -0
  267. package/src/migrations/functions/tests/quotas1.spec.js +27 -0
  268. package/src/migrations/functions/tests/userEmailViewCasing.spec.js +25 -0
  269. package/src/migrations/functions/usageQuotas/index.ts +8 -0
  270. package/src/migrations/functions/usageQuotas/syncApps.ts +18 -0
  271. package/src/migrations/functions/usageQuotas/syncRows.ts +21 -0
  272. package/src/migrations/functions/usageQuotas/tests/syncApps.spec.js +37 -0
  273. package/src/migrations/functions/usageQuotas/tests/syncRows.spec.js +43 -0
  274. package/src/migrations/functions/userEmailViewCasing.ts +13 -0
  275. package/src/migrations/index.ts +56 -0
  276. package/src/tests/utilities/TestConfiguration.js +14 -6
  277. package/src/threads/query.js +5 -0
  278. package/src/utilities/rowProcessor/index.js +24 -12
  279. package/src/utilities/rowProcessor/utils.js +23 -7
  280. package/src/utilities/tests/usageQuota/usageQuota.spec.js +72 -0
  281. package/src/utilities/usageQuota/index.js +92 -0
  282. package/src/utilities/usageQuota/rows.js +74 -0
  283. package/src/utilities/usageQuota/usageQuoteReset.js +18 -0
  284. package/src/utilities/workerRequests.js +0 -23
  285. package/builder/assets/index.39e01193.css +0 -1
  286. package/dist/api/controllers/hosting.js +0 -30
  287. package/dist/api/routes/hosting.js +0 -11
  288. package/src/api/controllers/hosting.js +0 -22
  289. package/src/api/routes/hosting.js +0 -13
  290. package/src/api/routes/tests/hosting.spec.js +0 -36
  291. package/src/utilities/usageQuota.js +0 -73
@@ -14,7 +14,7 @@ const { resolve, join } = require("../../../utilities/centralPath");
14
14
  const uuid = require("uuid");
15
15
  const { ObjectStoreBuckets } = require("../../../constants");
16
16
  const { processString } = require("@budibase/string-templates");
17
- const { getDeployedApps } = require("../../../utilities/workerRequests");
17
+ const { getAllApps } = require("@budibase/backend-core/db");
18
18
  const CouchDB = require("../../../db");
19
19
  const { loadHandlebarsFile, NODE_MODULES_PATH, TOP_LEVEL_PATH, } = require("../../../utilities/fileSystem");
20
20
  const env = require("../../../environment");
@@ -22,6 +22,8 @@ const { clientLibraryPath } = require("../../../utilities");
22
22
  const { upload } = require("../../../utilities/fileSystem");
23
23
  const { attachmentsRelativeURL } = require("../../../utilities");
24
24
  const { DocumentTypes } = require("../../../db/utils");
25
+ const AWS = require("aws-sdk");
26
+ const AWS_REGION = env.AWS_REGION ? env.AWS_REGION : "eu-west-1";
25
27
  function prepareUpload({ s3Key, bucket, metadata, file }) {
26
28
  return __awaiter(this, void 0, void 0, function* () {
27
29
  const response = yield upload({
@@ -41,13 +43,15 @@ function prepareUpload({ s3Key, bucket, metadata, file }) {
41
43
  };
42
44
  });
43
45
  }
44
- function checkForSelfHostedURL(ctx) {
46
+ function getAppIdFromUrl(ctx) {
45
47
  return __awaiter(this, void 0, void 0, function* () {
46
- // the "appId" component of the URL may actually be a specific self hosted URL
48
+ // the "appId" component of the URL can be the id or the custom url
47
49
  let possibleAppUrl = `/${encodeURI(ctx.params.appId).toLowerCase()}`;
48
- const apps = yield getDeployedApps();
49
- if (apps[possibleAppUrl] && apps[possibleAppUrl].appId) {
50
- return apps[possibleAppUrl].appId;
50
+ // search prod apps for a url that matches, exclude dev where id is always used
51
+ const apps = yield getAllApps(CouchDB, { dev: false });
52
+ const app = apps.filter(a => a.url && a.url.toLowerCase() === possibleAppUrl)[0];
53
+ if (app && app.appId) {
54
+ return app.appId;
51
55
  }
52
56
  else {
53
57
  return ctx.params.appId;
@@ -80,10 +84,7 @@ exports.uploadFile = function (ctx) {
80
84
  };
81
85
  exports.serveApp = function (ctx) {
82
86
  return __awaiter(this, void 0, void 0, function* () {
83
- let appId = ctx.params.appId;
84
- if (env.SELF_HOSTED) {
85
- appId = yield checkForSelfHostedURL(ctx);
86
- }
87
+ let appId = yield getAppIdFromUrl(ctx);
87
88
  const App = require("./templates/BudibaseApp.svelte").default;
88
89
  const db = new CouchDB(appId, { skip_setup: true });
89
90
  const appInfo = yield db.get(DocumentTypes.APP_METADATA);
@@ -109,3 +110,51 @@ exports.serveClientLibrary = function (ctx) {
109
110
  });
110
111
  });
111
112
  };
113
+ exports.getSignedUploadURL = function (ctx) {
114
+ var _a, _b, _c;
115
+ return __awaiter(this, void 0, void 0, function* () {
116
+ const database = new CouchDB(ctx.appId);
117
+ // Ensure datasource is valid
118
+ let datasource;
119
+ try {
120
+ const { datasourceId } = ctx.params;
121
+ datasource = yield database.get(datasourceId);
122
+ if (!datasource) {
123
+ ctx.throw(400, "The specified datasource could not be found");
124
+ }
125
+ }
126
+ catch (error) {
127
+ ctx.throw(400, "The specified datasource could not be found");
128
+ }
129
+ // Ensure we aren't using a custom endpoint
130
+ if ((_a = datasource === null || datasource === void 0 ? void 0 : datasource.config) === null || _a === void 0 ? void 0 : _a.endpoint) {
131
+ ctx.throw(400, "S3 datasources with custom endpoints are not supported");
132
+ }
133
+ // Determine type of datasource and generate signed URL
134
+ let signedUrl;
135
+ let publicUrl;
136
+ if (datasource.source === "S3") {
137
+ const { bucket, key } = ctx.request.body || {};
138
+ if (!bucket || !key) {
139
+ ctx.throw(400, "bucket and key values are required");
140
+ return;
141
+ }
142
+ try {
143
+ const s3 = new AWS.S3({
144
+ region: AWS_REGION,
145
+ accessKeyId: (_b = datasource === null || datasource === void 0 ? void 0 : datasource.config) === null || _b === void 0 ? void 0 : _b.accessKeyId,
146
+ secretAccessKey: (_c = datasource === null || datasource === void 0 ? void 0 : datasource.config) === null || _c === void 0 ? void 0 : _c.secretAccessKey,
147
+ apiVersion: "2006-03-01",
148
+ signatureVersion: "v4",
149
+ });
150
+ const params = { Bucket: bucket, Key: key };
151
+ signedUrl = s3.getSignedUrl("putObject", params);
152
+ publicUrl = `https://${bucket}.s3.${AWS_REGION}.amazonaws.com/${key}`;
153
+ }
154
+ catch (error) {
155
+ ctx.throw(400, error);
156
+ }
157
+ }
158
+ ctx.body = { signedUrl, publicUrl };
159
+ });
160
+ };
@@ -0,0 +1,160 @@
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 CouchDB = require("../../../db");
12
+ const { FieldTypes, FormulaTypes } = require("../../../constants");
13
+ const { getAllInternalTables, clearColumns } = require("./utils");
14
+ const { doesContainStrings } = require("@budibase/string-templates");
15
+ const { cloneDeep } = require("lodash/fp");
16
+ const { isEqual, uniq } = require("lodash");
17
+ const { updateAllFormulasInTable } = require("../row/staticFormula");
18
+ function isStaticFormula(column) {
19
+ return (column.type === FieldTypes.FORMULA &&
20
+ column.formulaType === FormulaTypes.STATIC);
21
+ }
22
+ /**
23
+ * This retrieves the formula columns from a table schema that use a specified column name
24
+ * in the formula.
25
+ */
26
+ function getFormulaThatUseColumn(table, columnNames) {
27
+ let formula = [];
28
+ columnNames = Array.isArray(columnNames) ? columnNames : [columnNames];
29
+ for (let column of Object.values(table.schema)) {
30
+ // not a static formula, or doesn't contain a relationship
31
+ if (!isStaticFormula(column)) {
32
+ continue;
33
+ }
34
+ if (!doesContainStrings(column.formula, columnNames)) {
35
+ continue;
36
+ }
37
+ formula.push(column.name);
38
+ }
39
+ return formula;
40
+ }
41
+ /**
42
+ * This functions checks for when a related table, column or related column is deleted, if any
43
+ * tables need to have the formula column removed.
44
+ */
45
+ function checkIfFormulaNeedsCleared(appId, table, { oldTable, deletion }) {
46
+ return __awaiter(this, void 0, void 0, function* () {
47
+ const db = new CouchDB(appId);
48
+ // start by retrieving all tables, remove the current table from the list
49
+ const tables = (yield getAllInternalTables(appId)).filter(tbl => tbl._id !== table._id);
50
+ const schemaToUse = oldTable ? oldTable.schema : table.schema;
51
+ let removedColumns = Object.values(schemaToUse).filter(column => deletion || !table.schema[column.name]);
52
+ // remove any formula columns that used related columns
53
+ for (let removed of removedColumns) {
54
+ let tableToUse = table;
55
+ // if relationship, get the related table
56
+ if (removed.type === FieldTypes.LINK) {
57
+ tableToUse = tables.find(table => table._id === removed.tableId);
58
+ }
59
+ const columnsToDelete = getFormulaThatUseColumn(tableToUse, removed.name);
60
+ if (columnsToDelete.length > 0) {
61
+ yield clearColumns(db, table, columnsToDelete);
62
+ }
63
+ // need a special case, where a column has been removed from this table, but was used
64
+ // in a different, related tables formula
65
+ if (!table.relatedFormula) {
66
+ continue;
67
+ }
68
+ for (let relatedTableId of table.relatedFormula) {
69
+ const relatedColumns = Object.values(table.schema).filter(column => column.tableId === relatedTableId);
70
+ const relatedTable = tables.find(table => table._id === relatedTableId);
71
+ // look to see if the column was used in a relationship formula,
72
+ // relationships won't be used for this
73
+ if (relatedTable && relatedColumns && removed.type !== FieldTypes.LINK) {
74
+ let relatedFormulaToRemove = [];
75
+ for (let column of relatedColumns) {
76
+ relatedFormulaToRemove = relatedFormulaToRemove.concat(getFormulaThatUseColumn(relatedTable, [
77
+ column.fieldName,
78
+ removed.name,
79
+ ]));
80
+ }
81
+ if (relatedFormulaToRemove.length > 0) {
82
+ yield clearColumns(db, relatedTable, uniq(relatedFormulaToRemove));
83
+ }
84
+ }
85
+ }
86
+ }
87
+ });
88
+ }
89
+ /**
90
+ * This function adds a note to related tables that they are
91
+ * used in a static formula - so that the link controller
92
+ * can manage hydrating related rows formula fields. This is
93
+ * specifically only for static formula.
94
+ */
95
+ function updateRelatedFormulaLinksOnTables(appId, table, { deletion } = { deletion: false }) {
96
+ return __awaiter(this, void 0, void 0, function* () {
97
+ const db = new CouchDB(appId);
98
+ // start by retrieving all tables, remove the current table from the list
99
+ const tables = (yield getAllInternalTables(appId)).filter(tbl => tbl._id !== table._id);
100
+ // clone the tables, so we can compare at end
101
+ const initialTables = cloneDeep(tables);
102
+ // first find the related column names
103
+ const relatedColumns = Object.values(table.schema).filter(col => col.type === FieldTypes.LINK);
104
+ // we start by removing the formula field from all tables
105
+ for (let otherTable of tables) {
106
+ if (!otherTable.relatedFormula) {
107
+ continue;
108
+ }
109
+ const index = otherTable.relatedFormula.indexOf(table._id);
110
+ if (index !== -1) {
111
+ otherTable.relatedFormula.splice(index, 1);
112
+ }
113
+ }
114
+ // if deleting, just remove the table IDs, don't try add
115
+ if (!deletion) {
116
+ for (let relatedCol of relatedColumns) {
117
+ let columns = getFormulaThatUseColumn(table, relatedCol.name);
118
+ if (!columns || columns.length === 0) {
119
+ continue;
120
+ }
121
+ const relatedTable = tables.find(related => related._id === relatedCol.tableId);
122
+ // check if the table is already in the list of related formula, if it isn't, then add it
123
+ if (relatedTable &&
124
+ (!relatedTable.relatedFormula ||
125
+ !relatedTable.relatedFormula.includes(table._id))) {
126
+ relatedTable.relatedFormula = relatedTable.relatedFormula
127
+ ? [...relatedTable.relatedFormula, table._id]
128
+ : [table._id];
129
+ }
130
+ }
131
+ }
132
+ // now we just need to compare all the tables and see if any need saved
133
+ for (let initial of initialTables) {
134
+ const found = tables.find(tbl => initial._id === tbl._id);
135
+ if (found && !isEqual(initial, found)) {
136
+ yield db.put(found);
137
+ }
138
+ }
139
+ });
140
+ }
141
+ function checkIfFormulaUpdated(appId, table, { oldTable }) {
142
+ return __awaiter(this, void 0, void 0, function* () {
143
+ // look to see if any formula values have changed
144
+ const shouldUpdate = Object.values(table.schema).find(column => isStaticFormula(column) &&
145
+ (!oldTable ||
146
+ !oldTable.schema[column.name] ||
147
+ !isEqual(oldTable.schema[column.name], column)));
148
+ // if a static formula column has updated, then need to run the update
149
+ if (shouldUpdate != null) {
150
+ yield updateAllFormulasInTable(appId, table);
151
+ }
152
+ });
153
+ }
154
+ exports.runStaticFormulaChecks = (appId, table, { oldTable, deletion }) => __awaiter(void 0, void 0, void 0, function* () {
155
+ yield updateRelatedFormulaLinksOnTables(appId, table, { deletion });
156
+ yield checkIfFormulaNeedsCleared(appId, table, { oldTable, deletion });
157
+ if (!deletion) {
158
+ yield checkIfFormulaUpdated(appId, table, { oldTable });
159
+ }
160
+ });
@@ -12,9 +12,9 @@ const CouchDB = require("../../../db");
12
12
  const internal = require("./internal");
13
13
  const external = require("./external");
14
14
  const csvParser = require("../../../utilities/csvParser");
15
- const { isExternalTable } = require("../../../integrations/utils");
16
- const { getTableParams, getDatasourceParams, BudibaseInternalDB, } = require("../../../db/utils");
17
- const { getTable } = require("./utils");
15
+ const { isExternalTable, isSQL } = require("../../../integrations/utils");
16
+ const { getDatasourceParams } = require("../../../db/utils");
17
+ const { getTable, getAllInternalTables } = require("./utils");
18
18
  function pickApi({ tableId, table }) {
19
19
  if (table && !tableId) {
20
20
  tableId = table._id;
@@ -31,15 +31,18 @@ function pickApi({ tableId, table }) {
31
31
  exports.fetch = function (ctx) {
32
32
  return __awaiter(this, void 0, void 0, function* () {
33
33
  const db = new CouchDB(ctx.appId);
34
- const internalTables = yield db.allDocs(getTableParams(null, {
35
- include_docs: true,
36
- }));
37
- const internal = internalTables.rows.map(row => (Object.assign(Object.assign({}, row.doc), { type: "internal", sourceId: BudibaseInternalDB._id })));
34
+ const internal = yield getAllInternalTables(ctx.appId);
38
35
  const externalTables = yield db.allDocs(getDatasourceParams("plus", {
39
36
  include_docs: true,
40
37
  }));
41
- const external = externalTables.rows.flatMap(row => {
42
- return Object.values(row.doc.entities || {}).map(entity => (Object.assign(Object.assign({}, entity), { type: "external", sourceId: row.doc._id })));
38
+ const external = externalTables.rows.flatMap(tableDoc => {
39
+ let entities = tableDoc.doc.entities;
40
+ if (entities) {
41
+ return Object.values(entities).map(entity => (Object.assign(Object.assign({}, entity), { type: "external", sourceId: tableDoc.doc._id, sql: isSQL(tableDoc.doc) })));
42
+ }
43
+ else {
44
+ return [];
45
+ }
43
46
  });
44
47
  ctx.body = [...internal, ...external];
45
48
  });
@@ -24,6 +24,9 @@ const linkRows = require("../../../db/linkedRows");
24
24
  const { getRowParams, generateTableID } = require("../../../db/utils");
25
25
  const { FieldTypes } = require("../../../constants");
26
26
  const { TableSaveFunctions, hasTypeChanged, getTable, handleDataImport, } = require("./utils");
27
+ const usageQuota = require("../../../utilities/usageQuota");
28
+ const { cleanupAttachments } = require("../../../utilities/rowProcessor");
29
+ const { runStaticFormulaChecks } = require("./bulkFormula");
27
30
  exports.save = function (ctx) {
28
31
  return __awaiter(this, void 0, void 0, function* () {
29
32
  const appId = ctx.appId;
@@ -105,6 +108,8 @@ exports.save = function (ctx) {
105
108
  const result = yield db.put(tableToSave);
106
109
  tableToSave._rev = result.rev;
107
110
  tableToSave = yield tableSaveFunctions.after(tableToSave);
111
+ // has to run after, make sure it has _id
112
+ yield runStaticFormulaChecks(appId, tableToSave, { oldTable });
108
113
  return tableToSave;
109
114
  });
110
115
  };
@@ -118,6 +123,7 @@ exports.destroy = function (ctx) {
118
123
  include_docs: true,
119
124
  }));
120
125
  yield db.bulkDocs(rows.rows.map(row => (Object.assign(Object.assign({}, row.doc), { _deleted: true }))));
126
+ yield usageQuota.update(usageQuota.Properties.ROW, -rows.rows.length);
121
127
  // update linked rows
122
128
  yield linkRows.updateLinks({
123
129
  appId,
@@ -132,6 +138,9 @@ exports.destroy = function (ctx) {
132
138
  if (existingIndex) {
133
139
  yield db.deleteIndex(existingIndex);
134
140
  }
141
+ // has to run after, make sure it has _id
142
+ yield runStaticFormulaChecks(appId, tableToDelete, { deletion: true });
143
+ yield cleanupAttachments(appId, tableToDelete, { rows });
135
144
  return tableToDelete;
136
145
  });
137
146
  };
@@ -10,15 +10,27 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  const CouchDB = require("../../../db");
12
12
  const csvParser = require("../../../utilities/csvParser");
13
- const { getRowParams, generateRowID, InternalTables, } = require("../../../db/utils");
14
- const { isEqual } = require("lodash/fp");
13
+ const { getRowParams, generateRowID, InternalTables, getTableParams, BudibaseInternalDB, } = require("../../../db/utils");
14
+ const { isEqual } = require("lodash");
15
15
  const { AutoFieldSubTypes, FieldTypes } = require("../../../constants");
16
- const { inputProcessing } = require("../../../utilities/rowProcessor");
17
- const { USERS_TABLE_SCHEMA, SwitchableTypes } = require("../../../constants");
18
- const { isExternalTable, breakExternalTableId, } = require("../../../integrations/utils");
16
+ const { inputProcessing, cleanupAttachments, } = require("../../../utilities/rowProcessor");
17
+ const { USERS_TABLE_SCHEMA, SwitchableTypes, CanSwitchTypes, } = require("../../../constants");
18
+ const { isExternalTable, breakExternalTableId, isSQL, } = require("../../../integrations/utils");
19
19
  const { getViews, saveView } = require("../view/utils");
20
20
  const viewTemplate = require("../view/viewBuilder");
21
- exports.checkForColumnUpdates = (db, oldTable, updatedTable) => __awaiter(void 0, void 0, void 0, function* () {
21
+ const usageQuota = require("../../../utilities/usageQuota");
22
+ const { cloneDeep } = require("lodash/fp");
23
+ exports.clearColumns = (appId, table, columnNames) => __awaiter(void 0, void 0, void 0, function* () {
24
+ const db = new CouchDB(appId);
25
+ const rows = yield db.allDocs(getRowParams(table._id, null, {
26
+ include_docs: true,
27
+ }));
28
+ return db.bulkDocs(rows.rows.map(({ doc }) => {
29
+ columnNames.forEach(colName => delete doc[colName]);
30
+ return doc;
31
+ }));
32
+ });
33
+ exports.checkForColumnUpdates = (appId, db, oldTable, updatedTable) => __awaiter(void 0, void 0, void 0, function* () {
22
34
  let updatedRows = [];
23
35
  const rename = updatedTable._rename;
24
36
  let deletedColumns = [];
@@ -31,16 +43,20 @@ exports.checkForColumnUpdates = (db, oldTable, updatedTable) => __awaiter(void 0
31
43
  const rows = yield db.allDocs(getRowParams(updatedTable._id, null, {
32
44
  include_docs: true,
33
45
  }));
34
- updatedRows = rows.rows.map(({ doc }) => {
46
+ const rawRows = rows.rows.map(({ doc }) => doc);
47
+ updatedRows = rawRows.map(row => {
48
+ row = cloneDeep(row);
35
49
  if (rename) {
36
- doc[rename.updated] = doc[rename.old];
37
- delete doc[rename.old];
50
+ row[rename.updated] = row[rename.old];
51
+ delete row[rename.old];
38
52
  }
39
53
  else if (deletedColumns.length !== 0) {
40
- deletedColumns.forEach(colName => delete doc[colName]);
54
+ deletedColumns.forEach(colName => delete row[colName]);
41
55
  }
42
- return doc;
56
+ return row;
43
57
  });
58
+ // cleanup any attachments from object storage for deleted attachment columns
59
+ yield cleanupAttachments(appId, updatedTable, { oldTable, rows: rawRows });
44
60
  // Update views
45
61
  yield exports.checkForViewUpdates(db, updatedTable, rename, deletedColumns);
46
62
  delete updatedTable._rename;
@@ -95,7 +111,11 @@ exports.handleDataImport = (appId, user, table, dataImport) => __awaiter(void 0,
95
111
  }
96
112
  finalData.push(row);
97
113
  }
114
+ yield usageQuota.update(usageQuota.Properties.ROW, finalData.length, {
115
+ dryRun: true,
116
+ });
98
117
  yield db.bulkDocs(finalData);
118
+ yield usageQuota.update(usageQuota.Properties.ROW, finalData.length);
99
119
  let response = yield db.put(table);
100
120
  table._rev = response._rev;
101
121
  return table;
@@ -174,7 +194,7 @@ class TableSaveFunctions {
174
194
  // when confirmed valid
175
195
  mid(table) {
176
196
  return __awaiter(this, void 0, void 0, function* () {
177
- let response = yield exports.checkForColumnUpdates(this.db, this.oldTable, table);
197
+ let response = yield exports.checkForColumnUpdates(this.appId, this.db, this.oldTable, table);
178
198
  this.rows = this.rows.concat(response.rows);
179
199
  return table;
180
200
  });
@@ -191,6 +211,13 @@ class TableSaveFunctions {
191
211
  return this.rows;
192
212
  }
193
213
  }
214
+ exports.getAllInternalTables = (appId) => __awaiter(void 0, void 0, void 0, function* () {
215
+ const db = new CouchDB(appId);
216
+ const internalTables = yield db.allDocs(getTableParams(null, {
217
+ include_docs: true,
218
+ }));
219
+ return internalTables.rows.map(tableDoc => (Object.assign(Object.assign({}, tableDoc.doc), { type: "internal", sourceId: BudibaseInternalDB._id })));
220
+ });
194
221
  exports.getAllExternalTables = (appId, datasourceId) => __awaiter(void 0, void 0, void 0, function* () {
195
222
  const db = new CouchDB(appId);
196
223
  const datasource = yield db.get(datasourceId);
@@ -207,7 +234,9 @@ exports.getTable = (appId, tableId) => __awaiter(void 0, void 0, void 0, functio
207
234
  const db = new CouchDB(appId);
208
235
  if (isExternalTable(tableId)) {
209
236
  let { datasourceId, tableName } = breakExternalTableId(tableId);
210
- return exports.getExternalTable(appId, datasourceId, tableName);
237
+ const datasource = yield db.get(datasourceId);
238
+ const table = yield exports.getExternalTable(appId, datasourceId, tableName);
239
+ return Object.assign(Object.assign({}, table), { sql: isSQL(datasource) });
211
240
  }
212
241
  else {
213
242
  return db.get(tableId);
@@ -295,6 +324,19 @@ exports.foreignKeyStructure = (keyName, meta = null) => {
295
324
  }
296
325
  return structure;
297
326
  };
327
+ exports.areSwitchableTypes = (type1, type2) => {
328
+ if (SwitchableTypes.indexOf(type1) === -1 &&
329
+ SwitchableTypes.indexOf(type2) === -1) {
330
+ return false;
331
+ }
332
+ for (let option of CanSwitchTypes) {
333
+ const index1 = option.indexOf(type1), index2 = option.indexOf(type2);
334
+ if (index1 !== -1 && index2 !== -1 && index1 !== index2) {
335
+ return true;
336
+ }
337
+ }
338
+ return false;
339
+ };
298
340
  exports.hasTypeChanged = (table, oldTable) => {
299
341
  if (!oldTable) {
300
342
  return false;
@@ -305,7 +347,7 @@ exports.hasTypeChanged = (table, oldTable) => {
305
347
  continue;
306
348
  }
307
349
  const newType = table.schema[key].type;
308
- if (oldType !== newType && SwitchableTypes.indexOf(oldType) === -1) {
350
+ if (oldType !== newType && !exports.areSwitchableTypes(oldType, newType)) {
309
351
  return true;
310
352
  }
311
353
  }
@@ -155,6 +155,8 @@ exports.updateSelfMetadata = function (ctx) {
155
155
  ctx.request.body._id = ctx.user._id;
156
156
  // make sure no stale rev
157
157
  delete ctx.request.body._rev;
158
+ // make sure no csrf token
159
+ delete ctx.request.body.csrfToken;
158
160
  yield exports.updateMetadata(ctx);
159
161
  });
160
162
  };
@@ -21,11 +21,11 @@ const integrationRoutes = require("./integration");
21
21
  const permissionRoutes = require("./permission");
22
22
  const datasourceRoutes = require("./datasource");
23
23
  const queryRoutes = require("./query");
24
- const hostingRoutes = require("./hosting");
25
24
  const backupRoutes = require("./backup");
26
25
  const metadataRoutes = require("./metadata");
27
26
  const devRoutes = require("./dev");
28
27
  const cloudRoutes = require("./cloud");
28
+ const migrationRoutes = require("./migrations");
29
29
  exports.mainRoutes = [
30
30
  authRoutes,
31
31
  deployRoutes,
@@ -46,7 +46,6 @@ exports.mainRoutes = [
46
46
  permissionRoutes,
47
47
  datasourceRoutes,
48
48
  queryRoutes,
49
- hostingRoutes,
50
49
  backupRoutes,
51
50
  metadataRoutes,
52
51
  devRoutes,
@@ -55,5 +54,6 @@ exports.mainRoutes = [
55
54
  // this could be breaking as koa may recognise other routes as this
56
55
  tableRoutes,
57
56
  rowRoutes,
57
+ migrationRoutes,
58
58
  ];
59
59
  exports.staticRoutes = staticRoutes;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ const Router = require("@koa/router");
3
+ const migrationsController = require("../controllers/migrations");
4
+ const router = Router();
5
+ const { internalApi } = require("@budibase/backend-core/auth");
6
+ router
7
+ .post("/api/migrations/run", internalApi, migrationsController.migrate)
8
+ .get("/api/migrations/definitions", internalApi, migrationsController.fetchDefinitions);
9
+ module.exports = router;
@@ -43,5 +43,6 @@ router
43
43
  .post("/api/attachments/process", authorized(BUILDER), controller.uploadFile)
44
44
  .post("/api/attachments/:tableId/upload", paramResource("tableId"), authorized(PermissionTypes.TABLE, PermissionLevels.WRITE), controller.uploadFile)
45
45
  // TODO: this likely needs to be secured in some way
46
- .get("/:appId/:path*", controller.serveApp);
46
+ .get("/:appId/:path*", controller.serveApp)
47
+ .post("/api/attachments/:datasourceId/url", authorized(PermissionTypes.TABLE, PermissionLevels.READ), controller.getSignedUploadURL);
47
48
  module.exports = router;
package/dist/app.js CHANGED
@@ -1,4 +1,23 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
11
+ }) : function(o, v) {
12
+ o["default"] = v;
13
+ });
14
+ var __importStar = (this && this.__importStar) || function (mod) {
15
+ if (mod && mod.__esModule) return mod;
16
+ var result = {};
17
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
+ __setModuleDefault(result, mod);
19
+ return result;
20
+ };
2
21
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
22
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
23
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -9,7 +28,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
28
  });
10
29
  };
11
30
  Object.defineProperty(exports, "__esModule", { value: true });
12
- const env = require("./environment");
31
+ const env = __importStar(require("./environment"));
13
32
  const CouchDB = require("./db");
14
33
  require("@budibase/backend-core").init(CouchDB);
15
34
  const Koa = require("koa");
@@ -24,6 +43,7 @@ const Sentry = require("@sentry/node");
24
43
  const fileSystem = require("./utilities/fileSystem");
25
44
  const bullboard = require("./automations/bullboard");
26
45
  const redis = require("./utilities/redis");
46
+ const migrations = __importStar(require("./migrations"));
27
47
  const app = new Koa();
28
48
  // set up top level koa middleware
29
49
  app.use(koaBody({
@@ -76,12 +96,22 @@ module.exports = server.listen(env.PORT || 0, () => __awaiter(void 0, void 0, vo
76
96
  yield redis.init();
77
97
  yield automations.init();
78
98
  }));
79
- process.on("uncaughtException", err => {
80
- console.error(err);
99
+ const shutdown = () => {
81
100
  server.close();
82
101
  server.destroy();
102
+ };
103
+ process.on("uncaughtException", err => {
104
+ console.error(err);
105
+ shutdown();
83
106
  });
84
107
  process.on("SIGTERM", () => {
85
- server.close();
86
- server.destroy();
108
+ shutdown();
87
109
  });
110
+ // run migrations on startup if not done via http
111
+ // not recommended in a clustered environment
112
+ if (!env.HTTP_MIGRATIONS) {
113
+ migrations.migrate().catch(err => {
114
+ console.error("Error performing migrations. Exiting.\n", err);
115
+ shutdown();
116
+ });
117
+ }
@@ -10,7 +10,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  const rowController = require("../../api/controllers/row");
12
12
  const automationUtils = require("../automationUtils");
13
- const env = require("../../environment");
14
13
  const usage = require("../../utilities/usageQuota");
15
14
  const { buildCtx } = require("./utils");
16
15
  exports.definition = {
@@ -87,10 +86,9 @@ exports.run = function ({ inputs, appId, emitter }) {
87
86
  });
88
87
  try {
89
88
  inputs.row = yield automationUtils.cleanUpRow(appId, inputs.row.tableId, inputs.row);
90
- if (env.USE_QUOTAS) {
91
- yield usage.update(usage.Properties.ROW, 1);
92
- }
89
+ yield usage.update(usage.Properties.ROW, 1, { dryRun: true });
93
90
  yield rowController.save(ctx);
91
+ yield usage.update(usage.Properties.ROW, 1);
94
92
  return {
95
93
  row: inputs.row,
96
94
  response: ctx.body,
@@ -9,7 +9,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  const rowController = require("../../api/controllers/row");
12
- const env = require("../../environment");
13
12
  const usage = require("../../utilities/usageQuota");
14
13
  const { buildCtx } = require("./utils");
15
14
  const automationUtils = require("../automationUtils");
@@ -81,9 +80,7 @@ exports.run = function ({ inputs, appId, emitter }) {
81
80
  },
82
81
  });
83
82
  try {
84
- if (env.isProd()) {
85
- yield usage.update(usage.Properties.ROW, -1);
86
- }
83
+ yield usage.update(usage.Properties.ROW, -1);
87
84
  yield rowController.destroy(ctx);
88
85
  return {
89
86
  response: ctx.body,