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

153 lines
3.8 KiB
JavaScript
Raw Normal View History

const { processString } = require("@budibase/string-templates")
2021-01-07 01:28:51 +13:00
const CouchDB = require("../../db")
const { generateQueryID, getQueryParams } = require("../../db/utils")
const { integrations } = require("../../integrations")
const { BaseQueryVerbs } = require("../../constants")
const env = require("../../environment")
// 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
function formatResponse(resp) {
if (typeof resp === "string") {
resp = JSON.parse(resp)
}
if (!Array.isArray(resp)) {
resp = [resp]
}
return resp
}
2021-01-07 01:28:51 +13:00
exports.fetch = async function(ctx) {
const db = new CouchDB(ctx.user.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,
})
)
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-01-07 01:28:51 +13:00
exports.save = async function(ctx) {
const db = new CouchDB(ctx.user.appId)
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
async function enrichQueryFields(fields, parameters) {
2021-01-14 03:11:53 +13:00
const enrichedQuery = {}
2021-01-14 03:11:53 +13:00
// enrich the fields with dynamic parameters
for (let key of Object.keys(fields)) {
2021-02-16 06:05:53 +13:00
if (typeof fields[key] === "object") {
// enrich nested fields object
enrichedQuery[key] = await enrichQueryFields(fields[key], parameters)
} else {
// enrich string value as normal
enrichedQuery[key] = await processString(fields[key], parameters)
}
2021-01-12 06:18:22 +13:00
}
2021-02-16 08:57:49 +13:00
if (
enrichedQuery.json ||
enrichedQuery.customData ||
enrichedQuery.requestBody
) {
try {
enrichedQuery.json = JSON.parse(
2021-02-16 08:57:49 +13:00
enrichedQuery.json ||
enrichedQuery.customData ||
enrichedQuery.requestBody
)
} catch (err) {
throw { message: `JSON Invalid - error: ${err}` }
}
delete enrichedQuery.customData
}
2021-01-14 03:11:53 +13:00
return enrichedQuery
}
2020-12-19 07:19:43 +13:00
exports.find = async function(ctx) {
const db = new CouchDB(ctx.user.appId)
const query = enrichQueries(await db.get(ctx.params.queryId))
// remove properties that could be dangerous in real app
if (env.CLOUD) {
delete query.fields
delete query.parameters
delete query.schema
}
ctx.body = query
}
2021-01-14 03:11:53 +13:00
exports.preview = async function(ctx) {
2021-01-07 01:28:51 +13:00
const db = new CouchDB(ctx.user.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-01-07 01:28:51 +13:00
const Integration = integrations[datasource.source]
2020-12-19 07:19:43 +13:00
2021-01-07 01:28:51 +13:00
if (!Integration) {
ctx.throw(400, "Integration type does not exist.")
return
}
2021-01-14 03:11:53 +13:00
const { fields, parameters, queryVerb } = ctx.request.body
const enrichedQuery = await enrichQueryFields(fields, parameters)
2021-01-14 03:11:53 +13:00
ctx.body = formatResponse(
await new Integration(datasource.config)[queryVerb](enrichedQuery)
)
2021-01-07 01:28:51 +13:00
}
exports.execute = async function(ctx) {
const db = new CouchDB(ctx.user.appId)
const query = await db.get(ctx.params.queryId)
const datasource = await db.get(query.datasourceId)
2021-01-07 01:28:51 +13:00
const Integration = integrations[datasource.source]
if (!Integration) {
ctx.throw(400, "Integration type does not exist.")
return
}
2021-01-13 05:49:11 +13:00
const enrichedQuery = await enrichQueryFields(
2021-01-14 03:11:53 +13:00
query.fields,
ctx.request.body.parameters
)
2021-01-13 05:49:11 +13:00
// call the relevant CRUD method on the integration class
ctx.body = formatResponse(
await new Integration(datasource.config)[query.queryVerb](enrichedQuery)
2021-01-14 03:11:53 +13:00
)
2021-01-07 01:28:51 +13:00
}
exports.destroy = async function(ctx) {
const db = new CouchDB(ctx.user.appId)
await db.remove(ctx.params.queryId, ctx.params.revId)
2021-01-07 01:28:51 +13:00
ctx.message = `Query deleted.`
ctx.status = 200
}