2020-12-18 19:19:43 +01:00
|
|
|
const CouchDB = require("../../db")
|
2021-01-12 17:49:11 +01:00
|
|
|
const {
|
|
|
|
generateDatasourceID,
|
|
|
|
getDatasourceParams,
|
|
|
|
getQueryParams,
|
|
|
|
} = require("../../db/utils")
|
2020-12-18 19:19:43 +01:00
|
|
|
|
|
|
|
exports.fetch = async function(ctx) {
|
2021-03-29 18:32:05 +02:00
|
|
|
const database = new CouchDB(ctx.appId)
|
2021-03-05 13:11:44 +01:00
|
|
|
ctx.body = (
|
2020-12-18 19:19:43 +01:00
|
|
|
await database.allDocs(
|
|
|
|
getDatasourceParams(null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
).rows.map(row => row.doc)
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.save = async function(ctx) {
|
2021-03-29 18:32:05 +02:00
|
|
|
const db = new CouchDB(ctx.appId)
|
2020-12-18 19:19:43 +01:00
|
|
|
|
|
|
|
const datasource = {
|
|
|
|
_id: generateDatasourceID(),
|
|
|
|
type: "datasource",
|
|
|
|
...ctx.request.body,
|
|
|
|
}
|
|
|
|
|
2021-03-10 12:56:52 +01:00
|
|
|
const response = await db.post(datasource)
|
|
|
|
datasource._rev = response.rev
|
2020-12-18 19:19:43 +01:00
|
|
|
|
|
|
|
ctx.status = 200
|
2021-03-10 12:56:52 +01:00
|
|
|
ctx.message = "Datasource saved successfully."
|
|
|
|
ctx.body = datasource
|
2020-12-18 19:19:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.destroy = async function(ctx) {
|
2021-03-29 18:32:05 +02:00
|
|
|
const db = new CouchDB(ctx.appId)
|
2021-01-12 17:49:11 +01:00
|
|
|
|
|
|
|
// Delete all queries for the datasource
|
|
|
|
const rows = await db.allDocs(getQueryParams(ctx.params.datasourceId, null))
|
|
|
|
await db.bulkDocs(rows.rows.map(row => ({ ...row.doc, _deleted: true })))
|
|
|
|
|
|
|
|
// delete the datasource
|
2021-01-12 18:45:43 +01:00
|
|
|
await db.remove(ctx.params.datasourceId, ctx.params.revId)
|
2021-01-12 17:49:11 +01:00
|
|
|
|
2020-12-18 19:19:43 +01:00
|
|
|
ctx.message = `Datasource deleted.`
|
|
|
|
ctx.status = 200
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.find = async function(ctx) {
|
2021-03-29 18:32:05 +02:00
|
|
|
const database = new CouchDB(ctx.appId)
|
2021-03-10 12:56:52 +01:00
|
|
|
ctx.body = await database.get(ctx.params.datasourceId)
|
2020-12-18 19:19:43 +01:00
|
|
|
}
|