@budibase/server 1.0.212-alpha.12 → 1.0.212-alpha.15

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 (202) hide show
  1. package/__mocks__/node-fetch.ts +9 -0
  2. package/builder/assets/{index.969c2236.css → index.7786f497.css} +2 -2
  3. package/builder/assets/index.83448a6c.js +977 -0
  4. package/builder/assets/{vendor.9bf2fcb4.js → vendor.001dcf8c.js} +163 -163
  5. package/builder/index.html +3 -3
  6. package/coverage/clover.xml +636 -590
  7. package/coverage/coverage-final.json +20 -20
  8. package/coverage/lcov-report/api/controllers/apikeys.js.html +1 -1
  9. package/coverage/lcov-report/api/controllers/auth.js.html +1 -1
  10. package/coverage/lcov-report/api/controllers/automation.js.html +111 -12
  11. package/coverage/lcov-report/api/controllers/backup.js.html +1 -1
  12. package/coverage/lcov-report/api/controllers/cloud.js.html +1 -1
  13. package/coverage/lcov-report/api/controllers/component.js.html +1 -1
  14. package/coverage/lcov-report/api/controllers/datasource.js.html +16 -16
  15. package/coverage/lcov-report/api/controllers/dev.js.html +1 -1
  16. package/coverage/lcov-report/api/controllers/index.html +19 -19
  17. package/coverage/lcov-report/api/controllers/integration.js.html +1 -1
  18. package/coverage/lcov-report/api/controllers/layout.js.html +1 -1
  19. package/coverage/lcov-report/api/controllers/metadata.js.html +1 -1
  20. package/coverage/lcov-report/api/controllers/migrations.js.html +1 -1
  21. package/coverage/lcov-report/api/controllers/permission.js.html +1 -1
  22. package/coverage/lcov-report/api/controllers/query/index.html +1 -1
  23. package/coverage/lcov-report/api/controllers/query/validation.js.html +1 -1
  24. package/coverage/lcov-report/api/controllers/role.js.html +1 -1
  25. package/coverage/lcov-report/api/controllers/routing.js.html +1 -1
  26. package/coverage/lcov-report/api/controllers/row/external.js.html +1 -1
  27. package/coverage/lcov-report/api/controllers/row/index.html +1 -1
  28. package/coverage/lcov-report/api/controllers/row/internal.js.html +1 -1
  29. package/coverage/lcov-report/api/controllers/row/internalSearch.js.html +1 -1
  30. package/coverage/lcov-report/api/controllers/row/staticFormula.js.html +1 -1
  31. package/coverage/lcov-report/api/controllers/row/utils.js.html +1 -1
  32. package/coverage/lcov-report/api/controllers/screen.js.html +1 -1
  33. package/coverage/lcov-report/api/controllers/script.js.html +1 -1
  34. package/coverage/lcov-report/api/controllers/table/bulkFormula.js.html +1 -1
  35. package/coverage/lcov-report/api/controllers/table/external.js.html +1 -1
  36. package/coverage/lcov-report/api/controllers/table/index.html +1 -1
  37. package/coverage/lcov-report/api/controllers/table/index.js.html +1 -1
  38. package/coverage/lcov-report/api/controllers/templates.js.html +1 -1
  39. package/coverage/lcov-report/api/controllers/user.js.html +1 -1
  40. package/coverage/lcov-report/api/controllers/view/exporters.js.html +1 -1
  41. package/coverage/lcov-report/api/controllers/view/index.html +1 -1
  42. package/coverage/lcov-report/api/controllers/view/index.js.html +1 -1
  43. package/coverage/lcov-report/api/controllers/view/utils.js.html +1 -1
  44. package/coverage/lcov-report/api/controllers/view/viewBuilder.js.html +1 -1
  45. package/coverage/lcov-report/api/controllers/webhook.js.html +1 -1
  46. package/coverage/lcov-report/api/index.html +15 -15
  47. package/coverage/lcov-report/api/index.js.html +22 -13
  48. package/coverage/lcov-report/api/routes/analytics.js.html +1 -1
  49. package/coverage/lcov-report/api/routes/apikeys.js.html +1 -1
  50. package/coverage/lcov-report/api/routes/auth.js.html +1 -1
  51. package/coverage/lcov-report/api/routes/automation.js.html +32 -2
  52. package/coverage/lcov-report/api/routes/backup.js.html +1 -1
  53. package/coverage/lcov-report/api/routes/cloud.js.html +1 -1
  54. package/coverage/lcov-report/api/routes/component.js.html +1 -1
  55. package/coverage/lcov-report/api/routes/datasource.js.html +1 -1
  56. package/coverage/lcov-report/api/routes/deploy.js.html +1 -1
  57. package/coverage/lcov-report/api/routes/dev.js.html +1 -1
  58. package/coverage/lcov-report/api/routes/index.html +1 -1
  59. package/coverage/lcov-report/api/routes/integration.js.html +1 -1
  60. package/coverage/lcov-report/api/routes/layout.js.html +1 -1
  61. package/coverage/lcov-report/api/routes/metadata.js.html +1 -1
  62. package/coverage/lcov-report/api/routes/migrations.js.html +1 -1
  63. package/coverage/lcov-report/api/routes/permission.js.html +1 -1
  64. package/coverage/lcov-report/api/routes/query.js.html +1 -1
  65. package/coverage/lcov-report/api/routes/role.js.html +1 -1
  66. package/coverage/lcov-report/api/routes/routing.js.html +1 -1
  67. package/coverage/lcov-report/api/routes/screen.js.html +1 -1
  68. package/coverage/lcov-report/api/routes/script.js.html +1 -1
  69. package/coverage/lcov-report/api/routes/table.js.html +1 -1
  70. package/coverage/lcov-report/api/routes/templates.js.html +1 -1
  71. package/coverage/lcov-report/api/routes/user.js.html +1 -1
  72. package/coverage/lcov-report/api/routes/utils/index.html +1 -1
  73. package/coverage/lcov-report/api/routes/utils/validators.js.html +1 -1
  74. package/coverage/lcov-report/api/routes/view.js.html +1 -1
  75. package/coverage/lcov-report/api/routes/webhook.js.html +1 -1
  76. package/coverage/lcov-report/automations/actions.js.html +1 -1
  77. package/coverage/lcov-report/automations/automationUtils.js.html +1 -1
  78. package/coverage/lcov-report/automations/bullboard.js.html +1 -1
  79. package/coverage/lcov-report/automations/index.html +1 -1
  80. package/coverage/lcov-report/automations/index.js.html +1 -1
  81. package/coverage/lcov-report/automations/steps/bash.js.html +1 -1
  82. package/coverage/lcov-report/automations/steps/delay.js.html +1 -1
  83. package/coverage/lcov-report/automations/steps/discord.js.html +1 -1
  84. package/coverage/lcov-report/automations/steps/executeQuery.js.html +1 -1
  85. package/coverage/lcov-report/automations/steps/executeScript.js.html +1 -1
  86. package/coverage/lcov-report/automations/steps/filter.js.html +58 -34
  87. package/coverage/lcov-report/automations/steps/index.html +11 -11
  88. package/coverage/lcov-report/automations/steps/integromat.js.html +1 -1
  89. package/coverage/lcov-report/automations/steps/loop.js.html +1 -1
  90. package/coverage/lcov-report/automations/steps/outgoingWebhook.js.html +1 -1
  91. package/coverage/lcov-report/automations/steps/queryRows.js.html +1 -1
  92. package/coverage/lcov-report/automations/steps/sendSmtpEmail.js.html +1 -1
  93. package/coverage/lcov-report/automations/steps/serverLog.js.html +1 -1
  94. package/coverage/lcov-report/automations/steps/slack.js.html +1 -1
  95. package/coverage/lcov-report/automations/steps/updateRow.js.html +1 -1
  96. package/coverage/lcov-report/automations/steps/utils.js.html +1 -1
  97. package/coverage/lcov-report/automations/steps/zapier.js.html +1 -1
  98. package/coverage/lcov-report/automations/triggerInfo/app.js.html +1 -1
  99. package/coverage/lcov-report/automations/triggerInfo/cron.js.html +1 -1
  100. package/coverage/lcov-report/automations/triggerInfo/index.html +1 -1
  101. package/coverage/lcov-report/automations/triggerInfo/index.js.html +1 -1
  102. package/coverage/lcov-report/automations/triggerInfo/rowDeleted.js.html +1 -1
  103. package/coverage/lcov-report/automations/triggerInfo/rowSaved.js.html +1 -1
  104. package/coverage/lcov-report/automations/triggerInfo/rowUpdated.js.html +1 -1
  105. package/coverage/lcov-report/automations/triggerInfo/webhook.js.html +1 -1
  106. package/coverage/lcov-report/automations/triggers.js.html +1 -1
  107. package/coverage/lcov-report/constants/index.html +1 -1
  108. package/coverage/lcov-report/constants/index.js.html +1 -1
  109. package/coverage/lcov-report/constants/layouts.js.html +1 -1
  110. package/coverage/lcov-report/constants/screens.js.html +1 -1
  111. package/coverage/lcov-report/db/inMemoryView.js.html +1 -1
  112. package/coverage/lcov-report/db/index.html +14 -14
  113. package/coverage/lcov-report/db/index.js.html +1 -1
  114. package/coverage/lcov-report/db/linkedRows/LinkController.js.html +1 -1
  115. package/coverage/lcov-report/db/linkedRows/index.html +13 -13
  116. package/coverage/lcov-report/db/linkedRows/index.js.html +10 -7
  117. package/coverage/lcov-report/db/linkedRows/linkUtils.js.html +1 -1
  118. package/coverage/lcov-report/db/newid.js.html +2 -2
  119. package/coverage/lcov-report/db/utils.js.html +25 -52
  120. package/coverage/lcov-report/events/AutomationEmitter.js.html +1 -1
  121. package/coverage/lcov-report/events/index.html +1 -1
  122. package/coverage/lcov-report/events/index.js.html +1 -1
  123. package/coverage/lcov-report/events/utils.js.html +1 -1
  124. package/coverage/lcov-report/index.html +57 -57
  125. package/coverage/lcov-report/integrations/tests/TestConfiguration.js.html +1 -1
  126. package/coverage/lcov-report/integrations/tests/index.html +1 -1
  127. package/coverage/lcov-report/middleware/appInfo.js.html +1 -1
  128. package/coverage/lcov-report/middleware/builder.js.html +12 -12
  129. package/coverage/lcov-report/middleware/currentapp.js.html +33 -33
  130. package/coverage/lcov-report/middleware/index.html +1 -1
  131. package/coverage/lcov-report/middleware/joi-validator.js.html +10 -10
  132. package/coverage/lcov-report/middleware/publicApi.js.html +1 -1
  133. package/coverage/lcov-report/middleware/resourceId.js.html +7 -7
  134. package/coverage/lcov-report/middleware/selfhost.js.html +1 -1
  135. package/coverage/lcov-report/middleware/utils.js.html +2 -2
  136. package/coverage/lcov-report/threads/automation.js.html +17 -8
  137. package/coverage/lcov-report/threads/index.html +25 -25
  138. package/coverage/lcov-report/threads/query.js.html +280 -76
  139. package/coverage/lcov-report/threads/utils.js.html +4 -4
  140. package/coverage/lcov-report/utilities/budibaseDir.js.html +1 -1
  141. package/coverage/lcov-report/utilities/centralPath.js.html +1 -1
  142. package/coverage/lcov-report/utilities/csvParser.js.html +1 -1
  143. package/coverage/lcov-report/utilities/global.js.html +14 -14
  144. package/coverage/lcov-report/utilities/index.html +1 -1
  145. package/coverage/lcov-report/utilities/index.js.html +9 -9
  146. package/coverage/lcov-report/utilities/queue/inMemoryQueue.js.html +2 -2
  147. package/coverage/lcov-report/utilities/queue/index.html +1 -1
  148. package/coverage/lcov-report/utilities/routing/index.html +1 -1
  149. package/coverage/lcov-report/utilities/routing/index.js.html +1 -1
  150. package/coverage/lcov-report/utilities/rowProcessor/index.html +1 -1
  151. package/coverage/lcov-report/utilities/rowProcessor/index.js.html +1 -1
  152. package/coverage/lcov-report/utilities/rowProcessor/utils.js.html +1 -1
  153. package/coverage/lcov-report/utilities/scriptRunner.js.html +1 -1
  154. package/coverage/lcov-report/utilities/security.js.html +2 -2
  155. package/coverage/lcov-report/utilities/statusCodes.js.html +1 -1
  156. package/coverage/lcov-report/utilities/usageQuota/index.html +1 -1
  157. package/coverage/lcov-report/utilities/usageQuota/rows.js.html +1 -1
  158. package/coverage/lcov-report/utilities/usageQuota/usageQuoteReset.js.html +1 -1
  159. package/coverage/lcov-report/utilities/users.js.html +1 -1
  160. package/coverage/lcov-report/utilities/workerRequests.js.html +1 -1
  161. package/coverage/lcov.info +1131 -1049
  162. package/dist/api/controllers/application.js +2 -1
  163. package/dist/api/controllers/automation.js +28 -2
  164. package/dist/api/controllers/query/index.js +25 -0
  165. package/dist/api/index.js +4 -1
  166. package/dist/api/routes/automation.js +2 -0
  167. package/dist/automations/logging/index.js +70 -0
  168. package/dist/automations/steps/filter.js +38 -29
  169. package/dist/db/linkedRows/index.js +1 -0
  170. package/dist/db/utils.js +3 -31
  171. package/dist/environment.js +1 -0
  172. package/dist/integrations/queries/sql.js +3 -0
  173. package/dist/integrations/rest.js +1 -1
  174. package/dist/package.json +6 -6
  175. package/dist/tests/utilities/TestConfiguration.js +19 -6
  176. package/dist/threads/automation.js +5 -2
  177. package/dist/threads/index.js +15 -1
  178. package/dist/threads/query.js +53 -8
  179. package/dist/tsconfig.build.tsbuildinfo +1 -1
  180. package/package.json +7 -7
  181. package/src/api/controllers/application.ts +2 -1
  182. package/src/api/controllers/automation.js +35 -2
  183. package/src/api/controllers/query/index.ts +29 -1
  184. package/src/api/index.js +4 -1
  185. package/src/api/routes/automation.js +10 -0
  186. package/src/api/routes/tests/query.spec.js +166 -0
  187. package/src/automations/logging/index.ts +35 -0
  188. package/src/automations/steps/filter.js +36 -28
  189. package/src/automations/tests/filter.spec.js +2 -1
  190. package/src/db/linkedRows/index.js +1 -0
  191. package/src/db/utils.js +4 -13
  192. package/src/environment.js +1 -0
  193. package/src/integrations/queries/sql.ts +3 -1
  194. package/src/integrations/rest.ts +1 -1
  195. package/src/integrations/tests/rest.spec.js +16 -1
  196. package/src/migrations/tests/index.spec.ts +18 -2
  197. package/src/module.d.ts +2 -0
  198. package/src/tests/utilities/TestConfiguration.js +22 -3
  199. package/src/threads/automation.js +5 -2
  200. package/src/threads/index.ts +5 -1
  201. package/src/threads/query.js +78 -10
  202. package/builder/assets/index.a895b27f.js +0 -978
@@ -14,6 +14,8 @@ const ScriptRunner = require("../utilities/scriptRunner");
14
14
  const { integrations } = require("../integrations");
15
15
  const { processStringSync } = require("@budibase/string-templates");
16
16
  const { doInAppContext, getAppDB } = require("@budibase/backend-core/context");
17
+ const { refreshOAuthToken, updateUserOAuth, } = require("@budibase/backend-core/auth");
18
+ const { getGlobalIDFromUserMetadataID } = require("../db/utils");
17
19
  const { isSQL } = require("../integrations/utils");
18
20
  const { enrichQueryFields, interpolateSQL, } = require("../integrations/queries/sql");
19
21
  class QueryRunner {
@@ -27,29 +29,46 @@ class QueryRunner {
27
29
  this.queryId = input.queryId;
28
30
  this.noRecursiveQuery = flags.noRecursiveQuery;
29
31
  this.cachedVariables = [];
32
+ // Additional context items for enrichment
33
+ this.ctx = input.ctx;
30
34
  // allows the response from a query to be stored throughout this
31
35
  // execution so that if it needs to be re-used for another variable
32
36
  // it can be
33
37
  this.queryResponse = {};
34
38
  this.hasRerun = false;
39
+ this.hasRefreshedOAuth = false;
35
40
  }
36
41
  execute() {
42
+ var _a;
37
43
  return __awaiter(this, void 0, void 0, function* () {
38
44
  let { datasource, fields, queryVerb, transformer } = this;
39
45
  const Integration = integrations[datasource.source];
40
46
  if (!Integration) {
41
47
  throw "Integration type does not exist.";
42
48
  }
49
+ if (datasource.config.authConfigs) {
50
+ datasource.config.authConfigs = datasource.config.authConfigs.map(config => {
51
+ return enrichQueryFields(config, this.ctx);
52
+ });
53
+ }
43
54
  const integration = new Integration(datasource.config);
44
55
  // pre-query, make sure datasource variables are added to parameters
45
56
  const parameters = yield this.addDatasourceVariables();
57
+ // Enrich the parameters with the addition context items.
58
+ // 'user' is now a reserved variable key in mapping parameters
59
+ const enrichedParameters = enrichQueryFields(parameters, this.ctx);
60
+ const enrichedContext = Object.assign(Object.assign({}, enrichedParameters), this.ctx);
61
+ // Parse global headers
62
+ if (datasource.config.defaultHeaders) {
63
+ datasource.config.defaultHeaders = enrichQueryFields(datasource.config.defaultHeaders, enrichedContext);
64
+ }
46
65
  let query;
47
66
  // handle SQL injections by interpolating the variables
48
67
  if (isSQL(datasource)) {
49
- query = interpolateSQL(fields, parameters, integration);
68
+ query = interpolateSQL(fields, enrichedParameters, integration);
50
69
  }
51
70
  else {
52
- query = enrichQueryFields(fields, parameters);
71
+ query = enrichQueryFields(fields, enrichedContext);
53
72
  }
54
73
  // Add pagination values for REST queries
55
74
  if (this.pagination) {
@@ -67,17 +86,25 @@ class QueryRunner {
67
86
  if (transformer) {
68
87
  const runner = new ScriptRunner(transformer, {
69
88
  data: rows,
70
- params: parameters,
89
+ params: enrichedParameters,
71
90
  });
72
91
  rows = runner.execute();
73
92
  }
74
93
  // if the request fails we retry once, invalidating the cached value
75
- if (info &&
76
- info.code >= 400 &&
77
- this.cachedVariables.length > 0 &&
78
- !this.hasRerun) {
94
+ if (info && info.code >= 400 && !this.hasRerun) {
95
+ if (((_a = this.ctx.user) === null || _a === void 0 ? void 0 : _a.provider) &&
96
+ info.code === 401 &&
97
+ !this.hasRefreshedOAuth) {
98
+ // Attempt to refresh the access token from the provider
99
+ this.hasRefreshedOAuth = true;
100
+ const authResponse = yield this.refreshOAuth2(this.ctx);
101
+ if (!authResponse || authResponse.err) {
102
+ // In this event the user may have oAuth issues that
103
+ // could require re-authenticating with their provider.
104
+ throw new Error("OAuth2 access token could not be refreshed");
105
+ }
106
+ }
79
107
  this.hasRerun = true;
80
- // invalidate the cache value
81
108
  yield threadUtils.invalidateDynamicVariables(this.cachedVariables);
82
109
  return this.execute();
83
110
  }
@@ -115,6 +142,24 @@ class QueryRunner {
115
142
  }, { noRecursiveQuery: true }).execute();
116
143
  });
117
144
  }
145
+ refreshOAuth2(ctx) {
146
+ return __awaiter(this, void 0, void 0, function* () {
147
+ const { oauth2, providerType, _id } = ctx.user;
148
+ const { configId } = ctx.auth;
149
+ if (!providerType || !(oauth2 === null || oauth2 === void 0 ? void 0 : oauth2.refreshToken)) {
150
+ console.error("No refresh token found for authenticated user");
151
+ return;
152
+ }
153
+ const resp = yield refreshOAuthToken(oauth2.refreshToken, providerType, configId);
154
+ // Refresh session flow. Should be in same location as refreshOAuthToken
155
+ // There are several other properties available in 'resp'
156
+ if (!resp.error) {
157
+ const globalUserId = getGlobalIDFromUserMetadataID(_id);
158
+ yield updateUserOAuth(globalUserId, resp);
159
+ }
160
+ return resp;
161
+ });
162
+ }
118
163
  getDynamicVariable(variable) {
119
164
  return __awaiter(this, void 0, void 0, function* () {
120
165
  let { parameters } = this;