@budibase/server 0.9.185-alpha.0 → 0.9.185-alpha.12

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 (257) hide show
  1. package/Dockerfile +1 -1
  2. package/builder/assets/bb-space-man.5be51a45.svg +290 -0
  3. package/builder/assets/index.4a7c5636.css +1 -0
  4. package/builder/assets/index.708945dd.js +1316 -0
  5. package/builder/assets/vendor.470042a4.js +152 -0
  6. package/builder/assets/vendor.5e1fe644.css +1 -0
  7. package/builder/index.html +4 -4
  8. package/coverage/clover.xml +1060 -928
  9. package/coverage/coverage-final.json +90 -88
  10. package/coverage/lcov-report/api/controllers/analytics.js.html +2 -2
  11. package/coverage/lcov-report/api/controllers/apikeys.js.html +2 -2
  12. package/coverage/lcov-report/api/controllers/application.js.html +43 -13
  13. package/coverage/lcov-report/api/controllers/auth.js.html +7 -7
  14. package/coverage/lcov-report/api/controllers/automation.js.html +3 -3
  15. package/coverage/lcov-report/api/controllers/backup.js.html +1 -1
  16. package/coverage/lcov-report/api/controllers/cloud.js.html +1 -1
  17. package/coverage/lcov-report/api/controllers/component.js.html +4 -4
  18. package/coverage/lcov-report/api/controllers/datasource.js.html +31 -7
  19. package/coverage/lcov-report/api/controllers/dev.js.html +16 -7
  20. package/coverage/lcov-report/api/controllers/hosting.js.html +10 -10
  21. package/coverage/lcov-report/api/controllers/index.html +75 -75
  22. package/coverage/lcov-report/api/controllers/integration.js.html +1 -1
  23. package/coverage/lcov-report/api/controllers/layout.js.html +3 -3
  24. package/coverage/lcov-report/api/controllers/metadata.js.html +3 -3
  25. package/coverage/lcov-report/api/controllers/permission.js.html +3 -3
  26. package/coverage/lcov-report/api/controllers/query.js.html +48 -210
  27. package/coverage/lcov-report/api/controllers/role.js.html +9 -9
  28. package/coverage/lcov-report/api/controllers/routing.js.html +3 -3
  29. package/coverage/lcov-report/api/controllers/row/external.js.html +12 -6
  30. package/coverage/lcov-report/api/controllers/row/index.html +18 -18
  31. package/coverage/lcov-report/api/controllers/row/index.js.html +2 -2
  32. package/coverage/lcov-report/api/controllers/row/internal.js.html +9 -9
  33. package/coverage/lcov-report/api/controllers/row/internalSearch.js.html +2 -2
  34. package/coverage/lcov-report/api/controllers/row/utils.js.html +2 -2
  35. package/coverage/lcov-report/api/controllers/screen.js.html +2 -2
  36. package/coverage/lcov-report/api/controllers/script.js.html +3 -3
  37. package/coverage/lcov-report/api/controllers/table/external.js.html +65 -8
  38. package/coverage/lcov-report/api/controllers/table/index.html +42 -42
  39. package/coverage/lcov-report/api/controllers/table/index.js.html +66 -12
  40. package/coverage/lcov-report/api/controllers/table/internal.js.html +49 -10
  41. package/coverage/lcov-report/api/controllers/table/utils.js.html +57 -45
  42. package/coverage/lcov-report/api/controllers/templates.js.html +4 -4
  43. package/coverage/lcov-report/api/controllers/user.js.html +33 -18
  44. package/coverage/lcov-report/api/controllers/view/exporters.js.html +1 -1
  45. package/coverage/lcov-report/api/controllers/view/index.html +9 -9
  46. package/coverage/lcov-report/api/controllers/view/index.js.html +5 -5
  47. package/coverage/lcov-report/api/controllers/view/utils.js.html +11 -11
  48. package/coverage/lcov-report/api/controllers/view/viewBuilder.js.html +3 -3
  49. package/coverage/lcov-report/api/controllers/webhook.js.html +4 -4
  50. package/coverage/lcov-report/api/index.html +1 -1
  51. package/coverage/lcov-report/api/index.js.html +1 -1
  52. package/coverage/lcov-report/api/routes/analytics.js.html +1 -1
  53. package/coverage/lcov-report/api/routes/apikeys.js.html +1 -1
  54. package/coverage/lcov-report/api/routes/application.js.html +1 -1
  55. package/coverage/lcov-report/api/routes/auth.js.html +1 -1
  56. package/coverage/lcov-report/api/routes/automation.js.html +1 -1
  57. package/coverage/lcov-report/api/routes/backup.js.html +1 -1
  58. package/coverage/lcov-report/api/routes/cloud.js.html +1 -1
  59. package/coverage/lcov-report/api/routes/component.js.html +1 -1
  60. package/coverage/lcov-report/api/routes/datasource.js.html +1 -1
  61. package/coverage/lcov-report/api/routes/deploy.js.html +1 -1
  62. package/coverage/lcov-report/api/routes/dev.js.html +2 -2
  63. package/coverage/lcov-report/api/routes/hosting.js.html +1 -1
  64. package/coverage/lcov-report/api/routes/index.html +4 -4
  65. package/coverage/lcov-report/api/routes/index.js.html +1 -1
  66. package/coverage/lcov-report/api/routes/integration.js.html +1 -1
  67. package/coverage/lcov-report/api/routes/layout.js.html +1 -1
  68. package/coverage/lcov-report/api/routes/metadata.js.html +1 -1
  69. package/coverage/lcov-report/api/routes/permission.js.html +1 -1
  70. package/coverage/lcov-report/api/routes/query.js.html +5 -2
  71. package/coverage/lcov-report/api/routes/role.js.html +1 -1
  72. package/coverage/lcov-report/api/routes/routing.js.html +1 -1
  73. package/coverage/lcov-report/api/routes/row.js.html +1 -1
  74. package/coverage/lcov-report/api/routes/screen.js.html +1 -1
  75. package/coverage/lcov-report/api/routes/script.js.html +1 -1
  76. package/coverage/lcov-report/api/routes/static.js.html +2 -2
  77. package/coverage/lcov-report/api/routes/table.js.html +35 -2
  78. package/coverage/lcov-report/api/routes/templates.js.html +1 -1
  79. package/coverage/lcov-report/api/routes/user.js.html +1 -1
  80. package/coverage/lcov-report/api/routes/view.js.html +1 -1
  81. package/coverage/lcov-report/api/routes/webhook.js.html +1 -1
  82. package/coverage/lcov-report/automations/actions.js.html +2 -2
  83. package/coverage/lcov-report/automations/automationUtils.js.html +3 -3
  84. package/coverage/lcov-report/automations/bullboard.js.html +71 -20
  85. package/coverage/lcov-report/automations/index.html +33 -48
  86. package/coverage/lcov-report/automations/index.js.html +2 -2
  87. package/coverage/lcov-report/automations/steps/bash.js.html +1 -1
  88. package/coverage/lcov-report/automations/steps/createRow.js.html +1 -1
  89. package/coverage/lcov-report/automations/steps/delay.js.html +1 -1
  90. package/coverage/lcov-report/automations/steps/deleteRow.js.html +1 -1
  91. package/coverage/lcov-report/automations/steps/discord.js.html +3 -3
  92. package/coverage/lcov-report/automations/steps/executeQuery.js.html +1 -1
  93. package/coverage/lcov-report/automations/steps/executeScript.js.html +3 -3
  94. package/coverage/lcov-report/automations/steps/filter.js.html +1 -1
  95. package/coverage/lcov-report/automations/steps/index.html +14 -14
  96. package/coverage/lcov-report/automations/steps/integromat.js.html +1 -1
  97. package/coverage/lcov-report/automations/steps/outgoingWebhook.js.html +3 -3
  98. package/coverage/lcov-report/automations/steps/queryRows.js.html +3 -3
  99. package/coverage/lcov-report/automations/steps/sendSmtpEmail.js.html +1 -1
  100. package/coverage/lcov-report/automations/steps/serverLog.js.html +1 -1
  101. package/coverage/lcov-report/automations/steps/slack.js.html +1 -1
  102. package/coverage/lcov-report/automations/steps/updateRow.js.html +1 -1
  103. package/coverage/lcov-report/automations/steps/utils.js.html +7 -7
  104. package/coverage/lcov-report/automations/steps/zapier.js.html +1 -1
  105. package/coverage/lcov-report/automations/triggerInfo/app.js.html +2 -2
  106. package/coverage/lcov-report/automations/triggerInfo/cron.js.html +2 -2
  107. package/coverage/lcov-report/automations/triggerInfo/index.html +1 -1
  108. package/coverage/lcov-report/automations/triggerInfo/index.js.html +8 -8
  109. package/coverage/lcov-report/automations/triggerInfo/rowDeleted.js.html +2 -2
  110. package/coverage/lcov-report/automations/triggerInfo/rowSaved.js.html +2 -2
  111. package/coverage/lcov-report/automations/triggerInfo/rowUpdated.js.html +2 -2
  112. package/coverage/lcov-report/automations/triggerInfo/webhook.js.html +2 -2
  113. package/coverage/lcov-report/automations/triggers.js.html +22 -10
  114. package/coverage/lcov-report/automations/utils.js.html +42 -12
  115. package/coverage/lcov-report/constants/index.html +1 -1
  116. package/coverage/lcov-report/constants/index.js.html +5 -2
  117. package/coverage/lcov-report/constants/layouts.js.html +1 -1
  118. package/coverage/lcov-report/constants/screens.js.html +1 -1
  119. package/coverage/lcov-report/db/client.js.html +2 -2
  120. package/coverage/lcov-report/db/inMemoryView.js.html +2 -2
  121. package/coverage/lcov-report/db/index.html +2 -2
  122. package/coverage/lcov-report/db/index.js.html +1 -1
  123. package/coverage/lcov-report/db/linkedRows/LinkController.js.html +6 -6
  124. package/coverage/lcov-report/db/linkedRows/index.html +10 -10
  125. package/coverage/lcov-report/db/linkedRows/index.js.html +5 -5
  126. package/coverage/lcov-report/db/linkedRows/linkUtils.js.html +6 -6
  127. package/coverage/lcov-report/db/newid.js.html +1 -1
  128. package/coverage/lcov-report/db/utils.js.html +1 -1
  129. package/coverage/lcov-report/events/AutomationEmitter.js.html +6 -6
  130. package/coverage/lcov-report/events/index.html +6 -6
  131. package/coverage/lcov-report/events/index.js.html +1 -1
  132. package/coverage/lcov-report/events/utils.js.html +5 -5
  133. package/coverage/lcov-report/index.html +94 -79
  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 +2 -2
  137. package/coverage/lcov-report/middleware/authorized.js.html +5 -5
  138. package/coverage/lcov-report/middleware/builder.js.html +16 -7
  139. package/coverage/lcov-report/middleware/currentapp.js.html +6 -6
  140. package/coverage/lcov-report/middleware/index.html +21 -21
  141. package/coverage/lcov-report/middleware/joi-validator.js.html +2 -2
  142. package/coverage/lcov-report/middleware/resourceId.js.html +18 -18
  143. package/coverage/lcov-report/middleware/selfhost.js.html +2 -2
  144. package/coverage/lcov-report/middleware/usageQuota.js.html +4 -4
  145. package/coverage/lcov-report/middleware/utils.js.html +1 -1
  146. package/coverage/lcov-report/{automations/thread.js.html → threads/automation.js.html} +83 -32
  147. package/coverage/lcov-report/threads/index.html +141 -0
  148. package/coverage/lcov-report/threads/index.js.html +260 -0
  149. package/coverage/lcov-report/threads/query.js.html +269 -0
  150. package/coverage/lcov-report/utilities/bcrypt.js.html +1 -1
  151. package/coverage/lcov-report/utilities/budibaseDir.js.html +1 -1
  152. package/coverage/lcov-report/utilities/centralPath.js.html +1 -1
  153. package/coverage/lcov-report/utilities/csvParser.js.html +144 -15
  154. package/coverage/lcov-report/utilities/global.js.html +2 -2
  155. package/coverage/lcov-report/utilities/index.html +34 -34
  156. package/coverage/lcov-report/utilities/index.js.html +12 -9
  157. package/coverage/lcov-report/utilities/queue/inMemoryQueue.js.html +30 -9
  158. package/coverage/lcov-report/utilities/queue/index.html +14 -14
  159. package/coverage/lcov-report/utilities/routing/index.html +1 -1
  160. package/coverage/lcov-report/utilities/routing/index.js.html +1 -1
  161. package/coverage/lcov-report/utilities/rowProcessor/index.html +4 -4
  162. package/coverage/lcov-report/utilities/rowProcessor/index.js.html +2 -2
  163. package/coverage/lcov-report/utilities/rowProcessor/utils.js.html +3 -3
  164. package/coverage/lcov-report/utilities/scriptRunner.js.html +16 -7
  165. package/coverage/lcov-report/utilities/security.js.html +3 -3
  166. package/coverage/lcov-report/utilities/statusCodes.js.html +1 -1
  167. package/coverage/lcov-report/utilities/users.js.html +1 -1
  168. package/coverage/lcov-report/utilities/workerRequests.js.html +5 -5
  169. package/coverage/lcov.info +1753 -1527
  170. package/dist/api/controllers/application.js +11 -1
  171. package/dist/api/controllers/datasource.js +6 -2
  172. package/dist/api/controllers/deploy/index.js +2 -0
  173. package/dist/api/controllers/dev.js +4 -1
  174. package/dist/api/controllers/query.js +27 -57
  175. package/dist/api/controllers/row/ExternalRequest.js +2 -2
  176. package/dist/api/controllers/row/external.js +1 -0
  177. package/dist/api/controllers/table/external.js +17 -0
  178. package/dist/api/controllers/table/index.js +21 -2
  179. package/dist/api/controllers/table/internal.js +10 -1
  180. package/dist/api/controllers/table/utils.js +28 -27
  181. package/dist/api/controllers/user.js +10 -6
  182. package/dist/api/routes/query.js +1 -1
  183. package/dist/api/routes/table.js +7 -1
  184. package/dist/app.js +2 -11
  185. package/dist/automations/bullboard.js +37 -10
  186. package/dist/automations/triggers.js +7 -3
  187. package/dist/automations/utils.js +11 -2
  188. package/dist/constants/index.js +1 -0
  189. package/dist/definitions/datasource.js +1 -0
  190. package/dist/integrations/base/IntegrationBase.js +2 -0
  191. package/dist/integrations/base/sql.js +48 -8
  192. package/dist/integrations/base/sqlTable.js +1 -2
  193. package/dist/integrations/mysql.js +3 -1
  194. package/dist/integrations/postgres.js +2 -2
  195. package/dist/middleware/builder.js +4 -1
  196. package/dist/package.json +7 -6
  197. package/dist/{automations/thread.js → threads/automation.js} +23 -7
  198. package/dist/threads/index.js +58 -0
  199. package/dist/threads/query.js +61 -0
  200. package/dist/tsconfig.tsbuildinfo +1 -1
  201. package/dist/utilities/csvParser.js +46 -5
  202. package/dist/utilities/index.js +1 -0
  203. package/dist/utilities/queue/inMemoryQueue.js +8 -0
  204. package/dist/utilities/scriptRunner.js +4 -1
  205. package/package.json +8 -7
  206. package/scripts/likeCypress.ts +33 -0
  207. package/src/api/controllers/application.js +11 -1
  208. package/src/api/controllers/datasource.js +10 -2
  209. package/src/api/controllers/deploy/index.js +2 -0
  210. package/src/api/controllers/dev.js +4 -1
  211. package/src/api/controllers/query.js +26 -80
  212. package/src/api/controllers/row/ExternalRequest.ts +6 -2
  213. package/src/api/controllers/row/external.js +2 -0
  214. package/src/api/controllers/table/external.js +19 -0
  215. package/src/api/controllers/table/index.js +20 -2
  216. package/src/api/controllers/table/internal.js +14 -1
  217. package/src/api/controllers/table/utils.js +36 -32
  218. package/src/api/controllers/user.js +15 -10
  219. package/src/api/routes/query.js +1 -0
  220. package/src/api/routes/table.js +11 -0
  221. package/src/api/routes/tests/misc.spec.js +5 -1
  222. package/src/app.ts +2 -10
  223. package/src/automations/bullboard.js +28 -11
  224. package/src/automations/tests/automation.spec.js +3 -4
  225. package/src/automations/triggers.js +7 -3
  226. package/src/automations/utils.js +12 -2
  227. package/src/constants/index.js +1 -0
  228. package/src/definitions/datasource.ts +3 -2
  229. package/src/integrations/airtable.ts +2 -1
  230. package/src/integrations/arangodb.ts +2 -1
  231. package/src/integrations/base/IntegrationBase.ts +6 -0
  232. package/src/integrations/base/datasourcePlus.ts +2 -1
  233. package/src/integrations/base/sql.ts +51 -9
  234. package/src/integrations/base/sqlTable.ts +1 -4
  235. package/src/integrations/couchdb.ts +2 -1
  236. package/src/integrations/dynamodb.ts +2 -1
  237. package/src/integrations/elasticsearch.ts +2 -1
  238. package/src/integrations/mongodb.ts +2 -1
  239. package/src/integrations/mysql.ts +1 -1
  240. package/src/integrations/postgres.ts +5 -5
  241. package/src/integrations/rest.ts +2 -1
  242. package/src/integrations/s3.ts +2 -1
  243. package/src/middleware/builder.js +4 -1
  244. package/src/{automations/thread.js → threads/automation.js} +24 -7
  245. package/src/threads/index.js +60 -0
  246. package/src/threads/query.js +63 -0
  247. package/src/utilities/csvParser.js +48 -5
  248. package/src/utilities/index.js +1 -0
  249. package/src/utilities/queue/inMemoryQueue.js +7 -0
  250. package/src/utilities/scriptRunner.js +4 -1
  251. package/src/utilities/tests/__snapshots__/csvParser.spec.js.snap +0 -6
  252. package/src/utilities/tests/csvParser.spec.js +3 -0
  253. package/tsconfig.json +1 -1
  254. package/builder/assets/index.0e6a5c20.js +0 -201
  255. package/builder/assets/index.cf21e4d2.css +0 -29
  256. package/builder/assets/vendor.278a305c.js +0 -9
  257. package/builder/assets/vendor.d75d5eb7.css +0 -1
@@ -1,5 +1,5 @@
1
- const actions = require("./actions")
2
- const automationUtils = require("./automationUtils")
1
+ const actions = require("../automations/actions")
2
+ const automationUtils = require("../automations/automationUtils")
3
3
  const AutomationEmitter = require("../events/AutomationEmitter")
4
4
  const { processObject } = require("@budibase/string-templates")
5
5
  const { DEFAULT_TENANT_ID } = require("@budibase/auth").constants
@@ -8,8 +8,10 @@ const { DocumentTypes, isDevAppID } = require("../db/utils")
8
8
  const { doInTenant } = require("@budibase/auth/tenancy")
9
9
  const env = require("../environment")
10
10
  const usage = require("../utilities/usageQuota")
11
+ const { definitions: triggerDefs } = require("../automations/triggerInfo")
11
12
 
12
13
  const FILTER_STEP_ID = actions.ACTION_DEFINITIONS.FILTER.stepId
14
+ const CRON_STEP_ID = triggerDefs.CRON.stepId
13
15
  const STOPPED_STATUS = { success: false, status: "STOPPED" }
14
16
 
15
17
  /**
@@ -23,6 +25,8 @@ class Orchestrator {
23
25
  this._chainCount = this._metadata ? this._metadata.automationChainCount : 0
24
26
  this._appId = triggerOutput.appId
25
27
  this._app = null
28
+ const triggerStepId = automation.definition.trigger.stepId
29
+ triggerOutput = this.cleanupTriggerOutputs(triggerStepId, triggerOutput)
26
30
  // remove from context
27
31
  delete triggerOutput.appId
28
32
  delete triggerOutput.metadata
@@ -34,11 +38,17 @@ class Orchestrator {
34
38
  this._emitter = new AutomationEmitter(this._chainCount + 1)
35
39
  this.executionOutput = { trigger: {}, steps: [] }
36
40
  // setup the execution output
37
- const triggerStepId = automation.definition.trigger.stepId
38
41
  const triggerId = automation.definition.trigger.id
39
42
  this.updateExecutionOutput(triggerId, triggerStepId, null, triggerOutput)
40
43
  }
41
44
 
45
+ cleanupTriggerOutputs(stepId, triggerOutput) {
46
+ if (stepId === CRON_STEP_ID) {
47
+ triggerOutput.timestamp = Date.now()
48
+ }
49
+ return triggerOutput
50
+ }
51
+
42
52
  async getStepFunctionality(stepId) {
43
53
  let step = await actions.getAction(stepId)
44
54
  if (step == null) {
@@ -119,10 +129,17 @@ class Orchestrator {
119
129
  }
120
130
  }
121
131
 
122
- module.exports = async job => {
132
+ module.exports = (input, callback) => {
123
133
  const automationOrchestrator = new Orchestrator(
124
- job.data.automation,
125
- job.data.event
134
+ input.data.automation,
135
+ input.data.event
126
136
  )
127
- return automationOrchestrator.execute()
137
+ automationOrchestrator
138
+ .execute()
139
+ .then(response => {
140
+ callback(null, response)
141
+ })
142
+ .catch(err => {
143
+ callback(err)
144
+ })
128
145
  }
@@ -0,0 +1,60 @@
1
+ const workerFarm = require("worker-farm")
2
+ const env = require("../environment")
3
+
4
+ const ThreadType = {
5
+ QUERY: "query",
6
+ AUTOMATION: "automation",
7
+ }
8
+
9
+ function typeToFile(type) {
10
+ let filename = null
11
+ switch (type) {
12
+ case ThreadType.QUERY:
13
+ filename = "./query"
14
+ break
15
+ case ThreadType.AUTOMATION:
16
+ filename = "./automation"
17
+ break
18
+ default:
19
+ throw "Unknown thread type"
20
+ }
21
+ return require.resolve(filename)
22
+ }
23
+
24
+ class Thread {
25
+ constructor(type, opts = { timeoutMs: null, count: 1 }) {
26
+ this.type = type
27
+ if (!env.isTest()) {
28
+ const workerOpts = {
29
+ autoStart: true,
30
+ maxConcurrentWorkers: opts.count ? opts.count : 1,
31
+ }
32
+ if (opts.timeoutMs) {
33
+ workerOpts.maxCallTime = opts.timeoutMs
34
+ }
35
+ this.workers = workerFarm(workerOpts, typeToFile(type))
36
+ }
37
+ }
38
+
39
+ run(data) {
40
+ return new Promise((resolve, reject) => {
41
+ let fncToCall
42
+ // if in test then don't use threading
43
+ if (env.isTest()) {
44
+ fncToCall = require(typeToFile(this.type))
45
+ } else {
46
+ fncToCall = this.workers
47
+ }
48
+ fncToCall(data, (err, response) => {
49
+ if (err) {
50
+ reject(err)
51
+ } else {
52
+ resolve(response)
53
+ }
54
+ })
55
+ })
56
+ }
57
+ }
58
+
59
+ module.exports.Thread = Thread
60
+ module.exports.ThreadType = ThreadType
@@ -0,0 +1,63 @@
1
+ const ScriptRunner = require("../utilities/scriptRunner")
2
+ const { integrations } = require("../integrations")
3
+
4
+ function formatResponse(resp) {
5
+ if (typeof resp === "string") {
6
+ try {
7
+ resp = JSON.parse(resp)
8
+ } catch (err) {
9
+ resp = { response: resp }
10
+ }
11
+ }
12
+ return resp
13
+ }
14
+
15
+ async function runAndTransform(datasource, queryVerb, query, transformer) {
16
+ const Integration = integrations[datasource.source]
17
+ if (!Integration) {
18
+ throw "Integration type does not exist."
19
+ }
20
+ const integration = new Integration(datasource.config)
21
+
22
+ let rows = formatResponse(await integration[queryVerb](query))
23
+
24
+ // transform as required
25
+ if (transformer) {
26
+ const runner = new ScriptRunner(transformer, { data: rows })
27
+ rows = runner.execute()
28
+ }
29
+
30
+ // needs to an array for next step
31
+ if (!Array.isArray(rows)) {
32
+ rows = [rows]
33
+ }
34
+
35
+ // map into JSON if just raw primitive here
36
+ if (rows.find(row => typeof row !== "object")) {
37
+ rows = rows.map(value => ({ value }))
38
+ }
39
+
40
+ // get all the potential fields in the schema
41
+ let keys = rows.flatMap(Object.keys)
42
+
43
+ if (integration.end) {
44
+ integration.end()
45
+ }
46
+
47
+ return { rows, keys }
48
+ }
49
+
50
+ module.exports = (input, callback) => {
51
+ runAndTransform(
52
+ input.datasource,
53
+ input.queryVerb,
54
+ input.query,
55
+ input.transformer
56
+ )
57
+ .then(response => {
58
+ callback(null, response)
59
+ })
60
+ .catch(err => {
61
+ callback(err)
62
+ })
63
+ }
@@ -51,7 +51,7 @@ function parse(csvString, parsers) {
51
51
  })
52
52
  result.subscribe(row => {
53
53
  // For each CSV row parse all the columns that need parsed
54
- for (let key in parsers) {
54
+ for (let key of Object.keys(parsers)) {
55
55
  if (!schema[key] || schema[key].success) {
56
56
  // get the validator for the column type
57
57
  const validator = VALIDATORS[parsers[key].type]
@@ -76,16 +76,58 @@ function parse(csvString, parsers) {
76
76
  })
77
77
  }
78
78
 
79
- async function transform({ schema, csvString }) {
79
+ function updateSchema({ schema, existingTable }) {
80
+ if (!schema) {
81
+ return schema
82
+ }
83
+ const finalSchema = {}
84
+ const schemaKeyMap = {}
85
+ Object.keys(schema).forEach(key => (schemaKeyMap[key.toLowerCase()] = key))
86
+ for (let [key, field] of Object.entries(existingTable.schema)) {
87
+ const lcKey = key.toLowerCase()
88
+ const foundKey = schemaKeyMap[lcKey]
89
+ if (foundKey) {
90
+ finalSchema[key] = schema[foundKey]
91
+ finalSchema[key].type = field.type
92
+ }
93
+ }
94
+ return finalSchema
95
+ }
96
+
97
+ async function transform({ schema, csvString, existingTable }) {
80
98
  const colParser = {}
81
99
 
82
- for (let key in schema) {
100
+ // make sure the table has all the columns required for import
101
+ if (existingTable) {
102
+ schema = updateSchema({ schema, existingTable })
103
+ }
104
+
105
+ for (let key of Object.keys(schema)) {
83
106
  colParser[key] = PARSERS[schema[key].type] || schema[key].type
84
107
  }
85
108
 
86
109
  try {
87
- const json = await csv({ colParser }).fromString(csvString)
88
- return json
110
+ const data = await csv({ colParser }).fromString(csvString)
111
+ const schemaKeyMap = {}
112
+ Object.keys(schema).forEach(key => (schemaKeyMap[key.toLowerCase()] = key))
113
+ for (let element of data) {
114
+ if (!data) {
115
+ continue
116
+ }
117
+ for (let key of Object.keys(element)) {
118
+ const mappedKey = schemaKeyMap[key.toLowerCase()]
119
+ // isn't a column in the table, remove it
120
+ if (mappedKey == null) {
121
+ delete element[key]
122
+ }
123
+ // casing is different, fix it in row
124
+ else if (key !== mappedKey) {
125
+ element[mappedKey] = element[key]
126
+ delete element[key]
127
+ }
128
+ }
129
+ }
130
+ return data
89
131
  } catch (err) {
90
132
  console.error(`Error transforming CSV to JSON for data import`, err)
91
133
  throw err
@@ -95,4 +137,5 @@ async function transform({ schema, csvString }) {
95
137
  module.exports = {
96
138
  parse,
97
139
  transform,
140
+ updateSchema,
98
141
  }
@@ -48,6 +48,7 @@ exports.objectStoreUrl = () => {
48
48
  * via a specific endpoint (under /api/assets/client).
49
49
  * @param {string} appId In production we need the appId to look up the correct bucket, as the
50
50
  * version of the client lib may differ between apps.
51
+ * @param {string} version The version to retrieve.
51
52
  * @return {string} The URL to be inserted into appPackage response or server rendered
52
53
  * app index file.
53
54
  */
@@ -89,6 +89,13 @@ class InMemoryQueue {
89
89
  getRepeatableJobs() {
90
90
  return []
91
91
  }
92
+
93
+ /**
94
+ * Implemented for tests
95
+ */
96
+ async clean() {
97
+ return []
98
+ }
92
99
  }
93
100
 
94
101
  module.exports = InMemoryQueue
@@ -1,10 +1,13 @@
1
1
  const fetch = require("node-fetch")
2
2
  const { VM, VMScript } = require("vm2")
3
+ const JS_TIMEOUT_MS = 1000
3
4
 
4
5
  class ScriptRunner {
5
6
  constructor(script, context) {
6
7
  const code = `let fn = () => {\n${script}\n}; results.out = fn();`
7
- this.vm = new VM()
8
+ this.vm = new VM({
9
+ timeout: JS_TIMEOUT_MS,
10
+ })
8
11
  this.results = { out: "" }
9
12
  this.vm.setGlobals(context)
10
13
  this.vm.setGlobal("fetch", fetch)
@@ -3,19 +3,13 @@
3
3
  exports[`CSV Parser transformation transforms a CSV file into JSON 1`] = `
4
4
  Array [
5
5
  Object {
6
- "Address": "5 Sesame Street",
7
6
  "Age": 4324,
8
- "Name": "Bertå",
9
7
  },
10
8
  Object {
11
- "Address": "1 World Trade Center",
12
9
  "Age": 34,
13
- "Name": "Ernie",
14
10
  },
15
11
  Object {
16
- "Address": "44 Second Avenue",
17
12
  "Age": 23423,
18
- "Name": "Big Bird",
19
13
  },
20
14
  ]
21
15
  `;
@@ -24,6 +24,9 @@ const SCHEMAS = {
24
24
  Age: {
25
25
  type: "omit",
26
26
  },
27
+ Name: {
28
+ type: "string",
29
+ },
27
30
  },
28
31
  BROKEN: {
29
32
  Address: {
package/tsconfig.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "compilerOptions": {
3
3
  "target": "es6",
4
4
  "module": "commonjs",
5
- "lib": ["es6"],
5
+ "lib": ["es2019"],
6
6
  "allowJs": true,
7
7
  "outDir": "dist",
8
8
  "strict": true,