@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
@@ -8,9 +8,7 @@ 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 core = require("@budibase/backend-core");
12
- const CouchDB = require("../../db");
13
- core.init(CouchDB);
11
+ require("../../db").init();
14
12
  const { BUILTIN_ROLE_IDS } = require("@budibase/backend-core/roles");
15
13
  const env = require("../../environment");
16
14
  const { basicTable, basicRow, basicRole, basicAutomation, basicDatasource, basicQuery, basicScreen, basicLayout, basicWebhook, TENANT_ID, } = require("./structures");
@@ -19,7 +17,7 @@ const supertest = require("supertest");
19
17
  const { cleanup } = require("../../utilities/fileSystem");
20
18
  const { Cookies, Headers } = require("@budibase/backend-core/constants");
21
19
  const { jwt } = require("@budibase/backend-core/auth");
22
- const { getGlobalDB } = require("@budibase/backend-core/tenancy");
20
+ const { doInTenant, doWithGlobalDB } = require("@budibase/backend-core/tenancy");
23
21
  const { createASession } = require("@budibase/backend-core/sessions");
24
22
  const { user: userCache } = require("@budibase/backend-core/cache");
25
23
  const newid = require("../../db/newid");
@@ -50,8 +48,23 @@ class TestConfiguration {
50
48
  getAppId() {
51
49
  return this.appId;
52
50
  }
53
- getCouch() {
54
- return CouchDB;
51
+ doInContext(appId, task) {
52
+ return __awaiter(this, void 0, void 0, function* () {
53
+ if (!appId) {
54
+ appId = this.appId;
55
+ }
56
+ return doInTenant(TENANT_ID, () => {
57
+ // check if already in a context
58
+ if (context.getAppId() == null && appId !== null) {
59
+ return context.doInAppContext(appId, () => __awaiter(this, void 0, void 0, function* () {
60
+ return task();
61
+ }));
62
+ }
63
+ else {
64
+ return task();
65
+ }
66
+ });
67
+ });
55
68
  }
56
69
  _req(config, params, controlFunc) {
57
70
  return __awaiter(this, void 0, void 0, function* () {
@@ -65,66 +78,57 @@ class TestConfiguration {
65
78
  request.request = {
66
79
  body: config,
67
80
  };
68
- function run() {
69
- return __awaiter(this, void 0, void 0, function* () {
70
- if (params) {
71
- request.params = params;
72
- }
73
- yield controlFunc(request);
74
- return request.body;
75
- });
76
- }
77
- // check if already in a context
78
- if (context.getAppId() == null && this.appId !== null) {
79
- return context.doInAppContext(this.appId, () => __awaiter(this, void 0, void 0, function* () {
80
- return run();
81
- }));
82
- }
83
- else {
84
- return run();
85
- }
81
+ return this.doInContext(this.appId, () => __awaiter(this, void 0, void 0, function* () {
82
+ if (params) {
83
+ request.params = params;
84
+ }
85
+ yield controlFunc(request);
86
+ return request.body;
87
+ }));
86
88
  });
87
89
  }
88
90
  generateApiKey(userId = GLOBAL_USER_ID) {
89
91
  return __awaiter(this, void 0, void 0, function* () {
90
- const db = getGlobalDB(TENANT_ID);
91
- const id = generateDevInfoID(userId);
92
- let devInfo;
93
- try {
94
- devInfo = yield db.get(id);
95
- }
96
- catch (err) {
97
- devInfo = { _id: id, userId };
98
- }
99
- devInfo.apiKey = encrypt(`${TENANT_ID}${SEPARATOR}${newid()}`);
100
- yield db.put(devInfo);
101
- return devInfo.apiKey;
92
+ return doWithGlobalDB(TENANT_ID, (db) => __awaiter(this, void 0, void 0, function* () {
93
+ const id = generateDevInfoID(userId);
94
+ let devInfo;
95
+ try {
96
+ devInfo = yield db.get(id);
97
+ }
98
+ catch (err) {
99
+ devInfo = { _id: id, userId };
100
+ }
101
+ devInfo.apiKey = encrypt(`${TENANT_ID}${SEPARATOR}${newid()}`);
102
+ yield db.put(devInfo);
103
+ return devInfo.apiKey;
104
+ }));
102
105
  });
103
106
  }
104
107
  globalUser({ id = GLOBAL_USER_ID, builder = true, email = EMAIL, roles, } = {}) {
105
108
  return __awaiter(this, void 0, void 0, function* () {
106
- const db = getGlobalDB(TENANT_ID);
107
- let existing;
108
- try {
109
- existing = yield db.get(id);
110
- }
111
- catch (err) {
112
- existing = { email };
113
- }
114
- const user = Object.assign(Object.assign({ _id: id }, existing), { roles: roles || {}, tenantId: TENANT_ID });
115
- yield createASession(id, {
116
- sessionId: "sessionid",
117
- tenantId: TENANT_ID,
118
- csrfToken: CSRF_TOKEN,
119
- });
120
- if (builder) {
121
- user.builder = { global: true };
122
- }
123
- else {
124
- user.builder = { global: false };
125
- }
126
- const resp = yield db.put(user);
127
- return Object.assign({ _rev: resp._rev }, user);
109
+ return doWithGlobalDB(TENANT_ID, (db) => __awaiter(this, void 0, void 0, function* () {
110
+ let existing;
111
+ try {
112
+ existing = yield db.get(id);
113
+ }
114
+ catch (err) {
115
+ existing = { email };
116
+ }
117
+ const user = Object.assign(Object.assign({ _id: id }, existing), { roles: roles || {}, tenantId: TENANT_ID });
118
+ yield createASession(id, {
119
+ sessionId: "sessionid",
120
+ tenantId: TENANT_ID,
121
+ csrfToken: CSRF_TOKEN,
122
+ });
123
+ if (builder) {
124
+ user.builder = { global: true };
125
+ }
126
+ else {
127
+ user.builder = { global: false };
128
+ }
129
+ const resp = yield db.put(user);
130
+ return Object.assign({ _rev: resp._rev }, user);
131
+ }));
128
132
  });
129
133
  }
130
134
  // use a new id as the name to avoid name collisions
@@ -182,9 +186,12 @@ class TestConfiguration {
182
186
  createApp(appName) {
183
187
  return __awaiter(this, void 0, void 0, function* () {
184
188
  // create dev app
189
+ // clear any old app
190
+ this.appId = null;
191
+ yield context.updateAppId(null);
185
192
  this.app = yield this._req({ name: appName }, null, controllers.app.create);
186
193
  this.appId = this.app.appId;
187
- context.updateAppId(this.appId);
194
+ yield context.updateAppId(this.appId);
188
195
  // create production app
189
196
  this.prodApp = yield this.deploy();
190
197
  this.prodAppId = this.prodApp.appId;
@@ -34,12 +34,14 @@ class Thread {
34
34
  maxConcurrentWorkers: this.count,
35
35
  };
36
36
  if (opts.timeoutMs) {
37
+ this.timeoutMs = opts.timeoutMs;
37
38
  workerOpts.maxCallTime = opts.timeoutMs;
38
39
  }
39
40
  this.workers = workerFarm(workerOpts, typeToFile(type));
40
41
  }
41
42
  }
42
43
  run(data) {
44
+ const timeoutMs = this.timeoutMs;
43
45
  return new Promise((resolve, reject) => {
44
46
  let fncToCall;
45
47
  // if in test then don't use threading
@@ -50,7 +52,10 @@ class Thread {
50
52
  fncToCall = this.workers;
51
53
  }
52
54
  fncToCall(data, (err, response) => {
53
- if (err) {
55
+ if (err && err.type === "TimeoutError") {
56
+ reject(new Error(`Query response time exceeded ${timeoutMs}ms timeout.`));
57
+ }
58
+ else if (err) {
54
59
  reject(err);
55
60
  }
56
61
  else {
@@ -12,10 +12,10 @@ const threadUtils = require("./utils");
12
12
  threadUtils.threadSetup();
13
13
  const ScriptRunner = require("../utilities/scriptRunner");
14
14
  const { integrations } = require("../integrations");
15
- const { processStringSync, findHBSBlocks, } = require("@budibase/string-templates");
15
+ const { processStringSync } = require("@budibase/string-templates");
16
16
  const { doInAppContext, getAppDB } = require("@budibase/backend-core/context");
17
17
  const { isSQL } = require("../integrations/utils");
18
- const CONST_CHAR_REGEX = new RegExp("'[^']*'", "g");
18
+ const { enrichQueryFields, interpolateSQL, } = require("../integrations/queries/sql");
19
19
  class QueryRunner {
20
20
  constructor(input, flags = { noRecursiveQuery: false }) {
21
21
  this.datasource = input.datasource;
@@ -33,67 +33,6 @@ class QueryRunner {
33
33
  this.queryResponse = {};
34
34
  this.hasRerun = false;
35
35
  }
36
- interpolateSQL(fields, parameters, integration) {
37
- let sql = fields.sql;
38
- if (!sql) {
39
- return fields;
40
- }
41
- const bindings = findHBSBlocks(sql);
42
- let variables = [], arrays = [];
43
- for (let binding of bindings) {
44
- // look for array/list operations in the SQL statement, which will need handled later
45
- const listRegexMatch = sql.match(new RegExp(`(in|IN|In|iN)( )+[(]?${binding}[)]?`));
46
- // check if the variable was used as part of a string concat e.g. 'Hello {{binding}}'
47
- // start by finding all the instances of const character strings
48
- const charConstMatch = sql.match(CONST_CHAR_REGEX) || [];
49
- // now look within them to see if a binding is used
50
- const charConstBindingMatch = charConstMatch.find(string => string.match(new RegExp(`'[^']*${binding}[^']*'`)));
51
- if (charConstBindingMatch) {
52
- let [part1, part2] = charConstBindingMatch.split(binding);
53
- part1 = `'${part1.substring(1)}'`;
54
- part2 = `'${part2.substring(0, part2.length - 1)}'`;
55
- sql = sql.replace(charConstBindingMatch, integration.getStringConcat([
56
- part1,
57
- integration.getBindingIdentifier(),
58
- part2,
59
- ]));
60
- }
61
- // generate SQL parameterised array
62
- else if (listRegexMatch) {
63
- arrays.push(binding);
64
- // determine the length of the array
65
- const value = this.enrichQueryFields([binding], parameters)[0].split(",");
66
- // build a string like ($1, $2, $3)
67
- let replacement = `${Array.apply(null, Array(value.length))
68
- .map(() => integration.getBindingIdentifier())
69
- .join(",")}`;
70
- // check if parentheses are needed
71
- if (!listRegexMatch[0].includes(`(${binding})`)) {
72
- replacement = `(${replacement})`;
73
- }
74
- sql = sql.replace(binding, replacement);
75
- }
76
- else {
77
- sql = sql.replace(binding, integration.getBindingIdentifier());
78
- }
79
- variables.push(binding);
80
- }
81
- // replicate the knex structure
82
- fields.sql = sql;
83
- fields.bindings = this.enrichQueryFields(variables, parameters);
84
- // check for arrays in the data
85
- let updated = [];
86
- for (let i = 0; i < variables.length; i++) {
87
- if (arrays.includes(variables[i])) {
88
- updated = updated.concat(fields.bindings[i].split(","));
89
- }
90
- else {
91
- updated.push(fields.bindings[i]);
92
- }
93
- }
94
- fields.bindings = updated;
95
- return fields;
96
- }
97
36
  execute() {
98
37
  return __awaiter(this, void 0, void 0, function* () {
99
38
  let { datasource, fields, queryVerb, transformer } = this;
@@ -107,10 +46,10 @@ class QueryRunner {
107
46
  let query;
108
47
  // handle SQL injections by interpolating the variables
109
48
  if (isSQL(datasource)) {
110
- query = this.interpolateSQL(fields, parameters, integration);
49
+ query = interpolateSQL(fields, parameters, integration);
111
50
  }
112
51
  else {
113
- query = this.enrichQueryFields(fields, parameters);
52
+ query = enrichQueryFields(fields, parameters);
114
53
  }
115
54
  // Add pagination values for REST queries
116
55
  if (this.pagination) {
@@ -237,44 +176,6 @@ class QueryRunner {
237
176
  return parameters;
238
177
  });
239
178
  }
240
- enrichQueryFields(fields, parameters = {}) {
241
- const enrichedQuery = Array.isArray(fields) ? [] : {};
242
- // enrich the fields with dynamic parameters
243
- for (let key of Object.keys(fields)) {
244
- if (fields[key] == null) {
245
- continue;
246
- }
247
- if (typeof fields[key] === "object") {
248
- // enrich nested fields object
249
- enrichedQuery[key] = this.enrichQueryFields(fields[key], parameters);
250
- }
251
- else if (typeof fields[key] === "string") {
252
- // enrich string value as normal
253
- enrichedQuery[key] = processStringSync(fields[key], parameters, {
254
- noEscaping: true,
255
- noHelpers: true,
256
- escapeNewlines: true,
257
- });
258
- }
259
- else {
260
- enrichedQuery[key] = fields[key];
261
- }
262
- }
263
- if (enrichedQuery.json ||
264
- enrichedQuery.customData ||
265
- enrichedQuery.requestBody) {
266
- try {
267
- enrichedQuery.json = JSON.parse(enrichedQuery.json ||
268
- enrichedQuery.customData ||
269
- enrichedQuery.requestBody);
270
- }
271
- catch (err) {
272
- // no json found, ignore
273
- }
274
- delete enrichedQuery.customData;
275
- }
276
- return enrichedQuery;
277
- }
278
179
  }
279
180
  module.exports = (input, callback) => {
280
181
  doInAppContext(input.appId, () => {
@@ -9,8 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  const env = require("../environment");
12
- const CouchDB = require("../db");
13
- const { init } = require("@budibase/backend-core");
12
+ const db = require("../db");
14
13
  const redis = require("@budibase/backend-core/redis");
15
14
  const { SEPARATOR } = require("@budibase/backend-core/db");
16
15
  const VARIABLE_TTL_SECONDS = 3600;
@@ -34,7 +33,7 @@ exports.threadSetup = () => {
34
33
  }
35
34
  // when thread starts, make sure it is recorded
36
35
  env.setInThread();
37
- init(CouchDB);
36
+ db.init();
38
37
  };
39
38
  function makeVariableKey(queryId, variable) {
40
39
  return `${queryId}${SEPARATOR}${variable}`;