diff --git a/packages/server/src/api/controllers/datasource.ts b/packages/server/src/api/controllers/datasource.ts index 8f13e0e618..5c2c4ef684 100644 --- a/packages/server/src/api/controllers/datasource.ts +++ b/packages/server/src/api/controllers/datasource.ts @@ -21,6 +21,7 @@ import { CreateDatasourceRequest, VerifyDatasourceRequest, VerifyDatasourceResponse, + FetchTablesDatasourceResponse, IntegrationBase, DatasourcePlus, } from "@budibase/types" @@ -153,6 +154,21 @@ export async function verify( } } +export async function tables( + ctx: UserCtx +) { + 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) { const db = context.getAppDB() const datasource = await sdk.datasources.get(ctx.params.datasourceId) diff --git a/packages/server/src/api/routes/datasource.ts b/packages/server/src/api/routes/datasource.ts index 654fb794e3..89bc12c543 100644 --- a/packages/server/src/api/routes/datasource.ts +++ b/packages/server/src/api/routes/datasource.ts @@ -20,6 +20,11 @@ router authorized(permissions.BUILDER), datasourceController.verify ) + .get( + "/api/datasources/:datasourceId/tables", + authorized(permissions.BUILDER), + datasourceController.tables + ) .get( "/api/datasources/:datasourceId", authorized( diff --git a/packages/types/src/api/web/app/datasource.ts b/packages/types/src/api/web/app/datasource.ts index 983fd45b92..12c9753aa1 100644 --- a/packages/types/src/api/web/app/datasource.ts +++ b/packages/types/src/api/web/app/datasource.ts @@ -23,6 +23,10 @@ export interface VerifyDatasourceResponse { error?: string } +export interface FetchTablesDatasourceResponse { + tables: string[] +} + export interface UpdateDatasourceRequest extends Datasource { datasource: Datasource }