2021-12-15 12:25:52 +01:00
|
|
|
require("./utils").threadSetup()
|
2021-11-11 13:11:09 +01:00
|
|
|
const ScriptRunner = require("../utilities/scriptRunner")
|
|
|
|
const { integrations } = require("../integrations")
|
2021-12-16 17:58:15 +01:00
|
|
|
const { processStringSync } = require("@budibase/string-templates")
|
|
|
|
|
|
|
|
async function addDatasourceVariables(datasource, parameters) {
|
|
|
|
if (!datasource || !datasource.config) {
|
|
|
|
return parameters
|
|
|
|
}
|
|
|
|
const staticVars = datasource.config.staticVariables || {}
|
|
|
|
const dynamicVars = datasource.config.dynamicVariables || []
|
|
|
|
for (let [key, value] of Object.entries(staticVars)) {
|
|
|
|
if (!parameters[key]) {
|
|
|
|
parameters[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (let variable of dynamicVars) {
|
|
|
|
console.log(variable)
|
|
|
|
// TODO: get the variable from query
|
|
|
|
}
|
|
|
|
return parameters
|
|
|
|
}
|
|
|
|
|
|
|
|
function enrichQueryFields(fields, parameters = {}) {
|
|
|
|
const enrichedQuery = {}
|
|
|
|
|
|
|
|
// enrich the fields with dynamic parameters
|
|
|
|
for (let key of Object.keys(fields)) {
|
|
|
|
if (fields[key] == null) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if (typeof fields[key] === "object") {
|
|
|
|
// enrich nested fields object
|
|
|
|
enrichedQuery[key] = enrichQueryFields(fields[key], parameters)
|
|
|
|
} else if (typeof fields[key] === "string") {
|
|
|
|
// enrich string value as normal
|
|
|
|
enrichedQuery[key] = processStringSync(fields[key], parameters, {
|
|
|
|
noHelpers: true,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
enrichedQuery[key] = fields[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
enrichedQuery.json ||
|
|
|
|
enrichedQuery.customData ||
|
|
|
|
enrichedQuery.requestBody
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
enrichedQuery.json = JSON.parse(
|
|
|
|
enrichedQuery.json ||
|
|
|
|
enrichedQuery.customData ||
|
|
|
|
enrichedQuery.requestBody
|
|
|
|
)
|
|
|
|
} catch (err) {
|
|
|
|
// no json found, ignore
|
|
|
|
}
|
|
|
|
delete enrichedQuery.customData
|
|
|
|
}
|
|
|
|
|
|
|
|
return enrichedQuery
|
|
|
|
}
|
2021-11-10 20:35:09 +01:00
|
|
|
|
|
|
|
function formatResponse(resp) {
|
|
|
|
if (typeof resp === "string") {
|
|
|
|
try {
|
|
|
|
resp = JSON.parse(resp)
|
|
|
|
} catch (err) {
|
|
|
|
resp = { response: resp }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
2021-12-06 19:23:18 +01:00
|
|
|
function hasExtraData(response) {
|
|
|
|
return (
|
|
|
|
typeof response === "object" &&
|
|
|
|
!Array.isArray(response) &&
|
2021-12-09 13:30:05 +01:00
|
|
|
response.data != null &&
|
|
|
|
response.info != null
|
2021-12-06 19:23:18 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-16 17:58:15 +01:00
|
|
|
async function runAndTransform(
|
|
|
|
datasource,
|
|
|
|
queryVerb,
|
|
|
|
fields,
|
|
|
|
parameters,
|
|
|
|
transformer
|
|
|
|
) {
|
|
|
|
// pre-query, make sure datasource variables are added to parameters
|
|
|
|
parameters = await addDatasourceVariables(datasource, parameters)
|
|
|
|
const query = enrichQueryFields(fields, parameters)
|
2021-11-10 20:35:09 +01:00
|
|
|
const Integration = integrations[datasource.source]
|
|
|
|
if (!Integration) {
|
|
|
|
throw "Integration type does not exist."
|
|
|
|
}
|
|
|
|
const integration = new Integration(datasource.config)
|
|
|
|
|
2021-12-06 19:23:18 +01:00
|
|
|
let output = formatResponse(await integration[queryVerb](query))
|
|
|
|
let rows = output,
|
2021-12-06 19:35:44 +01:00
|
|
|
info = undefined,
|
2021-12-09 13:30:05 +01:00
|
|
|
extra = undefined
|
2021-12-06 19:23:18 +01:00
|
|
|
if (hasExtraData(output)) {
|
|
|
|
rows = output.data
|
|
|
|
info = output.info
|
2021-12-09 13:30:05 +01:00
|
|
|
extra = output.extra
|
2021-12-06 19:23:18 +01:00
|
|
|
}
|
2021-11-10 20:35:09 +01:00
|
|
|
|
|
|
|
// transform as required
|
|
|
|
if (transformer) {
|
|
|
|
const runner = new ScriptRunner(transformer, { data: rows })
|
|
|
|
rows = runner.execute()
|
|
|
|
}
|
|
|
|
|
|
|
|
// needs to an array for next step
|
|
|
|
if (!Array.isArray(rows)) {
|
|
|
|
rows = [rows]
|
|
|
|
}
|
|
|
|
|
|
|
|
// map into JSON if just raw primitive here
|
|
|
|
if (rows.find(row => typeof row !== "object")) {
|
|
|
|
rows = rows.map(value => ({ value }))
|
|
|
|
}
|
|
|
|
|
|
|
|
// get all the potential fields in the schema
|
|
|
|
let keys = rows.flatMap(Object.keys)
|
|
|
|
|
|
|
|
if (integration.end) {
|
|
|
|
integration.end()
|
|
|
|
}
|
|
|
|
|
2021-12-09 13:30:05 +01:00
|
|
|
return { rows, keys, info, extra }
|
2021-11-10 20:35:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = (input, callback) => {
|
|
|
|
runAndTransform(
|
|
|
|
input.datasource,
|
|
|
|
input.queryVerb,
|
2021-12-16 17:58:15 +01:00
|
|
|
input.fields,
|
|
|
|
input.parameters,
|
2021-11-10 20:35:09 +01:00
|
|
|
input.transformer
|
|
|
|
)
|
|
|
|
.then(response => {
|
|
|
|
callback(null, response)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
callback(err)
|
|
|
|
})
|
|
|
|
}
|