Adding API for retrieving table names.

This commit is contained in:
Michael Drury 2023-05-18 23:22:52 +01:00
parent 732ebb4f87
commit 2223027d28
3 changed files with 25 additions and 0 deletions

View File

@ -21,6 +21,7 @@ import {
CreateDatasourceRequest, CreateDatasourceRequest,
VerifyDatasourceRequest, VerifyDatasourceRequest,
VerifyDatasourceResponse, VerifyDatasourceResponse,
FetchTablesDatasourceResponse,
IntegrationBase, IntegrationBase,
DatasourcePlus, DatasourcePlus,
} from "@budibase/types" } from "@budibase/types"
@ -153,6 +154,21 @@ export async function verify(
} }
} }
export async function tables(
ctx: UserCtx<void, FetchTablesDatasourceResponse>
) {
const datasourceId = ctx.params.datasourceId
const datasource = await sdk.datasources.get(datasourceId, { enriched: true })
const connector = (await getConnector(datasource)) as DatasourcePlus
if (!connector.getTableNames) {
ctx.throw(400, "Table name fetching not supported by datasource")
}
const tables = await connector.getTableNames()
ctx.body = {
tables,
}
}
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)

View File

@ -20,6 +20,11 @@ router
authorized(permissions.BUILDER), authorized(permissions.BUILDER),
datasourceController.verify datasourceController.verify
) )
.get(
"/api/datasources/:datasourceId/tables",
authorized(permissions.BUILDER),
datasourceController.tables
)
.get( .get(
"/api/datasources/:datasourceId", "/api/datasources/:datasourceId",
authorized( authorized(

View File

@ -23,6 +23,10 @@ export interface VerifyDatasourceResponse {
error?: string error?: string
} }
export interface FetchTablesDatasourceResponse {
tables: string[]
}
export interface UpdateDatasourceRequest extends Datasource { export interface UpdateDatasourceRequest extends Datasource {
datasource: Datasource datasource: Datasource
} }