Implementing main body of connection verification endpoint.
This commit is contained in:
parent
ce6c5bfa68
commit
57a633b926
|
@ -18,11 +18,68 @@ import {
|
||||||
Row,
|
Row,
|
||||||
CreateDatasourceResponse,
|
CreateDatasourceResponse,
|
||||||
UpdateDatasourceResponse,
|
UpdateDatasourceResponse,
|
||||||
UpdateDatasourceRequest,
|
|
||||||
CreateDatasourceRequest,
|
CreateDatasourceRequest,
|
||||||
|
VerifyDatasourceRequest,
|
||||||
|
VerifyDatasourceResponse,
|
||||||
|
IntegrationBase,
|
||||||
|
DatasourcePlus,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import sdk from "../../sdk"
|
import sdk from "../../sdk"
|
||||||
|
|
||||||
|
function getErrorTables(errors: any, errorType: string) {
|
||||||
|
return Object.entries(errors)
|
||||||
|
.filter(entry => entry[1] === errorType)
|
||||||
|
.map(([name]) => name)
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateError(error: any, newError: any, tables: string[]) {
|
||||||
|
if (!error) {
|
||||||
|
error = ""
|
||||||
|
}
|
||||||
|
if (error.length > 0) {
|
||||||
|
error += "\n"
|
||||||
|
}
|
||||||
|
error += `${newError} ${tables.join(", ")}`
|
||||||
|
return error
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getConnector(
|
||||||
|
datasource: Datasource
|
||||||
|
): Promise<IntegrationBase | DatasourcePlus> {
|
||||||
|
const Connector = await getIntegration(datasource.source)
|
||||||
|
datasource = await sdk.datasources.enrich(datasource)
|
||||||
|
// Connect to the DB and build the schema
|
||||||
|
return new Connector(datasource.config)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function buildSchemaHelper(datasource: Datasource) {
|
||||||
|
const connector = (await getConnector(datasource)) as DatasourcePlus
|
||||||
|
await connector.buildSchema(datasource._id!, datasource.entities!)
|
||||||
|
|
||||||
|
const errors = connector.schemaErrors
|
||||||
|
let error = null
|
||||||
|
if (errors && Object.keys(errors).length > 0) {
|
||||||
|
const noKey = getErrorTables(errors, BuildSchemaErrors.NO_KEY)
|
||||||
|
const invalidCol = getErrorTables(errors, BuildSchemaErrors.INVALID_COLUMN)
|
||||||
|
if (noKey.length) {
|
||||||
|
error = updateError(
|
||||||
|
error,
|
||||||
|
"No primary key constraint found for the following:",
|
||||||
|
noKey
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if (invalidCol.length) {
|
||||||
|
const invalidCols = Object.values(InvalidColumns).join(", ")
|
||||||
|
error = updateError(
|
||||||
|
error,
|
||||||
|
`Cannot use columns ${invalidCols} found in following:`,
|
||||||
|
invalidCol
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return { tables: connector.tables, error }
|
||||||
|
}
|
||||||
|
|
||||||
export async function fetch(ctx: UserCtx) {
|
export async function fetch(ctx: UserCtx) {
|
||||||
// Get internal tables
|
// Get internal tables
|
||||||
const db = context.getAppDB()
|
const db = context.getAppDB()
|
||||||
|
@ -66,6 +123,20 @@ export async function fetch(ctx: UserCtx) {
|
||||||
ctx.body = [bbInternalDb, ...datasources]
|
ctx.body = [bbInternalDb, ...datasources]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function verify(
|
||||||
|
ctx: UserCtx<VerifyDatasourceRequest, VerifyDatasourceResponse>
|
||||||
|
) {
|
||||||
|
const datasource = ctx.request.body.datasource
|
||||||
|
const connector = (await getConnector(datasource)) as IntegrationBase
|
||||||
|
if (!connector.connection) {
|
||||||
|
ctx.throw(400, "Connection information verification not supported")
|
||||||
|
}
|
||||||
|
const connectionInfo = await connector.connection()
|
||||||
|
ctx.body = {
|
||||||
|
connected: connectionInfo.connected,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function buildSchemaFromDb(ctx: UserCtx) {
|
export async function buildSchemaFromDb(ctx: UserCtx) {
|
||||||
const db = context.getAppDB()
|
const db = context.getAppDB()
|
||||||
const datasource = await sdk.datasources.get(ctx.params.datasourceId)
|
const datasource = await sdk.datasources.get(ctx.params.datasourceId)
|
||||||
|
@ -311,51 +382,3 @@ export async function query(ctx: UserCtx) {
|
||||||
ctx.throw(400, err)
|
ctx.throw(400, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getErrorTables(errors: any, errorType: string) {
|
|
||||||
return Object.entries(errors)
|
|
||||||
.filter(entry => entry[1] === errorType)
|
|
||||||
.map(([name]) => name)
|
|
||||||
}
|
|
||||||
|
|
||||||
function updateError(error: any, newError: any, tables: string[]) {
|
|
||||||
if (!error) {
|
|
||||||
error = ""
|
|
||||||
}
|
|
||||||
if (error.length > 0) {
|
|
||||||
error += "\n"
|
|
||||||
}
|
|
||||||
error += `${newError} ${tables.join(", ")}`
|
|
||||||
return error
|
|
||||||
}
|
|
||||||
|
|
||||||
async function buildSchemaHelper(datasource: Datasource) {
|
|
||||||
const Connector = await getIntegration(datasource.source)
|
|
||||||
datasource = await sdk.datasources.enrich(datasource)
|
|
||||||
// Connect to the DB and build the schema
|
|
||||||
const connector = new Connector(datasource.config)
|
|
||||||
await connector.buildSchema(datasource._id, datasource.entities)
|
|
||||||
|
|
||||||
const errors = connector.schemaErrors
|
|
||||||
let error = null
|
|
||||||
if (errors && Object.keys(errors).length > 0) {
|
|
||||||
const noKey = getErrorTables(errors, BuildSchemaErrors.NO_KEY)
|
|
||||||
const invalidCol = getErrorTables(errors, BuildSchemaErrors.INVALID_COLUMN)
|
|
||||||
if (noKey.length) {
|
|
||||||
error = updateError(
|
|
||||||
error,
|
|
||||||
"No primary key constraint found for the following:",
|
|
||||||
noKey
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if (invalidCol.length) {
|
|
||||||
const invalidCols = Object.values(InvalidColumns).join(", ")
|
|
||||||
error = updateError(
|
|
||||||
error,
|
|
||||||
`Cannot use columns ${invalidCols} found in following:`,
|
|
||||||
invalidCol
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return { tables: connector.tables, error }
|
|
||||||
}
|
|
||||||
|
|
|
@ -15,6 +15,11 @@ router
|
||||||
authorized(permissions.BUILDER),
|
authorized(permissions.BUILDER),
|
||||||
datasourceController.fetch
|
datasourceController.fetch
|
||||||
)
|
)
|
||||||
|
.post(
|
||||||
|
"/api/datasources/verify",
|
||||||
|
authorized(permissions.BUILDER),
|
||||||
|
datasourceController.verify
|
||||||
|
)
|
||||||
.get(
|
.get(
|
||||||
"/api/datasources/:datasourceId",
|
"/api/datasources/:datasourceId",
|
||||||
authorized(
|
authorized(
|
||||||
|
|
|
@ -14,6 +14,14 @@ export interface CreateDatasourceRequest {
|
||||||
fetchSchema?: boolean
|
fetchSchema?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface VerifyDatasourceRequest {
|
||||||
|
datasource: Datasource
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface VerifyDatasourceResponse {
|
||||||
|
connected: boolean
|
||||||
|
}
|
||||||
|
|
||||||
export interface UpdateDatasourceRequest extends Datasource {
|
export interface UpdateDatasourceRequest extends Datasource {
|
||||||
datasource: Datasource
|
datasource: Datasource
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue