1
0
Fork 0
mirror of synced 2024-06-01 18:20:18 +12:00
budibase/packages/server/src/api/controllers/query/index.js

200 lines
5.2 KiB
JavaScript
Raw Normal View History

2021-12-03 04:20:03 +13:00
const CouchDB = require("../../../db")
const {
generateQueryID,
getQueryParams,
isProdAppID,
2021-12-03 04:20:03 +13:00
} = require("../../../db/utils")
const { BaseQueryVerbs } = require("../../../constants")
const { Thread, ThreadType } = require("../../../threads")
const { save: saveDatasource } = require("../datasource")
2021-12-01 22:48:52 +13:00
const { RestImporter } = require("./import")
const { invalidateDynamicVariables } = require("../../../threads/utils")
2022-01-18 03:57:31 +13:00
const environment = require("../../../environment")
2022-01-18 03:57:31 +13:00
const Runner = new Thread(ThreadType.QUERY, {
timeoutMs: environment.QUERY_THREAD_TIMEOUT || 10000,
})
// 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) {
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-11-26 06:14:07 +13:00
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
exports.import = async ctx => {
const body = ctx.request.body
const data = body.data
2021-12-01 22:48:52 +13:00
const importer = new RestImporter(data)
await importer.init()
let datasourceId
if (!body.datasourceId) {
// construct new datasource
2021-12-01 22:48:52 +13:00
const info = await importer.getInfo()
let datasource = {
type: "datasource",
source: "REST",
config: {
url: info.url,
2021-12-01 22:48:52 +13:00
defaultHeaders: [],
},
name: info.name,
}
// save the datasource
const datasourceCtx = { ...ctx }
datasourceCtx.request.body.datasource = datasource
await saveDatasource(datasourceCtx)
datasourceId = datasourceCtx.body.datasource._id
2021-11-26 22:51:56 +13:00
} else {
// use existing datasource
datasourceId = body.datasourceId
2021-11-26 06:14:07 +13:00
}
2021-12-01 22:48:52 +13:00
const importResult = await importer.importQueries(ctx.appId, datasourceId)
2021-11-26 06:14:07 +13:00
ctx.body = {
...importResult,
datasourceId,
2021-11-26 06:14:07 +13:00
}
ctx.status = 200
}
2021-05-03 19:31:09 +12:00
exports.save = async function (ctx) {
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-05-03 19:31:09 +12:00
exports.find = async function (ctx) {
const db = new CouchDB(ctx.appId)
const query = enrichQueries(await db.get(ctx.params.queryId))
// remove properties that could be dangerous in real app
if (isProdAppID(ctx.appId)) {
delete query.fields
delete query.parameters
}
ctx.body = query
}
2021-05-03 19:31:09 +12:00
exports.preview = async function (ctx) {
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)
// preview may not have a queryId as it hasn't been saved, but if it does
// this stops dynamic variables from calling the same query
const { fields, parameters, queryVerb, transformer, queryId } =
ctx.request.body
try {
const { rows, keys, info, extra } = await Runner.run({
appId: ctx.appId,
datasource,
queryVerb,
2021-12-17 05:58:15 +13:00
fields,
parameters,
transformer,
queryId,
})
ctx.body = {
rows,
schemaFields: [...new Set(keys)],
info,
extra,
}
} catch (err) {
ctx.throw(400, err)
}
2021-01-07 01:28:51 +13:00
}
async function execute(ctx, opts = { rowsOnly: false }) {
const db = new CouchDB(ctx.appId)
2021-01-07 01:28:51 +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-01-13 05:49:11 +13:00
// call the relevant CRUD method on the integration class
try {
const { rows, pagination, extra } = await Runner.run({
appId: ctx.appId,
datasource,
queryVerb: query.queryVerb,
2021-12-17 05:58:15 +13:00
fields: query.fields,
pagination: ctx.request.body.pagination,
parameters: ctx.request.body.parameters,
transformer: query.transformer,
queryId: ctx.params.queryId,
})
if (opts && opts.rowsOnly) {
ctx.body = rows
} else {
ctx.body = { data: rows, pagination, ...extra }
}
} catch (err) {
ctx.throw(400, err)
}
2021-01-07 01:28:51 +13:00
}
exports.executeV1 = async function (ctx) {
return execute(ctx, { rowsOnly: true })
}
exports.executeV2 = async function (ctx) {
return execute(ctx, { rowsOnly: false })
}
const removeDynamicVariables = async (db, queryId) => {
const query = await db.get(queryId)
const datasource = await db.get(query.datasourceId)
const dynamicVariables = datasource.config.dynamicVariables
if (dynamicVariables) {
// delete dynamic variables from the datasource
const newVariables = dynamicVariables.filter(dv => dv.queryId !== queryId)
datasource.config.dynamicVariables = newVariables
await db.put(datasource)
// invalidate the deleted variables
const variablesToDelete = dynamicVariables.filter(
dv => dv.queryId === queryId
)
await invalidateDynamicVariables(variablesToDelete)
}
}
2021-05-03 19:31:09 +12:00
exports.destroy = async function (ctx) {
const db = new CouchDB(ctx.appId)
await removeDynamicVariables(db, ctx.params.queryId)
await db.remove(ctx.params.queryId, ctx.params.revId)
2021-01-07 01:28:51 +13:00
ctx.message = `Query deleted.`
ctx.status = 200
}