2021-02-03 05:26:15 +13:00
|
|
|
const { processString } = require("@budibase/string-templates")
|
2021-01-07 01:28:51 +13:00
|
|
|
const CouchDB = require("../../db")
|
|
|
|
const { generateQueryID, getQueryParams } = require("../../db/utils")
|
2021-02-06 05:45:23 +13:00
|
|
|
const { BaseQueryVerbs } = require("../../constants")
|
|
|
|
const env = require("../../environment")
|
2021-11-12 01:11:09 +13:00
|
|
|
const { Thread, ThreadType } = require("../../threads")
|
|
|
|
|
2021-11-26 06:14:07 +13:00
|
|
|
const fetch = require("node-fetch")
|
|
|
|
const Joi = require("joi")
|
|
|
|
const { save: saveDatasource } = require("./datasource")
|
|
|
|
|
2021-11-12 01:11:09 +13:00
|
|
|
const Runner = new Thread(ThreadType.QUERY, { timeoutMs: 10000 })
|
2021-02-06 05:45:23 +13:00
|
|
|
|
|
|
|
// simple function to append "readable" to all read queries
|
|
|
|
function enrichQueries(input) {
|
|
|
|
const wasArray = Array.isArray(input)
|
|
|
|
const queries = wasArray ? input : [input]
|
|
|
|
for (let query of queries) {
|
|
|
|
if (query.queryVerb === BaseQueryVerbs.READ) {
|
|
|
|
query.readable = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wasArray ? queries : queries[0]
|
|
|
|
}
|
2020-12-19 07:19:43 +13:00
|
|
|
|
2021-05-03 19:31:09 +12:00
|
|
|
exports.fetch = async function (ctx) {
|
2021-03-30 05:32:05 +13:00
|
|
|
const db = new CouchDB(ctx.appId)
|
2020-12-19 07:19:43 +13:00
|
|
|
|
2021-01-07 01:28:51 +13:00
|
|
|
const body = await db.allDocs(
|
|
|
|
getQueryParams(null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
2021-05-04 22:32:22 +12:00
|
|
|
ctx.body = enrichQueries(body.rows.map(row => row.doc))
|
2021-01-07 01:28:51 +13:00
|
|
|
}
|
2020-12-19 07:19:43 +13:00
|
|
|
|
2021-11-26 06:14:07 +13:00
|
|
|
// const query = {
|
|
|
|
// datasourceId: "datasource_b9a474302a174d1295e4c273cd72bde9",
|
|
|
|
// name: "available pets (import)",
|
|
|
|
// parameters: [],
|
|
|
|
// fields: {
|
|
|
|
// headers: {},
|
|
|
|
// queryString: "status=available",
|
|
|
|
// path: "v2/pet/findByStatus",
|
|
|
|
// },
|
|
|
|
// queryVerb: "read",
|
|
|
|
// transformer: "return data",
|
|
|
|
// schema: {},
|
|
|
|
// readable: true
|
|
|
|
// }
|
|
|
|
|
|
|
|
function generateQueryValidation() {
|
|
|
|
// prettier-ignore
|
|
|
|
return Joi.object({
|
|
|
|
_id: Joi.string(),
|
|
|
|
_rev: Joi.string(),
|
|
|
|
name: Joi.string().required(),
|
|
|
|
fields: Joi.object().required(),
|
|
|
|
datasourceId: Joi.string().required(),
|
|
|
|
readable: Joi.boolean(),
|
|
|
|
parameters: Joi.array().items(Joi.object({
|
|
|
|
name: Joi.string(),
|
|
|
|
default: Joi.string().allow(""),
|
|
|
|
})),
|
|
|
|
queryVerb: Joi.string().allow().required(),
|
|
|
|
extra: Joi.object().optional(),
|
|
|
|
schema: Joi.object({}).required().unknown(true),
|
|
|
|
transformer: Joi.string().optional(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const verbs = {
|
|
|
|
get: "read",
|
|
|
|
post: "create",
|
|
|
|
put: "update",
|
|
|
|
patch: "patch",
|
|
|
|
delete: "delete",
|
|
|
|
}
|
|
|
|
|
|
|
|
const constructQuery = (datasource, swagger, path, method, config) => {
|
|
|
|
const query = {
|
|
|
|
datasourceId: datasource._id,
|
|
|
|
}
|
|
|
|
query.name = config.operationId || path
|
|
|
|
query.parameters = []
|
|
|
|
query.fields = {
|
|
|
|
headers: {},
|
|
|
|
// queryString: "status=available",
|
|
|
|
path: path,
|
|
|
|
}
|
|
|
|
query.transformer = "return data"
|
|
|
|
query.schema = {}
|
|
|
|
query.readable = true
|
|
|
|
query.queryVerb = verbs[method]
|
|
|
|
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
|
|
|
// {
|
|
|
|
// "type": "url",
|
|
|
|
// "data": "www.url.com/swagger.json"
|
|
|
|
// }
|
|
|
|
|
|
|
|
exports.import = async function (ctx) {
|
|
|
|
const importConfig = ctx.request.body
|
|
|
|
|
|
|
|
let data
|
|
|
|
|
|
|
|
if (importConfig.type === "url") {
|
|
|
|
data = await fetch(importConfig.data).then(res => res.json())
|
|
|
|
} else if (importConfig.type === "raw") {
|
|
|
|
data = JSON.parse(importConfig.data)
|
|
|
|
}
|
|
|
|
|
|
|
|
const db = new CouchDB(ctx.appId)
|
|
|
|
const schema = generateQueryValidation()
|
|
|
|
|
|
|
|
// create datasource
|
|
|
|
const scheme = data.schemes.includes("https") ? "https" : "http"
|
|
|
|
const url = `${scheme}://${data.host}${data.basePath}`
|
|
|
|
const name = data.info.title
|
|
|
|
|
|
|
|
// TODO: Refactor datasource creation into shared function
|
|
|
|
const datasourceCtx = {
|
|
|
|
...ctx,
|
|
|
|
}
|
|
|
|
datasourceCtx.request.body.datasource = {
|
|
|
|
type: "datasource",
|
|
|
|
source: "REST",
|
|
|
|
config: {
|
|
|
|
url: url,
|
|
|
|
defaultHeaders: {},
|
|
|
|
},
|
|
|
|
name: name,
|
|
|
|
}
|
|
|
|
await saveDatasource(datasourceCtx)
|
|
|
|
const datasource = datasourceCtx.body.datasource
|
|
|
|
|
|
|
|
// create query
|
|
|
|
|
|
|
|
for (const [path, method] of Object.entries(data.paths)) {
|
|
|
|
for (const [methodName, config] of Object.entries(method)) {
|
|
|
|
const query = constructQuery(datasource, data, path, methodName, config)
|
|
|
|
|
|
|
|
// validate query
|
|
|
|
const { error } = schema.validate(query)
|
|
|
|
if (error) {
|
|
|
|
ctx.throw(400, `Invalid - ${error.message}`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// persist query
|
|
|
|
query._id = generateQueryID(query.datasourceId)
|
|
|
|
await db.put(query)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return the datasource
|
|
|
|
ctx.body = { datasource }
|
|
|
|
ctx.status = 200
|
|
|
|
}
|
|
|
|
|
2021-05-03 19:31:09 +12:00
|
|
|
exports.save = async function (ctx) {
|
2021-03-30 05:32:05 +13:00
|
|
|
const db = new CouchDB(ctx.appId)
|
2021-01-07 01:28:51 +13:00
|
|
|
const query = ctx.request.body
|
2020-12-19 07:19:43 +13:00
|
|
|
|
2021-01-07 01:28:51 +13:00
|
|
|
if (!query._id) {
|
|
|
|
query._id = generateQueryID(query.datasourceId)
|
|
|
|
}
|
2020-12-19 07:19:43 +13:00
|
|
|
|
2021-01-07 01:28:51 +13:00
|
|
|
const response = await db.put(query)
|
|
|
|
query._rev = response.rev
|
2020-12-19 07:19:43 +13:00
|
|
|
|
2021-01-07 01:28:51 +13:00
|
|
|
ctx.body = query
|
|
|
|
ctx.message = `Query ${query.name} saved successfully.`
|
|
|
|
}
|
2020-12-19 07:19:43 +13:00
|
|
|
|
2021-06-09 01:26:06 +12:00
|
|
|
async function enrichQueryFields(fields, parameters = {}) {
|
2021-01-14 03:11:53 +13:00
|
|
|
const enrichedQuery = {}
|
2021-01-16 02:11:51 +13:00
|
|
|
|
2021-01-14 03:11:53 +13:00
|
|
|
// enrich the fields with dynamic parameters
|
2021-02-03 05:26:15 +13:00
|
|
|
for (let key of Object.keys(fields)) {
|
2021-05-12 23:52:34 +12:00
|
|
|
if (fields[key] == null) {
|
|
|
|
continue
|
|
|
|
}
|
2021-02-16 06:05:53 +13:00
|
|
|
if (typeof fields[key] === "object") {
|
|
|
|
// enrich nested fields object
|
|
|
|
enrichedQuery[key] = await enrichQueryFields(fields[key], parameters)
|
2021-03-10 23:39:58 +13:00
|
|
|
} else if (typeof fields[key] === "string") {
|
2021-02-16 06:05:53 +13:00
|
|
|
// enrich string value as normal
|
2021-10-27 08:04:36 +13:00
|
|
|
enrichedQuery[key] = await processString(fields[key], parameters, {
|
|
|
|
noHelpers: true,
|
|
|
|
})
|
2021-03-10 23:39:58 +13:00
|
|
|
} else {
|
|
|
|
enrichedQuery[key] = fields[key]
|
2021-02-16 06:05:53 +13:00
|
|
|
}
|
2021-01-12 06:18:22 +13:00
|
|
|
}
|
2021-01-16 02:11:51 +13:00
|
|
|
|
2021-02-16 08:57:49 +13:00
|
|
|
if (
|
|
|
|
enrichedQuery.json ||
|
|
|
|
enrichedQuery.customData ||
|
|
|
|
enrichedQuery.requestBody
|
|
|
|
) {
|
2021-02-03 05:44:31 +13:00
|
|
|
try {
|
|
|
|
enrichedQuery.json = JSON.parse(
|
2021-02-16 08:57:49 +13:00
|
|
|
enrichedQuery.json ||
|
|
|
|
enrichedQuery.customData ||
|
|
|
|
enrichedQuery.requestBody
|
2021-02-03 05:44:31 +13:00
|
|
|
)
|
|
|
|
} catch (err) {
|
|
|
|
throw { message: `JSON Invalid - error: ${err}` }
|
|
|
|
}
|
2021-01-16 02:11:51 +13:00
|
|
|
delete enrichedQuery.customData
|
|
|
|
}
|
|
|
|
|
2021-01-14 03:11:53 +13:00
|
|
|
return enrichedQuery
|
|
|
|
}
|
2020-12-19 07:19:43 +13:00
|
|
|
|
2021-05-03 19:31:09 +12:00
|
|
|
exports.find = async function (ctx) {
|
2021-03-30 05:32:05 +13:00
|
|
|
const db = new CouchDB(ctx.appId)
|
2021-02-06 05:45:23 +13:00
|
|
|
const query = enrichQueries(await db.get(ctx.params.queryId))
|
|
|
|
// remove properties that could be dangerous in real app
|
2021-03-25 07:21:23 +13:00
|
|
|
if (env.isProd()) {
|
2021-02-06 05:45:23 +13:00
|
|
|
delete query.fields
|
|
|
|
delete query.parameters
|
|
|
|
delete query.schema
|
|
|
|
}
|
|
|
|
ctx.body = query
|
|
|
|
}
|
|
|
|
|
2021-05-03 19:31:09 +12:00
|
|
|
exports.preview = async function (ctx) {
|
2021-03-30 05:32:05 +13:00
|
|
|
const db = new CouchDB(ctx.appId)
|
2020-12-19 07:19:43 +13:00
|
|
|
|
2021-01-14 03:11:53 +13:00
|
|
|
const datasource = await db.get(ctx.request.body.datasourceId)
|
2020-12-19 07:19:43 +13:00
|
|
|
|
2021-10-13 06:45:13 +13:00
|
|
|
const { fields, parameters, queryVerb, transformer } = ctx.request.body
|
2021-02-03 05:26:15 +13:00
|
|
|
const enrichedQuery = await enrichQueryFields(fields, parameters)
|
2021-02-23 06:41:02 +13:00
|
|
|
|
2021-11-12 05:20:30 +13:00
|
|
|
try {
|
|
|
|
const { rows, keys } = await Runner.run({
|
|
|
|
datasource,
|
|
|
|
queryVerb,
|
|
|
|
query: enrichedQuery,
|
|
|
|
transformer,
|
|
|
|
})
|
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
rows,
|
|
|
|
schemaFields: [...new Set(keys)],
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
ctx.throw(400, err)
|
2021-02-23 06:41:02 +13:00
|
|
|
}
|
2021-01-07 01:28:51 +13:00
|
|
|
}
|
|
|
|
|
2021-05-03 19:31:09 +12:00
|
|
|
exports.execute = async function (ctx) {
|
2021-03-30 05:32:05 +13:00
|
|
|
const db = new CouchDB(ctx.appId)
|
2021-01-07 01:28:51 +13:00
|
|
|
|
2021-01-09 01:06:37 +13:00
|
|
|
const query = await db.get(ctx.params.queryId)
|
|
|
|
const datasource = await db.get(query.datasourceId)
|
2021-01-07 01:28:51 +13:00
|
|
|
|
2021-02-03 05:26:15 +13:00
|
|
|
const enrichedQuery = await enrichQueryFields(
|
2021-01-14 03:11:53 +13:00
|
|
|
query.fields,
|
|
|
|
ctx.request.body.parameters
|
|
|
|
)
|
2021-10-13 06:45:13 +13:00
|
|
|
|
2021-01-13 05:49:11 +13:00
|
|
|
// call the relevant CRUD method on the integration class
|
2021-11-12 05:20:30 +13:00
|
|
|
try {
|
|
|
|
const { rows } = await Runner.run({
|
|
|
|
datasource,
|
|
|
|
queryVerb: query.queryVerb,
|
|
|
|
query: enrichedQuery,
|
|
|
|
transformer: query.transformer,
|
|
|
|
})
|
|
|
|
ctx.body = rows
|
|
|
|
} catch (err) {
|
|
|
|
ctx.throw(400, err)
|
|
|
|
}
|
2021-01-07 01:28:51 +13:00
|
|
|
}
|
|
|
|
|
2021-05-03 19:31:09 +12:00
|
|
|
exports.destroy = async function (ctx) {
|
2021-03-30 05:32:05 +13:00
|
|
|
const db = new CouchDB(ctx.appId)
|
2021-01-13 06:45:43 +13:00
|
|
|
await db.remove(ctx.params.queryId, ctx.params.revId)
|
2021-01-07 01:28:51 +13:00
|
|
|
ctx.message = `Query deleted.`
|
|
|
|
ctx.status = 200
|
|
|
|
}
|