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

180 lines
4.7 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,
DocumentTypes,
BudibaseInternalDB,
getTableParams,
2021-01-13 05:49:11 +13:00
} = require("../../db/utils")
const { BuildSchemaErrors } = require("../../constants")
const { integrations } = require("../../integrations")
2021-10-29 07:39:42 +13:00
const { getDatasourceAndQuery } = require("./row/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)
// 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-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)
ctx.body = [bbInternalDb, ...datasources]
2020-12-19 07:19:43 +13:00
}
2021-06-17 02:45:57 +12:00
exports.buildSchemaFromDb = async function (ctx) {
const db = new CouchDB(ctx.appId)
const datasource = await db.get(ctx.params.datasourceId)
2021-06-17 02:45:57 +12:00
const { tables, error } = await buildSchemaHelper(datasource)
datasource.entities = tables
2021-06-17 02:45:57 +12:00
const dbResp = await db.put(datasource)
datasource._rev = dbResp.rev
2021-06-17 02:45:57 +12:00
const response = { datasource }
if (error) {
response.error = error
}
ctx.body = response
2021-06-17 02:45:57 +12:00
}
2021-08-18 09:57:11 +12:00
exports.update = async function (ctx) {
const db = new CouchDB(ctx.appId)
const datasourceId = ctx.params.datasourceId
let datasource = await db.get(datasourceId)
datasource = { ...datasource, ...ctx.request.body }
2021-08-18 09:57:11 +12:00
const response = await db.put(datasource)
datasource._rev = response.rev
// Drain connection pools when configuration is changed
if (datasource.source) {
const source = integrations[datasource.source]
if (source && source.pool) {
await source.pool.end()
}
}
ctx.status = 200
ctx.message = "Datasource saved successfully."
ctx.body = { datasource }
2021-08-18 09:57:11 +12:00
}
2021-05-03 19:31:09 +12:00
exports.save = async function (ctx) {
const db = new CouchDB(ctx.appId)
const plus = ctx.request.body.datasource.plus
const fetchSchema = ctx.request.body.fetchSchema
2020-12-19 07:19:43 +13:00
const datasource = {
_id: generateDatasourceID({ plus }),
type: plus ? DocumentTypes.DATASOURCE_PLUS : DocumentTypes.DATASOURCE,
...ctx.request.body.datasource,
}
let schemaError = null
if (fetchSchema) {
const { tables, error } = await buildSchemaHelper(datasource)
schemaError = error
datasource.entities = tables
2020-12-19 07:19:43 +13:00
}
const dbResp = await db.put(datasource)
datasource._rev = dbResp.rev
2020-12-19 07:19:43 +13:00
// Drain connection pools when configuration is changed
2021-06-04 04:56:04 +12:00
if (datasource.source) {
const source = integrations[datasource.source]
if (source && source.pool) {
await source.pool.end()
}
}
const response = { datasource }
if (schemaError) {
response.error = schemaError
}
ctx.body = response
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
}
2021-06-04 04:56:04 +12:00
2021-06-04 05:48:04 +12:00
// dynamic query functionality
exports.query = async function (ctx) {
const queryJson = ctx.request.body
try {
2021-10-29 07:39:42 +13:00
ctx.body = await getDatasourceAndQuery(ctx.appId, queryJson)
} catch (err) {
ctx.throw(400, err)
}
2021-06-04 05:48:04 +12:00
}
const buildSchemaHelper = async datasource => {
const Connector = integrations[datasource.source]
// Connect to the DB and build the schema
const connector = new Connector(datasource.config)
await connector.buildSchema(datasource._id, datasource.entities)
datasource.entities = connector.tables
// make sure they all have a display name selected
for (let entity of Object.values(datasource.entities)) {
if (entity.primaryDisplay) {
continue
}
const notAutoColumn = Object.values(entity.schema).find(
schema => !schema.autocolumn
)
if (notAutoColumn) {
entity.primaryDisplay = notAutoColumn.name
}
}
const errors = connector.schemaErrors
let error = null
if (errors && Object.keys(errors).length > 0) {
const noKeyTables = Object.entries(errors)
.filter(entry => entry[1] === BuildSchemaErrors.NO_KEY)
.map(([name]) => name)
error = `No primary key constraint found for the following: ${noKeyTables.join(
", "
)}`
}
return { tables: connector.tables, error }
}