@budibase/server 1.0.219 → 1.0.220-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 (284) hide show
  1. package/.yarnrc +1 -0
  2. package/__mocks__/node-fetch.ts +9 -0
  3. package/builder/assets/index.7786f497.css +6 -0
  4. package/builder/assets/index.e1885c5f.js +974 -0
  5. package/builder/assets/{vendor.9bf2fcb4.js → vendor.001dcf8c.js} +163 -163
  6. package/builder/index.html +3 -3
  7. package/coverage/clover.xml +1018 -950
  8. package/coverage/coverage-final.json +31 -31
  9. package/coverage/lcov-report/api/controllers/apikeys.js.html +1 -1
  10. package/coverage/lcov-report/api/controllers/auth.js.html +1 -1
  11. package/coverage/lcov-report/api/controllers/automation.js.html +111 -12
  12. package/coverage/lcov-report/api/controllers/backup.js.html +9 -6
  13. package/coverage/lcov-report/api/controllers/cloud.js.html +1 -1
  14. package/coverage/lcov-report/api/controllers/component.js.html +1 -1
  15. package/coverage/lcov-report/api/controllers/datasource.js.html +16 -16
  16. package/coverage/lcov-report/api/controllers/dev.js.html +13 -166
  17. package/coverage/lcov-report/api/controllers/index.html +32 -32
  18. package/coverage/lcov-report/api/controllers/integration.js.html +1 -1
  19. package/coverage/lcov-report/api/controllers/layout.js.html +1 -1
  20. package/coverage/lcov-report/api/controllers/metadata.js.html +1 -1
  21. package/coverage/lcov-report/api/controllers/migrations.js.html +1 -1
  22. package/coverage/lcov-report/api/controllers/permission.js.html +1 -1
  23. package/coverage/lcov-report/api/controllers/query/index.html +1 -1
  24. package/coverage/lcov-report/api/controllers/query/validation.js.html +1 -1
  25. package/coverage/lcov-report/api/controllers/role.js.html +1 -1
  26. package/coverage/lcov-report/api/controllers/routing.js.html +1 -1
  27. package/coverage/lcov-report/api/controllers/row/external.js.html +47 -17
  28. package/coverage/lcov-report/api/controllers/row/index.html +33 -33
  29. package/coverage/lcov-report/api/controllers/row/internal.js.html +1 -1
  30. package/coverage/lcov-report/api/controllers/row/internalSearch.js.html +159 -21
  31. package/coverage/lcov-report/api/controllers/row/staticFormula.js.html +1 -1
  32. package/coverage/lcov-report/api/controllers/row/utils.js.html +17 -8
  33. package/coverage/lcov-report/api/controllers/screen.js.html +1 -1
  34. package/coverage/lcov-report/api/controllers/script.js.html +1 -1
  35. package/coverage/lcov-report/api/controllers/table/bulkFormula.js.html +1 -1
  36. package/coverage/lcov-report/api/controllers/table/external.js.html +1 -1
  37. package/coverage/lcov-report/api/controllers/table/index.html +1 -1
  38. package/coverage/lcov-report/api/controllers/table/index.js.html +1 -1
  39. package/coverage/lcov-report/api/controllers/templates.js.html +1 -1
  40. package/coverage/lcov-report/api/controllers/user.js.html +1 -1
  41. package/coverage/lcov-report/api/controllers/view/exporters.js.html +1 -1
  42. package/coverage/lcov-report/api/controllers/view/index.html +1 -1
  43. package/coverage/lcov-report/api/controllers/view/index.js.html +1 -1
  44. package/coverage/lcov-report/api/controllers/view/utils.js.html +1 -1
  45. package/coverage/lcov-report/api/controllers/view/viewBuilder.js.html +1 -1
  46. package/coverage/lcov-report/api/controllers/webhook.js.html +1 -1
  47. package/coverage/lcov-report/api/index.html +15 -15
  48. package/coverage/lcov-report/api/index.js.html +22 -13
  49. package/coverage/lcov-report/api/routes/analytics.js.html +1 -1
  50. package/coverage/lcov-report/api/routes/apikeys.js.html +1 -1
  51. package/coverage/lcov-report/api/routes/auth.js.html +1 -1
  52. package/coverage/lcov-report/api/routes/automation.js.html +32 -2
  53. package/coverage/lcov-report/api/routes/backup.js.html +1 -1
  54. package/coverage/lcov-report/api/routes/cloud.js.html +1 -1
  55. package/coverage/lcov-report/api/routes/component.js.html +1 -1
  56. package/coverage/lcov-report/api/routes/datasource.js.html +1 -1
  57. package/coverage/lcov-report/api/routes/deploy.js.html +1 -1
  58. package/coverage/lcov-report/api/routes/dev.js.html +2 -5
  59. package/coverage/lcov-report/api/routes/index.html +1 -1
  60. package/coverage/lcov-report/api/routes/integration.js.html +1 -1
  61. package/coverage/lcov-report/api/routes/layout.js.html +1 -1
  62. package/coverage/lcov-report/api/routes/metadata.js.html +1 -1
  63. package/coverage/lcov-report/api/routes/migrations.js.html +1 -1
  64. package/coverage/lcov-report/api/routes/permission.js.html +1 -1
  65. package/coverage/lcov-report/api/routes/query.js.html +1 -1
  66. package/coverage/lcov-report/api/routes/role.js.html +1 -1
  67. package/coverage/lcov-report/api/routes/routing.js.html +1 -1
  68. package/coverage/lcov-report/api/routes/screen.js.html +1 -1
  69. package/coverage/lcov-report/api/routes/script.js.html +1 -1
  70. package/coverage/lcov-report/api/routes/table.js.html +1 -1
  71. package/coverage/lcov-report/api/routes/templates.js.html +1 -1
  72. package/coverage/lcov-report/api/routes/user.js.html +1 -1
  73. package/coverage/lcov-report/api/routes/utils/index.html +1 -1
  74. package/coverage/lcov-report/api/routes/utils/validators.js.html +5 -2
  75. package/coverage/lcov-report/api/routes/view.js.html +1 -1
  76. package/coverage/lcov-report/api/routes/webhook.js.html +1 -1
  77. package/coverage/lcov-report/automations/actions.js.html +1 -1
  78. package/coverage/lcov-report/automations/automationUtils.js.html +57 -12
  79. package/coverage/lcov-report/automations/bullboard.js.html +1 -1
  80. package/coverage/lcov-report/automations/index.html +17 -17
  81. package/coverage/lcov-report/automations/index.js.html +1 -1
  82. package/coverage/lcov-report/automations/steps/bash.js.html +1 -1
  83. package/coverage/lcov-report/automations/steps/delay.js.html +1 -1
  84. package/coverage/lcov-report/automations/steps/discord.js.html +1 -1
  85. package/coverage/lcov-report/automations/steps/executeQuery.js.html +2 -2
  86. package/coverage/lcov-report/automations/steps/executeScript.js.html +1 -1
  87. package/coverage/lcov-report/automations/steps/filter.js.html +58 -34
  88. package/coverage/lcov-report/automations/steps/index.html +11 -11
  89. package/coverage/lcov-report/automations/steps/integromat.js.html +1 -1
  90. package/coverage/lcov-report/automations/steps/loop.js.html +1 -1
  91. package/coverage/lcov-report/automations/steps/outgoingWebhook.js.html +1 -1
  92. package/coverage/lcov-report/automations/steps/queryRows.js.html +1 -1
  93. package/coverage/lcov-report/automations/steps/sendSmtpEmail.js.html +1 -1
  94. package/coverage/lcov-report/automations/steps/serverLog.js.html +1 -1
  95. package/coverage/lcov-report/automations/steps/slack.js.html +1 -1
  96. package/coverage/lcov-report/automations/steps/updateRow.js.html +1 -1
  97. package/coverage/lcov-report/automations/steps/utils.js.html +1 -1
  98. package/coverage/lcov-report/automations/steps/zapier.js.html +1 -1
  99. package/coverage/lcov-report/automations/triggerInfo/app.js.html +1 -1
  100. package/coverage/lcov-report/automations/triggerInfo/cron.js.html +1 -1
  101. package/coverage/lcov-report/automations/triggerInfo/index.html +1 -1
  102. package/coverage/lcov-report/automations/triggerInfo/index.js.html +1 -1
  103. package/coverage/lcov-report/automations/triggerInfo/rowDeleted.js.html +1 -1
  104. package/coverage/lcov-report/automations/triggerInfo/rowSaved.js.html +1 -1
  105. package/coverage/lcov-report/automations/triggerInfo/rowUpdated.js.html +1 -1
  106. package/coverage/lcov-report/automations/triggerInfo/webhook.js.html +1 -1
  107. package/coverage/lcov-report/automations/triggers.js.html +4 -4
  108. package/coverage/lcov-report/constants/index.html +1 -1
  109. package/coverage/lcov-report/constants/index.js.html +1 -1
  110. package/coverage/lcov-report/constants/layouts.js.html +1 -1
  111. package/coverage/lcov-report/constants/screens.js.html +1 -1
  112. package/coverage/lcov-report/db/inMemoryView.js.html +1 -1
  113. package/coverage/lcov-report/db/index.html +14 -14
  114. package/coverage/lcov-report/db/index.js.html +1 -1
  115. package/coverage/lcov-report/db/linkedRows/LinkController.js.html +1 -1
  116. package/coverage/lcov-report/db/linkedRows/index.html +13 -13
  117. package/coverage/lcov-report/db/linkedRows/index.js.html +10 -7
  118. package/coverage/lcov-report/db/linkedRows/linkUtils.js.html +1 -1
  119. package/coverage/lcov-report/db/newid.js.html +2 -2
  120. package/coverage/lcov-report/db/utils.js.html +27 -51
  121. package/coverage/lcov-report/events/AutomationEmitter.js.html +1 -1
  122. package/coverage/lcov-report/events/index.html +1 -1
  123. package/coverage/lcov-report/events/index.js.html +1 -1
  124. package/coverage/lcov-report/events/utils.js.html +1 -1
  125. package/coverage/lcov-report/index.html +83 -83
  126. package/coverage/lcov-report/integrations/tests/TestConfiguration.js.html +1 -1
  127. package/coverage/lcov-report/integrations/tests/index.html +1 -1
  128. package/coverage/lcov-report/middleware/appInfo.js.html +1 -1
  129. package/coverage/lcov-report/middleware/builder.js.html +12 -12
  130. package/coverage/lcov-report/middleware/currentapp.js.html +33 -33
  131. package/coverage/lcov-report/middleware/index.html +1 -1
  132. package/coverage/lcov-report/middleware/joi-validator.js.html +10 -10
  133. package/coverage/lcov-report/middleware/publicApi.js.html +1 -1
  134. package/coverage/lcov-report/middleware/resourceId.js.html +7 -7
  135. package/coverage/lcov-report/middleware/selfhost.js.html +1 -1
  136. package/coverage/lcov-report/middleware/utils.js.html +2 -2
  137. package/coverage/lcov-report/threads/automation.js.html +18 -9
  138. package/coverage/lcov-report/threads/index.html +25 -25
  139. package/coverage/lcov-report/threads/query.js.html +301 -79
  140. package/coverage/lcov-report/threads/utils.js.html +4 -4
  141. package/coverage/lcov-report/utilities/budibaseDir.js.html +1 -1
  142. package/coverage/lcov-report/utilities/centralPath.js.html +1 -1
  143. package/coverage/lcov-report/utilities/csvParser.js.html +1 -1
  144. package/coverage/lcov-report/utilities/global.js.html +14 -14
  145. package/coverage/lcov-report/utilities/index.html +26 -26
  146. package/coverage/lcov-report/utilities/index.js.html +17 -32
  147. package/coverage/lcov-report/utilities/queue/inMemoryQueue.js.html +2 -2
  148. package/coverage/lcov-report/utilities/queue/index.html +1 -1
  149. package/coverage/lcov-report/utilities/routing/index.html +1 -1
  150. package/coverage/lcov-report/utilities/routing/index.js.html +1 -1
  151. package/coverage/lcov-report/utilities/rowProcessor/index.html +1 -1
  152. package/coverage/lcov-report/utilities/rowProcessor/index.js.html +1 -1
  153. package/coverage/lcov-report/utilities/rowProcessor/utils.js.html +1 -1
  154. package/coverage/lcov-report/utilities/scriptRunner.js.html +1 -1
  155. package/coverage/lcov-report/utilities/security.js.html +2 -2
  156. package/coverage/lcov-report/utilities/statusCodes.js.html +1 -1
  157. package/coverage/lcov-report/utilities/usageQuota/index.html +1 -1
  158. package/coverage/lcov-report/utilities/usageQuota/rows.js.html +1 -1
  159. package/coverage/lcov-report/utilities/usageQuota/usageQuoteReset.js.html +1 -1
  160. package/coverage/lcov-report/utilities/users.js.html +1 -1
  161. package/coverage/lcov-report/utilities/workerRequests.js.html +57 -9
  162. package/coverage/lcov.info +1789 -1665
  163. package/dist/api/controllers/application.js +19 -16
  164. package/dist/api/controllers/automation.js +28 -2
  165. package/dist/api/controllers/backup.js +3 -2
  166. package/dist/api/controllers/dev.js +2 -40
  167. package/dist/api/controllers/public/applications.js +1 -1
  168. package/dist/api/controllers/public/utils.js +8 -1
  169. package/dist/api/controllers/query/index.js +31 -3
  170. package/dist/api/controllers/row/external.js +19 -7
  171. package/dist/api/controllers/row/internalSearch.js +47 -10
  172. package/dist/api/controllers/row/utils.js +3 -0
  173. package/dist/api/controllers/static/index.js +1 -5
  174. package/dist/api/index.js +4 -1
  175. package/dist/api/routes/automation.js +2 -0
  176. package/dist/api/routes/dev.js +1 -2
  177. package/dist/api/routes/public/rows.js +7 -2
  178. package/dist/api/routes/utils/validators.js +1 -0
  179. package/dist/app.js +20 -0
  180. package/dist/automations/automationUtils.js +16 -4
  181. package/dist/automations/logging/index.js +70 -0
  182. package/dist/automations/steps/executeQuery.js +1 -1
  183. package/dist/automations/steps/filter.js +38 -29
  184. package/dist/automations/triggers.js +1 -1
  185. package/dist/db/linkedRows/index.js +1 -0
  186. package/dist/db/utils.js +4 -31
  187. package/dist/definitions/datasource.js +1 -0
  188. package/dist/environment.js +3 -0
  189. package/dist/integrations/airtable.js +1 -0
  190. package/dist/integrations/arangodb.js +1 -0
  191. package/dist/integrations/base/sql.js +22 -3
  192. package/dist/integrations/couchdb.js +1 -0
  193. package/dist/integrations/dynamodb.js +1 -0
  194. package/dist/integrations/elasticsearch.js +1 -0
  195. package/dist/integrations/firebase.js +1 -0
  196. package/dist/integrations/googlesheets.js +1 -0
  197. package/dist/integrations/index.js +4 -0
  198. package/dist/integrations/microsoftSqlServer.js +2 -1
  199. package/dist/integrations/mongodb.js +1 -0
  200. package/dist/integrations/mysql.js +2 -1
  201. package/dist/integrations/oracle.js +1 -0
  202. package/dist/integrations/postgres.js +2 -1
  203. package/dist/integrations/queries/sql.js +3 -0
  204. package/dist/integrations/redis.js +1 -0
  205. package/dist/integrations/rest.js +2 -1
  206. package/dist/integrations/s3.js +1 -0
  207. package/dist/integrations/snowflake.js +102 -0
  208. package/dist/package.json +7 -6
  209. package/dist/tests/utilities/TestConfiguration.js +19 -6
  210. package/dist/threads/automation.js +6 -3
  211. package/dist/threads/index.js +15 -1
  212. package/dist/threads/query.js +65 -12
  213. package/dist/tsconfig.build.tsbuildinfo +1 -1
  214. package/dist/utilities/fileSystem/index.js +16 -6
  215. package/dist/utilities/index.js +1 -5
  216. package/dist/utilities/workerRequests.js +8 -0
  217. package/package.json +8 -7
  218. package/scripts/dev/manage.js +2 -0
  219. package/scripts/integrations/oracle/instantclient/linux/arm64/basiclite-19.10.zip +0 -0
  220. package/scripts/integrations/oracle/instantclient/linux/arm64/install.sh +23 -0
  221. package/scripts/integrations/oracle/instantclient/linux/install.sh +10 -0
  222. package/specs/openapi.json +9 -5
  223. package/specs/openapi.yaml +10 -4
  224. package/src/api/controllers/application.ts +19 -19
  225. package/src/api/controllers/automation.js +35 -2
  226. package/src/api/controllers/backup.js +3 -2
  227. package/src/api/controllers/dev.js +2 -53
  228. package/src/api/controllers/public/applications.ts +2 -2
  229. package/src/api/controllers/public/utils.ts +8 -1
  230. package/src/api/controllers/query/index.ts +40 -4
  231. package/src/api/controllers/row/external.js +19 -9
  232. package/src/api/controllers/row/internalSearch.js +55 -9
  233. package/src/api/controllers/row/utils.js +3 -0
  234. package/src/api/controllers/static/index.js +1 -6
  235. package/src/api/index.js +4 -1
  236. package/src/api/routes/automation.js +10 -0
  237. package/src/api/routes/dev.js +0 -1
  238. package/src/api/routes/public/rows.ts +7 -2
  239. package/src/api/routes/tests/query.spec.js +166 -1
  240. package/src/api/routes/utils/validators.js +1 -0
  241. package/src/app.ts +30 -1
  242. package/src/automations/automationUtils.js +19 -4
  243. package/src/automations/logging/index.ts +35 -0
  244. package/src/automations/steps/executeQuery.js +1 -1
  245. package/src/automations/steps/filter.js +36 -28
  246. package/src/automations/tests/filter.spec.js +2 -1
  247. package/src/automations/triggers.js +1 -1
  248. package/src/db/linkedRows/index.js +1 -0
  249. package/src/db/utils.js +5 -13
  250. package/src/definitions/common.ts +1 -0
  251. package/src/definitions/datasource.ts +2 -0
  252. package/src/definitions/openapi.ts +471 -469
  253. package/src/environment.js +3 -0
  254. package/src/integrations/airtable.ts +1 -0
  255. package/src/integrations/arangodb.ts +1 -0
  256. package/src/integrations/base/sql.ts +29 -11
  257. package/src/integrations/couchdb.ts +1 -0
  258. package/src/integrations/dynamodb.ts +1 -0
  259. package/src/integrations/elasticsearch.ts +1 -0
  260. package/src/integrations/firebase.ts +1 -0
  261. package/src/integrations/googlesheets.ts +1 -0
  262. package/src/integrations/index.ts +4 -0
  263. package/src/integrations/microsoftSqlServer.ts +2 -0
  264. package/src/integrations/mongodb.ts +1 -0
  265. package/src/integrations/mysql.ts +2 -0
  266. package/src/integrations/oracle.ts +1 -0
  267. package/src/integrations/postgres.ts +2 -0
  268. package/src/integrations/queries/sql.ts +3 -1
  269. package/src/integrations/redis.ts +1 -0
  270. package/src/integrations/rest.ts +2 -1
  271. package/src/integrations/s3.ts +1 -0
  272. package/src/integrations/snowflake.ts +99 -0
  273. package/src/integrations/tests/rest.spec.js +16 -1
  274. package/src/migrations/tests/index.spec.ts +18 -2
  275. package/src/module.d.ts +2 -0
  276. package/src/tests/utilities/TestConfiguration.js +22 -3
  277. package/src/threads/automation.js +6 -3
  278. package/src/threads/index.ts +5 -1
  279. package/src/threads/query.js +88 -14
  280. package/src/utilities/fileSystem/index.js +22 -8
  281. package/src/utilities/index.js +1 -6
  282. package/src/utilities/workerRequests.js +16 -0
  283. package/builder/assets/index.1c640a53.js +0 -1316
  284. package/builder/assets/index.24307ca0.css +0 -6
@@ -4,6 +4,14 @@ const ScriptRunner = require("../utilities/scriptRunner")
4
4
  const { integrations } = require("../integrations")
5
5
  const { processStringSync } = require("@budibase/string-templates")
6
6
  const { doInAppContext, getAppDB } = require("@budibase/backend-core/context")
7
+ const {
8
+ refreshOAuthToken,
9
+ updateUserOAuth,
10
+ } = require("@budibase/backend-core/auth")
11
+ const { user: userCache } = require("@budibase/backend-core/cache")
12
+ const { getGlobalIDFromUserMetadataID } = require("../db/utils")
13
+ const { cloneDeep } = require("lodash/fp")
14
+
7
15
  const { isSQL } = require("../integrations/utils")
8
16
  const {
9
17
  enrichQueryFields,
@@ -21,29 +29,58 @@ class QueryRunner {
21
29
  this.queryId = input.queryId
22
30
  this.noRecursiveQuery = flags.noRecursiveQuery
23
31
  this.cachedVariables = []
32
+ // Additional context items for enrichment
33
+ this.ctx = input.ctx
24
34
  // allows the response from a query to be stored throughout this
25
35
  // execution so that if it needs to be re-used for another variable
26
36
  // it can be
27
37
  this.queryResponse = {}
28
38
  this.hasRerun = false
39
+ this.hasRefreshedOAuth = false
29
40
  }
30
41
 
31
42
  async execute() {
32
43
  let { datasource, fields, queryVerb, transformer } = this
33
- const Integration = integrations[datasource.source]
44
+
45
+ let datasourceClone = cloneDeep(datasource)
46
+ let fieldsClone = cloneDeep(fields)
47
+
48
+ const Integration = integrations[datasourceClone.source]
34
49
  if (!Integration) {
35
50
  throw "Integration type does not exist."
36
51
  }
37
- const integration = new Integration(datasource.config)
52
+
53
+ if (datasourceClone.config.authConfigs) {
54
+ datasourceClone.config.authConfigs =
55
+ datasourceClone.config.authConfigs.map(config => {
56
+ return enrichQueryFields(config, this.ctx)
57
+ })
58
+ }
59
+
60
+ const integration = new Integration(datasourceClone.config)
38
61
 
39
62
  // pre-query, make sure datasource variables are added to parameters
40
63
  const parameters = await this.addDatasourceVariables()
64
+
65
+ // Enrich the parameters with the addition context items.
66
+ // 'user' is now a reserved variable key in mapping parameters
67
+ const enrichedParameters = enrichQueryFields(parameters, this.ctx)
68
+ const enrichedContext = { ...enrichedParameters, ...this.ctx }
69
+
70
+ // Parse global headers
71
+ if (datasourceClone.config.defaultHeaders) {
72
+ datasourceClone.config.defaultHeaders = enrichQueryFields(
73
+ datasourceClone.config.defaultHeaders,
74
+ enrichedContext
75
+ )
76
+ }
77
+
41
78
  let query
42
79
  // handle SQL injections by interpolating the variables
43
- if (isSQL(datasource)) {
44
- query = interpolateSQL(fields, parameters, integration)
80
+ if (isSQL(datasourceClone)) {
81
+ query = interpolateSQL(fieldsClone, enrichedParameters, integration)
45
82
  } else {
46
- query = enrichQueryFields(fields, parameters)
83
+ query = enrichQueryFields(fieldsClone, enrichedContext)
47
84
  }
48
85
 
49
86
  // Add pagination values for REST queries
@@ -67,20 +104,25 @@ class QueryRunner {
67
104
  if (transformer) {
68
105
  const runner = new ScriptRunner(transformer, {
69
106
  data: rows,
70
- params: parameters,
107
+ params: enrichedParameters,
71
108
  })
72
109
  rows = runner.execute()
73
110
  }
74
111
 
75
112
  // if the request fails we retry once, invalidating the cached value
76
- if (
77
- info &&
78
- info.code >= 400 &&
79
- this.cachedVariables.length > 0 &&
80
- !this.hasRerun
81
- ) {
82
- this.hasRerun = true
83
- // invalidate the cache value
113
+ if (info && info.code >= 400 && !this.hasRerun) {
114
+ if (
115
+ this.ctx.user?.provider &&
116
+ info.code === 401 &&
117
+ !this.hasRefreshedOAuth
118
+ ) {
119
+ await this.refreshOAuth2(this.ctx)
120
+ // Attempt to refresh the access token from the provider
121
+ this.hasRefreshedOAuth = true
122
+ } else {
123
+ this.hasRerun = true
124
+ }
125
+
84
126
  await threadUtils.invalidateDynamicVariables(this.cachedVariables)
85
127
  return this.execute()
86
128
  }
@@ -126,6 +168,38 @@ class QueryRunner {
126
168
  ).execute()
127
169
  }
128
170
 
171
+ async refreshOAuth2(ctx) {
172
+ const { oauth2, providerType, _id } = ctx.user
173
+ const { configId } = ctx.auth
174
+
175
+ if (!providerType || !oauth2?.refreshToken) {
176
+ throw new Error("No refresh token found for authenticated user")
177
+ }
178
+
179
+ const resp = await refreshOAuthToken(
180
+ oauth2.refreshToken,
181
+ providerType,
182
+ configId
183
+ )
184
+
185
+ // Refresh session flow. Should be in same location as refreshOAuthToken
186
+ // There are several other properties available in 'resp'
187
+ if (!resp.err) {
188
+ const globalUserId = getGlobalIDFromUserMetadataID(_id)
189
+ await updateUserOAuth(globalUserId, resp)
190
+ this.ctx.user = await userCache.getUser(globalUserId)
191
+ } else {
192
+ // In this event the user may have oAuth issues that
193
+ // could require re-authenticating with their provider.
194
+ let errorMessage = resp.err.data ? resp.err.data : resp.err.toString()
195
+ throw new Error(
196
+ "OAuth2 access token could not be refreshed: " + errorMessage
197
+ )
198
+ }
199
+
200
+ return resp
201
+ }
202
+
129
203
  async getDynamicVariable(variable) {
130
204
  let { parameters } = this
131
205
  const queryId = variable.queryId,
@@ -21,6 +21,7 @@ const env = require("../../environment")
21
21
  const {
22
22
  USER_METDATA_PREFIX,
23
23
  LINK_USER_METADATA_PREFIX,
24
+ TABLE_ROW_PREFIX,
24
25
  } = require("../../db/utils")
25
26
  const MemoryStream = require("memorystream")
26
27
  const { getAppId } = require("@budibase/backend-core/context")
@@ -109,6 +110,23 @@ exports.apiFileReturn = contents => {
109
110
  return fs.createReadStream(path)
110
111
  }
111
112
 
113
+ exports.defineFilter = excludeRows => {
114
+ if (excludeRows) {
115
+ return doc =>
116
+ !(
117
+ doc._id.includes(USER_METDATA_PREFIX) ||
118
+ doc._id.includes(LINK_USER_METADATA_PREFIX) ||
119
+ doc._id.includes(TABLE_ROW_PREFIX)
120
+ )
121
+ } else if (!excludeRows) {
122
+ return doc =>
123
+ !(
124
+ doc._id.includes(USER_METDATA_PREFIX) ||
125
+ doc._id.includes(LINK_USER_METADATA_PREFIX)
126
+ )
127
+ }
128
+ }
129
+
112
130
  /**
113
131
  * Local utility to back up the database state for an app, excluding global user
114
132
  * data or user relationships.
@@ -116,14 +134,10 @@ exports.apiFileReturn = contents => {
116
134
  * @param {object} config Config to send to export DB
117
135
  * @returns {*} either a string or a stream of the backup
118
136
  */
119
- const backupAppData = async (appId, config) => {
137
+ const backupAppData = async (appId, config, includeRows) => {
120
138
  return await exports.exportDB(appId, {
121
139
  ...config,
122
- filter: doc =>
123
- !(
124
- doc._id.includes(USER_METDATA_PREFIX) ||
125
- doc._id.includes(LINK_USER_METADATA_PREFIX)
126
- ),
140
+ filter: exports.defineFilter(includeRows),
127
141
  })
128
142
  }
129
143
 
@@ -142,8 +156,8 @@ exports.performBackup = async (appId, backupName) => {
142
156
  * @param {string} appId The ID of the app which is to be backed up.
143
157
  * @returns {*} a readable stream of the backup which is written in real time
144
158
  */
145
- exports.streamBackup = async appId => {
146
- return await backupAppData(appId, { stream: true })
159
+ exports.streamBackup = async (appId, includeRows) => {
160
+ return await backupAppData(appId, { stream: true }, includeRows)
147
161
  }
148
162
 
149
163
  /**
@@ -54,13 +54,8 @@ exports.objectStoreUrl = () => {
54
54
  * @return {string} The URL to be inserted into appPackage response or server rendered
55
55
  * app index file.
56
56
  */
57
- exports.clientLibraryPath = (appId, version, ctx) => {
57
+ exports.clientLibraryPath = (appId, version) => {
58
58
  if (env.isProd()) {
59
- // TODO: remove - for beta testing UI
60
- if (ctx && ctx.cookies.get("beta:design_ui")) {
61
- return "https://cdn.budi.live/beta:design_ui/budibase-client.js"
62
- }
63
-
64
59
  let url = `${exports.objectStoreUrl()}/${sanitizeKey(
65
60
  appId
66
61
  )}/budibase-client.js`
@@ -137,3 +137,19 @@ exports.readGlobalUser = async ctx => {
137
137
  )
138
138
  return checkResponse(response, "get user", { ctx })
139
139
  }
140
+
141
+ exports.createAdminUser = async (email, password, tenantId) => {
142
+ const response = await fetch(
143
+ checkSlashesInUrl(env.WORKER_URL + "/api/global/users/init"),
144
+ request(null, { method: "POST", body: { email, password, tenantId } })
145
+ )
146
+ return checkResponse(response, "create admin user")
147
+ }
148
+
149
+ exports.getChecklist = async () => {
150
+ const response = await fetch(
151
+ checkSlashesInUrl(env.WORKER_URL + "/api/global/configs/checklist"),
152
+ request(null, { method: "GET" })
153
+ )
154
+ return checkResponse(response, "get checklist")
155
+ }