2021-01-06 13:28:51 +01:00
|
|
|
const handlebars = require("handlebars")
|
|
|
|
const CouchDB = require("../../db")
|
|
|
|
const { generateQueryID, getQueryParams } = require("../../db/utils")
|
|
|
|
const { integrations } = require("../../integrations")
|
2020-12-18 19:19:43 +01:00
|
|
|
|
2021-01-06 13:28:51 +01:00
|
|
|
exports.fetch = async function(ctx) {
|
|
|
|
const db = new CouchDB(ctx.user.appId)
|
2020-12-18 19:19:43 +01:00
|
|
|
|
2021-01-06 13:28:51 +01:00
|
|
|
const body = await db.allDocs(
|
|
|
|
getQueryParams(null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
ctx.body = body.rows.map(row => row.doc)
|
|
|
|
}
|
2020-12-18 19:19:43 +01:00
|
|
|
|
2021-01-06 13:28:51 +01:00
|
|
|
exports.save = async function(ctx) {
|
|
|
|
const db = new CouchDB(ctx.user.appId)
|
|
|
|
const query = ctx.request.body
|
2020-12-18 19:19:43 +01:00
|
|
|
|
2021-01-06 13:28:51 +01:00
|
|
|
if (!query._id) {
|
|
|
|
query._id = generateQueryID(query.datasourceId)
|
|
|
|
}
|
2020-12-18 19:19:43 +01:00
|
|
|
|
2021-01-06 13:28:51 +01:00
|
|
|
const response = await db.put(query)
|
|
|
|
query._rev = response.rev
|
2020-12-18 19:19:43 +01:00
|
|
|
|
2021-01-06 13:28:51 +01:00
|
|
|
ctx.body = query
|
|
|
|
ctx.message = `Query ${query.name} saved successfully.`
|
|
|
|
}
|
2020-12-18 19:19:43 +01:00
|
|
|
|
2021-01-13 15:11:53 +01:00
|
|
|
function enrichQueryFields(fields, parameters) {
|
|
|
|
const enrichedQuery = {}
|
|
|
|
// enrich the fields with dynamic parameters
|
|
|
|
for (let key in fields) {
|
|
|
|
const template = handlebars.compile(fields[key])
|
|
|
|
enrichedQuery[key] = template(parameters)
|
2021-01-11 18:18:22 +01:00
|
|
|
}
|
2021-01-13 15:11:53 +01:00
|
|
|
return enrichedQuery
|
|
|
|
}
|
2020-12-18 19:19:43 +01:00
|
|
|
|
2021-01-13 15:11:53 +01:00
|
|
|
exports.preview = async function(ctx) {
|
2021-01-06 13:28:51 +01:00
|
|
|
const db = new CouchDB(ctx.user.appId)
|
2020-12-18 19:19:43 +01:00
|
|
|
|
2021-01-13 15:11:53 +01:00
|
|
|
const datasource = await db.get(ctx.request.body.datasourceId)
|
2020-12-18 19:19:43 +01:00
|
|
|
|
2021-01-06 13:28:51 +01:00
|
|
|
const Integration = integrations[datasource.source]
|
2020-12-18 19:19:43 +01:00
|
|
|
|
2021-01-06 13:28:51 +01:00
|
|
|
if (!Integration) {
|
|
|
|
ctx.throw(400, "Integration type does not exist.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-13 15:11:53 +01:00
|
|
|
const { fields, parameters, queryVerb } = ctx.request.body
|
|
|
|
|
|
|
|
const enrichedQuery = enrichQueryFields(fields, parameters)
|
|
|
|
|
|
|
|
ctx.body = await new Integration(datasource.config)[queryVerb](enrichedQuery)
|
2021-01-06 13:28:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.execute = async function(ctx) {
|
|
|
|
const db = new CouchDB(ctx.user.appId)
|
|
|
|
|
2021-01-08 13:06:37 +01:00
|
|
|
const query = await db.get(ctx.params.queryId)
|
|
|
|
const datasource = await db.get(query.datasourceId)
|
2021-01-06 13:28:51 +01:00
|
|
|
|
|
|
|
const Integration = integrations[datasource.source]
|
|
|
|
|
|
|
|
if (!Integration) {
|
|
|
|
ctx.throw(400, "Integration type does not exist.")
|
|
|
|
return
|
|
|
|
}
|
2021-01-12 17:49:11 +01:00
|
|
|
|
2021-01-13 15:11:53 +01:00
|
|
|
const enrichedQuery = enrichQueryFields(
|
|
|
|
query.fields,
|
|
|
|
ctx.request.body.parameters
|
|
|
|
)
|
|
|
|
|
2021-01-12 17:49:11 +01:00
|
|
|
// call the relevant CRUD method on the integration class
|
2021-01-13 15:11:53 +01:00
|
|
|
const response = await new Integration(datasource.config)[query.queryVerb](
|
|
|
|
enrichedQuery
|
|
|
|
)
|
2021-01-06 13:28:51 +01:00
|
|
|
|
|
|
|
ctx.body = response
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.destroy = async function(ctx) {
|
|
|
|
const db = new CouchDB(ctx.user.appId)
|
2021-01-12 18:45:43 +01:00
|
|
|
await db.remove(ctx.params.queryId, ctx.params.revId)
|
2021-01-06 13:28:51 +01:00
|
|
|
ctx.message = `Query deleted.`
|
|
|
|
ctx.status = 200
|
|
|
|
}
|