From 40111491c42ce81d30be64ee45cb243450572835 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Thu, 13 Jan 2022 15:10:55 +0100 Subject: [PATCH] remove old query file --- packages/server/src/api/controllers/query.js | 161 ------------------- 1 file changed, 161 deletions(-) delete mode 100644 packages/server/src/api/controllers/query.js diff --git a/packages/server/src/api/controllers/query.js b/packages/server/src/api/controllers/query.js deleted file mode 100644 index 4610fe57c3..0000000000 --- a/packages/server/src/api/controllers/query.js +++ /dev/null @@ -1,161 +0,0 @@ -const { processString } = require("@budibase/string-templates") -const CouchDB = require("../../db") -const { - generateQueryID, - getQueryParams, - isProdAppID, -} = require("../../db/utils") -const { BaseQueryVerbs } = require("../../constants") -const { Thread, ThreadType } = require("../../threads") -const env = require("../../environment") - -const Runner = new Thread(ThreadType.QUERY, { - timeoutMs: env.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] -} - -exports.fetch = async function (ctx) { - const db = new CouchDB(ctx.appId) - - const body = await db.allDocs( - getQueryParams(null, { - include_docs: true, - }) - ) - ctx.body = enrichQueries(body.rows.map(row => row.doc)) -} - -exports.save = async function (ctx) { - const db = new CouchDB(ctx.appId) - const query = ctx.request.body - - if (!query._id) { - query._id = generateQueryID(query.datasourceId) - } - - const response = await db.put(query) - query._rev = response.rev - - ctx.body = query - ctx.message = `Query ${query.name} saved successfully.` -} - -async 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] = await enrichQueryFields(fields[key], parameters) - } else if (typeof fields[key] === "string") { - // enrich string value as normal - enrichedQuery[key] = await processString(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) { - throw { message: `JSON Invalid - error: ${err}` } - } - delete enrichedQuery.customData - } - - return enrichedQuery -} - -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 -} - -exports.preview = async function (ctx) { - const db = new CouchDB(ctx.appId) - - const datasource = await db.get(ctx.request.body.datasourceId) - - const { fields, parameters, queryVerb, transformer } = ctx.request.body - const enrichedQuery = await enrichQueryFields(fields, parameters) - - 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) - } -} - -exports.execute = async function (ctx) { - const db = new CouchDB(ctx.appId) - - const query = await db.get(ctx.params.queryId) - const datasource = await db.get(query.datasourceId) - - const enrichedQuery = await enrichQueryFields( - query.fields, - ctx.request.body.parameters - ) - - // call the relevant CRUD method on the integration class - try { - const { rows } = await Runner.run({ - datasource, - queryVerb: query.queryVerb, - query: enrichedQuery, - transformer: query.transformer, - }) - ctx.body = rows - } catch (err) { - ctx.throw(400, err) - } -} - -exports.destroy = async function (ctx) { - const db = new CouchDB(ctx.appId) - await db.remove(ctx.params.queryId, ctx.params.revId) - ctx.message = `Query deleted.` - ctx.status = 200 -}