1
0
Fork 0
mirror of synced 2024-06-28 02:50:50 +12:00
budibase/packages/server/src/api/controllers/datasource.js

54 lines
1.3 KiB
JavaScript
Raw Normal View History

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) {
const database = new CouchDB(ctx.appId)
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) {
const db = new CouchDB(ctx.appId)
2020-12-19 07:19:43 +13:00
const datasource = {
_id: generateDatasourceID(),
type: "datasource",
...ctx.request.body,
}
const response = await db.post(datasource)
datasource._rev = response.rev
2020-12-19 07:19:43 +13:00
ctx.status = 200
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) {
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
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) {
const database = new CouchDB(ctx.appId)
ctx.body = await database.get(ctx.params.datasourceId)
2020-12-19 07:19:43 +13:00
}