2021-12-16 23:43:14 +01:00
|
|
|
const threadUtils = require("./utils")
|
|
|
|
threadUtils.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")
|
2022-01-28 01:05:39 +01:00
|
|
|
const { doInAppContext, getAppDB } = require("@budibase/backend-core/context")
|
2021-12-16 23:43:14 +01:00
|
|
|
|
2022-01-05 10:21:25 +01:00
|
|
|
const IS_TRIPLE_BRACE = new RegExp(/^{{3}.*}{3}$/)
|
|
|
|
const IS_HANDLEBARS = new RegExp(/^{{2}.*}{2}$/)
|
|
|
|
|
2021-12-16 23:43:14 +01:00
|
|
|
class QueryRunner {
|
|
|
|
constructor(input, flags = { noRecursiveQuery: false }) {
|
|
|
|
this.datasource = input.datasource
|
|
|
|
this.queryVerb = input.queryVerb
|
|
|
|
this.fields = input.fields
|
|
|
|
this.parameters = input.parameters
|
2022-01-05 10:16:10 +01:00
|
|
|
this.pagination = input.pagination
|
2021-12-16 23:43:14 +01:00
|
|
|
this.transformer = input.transformer
|
2021-12-17 15:08:48 +01:00
|
|
|
this.queryId = input.queryId
|
2021-12-16 23:43:14 +01:00
|
|
|
this.noRecursiveQuery = flags.noRecursiveQuery
|
2021-12-17 18:56:28 +01:00
|
|
|
this.cachedVariables = []
|
2021-12-17 19:21:36 +01:00
|
|
|
// allows the response from a query to be stored throughout this
|
|
|
|
// execution so that if it needs to be re-used for another variable
|
|
|
|
// it can be
|
|
|
|
this.queryResponse = {}
|
2021-12-17 18:56:28 +01:00
|
|
|
this.hasRerun = false
|
2021-12-16 17:58:15 +01:00
|
|
|
}
|
|
|
|
|
2021-12-16 23:43:14 +01:00
|
|
|
async execute() {
|
|
|
|
let { datasource, fields, queryVerb, transformer } = this
|
|
|
|
// pre-query, make sure datasource variables are added to parameters
|
|
|
|
const parameters = await this.addDatasourceVariables()
|
2022-01-05 10:16:10 +01:00
|
|
|
let query = this.enrichQueryFields(fields, parameters)
|
2022-01-05 15:26:13 +01:00
|
|
|
|
|
|
|
// Add pagination values for REST queries
|
|
|
|
if (this.pagination) {
|
|
|
|
query.paginationValues = this.pagination
|
|
|
|
}
|
|
|
|
|
2021-12-16 23:43:14 +01:00
|
|
|
const Integration = integrations[datasource.source]
|
|
|
|
if (!Integration) {
|
|
|
|
throw "Integration type does not exist."
|
2021-12-16 17:58:15 +01:00
|
|
|
}
|
2021-12-16 23:43:14 +01:00
|
|
|
const integration = new Integration(datasource.config)
|
|
|
|
|
|
|
|
let output = threadUtils.formatResponse(await integration[queryVerb](query))
|
|
|
|
let rows = output,
|
|
|
|
info = undefined,
|
2022-01-05 18:28:57 +01:00
|
|
|
extra = undefined,
|
|
|
|
pagination = undefined
|
2021-12-16 23:43:14 +01:00
|
|
|
if (threadUtils.hasExtraData(output)) {
|
|
|
|
rows = output.data
|
|
|
|
info = output.info
|
|
|
|
extra = output.extra
|
2022-01-05 18:28:57 +01:00
|
|
|
pagination = output.pagination
|
2021-12-16 17:58:15 +01:00
|
|
|
}
|
|
|
|
|
2021-12-16 23:43:14 +01:00
|
|
|
// transform as required
|
|
|
|
if (transformer) {
|
2022-01-11 12:17:35 +01:00
|
|
|
const runner = new ScriptRunner(transformer, {
|
|
|
|
data: rows,
|
|
|
|
params: parameters,
|
|
|
|
})
|
2021-12-16 23:43:14 +01:00
|
|
|
rows = runner.execute()
|
2021-12-16 17:58:15 +01:00
|
|
|
}
|
|
|
|
|
2021-12-17 18:56:28 +01:00
|
|
|
// if the request fails we retry once, invalidating the cached value
|
|
|
|
if (
|
|
|
|
info &&
|
|
|
|
info.code >= 400 &&
|
|
|
|
this.cachedVariables.length > 0 &&
|
|
|
|
!this.hasRerun
|
|
|
|
) {
|
|
|
|
this.hasRerun = true
|
|
|
|
// invalidate the cache value
|
|
|
|
await threadUtils.invalidateDynamicVariables(this.cachedVariables)
|
|
|
|
return this.execute()
|
|
|
|
}
|
|
|
|
|
2022-01-20 17:01:09 +01:00
|
|
|
// check for undefined response
|
|
|
|
if (!rows) {
|
|
|
|
rows = []
|
|
|
|
}
|
|
|
|
|
2021-12-16 23:43:14 +01:00
|
|
|
// needs to an array for next step
|
|
|
|
if (!Array.isArray(rows)) {
|
|
|
|
rows = [rows]
|
2021-11-10 20:35:09 +01:00
|
|
|
}
|
|
|
|
|
2021-12-16 23:43:14 +01:00
|
|
|
// map into JSON if just raw primitive here
|
|
|
|
if (rows.find(row => typeof row !== "object")) {
|
|
|
|
rows = rows.map(value => ({ value }))
|
|
|
|
}
|
2021-12-06 19:23:18 +01:00
|
|
|
|
2021-12-16 23:43:14 +01:00
|
|
|
// get all the potential fields in the schema
|
|
|
|
let keys = rows.flatMap(Object.keys)
|
2021-11-10 20:35:09 +01:00
|
|
|
|
2021-12-16 23:43:14 +01:00
|
|
|
if (integration.end) {
|
|
|
|
integration.end()
|
|
|
|
}
|
2021-11-10 20:35:09 +01:00
|
|
|
|
2022-01-05 18:28:57 +01:00
|
|
|
return { rows, keys, info, extra, pagination }
|
2021-11-10 20:35:09 +01:00
|
|
|
}
|
|
|
|
|
2021-12-16 23:43:14 +01:00
|
|
|
async runAnotherQuery(queryId, parameters) {
|
2022-01-28 01:05:39 +01:00
|
|
|
const db = getAppDB()
|
2021-12-16 23:43:14 +01:00
|
|
|
const query = await db.get(queryId)
|
|
|
|
const datasource = await db.get(query.datasourceId)
|
|
|
|
return new QueryRunner(
|
|
|
|
{
|
|
|
|
datasource,
|
|
|
|
queryVerb: query.queryVerb,
|
|
|
|
fields: query.fields,
|
|
|
|
parameters,
|
|
|
|
transformer: query.transformer,
|
|
|
|
},
|
|
|
|
{ noRecursiveQuery: true }
|
|
|
|
).execute()
|
2021-11-10 20:35:09 +01:00
|
|
|
}
|
|
|
|
|
2021-12-16 23:43:14 +01:00
|
|
|
async getDynamicVariable(variable) {
|
|
|
|
let { parameters } = this
|
|
|
|
const queryId = variable.queryId,
|
|
|
|
name = variable.name
|
|
|
|
let value = await threadUtils.checkCacheForDynamicVariable(queryId, name)
|
|
|
|
if (!value) {
|
2021-12-17 19:21:36 +01:00
|
|
|
value = this.queryResponse[queryId]
|
|
|
|
? this.queryResponse[queryId]
|
|
|
|
: await this.runAnotherQuery(queryId, parameters)
|
|
|
|
// store incase this query is to be called again
|
|
|
|
this.queryResponse[queryId] = value
|
2021-12-16 23:43:14 +01:00
|
|
|
await threadUtils.storeDynamicVariable(queryId, name, value)
|
2021-12-17 18:56:28 +01:00
|
|
|
} else {
|
|
|
|
this.cachedVariables.push({ queryId, name })
|
2021-12-16 23:43:14 +01:00
|
|
|
}
|
|
|
|
return value
|
2021-11-10 20:35:09 +01:00
|
|
|
}
|
|
|
|
|
2021-12-16 23:43:14 +01:00
|
|
|
async addDatasourceVariables() {
|
|
|
|
let { datasource, parameters, fields } = this
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!this.noRecursiveQuery) {
|
|
|
|
// need to see if this uses any variables
|
|
|
|
const stringFields = JSON.stringify(fields)
|
|
|
|
const foundVars = dynamicVars.filter(variable => {
|
2021-12-17 15:08:48 +01:00
|
|
|
// don't allow a query to use its own dynamic variable (loop)
|
|
|
|
if (variable.queryId === this.queryId) {
|
|
|
|
return false
|
|
|
|
}
|
2021-12-16 23:43:14 +01:00
|
|
|
// look for {{ variable }} but allow spaces between handlebars
|
|
|
|
const regex = new RegExp(`{{[ ]*${variable.name}[ ]*}}`)
|
|
|
|
return regex.test(stringFields)
|
|
|
|
})
|
|
|
|
const dynamics = foundVars.map(dynVar => this.getDynamicVariable(dynVar))
|
|
|
|
const responses = await Promise.all(dynamics)
|
|
|
|
for (let i = 0; i < foundVars.length; i++) {
|
|
|
|
const variable = foundVars[i]
|
|
|
|
parameters[variable.name] = processStringSync(variable.value, {
|
|
|
|
data: responses[i].rows,
|
|
|
|
info: responses[i].extra,
|
|
|
|
})
|
2021-12-17 18:56:28 +01:00
|
|
|
// make sure its known that this uses dynamic variables in case it fails
|
|
|
|
this.hasDynamicVariables = true
|
2021-12-16 23:43:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return parameters
|
2021-11-10 20:35:09 +01:00
|
|
|
}
|
2022-01-05 10:16:10 +01:00
|
|
|
|
|
|
|
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] = this.enrichQueryFields(fields[key], parameters)
|
|
|
|
} else if (typeof fields[key] === "string") {
|
|
|
|
// enrich string value as normal
|
2022-01-05 10:21:25 +01:00
|
|
|
let value = fields[key]
|
|
|
|
// add triple brace to avoid escaping e.g. '=' in cookie header
|
|
|
|
if (IS_HANDLEBARS.test(value) && !IS_TRIPLE_BRACE.test(value)) {
|
|
|
|
value = `{${value}}`
|
|
|
|
}
|
|
|
|
enrichedQuery[key] = processStringSync(value, parameters, {
|
2022-01-05 10:16:10 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = (input, callback) => {
|
2022-01-28 01:05:39 +01:00
|
|
|
doInAppContext(input.appId, () => {
|
|
|
|
const Runner = new QueryRunner(input)
|
|
|
|
Runner.execute()
|
|
|
|
.then(response => {
|
|
|
|
callback(null, response)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
callback(err)
|
|
|
|
})
|
|
|
|
})
|
2021-11-10 20:35:09 +01:00
|
|
|
}
|