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

86 lines
2.1 KiB
JavaScript
Raw Normal View History

2021-01-07 01:28:51 +13:00
const handlebars = require("handlebars")
const CouchDB = require("../../db")
const { generateQueryID, getQueryParams } = require("../../db/utils")
const { integrations } = require("../../integrations")
2020-12-19 07:19:43 +13:00
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 = body.rows.map(row => row.doc)
}
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
2021-01-07 01:28:51 +13:00
exports.preview = async function(ctx) {
2021-01-13 05:49:11 +13:00
const { query, datasourceId, parameters, queryVerb } = ctx.request.body
2021-01-08 02:13:46 +13:00
2021-01-12 06:18:22 +13:00
let parsedQuery = ""
if (query) {
const queryTemplate = handlebars.compile(query)
parsedQuery = queryTemplate(parameters)
}
2020-12-19 07:19:43 +13:00
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-07 01:28:51 +13:00
const datasource = await db.get(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-13 05:49:11 +13:00
ctx.body = await new Integration(datasource.config, parsedQuery)[queryVerb]()
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 queryTemplate = handlebars.compile(query.queryString)
2021-01-09 07:22:03 +13:00
const parsedQuery = queryTemplate(ctx.request.body.parameters)
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
// call the relevant CRUD method on the integration class
const response = await new Integration(datasource.config, parsedQuery)[
query.queryVerb
]()
2021-01-07 01:28:51 +13:00
ctx.body = response
}
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
}