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