2020-12-18 19:19:43 +01:00
|
|
|
const CouchDB = require("../../db")
|
2021-01-12 17:49:11 +01:00
|
|
|
const {
|
|
|
|
generateDatasourceID,
|
|
|
|
getDatasourceParams,
|
|
|
|
getQueryParams,
|
2021-06-11 18:52:08 +02:00
|
|
|
DocumentTypes,
|
2021-06-15 20:48:05 +02:00
|
|
|
BudibaseInternalDB,
|
|
|
|
getTableParams,
|
2021-01-12 17:49:11 +01:00
|
|
|
} = require("../../db/utils")
|
2021-06-01 16:00:28 +02:00
|
|
|
const { integrations } = require("../../integrations")
|
2021-06-03 18:56:04 +02:00
|
|
|
const plusIntegrations = require("../../integrations/plus")
|
2021-06-14 20:05:39 +02:00
|
|
|
const { makeExternalQuery } = require("./row/utils")
|
2020-12-18 19:19:43 +01:00
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.fetch = async function (ctx) {
|
2021-03-29 18:32:05 +02:00
|
|
|
const database = new CouchDB(ctx.appId)
|
2021-06-15 20:48:05 +02:00
|
|
|
|
|
|
|
// Get internal tables
|
|
|
|
const db = new CouchDB(ctx.appId)
|
|
|
|
const internalTables = await db.allDocs(
|
|
|
|
getTableParams(null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
const internal = internalTables.rows.map(row => row.doc)
|
|
|
|
|
|
|
|
const bbInternalDb = {
|
|
|
|
...BudibaseInternalDB,
|
|
|
|
entities: internal,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get external datasources
|
|
|
|
const datasources = (
|
2020-12-18 19:19:43 +01:00
|
|
|
await database.allDocs(
|
|
|
|
getDatasourceParams(null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
2021-05-04 12:32:22 +02:00
|
|
|
).rows.map(row => row.doc)
|
2021-06-15 20:48:05 +02:00
|
|
|
|
|
|
|
ctx.body = [bbInternalDb, ...datasources]
|
2020-12-18 19:19:43 +01:00
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.save = async function (ctx) {
|
2021-03-29 18:32:05 +02:00
|
|
|
const db = new CouchDB(ctx.appId)
|
2021-06-11 18:52:08 +02:00
|
|
|
const plus = ctx.request.body.plus
|
2020-12-18 19:19:43 +01:00
|
|
|
|
|
|
|
const datasource = {
|
2021-06-11 18:52:08 +02:00
|
|
|
_id: generateDatasourceID({ plus }),
|
|
|
|
type: plus ? DocumentTypes.DATASOURCE_PLUS : DocumentTypes.DATASOURCE,
|
2020-12-18 19:19:43 +01:00
|
|
|
...ctx.request.body,
|
|
|
|
}
|
|
|
|
|
2021-06-11 18:52:08 +02:00
|
|
|
// update the schema
|
|
|
|
if (ctx.query.refresh) {
|
|
|
|
const PlusConnector = plusIntegrations[datasource.source].integration
|
|
|
|
|
|
|
|
const connector = new PlusConnector(ctx.request.body.config)
|
2021-06-15 14:20:25 +02:00
|
|
|
await connector.init(datasource._id)
|
2021-06-11 18:52:08 +02:00
|
|
|
|
|
|
|
datasource.entities = connector.tables
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-06-01 16:00:28 +02:00
|
|
|
// Drain connection pools when configuration is changed
|
2021-06-03 18:56:04 +02:00
|
|
|
if (datasource.source) {
|
|
|
|
const source = integrations[datasource.source]
|
|
|
|
if (source && source.pool) {
|
|
|
|
await source.pool.end()
|
|
|
|
}
|
2021-06-01 16:00:28 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02: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))
|
2021-05-04 12:32:22 +02:00
|
|
|
await db.bulkDocs(rows.rows.map(row => ({ ...row.doc, _deleted: true })))
|
2021-01-12 17:49:11 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
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
|
|
|
}
|
2021-06-03 18:56:04 +02:00
|
|
|
|
2021-06-03 19:48:04 +02:00
|
|
|
// dynamic query functionality
|
|
|
|
exports.query = async function (ctx) {
|
2021-06-04 15:53:49 +02:00
|
|
|
const queryJson = ctx.request.body
|
2021-06-14 20:05:39 +02:00
|
|
|
try {
|
|
|
|
ctx.body = await makeExternalQuery(ctx.appId, queryJson)
|
|
|
|
} catch (err) {
|
|
|
|
ctx.throw(400, err)
|
2021-06-04 15:53:49 +02:00
|
|
|
}
|
2021-06-03 19:48:04 +02:00
|
|
|
}
|