@budibase/server 1.0.113 → 1.0.116

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 (238) hide show
  1. package/builder/assets/{index.e3e5909b.js → index.28d8c64f.js} +4 -4
  2. package/builder/index.html +1 -1
  3. package/coverage/clover.xml +1212 -1269
  4. package/coverage/coverage-final.json +86 -87
  5. package/coverage/lcov-report/api/controllers/analytics.js.html +1 -1
  6. package/coverage/lcov-report/api/controllers/apikeys.js.html +1 -1
  7. package/coverage/lcov-report/api/controllers/application.js.html +126 -120
  8. package/coverage/lcov-report/api/controllers/auth.js.html +1 -1
  9. package/coverage/lcov-report/api/controllers/automation.js.html +21 -21
  10. package/coverage/lcov-report/api/controllers/backup.js.html +1 -1
  11. package/coverage/lcov-report/api/controllers/cloud.js.html +1 -1
  12. package/coverage/lcov-report/api/controllers/component.js.html +1 -1
  13. package/coverage/lcov-report/api/controllers/datasource.js.html +16 -16
  14. package/coverage/lcov-report/api/controllers/dev.js.html +23 -14
  15. package/coverage/lcov-report/api/controllers/index.html +17 -17
  16. package/coverage/lcov-report/api/controllers/integration.js.html +1 -1
  17. package/coverage/lcov-report/api/controllers/layout.js.html +6 -6
  18. package/coverage/lcov-report/api/controllers/metadata.js.html +1 -1
  19. package/coverage/lcov-report/api/controllers/migrations.js.html +1 -1
  20. package/coverage/lcov-report/api/controllers/permission.js.html +15 -15
  21. package/coverage/lcov-report/api/controllers/query/index.html +1 -1
  22. package/coverage/lcov-report/api/controllers/query/index.js.html +19 -19
  23. package/coverage/lcov-report/api/controllers/query/validation.js.html +1 -1
  24. package/coverage/lcov-report/api/controllers/role.js.html +9 -9
  25. package/coverage/lcov-report/api/controllers/routing.js.html +1 -1
  26. package/coverage/lcov-report/api/controllers/row/external.js.html +19 -19
  27. package/coverage/lcov-report/api/controllers/row/index.html +10 -10
  28. package/coverage/lcov-report/api/controllers/row/index.js.html +14 -14
  29. package/coverage/lcov-report/api/controllers/row/internal.js.html +37 -34
  30. package/coverage/lcov-report/api/controllers/row/internalSearch.js.html +9 -9
  31. package/coverage/lcov-report/api/controllers/row/staticFormula.js.html +10 -10
  32. package/coverage/lcov-report/api/controllers/row/utils.js.html +12 -12
  33. package/coverage/lcov-report/api/controllers/screen.js.html +7 -7
  34. package/coverage/lcov-report/api/controllers/script.js.html +4 -4
  35. package/coverage/lcov-report/api/controllers/table/bulkFormula.js.html +9 -9
  36. package/coverage/lcov-report/api/controllers/table/external.js.html +12 -12
  37. package/coverage/lcov-report/api/controllers/table/index.html +1 -1
  38. package/coverage/lcov-report/api/controllers/table/index.js.html +14 -14
  39. package/coverage/lcov-report/api/controllers/table/internal.js.html +13 -13
  40. package/coverage/lcov-report/api/controllers/table/utils.js.html +30 -30
  41. package/coverage/lcov-report/api/controllers/templates.js.html +1 -1
  42. package/coverage/lcov-report/api/controllers/user.js.html +19 -19
  43. package/coverage/lcov-report/api/controllers/view/exporters.js.html +4 -4
  44. package/coverage/lcov-report/api/controllers/view/index.html +1 -1
  45. package/coverage/lcov-report/api/controllers/view/index.js.html +14 -14
  46. package/coverage/lcov-report/api/controllers/view/utils.js.html +12 -12
  47. package/coverage/lcov-report/api/controllers/view/viewBuilder.js.html +7 -7
  48. package/coverage/lcov-report/api/controllers/webhook.js.html +17 -17
  49. package/coverage/lcov-report/api/index.html +1 -1
  50. package/coverage/lcov-report/api/index.js.html +1 -1
  51. package/coverage/lcov-report/api/routes/analytics.js.html +1 -1
  52. package/coverage/lcov-report/api/routes/apikeys.js.html +1 -1
  53. package/coverage/lcov-report/api/routes/application.js.html +1 -1
  54. package/coverage/lcov-report/api/routes/auth.js.html +1 -1
  55. package/coverage/lcov-report/api/routes/automation.js.html +1 -1
  56. package/coverage/lcov-report/api/routes/backup.js.html +1 -1
  57. package/coverage/lcov-report/api/routes/cloud.js.html +1 -1
  58. package/coverage/lcov-report/api/routes/component.js.html +1 -1
  59. package/coverage/lcov-report/api/routes/datasource.js.html +1 -1
  60. package/coverage/lcov-report/api/routes/deploy.js.html +1 -1
  61. package/coverage/lcov-report/api/routes/dev.js.html +1 -1
  62. package/coverage/lcov-report/api/routes/index.html +1 -1
  63. package/coverage/lcov-report/api/routes/index.js.html +1 -1
  64. package/coverage/lcov-report/api/routes/integration.js.html +1 -1
  65. package/coverage/lcov-report/api/routes/layout.js.html +1 -1
  66. package/coverage/lcov-report/api/routes/metadata.js.html +1 -1
  67. package/coverage/lcov-report/api/routes/migrations.js.html +1 -1
  68. package/coverage/lcov-report/api/routes/permission.js.html +1 -1
  69. package/coverage/lcov-report/api/routes/query.js.html +1 -1
  70. package/coverage/lcov-report/api/routes/role.js.html +1 -1
  71. package/coverage/lcov-report/api/routes/routing.js.html +1 -1
  72. package/coverage/lcov-report/api/routes/row.js.html +1 -1
  73. package/coverage/lcov-report/api/routes/screen.js.html +1 -1
  74. package/coverage/lcov-report/api/routes/script.js.html +1 -1
  75. package/coverage/lcov-report/api/routes/static.js.html +1 -1
  76. package/coverage/lcov-report/api/routes/table.js.html +1 -1
  77. package/coverage/lcov-report/api/routes/templates.js.html +1 -1
  78. package/coverage/lcov-report/api/routes/user.js.html +1 -1
  79. package/coverage/lcov-report/api/routes/utils/index.html +1 -1
  80. package/coverage/lcov-report/api/routes/utils/validators.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 +5 -5
  87. package/coverage/lcov-report/automations/index.js.html +1 -1
  88. package/coverage/lcov-report/automations/steps/bash.js.html +7 -7
  89. package/coverage/lcov-report/automations/steps/createRow.js.html +7 -7
  90. package/coverage/lcov-report/automations/steps/delay.js.html +4 -4
  91. package/coverage/lcov-report/automations/steps/deleteRow.js.html +7 -7
  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 +1 -1
  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 +43 -40
  116. package/coverage/lcov-report/constants/index.html +1 -1
  117. package/coverage/lcov-report/constants/index.js.html +27 -27
  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/inMemoryView.js.html +52 -49
  121. package/coverage/lcov-report/db/index.html +21 -36
  122. package/coverage/lcov-report/db/index.js.html +49 -10
  123. package/coverage/lcov-report/db/linkedRows/LinkController.js.html +7 -7
  124. package/coverage/lcov-report/db/linkedRows/index.html +1 -1
  125. package/coverage/lcov-report/db/linkedRows/index.js.html +18 -18
  126. package/coverage/lcov-report/db/linkedRows/linkUtils.js.html +13 -13
  127. package/coverage/lcov-report/db/newid.js.html +2 -2
  128. package/coverage/lcov-report/db/utils.js.html +13 -13
  129. package/coverage/lcov-report/events/AutomationEmitter.js.html +1 -1
  130. package/coverage/lcov-report/events/index.html +1 -1
  131. package/coverage/lcov-report/events/index.js.html +5 -5
  132. package/coverage/lcov-report/events/utils.js.html +3 -3
  133. package/coverage/lcov-report/index.html +52 -52
  134. package/coverage/lcov-report/integrations/tests/TestConfiguration.js.html +1 -1
  135. package/coverage/lcov-report/integrations/tests/index.html +1 -1
  136. package/coverage/lcov-report/middleware/appInfo.js.html +1 -1
  137. package/coverage/lcov-report/middleware/authorized.js.html +1 -1
  138. package/coverage/lcov-report/middleware/builder.js.html +15 -12
  139. package/coverage/lcov-report/middleware/currentapp.js.html +1 -1
  140. package/coverage/lcov-report/middleware/index.html +4 -4
  141. package/coverage/lcov-report/middleware/joi-validator.js.html +1 -1
  142. package/coverage/lcov-report/middleware/publicApi.js.html +1 -1
  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 +1 -1
  146. package/coverage/lcov-report/middleware/utils.js.html +1 -1
  147. package/coverage/lcov-report/threads/automation.js.html +1 -1
  148. package/coverage/lcov-report/threads/index.html +34 -34
  149. package/coverage/lcov-report/threads/index.js.html +35 -17
  150. package/coverage/lcov-report/threads/query.js.html +20 -356
  151. package/coverage/lcov-report/threads/utils.js.html +19 -22
  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 +15 -15
  157. package/coverage/lcov-report/utilities/index.html +1 -1
  158. package/coverage/lcov-report/utilities/index.js.html +26 -26
  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 +1 -1
  164. package/coverage/lcov-report/utilities/rowProcessor/index.js.html +18 -18
  165. package/coverage/lcov-report/utilities/rowProcessor/utils.js.html +4 -4
  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 +13 -13
  170. package/coverage/lcov-report/utilities/usageQuota/index.js.html +8 -8
  171. package/coverage/lcov-report/utilities/usageQuota/rows.js.html +38 -26
  172. package/coverage/lcov-report/utilities/usageQuota/usageQuoteReset.js.html +1 -1
  173. package/coverage/lcov-report/utilities/users.js.html +5 -5
  174. package/coverage/lcov-report/utilities/workerRequests.js.html +28 -28
  175. package/coverage/lcov.info +1570 -1660
  176. package/dist/api/controllers/application.js +4 -1
  177. package/dist/api/controllers/deploy/index.js +5 -1
  178. package/dist/api/controllers/dev.js +9 -5
  179. package/dist/api/controllers/row/internal.js +2 -1
  180. package/dist/api/controllers/webhook.js +2 -2
  181. package/dist/api/routes/tests/utilities/TestFunctions.js +1 -4
  182. package/dist/app.js +5 -2
  183. package/dist/automations/utils.js +6 -5
  184. package/dist/db/inMemoryView.js +33 -31
  185. package/dist/db/index.js +13 -2
  186. package/dist/integrations/couchdb.js +19 -25
  187. package/dist/integrations/queries/sql.js +108 -0
  188. package/dist/middleware/builder.js +10 -9
  189. package/dist/migrations/index.js +1 -5
  190. package/dist/package.json +5 -5
  191. package/dist/tests/utilities/TestConfiguration.js +66 -59
  192. package/dist/threads/index.js +6 -1
  193. package/dist/threads/query.js +4 -103
  194. package/dist/threads/utils.js +2 -3
  195. package/dist/tsconfig.tsbuildinfo +1 -1
  196. package/dist/utilities/fileSystem/index.js +27 -26
  197. package/dist/utilities/usageQuota/rows.js +16 -16
  198. package/package.json +6 -6
  199. package/scripts/exportAppTemplate.js +10 -8
  200. package/scripts/integrations/mysql/init.sql +1 -0
  201. package/src/api/controllers/application.js +3 -1
  202. package/src/api/controllers/deploy/index.js +4 -4
  203. package/src/api/controllers/dev.js +9 -6
  204. package/src/api/controllers/query/import/tests/index.spec.js +7 -11
  205. package/src/api/controllers/row/internal.js +2 -1
  206. package/src/api/controllers/webhook.js +2 -2
  207. package/src/api/routes/tests/row.spec.js +15 -11
  208. package/src/api/routes/tests/table.spec.js +3 -2
  209. package/src/api/routes/tests/utilities/TestFunctions.js +1 -5
  210. package/src/app.ts +2 -2
  211. package/src/automations/tests/deleteRow.spec.js +1 -1
  212. package/src/automations/utils.js +6 -5
  213. package/src/db/inMemoryView.js +37 -36
  214. package/src/db/index.js +15 -2
  215. package/src/db/tests/linkTests.spec.js +8 -7
  216. package/src/integrations/couchdb.ts +28 -24
  217. package/src/integrations/queries/sql.ts +125 -0
  218. package/src/integrations/tests/airtable.spec.js +3 -1
  219. package/src/integrations/tests/couchdb.spec.js +4 -1
  220. package/src/middleware/builder.js +10 -9
  221. package/src/middleware/tests/authorized.spec.js +0 -1
  222. package/src/middleware/tests/usageQuota.spec.js +6 -20
  223. package/src/migrations/functions/tests/appUrls.spec.js +8 -11
  224. package/src/migrations/functions/tests/userEmailViewCasing.spec.js +13 -10
  225. package/src/migrations/functions/usageQuotas/tests/syncApps.spec.js +25 -21
  226. package/src/migrations/functions/usageQuotas/tests/syncRows.spec.js +26 -24
  227. package/src/migrations/index.ts +1 -2
  228. package/src/module.d.ts +8 -0
  229. package/src/tests/utilities/TestConfiguration.js +62 -55
  230. package/src/threads/index.js +7 -1
  231. package/src/threads/query.js +7 -119
  232. package/src/threads/utils.js +2 -3
  233. package/src/utilities/fileSystem/index.js +30 -30
  234. package/src/utilities/usageQuota/rows.js +22 -18
  235. package/coverage/lcov-report/db/client.js.html +0 -173
  236. package/dist/db/client.js +0 -25
  237. package/scripts/replicateApp.js +0 -50
  238. package/src/db/client.js +0 -31
@@ -92,7 +92,7 @@ function createInstance(template) {
92
92
  const tenantId = isMultiTenant() ? getTenantId() : null;
93
93
  const baseAppId = generateAppID(tenantId);
94
94
  const appId = generateDevAppID(baseAppId);
95
- updateAppId(appId);
95
+ yield updateAppId(appId);
96
96
  const db = getAppDB();
97
97
  yield db.put({
98
98
  _id: "_design/database",
@@ -347,6 +347,9 @@ exports.sync = (ctx, next) => __awaiter(void 0, void 0, void 0, function* () {
347
347
  catch (err) {
348
348
  error = err;
349
349
  }
350
+ finally {
351
+ yield replication.close();
352
+ }
350
353
  // sync the users
351
354
  yield syncGlobalUsers();
352
355
  if (error) {
@@ -81,11 +81,12 @@ function initDeployedApp(prodAppId) {
81
81
  }
82
82
  function deployApp(deployment) {
83
83
  return __awaiter(this, void 0, void 0, function* () {
84
+ let replication;
84
85
  try {
85
86
  const appId = getAppId();
86
87
  const devAppId = getDevelopmentAppID(appId);
87
88
  const productionAppId = getProdAppID(appId);
88
- const replication = new Replication({
89
+ replication = new Replication({
89
90
  source: devAppId,
90
91
  target: productionAppId,
91
92
  });
@@ -109,6 +110,9 @@ function deployApp(deployment) {
109
110
  yield storeDeploymentHistory(deployment);
110
111
  throw Object.assign(Object.assign({}, err), { message: `Deployment Failed: ${err.message}` });
111
112
  }
113
+ finally {
114
+ yield replication.close();
115
+ }
112
116
  });
113
117
  }
114
118
  exports.fetchDeployments = function (ctx) {
@@ -81,8 +81,9 @@ exports.revert = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
81
81
  try {
82
82
  const db = getProdAppDB({ skip_setup: true });
83
83
  const info = yield db.info();
84
- if (info.error)
84
+ if (info.error) {
85
85
  throw info.error;
86
+ }
86
87
  const deploymentDoc = yield db.get(DocumentTypes.DEPLOYMENTS);
87
88
  if (!deploymentDoc.history ||
88
89
  Object.keys(deploymentDoc.history).length === 0) {
@@ -92,11 +93,11 @@ exports.revert = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
92
93
  catch (err) {
93
94
  return ctx.throw(400, "App has not yet been deployed");
94
95
  }
96
+ const replication = new Replication({
97
+ source: productionAppId,
98
+ target: appId,
99
+ });
95
100
  try {
96
- const replication = new Replication({
97
- source: productionAppId,
98
- target: appId,
99
- });
100
101
  yield replication.rollback();
101
102
  // update appID in reverted app to be dev version again
102
103
  const db = getAppDB();
@@ -112,6 +113,9 @@ exports.revert = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
112
113
  catch (err) {
113
114
  ctx.throw(400, `Unable to revert. ${err}`);
114
115
  }
116
+ finally {
117
+ yield replication.close();
118
+ }
115
119
  });
116
120
  exports.getBudibaseVersion = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
117
121
  ctx.body = {
@@ -21,6 +21,7 @@ var __rest = (this && this.__rest) || function (s, e) {
21
21
  };
22
22
  const linkRows = require("../../../db/linkedRows");
23
23
  const { generateRowID, getRowParams, getTableIDFromRowID, DocumentTypes, InternalTables, } = require("../../../db/utils");
24
+ const { dangerousGetDB } = require("@budibase/backend-core/db");
24
25
  const userController = require("../user");
25
26
  const { inputProcessing, outputProcessing, cleanupAttachments, } = require("../../../utilities/rowProcessor");
26
27
  const { FieldTypes } = require("../../../constants");
@@ -233,7 +234,7 @@ exports.fetch = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
233
234
  return outputProcessing(table, rows);
234
235
  });
235
236
  exports.find = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
236
- const db = getAppDB();
237
+ const db = dangerousGetDB(ctx.appId);
237
238
  const table = yield db.get(ctx.params.tableId);
238
239
  let row = yield findRow(ctx, ctx.params.tableId, ctx.params.rowId);
239
240
  row = yield outputProcessing(table, row);
@@ -55,7 +55,7 @@ exports.destroy = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
55
55
  ctx.body = yield db.remove(ctx.params.id, ctx.params.rev);
56
56
  });
57
57
  exports.buildSchema = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
58
- updateAppId(ctx.params.instance);
58
+ yield updateAppId(ctx.params.instance);
59
59
  const db = getAppDB();
60
60
  const webhook = yield db.get(ctx.params.id);
61
61
  webhook.bodySchema = toJsonSchema(ctx.request.body);
@@ -80,7 +80,7 @@ exports.buildSchema = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
80
80
  });
81
81
  exports.trigger = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
82
82
  const prodAppId = getProdAppID(ctx.params.instance);
83
- updateAppId(prodAppId);
83
+ yield updateAppId(prodAppId);
84
84
  try {
85
85
  const db = getAppDB();
86
86
  const webhook = yield db.get(ctx.params.id);
@@ -13,7 +13,7 @@ const appController = require("../../../controllers/application");
13
13
  const { AppStatus } = require("../../../../db/utils");
14
14
  const { BUILTIN_ROLE_IDS } = require("@budibase/backend-core/roles");
15
15
  const { TENANT_ID } = require("../../../../tests/utilities/structures");
16
- const { getAppDB, doInAppContext } = require("@budibase/backend-core/context");
16
+ const { doInAppContext } = require("@budibase/backend-core/context");
17
17
  const env = require("../../../../environment");
18
18
  function Request(appId, params) {
19
19
  this.appId = appId;
@@ -101,9 +101,6 @@ exports.checkPermissionsEndpoint = ({ config, method, url, body, passRole, failR
101
101
  .set(failHeader)
102
102
  .expect(403);
103
103
  });
104
- exports.getDB = () => {
105
- return getAppDB();
106
- };
107
104
  exports.testAutomation = (config, automation) => __awaiter(void 0, void 0, void 0, function* () {
108
105
  return runRequest(automation.appId, () => __awaiter(void 0, void 0, void 0, function* () {
109
106
  return yield config.request
package/dist/app.js CHANGED
@@ -27,10 +27,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
27
27
  step((generator = generator.apply(thisArg, _arguments || [])).next());
28
28
  });
29
29
  };
30
+ var __importDefault = (this && this.__importDefault) || function (mod) {
31
+ return (mod && mod.__esModule) ? mod : { "default": mod };
32
+ };
30
33
  Object.defineProperty(exports, "__esModule", { value: true });
31
34
  const env = __importStar(require("./environment"));
32
- const CouchDB = require("./db");
33
- require("@budibase/backend-core").init(CouchDB);
35
+ const db_1 = __importDefault(require("./db"));
36
+ db_1.default.init();
34
37
  const Koa = require("koa");
35
38
  const destroyable = require("server-destroy");
36
39
  const koaBody = require("koa-body");
@@ -11,13 +11,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  const { Thread, ThreadType } = require("../threads");
12
12
  const { definitions } = require("./triggerInfo");
13
13
  const webhooks = require("../api/controllers/webhook");
14
- const CouchDB = require("../db");
15
14
  const { queue } = require("./bullboard");
16
15
  const newid = require("../db/newid");
17
16
  const { updateEntityMetadata } = require("../utilities");
18
17
  const { MetadataTypes, WebhookType } = require("../constants");
19
18
  const { getProdAppID } = require("@budibase/backend-core/db");
20
19
  const { cloneDeep } = require("lodash/fp");
20
+ const { doWithDB } = require("@budibase/backend-core/db");
21
21
  const { getAppDB, getAppId } = require("@budibase/backend-core/context");
22
22
  const WH_STEP_ID = definitions.WEBHOOK.stepId;
23
23
  const CRON_STEP_ID = definitions.CRON.stepId;
@@ -94,10 +94,11 @@ exports.enableCronTrigger = (appId, automation) => __awaiter(void 0, void 0, voi
94
94
  // can't use getAppDB here as this is likely to be called from dev app,
95
95
  // but this call could be for dev app or prod app, need to just use what
96
96
  // was passed in
97
- const db = new CouchDB(appId);
98
- const response = yield db.put(automation);
99
- automation._id = response.id;
100
- automation._rev = response.rev;
97
+ yield doWithDB(appId, (db) => __awaiter(void 0, void 0, void 0, function* () {
98
+ const response = yield db.put(automation);
99
+ automation._id = response.id;
100
+ automation._rev = response.rev;
101
+ }));
101
102
  }
102
103
  return automation;
103
104
  });
@@ -8,44 +8,46 @@ 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 PouchDB = require("pouchdb");
12
- const memory = require("pouchdb-adapter-memory");
13
11
  const newid = require("./newid");
14
- PouchDB.plugin(memory);
15
- const Pouch = PouchDB.defaults({
16
- prefix: undefined,
17
- adapter: "memory",
18
- });
12
+ // bypass the main application db config
13
+ // use in memory pouchdb directly
14
+ const { getPouch, closeDB } = require("@budibase/backend-core/db");
15
+ const Pouch = getPouch({ inMemory: true });
19
16
  exports.runView = (view, calculation, group, data) => __awaiter(void 0, void 0, void 0, function* () {
20
17
  // use a different ID each time for the DB, make sure they
21
18
  // are always unique for each query, don't want overlap
22
19
  // which could cause 409s
23
20
  const db = new Pouch(newid());
24
- // write all the docs to the in memory Pouch (remove revs)
25
- yield db.bulkDocs(data.map(row => (Object.assign(Object.assign({}, row), { _rev: undefined }))));
26
- let fn = (doc, emit) => emit(doc._id);
27
- eval("fn = " + view.map.replace("function (doc)", "function (doc, emit)"));
28
- const queryFns = {
29
- meta: view.meta,
30
- map: fn,
31
- };
32
- if (view.reduce) {
33
- queryFns.reduce = view.reduce;
34
- }
35
- const response = yield db.query(queryFns, {
36
- include_docs: !calculation,
37
- group: !!group,
38
- });
39
- // need to fix the revs to be totally accurate
40
- for (let row of response.rows) {
41
- if (!row._rev || !row._id) {
42
- continue;
21
+ try {
22
+ // write all the docs to the in memory Pouch (remove revs)
23
+ yield db.bulkDocs(data.map(row => (Object.assign(Object.assign({}, row), { _rev: undefined }))));
24
+ let fn = (doc, emit) => emit(doc._id);
25
+ eval("fn = " + view.map.replace("function (doc)", "function (doc, emit)"));
26
+ const queryFns = {
27
+ meta: view.meta,
28
+ map: fn,
29
+ };
30
+ if (view.reduce) {
31
+ queryFns.reduce = view.reduce;
43
32
  }
44
- const found = data.find(possible => possible._id === row._id);
45
- if (found) {
46
- row._rev = found._rev;
33
+ const response = yield db.query(queryFns, {
34
+ include_docs: !calculation,
35
+ group: !!group,
36
+ });
37
+ // need to fix the revs to be totally accurate
38
+ for (let row of response.rows) {
39
+ if (!row._rev || !row._id) {
40
+ continue;
41
+ }
42
+ const found = data.find(possible => possible._id === row._id);
43
+ if (found) {
44
+ row._rev = found._rev;
45
+ }
47
46
  }
47
+ return response;
48
+ }
49
+ finally {
50
+ yield db.destroy();
51
+ yield closeDB(db);
48
52
  }
49
- yield db.destroy();
50
- return response;
51
53
  });
package/dist/db/index.js CHANGED
@@ -1,3 +1,14 @@
1
1
  "use strict";
2
- const client = require("./client");
3
- module.exports = client;
2
+ const core = require("@budibase/backend-core");
3
+ const env = require("../environment");
4
+ exports.init = () => {
5
+ const dbConfig = {
6
+ replication: true,
7
+ find: true,
8
+ };
9
+ if (env.isTest()) {
10
+ dbConfig.inMemory = true;
11
+ dbConfig.allDbs = true;
12
+ }
13
+ core.init({ db: dbConfig });
14
+ };
@@ -54,49 +54,43 @@ var CouchDBModule;
54
54
  this.config = config;
55
55
  this.client = new PouchDB(`${config.url}/${config.database}`);
56
56
  }
57
- create(query) {
57
+ query(command, errorMsg, query) {
58
58
  return __awaiter(this, void 0, void 0, function* () {
59
59
  try {
60
- return this.client.post(query.json);
60
+ const response = yield this.client[command](query.id || query.json);
61
+ yield this.client.close();
62
+ return response;
61
63
  }
62
64
  catch (err) {
63
- console.error("Error writing to couchDB", err);
65
+ console.error(errorMsg, err);
64
66
  throw err;
65
67
  }
66
68
  });
67
69
  }
70
+ create(query) {
71
+ return __awaiter(this, void 0, void 0, function* () {
72
+ return this.query("post", "Error writing to couchDB", query);
73
+ });
74
+ }
68
75
  read(query) {
69
76
  return __awaiter(this, void 0, void 0, function* () {
70
- try {
71
- const result = yield this.client.allDocs(Object.assign({ include_docs: true }, query.json));
72
- return result.rows.map((row) => row.doc);
73
- }
74
- catch (err) {
75
- console.error("Error querying couchDB", err);
76
- throw err;
77
- }
77
+ const result = yield this.query("allDocs", "Error querying couchDB", {
78
+ json: Object.assign({ include_docs: true }, query.json),
79
+ });
80
+ return result.rows.map((row) => row.doc);
78
81
  });
79
82
  }
80
83
  update(query) {
81
84
  return __awaiter(this, void 0, void 0, function* () {
82
- try {
83
- return this.client.put(query.json);
84
- }
85
- catch (err) {
86
- console.error("Error updating couchDB document", err);
87
- throw err;
88
- }
85
+ return this.query("put", "Error updating couchDB document", query);
89
86
  });
90
87
  }
91
88
  delete(query) {
92
89
  return __awaiter(this, void 0, void 0, function* () {
93
- try {
94
- return yield this.client.remove(query.id);
95
- }
96
- catch (err) {
97
- console.error("Error deleting couchDB document", err);
98
- throw err;
99
- }
90
+ const doc = yield this.query("get", "Cannot find doc to be deleted", query);
91
+ return this.query("remove", "Error deleting couchDB document", {
92
+ json: doc,
93
+ });
100
94
  });
101
95
  }
102
96
  }
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.interpolateSQL = exports.enrichQueryFields = void 0;
4
+ const string_templates_1 = require("@budibase/string-templates");
5
+ const CONST_CHAR_REGEX = new RegExp("'[^']*'", "g");
6
+ function enrichQueryFields(fields, parameters = {}) {
7
+ const enrichedQuery = Array.isArray(fields) ? [] : {};
8
+ // enrich the fields with dynamic parameters
9
+ for (let key of Object.keys(fields)) {
10
+ if (fields[key] == null) {
11
+ continue;
12
+ }
13
+ if (typeof fields[key] === "object") {
14
+ // enrich nested fields object
15
+ enrichedQuery[key] = enrichQueryFields(fields[key], parameters);
16
+ }
17
+ else if (typeof fields[key] === "string") {
18
+ // enrich string value as normal
19
+ enrichedQuery[key] = string_templates_1.processStringSync(fields[key], parameters, {
20
+ noEscaping: true,
21
+ noHelpers: true,
22
+ escapeNewlines: true,
23
+ });
24
+ }
25
+ else {
26
+ enrichedQuery[key] = fields[key];
27
+ }
28
+ }
29
+ if (enrichedQuery.json ||
30
+ enrichedQuery.customData ||
31
+ enrichedQuery.requestBody) {
32
+ try {
33
+ enrichedQuery.json = JSON.parse(enrichedQuery.json ||
34
+ enrichedQuery.customData ||
35
+ enrichedQuery.requestBody);
36
+ }
37
+ catch (err) {
38
+ // no json found, ignore
39
+ }
40
+ delete enrichedQuery.customData;
41
+ }
42
+ return enrichedQuery;
43
+ }
44
+ exports.enrichQueryFields = enrichQueryFields;
45
+ function interpolateSQL(fields, parameters, integration) {
46
+ let sql = fields.sql;
47
+ if (!sql || typeof sql !== "string") {
48
+ return fields;
49
+ }
50
+ const bindings = string_templates_1.findHBSBlocks(sql);
51
+ let variables = [], arrays = [];
52
+ for (let binding of bindings) {
53
+ // look for array/list operations in the SQL statement, which will need handled later
54
+ const listRegexMatch = sql.match(new RegExp(`(in|IN|In|iN)( )+[(]?${binding}[)]?`));
55
+ // check if the variable was used as part of a string concat e.g. 'Hello {{binding}}'
56
+ // start by finding all the instances of const character strings
57
+ const charConstMatch = sql.match(CONST_CHAR_REGEX) || [];
58
+ // now look within them to see if a binding is used
59
+ const charConstBindingMatch = charConstMatch.find((string) => string.match(new RegExp(`'[^']*${binding}[^']*'`)));
60
+ if (charConstBindingMatch) {
61
+ let [part1, part2] = charConstBindingMatch.split(binding);
62
+ part1 = `'${part1.substring(1)}'`;
63
+ part2 = `'${part2.substring(0, part2.length - 1)}'`;
64
+ sql = sql.replace(charConstBindingMatch, integration.getStringConcat([
65
+ part1,
66
+ integration.getBindingIdentifier(),
67
+ part2,
68
+ ]));
69
+ }
70
+ // generate SQL parameterised array
71
+ else if (listRegexMatch) {
72
+ arrays.push(binding);
73
+ // determine the length of the array
74
+ const value = enrichQueryFields([binding], parameters)[0]
75
+ .split(",")
76
+ .map((val) => val.trim());
77
+ // build a string like ($1, $2, $3)
78
+ let replacement = `${Array.apply(null, Array(value.length))
79
+ .map(() => integration.getBindingIdentifier())
80
+ .join(",")}`;
81
+ // check if parentheses are needed
82
+ if (!listRegexMatch[0].includes(`(${binding})`)) {
83
+ replacement = `(${replacement})`;
84
+ }
85
+ sql = sql.replace(binding, replacement);
86
+ }
87
+ else {
88
+ sql = sql.replace(binding, integration.getBindingIdentifier());
89
+ }
90
+ variables.push(binding);
91
+ }
92
+ // replicate the knex structure
93
+ fields.sql = sql;
94
+ fields.bindings = enrichQueryFields(variables, parameters);
95
+ // check for arrays in the data
96
+ let updated = [];
97
+ for (let i = 0; i < variables.length; i++) {
98
+ if (arrays.includes(variables[i])) {
99
+ updated = updated.concat(fields.bindings[i].split(",").map((val) => val.trim()));
100
+ }
101
+ else {
102
+ updated.push(fields.bindings[i]);
103
+ }
104
+ }
105
+ fields.bindings = updated;
106
+ return fields;
107
+ }
108
+ exports.interpolateSQL = interpolateSQL;
@@ -10,7 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  const { APP_DEV_PREFIX } = require("../db/utils");
12
12
  const { doesUserHaveLock, updateLock, checkDebounce, setDebounce, } = require("../utilities/redis");
13
- const { getDB } = require("@budibase/backend-core/db");
13
+ const { doWithDB } = require("@budibase/backend-core/db");
14
14
  const { DocumentTypes } = require("../db/utils");
15
15
  const { PermissionTypes } = require("@budibase/backend-core/permissions");
16
16
  const { app: appCache } = require("@budibase/backend-core/cache");
@@ -49,14 +49,15 @@ function updateAppUpdatedAt(ctx) {
49
49
  if (ctx.method === "GET" || (yield checkDebounce(appId))) {
50
50
  return;
51
51
  }
52
- const db = getDB(appId);
53
- const metadata = yield db.get(DocumentTypes.APP_METADATA);
54
- metadata.updatedAt = new Date().toISOString();
55
- const response = yield db.put(metadata);
56
- metadata._rev = response.rev;
57
- yield appCache.invalidateAppMetadata(appId, metadata);
58
- // set a new debounce record with a short TTL
59
- yield setDebounce(appId, DEBOUNCE_TIME_SEC);
52
+ yield doWithDB(appId, (db) => __awaiter(this, void 0, void 0, function* () {
53
+ const metadata = yield db.get(DocumentTypes.APP_METADATA);
54
+ metadata.updatedAt = new Date().toISOString();
55
+ const response = yield db.put(metadata);
56
+ metadata._rev = response.rev;
57
+ yield appCache.invalidateAppMetadata(appId, metadata);
58
+ // set a new debounce record with a short TTL
59
+ yield setDebounce(appId, DEBOUNCE_TIME_SEC);
60
+ }));
60
61
  });
61
62
  }
62
63
  module.exports = (ctx, permType) => __awaiter(void 0, void 0, void 0, function* () {
@@ -27,12 +27,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
27
27
  step((generator = generator.apply(thisArg, _arguments || [])).next());
28
28
  });
29
29
  };
30
- var __importDefault = (this && this.__importDefault) || function (mod) {
31
- return (mod && mod.__esModule) ? mod : { "default": mod };
32
- };
33
30
  Object.defineProperty(exports, "__esModule", { value: true });
34
31
  exports.migrate = exports.MIGRATIONS = void 0;
35
- const db_1 = __importDefault(require("../db"));
36
32
  const { MIGRATION_TYPES, runMigrations, } = require("@budibase/backend-core/migrations");
37
33
  // migration functions
38
34
  const userEmailViewCasing = __importStar(require("./functions/userEmailViewCasing"));
@@ -57,6 +53,6 @@ exports.MIGRATIONS = [
57
53
  },
58
54
  ];
59
55
  const migrate = (options) => __awaiter(void 0, void 0, void 0, function* () {
60
- yield runMigrations(db_1.default, exports.MIGRATIONS, options);
56
+ yield runMigrations(exports.MIGRATIONS, options);
61
57
  });
62
58
  exports.migrate = migrate;
package/dist/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@budibase/server",
3
3
  "email": "hi@budibase.com",
4
- "version": "1.0.112",
4
+ "version": "1.0.115",
5
5
  "description": "Budibase Web Server",
6
6
  "main": "src/index.ts",
7
7
  "repository": {
@@ -68,9 +68,9 @@
68
68
  "license": "GPL-3.0",
69
69
  "dependencies": {
70
70
  "@apidevtools/swagger-parser": "^10.0.3",
71
- "@budibase/backend-core": "^1.0.112",
72
- "@budibase/client": "^1.0.112",
73
- "@budibase/string-templates": "^1.0.112",
71
+ "@budibase/backend-core": "^1.0.115",
72
+ "@budibase/client": "^1.0.115",
73
+ "@budibase/string-templates": "^1.0.115",
74
74
  "@bull-board/api": "^3.7.0",
75
75
  "@bull-board/koa": "^3.7.0",
76
76
  "@elastic/elasticsearch": "7.10.0",
@@ -117,7 +117,7 @@
117
117
  "pg": "8.5.1",
118
118
  "pino-pretty": "4.0.0",
119
119
  "posthog-node": "^1.1.4",
120
- "pouchdb": "7.2.1",
120
+ "pouchdb": "7.3.0",
121
121
  "pouchdb-adapter-memory": "^7.2.1",
122
122
  "pouchdb-all-dbs": "1.0.2",
123
123
  "pouchdb-find": "^7.2.2",